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
{
37 * This module uses a custom feed wrapper printer.
39 * @return ApiFormatFeedWrapper
41 public function getCustomPrinter() {
42 return new ApiFormatFeedWrapper( $this->getMain() );
45 private $linkToDiffs = false;
48 * Make a nested call to the API to request watchlist items in the last $hours.
49 * Wrap the result as an RSS/Atom feed.
51 public function execute() {
52 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
55 $params = $this->extractRequestParams();
58 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
61 if( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
62 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
65 // limit to the number of hours going from now back
66 $endTime = wfTimestamp( TS_MW
, time() - intval( $params['hours'] * 60 * 60 ) );
68 // Prepare parameters for nested request
72 'siprop' => 'general',
73 'list' => 'watchlist',
74 'wlprop' => 'title|user|comment|timestamp',
75 'wldir' => 'older', // reverse order - from newest to oldest
76 'wlend' => $endTime, // stop at this time
77 'wllimit' => ( 50 > $wgFeedLimit ) ?
$wgFeedLimit : 50
80 if ( $params['wlowner'] !== null ) {
81 $fauxReqArr['wlowner'] = $params['wlowner'];
83 if ( $params['wltoken'] !== null ) {
84 $fauxReqArr['wltoken'] = $params['wltoken'];
86 if ( $params['wlexcludeuser'] !== null ) {
87 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
90 // Support linking to diffs instead of article
91 if ( $params['linktodiffs'] ) {
92 $this->linkToDiffs
= true;
93 $fauxReqArr['wlprop'] .= '|ids';
96 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
97 if ( $params['allrev'] ) {
98 $fauxReqArr['wlallrev'] = '';
101 // Create the request
102 $fauxReq = new FauxRequest( $fauxReqArr );
105 $module = new ApiMain( $fauxReq );
109 $data = $module->getResultData();
111 $feedItems = array();
112 foreach ( (array)$data['query']['watchlist'] as $info ) {
113 $feedItems[] = $this->createFeedItem( $info );
116 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
118 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
119 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
121 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
123 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
125 } catch ( Exception
$e ) {
127 // Error results should not be cached
128 $this->getMain()->setCacheMaxAge( 0 );
130 $feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
131 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
133 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
134 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
135 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
137 if ( $e instanceof UsageException
) {
138 $errorCode = $e->getCodeString();
140 // Something is seriously wrong
141 $errorCode = 'internal_api_error';
144 $errorText = $e->getMessage();
145 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
146 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
154 private function createFeedItem( $info ) {
155 $titleStr = $info['title'];
156 $title = Title
::newFromText( $titleStr );
157 if ( $this->linkToDiffs
&& isset( $info['revid'] ) ) {
158 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
160 $titleUrl = $title->getFullURL();
162 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
163 $timestamp = $info['timestamp'];
164 $user = $info['user'];
166 $completeText = "$comment ($user)";
168 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
171 public function getAllowedParams() {
172 global $wgFeedClasses;
173 $feedFormatNames = array_keys( $wgFeedClasses );
175 'feedformat' => array(
176 ApiBase
::PARAM_DFLT
=> 'rss',
177 ApiBase
::PARAM_TYPE
=> $feedFormatNames
180 ApiBase
::PARAM_DFLT
=> 24,
181 ApiBase
::PARAM_TYPE
=> 'integer',
182 ApiBase
::PARAM_MIN
=> 1,
183 ApiBase
::PARAM_MAX
=> 72,
187 ApiBase
::PARAM_TYPE
=> 'user'
190 ApiBase
::PARAM_TYPE
=> 'string'
192 'wlexcludeuser' => array(
193 ApiBase
::PARAM_TYPE
=> 'user'
195 'linktodiffs' => false,
199 public function getParamDescription() {
201 'feedformat' => 'The format of the feed',
202 'hours' => 'List pages modified within this many hours from now',
203 'allrev' => 'Include multiple revisions of the same page within given timeframe',
204 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}wltoken if it's not you)",
205 'wltoken' => 'Security token that requested user set in their preferences',
206 'wlexcludeuser' => 'A user whose edits should not be shown in the watchlist',
207 'linktodiffs' => 'Link to change differences instead of article pages',
211 public function getDescription() {
212 return 'Returns a watchlist feed';
215 public function getPossibleErrors() {
216 return array_merge( parent
::getPossibleErrors(), array(
217 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
218 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
222 public function getExamples() {
224 'api.php?action=feedwatchlist',
225 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
229 public function getHelpUrls() {
230 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';