2006-10-16 00:08:03 +00:00
|
|
|
<?php
|
2010-02-23 12:30:23 +00:00
|
|
|
/**
|
2010-12-22 20:52:06 +00:00
|
|
|
*
|
2006-10-16 00:08:03 +00:00
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Oct 13, 2006
|
|
|
|
|
*
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
|
2006-10-16 00:08:03 +00:00
|
|
|
*
|
|
|
|
|
* 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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-10-16 00:08:03 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2006-10-16 00:08:03 +00:00
|
|
|
*/
|
|
|
|
|
|
2007-04-20 08:55:14 +00:00
|
|
|
/**
|
2007-05-20 10:08:40 +00:00
|
|
|
* This action allows users to get their watchlist items in RSS/Atom formats.
|
|
|
|
|
* When executed, it performs a nested call to the API to get the needed data,
|
|
|
|
|
* and formats it in a proper format.
|
2008-04-14 07:45:50 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup API
|
2007-04-20 08:55:14 +00:00
|
|
|
*/
|
2006-10-16 00:08:03 +00:00
|
|
|
class ApiFeedWatchlist extends ApiBase {
|
|
|
|
|
|
2013-03-24 00:32:58 +00:00
|
|
|
private $watchlistModule = null;
|
2013-04-09 18:16:08 +00:00
|
|
|
private $linkToDiffs = false;
|
|
|
|
|
private $linkToSections = false;
|
2013-03-24 00:32:58 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* This module uses a custom feed wrapper printer.
|
2011-09-21 16:36:43 +00:00
|
|
|
*
|
|
|
|
|
* @return ApiFormatFeedWrapper
|
2007-05-20 10:08:40 +00:00
|
|
|
*/
|
2006-10-16 00:08:03 +00:00
|
|
|
public function getCustomPrinter() {
|
2010-01-11 15:55:52 +00:00
|
|
|
return new ApiFormatFeedWrapper( $this->getMain() );
|
2006-10-16 00:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-21 04:34:48 +00:00
|
|
|
/**
|
|
|
|
|
* Make a nested call to the API to request watchlist items in the last $hours.
|
|
|
|
|
* Wrap the result as an RSS/Atom feed.
|
|
|
|
|
*/
|
2006-10-16 00:08:03 +00:00
|
|
|
public function execute() {
|
2011-06-06 14:06:37 +00:00
|
|
|
global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
|
2006-10-16 00:08:03 +00:00
|
|
|
|
2007-05-19 05:15:24 +00:00
|
|
|
try {
|
2007-05-20 10:08:40 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2013-04-19 18:03:05 +00:00
|
|
|
if ( !$wgFeed ) {
|
2011-06-06 14:06:37 +00:00
|
|
|
$this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-19 18:03:05 +00:00
|
|
|
if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
|
2011-06-06 14:06:37 +00:00
|
|
|
$this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
// limit to the number of hours going from now back
|
2010-01-11 15:55:52 +00:00
|
|
|
$endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-09-26 03:24:01 +00:00
|
|
|
// Prepare parameters for nested request
|
2010-02-23 12:30:23 +00:00
|
|
|
$fauxReqArr = array(
|
2007-05-20 10:08:40 +00:00
|
|
|
'action' => 'query',
|
|
|
|
|
'meta' => 'siteinfo',
|
|
|
|
|
'siprop' => 'general',
|
|
|
|
|
'list' => 'watchlist',
|
2007-05-21 04:34:48 +00:00
|
|
|
'wlprop' => 'title|user|comment|timestamp',
|
2010-02-23 12:30:23 +00:00
|
|
|
'wldir' => 'older', // reverse order - from newest to oldest
|
2010-10-16 16:53:10 +00:00
|
|
|
'wlend' => $endTime, // stop at this time
|
2013-07-07 18:39:53 +00:00
|
|
|
'wllimit' => min( 50, $wgFeedLimit )
|
2007-09-26 03:24:01 +00:00
|
|
|
);
|
|
|
|
|
|
2013-03-10 21:08:22 +00:00
|
|
|
if ( $params['wlowner'] !== null ) {
|
2009-07-26 17:04:22 +00:00
|
|
|
$fauxReqArr['wlowner'] = $params['wlowner'];
|
2010-02-19 01:25:57 +00:00
|
|
|
}
|
2013-03-10 21:08:22 +00:00
|
|
|
if ( $params['wltoken'] !== null ) {
|
2009-07-24 01:22:06 +00:00
|
|
|
$fauxReqArr['wltoken'] = $params['wltoken'];
|
2010-02-19 01:25:57 +00:00
|
|
|
}
|
2013-03-10 21:08:22 +00:00
|
|
|
if ( $params['wlexcludeuser'] !== null ) {
|
|
|
|
|
$fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
|
|
|
|
|
}
|
2013-03-24 00:32:58 +00:00
|
|
|
if ( $params['wlshow'] !== null ) {
|
|
|
|
|
$fauxReqArr['wlshow'] = $params['wlshow'];
|
|
|
|
|
}
|
2013-04-30 16:01:00 +00:00
|
|
|
if ( $params['wltype'] !== null ) {
|
|
|
|
|
$fauxReqArr['wltype'] = $params['wltype'];
|
|
|
|
|
}
|
2009-07-24 01:22:06 +00:00
|
|
|
|
2010-11-27 19:24:48 +00:00
|
|
|
// Support linking to diffs instead of article
|
|
|
|
|
if ( $params['linktodiffs'] ) {
|
|
|
|
|
$this->linkToDiffs = true;
|
|
|
|
|
$fauxReqArr['wlprop'] .= '|ids';
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-09 18:16:08 +00:00
|
|
|
// Support linking directly to sections when possible
|
|
|
|
|
// (possible only if section name is present in comment)
|
|
|
|
|
if ( $params['linktosections'] ) {
|
|
|
|
|
$this->linkToSections = true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 03:24:01 +00:00
|
|
|
// Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
|
2011-10-13 16:40:22 +00:00
|
|
|
if ( $params['allrev'] ) {
|
2010-02-19 01:25:57 +00:00
|
|
|
$fauxReqArr['wlallrev'] = '';
|
|
|
|
|
}
|
2007-09-26 03:24:01 +00:00
|
|
|
|
|
|
|
|
// Create the request
|
2010-02-23 12:30:23 +00:00
|
|
|
$fauxReq = new FauxRequest( $fauxReqArr );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
// Execute
|
2010-01-11 15:55:52 +00:00
|
|
|
$module = new ApiMain( $fauxReq );
|
2007-05-19 05:15:24 +00:00
|
|
|
$module->execute();
|
2006-10-16 00:08:03 +00:00
|
|
|
|
2007-05-19 05:15:24 +00:00
|
|
|
// Get data array
|
|
|
|
|
$data = $module->getResultData();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-02-19 01:25:57 +00:00
|
|
|
$feedItems = array();
|
2010-01-11 15:55:52 +00:00
|
|
|
foreach ( (array)$data['query']['watchlist'] as $info ) {
|
|
|
|
|
$feedItems[] = $this->createFeedItem( $info );
|
2007-05-19 05:15:24 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-07-24 01:04:15 +00:00
|
|
|
$msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
|
2011-06-06 15:01:27 +00:00
|
|
|
|
|
|
|
|
$feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
|
2010-02-23 12:30:23 +00:00
|
|
|
$feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-06-06 15:01:27 +00:00
|
|
|
$feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-02-23 12:30:23 +00:00
|
|
|
ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
|
2010-01-11 15:55:52 +00:00
|
|
|
} catch ( Exception $e ) {
|
2006-10-16 00:08:03 +00:00
|
|
|
|
2007-05-19 05:15:24 +00:00
|
|
|
// Error results should not be cached
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->getMain()->setCacheMaxAge( 0 );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-07-24 01:04:15 +00:00
|
|
|
$feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
|
2010-02-23 12:30:23 +00:00
|
|
|
$feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
|
2012-07-24 01:04:15 +00:00
|
|
|
$msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
|
|
|
|
|
$feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
|
2006-10-18 23:49:09 +00:00
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( $e instanceof UsageException ) {
|
2007-05-19 05:15:24 +00:00
|
|
|
$errorCode = $e->getCodeString();
|
|
|
|
|
} else {
|
|
|
|
|
// Something is seriously wrong
|
|
|
|
|
$errorCode = 'internal_api_error';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$errorText = $e->getMessage();
|
2010-02-23 12:30:23 +00:00
|
|
|
$feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
|
|
|
|
|
ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
|
2007-05-19 05:15:24 +00:00
|
|
|
}
|
2006-10-16 00:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-27 00:46:17 +00:00
|
|
|
/**
|
|
|
|
|
* @param $info array
|
|
|
|
|
* @return FeedItem
|
|
|
|
|
*/
|
2010-01-11 15:55:52 +00:00
|
|
|
private function createFeedItem( $info ) {
|
2006-10-18 23:49:09 +00:00
|
|
|
$titleStr = $info['title'];
|
2010-02-23 12:30:23 +00:00
|
|
|
$title = Title::newFromText( $titleStr );
|
2010-12-03 19:32:25 +00:00
|
|
|
if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
|
2010-11-27 19:24:48 +00:00
|
|
|
$titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
|
|
|
|
|
} else {
|
|
|
|
|
$titleUrl = $title->getFullURL();
|
|
|
|
|
}
|
2006-11-29 05:45:03 +00:00
|
|
|
$comment = isset( $info['comment'] ) ? $info['comment'] : null;
|
2013-04-09 18:16:08 +00:00
|
|
|
|
|
|
|
|
// Create an anchor to section.
|
|
|
|
|
// The anchor won't work for sections that have dupes on page
|
|
|
|
|
// as there's no way to strip that info from ApiWatchlist (apparently?).
|
|
|
|
|
// RegExp in the line below is equal to Linker::formatAutocomments().
|
|
|
|
|
if ( $this->linkToSections && $comment !== null && preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches ) ) {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
$sectionTitle = $wgParser->stripSectionName( $matches[2] );
|
|
|
|
|
$sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle );
|
|
|
|
|
$titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL();
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-18 23:49:09 +00:00
|
|
|
$timestamp = $info['timestamp'];
|
|
|
|
|
$user = $info['user'];
|
|
|
|
|
|
|
|
|
|
$completeText = "$comment ($user)";
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
|
2006-10-18 23:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
2013-03-24 00:32:58 +00:00
|
|
|
private function getWatchlistModule() {
|
|
|
|
|
if ( $this->watchlistModule === null ) {
|
|
|
|
|
$this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
|
|
|
|
|
->getModuleManager()->getModule( 'watchlist' );
|
|
|
|
|
}
|
2013-11-14 12:40:22 +00:00
|
|
|
|
2013-03-24 00:32:58 +00:00
|
|
|
return $this->watchlistModule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getAllowedParams( $flags = 0 ) {
|
2006-10-16 00:08:03 +00:00
|
|
|
global $wgFeedClasses;
|
2010-01-11 15:55:52 +00:00
|
|
|
$feedFormatNames = array_keys( $wgFeedClasses );
|
2013-03-24 00:32:58 +00:00
|
|
|
$ret = array(
|
2010-02-23 12:30:23 +00:00
|
|
|
'feedformat' => array(
|
|
|
|
|
ApiBase::PARAM_DFLT => 'rss',
|
|
|
|
|
ApiBase::PARAM_TYPE => $feedFormatNames
|
2007-05-19 18:08:36 +00:00
|
|
|
),
|
2010-02-23 12:30:23 +00:00
|
|
|
'hours' => array(
|
|
|
|
|
ApiBase::PARAM_DFLT => 24,
|
|
|
|
|
ApiBase::PARAM_TYPE => 'integer',
|
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
|
|
|
|
ApiBase::PARAM_MAX => 72,
|
2007-09-26 03:24:01 +00:00
|
|
|
),
|
2010-11-27 19:24:48 +00:00
|
|
|
'linktodiffs' => false,
|
2013-04-09 18:16:08 +00:00
|
|
|
'linktosections' => false,
|
2006-10-16 00:08:03 +00:00
|
|
|
);
|
2013-03-24 00:32:58 +00:00
|
|
|
if ( $flags ) {
|
|
|
|
|
$wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
|
|
|
|
|
$ret['allrev'] = $wlparams['allrev'];
|
|
|
|
|
$ret['wlowner'] = $wlparams['owner'];
|
|
|
|
|
$ret['wltoken'] = $wlparams['token'];
|
|
|
|
|
$ret['wlshow'] = $wlparams['show'];
|
2013-04-30 16:01:00 +00:00
|
|
|
$ret['wltype'] = $wlparams['type'];
|
2013-03-24 00:32:58 +00:00
|
|
|
$ret['wlexcludeuser'] = $wlparams['excludeuser'];
|
|
|
|
|
} else {
|
|
|
|
|
$ret['allrev'] = null;
|
|
|
|
|
$ret['wlowner'] = null;
|
|
|
|
|
$ret['wltoken'] = null;
|
|
|
|
|
$ret['wlshow'] = null;
|
2013-04-30 16:01:00 +00:00
|
|
|
$ret['wltype'] = null;
|
2013-03-24 00:32:58 +00:00
|
|
|
$ret['wlexcludeuser'] = null;
|
|
|
|
|
}
|
2013-11-14 12:40:22 +00:00
|
|
|
|
2013-03-24 00:32:58 +00:00
|
|
|
return $ret;
|
2006-10-16 00:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2013-03-24 00:32:58 +00:00
|
|
|
$wldescr = $this->getWatchlistModule()->getParamDescription();
|
2013-11-14 12:40:22 +00:00
|
|
|
|
2010-02-23 12:30:23 +00:00
|
|
|
return array(
|
2007-05-19 18:08:36 +00:00
|
|
|
'feedformat' => 'The format of the feed',
|
2013-03-24 00:32:58 +00:00
|
|
|
'hours' => 'List pages modified within this many hours from now',
|
2011-11-14 14:36:06 +00:00
|
|
|
'linktodiffs' => 'Link to change differences instead of article pages',
|
2013-04-09 18:16:08 +00:00
|
|
|
'linktosections' => 'Link directly to changed sections if possible',
|
2013-03-24 00:32:58 +00:00
|
|
|
'allrev' => $wldescr['allrev'],
|
|
|
|
|
'wlowner' => $wldescr['owner'],
|
|
|
|
|
'wltoken' => $wldescr['token'],
|
|
|
|
|
'wlshow' => $wldescr['show'],
|
2013-04-30 16:01:00 +00:00
|
|
|
'wltype' => $wldescr['type'],
|
2013-03-24 00:32:58 +00:00
|
|
|
'wlexcludeuser' => $wldescr['excludeuser'],
|
2006-10-16 00:08:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2011-04-25 14:05:57 +00:00
|
|
|
return 'Returns a watchlist feed';
|
2006-10-16 00:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-06 14:06:37 +00:00
|
|
|
public function getPossibleErrors() {
|
|
|
|
|
return array_merge( parent::getPossibleErrors(), array(
|
|
|
|
|
array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
|
|
|
|
|
array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-17 22:24:21 +00:00
|
|
|
public function getExamples() {
|
2010-02-23 12:30:23 +00:00
|
|
|
return array(
|
2010-11-27 19:24:48 +00:00
|
|
|
'api.php?action=feedwatchlist',
|
2011-04-10 12:15:03 +00:00
|
|
|
'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
|
2006-10-16 00:08:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2011-11-28 15:43:11 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2006-10-16 00:08:03 +00:00
|
|
|
}
|