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 $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
62 $params = $this->extractRequestParams();
65 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
68 if( !isset( $wgFeedClasses[ $params['feed'] ] ) ) {
69 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
72 // limit to the number of hours going from now back
73 $endTime = wfTimestamp( TS_MW
, time() - intval( $params['hours'] * 60 * 60 ) );
75 // Prepare parameters for nested request
79 'siprop' => 'general',
80 'list' => 'watchlist',
81 'wlprop' => 'title|user|comment|timestamp',
82 'wldir' => 'older', // reverse order - from newest to oldest
83 'wlend' => $endTime, // stop at this time
84 'wllimit' => ( 50 > $wgFeedLimit ) ?
$wgFeedLimit : 50
87 if ( !is_null( $params['wlowner'] ) ) {
88 $fauxReqArr['wlowner'] = $params['wlowner'];
90 if ( !is_null( $params['wltoken'] ) ) {
91 $fauxReqArr['wltoken'] = $params['wltoken'];
94 // Support linking to diffs instead of article
95 if ( $params['linktodiffs'] ) {
96 $this->linkToDiffs
= true;
97 $fauxReqArr['wlprop'] .= '|ids';
100 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
101 if ( !is_null( $params['allrev'] ) ) {
102 $fauxReqArr['wlallrev'] = '';
105 // Create the request
106 $fauxReq = new FauxRequest( $fauxReqArr );
109 $module = new ApiMain( $fauxReq );
113 $data = $module->getResultData();
115 $feedItems = array();
116 foreach ( (array)$data['query']['watchlist'] as $info ) {
117 $feedItems[] = $this->createFeedItem( $info );
120 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
121 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
123 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
125 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
127 } catch ( Exception
$e ) {
129 // Error results should not be cached
130 $this->getMain()->setCacheMaxAge( 0 );
132 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
133 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
135 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
136 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
138 if ( $e instanceof UsageException
) {
139 $errorCode = $e->getCodeString();
141 // Something is seriously wrong
142 $errorCode = 'internal_api_error';
145 $errorText = $e->getMessage();
146 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
147 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
151 private function createFeedItem( $info ) {
152 $titleStr = $info['title'];
153 $title = Title
::newFromText( $titleStr );
154 if ( $this->linkToDiffs
&& isset( $info['revid'] ) ) {
155 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
157 $titleUrl = $title->getFullURL();
159 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
160 $timestamp = $info['timestamp'];
161 $user = $info['user'];
163 $completeText = "$comment ($user)";
165 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
168 public function getAllowedParams() {
169 global $wgFeedClasses;
170 $feedFormatNames = array_keys( $wgFeedClasses );
172 'feedformat' => array(
173 ApiBase
::PARAM_DFLT
=> 'rss',
174 ApiBase
::PARAM_TYPE
=> $feedFormatNames
177 ApiBase
::PARAM_DFLT
=> 24,
178 ApiBase
::PARAM_TYPE
=> 'integer',
179 ApiBase
::PARAM_MIN
=> 1,
180 ApiBase
::PARAM_MAX
=> 72,
184 ApiBase
::PARAM_TYPE
=> 'user'
187 ApiBase
::PARAM_TYPE
=> 'string'
189 'linktodiffs' => false,
193 public function getParamDescription() {
195 'feedformat' => 'The format of the feed',
196 'hours' => 'List pages modified within this many hours from now',
197 'allrev' => 'Include multiple revisions of the same page within given timeframe',
198 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
199 'wltoken' => 'Security token that requested user set in their preferences',
200 'linktodiffs' => 'Link to change differences instead of article pages'
204 public function getDescription() {
205 return 'Returns a watchlist feed';
208 public function getPossibleErrors() {
209 return array_merge( parent
::getPossibleErrors(), array(
210 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
211 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
215 protected function getExamples() {
217 'api.php?action=feedwatchlist',
218 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
222 public function getVersion() {
223 return __CLASS__
. ': $Id$';