MediaWiki  master
FeedUtils.php
Go to the documentation of this file.
1 <?php
29 class FeedUtils {
30 
37  public static function checkFeedOutput( $type ) {
38  global $wgOut, $wgFeed, $wgFeedClasses;
39 
40  if ( !$wgFeed ) {
41  $wgOut->addWikiMsg( 'feed-unavailable' );
42  return false;
43  }
44 
45  if ( !isset( $wgFeedClasses[$type] ) ) {
46  $wgOut->addWikiMsg( 'feed-invalid' );
47  return false;
48  }
49 
50  return true;
51  }
52 
60  public static function formatDiff( $row ) {
61  $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
62  $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
63  $actiontext = '';
64  if ( $row->rc_type == RC_LOG ) {
65  $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
66  $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
67  }
68  return self::formatDiffRow( $titleObj,
69  $row->rc_last_oldid, $row->rc_this_oldid,
70  $timestamp,
71  $row->rc_deleted & Revision::DELETED_COMMENT
72  ? wfMessage( 'rev-deleted-comment' )->escaped()
73  : CommentStore::getStore()->getComment( 'rc_comment', $row )->text,
74  $actiontext
75  );
76  }
77 
89  public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
90  $comment, $actiontext = ''
91  ) {
92  global $wgFeedDiffCutoff, $wgLang;
93 
94  // log entries
95  $completeText = '<p>' . implode( ' ',
96  array_filter(
97  [
98  $actiontext,
99  Linker::formatComment( $comment ) ] ) ) . "</p>\n";
100 
101  // NOTE: Check permissions for anonymous users, not current user.
102  // No "privileged" version should end up in the cache.
103  // Most feed readers will not log in anyway.
104  $anon = new User();
105  $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
106 
107  // Can't diff special pages, unreadable pages or pages with no new revision
108  // to compare against: just return the text.
109  if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
110  return $completeText;
111  }
112 
113  if ( $oldid ) {
114  $diffText = '';
115  // Don't bother generating the diff if we won't be able to show it
116  if ( $wgFeedDiffCutoff > 0 ) {
117  $rev = Revision::newFromId( $oldid );
118 
119  if ( !$rev ) {
120  $diffText = false;
121  } else {
123  $context->setTitle( $title );
124 
125  $contentHandler = $rev->getContentHandler();
126  $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
127  $diffText = $de->getDiff(
128  wfMessage( 'previousrevision' )->text(), // hack
129  wfMessage( 'revisionasof',
130  $wgLang->timeanddate( $timestamp ),
131  $wgLang->date( $timestamp ),
132  $wgLang->time( $timestamp ) )->text() );
133  }
134  }
135 
136  if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
137  // Omit large diffs
138  $diffText = self::getDiffLink( $title, $newid, $oldid );
139  } elseif ( $diffText === false ) {
140  // Error in diff engine, probably a missing revision
141  $diffText = "<p>Can't load revision $newid</p>";
142  } else {
143  // Diff output fine, clean up any illegal UTF-8
144  $diffText = UtfNormal\Validator::cleanUp( $diffText );
145  $diffText = self::applyDiffStyle( $diffText );
146  }
147  } else {
148  $rev = Revision::newFromId( $newid );
149  if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
150  $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
151  } else {
152  $newContent = $rev->getContent();
153  }
154 
155  if ( $newContent instanceof TextContent ) {
156  // only textual content has a "source view".
157  $text = $newContent->getText();
158 
159  if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
160  $html = null;
161  } else {
162  $html = nl2br( htmlspecialchars( $text ) );
163  }
164  } else {
165  // XXX: we could get an HTML representation of the content via getParserOutput, but that may
166  // contain JS magic and generally may not be suitable for inclusion in a feed.
167  // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
168  // Compare also ApiFeedContributions::feedItemDesc
169  $html = null;
170  }
171 
172  if ( $html === null ) {
173  // Omit large new page diffs, T31110
174  // Also use diff link for non-textual content
175  $diffText = self::getDiffLink( $title, $newid );
176  } else {
177  $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
178  '<div>' . $html . '</div>';
179  }
180  }
181  $completeText .= $diffText;
182 
183  return $completeText;
184  }
185 
195  protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
196  $queryParameters = [ 'diff' => $newid ];
197  if ( $oldid != null ) {
198  $queryParameters['oldid'] = $oldid;
199  }
200  $diffUrl = $title->getFullURL( $queryParameters );
201 
202  $diffLink = Html::element( 'a', [ 'href' => $diffUrl ],
203  wfMessage( 'showdiff' )->inContentLanguage()->text() );
204 
205  return $diffLink;
206  }
207 
216  public static function applyDiffStyle( $text ) {
217  $styles = [
218  'diff' => 'background-color: #fff; color: #222;',
219  'diff-otitle' => 'background-color: #fff; color: #222; text-align: center;',
220  'diff-ntitle' => 'background-color: #fff; color: #222; text-align: center;',
221  'diff-addedline' => 'color: #222; font-size: 88%; border-style: solid; '
222  . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
223  . 'vertical-align: top; white-space: pre-wrap;',
224  'diff-deletedline' => 'color: #222; font-size: 88%; border-style: solid; '
225  . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
226  . 'vertical-align: top; white-space: pre-wrap;',
227  'diff-context' => 'background-color: #f8f9fa; color: #222; font-size: 88%; '
228  . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
229  . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
230  'diffchange' => 'font-weight: bold; text-decoration: none;',
231  ];
232 
233  foreach ( $styles as $class => $style ) {
234  $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
235  "\\1style=\"$style\"\\3", $text );
236  }
237 
238  return $text;
239  }
240 
241 }
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition: hooks.txt:1982
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
static getDiffLink(Title $title, $newid, $oldid=null)
Generates a diff link.
Definition: FeedUtils.php:195
static formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='')
Really format a diff for the newsfeed.
Definition: FeedUtils.php:89
$wgFeed
Provide syndication feeds (RSS, Atom) for, e.g., Recentchanges, Newpages.
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
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:931
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
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:1160
static applyDiffStyle( $text)
Hacky application of diff styles for the feeds.
Definition: FeedUtils.php:216
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition: Title.php:1924
static newFromRow( $row)
Handy shortcut for constructing a formatter directly from database row.
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1766
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:589
static formatDiff( $row)
Format a diff for the newsfeed.
Definition: FeedUtils.php:60
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static getStore()
Helper functions for feeds.
Definition: FeedUtils.php:29
static checkFeedOutput( $type)
Check whether feeds can be used and that $type is a valid feed type.
Definition: FeedUtils.php:37
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition: hooks.txt:2633
$wgOut
Definition: Setup.php:936
const DELETED_COMMENT
Definition: Revision.php:47
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:118
const RC_LOG
Definition: Defines.php:140
$wgFeedClasses
Available feeds objects.