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