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 $lastmod Integer: timestamp of the last item in the recentchanges table (only used for the cache key)
44 * @param $opts FormOptions as in SpecialRecentChanges::getDefaultOptions()
45 * @return null or true
47 public function execute( $feed, $rows, $lastmod, $opts ) {
48 global $messageMemc, $wgFeedCacheTimeout;
49 global $wgSitename, $wgLang;
51 if ( !FeedUtils
::checkFeedOutput( $this->format
) ) {
55 $timekey = wfMemcKey( $this->type
, $this->format
, 'timestamp' );
56 $optionsHash = md5( serialize( $opts->getAllValues() ) );
57 $key = wfMemcKey( $this->type
, $this->format
, $wgLang->getCode(), $optionsHash );
59 FeedUtils
::checkPurge($timekey, $key);
62 * Bumping around loading up diffs can be pretty slow, so where
63 * possible we want to cache the feed output so the next visitor
66 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
67 if( is_string( $cachedFeed ) ) {
68 wfDebug( "RC: Outputting cached feed\n" );
72 wfDebug( "RC: rendering new feed and caching it\n" );
74 self
::generateFeed( $rows, $feed );
75 $cachedFeed = ob_get_contents();
77 $this->saveToCache( $cachedFeed, $timekey, $key );
83 * Save to feed result to $messageMemc
85 * @param $feed String: feed's content
86 * @param $timekey String: memcached key of the last modification
87 * @param $key String: memcached key of the content
89 public function saveToCache( $feed, $timekey, $key ) {
91 $expire = 3600 * 24; # One day
92 $messageMemc->set( $key, $feed, $expire );
93 $messageMemc->set( $timekey, wfTimestamp( TS_MW
), $expire );
97 * Try to load the feed result from $messageMemc
99 * @param $lastmod Integer: timestamp of the last item in the recentchanges table
100 * @param $timekey String: memcached key of the last modification
101 * @param $key String: memcached key of the content
102 * @return feed's content on cache hit or false on cache miss
104 public function loadFromCache( $lastmod, $timekey, $key ) {
105 global $wgFeedCacheTimeout, $messageMemc;
106 $feedLastmod = $messageMemc->get( $timekey );
108 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
110 * If the cached feed was rendered very recently, we may
111 * go ahead and use it even if there have been edits made
112 * since it was rendered. This keeps a swarm of requests
113 * from being too bad on a super-frequently edited wiki.
116 $feedAge = time() - wfTimestamp( TS_UNIX
, $feedLastmod );
117 $feedLastmodUnix = wfTimestamp( TS_UNIX
, $feedLastmod );
118 $lastmodUnix = wfTimestamp( TS_UNIX
, $lastmod );
120 if( $feedAge < $wgFeedCacheTimeout ||
$feedLastmodUnix > $lastmodUnix) {
121 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
122 return $messageMemc->get( $key );
124 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
131 * Generate the feed items given a row from the database.
132 * @param $rows DatabaseBase resource with recentchanges rows
133 * @param $feed Feed object
135 public static function generateFeed( $rows, &$feed ) {
136 wfProfileIn( __METHOD__
);
140 # Merge adjacent edits by one user
143 foreach( $rows as $obj ) {
145 $obj->rc_namespace
>= 0 &&
146 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
147 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
148 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
155 foreach( $sorted as $obj ) {
156 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
157 $talkpage = $title->getTalkPage();
158 // Skip items with deleted content (avoids partially complete/inconsistent output)
159 if( $obj->rc_deleted
) continue;
160 $item = new FeedItem(
161 $title->getPrefixedText(),
162 FeedUtils
::formatDiff( $obj ),
163 $obj->rc_this_oldid ?
$title->getFullURL( 'diff=' . $obj->rc_this_oldid
. '&oldid=prev' ) : $title->getFullURL(),
165 ($obj->rc_deleted
& Revision
::DELETED_USER
) ?
wfMsgHtml('rev-deleted-user') : $obj->rc_user_text
,
166 $talkpage->getFullURL()
168 $feed->outItem( $item );
171 wfProfileOut( __METHOD__
);