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