4 * Created on Oct 13, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiBase.php" );
32 * This action allows users to get their watchlist items in RSS/Atom formats.
33 * When executed, it performs a nested call to the API to get the needed data,
34 * and formats it in a proper format.
38 class ApiFeedWatchlist
extends ApiBase
{
40 public function __construct( $main, $action ) {
41 parent
::__construct( $main, $action );
45 * This module uses a custom feed wrapper printer.
47 public function getCustomPrinter() {
48 return new ApiFormatFeedWrapper( $this->getMain() );
52 * Make a nested call to the API to request watchlist items in the last $hours.
53 * Wrap the result as an RSS/Atom feed.
55 public function execute() {
56 global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgContLanguageCode;
59 $params = $this->extractRequestParams();
61 // limit to the number of hours going from now back
62 $endTime = wfTimestamp( TS_MW
, time() - intval( $params['hours'] * 60 * 60 ) );
64 $dbr = wfGetDB( DB_SLAVE
);
65 // Prepare parameters for nested request
69 'siprop' => 'general',
70 'list' => 'watchlist',
71 'wlprop' => 'title|user|comment|timestamp',
72 'wldir' => 'older', // reverse order - from newest to oldest
73 'wlend' => $dbr->timestamp( $endTime ), // stop at this time
74 'wllimit' => ( 50 > $wgFeedLimit ) ?
$wgFeedLimit : 50
77 if ( !is_null( $params['wlowner'] ) ) {
78 $fauxReqArr['wlowner'] = $params['wlowner'];
80 if ( !is_null( $params['wltoken'] ) ) {
81 $fauxReqArr['wltoken'] = $params['wltoken'];
84 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
85 if ( !is_null( $params['allrev'] ) ) {
86 $fauxReqArr['wlallrev'] = '';
90 $fauxReq = new FauxRequest( $fauxReqArr );
93 $module = new ApiMain( $fauxReq );
97 $data = $module->getResultData();
100 foreach ( (array)$data['query']['watchlist'] as $info ) {
101 $feedItems[] = $this->createFeedItem( $info );
104 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
105 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
107 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
109 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
111 } catch ( Exception
$e ) {
113 // Error results should not be cached
114 $this->getMain()->setCacheMaxAge( 0 );
116 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
117 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
119 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
120 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
122 if ( $e instanceof UsageException
) {
123 $errorCode = $e->getCodeString();
125 // Something is seriously wrong
126 $errorCode = 'internal_api_error';
129 $errorText = $e->getMessage();
130 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
131 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
135 private function createFeedItem( $info ) {
136 $titleStr = $info['title'];
137 $title = Title
::newFromText( $titleStr );
138 $titleUrl = $title->getFullURL();
139 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
140 $timestamp = $info['timestamp'];
141 $user = $info['user'];
143 $completeText = "$comment ($user)";
145 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
148 public function getAllowedParams() {
149 global $wgFeedClasses;
150 $feedFormatNames = array_keys( $wgFeedClasses );
152 'feedformat' => array(
153 ApiBase
::PARAM_DFLT
=> 'rss',
154 ApiBase
::PARAM_TYPE
=> $feedFormatNames
157 ApiBase
::PARAM_DFLT
=> 24,
158 ApiBase
::PARAM_TYPE
=> 'integer',
159 ApiBase
::PARAM_MIN
=> 1,
160 ApiBase
::PARAM_MAX
=> 72,
164 ApiBase
::PARAM_TYPE
=> 'user'
167 ApiBase
::PARAM_TYPE
=> 'string'
172 public function getParamDescription() {
174 'feedformat' => 'The format of the feed',
175 'hours' => 'List pages modified within this many hours from now',
176 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
177 'wlowner' => "The user whose watchlist you want (must be accompanied by wltoken if it's not you)",
178 'wltoken' => 'Security token that requested user set in their preferences'
182 public function getDescription() {
183 return 'This module returns a watchlist feed';
186 protected function getExamples() {
188 'api.php?action=feedwatchlist'
192 public function getVersion() {
193 return __CLASS__
. ': $Id$';