Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / changes / ChangesFeed.php
blob28c2f7ed4c9aa6de07b4e65bfacffd5e9f321a8e
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|bool 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 ChannelFeed $feed ChannelFeed subclass object (generally the one returned
71 * by getFeedObject())
72 * @param ResultWrapper $rows ResultWrapper object with rows in recentchanges table
73 * @param int $lastmod Timestamp of the last item in the recentchanges table (only
74 * used for the cache key)
75 * @param FormOptions $opts As in SpecialRecentChanges::getDefaultOptions()
76 * @return null|bool True or null
78 public function execute( $feed, $rows, $lastmod, $opts ) {
79 global $wgLang, $wgRenderHashAppend;
81 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
82 return null;
85 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
86 $timekey = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
87 $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
89 FeedUtils::checkPurge( $timekey, $key );
91 /**
92 * Bumping around loading up diffs can be pretty slow, so where
93 * possible we want to cache the feed output so the next visitor
94 * gets it quick too.
96 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
97 if ( is_string( $cachedFeed ) ) {
98 wfDebug( "RC: Outputting cached feed\n" );
99 $feed->httpHeaders();
100 echo $cachedFeed;
101 } else {
102 wfDebug( "RC: rendering new feed and caching it\n" );
103 ob_start();
104 self::generateFeed( $rows, $feed );
105 $cachedFeed = ob_get_contents();
106 ob_end_flush();
107 $this->saveToCache( $cachedFeed, $timekey, $key );
109 return true;
113 * Save to feed result to $messageMemc
115 * @param string $feed Feed's content
116 * @param string $timekey Memcached key of the last modification
117 * @param string $key Memcached key of the content
119 public function saveToCache( $feed, $timekey, $key ) {
120 global $messageMemc;
121 $expire = 3600 * 24; # One day
122 $messageMemc->set( $key, $feed, $expire );
123 $messageMemc->set( $timekey, wfTimestamp( TS_MW ), $expire );
127 * Try to load the feed result from $messageMemc
129 * @param int $lastmod Timestamp of the last item in the recentchanges table
130 * @param string $timekey Memcached key of the last modification
131 * @param string $key Memcached key of the content
132 * @return string|bool Feed's content on cache hit or false on cache miss
134 public function loadFromCache( $lastmod, $timekey, $key ) {
135 global $wgFeedCacheTimeout, $wgOut, $messageMemc;
137 $feedLastmod = $messageMemc->get( $timekey );
139 if ( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
141 * If the cached feed was rendered very recently, we may
142 * go ahead and use it even if there have been edits made
143 * since it was rendered. This keeps a swarm of requests
144 * from being too bad on a super-frequently edited wiki.
147 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
148 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
149 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
151 if ( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix ) {
152 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
153 if ( $feedLastmodUnix < $lastmodUnix ) {
154 $wgOut->setLastModified( $feedLastmod ); // bug 21916
156 return $messageMemc->get( $key );
157 } else {
158 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
161 return false;
165 * Generate the feed items given a row from the database, printing the feed.
166 * @param object $rows DatabaseBase resource with recentchanges rows
167 * @param Feed $feed
169 public static function generateFeed( $rows, &$feed ) {
170 $items = self::buildItems( $rows );
171 $feed->outHeader();
172 foreach ( $items as $item ) {
173 $feed->outItem( $item );
175 $feed->outFooter();
179 * Generate the feed items given a row from the database.
180 * @param object $rows DatabaseBase resource with recentchanges rows
181 * @return array
183 public static function buildItems( $rows ) {
184 $items = array();
186 # Merge adjacent edits by one user
187 $sorted = array();
188 $n = 0;
189 foreach ( $rows as $obj ) {
190 if ( $n > 0 &&
191 $obj->rc_type == RC_EDIT &&
192 $obj->rc_namespace >= 0 &&
193 $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
194 $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
195 $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
196 } else {
197 $sorted[$n] = $obj;
198 $n++;
202 foreach ( $sorted as $obj ) {
203 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
204 $talkpage = MWNamespace::canTalk( $obj->rc_namespace )
205 ? $title->getTalkPage()->getFullURL()
206 : '';
208 // Skip items with deleted content (avoids partially complete/inconsistent output)
209 if ( $obj->rc_deleted ) {
210 continue;
213 if ( $obj->rc_this_oldid ) {
214 $url = $title->getFullURL( array(
215 'diff' => $obj->rc_this_oldid,
216 'oldid' => $obj->rc_last_oldid,
217 ) );
218 } else {
219 // log entry or something like that.
220 $url = $title->getFullURL();
223 $items[] = new FeedItem(
224 $title->getPrefixedText(),
225 FeedUtils::formatDiff( $obj ),
226 $url,
227 $obj->rc_timestamp,
228 ( $obj->rc_deleted & Revision::DELETED_USER )
229 ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
230 $talkpage
234 return $items;