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, $wgRenderHashAppend;
51 if ( !FeedUtils
::checkFeedOutput( $this->format
) ) {
55 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
56 $timekey = wfMemcKey( $this->type
, $this->format
, $wgLang->getCode(), $optionsHash, 'timestamp' );
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, $wgOut, $messageMemc;
107 $feedLastmod = $messageMemc->get( $timekey );
109 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
111 * If the cached feed was rendered very recently, we may
112 * go ahead and use it even if there have been edits made
113 * since it was rendered. This keeps a swarm of requests
114 * from being too bad on a super-frequently edited wiki.
117 $feedAge = time() - wfTimestamp( TS_UNIX
, $feedLastmod );
118 $feedLastmodUnix = wfTimestamp( TS_UNIX
, $feedLastmod );
119 $lastmodUnix = wfTimestamp( TS_UNIX
, $lastmod );
121 if( $feedAge < $wgFeedCacheTimeout ||
$feedLastmodUnix > $lastmodUnix) {
122 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
123 if ( $feedLastmodUnix < $lastmodUnix ) {
124 $wgOut->setLastModified( $feedLastmod ); // bug 21916
126 return $messageMemc->get( $key );
128 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
135 * Generate the feed items given a row from the database.
136 * @param $rows DatabaseBase resource with recentchanges rows
137 * @param $feed Feed object
139 public static function generateFeed( $rows, &$feed ) {
140 wfProfileIn( __METHOD__
);
144 # Merge adjacent edits by one user
147 foreach( $rows as $obj ) {
149 $obj->rc_namespace
>= 0 &&
150 $obj->rc_cur_id
== $sorted[$n-1]->rc_cur_id
&&
151 $obj->rc_user_text
== $sorted[$n-1]->rc_user_text
) {
152 $sorted[$n-1]->rc_last_oldid
= $obj->rc_last_oldid
;
159 foreach( $sorted as $obj ) {
160 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
161 $talkpage = $title->getTalkPage();
162 // Skip items with deleted content (avoids partially complete/inconsistent output)
163 if( $obj->rc_deleted
) continue;
164 $item = new FeedItem(
165 $title->getPrefixedText(),
166 FeedUtils
::formatDiff( $obj ),
167 $obj->rc_this_oldid ?
$title->getFullURL( 'diff=' . $obj->rc_this_oldid
. '&oldid=prev' ) : $title->getFullURL(),
169 ($obj->rc_deleted
& Revision
::DELETED_USER
) ?
wfMsgHtml('rev-deleted-user') : $obj->rc_user_text
,
170 $talkpage->getFullURL()
172 $feed->outItem( $item );
175 wfProfileOut( __METHOD__
);