EditPage::newSectionSummary should return a value in all code paths
[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 $linkToSections = false;
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 $config = $this->getConfig();
54 $feedClasses = $config->get( 'FeedClasses' );
55 try {
56 $params = $this->extractRequestParams();
57
58 if ( !$config->get( 'Feed' ) ) {
59 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
60 }
61
62 if ( !isset( $feedClasses[$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|ids',
76 'wldir' => 'older', // reverse order - from newest to oldest
77 'wlend' => $endTime, // stop at this time
78 'wllimit' => min( 50, $this->getConfig()->get( 'FeedLimit' ) )
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 if ( $params['wltype'] !== null ) {
94 $fauxReqArr['wltype'] = $params['wltype'];
95 }
96
97 // Support linking directly to sections when possible
98 // (possible only if section name is present in comment)
99 if ( $params['linktosections'] ) {
100 $this->linkToSections = true;
101 }
102
103 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
104 if ( $params['allrev'] ) {
105 $fauxReqArr['wlallrev'] = '';
106 }
107
108 // Create the request
109 $fauxReq = new FauxRequest( $fauxReqArr );
110
111 // Execute
112 $module = new ApiMain( $fauxReq );
113 $module->execute();
114
115 // Get data array
116 $data = $module->getResultData();
117
118 $feedItems = array();
119 foreach ( (array)$data['query']['watchlist'] as $info ) {
120 $feedItems[] = $this->createFeedItem( $info );
121 }
122
123 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
124
125 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - ' . $msg . ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
126 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
127
128 $feed = new $feedClasses[$params['feedformat']] (
129 $feedTitle,
130 htmlspecialchars( $msg ),
131 $feedUrl
132 );
133
134 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
135 } catch ( Exception $e ) {
136 // Error results should not be cached
137 $this->getMain()->setCacheMaxAge( 0 );
138
139 // @todo FIXME: Localise brackets
140 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - Error - ' .
141 wfMessage( 'watchlist' )->inContentLanguage()->text() .
142 ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
143 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
144
145 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
146 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
147 $feed = new $feedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
148
149 if ( $e instanceof UsageException ) {
150 $errorCode = $e->getCodeString();
151 } else {
152 // Something is seriously wrong
153 $errorCode = 'internal_api_error';
154 }
155
156 $errorText = $e->getMessage();
157 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
158 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
159 }
160 }
161
162 /**
163 * @param array $info
164 * @return FeedItem
165 */
166 private function createFeedItem( $info ) {
167 $titleStr = $info['title'];
168 $title = Title::newFromText( $titleStr );
169 if ( isset( $info['revid'] ) ) {
170 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
171 } else {
172 $titleUrl = $title->getFullURL();
173 }
174 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
175
176 // Create an anchor to section.
177 // The anchor won't work for sections that have dupes on page
178 // as there's no way to strip that info from ApiWatchlist (apparently?).
179 // RegExp in the line below is equal to Linker::formatAutocomments().
180 if ( $this->linkToSections && $comment !== null &&
181 preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches )
182 ) {
183 global $wgParser;
184
185 $sectionTitle = $wgParser->stripSectionName( $matches[2] );
186 $sectionTitle = Sanitizer::normalizeSectionNameWhitespace( $sectionTitle );
187 $titleUrl .= Title::newFromText( '#' . $sectionTitle )->getFragmentForURL();
188 }
189
190 $timestamp = $info['timestamp'];
191 $user = $info['user'];
192
193 $completeText = "$comment ($user)";
194
195 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
196 }
197
198 private function getWatchlistModule() {
199 if ( $this->watchlistModule === null ) {
200 $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
201 ->getModuleManager()->getModule( 'watchlist' );
202 }
203
204 return $this->watchlistModule;
205 }
206
207 public function getAllowedParams( $flags = 0 ) {
208 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
209 $ret = array(
210 'feedformat' => array(
211 ApiBase::PARAM_DFLT => 'rss',
212 ApiBase::PARAM_TYPE => $feedFormatNames
213 ),
214 'hours' => array(
215 ApiBase::PARAM_DFLT => 24,
216 ApiBase::PARAM_TYPE => 'integer',
217 ApiBase::PARAM_MIN => 1,
218 ApiBase::PARAM_MAX => 72,
219 ),
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 'linktosections' => 'Link directly to changed sections if possible',
249 'allrev' => $wldescr['allrev'],
250 'wlowner' => $wldescr['owner'],
251 'wltoken' => $wldescr['token'],
252 'wlshow' => $wldescr['show'],
253 'wltype' => $wldescr['type'],
254 'wlexcludeuser' => $wldescr['excludeuser'],
255 );
256 }
257
258 public function getDescription() {
259 return 'Returns a watchlist feed.';
260 }
261
262 public function getPossibleErrors() {
263 return array_merge( parent::getPossibleErrors(), array(
264 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
265 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
266 ) );
267 }
268
269 public function getExamples() {
270 return array(
271 'api.php?action=feedwatchlist',
272 'api.php?action=feedwatchlist&allrev=&hours=6'
273 );
274 }
275
276 public function getHelpUrls() {
277 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
278 }
279 }