Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / ChangesFeed.php
blobe900e3491318bb8b620fa4af255f9fc03b1bc749
1 <?php
2 /**
3 * Feed for list of changes.
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.
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.
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
20 * @file
23 /**
24 * Feed to Special:RecentChanges and Special:RecentChangesLiked
26 * @ingroup Feed
28 class ChangesFeed {
29 public $format, $type, $titleMsg, $descMsg;
31 /**
32 * Constructor
34 * @param string $format feed's format (either 'rss' or 'atom')
35 * @param string $type type of feed (for cache keys)
37 public function __construct( $format, $type ) {
38 $this->format = $format;
39 $this->type = $type;
42 /**
43 * Get a ChannelFeed subclass object to use
45 * @param string $title feed's title
46 * @param string $description feed's description
47 * @param string $url url of origin page
48 * @return ChannelFeed subclass or false on failure
50 public function getFeedObject( $title, $description, $url ) {
51 global $wgSitename, $wgLanguageCode, $wgFeedClasses;
53 if ( !isset( $wgFeedClasses[$this->format] ) ) {
54 return false;
57 if( !array_key_exists( $this->format, $wgFeedClasses ) ) {
58 // falling back to atom
59 $this->format = 'atom';
62 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
63 return new $wgFeedClasses[$this->format](
64 $feedTitle, htmlspecialchars( $description ), $url );
67 /**
68 * Generates feed's content
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
76 public function execute( $feed, $rows, $lastmod, $opts ) {
77 global $wgLang, $wgRenderHashAppend;
79 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
80 return null;
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 );
87 FeedUtils::checkPurge( $timekey, $key );
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.
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 );
107 return true;
111 * Save to feed result to $messageMemc
113 * @param string $feed feed's content
114 * @param string $timekey memcached key of the last modification
115 * @param string $key memcached key of the content
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 );
125 * Try to load the feed result from $messageMemc
127 * @param $lastmod Integer: timestamp of the last item in the recentchanges table
128 * @param string $timekey memcached key of the last modification
129 * @param string $key memcached key of the content
130 * @return string|bool feed's content on cache hit or false on cache miss
132 public function loadFromCache( $lastmod, $timekey, $key ) {
133 global $wgFeedCacheTimeout, $wgOut, $messageMemc;
135 $feedLastmod = $messageMemc->get( $timekey );
137 if( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
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.
145 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
146 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
147 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
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
154 return $messageMemc->get( $key );
155 } else {
156 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
159 return false;
163 * Generate the feed items given a row from the database.
164 * @param $rows DatabaseBase resource with recentchanges rows
165 * @param $feed Feed object
167 public static function generateFeed( $rows, &$feed ) {
168 wfProfileIn( __METHOD__ );
170 $feed->outHeader();
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++;
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;
194 if ( $obj->rc_this_oldid ) {
195 $url = $title->getFullURL(
196 'diff=' . $obj->rc_this_oldid .
197 '&oldid=' . $obj->rc_last_oldid
199 } else {
200 // log entry or something like that.
201 $url = $title->getFullURL();
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
212 $feed->outItem( $item );
214 $feed->outFooter();
215 wfProfileOut( __METHOD__ );