2006-10-16 00:08:03 +00:00
|
|
|
<?php
|
2010-02-23 12:30:23 +00:00
|
|
|
/**
|
2006-10-16 00:08:03 +00:00
|
|
|
* API for MediaWiki 1.8+
|
|
|
|
|
*
|
2010-08-07 19:59:42 +00:00
|
|
|
* Created on Oct 13, 2006
|
|
|
|
|
*
|
2010-02-23 12:30:23 +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
|
|
|
*/
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
2006-10-16 00:08:03 +00:00
|
|
|
// Eclipse helper - will be ignored in production
|
2010-02-23 12:30:23 +00:00
|
|
|
require_once( "ApiBase.php" );
|
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 {
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
public function __construct( $main, $action ) {
|
2010-02-23 12:30:23 +00:00
|
|
|
parent::__construct( $main, $action );
|
2006-10-16 00:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-05-20 10:08:40 +00:00
|
|
|
/**
|
|
|
|
|
* This module uses a custom feed wrapper printer.
|
|
|
|
|
*/
|
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() {
|
2008-05-16 21:34:46 +00:00
|
|
|
global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgContLanguageCode;
|
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
|
|
|
|
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
|
|
|
|
2008-01-21 19:37:07 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
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-01-11 15:55:52 +00:00
|
|
|
'wlend' => $dbr->timestamp( $endTime ), // stop at this time
|
|
|
|
|
'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
|
2007-09-26 03:24:01 +00:00
|
|
|
);
|
|
|
|
|
|
2010-02-19 01:25:57 +00:00
|
|
|
if ( !is_null( $params['wlowner'] ) ) {
|
2009-07-26 17:04:22 +00:00
|
|
|
$fauxReqArr['wlowner'] = $params['wlowner'];
|
2010-02-19 01:25:57 +00:00
|
|
|
}
|
|
|
|
|
if ( !is_null( $params['wltoken'] ) ) {
|
2009-07-24 01:22:06 +00:00
|
|
|
$fauxReqArr['wltoken'] = $params['wltoken'];
|
2010-02-19 01:25:57 +00:00
|
|
|
}
|
2009-07-24 01:22:06 +00:00
|
|
|
|
2007-09-26 03:24:01 +00:00
|
|
|
// Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
|
2010-02-23 12:30:23 +00:00
|
|
|
if ( !is_null( $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
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
|
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
|
|
|
$feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2010-02-23 12:30:23 +00:00
|
|
|
ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
|
2007-05-19 05:15:24 +00:00
|
|
|
|
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
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
$feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
|
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';
|
|
|
|
|
$feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $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
|
|
|
}
|
|
|
|
|
|
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 );
|
|
|
|
|
$titleUrl = $title->getFullURL();
|
2006-11-29 05:45:03 +00:00
|
|
|
$comment = isset( $info['comment'] ) ? $info['comment'] : null;
|
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
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2006-10-16 00:08:03 +00:00
|
|
|
global $wgFeedClasses;
|
2010-01-11 15:55:52 +00:00
|
|
|
$feedFormatNames = array_keys( $wgFeedClasses );
|
2006-10-16 00:08:03 +00:00
|
|
|
return 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
|
|
|
),
|
2009-07-24 01:22:06 +00:00
|
|
|
'allrev' => null,
|
2010-02-23 12:30:23 +00:00
|
|
|
'wlowner' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'user'
|
2009-07-24 01:22:06 +00:00
|
|
|
),
|
2010-02-23 12:30:23 +00:00
|
|
|
'wltoken' => array(
|
|
|
|
|
ApiBase::PARAM_TYPE => 'string'
|
2009-07-24 01:22:06 +00:00
|
|
|
)
|
2006-10-16 00:08:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getParamDescription() {
|
2010-02-23 12:30:23 +00:00
|
|
|
return array(
|
2007-05-19 18:08:36 +00:00
|
|
|
'feedformat' => 'The format of the feed',
|
2007-09-26 03:24:01 +00:00
|
|
|
'hours' => 'List pages modified within this many hours from now',
|
2010-05-11 22:30:18 +00:00
|
|
|
'allrev' => 'Include multiple revisions of the same page within given timeframe',
|
|
|
|
|
'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
|
2009-07-24 01:22:06 +00:00
|
|
|
'wltoken' => 'Security token that requested user set in their preferences'
|
2006-10-16 00:08:03 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getDescription() {
|
2006-10-16 00:08:03 +00:00
|
|
|
return 'This module returns a watchlist feed';
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-18 23:49:09 +00:00
|
|
|
protected function getExamples() {
|
2010-02-23 12:30:23 +00:00
|
|
|
return array(
|
2006-10-16 00:08:03 +00:00
|
|
|
'api.php?action=feedwatchlist'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVersion() {
|
2006-10-16 05:53:07 +00:00
|
|
|
return __CLASS__ . ': $Id$';
|
2006-10-16 00:08:03 +00:00
|
|
|
}
|
|
|
|
|
}
|