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