4 * Feed to Special:RecentChanges and Special:RecentChangesLiked
9 public $format, $type, $titleMsg, $descMsg;
14 * @param $format String: feed's format (either 'rss' or 'atom')
15 * @param $type String: type of feed (for cache keys)
17 public function __construct( $format, $type ) {
18 $this->format
= $format;
23 * Get a ChannelFeed subclass object to use
25 * @param $title String: feed's title
26 * @param $description String: feed's description
27 * @param $url String: url of origin page
28 * @return ChannelFeed subclass or false on failure
30 public function getFeedObject( $title, $description, $url ) {
31 global $wgSitename, $wgLanguageCode, $wgFeedClasses;
33 if ( !isset( $wgFeedClasses[$this->format
] ) ) {
37 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
38 return new $wgFeedClasses[$this->format
](
39 $feedTitle, htmlspecialchars( $description ), $url );
43 * Generates feed's content
45 * @param $feed ChannelFeed subclass object (generally the one returned by getFeedObject())
46 * @param $rows ResultWrapper object with rows in recentchanges table
47 * @param $lastmod Integer: timestamp of the last item in the recentchanges table (only used for the cache key)
48 * @param $opts FormOptions as in SpecialRecentChanges::getDefaultOptions()
49 * @return null or true
51 public function execute( $feed, $rows, $lastmod, $opts ) {
52 global $wgLang, $wgRenderHashAppend;
54 if ( !FeedUtils
::checkFeedOutput( $this->format
) ) {
58 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
59 $timekey = wfMemcKey( $this->type
, $this->format
, $wgLang->getCode(), $optionsHash, 'timestamp' );
60 $key = wfMemcKey( $this->type
, $this->format
, $wgLang->getCode(), $optionsHash );
62 FeedUtils
::checkPurge( $timekey, $key );
65 * Bumping around loading up diffs can be pretty slow, so where
66 * possible we want to cache the feed output so the next visitor
69 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
70 if( is_string( $cachedFeed ) ) {
71 wfDebug( "RC: Outputting cached feed\n" );
75 wfDebug( "RC: rendering new feed and caching it\n" );
77 self
::generateFeed( $rows, $feed );
78 $cachedFeed = ob_get_contents();
80 $this->saveToCache( $cachedFeed, $timekey, $key );
86 * Save to feed result to $messageMemc
88 * @param $feed String: feed's content
89 * @param $timekey String: memcached key of the last modification
90 * @param $key String: memcached key of the content
92 public function saveToCache( $feed, $timekey, $key ) {
94 $expire = 3600 * 24; # One day
95 $messageMemc->set( $key, $feed, $expire );
96 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
100 * Try to load the feed result from $messageMemc
102 * @param $lastmod Integer: timestamp of the last item in the recentchanges table
103 * @param $timekey String: memcached key of the last modification
104 * @param $key String: memcached key of the content
105 * @return feed's content on cache hit or false on cache miss
107 public function loadFromCache( $lastmod, $timekey, $key ) {
108 global $wgFeedCacheTimeout, $wgOut, $messageMemc;
110 $feedLastmod = $messageMemc->get( $timekey );
112 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
114 * If the cached feed was rendered very recently, we may
115 * go ahead and use it even if there have been edits made
116 * since it was rendered. This keeps a swarm of requests
117 * from being too bad on a super-frequently edited wiki.
120 $feedAge = time() - wfTimestamp( TS_UNIX
, $feedLastmod );
121 $feedLastmodUnix = wfTimestamp( TS_UNIX
, $feedLastmod );
122 $lastmodUnix = wfTimestamp( TS_UNIX
, $lastmod );
124 if( $feedAge < $wgFeedCacheTimeout ||
$feedLastmodUnix > $lastmodUnix) {
125 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
126 if ( $feedLastmodUnix < $lastmodUnix ) {
127 $wgOut->setLastModified( $feedLastmod ); // bug 21916
129 return $messageMemc->get( $key );
131 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
138 * Generate the feed items given a row from the database.
139 * @param $rows DatabaseBase resource with recentchanges rows
140 * @param $feed Feed object
142 public static function generateFeed( $rows, &$feed ) {
143 wfProfileIn( __METHOD__
);
147 # Merge adjacent edits by one user
150 foreach( $rows as $obj ) {
152 $obj->rc_namespace
>= 0 &&
153 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
154 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
155 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
162 foreach( $sorted as $obj ) {
163 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
164 $talkpage = $title->getTalkPage();
165 // Skip items with deleted content (avoids partially complete/inconsistent output)
166 if( $obj->rc_deleted
) continue;
167 $item = new FeedItem(
168 $title->getPrefixedText(),
169 FeedUtils
::formatDiff( $obj ),
170 $obj->rc_this_oldid ?
$title->getFullURL( 'diff=' . $obj->rc_this_oldid
. '&oldid=prev' ) : $title->getFullURL(),
172 ($obj->rc_deleted
& Revision
::DELETED_USER
) ?
wfMsgHtml('rev-deleted-user') : $obj->rc_user_text
,
173 $talkpage->getFullURL()
175 $feed->outItem( $item );
178 wfProfileOut( __METHOD__
);