Make ApiFeedWatchlist obey $wgFeed, and also die with an error if an invalid feed...
[lhc/web/wiklou.git] / includes / api / ApiFeedWatchlist.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 13, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
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.
36 *
37 * @ingroup API
38 */
39 class ApiFeedWatchlist extends ApiBase {
40
41 public function __construct( $main, $action ) {
42 parent::__construct( $main, $action );
43 }
44
45 /**
46 * This module uses a custom feed wrapper printer.
47 */
48 public function getCustomPrinter() {
49 return new ApiFormatFeedWrapper( $this->getMain() );
50 }
51
52 private $linkToDiffs = false;
53
54 /**
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.
57 */
58 public function execute() {
59 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
60
61 try {
62 $params = $this->extractRequestParams();
63
64 if( !$wgFeed ) {
65 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
66 }
67
68 if( !isset( $wgFeedClasses[ $params['feed'] ] ) ) {
69 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
70 }
71
72 // limit to the number of hours going from now back
73 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
74
75 // Prepare parameters for nested request
76 $fauxReqArr = array(
77 'action' => 'query',
78 'meta' => 'siteinfo',
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
85 );
86
87 if ( !is_null( $params['wlowner'] ) ) {
88 $fauxReqArr['wlowner'] = $params['wlowner'];
89 }
90 if ( !is_null( $params['wltoken'] ) ) {
91 $fauxReqArr['wltoken'] = $params['wltoken'];
92 }
93
94 // Support linking to diffs instead of article
95 if ( $params['linktodiffs'] ) {
96 $this->linkToDiffs = true;
97 $fauxReqArr['wlprop'] .= '|ids';
98 }
99
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'] = '';
103 }
104
105 // Create the request
106 $fauxReq = new FauxRequest( $fauxReqArr );
107
108 // Execute
109 $module = new ApiMain( $fauxReq );
110 $module->execute();
111
112 // Get data array
113 $data = $module->getResultData();
114
115 $feedItems = array();
116 foreach ( (array)$data['query']['watchlist'] as $info ) {
117 $feedItems[] = $this->createFeedItem( $info );
118 }
119
120 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
121 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
122
123 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
124
125 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
126
127 } catch ( Exception $e ) {
128
129 // Error results should not be cached
130 $this->getMain()->setCacheMaxAge( 0 );
131
132 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
133 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
134
135 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
136 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
137
138 if ( $e instanceof UsageException ) {
139 $errorCode = $e->getCodeString();
140 } else {
141 // Something is seriously wrong
142 $errorCode = 'internal_api_error';
143 }
144
145 $errorText = $e->getMessage();
146 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
147 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
148 }
149 }
150
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'] ) );
156 } else {
157 $titleUrl = $title->getFullURL();
158 }
159 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
160 $timestamp = $info['timestamp'];
161 $user = $info['user'];
162
163 $completeText = "$comment ($user)";
164
165 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
166 }
167
168 public function getAllowedParams() {
169 global $wgFeedClasses;
170 $feedFormatNames = array_keys( $wgFeedClasses );
171 return array (
172 'feedformat' => array(
173 ApiBase::PARAM_DFLT => 'rss',
174 ApiBase::PARAM_TYPE => $feedFormatNames
175 ),
176 'hours' => array(
177 ApiBase::PARAM_DFLT => 24,
178 ApiBase::PARAM_TYPE => 'integer',
179 ApiBase::PARAM_MIN => 1,
180 ApiBase::PARAM_MAX => 72,
181 ),
182 'allrev' => false,
183 'wlowner' => array(
184 ApiBase::PARAM_TYPE => 'user'
185 ),
186 'wltoken' => array(
187 ApiBase::PARAM_TYPE => 'string'
188 ),
189 'linktodiffs' => false,
190 );
191 }
192
193 public function getParamDescription() {
194 return array(
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'
201 );
202 }
203
204 public function getDescription() {
205 return 'Returns a watchlist feed';
206 }
207
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' ),
212 ) );
213 }
214
215 protected function getExamples() {
216 return array(
217 'api.php?action=feedwatchlist',
218 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
219 );
220 }
221
222 public function getVersion() {
223 return __CLASS__ . ': $Id$';
224 }
225 }