Merge "QUnit: Re-enable config.requireExpects and add missing numbers."
[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 /**
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.
31 *
32 * @ingroup API
33 */
34 class ApiFeedWatchlist extends ApiBase {
35
36 private $linkToDiffs = false;
37 private $watchlistModule = null;
38
39 /**
40 * This module uses a custom feed wrapper printer.
41 *
42 * @return ApiFormatFeedWrapper
43 */
44 public function getCustomPrinter() {
45 return new ApiFormatFeedWrapper( $this->getMain() );
46 }
47
48 /**
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.
51 */
52 public function execute() {
53 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
54
55 try {
56 $params = $this->extractRequestParams();
57
58 if( !$wgFeed ) {
59 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
60 }
61
62 if( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
63 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
64 }
65
66 // limit to the number of hours going from now back
67 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
68
69 // Prepare parameters for nested request
70 $fauxReqArr = array(
71 'action' => 'query',
72 'meta' => 'siteinfo',
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
79 );
80
81 if ( $params['wlowner'] !== null ) {
82 $fauxReqArr['wlowner'] = $params['wlowner'];
83 }
84 if ( $params['wltoken'] !== null ) {
85 $fauxReqArr['wltoken'] = $params['wltoken'];
86 }
87 if ( $params['wlexcludeuser'] !== null ) {
88 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
89 }
90 if ( $params['wlshow'] !== null ) {
91 $fauxReqArr['wlshow'] = $params['wlshow'];
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 ( $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 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
121
122 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
123 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
124
125 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
126
127 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
128
129 } catch ( Exception $e ) {
130
131 // Error results should not be cached
132 $this->getMain()->setCacheMaxAge( 0 );
133
134 $feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
135 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
136
137 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
138 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
139 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
140
141 if ( $e instanceof UsageException ) {
142 $errorCode = $e->getCodeString();
143 } else {
144 // Something is seriously wrong
145 $errorCode = 'internal_api_error';
146 }
147
148 $errorText = $e->getMessage();
149 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
150 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
151 }
152 }
153
154 /**
155 * @param $info array
156 * @return FeedItem
157 */
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'] ) );
163 } else {
164 $titleUrl = $title->getFullURL();
165 }
166 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
167 $timestamp = $info['timestamp'];
168 $user = $info['user'];
169
170 $completeText = "$comment ($user)";
171
172 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
173 }
174
175 private function getWatchlistModule() {
176 if ( $this->watchlistModule === null ) {
177 $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
178 ->getModuleManager()->getModule( 'watchlist' );
179 }
180 return $this->watchlistModule;
181 }
182
183 public function getAllowedParams( $flags = 0 ) {
184 global $wgFeedClasses;
185 $feedFormatNames = array_keys( $wgFeedClasses );
186 $ret = array(
187 'feedformat' => array(
188 ApiBase::PARAM_DFLT => 'rss',
189 ApiBase::PARAM_TYPE => $feedFormatNames
190 ),
191 'hours' => array(
192 ApiBase::PARAM_DFLT => 24,
193 ApiBase::PARAM_TYPE => 'integer',
194 ApiBase::PARAM_MIN => 1,
195 ApiBase::PARAM_MAX => 72,
196 ),
197 'linktodiffs' => false,
198 );
199 if ( $flags ) {
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'];
206 } else {
207 $ret['allrev'] = null;
208 $ret['wlowner'] = null;
209 $ret['wltoken'] = null;
210 $ret['wlshow'] = null;
211 $ret['wlexcludeuser'] = null;
212 }
213 return $ret;
214 }
215
216 public function getParamDescription() {
217 $wldescr = $this->getWatchlistModule()->getParamDescription();
218 return array(
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'],
227 );
228 }
229
230 public function getDescription() {
231 return 'Returns a watchlist feed';
232 }
233
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' ),
238 ) );
239 }
240
241 public function getExamples() {
242 return array(
243 'api.php?action=feedwatchlist',
244 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
245 );
246 }
247
248 public function getHelpUrls() {
249 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
250 }
251 }