]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/ChangesFeed.php
MediaWiki 1.16.1-scripts
[autoinstalls/mediawiki.git] / includes / ChangesFeed.php
1 <?php
2
3 /**
4  * Feed to Special:RecentChanges and Special:RecentChangesLiked
5  *
6  * @ingroup Feed
7  */
8 class ChangesFeed {
9         public $format, $type, $titleMsg, $descMsg;
10
11         /**
12          * Constructor
13          *
14          * @param $format String: feed's format (either 'rss' or 'atom')
15          * @param $type String: type of feed (for cache keys)
16          */
17         public function __construct( $format, $type ) {
18                 $this->format = $format;
19                 $this->type = $type;
20         }
21
22         /**
23          * Get a ChannelFeed subclass object to use
24          *
25          * @param $title String: feed's title
26          * @param $description String: feed's description
27          * @return ChannelFeed subclass or false on failure
28          */
29         public function getFeedObject( $title, $description ) {
30                 global $wgSitename, $wgContLanguageCode, $wgFeedClasses, $wgTitle;
31                 $feedTitle = "$wgSitename  - {$title} [$wgContLanguageCode]";
32                 if( !isset($wgFeedClasses[$this->format] ) )
33                         return false;
34                 return new $wgFeedClasses[$this->format](
35                         $feedTitle, htmlspecialchars( $description ), $wgTitle->getFullUrl() );
36         }
37
38         /**
39          * Generates feed's content
40          *
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
46          */
47         public function execute( $feed, $rows, $lastmod, $opts ) {
48                 global $messageMemc, $wgFeedCacheTimeout;
49                 global $wgSitename, $wgLang;
50
51                 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
52                         return;
53                 }
54
55                 $timekey = wfMemcKey( $this->type, $this->format, 'timestamp' );
56                 $optionsHash = md5( serialize( $opts->getAllValues() ) );
57                 $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
58
59                 FeedUtils::checkPurge($timekey, $key);
60
61                 /*
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
64                 * gets it quick too.
65                 */
66                 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
67                 if( is_string( $cachedFeed ) ) {
68                         wfDebug( "RC: Outputting cached feed\n" );
69                         $feed->httpHeaders();
70                         echo $cachedFeed;
71                 } else {
72                         wfDebug( "RC: rendering new feed and caching it\n" );
73                         ob_start();
74                         self::generateFeed( $rows, $feed );
75                         $cachedFeed = ob_get_contents();
76                         ob_end_flush();
77                         $this->saveToCache( $cachedFeed, $timekey, $key );
78                 }
79                 return true;
80         }
81
82         /**
83          * Save to feed result to $messageMemc
84          *
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
88          */
89         public function saveToCache( $feed, $timekey, $key ) {
90                 global $messageMemc;
91                 $expire = 3600 * 24; # One day
92                 $messageMemc->set( $key, $feed, $expire );
93                 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
94         }
95
96         /**
97          * Try to load the feed result from $messageMemc
98          *
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
103          */
104         public function loadFromCache( $lastmod, $timekey, $key ) {
105                 global $wgFeedCacheTimeout, $messageMemc;
106                 $feedLastmod = $messageMemc->get( $timekey );
107
108                 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
109                         /*
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.
114                         */
115
116                         $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
117                         $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
118                         $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
119
120                         if( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix) {
121                                 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
122                                 return $messageMemc->get( $key );
123                         } else {
124                                 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
125                         }
126                 }
127                 return false;
128         }
129
130         /**
131          * Generate the feed items given a row from the database.
132          * @param $rows DatabaseBase resource with recentchanges rows
133          * @param $feed Feed object
134          */
135         public static function generateFeed( $rows, &$feed ) {
136                 wfProfileIn( __METHOD__ );
137
138                 $feed->outHeader();
139
140                 # Merge adjacent edits by one user
141                 $sorted = array();
142                 $n = 0;
143                 foreach( $rows as $obj ) {
144                         if( $n > 0 &&
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;
149                         } else {
150                                 $sorted[$n] = $obj;
151                                 $n++;
152                         }
153                 }
154
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(),
164                                 $obj->rc_timestamp,
165                                 ($obj->rc_deleted & Revision::DELETED_USER) ? wfMsgHtml('rev-deleted-user') : $obj->rc_user_text,
166                                 $talkpage->getFullURL()
167                         );
168                         $feed->outItem( $item );
169                 }
170                 $feed->outFooter();
171                 wfProfileOut( __METHOD__ );
172         }
173
174 }