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 
75  public static function formatDiff( $row, $formattedComment = null ) {
76  $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
77  $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
78  $actiontext = '';
79  if ( $row->rc_type == RC_LOG ) {
80  $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
81  $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
82  }
83  if ( $row->rc_deleted & RevisionRecord::DELETED_COMMENT ) {
84  $formattedComment = wfMessage( 'rev-deleted-comment' )->escaped();
85  } elseif ( $formattedComment === null ) {
86  $formattedComment = Linker::formatComment(
87  CommentStore::getStore()->getComment( 'rc_comment', $row )->text );
88  }
89  return self::formatDiffRow2( $titleObj,
90  $row->rc_last_oldid, $row->rc_this_oldid,
91  $timestamp,
92  $formattedComment,
93  $actiontext
94  );
95  }
96 
110  public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
111  $comment, $actiontext = ''
112  ) {
113  $formattedComment = MediaWikiServices::getInstance()->getCommentFormatter()
114  ->format( $comment );
115  return self::formatDiffRow2( $title, $oldid, $newid, $timestamp,
116  $formattedComment, $actiontext );
117  }
118 
131  public static function formatDiffRow2( $title, $oldid, $newid, $timestamp,
132  $formattedComment, $actiontext = ''
133  ) {
134  global $wgFeedDiffCutoff;
135 
136  // log entries
137  $unwrappedText = implode(
138  ' ',
139  array_filter( [ $actiontext, $formattedComment ] )
140  );
141  $completeText = Html::rawElement( 'p', [], $unwrappedText ) . "\n";
142 
143  // NOTE: Check permissions for anonymous users, not current user.
144  // No "privileged" version should end up in the cache.
145  // Most feed readers will not log in anyway.
146  $anon = new User();
147  $services = MediaWikiServices::getInstance();
148  $permManager = $services->getPermissionManager();
149  $accErrors = $permManager->getPermissionErrors(
150  'read',
151  $anon,
152  $title
153  );
154 
155  // Can't diff special pages, unreadable pages or pages with no new revision
156  // to compare against: just return the text.
157  if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
158  return $completeText;
159  }
160 
161  $revLookup = $services->getRevisionLookup();
162  $contentHandlerFactory = $services->getContentHandlerFactory();
163  if ( $oldid ) {
164  $diffText = '';
165  // Don't bother generating the diff if we won't be able to show it
166  if ( $wgFeedDiffCutoff > 0 ) {
167  $revRecord = $revLookup->getRevisionById( $oldid );
168 
169  if ( !$revRecord ) {
170  $diffText = false;
171  } else {
172  $mainContext = RequestContext::getMain();
173  $context = clone RequestContext::getMain();
174  $context->setTitle( $title );
175 
176  $model = $revRecord->getSlot(
177  SlotRecord::MAIN,
178  RevisionRecord::RAW
179  )->getModel();
180  $contentHandler = $contentHandlerFactory->getContentHandler( $model );
181  $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
182  $lang = $mainContext->getLanguage();
183  $user = $mainContext->getUser();
184  $diffText = $de->getDiff(
185  $mainContext->msg( 'previousrevision' )->text(), // hack
186  $mainContext->msg( 'revisionasof',
187  $lang->userTimeAndDate( $timestamp, $user ),
188  $lang->userDate( $timestamp, $user ),
189  $lang->userTime( $timestamp, $user ) )->text() );
190  }
191  }
192 
193  if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
194  // Omit large diffs
195  $diffText = self::getDiffLink( $title, $newid, $oldid );
196  } elseif ( $diffText === false ) {
197  // Error in diff engine, probably a missing revision
198  $diffText = Html::rawElement(
199  'p',
200  [],
201  "Can't load revision $newid"
202  );
203  } else {
204  // Diff output fine, clean up any illegal UTF-8
205  $diffText = UtfNormal\Validator::cleanUp( $diffText );
206  $diffText = self::applyDiffStyle( $diffText );
207  }
208  } else {
209  $revRecord = $revLookup->getRevisionById( $newid );
210  if ( $wgFeedDiffCutoff <= 0 || $revRecord === null ) {
211  $newContent = $contentHandlerFactory
212  ->getContentHandler( $title->getContentModel() )
213  ->makeEmptyContent();
214  } else {
215  $newContent = $revRecord->getContent( SlotRecord::MAIN );
216  }
217 
218  if ( $newContent instanceof TextContent ) {
219  // only textual content has a "source view".
220  $text = $newContent->getText();
221 
222  if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
223  $html = null;
224  } else {
225  $html = nl2br( htmlspecialchars( $text ) );
226  }
227  } else {
228  // XXX: we could get an HTML representation of the content via getParserOutput, but that may
229  // contain JS magic and generally may not be suitable for inclusion in a feed.
230  // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
231  // Compare also ApiFeedContributions::feedItemDesc
232  $html = null;
233  }
234 
235  if ( $html === null ) {
236  // Omit large new page diffs, T31110
237  // Also use diff link for non-textual content
238  $diffText = self::getDiffLink( $title, $newid );
239  } else {
240  $diffText = Html::rawElement(
241  'p',
242  [],
243  Html::rawElement( 'b', [], wfMessage( 'newpage' )->text() )
244  );
245  $diffText .= Html::rawElement( 'div', [], $html );
246  }
247  }
248  $completeText .= $diffText;
249 
250  return $completeText;
251  }
252 
262  protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
263  $queryParameters = [ 'diff' => $newid ];
264  if ( $oldid != null ) {
265  $queryParameters['oldid'] = $oldid;
266  }
267  $diffUrl = $title->getFullURL( $queryParameters );
268 
269  $diffLink = Html::element( 'a', [ 'href' => $diffUrl ],
270  wfMessage( 'showdiff' )->inContentLanguage()->text() );
271 
272  return $diffLink;
273  }
274 
283  public static function applyDiffStyle( $text ) {
284  $styles = [
285  'diff' => 'background-color: #fff; color: #202122;',
286  'diff-otitle' => 'background-color: #fff; color: #202122; text-align: center;',
287  'diff-ntitle' => 'background-color: #fff; color: #202122; text-align: center;',
288  'diff-addedline' => 'color: #202122; font-size: 88%; border-style: solid; '
289  . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
290  . 'vertical-align: top; white-space: pre-wrap;',
291  'diff-deletedline' => 'color: #202122; font-size: 88%; border-style: solid; '
292  . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
293  . 'vertical-align: top; white-space: pre-wrap;',
294  'diff-context' => 'background-color: #f8f9fa; color: #202122; font-size: 88%; '
295  . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
296  . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
297  'diffchange' => 'font-weight: bold; text-decoration: none;',
298  ];
299 
300  foreach ( $styles as $class => $style ) {
301  $text = preg_replace( '/(<\w+\b[^<>]*)\bclass=([\'"])(?:[^\'"]*\s)?' .
302  preg_quote( $class ) . '(?:\s[^\'"]*)?\2(?=[^<>]*>)/',
303  '$1style="' . $style . '"', $text );
304  }
305 
306  return $text;
307  }
308 
309 }
MediaWiki\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:202
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1665
FeedUtils\formatDiffRow
static formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='')
Really format a diff for the newsfeed.
Definition: FeedUtils.php:110
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1183
FeedUtils\formatDiff
static formatDiff( $row, $formattedComment=null)
Format a diff for the newsfeed.
Definition: FeedUtils.php:75
RC_LOG
const RC_LOG
Definition: Defines.php:117
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:666
$wgFeedClasses
$wgFeedClasses
Available feeds objects.
Definition: DefaultSettings.php:8092
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:8071
FeedUtils\getDiffLink
static getDiffLink(Title $title, $newid, $oldid=null)
Generates a diff link.
Definition: FeedUtils.php:262
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:484
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:1398
Title
Represents a title within MediaWiki.
Definition: Title.php:47
FeedUtils\applyDiffStyle
static applyDiffStyle( $text)
Hacky application of diff styles for the feeds.
Definition: FeedUtils.php:283
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
$wgOut
$wgOut
Definition: Setup.php:862
FeedUtils\formatDiffRow2
static formatDiffRow2( $title, $oldid, $newid, $timestamp, $formattedComment, $actiontext='')
Really really format a diff for the newsfeed.
Definition: FeedUtils.php:131
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:67
MediaWiki\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:8049
$type
$type
Definition: testCompression.php:52