MediaWiki  master
FeedUtils.php
Go to the documentation of this file.
1 <?php
27 
33 class FeedUtils {
34 
41  public static function checkFeedOutput( $type ) {
42  global $wgOut, $wgFeed, $wgFeedClasses;
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  ) {
96  global $wgFeedDiffCutoff, $wgLang;
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 }
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1808
FeedUtils\formatDiffRow
static formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='')
Really format a diff for the newsfeed.
Definition: FeedUtils.php:93
RC_LOG
const RC_LOG
Definition: Defines.php:133
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1219
LogFormatter\newFromRow
static newFromRow( $row)
Handy shortcut for constructing a formatter directly from database row.
Definition: LogFormatter.php:72
$wgLang
$wgLang
Definition: Setup.php:776
$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:592
$wgFeedClasses
$wgFeedClasses
Available feeds objects.
Definition: DefaultSettings.php:7408
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:7387
FeedUtils\getDiffLink
static getDiffLink(Title $title, $newid, $oldid=null)
Generates a diff link.
Definition: FeedUtils.php:213
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:453
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:1199
FeedUtils\formatDiff
static formatDiff( $row)
Format a diff for the newsfeed.
Definition: FeedUtils.php:64
Title
Represents a title within MediaWiki.
Definition: Title.php:42
FeedUtils\applyDiffStyle
static applyDiffStyle( $text)
Hacky application of diff styles for the feeds.
Definition: FeedUtils.php:234
FeedUtils\checkFeedOutput
static checkFeedOutput( $type)
Check whether feeds can be used and that $type is a valid feed type.
Definition: FeedUtils.php:41
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
$wgOut
$wgOut
Definition: Setup.php:781
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:55
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
$wgFeed
$wgFeed
Provide syndication feeds (RSS, Atom) for, e.g., Recentchanges, Newpages.
Definition: DefaultSettings.php:7365
$type
$type
Definition: testCompression.php:52