Merge "Chinese Conversion Table Update 2017-5"
[lhc/web/wiklou.git] / includes / changes / 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 use Wikimedia\Rdbms\ResultWrapper;
24
25 /**
26 * Feed to Special:RecentChanges and Special:RecentChangesLiked
27 *
28 * @ingroup Feed
29 */
30 class ChangesFeed {
31 public $format, $type, $titleMsg, $descMsg;
32
33 /**
34 * @param string $format Feed's format (either 'rss' or 'atom')
35 * @param string $type 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 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
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 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
77 */
78 public function execute( $feed, $rows, $lastmod, $opts ) {
79 global $wgLang, $wgRenderHashAppend;
80
81 if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
82 return null;
83 }
84
85 $cache = ObjectCache::getMainWANInstance();
86 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
87 $timekey = $cache->makeKey(
88 $this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
89 $key = $cache->makeKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
90
91 FeedUtils::checkPurge( $timekey, $key );
92
93 /**
94 * Bumping around loading up diffs can be pretty slow, so where
95 * possible we want to cache the feed output so the next visitor
96 * gets it quick too.
97 */
98 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
99 if ( is_string( $cachedFeed ) ) {
100 wfDebug( "RC: Outputting cached feed\n" );
101 $feed->httpHeaders();
102 echo $cachedFeed;
103 } else {
104 wfDebug( "RC: rendering new feed and caching it\n" );
105 ob_start();
106 self::generateFeed( $rows, $feed );
107 $cachedFeed = ob_get_contents();
108 ob_end_flush();
109 $this->saveToCache( $cachedFeed, $timekey, $key );
110 }
111 return true;
112 }
113
114 /**
115 * Save to feed result to cache
116 *
117 * @param string $feed Feed's content
118 * @param string $timekey Memcached key of the last modification
119 * @param string $key Memcached key of the content
120 */
121 public function saveToCache( $feed, $timekey, $key ) {
122 $cache = ObjectCache::getMainWANInstance();
123 $cache->set( $key, $feed, $cache::TTL_DAY );
124 $cache->set( $timekey, wfTimestamp( TS_MW ), $cache::TTL_DAY );
125 }
126
127 /**
128 * Try to load the feed result from cache
129 *
130 * @param int $lastmod Timestamp of the last item in the recentchanges table
131 * @param string $timekey Memcached key of the last modification
132 * @param string $key Memcached key of the content
133 * @return string|bool Feed's content on cache hit or false on cache miss
134 */
135 public function loadFromCache( $lastmod, $timekey, $key ) {
136 global $wgFeedCacheTimeout, $wgOut;
137
138 $cache = ObjectCache::getMainWANInstance();
139 $feedLastmod = $cache->get( $timekey );
140
141 if ( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
142 /**
143 * If the cached feed was rendered very recently, we may
144 * go ahead and use it even if there have been edits made
145 * since it was rendered. This keeps a swarm of requests
146 * from being too bad on a super-frequently edited wiki.
147 */
148
149 $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
150 $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
151 $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
152
153 if ( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix ) {
154 wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
155 if ( $feedLastmodUnix < $lastmodUnix ) {
156 $wgOut->setLastModified( $feedLastmod ); // T23916
157 }
158 return $cache->get( $key );
159 } else {
160 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
161 }
162 }
163 return false;
164 }
165
166 /**
167 * Generate the feed items given a row from the database, printing the feed.
168 * @param object $rows IDatabase resource with recentchanges rows
169 * @param ChannelFeed &$feed
170 */
171 public static function generateFeed( $rows, &$feed ) {
172 $items = self::buildItems( $rows );
173 $feed->outHeader();
174 foreach ( $items as $item ) {
175 $feed->outItem( $item );
176 }
177 $feed->outFooter();
178 }
179
180 /**
181 * Generate the feed items given a row from the database.
182 * @param object $rows IDatabase resource with recentchanges rows
183 * @return array
184 */
185 public static function buildItems( $rows ) {
186 $items = [];
187
188 # Merge adjacent edits by one user
189 $sorted = [];
190 $n = 0;
191 foreach ( $rows as $obj ) {
192 if ( $obj->rc_type == RC_EXTERNAL ) {
193 continue;
194 }
195
196 if ( $n > 0 &&
197 $obj->rc_type == RC_EDIT &&
198 $obj->rc_namespace >= 0 &&
199 $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
200 $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
201 $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
202 } else {
203 $sorted[$n] = $obj;
204 $n++;
205 }
206 }
207
208 foreach ( $sorted as $obj ) {
209 $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
210 $talkpage = MWNamespace::canTalk( $obj->rc_namespace )
211 ? $title->getTalkPage()->getFullURL()
212 : '';
213
214 // Skip items with deleted content (avoids partially complete/inconsistent output)
215 if ( $obj->rc_deleted ) {
216 continue;
217 }
218
219 if ( $obj->rc_this_oldid ) {
220 $url = $title->getFullURL( [
221 'diff' => $obj->rc_this_oldid,
222 'oldid' => $obj->rc_last_oldid,
223 ] );
224 } else {
225 // log entry or something like that.
226 $url = $title->getFullURL();
227 }
228
229 $items[] = new FeedItem(
230 $title->getPrefixedText(),
231 FeedUtils::formatDiff( $obj ),
232 $url,
233 $obj->rc_timestamp,
234 ( $obj->rc_deleted & Revision::DELETED_USER )
235 ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
236 $talkpage
237 );
238 }
239
240 return $items;
241 }
242 }