(bug 42168) Nicely handle missing revisions in ApiQueryRevisions.
[lhc/web/wiklou.git] / includes / ChangesFeed.php
1 <?php
2 /**
3 * Feed for list of changes.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Feed to Special:RecentChanges and Special:RecentChangesLiked
25 *
26 * @ingroup Feed
27 */
28 class ChangesFeed {
29 public $format, $type, $titleMsg, $descMsg;
30
31 /**
32 * Constructor
33 *
34 * @param $format String: feed's format (either 'rss' or 'atom')
35 * @param $type String: type of feed (for cache keys)
36 */
37 public function __construct( $format, $type ) {
38 $this->format = $format;
39 $this->type = $type;
40 }
41
42 /**
43 * Get a ChannelFeed subclass object to use
44 *
45 * @param $title String: feed's title
46 * @param $description String: feed's description
47 * @param $url String: url of origin page
48 * @return ChannelFeed subclass or false on failure
49 */
50 public function getFeedObject( $title, $description, $url ) {
51 global $wgSitename, $wgLanguageCode, $wgFeedClasses;
52
53 if ( !isset( $wgFeedClasses[$this->format] ) ) {
54 return false;
55 }
56
57 if( !array_key_exists( $this->format, $wgFeedClasses ) ) {
58 // falling back to atom
59 $this->format = 'atom';
60 }
61
62 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
63 return new $wgFeedClasses[$this->format](
64 $feedTitle, htmlspecialchars( $description ), $url );
65 }
66
67 /**
68 * Generates feed's content
69 *
70 * @param $feed ChannelFeed subclass object (generally the one returned by getFeedObject())
71 * @param $rows ResultWrapper object with rows in recentchanges table
72 * @param $lastmod Integer: timestamp of the last item in the recentchanges table (only used for the cache key)
73 * @param $opts FormOptions as in SpecialRecentChanges::getDefaultOptions()
74 * @return null|bool True or null
75 */
76 public function execute( $feed, $rows, $lastmod, $opts ) {
77 global $wgLang, $wgRenderHashAppend;
78
79 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
80 return null;
81 }
82
83 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
84 $timekey = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
85 $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
86
87 FeedUtils::checkPurge( $timekey, $key );
88
89 /**
90 * Bumping around loading up diffs can be pretty slow, so where
91 * possible we want to cache the feed output so the next visitor
92 * gets it quick too.
93 */
94 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
95 if( is_string( $cachedFeed ) ) {
96 wfDebug( "RC: Outputting cached feed\n" );
97 $feed->httpHeaders();
98 echo $cachedFeed;
99 } else {
100 wfDebug( "RC: rendering new feed and caching it\n" );
101 ob_start();
102 self::generateFeed( $rows, $feed );
103 $cachedFeed = ob_get_contents();
104 ob_end_flush();
105 $this->saveToCache( $cachedFeed, $timekey, $key );
106 }
107 return true;
108 }
109
110 /**
111 * Save to feed result to $messageMemc
112 *
113 * @param $feed String: feed's content
114 * @param $timekey String: memcached key of the last modification
115 * @param $key String: memcached key of the content
116 */
117 public function saveToCache( $feed, $timekey, $key ) {
118 global $messageMemc;
119 $expire = 3600 * 24; # One day
120 $messageMemc->set( $key, $feed, $expire );
121 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
122 }
123
124 /**
125 * Try to load the feed result from $messageMemc
126 *
127 * @param $lastmod Integer: timestamp of the last item in the recentchanges table
128 * @param $timekey String: memcached key of the last modification
129 * @param $key String: memcached key of the content
130 * @return string|bool feed's content on cache hit or false on cache miss
131 */
132 public function loadFromCache( $lastmod, $timekey, $key ) {
133 global $wgFeedCacheTimeout, $wgOut, $messageMemc;
134
135 $feedLastmod = $messageMemc->get( $timekey );
136
137 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
138 /**
139 * If the cached feed was rendered very recently, we may
140 * go ahead and use it even if there have been edits made
141 * since it was rendered. This keeps a swarm of requests
142 * from being too bad on a super-frequently edited wiki.
143 */
144
145 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
146 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
147 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
148
149 if( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix) {
150 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
151 if ( $feedLastmodUnix < $lastmodUnix ) {
152 $wgOut->setLastModified( $feedLastmod ); // bug 21916
153 }
154 return $messageMemc->get( $key );
155 } else {
156 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
157 }
158 }
159 return false;
160 }
161
162 /**
163 * Generate the feed items given a row from the database.
164 * @param $rows DatabaseBase resource with recentchanges rows
165 * @param $feed Feed object
166 */
167 public static function generateFeed( $rows, &$feed ) {
168 wfProfileIn( __METHOD__ );
169
170 $feed->outHeader();
171
172 # Merge adjacent edits by one user
173 $sorted = array();
174 $n = 0;
175 foreach( $rows as $obj ) {
176 if( $n > 0 &&
177 $obj->rc_type == RC_EDIT &&
178 $obj->rc_namespace >= 0 &&
179 $obj->rc_cur_id == $sorted[$n-1]->rc_cur_id &&
180 $obj->rc_user_text == $sorted[$n-1]->rc_user_text ) {
181 $sorted[$n-1]->rc_last_oldid = $obj->rc_last_oldid;
182 } else {
183 $sorted[$n] = $obj;
184 $n++;
185 }
186 }
187
188 foreach( $sorted as $obj ) {
189 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
190 $talkpage = MWNamespace::canTalk( $obj->rc_namespace ) ? $title->getTalkPage()->getFullUrl() : '';
191 // Skip items with deleted content (avoids partially complete/inconsistent output)
192 if( $obj->rc_deleted ) continue;
193
194 if ( $obj->rc_this_oldid ) {
195 $url = $title->getFullURL(
196 'diff=' . $obj->rc_this_oldid .
197 '&oldid=' . $obj->rc_last_oldid
198 );
199 } else {
200 // log entry or something like that.
201 $url = $title->getFullURL();
202 }
203
204 $item = new FeedItem(
205 $title->getPrefixedText(),
206 FeedUtils::formatDiff( $obj ),
207 $url,
208 $obj->rc_timestamp,
209 ( $obj->rc_deleted & Revision::DELETED_USER ) ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
210 $talkpage
211 );
212 $feed->outItem( $item );
213 }
214 $feed->outFooter();
215 wfProfileOut( __METHOD__ );
216 }
217
218 }