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 * @return ChannelFeed subclass or false on failure
29 public function getFeedObject( $title, $description ) {
30 global $wgSitename, $wgContLanguageCode, $wgFeedClasses, $wgTitle;
31 $feedTitle = "$wgSitename - {$title} [$wgContLanguageCode]";
32 if( !isset($wgFeedClasses[$this->format
] ) )
34 return new $wgFeedClasses[$this->format
](
35 $feedTitle, htmlspecialchars( $description ), $wgTitle->getFullUrl() );
39 * Generates feed's content
41 * @param $feed ChannelFeed subclass object (generally the one returned by getFeedObject())
42 * @param $rows ResultWrapper object with rows in recentchanges table
43 * @param $limit Integer: number of rows in $rows (only used for the cache key)
44 * @param $hideminor Boolean: whether to hide minor edits (only used for the cache key)
45 * @param $lastmod Integer: timestamp of the last item in the recentchanges table (only used for the cache key)
46 * @param $target String: target's name; for Special:RecentChangesLinked (only used for the cache key)
47 * @param $namespace Integer: namespace id (only used for the cache key)
48 * @return null or true
50 public function execute( $feed, $rows, $limit=0, $hideminor=false, $lastmod=false, $target='', $namespace='' ) {
51 global $messageMemc, $wgFeedCacheTimeout;
52 global $wgSitename, $wgLang;
54 if ( !FeedUtils
::checkFeedOutput( $this->format
) ) {
58 $timekey = wfMemcKey( $this->type
, $this->format
, 'timestamp' );
59 $key = wfMemcKey( $this->type
, $this->format
, $limit, $hideminor, $target, $wgLang->getCode(), $namespace );
61 FeedUtils
::checkPurge($timekey, $key);
64 * Bumping around loading up diffs can be pretty slow, so where
65 * possible we want to cache the feed output so the next visitor
68 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
69 if( is_string( $cachedFeed ) ) {
70 wfDebug( "RC: Outputting cached feed\n" );
74 wfDebug( "RC: rendering new feed and caching it\n" );
76 self
::generateFeed( $rows, $feed );
77 $cachedFeed = ob_get_contents();
79 $this->saveToCache( $cachedFeed, $timekey, $key );
85 * Save to feed result to $messageMemc
87 * @param $feed String: feed's content
88 * @param $timekey String: memcached key of the last modification
89 * @param $key String: memcached key of the content
91 public function saveToCache( $feed, $timekey, $key ) {
93 $expire = 3600 * 24; # One day
94 $messageMemc->set( $key, $feed, $expire );
95 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
99 * Try to load the feed result from $messageMemc
101 * @param $lastmod Integer: timestamp of the last item in the recentchanges table
102 * @param $timekey String: memcached key of the last modification
103 * @param $key String: memcached key of the content
104 * @return feed's content on cache hit or false on cache miss
106 public function loadFromCache( $lastmod, $timekey, $key ) {
107 global $wgFeedCacheTimeout, $messageMemc;
108 $feedLastmod = $messageMemc->get( $timekey );
110 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
112 * If the cached feed was rendered very recently, we may
113 * go ahead and use it even if there have been edits made
114 * since it was rendered. This keeps a swarm of requests
115 * from being too bad on a super-frequently edited wiki.
118 $feedAge = time() - wfTimestamp( TS_UNIX
, $feedLastmod );
119 $feedLastmodUnix = wfTimestamp( TS_UNIX
, $feedLastmod );
120 $lastmodUnix = wfTimestamp( TS_UNIX
, $lastmod );
122 if( $feedAge < $wgFeedCacheTimeout ||
$feedLastmodUnix > $lastmodUnix) {
123 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
124 return $messageMemc->get( $key );
126 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
133 * Generate the feed items given a row from the database.
134 * @param $rows DatabaseBase resource with recentchanges rows
135 * @param $feed Feed object
137 public static function generateFeed( $rows, &$feed ) {
138 wfProfileIn( __METHOD__
);
142 # Merge adjacent edits by one user
145 foreach( $rows as $obj ) {
147 $obj->rc_namespace
>= 0 &&
148 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
149 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
150 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
157 foreach( $sorted as $obj ) {
158 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
159 $talkpage = $title->getTalkPage();
160 // Skip items with deleted content (avoids partially complete/inconsistent output)
161 if( $obj->rc_deleted
) continue;
162 $item = new FeedItem(
163 $title->getPrefixedText(),
164 FeedUtils
::formatDiff( $obj ),
165 $obj->rc_this_oldid ?
$title->getFullURL( 'diff=' . $obj->rc_this_oldid
. '&oldid=prev' ) : $title->getFullURL(),
167 ($obj->rc_deleted
& Revision
::DELETED_USER
) ?
wfMsgHtml('rev-deleted-user') : $obj->rc_user_text
,
168 $talkpage->getFullURL()
170 $feed->outItem( $item );
173 wfProfileOut( __METHOD__
);