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
23 use Wikimedia\Rdbms\ResultWrapper
;
26 * Feed to Special:RecentChanges and Special:RecentChangesLiked
31 public $format, $type, $titleMsg, $descMsg;
36 * @param string $format Feed's format (either 'rss' or 'atom')
37 * @param string $type Type of feed (for cache keys)
39 public function __construct( $format, $type ) {
40 $this->format
= $format;
45 * Get a ChannelFeed subclass object to use
47 * @param string $title Feed's title
48 * @param string $description Feed's description
49 * @param string $url Url of origin page
50 * @return ChannelFeed|bool ChannelFeed subclass or false on failure
52 public function getFeedObject( $title, $description, $url ) {
53 global $wgSitename, $wgLanguageCode, $wgFeedClasses;
55 if ( !isset( $wgFeedClasses[$this->format
] ) ) {
59 if ( !array_key_exists( $this->format
, $wgFeedClasses ) ) {
60 // falling back to atom
61 $this->format
= 'atom';
64 $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
65 return new $wgFeedClasses[$this->format
](
66 $feedTitle, htmlspecialchars( $description ), $url );
70 * Generates feed's content
72 * @param ChannelFeed $feed ChannelFeed subclass object (generally the one returned
74 * @param ResultWrapper $rows ResultWrapper object with rows in recentchanges table
75 * @param int $lastmod Timestamp of the last item in the recentchanges table (only
76 * used for the cache key)
77 * @param FormOptions $opts As in SpecialRecentChanges::getDefaultOptions()
78 * @return null|bool True or null
80 public function execute( $feed, $rows, $lastmod, $opts ) {
81 global $wgLang, $wgRenderHashAppend;
83 if ( !FeedUtils
::checkFeedOutput( $this->format
) ) {
87 $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
89 $this->type
, $this->format
, $wgLang->getCode(), $optionsHash, 'timestamp' );
90 $key = wfMemcKey( $this->type
, $this->format
, $wgLang->getCode(), $optionsHash );
92 FeedUtils
::checkPurge( $timekey, $key );
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
99 $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
100 if ( is_string( $cachedFeed ) ) {
101 wfDebug( "RC: Outputting cached feed\n" );
102 $feed->httpHeaders();
105 wfDebug( "RC: rendering new feed and caching it\n" );
107 self
::generateFeed( $rows, $feed );
108 $cachedFeed = ob_get_contents();
110 $this->saveToCache( $cachedFeed, $timekey, $key );
116 * Save to feed result to cache
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
122 public function saveToCache( $feed, $timekey, $key ) {
123 $cache = ObjectCache
::getMainWANInstance();
124 $cache->set( $key, $feed, $cache::TTL_DAY
);
125 $cache->set( $timekey, wfTimestamp( TS_MW
), $cache::TTL_DAY
);
129 * Try to load the feed result from cache
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
136 public function loadFromCache( $lastmod, $timekey, $key ) {
137 global $wgFeedCacheTimeout, $wgOut;
139 $cache = ObjectCache
::getMainWANInstance();
140 $feedLastmod = $cache->get( $timekey );
142 if ( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
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.
150 $feedAge = time() - wfTimestamp( TS_UNIX
, $feedLastmod );
151 $feedLastmodUnix = wfTimestamp( TS_UNIX
, $feedLastmod );
152 $lastmodUnix = wfTimestamp( TS_UNIX
, $lastmod );
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
159 return $cache->get( $key );
161 wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
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
172 public static function generateFeed( $rows, &$feed ) {
173 $items = self
::buildItems( $rows );
175 foreach ( $items as $item ) {
176 $feed->outItem( $item );
182 * Generate the feed items given a row from the database.
183 * @param object $rows IDatabase resource with recentchanges rows
186 public static function buildItems( $rows ) {
189 # Merge adjacent edits by one user
192 foreach ( $rows as $obj ) {
193 if ( $obj->rc_type
== RC_EXTERNAL
) {
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
;
209 foreach ( $sorted as $obj ) {
210 $title = Title
::makeTitle( $obj->rc_namespace
, $obj->rc_title
);
211 $talkpage = MWNamespace
::canTalk( $obj->rc_namespace
)
212 ?
$title->getTalkPage()->getFullURL()
215 // Skip items with deleted content (avoids partially complete/inconsistent output)
216 if ( $obj->rc_deleted
) {
220 if ( $obj->rc_this_oldid
) {
221 $url = $title->getFullURL( [
222 'diff' => $obj->rc_this_oldid
,
223 'oldid' => $obj->rc_last_oldid
,
226 // log entry or something like that.
227 $url = $title->getFullURL();
230 $items[] = new FeedItem(
231 $title->getPrefixedText(),
232 FeedUtils
::formatDiff( $obj ),
235 ( $obj->rc_deleted
& Revision
::DELETED_USER
)
236 ?
wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text
,