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