2008-06-17 08:24:00 +00:00
|
|
|
<?php
|
2012-05-21 19:56:04 +00:00
|
|
|
/**
|
|
|
|
|
* Feed for list of changes.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
|
2017-02-19 05:03:13 +00:00
|
|
|
use Wikimedia\Rdbms\ResultWrapper;
|
|
|
|
|
|
2010-01-09 21:59:40 +00:00
|
|
|
/**
|
|
|
|
|
* Feed to Special:RecentChanges and Special:RecentChangesLiked
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Feed
|
|
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
class ChangesFeed {
|
|
|
|
|
public $format, $type, $titleMsg, $descMsg;
|
|
|
|
|
|
2010-01-09 21:59:40 +00:00
|
|
|
/**
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param string $format Feed's format (either 'rss' or 'atom')
|
|
|
|
|
* @param string $type Type of feed (for cache keys)
|
2010-01-09 21:59:40 +00:00
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
public function __construct( $format, $type ) {
|
|
|
|
|
$this->format = $format;
|
|
|
|
|
$this->type = $type;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 21:59:40 +00:00
|
|
|
/**
|
|
|
|
|
* Get a ChannelFeed subclass object to use
|
|
|
|
|
*
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param string $title Feed's title
|
|
|
|
|
* @param string $description Feed's description
|
|
|
|
|
* @param string $url Url of origin page
|
|
|
|
|
* @return ChannelFeed|bool ChannelFeed subclass or false on failure
|
2010-01-09 21:59:40 +00:00
|
|
|
*/
|
2011-02-17 19:26:38 +00:00
|
|
|
public function getFeedObject( $title, $description, $url ) {
|
|
|
|
|
global $wgSitename, $wgLanguageCode, $wgFeedClasses;
|
|
|
|
|
|
|
|
|
|
if ( !isset( $wgFeedClasses[$this->format] ) ) {
|
2008-09-14 12:25:53 +00:00
|
|
|
return false;
|
2011-02-17 19:26:38 +00:00
|
|
|
}
|
|
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( !array_key_exists( $this->format, $wgFeedClasses ) ) {
|
2011-08-19 23:30:12 +00:00
|
|
|
// falling back to atom
|
|
|
|
|
$this->format = 'atom';
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-17 19:26:38 +00:00
|
|
|
$feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
|
2008-06-17 08:24:00 +00:00
|
|
|
return new $wgFeedClasses[$this->format](
|
2011-02-17 19:26:38 +00:00
|
|
|
$feedTitle, htmlspecialchars( $description ), $url );
|
2008-06-17 08:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-09 21:59:40 +00:00
|
|
|
/**
|
|
|
|
|
* Generates feed's content
|
|
|
|
|
*
|
2014-03-12 11:50:43 +00:00
|
|
|
* @param ChannelFeed $feed ChannelFeed subclass object (generally the one returned
|
|
|
|
|
* by getFeedObject())
|
|
|
|
|
* @param ResultWrapper $rows ResultWrapper object with rows in recentchanges table
|
|
|
|
|
* @param int $lastmod Timestamp of the last item in the recentchanges table (only
|
|
|
|
|
* used for the cache key)
|
|
|
|
|
* @param FormOptions $opts As in SpecialRecentChanges::getDefaultOptions()
|
2012-04-27 15:40:14 +00:00
|
|
|
* @return null|bool True or null
|
2010-01-09 21:59:40 +00:00
|
|
|
*/
|
2010-02-10 04:13:43 +00:00
|
|
|
public function execute( $feed, $rows, $lastmod, $opts ) {
|
2010-07-24 19:11:52 +00:00
|
|
|
global $wgLang, $wgRenderHashAppend;
|
2008-06-17 08:24:00 +00:00
|
|
|
|
|
|
|
|
if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
|
2012-04-27 15:40:14 +00:00
|
|
|
return null;
|
2008-06-17 08:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
2010-04-04 13:38:12 +00:00
|
|
|
$optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
|
2015-11-10 06:48:36 +00:00
|
|
|
$timekey = wfMemcKey(
|
|
|
|
|
$this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
|
2010-02-10 04:13:43 +00:00
|
|
|
$key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
|
2008-06-17 08:24:00 +00:00
|
|
|
|
2010-04-04 13:38:12 +00:00
|
|
|
FeedUtils::checkPurge( $timekey, $key );
|
2008-06-17 08:24:00 +00:00
|
|
|
|
2011-05-21 19:35:16 +00:00
|
|
|
/**
|
|
|
|
|
* Bumping around loading up diffs can be pretty slow, so where
|
|
|
|
|
* possible we want to cache the feed output so the next visitor
|
|
|
|
|
* gets it quick too.
|
|
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
$cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( is_string( $cachedFeed ) ) {
|
2008-06-17 08:24:00 +00:00
|
|
|
wfDebug( "RC: Outputting cached feed\n" );
|
|
|
|
|
$feed->httpHeaders();
|
|
|
|
|
echo $cachedFeed;
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( "RC: rendering new feed and caching it\n" );
|
|
|
|
|
ob_start();
|
|
|
|
|
self::generateFeed( $rows, $feed );
|
|
|
|
|
$cachedFeed = ob_get_contents();
|
|
|
|
|
ob_end_flush();
|
|
|
|
|
$this->saveToCache( $cachedFeed, $timekey, $key );
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-09 21:59:40 +00:00
|
|
|
/**
|
2015-11-10 06:48:36 +00:00
|
|
|
* Save to feed result to cache
|
2010-01-09 21:59:40 +00:00
|
|
|
*
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param string $feed Feed's content
|
|
|
|
|
* @param string $timekey Memcached key of the last modification
|
|
|
|
|
* @param string $key Memcached key of the content
|
2010-01-09 21:59:40 +00:00
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
public function saveToCache( $feed, $timekey, $key ) {
|
2015-11-10 06:48:36 +00:00
|
|
|
$cache = ObjectCache::getMainWANInstance();
|
|
|
|
|
$cache->set( $key, $feed, $cache::TTL_DAY );
|
|
|
|
|
$cache->set( $timekey, wfTimestamp( TS_MW ), $cache::TTL_DAY );
|
2008-06-17 08:24:00 +00:00
|
|
|
}
|
|
|
|
|
|
2010-01-09 21:59:40 +00:00
|
|
|
/**
|
2015-11-10 06:48:36 +00:00
|
|
|
* Try to load the feed result from cache
|
2010-01-09 21:59:40 +00:00
|
|
|
*
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param int $lastmod Timestamp of the last item in the recentchanges table
|
|
|
|
|
* @param string $timekey Memcached key of the last modification
|
|
|
|
|
* @param string $key Memcached key of the content
|
|
|
|
|
* @return string|bool Feed's content on cache hit or false on cache miss
|
2010-01-09 21:59:40 +00:00
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
public function loadFromCache( $lastmod, $timekey, $key ) {
|
2015-11-10 06:48:36 +00:00
|
|
|
global $wgFeedCacheTimeout, $wgOut;
|
2010-04-05 16:14:58 +00:00
|
|
|
|
2015-11-10 06:48:36 +00:00
|
|
|
$cache = ObjectCache::getMainWANInstance();
|
|
|
|
|
$feedLastmod = $cache->get( $timekey );
|
2008-06-17 08:24:00 +00:00
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
|
2013-02-03 20:05:24 +00:00
|
|
|
/**
|
2011-05-21 19:35:16 +00:00
|
|
|
* If the cached feed was rendered very recently, we may
|
|
|
|
|
* go ahead and use it even if there have been edits made
|
|
|
|
|
* since it was rendered. This keeps a swarm of requests
|
|
|
|
|
* from being too bad on a super-frequently edited wiki.
|
|
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
|
|
|
|
|
$feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
|
|
|
|
|
$feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
|
|
|
|
|
$lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
|
|
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix ) {
|
2008-06-17 08:24:00 +00:00
|
|
|
wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
|
2010-04-05 16:14:58 +00:00
|
|
|
if ( $feedLastmodUnix < $lastmodUnix ) {
|
2017-02-20 22:44:19 +00:00
|
|
|
$wgOut->setLastModified( $feedLastmod ); // T23916
|
2010-04-05 16:14:58 +00:00
|
|
|
}
|
2015-11-10 06:48:36 +00:00
|
|
|
return $cache->get( $key );
|
2008-06-17 08:24:00 +00:00
|
|
|
} else {
|
|
|
|
|
wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-02-02 16:30:44 +00:00
|
|
|
* Generate the feed items given a row from the database, printing the feed.
|
2016-09-26 22:40:07 +00:00
|
|
|
* @param object $rows IDatabase resource with recentchanges rows
|
2015-11-10 06:48:36 +00:00
|
|
|
* @param ChannelFeed $feed
|
2010-01-09 21:59:40 +00:00
|
|
|
*/
|
2008-06-17 08:24:00 +00:00
|
|
|
public static function generateFeed( $rows, &$feed ) {
|
2014-02-02 16:30:44 +00:00
|
|
|
$items = self::buildItems( $rows );
|
2008-06-17 08:24:00 +00:00
|
|
|
$feed->outHeader();
|
2014-02-02 16:30:44 +00:00
|
|
|
foreach ( $items as $item ) {
|
|
|
|
|
$feed->outItem( $item );
|
|
|
|
|
}
|
|
|
|
|
$feed->outFooter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate the feed items given a row from the database.
|
2016-09-26 22:40:07 +00:00
|
|
|
* @param object $rows IDatabase resource with recentchanges rows
|
2014-08-23 20:34:25 +00:00
|
|
|
* @return array
|
2014-02-02 16:30:44 +00:00
|
|
|
*/
|
|
|
|
|
public static function buildItems( $rows ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$items = [];
|
2008-06-17 08:24:00 +00:00
|
|
|
|
|
|
|
|
# Merge adjacent edits by one user
|
2016-02-17 09:09:32 +00:00
|
|
|
$sorted = [];
|
2008-06-17 08:24:00 +00:00
|
|
|
$n = 0;
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $rows as $obj ) {
|
2015-02-02 20:25:52 +00:00
|
|
|
if ( $obj->rc_type == RC_EXTERNAL ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $n > 0 &&
|
2011-06-19 05:45:40 +00:00
|
|
|
$obj->rc_type == RC_EDIT &&
|
2008-06-17 08:24:00 +00:00
|
|
|
$obj->rc_namespace >= 0 &&
|
2013-04-13 11:36:24 +00:00
|
|
|
$obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
|
|
|
|
|
$obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
|
|
|
|
|
$sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
|
2008-06-17 08:24:00 +00:00
|
|
|
} else {
|
|
|
|
|
$sorted[$n] = $obj;
|
|
|
|
|
$n++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-20 22:49:30 +00:00
|
|
|
foreach ( $sorted as $obj ) {
|
2008-06-17 08:24:00 +00:00
|
|
|
$title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
|
2014-03-12 11:50:43 +00:00
|
|
|
$talkpage = MWNamespace::canTalk( $obj->rc_namespace )
|
|
|
|
|
? $title->getTalkPage()->getFullURL()
|
|
|
|
|
: '';
|
|
|
|
|
|
2009-07-28 10:05:14 +00:00
|
|
|
// Skip items with deleted content (avoids partially complete/inconsistent output)
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $obj->rc_deleted ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2011-06-19 05:45:40 +00:00
|
|
|
|
|
|
|
|
if ( $obj->rc_this_oldid ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$url = $title->getFullURL( [
|
2013-04-19 12:53:20 +00:00
|
|
|
'diff' => $obj->rc_this_oldid,
|
|
|
|
|
'oldid' => $obj->rc_last_oldid,
|
2016-02-17 09:09:32 +00:00
|
|
|
] );
|
2011-06-19 05:45:40 +00:00
|
|
|
} else {
|
|
|
|
|
// log entry or something like that.
|
|
|
|
|
$url = $title->getFullURL();
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-02 16:30:44 +00:00
|
|
|
$items[] = new FeedItem(
|
2008-06-17 08:24:00 +00:00
|
|
|
$title->getPrefixedText(),
|
|
|
|
|
FeedUtils::formatDiff( $obj ),
|
2011-06-19 05:45:40 +00:00
|
|
|
$url,
|
2008-06-17 08:24:00 +00:00
|
|
|
$obj->rc_timestamp,
|
2013-08-24 15:06:25 +00:00
|
|
|
( $obj->rc_deleted & Revision::DELETED_USER )
|
|
|
|
|
? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
|
2011-05-14 12:24:45 +00:00
|
|
|
$talkpage
|
2009-07-28 10:05:14 +00:00
|
|
|
);
|
2008-06-17 08:24:00 +00:00
|
|
|
}
|
2014-02-02 16:30:44 +00:00
|
|
|
|
|
|
|
|
return $items;
|
2008-06-17 08:24:00 +00:00
|
|
|
}
|
2010-02-10 04:13:43 +00:00
|
|
|
}
|