MediaWiki  1.23.2
ChangesFeed.php
Go to the documentation of this file.
1 <?php
28 class ChangesFeed {
30 
37  public function __construct( $format, $type ) {
38  $this->format = $format;
39  $this->type = $type;
40  }
41 
50  public function getFeedObject( $title, $description, $url ) {
51  global $wgSitename, $wgLanguageCode, $wgFeedClasses;
52 
53  if ( !isset( $wgFeedClasses[$this->format] ) ) {
54  return false;
55  }
56 
57  if ( !array_key_exists( $this->format, $wgFeedClasses ) ) {
58  // falling back to atom
59  $this->format = 'atom';
60  }
61 
62  $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
63  return new $wgFeedClasses[$this->format](
64  $feedTitle, htmlspecialchars( $description ), $url );
65  }
66 
78  public function execute( $feed, $rows, $lastmod, $opts ) {
79  global $wgLang, $wgRenderHashAppend;
80 
81  if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
82  return null;
83  }
84 
85  $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
86  $timekey = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
87  $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
88 
89  FeedUtils::checkPurge( $timekey, $key );
90 
96  $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
97  if ( is_string( $cachedFeed ) ) {
98  wfDebug( "RC: Outputting cached feed\n" );
99  $feed->httpHeaders();
100  echo $cachedFeed;
101  } else {
102  wfDebug( "RC: rendering new feed and caching it\n" );
103  ob_start();
104  self::generateFeed( $rows, $feed );
105  $cachedFeed = ob_get_contents();
106  ob_end_flush();
107  $this->saveToCache( $cachedFeed, $timekey, $key );
108  }
109  return true;
110  }
111 
119  public function saveToCache( $feed, $timekey, $key ) {
121  $expire = 3600 * 24; # One day
122  $messageMemc->set( $key, $feed, $expire );
123  $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
124  }
125 
134  public function loadFromCache( $lastmod, $timekey, $key ) {
135  global $wgFeedCacheTimeout, $wgOut, $messageMemc;
136 
137  $feedLastmod = $messageMemc->get( $timekey );
138 
139  if ( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
147  $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
148  $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
149  $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
150 
151  if ( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix ) {
152  wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
153  if ( $feedLastmodUnix < $lastmodUnix ) {
154  $wgOut->setLastModified( $feedLastmod ); // bug 21916
155  }
156  return $messageMemc->get( $key );
157  } else {
158  wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
159  }
160  }
161  return false;
162  }
163 
169  public static function generateFeed( $rows, &$feed ) {
170  wfProfileIn( __METHOD__ );
171  $items = self::buildItems( $rows );
172  $feed->outHeader();
173  foreach ( $items as $item ) {
174  $feed->outItem( $item );
175  }
176  $feed->outFooter();
177  wfProfileOut( __METHOD__ );
178  }
179 
184  public static function buildItems( $rows ) {
185  wfProfileIn( __METHOD__ );
186  $items = array();
187 
188  # Merge adjacent edits by one user
189  $sorted = array();
190  $n = 0;
191  foreach ( $rows as $obj ) {
192  if ( $n > 0 &&
193  $obj->rc_type == RC_EDIT &&
194  $obj->rc_namespace >= 0 &&
195  $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
196  $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
197  $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
198  } else {
199  $sorted[$n] = $obj;
200  $n++;
201  }
202  }
203 
204  foreach ( $sorted as $obj ) {
205  $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
206  $talkpage = MWNamespace::canTalk( $obj->rc_namespace )
207  ? $title->getTalkPage()->getFullURL()
208  : '';
209 
210  // Skip items with deleted content (avoids partially complete/inconsistent output)
211  if ( $obj->rc_deleted ) {
212  continue;
213  }
214 
215  if ( $obj->rc_this_oldid ) {
216  $url = $title->getFullURL( array(
217  'diff' => $obj->rc_this_oldid,
218  'oldid' => $obj->rc_last_oldid,
219  ) );
220  } else {
221  // log entry or something like that.
222  $url = $title->getFullURL();
223  }
224 
225  $items[] = new FeedItem(
226  $title->getPrefixedText(),
227  FeedUtils::formatDiff( $obj ),
228  $url,
229  $obj->rc_timestamp,
230  ( $obj->rc_deleted & Revision::DELETED_USER )
231  ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
232  $talkpage
233  );
234  }
235 
236  wfProfileOut( __METHOD__ );
237  return $items;
238  }
239 }
Revision\DELETED_USER
const DELETED_USER
Definition: Revision.php:67
ChangesFeed\generateFeed
static generateFeed( $rows, &$feed)
Generate the feed items given a row from the database, printing the feed.
Definition: ChangesFeed.php:169
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
ChangesFeed\__construct
__construct( $format, $type)
Constructor.
Definition: ChangesFeed.php:37
FeedItem
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition: Feed.php:38
ChangesFeed\$type
$type
Definition: ChangesFeed.php:29
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
ChangesFeed\$format
$format
Definition: ChangesFeed.php:29
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
ChangesFeed\loadFromCache
loadFromCache( $lastmod, $timekey, $key)
Try to load the feed result from $messageMemc.
Definition: ChangesFeed.php:134
wfProfileIn
wfProfileIn( $functionname)
Begin profiling of a function.
Definition: Profiler.php:33
$n
$n
Definition: RandomTest.php:76
$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
RC_EDIT
const RC_EDIT
Definition: Defines.php:178
ChangesFeed\$descMsg
$descMsg
Definition: ChangesFeed.php:29
ChangesFeed\getFeedObject
getFeedObject( $title, $description, $url)
Get a ChannelFeed subclass object to use.
Definition: ChangesFeed.php:50
wfMemcKey
wfMemcKey()
Get a cache key.
Definition: GlobalFunctions.php:3571
MWNamespace\canTalk
static canTalk( $index)
Can this namespace ever have a talk namespace?
Definition: Namespace.php:293
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
ChangesFeed
Feed to Special:RecentChanges and Special:RecentChangesLiked.
Definition: ChangesFeed.php:28
ChangesFeed\$titleMsg
$titleMsg
Definition: ChangesFeed.php:29
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
wfDebug
wfDebug( $text, $dest='all')
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:933
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
FeedUtils\formatDiff
static formatDiff( $row)
Format a diff for the newsfeed.
Definition: FeedUtils.php:76
$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
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
TS_UNIX
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: GlobalFunctions.php:2426
ChangesFeed\execute
execute( $feed, $rows, $lastmod, $opts)
Generates feed's content.
Definition: ChangesFeed.php:78
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1230
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
ChangesFeed\buildItems
static buildItems( $rows)
Generate the feed items given a row from the database.
Definition: ChangesFeed.php:184
FeedUtils\checkFeedOutput
static checkFeedOutput( $type)
Check whether feeds can be used and that $type is a valid feed type.
Definition: FeedUtils.php:54
ChangesFeed\saveToCache
saveToCache( $feed, $timekey, $key)
Save to feed result to $messageMemc.
Definition: ChangesFeed.php:119