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