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