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
28 * This action allows users to get their watchlist items in RSS/Atom formats.
29 * When executed, it performs a nested call to the API to get the needed data,
30 * and formats it in a proper format.
34 class ApiFeedWatchlist
extends ApiBase
{
36 public function __construct( $main, $action ) {
37 parent
::__construct( $main, $action );
41 * This module uses a custom feed wrapper printer.
43 * @return ApiFormatFeedWrapper
45 public function getCustomPrinter() {
46 return new ApiFormatFeedWrapper( $this->getMain() );
49 private $linkToDiffs = false;
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 $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
59 $params = $this->extractRequestParams();
62 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
65 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
66 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
68 if ( !is_null( $params['wlexcludeuser'] ) ) {
69 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
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 ( $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 $msg = wfMsgForContent( 'watchlist' );
122 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
123 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
125 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
127 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
129 } catch ( Exception
$e ) {
131 // Error results should not be cached
132 $this->getMain()->setCacheMaxAge( 0 );
134 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
135 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
137 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
138 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
140 if ( $e instanceof UsageException
) {
141 $errorCode = $e->getCodeString();
143 // Something is seriously wrong
144 $errorCode = 'internal_api_error';
147 $errorText = $e->getMessage();
148 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
149 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
157 private function createFeedItem( $info ) {
158 $titleStr = $info['title'];
159 $title = Title
::newFromText( $titleStr );
160 if ( $this->linkToDiffs
&& isset( $info['revid'] ) ) {
161 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
163 $titleUrl = $title->getFullURL();
165 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
166 $timestamp = $info['timestamp'];
167 $user = $info['user'];
169 $completeText = "$comment ($user)";
171 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
174 public function getAllowedParams() {
175 global $wgFeedClasses;
176 $feedFormatNames = array_keys( $wgFeedClasses );
178 'feedformat' => array(
179 ApiBase
::PARAM_DFLT
=> 'rss',
180 ApiBase
::PARAM_TYPE
=> $feedFormatNames
183 ApiBase
::PARAM_DFLT
=> 24,
184 ApiBase
::PARAM_TYPE
=> 'integer',
185 ApiBase
::PARAM_MIN
=> 1,
186 ApiBase
::PARAM_MAX
=> 72,
190 ApiBase
::PARAM_TYPE
=> 'user'
193 ApiBase
::PARAM_TYPE
=> 'string'
195 'wlexcludeuser' => array(
196 ApiBase
::PARAM_TYPE
=> 'user'
198 'linktodiffs' => false,
202 public function getParamDescription() {
204 'feedformat' => 'The format of the feed',
205 'hours' => 'List pages modified within this many hours from now',
206 'allrev' => 'Include multiple revisions of the same page within given timeframe',
207 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}wltoken if it's not you)",
208 'wltoken' => 'Security token that requested user set in their preferences',
209 'wlexcludeuser' => 'A user whose edits should not be shown in the watchlist',
210 'linktodiffs' => 'Link to change differences instead of article pages',
214 public function getDescription() {
215 return 'Returns a watchlist feed';
218 public function getPossibleErrors() {
219 return array_merge( parent
::getPossibleErrors(), array(
220 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
221 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
225 public function getExamples() {
227 'api.php?action=feedwatchlist',
228 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
232 public function getHelpUrls() {
233 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
236 public function getVersion() {
237 return __CLASS__
. ': $Id$';