MediaWiki REL1_35
FeedUtils.php
Go to the documentation of this file.
1<?php
27
33class FeedUtils {
34
41 public static function checkFeedOutput( $type ) {
43
44 if ( !$wgFeed ) {
45 $wgOut->addWikiMsg( 'feed-unavailable' );
46 return false;
47 }
48
49 if ( !isset( $wgFeedClasses[$type] ) ) {
50 $wgOut->addWikiMsg( 'feed-invalid' );
51 return false;
52 }
53
54 return true;
55 }
56
64 public static function formatDiff( $row ) {
65 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
66 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
67 $actiontext = '';
68 if ( $row->rc_type == RC_LOG ) {
69 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
70 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
71 }
72 return self::formatDiffRow( $titleObj,
73 $row->rc_last_oldid, $row->rc_this_oldid,
74 $timestamp,
75 $row->rc_deleted & RevisionRecord::DELETED_COMMENT
76 ? wfMessage( 'rev-deleted-comment' )->escaped()
77 : CommentStore::getStore()->getComment( 'rc_comment', $row )->text,
78 $actiontext
79 );
80 }
81
93 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
94 $comment, $actiontext = ''
95 ) {
97
98 // log entries
99 $completeText = '<p>' . implode( ' ',
100 array_filter(
101 [
102 $actiontext,
103 Linker::formatComment( $comment ) ] ) ) . "</p>\n";
104
105 // NOTE: Check permissions for anonymous users, not current user.
106 // No "privileged" version should end up in the cache.
107 // Most feed readers will not log in anyway.
108 $anon = new User();
109 $services = MediaWikiServices::getInstance();
110 $permManager = $services->getPermissionManager();
111 $accErrors = $permManager->getPermissionErrors(
112 'read',
113 $anon,
114 $title
115 );
116
117 // Can't diff special pages, unreadable pages or pages with no new revision
118 // to compare against: just return the text.
119 if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
120 return $completeText;
121 }
122
123 $revLookup = $services->getRevisionLookup();
124 $contentHandlerFactory = $services->getContentHandlerFactory();
125 if ( $oldid ) {
126 $diffText = '';
127 // Don't bother generating the diff if we won't be able to show it
128 if ( $wgFeedDiffCutoff > 0 ) {
129 $revRecord = $revLookup->getRevisionById( $oldid );
130
131 if ( !$revRecord ) {
132 $diffText = false;
133 } else {
134 $context = clone RequestContext::getMain();
135 $context->setTitle( $title );
136
137 $model = $revRecord->getSlot(
138 SlotRecord::MAIN,
139 RevisionRecord::RAW
140 )->getModel();
141 $contentHandler = $contentHandlerFactory->getContentHandler( $model );
142 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
143 $diffText = $de->getDiff(
144 wfMessage( 'previousrevision' )->text(), // hack
145 wfMessage( 'revisionasof',
146 $wgLang->timeanddate( $timestamp ),
147 $wgLang->date( $timestamp ),
148 $wgLang->time( $timestamp ) )->text() );
149 }
150 }
151
152 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
153 // Omit large diffs
154 $diffText = self::getDiffLink( $title, $newid, $oldid );
155 } elseif ( $diffText === false ) {
156 // Error in diff engine, probably a missing revision
157 $diffText = "<p>Can't load revision $newid</p>";
158 } else {
159 // Diff output fine, clean up any illegal UTF-8
160 $diffText = UtfNormal\Validator::cleanUp( $diffText );
161 $diffText = self::applyDiffStyle( $diffText );
162 }
163 } else {
164 $revRecord = $revLookup->getRevisionById( $newid );
165 if ( $wgFeedDiffCutoff <= 0 || $revRecord === null ) {
166 $newContent = $contentHandlerFactory
167 ->getContentHandler( $title->getContentModel() )
168 ->makeEmptyContent();
169 } else {
170 $newContent = $revRecord->getContent( SlotRecord::MAIN );
171 }
172
173 if ( $newContent instanceof TextContent ) {
174 // only textual content has a "source view".
175 $text = $newContent->getText();
176
177 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
178 $html = null;
179 } else {
180 $html = nl2br( htmlspecialchars( $text ) );
181 }
182 } else {
183 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
184 // contain JS magic and generally may not be suitable for inclusion in a feed.
185 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
186 // Compare also ApiFeedContributions::feedItemDesc
187 $html = null;
188 }
189
190 if ( $html === null ) {
191 // Omit large new page diffs, T31110
192 // Also use diff link for non-textual content
193 $diffText = self::getDiffLink( $title, $newid );
194 } else {
195 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
196 '<div>' . $html . '</div>';
197 }
198 }
199 $completeText .= $diffText;
200
201 return $completeText;
202 }
203
213 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
214 $queryParameters = [ 'diff' => $newid ];
215 if ( $oldid != null ) {
216 $queryParameters['oldid'] = $oldid;
217 }
218 $diffUrl = $title->getFullURL( $queryParameters );
219
220 $diffLink = Html::element( 'a', [ 'href' => $diffUrl ],
221 wfMessage( 'showdiff' )->inContentLanguage()->text() );
222
223 return $diffLink;
224 }
225
234 public static function applyDiffStyle( $text ) {
235 $styles = [
236 'diff' => 'background-color: #fff; color: #202122;',
237 'diff-otitle' => 'background-color: #fff; color: #202122; text-align: center;',
238 'diff-ntitle' => 'background-color: #fff; color: #202122; text-align: center;',
239 'diff-addedline' => 'color: #202122; font-size: 88%; border-style: solid; '
240 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
241 . 'vertical-align: top; white-space: pre-wrap;',
242 'diff-deletedline' => 'color: #202122; font-size: 88%; border-style: solid; '
243 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
244 . 'vertical-align: top; white-space: pre-wrap;',
245 'diff-context' => 'background-color: #f8f9fa; color: #202122; font-size: 88%; '
246 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
247 . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
248 'diffchange' => 'font-weight: bold; text-decoration: none;',
249 ];
250
251 foreach ( $styles as $class => $style ) {
252 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
253 "\\1style=\"$style\"\\3", $text );
254 }
255
256 return $text;
257 }
258
259}
$wgFeedDiffCutoff
When generating Recentchanges RSS/Atom feed, diffs will not be generated for pages larger than this s...
$wgFeedClasses
Available feeds objects.
$wgFeed
Provide syndication feeds (RSS, Atom) for, e.g., Recentchanges, Newpages.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$wgOut
Definition Setup.php:786
$wgLang
Definition Setup.php:781
Helper functions for feeds.
Definition FeedUtils.php:33
static formatDiff( $row)
Format a diff for the newsfeed.
Definition FeedUtils.php:64
static checkFeedOutput( $type)
Check whether feeds can be used and that $type is a valid feed type.
Definition FeedUtils.php:41
static formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='')
Really format a diff for the newsfeed.
Definition FeedUtils.php:93
static applyDiffStyle( $text)
Hacky application of diff styles for the feeds.
static getDiffLink(Title $title, $newid, $oldid=null)
Generates a diff link.
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition Linker.php:1209
static newFromRow( $row)
Handy shortcut for constructing a formatter directly from database row.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
Value object representing a content slot associated with a page revision.
Content object implementation for representing flat text.
Represents a title within MediaWiki.
Definition Title.php:42
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:60
const RC_LOG
Definition Defines.php:134