ApiQueryWatchlist: Don't reinvent Title::newFromLinkTarget()
[lhc/web/wiklou.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 25, 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 use MediaWiki\MediaWikiServices;
28
29 /**
30 * This query action allows clients to retrieve a list of recently modified pages
31 * that are part of the logged-in user's watchlist.
32 *
33 * @ingroup API
34 */
35 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
36
37 private $commentStore;
38
39 public function __construct( ApiQuery $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'wl' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 private $fld_ids = false, $fld_title = false, $fld_patrol = false,
52 $fld_flags = false, $fld_timestamp = false, $fld_user = false,
53 $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
54 $fld_notificationtimestamp = false, $fld_userid = false,
55 $fld_loginfo = false;
56
57 /**
58 * @param ApiPageSet $resultPageSet
59 * @return void
60 */
61 private function run( $resultPageSet = null ) {
62 $this->selectNamedDB( 'watchlist', DB_REPLICA, 'watchlist' );
63
64 $params = $this->extractRequestParams();
65
66 $user = $this->getUser();
67 $wlowner = $this->getWatchlistUser( $params );
68
69 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
70 $prop = array_flip( $params['prop'] );
71
72 $this->fld_ids = isset( $prop['ids'] );
73 $this->fld_title = isset( $prop['title'] );
74 $this->fld_flags = isset( $prop['flags'] );
75 $this->fld_user = isset( $prop['user'] );
76 $this->fld_userid = isset( $prop['userid'] );
77 $this->fld_comment = isset( $prop['comment'] );
78 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
79 $this->fld_timestamp = isset( $prop['timestamp'] );
80 $this->fld_sizes = isset( $prop['sizes'] );
81 $this->fld_patrol = isset( $prop['patrol'] );
82 $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
83 $this->fld_loginfo = isset( $prop['loginfo'] );
84
85 if ( $this->fld_patrol ) {
86 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
87 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'patrol' );
88 }
89 }
90
91 if ( $this->fld_comment || $this->fld_parsedcomment ) {
92 $this->commentStore = new CommentStore( 'rc_comment' );
93 }
94 }
95
96 $options = [
97 'dir' => $params['dir'] === 'older'
98 ? WatchedItemQueryService::DIR_OLDER
99 : WatchedItemQueryService::DIR_NEWER,
100 ];
101
102 if ( is_null( $resultPageSet ) ) {
103 $options['includeFields'] = $this->getFieldsToInclude();
104 } else {
105 $options['usedInGenerator'] = true;
106 }
107
108 if ( $params['start'] ) {
109 $options['start'] = $params['start'];
110 }
111 if ( $params['end'] ) {
112 $options['end'] = $params['end'];
113 }
114
115 $startFrom = null;
116 if ( !is_null( $params['continue'] ) ) {
117 $cont = explode( '|', $params['continue'] );
118 $this->dieContinueUsageIf( count( $cont ) != 2 );
119 $continueTimestamp = $cont[0];
120 $continueId = (int)$cont[1];
121 $this->dieContinueUsageIf( $continueId != $cont[1] );
122 $startFrom = [ $continueTimestamp, $continueId ];
123 }
124
125 if ( $wlowner !== $user ) {
126 $options['watchlistOwner'] = $wlowner;
127 $options['watchlistOwnerToken'] = $params['token'];
128 }
129
130 if ( !is_null( $params['namespace'] ) ) {
131 $options['namespaceIds'] = $params['namespace'];
132 }
133
134 if ( $params['allrev'] ) {
135 $options['allRevisions'] = true;
136 }
137
138 if ( !is_null( $params['show'] ) ) {
139 $show = array_flip( $params['show'] );
140
141 /* Check for conflicting parameters. */
142 if ( $this->showParamsConflicting( $show ) ) {
143 $this->dieWithError( 'apierror-show' );
144 }
145
146 // Check permissions.
147 if ( isset( $show[WatchedItemQueryService::FILTER_PATROLLED] )
148 || isset( $show[WatchedItemQueryService::FILTER_NOT_PATROLLED] )
149 ) {
150 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
151 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
152 }
153 }
154
155 $options['filters'] = array_keys( $show );
156 }
157
158 if ( !is_null( $params['type'] ) ) {
159 try {
160 $rcTypes = RecentChange::parseToRCType( $params['type'] );
161 if ( $rcTypes ) {
162 $options['rcTypes'] = $rcTypes;
163 }
164 } catch ( Exception $e ) {
165 ApiBase::dieDebug( __METHOD__, $e->getMessage() );
166 }
167 }
168
169 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
170 if ( !is_null( $params['user'] ) ) {
171 $options['onlyByUser'] = $params['user'];
172 }
173 if ( !is_null( $params['excludeuser'] ) ) {
174 $options['notByUser'] = $params['excludeuser'];
175 }
176
177 $options['limit'] = $params['limit'];
178
179 Hooks::run( 'ApiQueryWatchlistPrepareWatchedItemQueryServiceOptions', [
180 $this, $params, &$options
181 ] );
182
183 $ids = [];
184 $count = 0;
185 $watchedItemQuery = MediaWikiServices::getInstance()->getWatchedItemQueryService();
186 $items = $watchedItemQuery->getWatchedItemsWithRecentChangeInfo( $wlowner, $options, $startFrom );
187
188 foreach ( $items as list( $watchedItem, $recentChangeInfo ) ) {
189 /** @var WatchedItem $watchedItem */
190 if ( is_null( $resultPageSet ) ) {
191 $vals = $this->extractOutputData( $watchedItem, $recentChangeInfo );
192 $fit = $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
193 if ( !$fit ) {
194 $startFrom = [ $recentChangeInfo['rc_timestamp'], $recentChangeInfo['rc_id'] ];
195 break;
196 }
197 } else {
198 if ( $params['allrev'] ) {
199 $ids[] = intval( $recentChangeInfo['rc_this_oldid'] );
200 } else {
201 $ids[] = intval( $recentChangeInfo['rc_cur_id'] );
202 }
203 }
204 }
205
206 if ( $startFrom !== null ) {
207 $this->setContinueEnumParameter( 'continue', implode( '|', $startFrom ) );
208 }
209
210 if ( is_null( $resultPageSet ) ) {
211 $this->getResult()->addIndexedTagName(
212 [ 'query', $this->getModuleName() ],
213 'item'
214 );
215 } elseif ( $params['allrev'] ) {
216 $resultPageSet->populateFromRevisionIDs( $ids );
217 } else {
218 $resultPageSet->populateFromPageIDs( $ids );
219 }
220 }
221
222 private function getFieldsToInclude() {
223 $includeFields = [];
224 if ( $this->fld_flags ) {
225 $includeFields[] = WatchedItemQueryService::INCLUDE_FLAGS;
226 }
227 if ( $this->fld_user || $this->fld_userid ) {
228 $includeFields[] = WatchedItemQueryService::INCLUDE_USER_ID;
229 }
230 if ( $this->fld_user ) {
231 $includeFields[] = WatchedItemQueryService::INCLUDE_USER;
232 }
233 if ( $this->fld_comment || $this->fld_parsedcomment ) {
234 $includeFields[] = WatchedItemQueryService::INCLUDE_COMMENT;
235 }
236 if ( $this->fld_patrol ) {
237 $includeFields[] = WatchedItemQueryService::INCLUDE_PATROL_INFO;
238 }
239 if ( $this->fld_sizes ) {
240 $includeFields[] = WatchedItemQueryService::INCLUDE_SIZES;
241 }
242 if ( $this->fld_loginfo ) {
243 $includeFields[] = WatchedItemQueryService::INCLUDE_LOG_INFO;
244 }
245 return $includeFields;
246 }
247
248 private function showParamsConflicting( array $show ) {
249 return ( isset( $show[WatchedItemQueryService::FILTER_MINOR] )
250 && isset( $show[WatchedItemQueryService::FILTER_NOT_MINOR] ) )
251 || ( isset( $show[WatchedItemQueryService::FILTER_BOT] )
252 && isset( $show[WatchedItemQueryService::FILTER_NOT_BOT] ) )
253 || ( isset( $show[WatchedItemQueryService::FILTER_ANON] )
254 && isset( $show[WatchedItemQueryService::FILTER_NOT_ANON] ) )
255 || ( isset( $show[WatchedItemQueryService::FILTER_PATROLLED] )
256 && isset( $show[WatchedItemQueryService::FILTER_NOT_PATROLLED] ) )
257 || ( isset( $show[WatchedItemQueryService::FILTER_UNREAD] )
258 && isset( $show[WatchedItemQueryService::FILTER_NOT_UNREAD] ) );
259 }
260
261 private function extractOutputData( WatchedItem $watchedItem, array $recentChangeInfo ) {
262 /* Determine the title of the page that has been changed. */
263 $title = Title::newFromLinkTarget( $watchedItem->getLinkTarget() );
264 $user = $this->getUser();
265
266 /* Our output data. */
267 $vals = [];
268 $type = intval( $recentChangeInfo['rc_type'] );
269 $vals['type'] = RecentChange::parseFromRCType( $type );
270 $anyHidden = false;
271
272 /* Create a new entry in the result for the title. */
273 if ( $this->fld_title || $this->fld_ids ) {
274 // These should already have been filtered out of the query, but just in case.
275 if ( $type === RC_LOG && ( $recentChangeInfo['rc_deleted'] & LogPage::DELETED_ACTION ) ) {
276 $vals['actionhidden'] = true;
277 $anyHidden = true;
278 }
279 if ( $type !== RC_LOG ||
280 LogEventsList::userCanBitfield(
281 $recentChangeInfo['rc_deleted'],
282 LogPage::DELETED_ACTION,
283 $user
284 )
285 ) {
286 if ( $this->fld_title ) {
287 ApiQueryBase::addTitleInfo( $vals, $title );
288 }
289 if ( $this->fld_ids ) {
290 $vals['pageid'] = intval( $recentChangeInfo['rc_cur_id'] );
291 $vals['revid'] = intval( $recentChangeInfo['rc_this_oldid'] );
292 $vals['old_revid'] = intval( $recentChangeInfo['rc_last_oldid'] );
293 }
294 }
295 }
296
297 /* Add user data and 'anon' flag, if user is anonymous. */
298 if ( $this->fld_user || $this->fld_userid ) {
299 if ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_USER ) {
300 $vals['userhidden'] = true;
301 $anyHidden = true;
302 }
303 if ( Revision::userCanBitfield(
304 $recentChangeInfo['rc_deleted'],
305 Revision::DELETED_USER,
306 $user
307 ) ) {
308 if ( $this->fld_userid ) {
309 $vals['userid'] = (int)$recentChangeInfo['rc_user'];
310 // for backwards compatibility
311 $vals['user'] = (int)$recentChangeInfo['rc_user'];
312 }
313
314 if ( $this->fld_user ) {
315 $vals['user'] = $recentChangeInfo['rc_user_text'];
316 }
317
318 if ( !$recentChangeInfo['rc_user'] ) {
319 $vals['anon'] = true;
320 }
321 }
322 }
323
324 /* Add flags, such as new, minor, bot. */
325 if ( $this->fld_flags ) {
326 $vals['bot'] = (bool)$recentChangeInfo['rc_bot'];
327 $vals['new'] = $recentChangeInfo['rc_type'] == RC_NEW;
328 $vals['minor'] = (bool)$recentChangeInfo['rc_minor'];
329 }
330
331 /* Add sizes of each revision. (Only available on 1.10+) */
332 if ( $this->fld_sizes ) {
333 $vals['oldlen'] = intval( $recentChangeInfo['rc_old_len'] );
334 $vals['newlen'] = intval( $recentChangeInfo['rc_new_len'] );
335 }
336
337 /* Add the timestamp. */
338 if ( $this->fld_timestamp ) {
339 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $recentChangeInfo['rc_timestamp'] );
340 }
341
342 if ( $this->fld_notificationtimestamp ) {
343 $vals['notificationtimestamp'] = ( $watchedItem->getNotificationTimestamp() == null )
344 ? ''
345 : wfTimestamp( TS_ISO_8601, $watchedItem->getNotificationTimestamp() );
346 }
347
348 /* Add edit summary / log summary. */
349 if ( $this->fld_comment || $this->fld_parsedcomment ) {
350 if ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_COMMENT ) {
351 $vals['commenthidden'] = true;
352 $anyHidden = true;
353 }
354 if ( Revision::userCanBitfield(
355 $recentChangeInfo['rc_deleted'],
356 Revision::DELETED_COMMENT,
357 $user
358 ) ) {
359 $comment = $this->commentStore->getComment( $recentChangeInfo )->text;
360 if ( $this->fld_comment ) {
361 $vals['comment'] = $comment;
362 }
363
364 if ( $this->fld_parsedcomment ) {
365 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
366 }
367 }
368 }
369
370 /* Add the patrolled flag */
371 if ( $this->fld_patrol ) {
372 $vals['patrolled'] = $recentChangeInfo['rc_patrolled'] == 1;
373 $vals['unpatrolled'] = ChangesList::isUnpatrolled( (object)$recentChangeInfo, $user );
374 }
375
376 if ( $this->fld_loginfo && $recentChangeInfo['rc_type'] == RC_LOG ) {
377 if ( $recentChangeInfo['rc_deleted'] & LogPage::DELETED_ACTION ) {
378 $vals['actionhidden'] = true;
379 $anyHidden = true;
380 }
381 if ( LogEventsList::userCanBitfield(
382 $recentChangeInfo['rc_deleted'],
383 LogPage::DELETED_ACTION,
384 $user
385 ) ) {
386 $vals['logid'] = intval( $recentChangeInfo['rc_logid'] );
387 $vals['logtype'] = $recentChangeInfo['rc_log_type'];
388 $vals['logaction'] = $recentChangeInfo['rc_log_action'];
389 $vals['logparams'] = LogFormatter::newFromRow( $recentChangeInfo )->formatParametersForApi();
390 }
391 }
392
393 if ( $anyHidden && ( $recentChangeInfo['rc_deleted'] & Revision::DELETED_RESTRICTED ) ) {
394 $vals['suppressed'] = true;
395 }
396
397 Hooks::run( 'ApiQueryWatchlistExtractOutputData', [
398 $this, $watchedItem, $recentChangeInfo, &$vals
399 ] );
400
401 return $vals;
402 }
403
404 public function getAllowedParams() {
405 return [
406 'allrev' => false,
407 'start' => [
408 ApiBase::PARAM_TYPE => 'timestamp'
409 ],
410 'end' => [
411 ApiBase::PARAM_TYPE => 'timestamp'
412 ],
413 'namespace' => [
414 ApiBase::PARAM_ISMULTI => true,
415 ApiBase::PARAM_TYPE => 'namespace'
416 ],
417 'user' => [
418 ApiBase::PARAM_TYPE => 'user',
419 ],
420 'excludeuser' => [
421 ApiBase::PARAM_TYPE => 'user',
422 ],
423 'dir' => [
424 ApiBase::PARAM_DFLT => 'older',
425 ApiBase::PARAM_TYPE => [
426 'newer',
427 'older'
428 ],
429 ApiHelp::PARAM_HELP_MSG => 'api-help-param-direction',
430 ],
431 'limit' => [
432 ApiBase::PARAM_DFLT => 10,
433 ApiBase::PARAM_TYPE => 'limit',
434 ApiBase::PARAM_MIN => 1,
435 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
436 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
437 ],
438 'prop' => [
439 ApiBase::PARAM_ISMULTI => true,
440 ApiBase::PARAM_DFLT => 'ids|title|flags',
441 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
442 ApiBase::PARAM_TYPE => [
443 'ids',
444 'title',
445 'flags',
446 'user',
447 'userid',
448 'comment',
449 'parsedcomment',
450 'timestamp',
451 'patrol',
452 'sizes',
453 'notificationtimestamp',
454 'loginfo',
455 ]
456 ],
457 'show' => [
458 ApiBase::PARAM_ISMULTI => true,
459 ApiBase::PARAM_TYPE => [
460 WatchedItemQueryService::FILTER_MINOR,
461 WatchedItemQueryService::FILTER_NOT_MINOR,
462 WatchedItemQueryService::FILTER_BOT,
463 WatchedItemQueryService::FILTER_NOT_BOT,
464 WatchedItemQueryService::FILTER_ANON,
465 WatchedItemQueryService::FILTER_NOT_ANON,
466 WatchedItemQueryService::FILTER_PATROLLED,
467 WatchedItemQueryService::FILTER_NOT_PATROLLED,
468 WatchedItemQueryService::FILTER_UNREAD,
469 WatchedItemQueryService::FILTER_NOT_UNREAD,
470 ]
471 ],
472 'type' => [
473 ApiBase::PARAM_DFLT => 'edit|new|log|categorize',
474 ApiBase::PARAM_ISMULTI => true,
475 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
476 ApiBase::PARAM_TYPE => RecentChange::getChangeTypes()
477 ],
478 'owner' => [
479 ApiBase::PARAM_TYPE => 'user'
480 ],
481 'token' => [
482 ApiBase::PARAM_TYPE => 'string',
483 ApiBase::PARAM_SENSITIVE => true,
484 ],
485 'continue' => [
486 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
487 ],
488 ];
489 }
490
491 protected function getExamplesMessages() {
492 return [
493 'action=query&list=watchlist'
494 => 'apihelp-query+watchlist-example-simple',
495 'action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment'
496 => 'apihelp-query+watchlist-example-props',
497 'action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment'
498 => 'apihelp-query+watchlist-example-allrev',
499 'action=query&generator=watchlist&prop=info'
500 => 'apihelp-query+watchlist-example-generator',
501 'action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user'
502 => 'apihelp-query+watchlist-example-generator-rev',
503 'action=query&list=watchlist&wlowner=Example&wltoken=123ABC'
504 => 'apihelp-query+watchlist-example-wlowner',
505 ];
506 }
507
508 public function getHelpUrls() {
509 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Watchlist';
510 }
511 }