5 * Created on Oct 13, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
33 * This action allows users to get their watchlist items in RSS/Atom formats.
34 * When executed, it performs a nested call to the API to get the needed data,
35 * and formats it in a proper format.
39 class ApiFeedWatchlist
extends ApiBase
{
41 public function __construct( $main, $action ) {
42 parent
::__construct( $main, $action );
46 * This module uses a custom feed wrapper printer.
48 public function getCustomPrinter() {
49 return new ApiFormatFeedWrapper( $this->getMain() );
52 private $linkToDiffs = false;
55 * Make a nested call to the API to request watchlist items in the last $hours.
56 * Wrap the result as an RSS/Atom feed.
58 public function execute() {
59 global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
62 $params = $this->extractRequestParams();
64 // limit to the number of hours going from now back
65 $endTime = wfTimestamp( TS_MW
, time() - intval( $params['hours'] * 60 * 60 ) );
67 // Prepare parameters for nested request
71 'siprop' => 'general',
72 'list' => 'watchlist',
73 'wlprop' => 'title|user|comment|timestamp',
74 'wldir' => 'older', // reverse order - from newest to oldest
75 'wlend' => $endTime, // stop at this time
76 'wllimit' => ( 50 > $wgFeedLimit ) ?
$wgFeedLimit : 50
79 if ( !is_null( $params['wlowner'] ) ) {
80 $fauxReqArr['wlowner'] = $params['wlowner'];
82 if ( !is_null( $params['wltoken'] ) ) {
83 $fauxReqArr['wltoken'] = $params['wltoken'];
86 // Support linking to diffs instead of article
87 if ( $params['linktodiffs'] ) {
88 $this->linkToDiffs
= true;
89 $fauxReqArr['wlprop'] .= '|ids';
92 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
93 if ( !is_null( $params['allrev'] ) ) {
94 $fauxReqArr['wlallrev'] = '';
98 $fauxReq = new FauxRequest( $fauxReqArr );
101 $module = new ApiMain( $fauxReq );
105 $data = $module->getResultData();
107 $feedItems = array();
108 foreach ( (array)$data['query']['watchlist'] as $info ) {
109 $feedItems[] = $this->createFeedItem( $info );
112 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
113 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
115 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
117 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
119 } catch ( Exception
$e ) {
121 // Error results should not be cached
122 $this->getMain()->setCacheMaxAge( 0 );
124 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
125 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
127 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
128 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
130 if ( $e instanceof UsageException
) {
131 $errorCode = $e->getCodeString();
133 // Something is seriously wrong
134 $errorCode = 'internal_api_error';
137 $errorText = $e->getMessage();
138 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
139 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
143 private function createFeedItem( $info ) {
144 $titleStr = $info['title'];
145 $title = Title
::newFromText( $titleStr );
146 if ( $this->linkToDiffs
&& isset( $info['revid'] ) ) {
147 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
149 $titleUrl = $title->getFullURL();
151 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
152 $timestamp = $info['timestamp'];
153 $user = $info['user'];
155 $completeText = "$comment ($user)";
157 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
160 public function getAllowedParams() {
161 global $wgFeedClasses;
162 $feedFormatNames = array_keys( $wgFeedClasses );
164 'feedformat' => array(
165 ApiBase
::PARAM_DFLT
=> 'rss',
166 ApiBase
::PARAM_TYPE
=> $feedFormatNames
169 ApiBase
::PARAM_DFLT
=> 24,
170 ApiBase
::PARAM_TYPE
=> 'integer',
171 ApiBase
::PARAM_MIN
=> 1,
172 ApiBase
::PARAM_MAX
=> 72,
176 ApiBase
::PARAM_TYPE
=> 'user'
179 ApiBase
::PARAM_TYPE
=> 'string'
181 'linktodiffs' => false,
185 public function getParamDescription() {
187 'feedformat' => 'The format of the feed',
188 'hours' => 'List pages modified within this many hours from now',
189 'allrev' => 'Include multiple revisions of the same page within given timeframe',
190 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
191 'wltoken' => 'Security token that requested user set in their preferences',
192 'linktodiffs' => 'Link to change differences instead of article pages'
196 public function getDescription() {
197 return 'Returns a watchlist feed';
200 protected function getExamples() {
202 'api.php?action=feedwatchlist',
203 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
207 public function getVersion() {
208 return __CLASS__
. ': $Id$';