API: Remove long-deprecated methods (and one class)
[lhc/web/wiklou.git] / includes / api / ApiFeedWatchlist.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * This action allows users to get their watchlist items in RSS/Atom formats.
25 * When executed, it performs a nested call to the API to get the needed data,
26 * and formats it in a proper format.
27 *
28 * @ingroup API
29 */
30 class ApiFeedWatchlist extends ApiBase {
31
32 private $watchlistModule = null;
33 private $linkToSections = false;
34
35 /**
36 * This module uses a custom feed wrapper printer.
37 *
38 * @return ApiFormatFeedWrapper
39 */
40 public function getCustomPrinter() {
41 return new ApiFormatFeedWrapper( $this->getMain() );
42 }
43
44 /**
45 * Make a nested call to the API to request watchlist items in the last $hours.
46 * Wrap the result as an RSS/Atom feed.
47 */
48 public function execute() {
49 $config = $this->getConfig();
50 $feedClasses = $config->get( 'FeedClasses' );
51 $params = [];
52 try {
53 $params = $this->extractRequestParams();
54
55 if ( !$config->get( 'Feed' ) ) {
56 $this->dieWithError( 'feed-unavailable' );
57 }
58
59 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
60 $this->dieWithError( 'feed-invalid' );
61 }
62
63 // limit to the number of hours going from now back
64 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
65
66 // Prepare parameters for nested request
67 $fauxReqArr = [
68 'action' => 'query',
69 'meta' => 'siteinfo',
70 'siprop' => 'general',
71 'list' => 'watchlist',
72 'wlprop' => 'title|user|comment|timestamp|ids',
73 'wldir' => 'older', // reverse order - from newest to oldest
74 'wlend' => $endTime, // stop at this time
75 'wllimit' => min( 50, $this->getConfig()->get( 'FeedLimit' ) )
76 ];
77
78 if ( $params['wlowner'] !== null ) {
79 $fauxReqArr['wlowner'] = $params['wlowner'];
80 }
81 if ( $params['wltoken'] !== null ) {
82 $fauxReqArr['wltoken'] = $params['wltoken'];
83 }
84 if ( $params['wlexcludeuser'] !== null ) {
85 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
86 }
87 if ( $params['wlshow'] !== null ) {
88 $fauxReqArr['wlshow'] = $params['wlshow'];
89 }
90 if ( $params['wltype'] !== null ) {
91 $fauxReqArr['wltype'] = $params['wltype'];
92 }
93
94 // Support linking directly to sections when possible
95 // (possible only if section name is present in comment)
96 if ( $params['linktosections'] ) {
97 $this->linkToSections = true;
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 $data = $module->getResult()->getResultData( [ 'query', 'watchlist' ] );
113 $feedItems = [];
114 foreach ( (array)$data as $key => $info ) {
115 if ( ApiResult::isMetadataKey( $key ) ) {
116 continue;
117 }
118 $feedItem = $this->createFeedItem( $info );
119 if ( $feedItem ) {
120 $feedItems[] = $feedItem;
121 }
122 }
123
124 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
125
126 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - ' . $msg .
127 ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
128 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
129
130 $feed = new $feedClasses[$params['feedformat']] (
131 $feedTitle,
132 htmlspecialchars( $msg ),
133 $feedUrl
134 );
135
136 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
137 } catch ( Exception $e ) {
138 // Error results should not be cached
139 $this->getMain()->setCacheMaxAge( 0 );
140
141 // @todo FIXME: Localise brackets
142 $feedTitle = $this->getConfig()->get( 'Sitename' ) . ' - Error - ' .
143 wfMessage( 'watchlist' )->inContentLanguage()->text() .
144 ' [' . $this->getConfig()->get( 'LanguageCode' ) . ']';
145 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
146
147 $feedFormat = $params['feedformat'] ?? 'rss';
148 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
149 $feed = new $feedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
150
151 if ( $e instanceof ApiUsageException ) {
152 foreach ( $e->getStatusValue()->getErrors() as $error ) {
153 $msg = ApiMessage::create( $error )
154 ->inLanguage( $this->getLanguage() );
155 $errorTitle = $this->msg( 'api-feed-error-title', $msg->getApiCode() );
156 $errorText = $msg->text();
157 $feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' );
158 }
159 } else {
160 // Something is seriously wrong
161 $errorCode = 'internal_api_error';
162 $errorTitle = $this->msg( 'api-feed-error-title', $errorCode );
163 $errorText = $e->getMessage();
164 $feedItems[] = new FeedItem( $errorTitle, $errorText, '', '', '' );
165 }
166
167 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
168 }
169 }
170
171 /**
172 * @param array $info
173 * @return FeedItem
174 */
175 private function createFeedItem( $info ) {
176 if ( !isset( $info['title'] ) ) {
177 // Probably a revdeled log entry, skip it.
178 return null;
179 }
180
181 $titleStr = $info['title'];
182 $title = Title::newFromText( $titleStr );
183 $curidParam = [];
184 if ( !$title || $title->isExternal() ) {
185 // Probably a formerly-valid title that's now conflicting with an
186 // interwiki prefix or the like.
187 if ( isset( $info['pageid'] ) ) {
188 $title = Title::newFromID( $info['pageid'] );
189 $curidParam = [ 'curid' => $info['pageid'] ];
190 }
191 if ( !$title || $title->isExternal() ) {
192 return null;
193 }
194 }
195 if ( isset( $info['revid'] ) ) {
196 $titleUrl = $title->getFullURL( [ 'diff' => $info['revid'] ] );
197 } else {
198 $titleUrl = $title->getFullURL( $curidParam );
199 }
200 $comment = $info['comment'] ?? null;
201
202 // Create an anchor to section.
203 // The anchor won't work for sections that have dupes on page
204 // as there's no way to strip that info from ApiWatchlist (apparently?).
205 // RegExp in the line below is equal to Linker::formatAutocomments().
206 if ( $this->linkToSections && $comment !== null &&
207 preg_match( '!(.*)/\*\s*(.*?)\s*\*/(.*)!', $comment, $matches )
208 ) {
209 global $wgParser;
210 $titleUrl .= $wgParser->guessSectionNameFromWikiText( $matches[ 2 ] );
211 }
212
213 $timestamp = $info['timestamp'];
214
215 if ( isset( $info['user'] ) ) {
216 $user = $info['user'];
217 $completeText = "$comment ($user)";
218 } else {
219 $user = '';
220 $completeText = (string)$comment;
221 }
222
223 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
224 }
225
226 private function getWatchlistModule() {
227 if ( $this->watchlistModule === null ) {
228 $this->watchlistModule = $this->getMain()->getModuleManager()->getModule( 'query' )
229 ->getModuleManager()->getModule( 'watchlist' );
230 }
231
232 return $this->watchlistModule;
233 }
234
235 public function getAllowedParams( $flags = 0 ) {
236 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
237 $ret = [
238 'feedformat' => [
239 ApiBase::PARAM_DFLT => 'rss',
240 ApiBase::PARAM_TYPE => $feedFormatNames
241 ],
242 'hours' => [
243 ApiBase::PARAM_DFLT => 24,
244 ApiBase::PARAM_TYPE => 'integer',
245 ApiBase::PARAM_MIN => 1,
246 ApiBase::PARAM_MAX => 72,
247 ],
248 'linktosections' => false,
249 ];
250
251 $copyParams = [
252 'allrev' => 'allrev',
253 'owner' => 'wlowner',
254 'token' => 'wltoken',
255 'show' => 'wlshow',
256 'type' => 'wltype',
257 'excludeuser' => 'wlexcludeuser',
258 ];
259 if ( $flags ) {
260 $wlparams = $this->getWatchlistModule()->getAllowedParams( $flags );
261 foreach ( $copyParams as $from => $to ) {
262 $p = $wlparams[$from];
263 if ( !is_array( $p ) ) {
264 $p = [ ApiBase::PARAM_DFLT => $p ];
265 }
266 if ( !isset( $p[ApiBase::PARAM_HELP_MSG] ) ) {
267 $p[ApiBase::PARAM_HELP_MSG] = "apihelp-query+watchlist-param-$from";
268 }
269 if ( isset( $p[ApiBase::PARAM_TYPE] ) && is_array( $p[ApiBase::PARAM_TYPE] ) &&
270 isset( $p[ApiBase::PARAM_HELP_MSG_PER_VALUE] )
271 ) {
272 foreach ( $p[ApiBase::PARAM_TYPE] as $v ) {
273 if ( !isset( $p[ApiBase::PARAM_HELP_MSG_PER_VALUE][$v] ) ) {
274 $p[ApiBase::PARAM_HELP_MSG_PER_VALUE][$v] = "apihelp-query+watchlist-paramvalue-$from-$v";
275 }
276 }
277 }
278 $ret[$to] = $p;
279 }
280 } else {
281 foreach ( $copyParams as $from => $to ) {
282 $ret[$to] = null;
283 }
284 }
285
286 return $ret;
287 }
288
289 protected function getExamplesMessages() {
290 return [
291 'action=feedwatchlist'
292 => 'apihelp-feedwatchlist-example-default',
293 'action=feedwatchlist&allrev=&hours=6'
294 => 'apihelp-feedwatchlist-example-all6hrs',
295 ];
296 }
297
298 public function getHelpUrls() {
299 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist_feed';
300 }
301 }