MediaWiki  1.23.3
FeedUtils.php
Go to the documentation of this file.
1 <?php
29 class FeedUtils {
30 
39  public static function checkPurge( $timekey, $key ) {
40  global $wgRequest, $wgUser, $messageMemc;
41  $purge = $wgRequest->getVal( 'action' ) === 'purge';
42  if ( $purge && $wgUser->isAllowed( 'purge' ) ) {
43  $messageMemc->delete( $timekey );
44  $messageMemc->delete( $key );
45  }
46  }
47 
54  public static function checkFeedOutput( $type ) {
55  global $wgOut, $wgFeed, $wgFeedClasses;
56 
57  if ( !$wgFeed ) {
58  $wgOut->addWikiMsg( 'feed-unavailable' );
59  return false;
60  }
61 
62  if ( !isset( $wgFeedClasses[$type] ) ) {
63  $wgOut->addWikiMsg( 'feed-invalid' );
64  return false;
65  }
66 
67  return true;
68  }
69 
76  public static function formatDiff( $row ) {
77  $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
78  $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
79  $actiontext = '';
80  if ( $row->rc_type == RC_LOG ) {
81  $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
82  $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
83  }
84  return self::formatDiffRow( $titleObj,
85  $row->rc_last_oldid, $row->rc_this_oldid,
86  $timestamp,
87  $row->rc_deleted & Revision::DELETED_COMMENT
88  ? wfMessage( 'rev-deleted-comment' )->escaped()
89  : $row->rc_comment,
90  $actiontext
91  );
92  }
93 
105  public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext = '' ) {
106  global $wgFeedDiffCutoff, $wgLang;
107  wfProfileIn( __METHOD__ );
108 
109  // log entries
110  $completeText = '<p>' . implode( ' ',
111  array_filter(
112  array(
113  $actiontext,
114  Linker::formatComment( $comment ) ) ) ) . "</p>\n";
115 
116  // NOTE: Check permissions for anonymous users, not current user.
117  // No "privileged" version should end up in the cache.
118  // Most feed readers will not log in anyway.
119  $anon = new User();
120  $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
121 
122  // Can't diff special pages, unreadable pages or pages with no new revision
123  // to compare against: just return the text.
124  if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
125  wfProfileOut( __METHOD__ );
126  return $completeText;
127  }
128 
129  if ( $oldid ) {
130  wfProfileIn( __METHOD__ . "-dodiff" );
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 {
146  $context = clone RequestContext::getMain();
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::cleanUp( $diffText );
169  $diffText = self::applyDiffStyle( $diffText );
170  }
171  wfProfileOut( __METHOD__ . "-dodiff" );
172  } else {
173  $rev = Revision::newFromId( $newid );
174  if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
175  $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
176  } else {
177  $newContent = $rev->getContent();
178  }
179 
180  if ( $newContent instanceof TextContent ) {
181  // only textual content has a "source view".
182  $text = $newContent->getNativeData();
183 
184  if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
185  $html = null;
186  } else {
187  $html = nl2br( htmlspecialchars( $text ) );
188  }
189  } else {
190  //XXX: we could get an HTML representation of the content via getParserOutput, but that may
191  // contain JS magic and generally may not be suitable for inclusion in a feed.
192  // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
193  //Compare also ApiFeedContributions::feedItemDesc
194  $html = null;
195  }
196 
197  if ( $html === null ) {
198 
199  // Omit large new page diffs, bug 29110
200  // Also use diff link for non-textual content
201  $diffText = self::getDiffLink( $title, $newid );
202  } else {
203  $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
204  '<div>' . $html . '</div>';
205  }
206  }
207  $completeText .= $diffText;
208 
209  wfProfileOut( __METHOD__ );
210  return $completeText;
211  }
212 
222  protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
223  $queryParameters = array( 'diff' => $newid );
224  if ( $oldid != null ) {
225  $queryParameters['oldid'] = $oldid;
226  }
227  $diffUrl = $title->getFullURL( $queryParameters );
228 
229  $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
230  wfMessage( 'showdiff' )->inContentLanguage()->text() );
231 
232  return $diffLink;
233  }
234 
243  public static function applyDiffStyle( $text ) {
244  $styles = array(
245  'diff' => 'background-color: white; color:black;',
246  'diff-otitle' => 'background-color: white; color:black; text-align: center;',
247  'diff-ntitle' => 'background-color: white; color:black; text-align: center;',
248  'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;',
249  'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;',
250  'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
251  'diffchange' => 'font-weight: bold; text-decoration: none;',
252  );
253 
254  foreach ( $styles as $class => $style ) {
255  $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
256  "\\1style=\"$style\"\\3", $text );
257  }
258 
259  return $text;
260  }
261 
262 }
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
$wgUser
$wgUser
Definition: Setup.php:552
Revision\DELETED_COMMENT
const DELETED_COMMENT
Definition: Revision.php:66
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:88
$html
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:1530
text
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
$timestamp
if( $limit) $timestamp
Definition: importImages.php:104
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
UtfNormal\cleanUp
static cleanUp( $string)
The ultimate convenience function! Clean up invalid UTF-8 sequences, and convert to normal form C,...
Definition: UtfNormal.php:79
FeedUtils\formatDiffRow
static formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='')
Really format a diff for the newsfeed.
Definition: FeedUtils.php:105
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
RC_LOG
const RC_LOG
Definition: Defines.php:181
$messageMemc
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php For a description of the see design txt $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest to get request data $messageMemc
Definition: globals.txt:25
ContentHandler\getForTitle
static getForTitle(Title $title)
Returns the appropriate ContentHandler singleton for the given title.
Definition: ContentHandler.php:259
Linker\formatComment
static formatComment( $comment, $title=null, $local=false)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition: Linker.php:1254
LogFormatter\newFromRow
static newFromRow( $row)
Handy shortcut for constructing a formatter directly from database row.
Definition: LogFormatter.php:71
Html\element
static element( $element, $attribs=array(), $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:148
wfProfileOut
wfProfileOut( $functionname='missing')
Stop profiling of a function.
Definition: Profiler.php:46
$wgOut
$wgOut
Definition: Setup.php:562
wfMessage
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 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
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
FeedUtils\checkPurge
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:39
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$comment
$comment
Definition: importImages.php:107
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
FeedUtils
Helper functions for feeds.
Definition: FeedUtils.php:29
FeedUtils\getDiffLink
static getDiffLink(Title $title, $newid, $oldid=null)
Generates a diff link.
Definition: FeedUtils.php:222
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:420
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
FeedUtils\formatDiff
static formatDiff( $row)
Format a diff for the newsfeed.
Definition: FeedUtils.php:76
$rev
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:1337
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$wgLang
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
FeedUtils\applyDiffStyle
static applyDiffStyle( $text)
Hacky application of diff styles for the feeds.
Definition: FeedUtils.php:243
as
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
User
User
Definition: All_system_messages.txt:425
FeedUtils\checkFeedOutput
static checkFeedOutput( $type)
Check whether feeds can be used and that $type is a valid feed type.
Definition: FeedUtils.php:54
$type
$type
Definition: testCompression.php:46