MediaWiki  1.27.2
FeedUtils.php
Go to the documentation of this file.
1 <?php
29 class FeedUtils {
30 
38  public static function checkPurge( $timekey, $key ) {
40 
41  $purge = $wgRequest->getVal( 'action' ) === 'purge';
42  if ( $purge && $wgUser->isAllowed( 'purge' ) ) {
44  $cache->delete( $timekey, 1 );
45  $cache->delete( $key, 1 );
46  }
47  }
48 
55  public static function checkFeedOutput( $type ) {
56  global $wgOut, $wgFeed, $wgFeedClasses;
57 
58  if ( !$wgFeed ) {
59  $wgOut->addWikiMsg( 'feed-unavailable' );
60  return false;
61  }
62 
63  if ( !isset( $wgFeedClasses[$type] ) ) {
64  $wgOut->addWikiMsg( 'feed-invalid' );
65  return false;
66  }
67 
68  return true;
69  }
70 
77  public static function formatDiff( $row ) {
78  $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
79  $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
80  $actiontext = '';
81  if ( $row->rc_type == RC_LOG ) {
82  $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
83  $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
84  }
85  return self::formatDiffRow( $titleObj,
86  $row->rc_last_oldid, $row->rc_this_oldid,
87  $timestamp,
88  $row->rc_deleted & Revision::DELETED_COMMENT
89  ? wfMessage( 'rev-deleted-comment' )->escaped()
90  : $row->rc_comment,
91  $actiontext
92  );
93  }
94 
106  public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
107  $comment, $actiontext = ''
108  ) {
109  global $wgFeedDiffCutoff, $wgLang;
110 
111  // log entries
112  $completeText = '<p>' . implode( ' ',
113  array_filter(
114  [
115  $actiontext,
116  Linker::formatComment( $comment ) ] ) ) . "</p>\n";
117 
118  // NOTE: Check permissions for anonymous users, not current user.
119  // No "privileged" version should end up in the cache.
120  // Most feed readers will not log in anyway.
121  $anon = new User();
122  $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
123 
124  // Can't diff special pages, unreadable pages or pages with no new revision
125  // to compare against: just return the text.
126  if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
127  return $completeText;
128  }
129 
130  if ( $oldid ) {
131 
132  # $diffText = $de->getDiff( wfMessage( 'revisionasof',
133  # $wgLang->timeanddate( $timestamp ),
134  # $wgLang->date( $timestamp ),
135  # $wgLang->time( $timestamp ) )->text(),
136  # wfMessage( 'currentrev' )->text() );
137 
138  $diffText = '';
139  // Don't bother generating the diff if we won't be able to show it
140  if ( $wgFeedDiffCutoff > 0 ) {
141  $rev = Revision::newFromId( $oldid );
142 
143  if ( !$rev ) {
144  $diffText = false;
145  } else {
147  $context->setTitle( $title );
148 
149  $contentHandler = $rev->getContentHandler();
150  $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
151  $diffText = $de->getDiff(
152  wfMessage( 'previousrevision' )->text(), // hack
153  wfMessage( 'revisionasof',
154  $wgLang->timeanddate( $timestamp ),
155  $wgLang->date( $timestamp ),
156  $wgLang->time( $timestamp ) )->text() );
157  }
158  }
159 
160  if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
161  // Omit large diffs
162  $diffText = self::getDiffLink( $title, $newid, $oldid );
163  } elseif ( $diffText === false ) {
164  // Error in diff engine, probably a missing revision
165  $diffText = "<p>Can't load revision $newid</p>";
166  } else {
167  // Diff output fine, clean up any illegal UTF-8
168  $diffText = UtfNormal\Validator::cleanUp( $diffText );
169  $diffText = self::applyDiffStyle( $diffText );
170  }
171  } else {
172  $rev = Revision::newFromId( $newid );
173  if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
174  $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
175  } else {
176  $newContent = $rev->getContent();
177  }
178 
179  if ( $newContent instanceof TextContent ) {
180  // only textual content has a "source view".
181  $text = $newContent->getNativeData();
182 
183  if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
184  $html = null;
185  } else {
186  $html = nl2br( htmlspecialchars( $text ) );
187  }
188  } else {
189  // XXX: we could get an HTML representation of the content via getParserOutput, but that may
190  // contain JS magic and generally may not be suitable for inclusion in a feed.
191  // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
192  // Compare also ApiFeedContributions::feedItemDesc
193  $html = null;
194  }
195 
196  if ( $html === null ) {
197 
198  // Omit large new page diffs, bug 29110
199  // Also use diff link for non-textual content
200  $diffText = self::getDiffLink( $title, $newid );
201  } else {
202  $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
203  '<div>' . $html . '</div>';
204  }
205  }
206  $completeText .= $diffText;
207 
208  return $completeText;
209  }
210 
220  protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
221  $queryParameters = [ 'diff' => $newid ];
222  if ( $oldid != null ) {
223  $queryParameters['oldid'] = $oldid;
224  }
225  $diffUrl = $title->getFullURL( $queryParameters );
226 
227  $diffLink = Html::element( 'a', [ 'href' => $diffUrl ],
228  wfMessage( 'showdiff' )->inContentLanguage()->text() );
229 
230  return $diffLink;
231  }
232 
241  public static function applyDiffStyle( $text ) {
242  $styles = [
243  'diff' => 'background-color: white; color:black;',
244  'diff-otitle' => 'background-color: white; color:black; text-align: center;',
245  'diff-ntitle' => 'background-color: white; color:black; text-align: center;',
246  'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; '
247  . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
248  . 'vertical-align: top; white-space: pre-wrap;',
249  'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; '
250  . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
251  . 'vertical-align: top; white-space: pre-wrap;',
252  'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; '
253  . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
254  . 'border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
255  'diffchange' => 'font-weight: bold; text-decoration: none;',
256  ];
257 
258  foreach ( $styles as $class => $style ) {
259  $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
260  "\\1style=\"$style\"\\3", $text );
261  }
262 
263  return $text;
264  }
265 
266 }
static getMainWANInstance()
Get the main WAN cache object.
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:1798
the array() calling protocol came about after MediaWiki 1.4rc1.
static newFromRow($row)
Handy shortcut for constructing a formatter directly from database row.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2321
static checkFeedOutput($type)
Check whether feeds can be used and that $type is a valid feed type.
Definition: FeedUtils.php:55
$context
Definition: load.php:44
static getDiffLink(Title $title, $newid, $oldid=null)
Generates a diff link.
Definition: FeedUtils.php:220
$comment
Represents a title within MediaWiki.
Definition: Title.php:34
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static applyDiffStyle($text)
Hacky application of diff styles for the feeds.
Definition: FeedUtils.php:241
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Content object implementation for representing flat text.
Definition: TextContent.php:35
static getMain()
Static methods.
if($limit) $timestamp
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 after processing after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation 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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
$cache
Definition: mcc.php:33
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:912
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
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:1584
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 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:1290
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
static newFromId($id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:99
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
Helper functions for feeds.
Definition: FeedUtils.php:29
static formatDiffRow($title, $oldid, $newid, $timestamp, $comment, $actiontext= '')
Really format a diff for the newsfeed.
Definition: FeedUtils.php:106
$wgOut
Definition: Setup.php:804
const DELETED_COMMENT
Definition: Revision.php:77
static checkPurge($timekey, $key)
Check whether feed's cache should be cleared; for changes feeds If the feed should be purged; $timeke...
Definition: FeedUtils.php:38
static element($element, $attribs=[], $contents= '')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:230
if(is_null($wgLocalTZoffset)) if(!$wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:657
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2338
static & makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:524
static formatDiff($row)
Format a diff for the newsfeed.
Definition: FeedUtils.php:77
const RC_LOG
Definition: Defines.php:171
getFullURL($query= '', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition: Title.php:1666
$wgUser
Definition: Setup.php:794