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 private $linkToDiffs = false;
37 private $watchlistModule = null;
40 * This module uses a custom feed wrapper printer.
42 * @return ApiFormatFeedWrapper
44 public function getCustomPrinter() {
45 return new ApiFormatFeedWrapper( $this->getMain() );
49 * Make a nested call to the API to request watchlist items in the last $hours.
50 * Wrap the result as an RSS/Atom feed.
52 public function execute() {
53 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
56 $params = $this->extractRequestParams();
59 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
62 if( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
63 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
66 // limit to the number of hours going from now back
67 $endTime = wfTimestamp( TS_MW
, time() - intval( $params['hours'] * 60 * 60 ) );
69 // Prepare parameters for nested request
73 'siprop' => 'general',
74 'list' => 'watchlist',
75 'wlprop' => 'title|user|comment|timestamp',
76 'wldir' => 'older', // reverse order - from newest to oldest
77 'wlend' => $endTime, // stop at this time
78 'wllimit' => ( 50 > $wgFeedLimit ) ?
$wgFeedLimit : 50
81 if ( $params['wlowner'] !== null ) {
82 $fauxReqArr['wlowner'] = $params['wlowner'];
84 if ( $params['wltoken'] !== null ) {
85 $fauxReqArr['wltoken'] = $params['wltoken'];
87 if ( $params['wlexcludeuser'] !== null ) {
88 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
90 if ( $params['wlshow'] !== null ) {
91 $fauxReqArr['wlshow'] = $params['wlshow'];
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 = wfMessage( 'watchlist' )->inContentLanguage()->text();
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 - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
135 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullURL();
137 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
138 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
139 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
141 if ( $e instanceof UsageException
) {
142 $errorCode = $e->getCodeString();
144 // Something is seriously wrong
145 $errorCode = 'internal_api_error';
148 $errorText = $e->getMessage();
149 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
150 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
158 private function createFeedItem( $info ) {
159 $titleStr = $info['title'];
160 $title = Title
::newFromText( $titleStr );
161 if ( $this->linkToDiffs
&& isset( $info['revid'] ) ) {
162 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
164 $titleUrl = $title->getFullURL();
166 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
167 $timestamp = $info['timestamp'];
168 $user = $info['user'];
170 $completeText = "$comment ($user)";
172 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
175 private function getWatchlistModule() {
176 if ( $this->watchlistModule
=== null ) {
177 $this->watchlistModule
= $this->getMain()->getModuleManager()->getModule( 'query' )
178 ->getModuleManager()->getModule( 'watchlist' );
180 return $this->watchlistModule
;
183 public function getAllowedParams( $flags = 0 ) {
184 global $wgFeedClasses;
185 $feedFormatNames = array_keys( $wgFeedClasses );
187 'feedformat' => array(
188 ApiBase
::PARAM_DFLT
=> 'rss',
189 ApiBase
::PARAM_TYPE
=> $feedFormatNames
192 ApiBase
::PARAM_DFLT
=> 24,
193 ApiBase
::PARAM_TYPE
=> 'integer',
194 ApiBase
::PARAM_MIN
=> 1,
195 ApiBase
::PARAM_MAX
=> 72,
197 'linktodiffs' => false,
200 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
201 $ret['allrev'] = $wlparams['allrev'];
202 $ret['wlowner'] = $wlparams['owner'];
203 $ret['wltoken'] = $wlparams['token'];
204 $ret['wlshow'] = $wlparams['show'];
205 $ret['wlexcludeuser'] = $wlparams['excludeuser'];
207 $ret['allrev'] = null;
208 $ret['wlowner'] = null;
209 $ret['wltoken'] = null;
210 $ret['wlshow'] = null;
211 $ret['wlexcludeuser'] = null;
216 public function getParamDescription() {
217 $wldescr = $this->getWatchlistModule()->getParamDescription();
219 'feedformat' => 'The format of the feed',
220 'hours' => 'List pages modified within this many hours from now',
221 'linktodiffs' => 'Link to change differences instead of article pages',
222 'allrev' => $wldescr['allrev'],
223 'wlowner' => $wldescr['owner'],
224 'wltoken' => $wldescr['token'],
225 'wlshow' => $wldescr['show'],
226 'wlexcludeuser' => $wldescr['excludeuser'],
230 public function getDescription() {
231 return 'Returns a watchlist feed';
234 public function getPossibleErrors() {
235 return array_merge( parent
::getPossibleErrors(), array(
236 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
237 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
241 public function getExamples() {
243 'api.php?action=feedwatchlist',
244 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
248 public function getHelpUrls() {
249 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';