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 * @return ApiFormatFeedWrapper
50 public function getCustomPrinter() {
51 return new ApiFormatFeedWrapper( $this->getMain() );
54 private $linkToDiffs = false;
57 * Make a nested call to the API to request watchlist items in the last $hours.
58 * Wrap the result as an RSS/Atom feed.
60 public function execute() {
61 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
64 $params = $this->extractRequestParams();
67 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
70 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
71 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
74 // limit to the number of hours going from now back
75 $endTime = wfTimestamp( TS_MW
, time() - intval( $params['hours'] * 60 * 60 ) );
77 // Prepare parameters for nested request
81 'siprop' => 'general',
82 'list' => 'watchlist',
83 'wlprop' => 'title|user|comment|timestamp',
84 'wldir' => 'older', // reverse order - from newest to oldest
85 'wlend' => $endTime, // stop at this time
86 'wllimit' => ( 50 > $wgFeedLimit ) ?
$wgFeedLimit : 50
89 if ( !is_null( $params['wlowner'] ) ) {
90 $fauxReqArr['wlowner'] = $params['wlowner'];
92 if ( !is_null( $params['wltoken'] ) ) {
93 $fauxReqArr['wltoken'] = $params['wltoken'];
96 // Support linking to diffs instead of article
97 if ( $params['linktodiffs'] ) {
98 $this->linkToDiffs
= true;
99 $fauxReqArr['wlprop'] .= '|ids';
102 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
103 if ( $params['allrev'] ) {
104 $fauxReqArr['wlallrev'] = '';
107 // Create the request
108 $fauxReq = new FauxRequest( $fauxReqArr );
111 $module = new ApiMain( $fauxReq );
115 $data = $module->getResultData();
117 $feedItems = array();
118 foreach ( (array)$data['query']['watchlist'] as $info ) {
119 $feedItems[] = $this->createFeedItem( $info );
122 $msg = wfMsgForContent( 'watchlist' );
124 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
125 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
127 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
129 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
131 } catch ( Exception
$e ) {
133 // Error results should not be cached
134 $this->getMain()->setCacheMaxAge( 0 );
136 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
137 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
139 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
140 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
142 if ( $e instanceof UsageException
) {
143 $errorCode = $e->getCodeString();
145 // Something is seriously wrong
146 $errorCode = 'internal_api_error';
149 $errorText = $e->getMessage();
150 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
151 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
155 private function createFeedItem( $info ) {
156 $titleStr = $info['title'];
157 $title = Title
::newFromText( $titleStr );
158 if ( $this->linkToDiffs
&& isset( $info['revid'] ) ) {
159 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
161 $titleUrl = $title->getFullURL();
163 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
164 $timestamp = $info['timestamp'];
165 $user = $info['user'];
167 $completeText = "$comment ($user)";
169 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
172 public function getAllowedParams() {
173 global $wgFeedClasses;
174 $feedFormatNames = array_keys( $wgFeedClasses );
176 'feedformat' => array(
177 ApiBase
::PARAM_DFLT
=> 'rss',
178 ApiBase
::PARAM_TYPE
=> $feedFormatNames
181 ApiBase
::PARAM_DFLT
=> 24,
182 ApiBase
::PARAM_TYPE
=> 'integer',
183 ApiBase
::PARAM_MIN
=> 1,
184 ApiBase
::PARAM_MAX
=> 72,
188 ApiBase
::PARAM_TYPE
=> 'user'
191 ApiBase
::PARAM_TYPE
=> 'string'
193 'linktodiffs' => false,
197 public function getParamDescription() {
199 'feedformat' => 'The format of the feed',
200 'hours' => 'List pages modified within this many hours from now',
201 'allrev' => 'Include multiple revisions of the same page within given timeframe',
202 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
203 'wltoken' => 'Security token that requested user set in their preferences',
204 'linktodiffs' => 'Link to change differences instead of article pages'
208 public function getDescription() {
209 return 'Returns a watchlist feed';
212 public function getPossibleErrors() {
213 return array_merge( parent
::getPossibleErrors(), array(
214 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
215 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
219 public function getExamples() {
221 'api.php?action=feedwatchlist',
222 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
226 public function getHelpUrls() {
227 return 'http://www.mediawiki.org/wiki/API:Watchlist_feed';
230 public function getVersion() {
231 return __CLASS__
. ': $Id$';