Merge "Break long lines in Action classes"
[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 $watchlistModule = null;
37 private $linkToDiffs = false;
38 private $linkToSections = false;
39
40 /**
41 * This module uses a custom feed wrapper printer.
42 *
43 * @return ApiFormatFeedWrapper
44 */
45 public function getCustomPrinter() {
46 return new ApiFormatFeedWrapper( $this->getMain() );
47 }
48
49 /**
50 * Make a nested call to the API to request watchlist items in the last $hours.
51 * Wrap the result as an RSS/Atom feed.
52 */
53 public function execute() {
54 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
55
56 try {
57 $params = $this->extractRequestParams();
58
59 if ( !$wgFeed ) {
60 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
61 }
62
63 if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
64 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
65 }
66
67 // limit to the number of hours going from now back
68 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
69
70 // Prepare parameters for nested request
71 $fauxReqArr = array(
72 'action' => 'query',
73 'meta' => 'siteinfo',
74 'siprop' => 'general',
75 'list' => 'watchlist',
76 'wlprop' => 'title|user|comment|timestamp',
77 'wldir' => 'older', // reverse order - from newest to oldest
78 'wlend' => $endTime, // stop at this time
79 'wllimit' => min( 50, $wgFeedLimit )
80 );
81
82 if ( $params['wlowner'] !== null ) {
83 $fauxReqArr['wlowner'] = $params['wlowner'];
84 }
85 if ( $params['wltoken'] !== null ) {
86 $fauxReqArr['wltoken'] = $params['wltoken'];
87 }
88 if ( $params['wlexcludeuser'] !== null ) {
89 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
90 }
91 if ( $params['wlshow'] !== null ) {
92 $fauxReqArr['wlshow'] = $params['wlshow'];
93 }
94 if ( $params['wltype'] !== null ) {
95 $fauxReqArr['wltype'] = $params['wltype'];
96 }
97
98 // Support linking to diffs instead of article
99 if ( $params['linktodiffs'] ) {
100 $this->linkToDiffs = true;
101 $fauxReqArr['wlprop'] .= '|ids';
102 }
103
104 // Support linking directly to sections when possible
105 // (possible only if section name is present in comment)
106 if ( $params['linktosections'] ) {
107 $this->linkToSections = true;
108 }
109
110 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
111 if ( $params['allrev'] ) {
112 $fauxReqArr['wlallrev'] = '';
113 }
114
115 // Create the request
116 $fauxReq = new FauxRequest( $fauxReqArr );
117
118 // Execute
119 $module = new ApiMain( $fauxReq );
120 $module->execute();
121
122 // Get data array
123 $data = $module->getResultData();
124
125 $feedItems = array();
126 foreach ( (array)$data['query']['watchlist'] as $info ) {
127 $feedItems[] = $this->createFeedItem( $info );
128 }
129
130 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
131
132 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
133 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
134
135 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
136
137 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
138 } catch ( Exception $e ) {
139
140 // Error results should not be cached
141 $this->getMain()->setCacheMaxAge( 0 );
142
143 $feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
144 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
145
146 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
147 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
148 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
149
150 if ( $e instanceof UsageException ) {
151 $errorCode = $e->getCodeString();
152 } else {
153 // Something is seriously wrong
154 $errorCode = 'internal_api_error';
155 }
156
157 $errorText = $e->getMessage();
158 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
159 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
160 }
161 }
162
163 /**
164 * @param $info array
165 * @return FeedItem
166 */
167 private function createFeedItem( $info ) {
168 $titleStr = $info['title'];
169 $title = Title::newFromText( $titleStr );
170 if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
171 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
172 } else {
173 $titleUrl = $title->getFullURL();
174 }
175 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
176
177 // Create an anchor to section.
178 // The anchor won't work for sections that have dupes on page
179 // as there's no way to strip that info from ApiWatchlist (apparently?).
180 // RegExp in the line below is equal to Linker::formatAutocomments().
181 if ( $this->linkToSections && $comment !== null && preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches ) ) {
182 global $wgParser;
183 $sectionTitle = $wgParser->stripSectionName( $matches[2] );
184 $sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle );
185 $titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL();
186 }
187
188 $timestamp = $info['timestamp'];
189 $user = $info['user'];
190
191 $completeText = "$comment ($user)";
192
193 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
194 }
195
196 private function getWatchlistModule() {
197 if ( $this->watchlistModule === null ) {
198 $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
199 ->getModuleManager()->getModule( 'watchlist' );
200 }
201
202 return $this->watchlistModule;
203 }
204
205 public function getAllowedParams( $flags = 0 ) {
206 global $wgFeedClasses;
207 $feedFormatNames = array_keys( $wgFeedClasses );
208 $ret = array(
209 'feedformat' => array(
210 ApiBase::PARAM_DFLT => 'rss',
211 ApiBase::PARAM_TYPE => $feedFormatNames
212 ),
213 'hours' => array(
214 ApiBase::PARAM_DFLT => 24,
215 ApiBase::PARAM_TYPE => 'integer',
216 ApiBase::PARAM_MIN => 1,
217 ApiBase::PARAM_MAX => 72,
218 ),
219 'linktodiffs' => false,
220 'linktosections' => false,
221 );
222 if ( $flags ) {
223 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
224 $ret['allrev'] = $wlparams['allrev'];
225 $ret['wlowner'] = $wlparams['owner'];
226 $ret['wltoken'] = $wlparams['token'];
227 $ret['wlshow'] = $wlparams['show'];
228 $ret['wltype'] = $wlparams['type'];
229 $ret['wlexcludeuser'] = $wlparams['excludeuser'];
230 } else {
231 $ret['allrev'] = null;
232 $ret['wlowner'] = null;
233 $ret['wltoken'] = null;
234 $ret['wlshow'] = null;
235 $ret['wltype'] = null;
236 $ret['wlexcludeuser'] = null;
237 }
238
239 return $ret;
240 }
241
242 public function getParamDescription() {
243 $wldescr = $this->getWatchlistModule()->getParamDescription();
244
245 return array(
246 'feedformat' => 'The format of the feed',
247 'hours' => 'List pages modified within this many hours from now',
248 'linktodiffs' => 'Link to change differences instead of article pages',
249 'linktosections' => 'Link directly to changed sections if possible',
250 'allrev' => $wldescr['allrev'],
251 'wlowner' => $wldescr['owner'],
252 'wltoken' => $wldescr['token'],
253 'wlshow' => $wldescr['show'],
254 'wltype' => $wldescr['type'],
255 'wlexcludeuser' => $wldescr['excludeuser'],
256 );
257 }
258
259 public function getDescription() {
260 return 'Returns a watchlist feed';
261 }
262
263 public function getPossibleErrors() {
264 return array_merge( parent::getPossibleErrors(), array(
265 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
266 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
267 ) );
268 }
269
270 public function getExamples() {
271 return array(
272 'api.php?action=feedwatchlist',
273 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
274 );
275 }
276
277 public function getHelpUrls() {
278 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
279 }
280 }