MediaWiki  master
WatchedItemQueryService.php
Go to the documentation of this file.
1 <?php
2 
10 use Wikimedia\Assert\Assert;
13 
25 
26  public const DIR_OLDER = 'older';
27  public const DIR_NEWER = 'newer';
28 
29  public const INCLUDE_FLAGS = 'flags';
30  public const INCLUDE_USER = 'user';
31  public const INCLUDE_USER_ID = 'userid';
32  public const INCLUDE_COMMENT = 'comment';
33  public const INCLUDE_PATROL_INFO = 'patrol';
34  public const INCLUDE_AUTOPATROL_INFO = 'autopatrol';
35  public const INCLUDE_SIZES = 'sizes';
36  public const INCLUDE_LOG_INFO = 'loginfo';
37  public const INCLUDE_TAGS = 'tags';
38 
39  // FILTER_* constants are part of public API (are used in ApiQueryWatchlist and
40  // ApiQueryWatchlistRaw classes) and should not be changed.
41  // Changing values of those constants will result in a breaking change in the API
42  public const FILTER_MINOR = 'minor';
43  public const FILTER_NOT_MINOR = '!minor';
44  public const FILTER_BOT = 'bot';
45  public const FILTER_NOT_BOT = '!bot';
46  public const FILTER_ANON = 'anon';
47  public const FILTER_NOT_ANON = '!anon';
48  public const FILTER_PATROLLED = 'patrolled';
49  public const FILTER_NOT_PATROLLED = '!patrolled';
50  public const FILTER_AUTOPATROLLED = 'autopatrolled';
51  public const FILTER_NOT_AUTOPATROLLED = '!autopatrolled';
52  public const FILTER_UNREAD = 'unread';
53  public const FILTER_NOT_UNREAD = '!unread';
54  public const FILTER_CHANGED = 'changed';
55  public const FILTER_NOT_CHANGED = '!changed';
56 
57  public const SORT_ASC = 'ASC';
58  public const SORT_DESC = 'DESC';
59 
63  private $loadBalancer;
64 
66  private $extensions = null;
67 
69  private $commentStore;
70 
72  private $actorMigration;
73 
76 
79 
81  private $hookRunner;
82 
84  private $userFactory;
85 
89  private $expiryEnabled;
90 
91  public function __construct(
97  HookContainer $hookContainer,
99  bool $expiryEnabled = false
100  ) {
101  $this->loadBalancer = $loadBalancer;
102  $this->commentStore = $commentStore;
103  $this->actorMigration = $actorMigration;
104  $this->watchedItemStore = $watchedItemStore;
105  $this->permissionManager = $permissionManager;
106  $this->hookRunner = new HookRunner( $hookContainer );
107  $this->userFactory = $userFactory;
108  $this->expiryEnabled = $expiryEnabled;
109  }
110 
114  private function getExtensions() {
115  if ( $this->extensions === null ) {
116  $this->extensions = [];
117  $this->hookRunner->onWatchedItemQueryServiceExtensions( $this->extensions, $this );
118  }
119  return $this->extensions;
120  }
121 
125  private function getConnection() {
126  return $this->loadBalancer->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
127  }
128 
173  User $user, array $options = [], &$startFrom = null
174  ) {
175  $options += [
176  'includeFields' => [],
177  'namespaceIds' => [],
178  'filters' => [],
179  'allRevisions' => false,
180  'usedInGenerator' => false
181  ];
182 
183  Assert::parameter(
184  !isset( $options['rcTypes'] )
185  || !array_diff( $options['rcTypes'], [ RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL, RC_CATEGORIZE ] ),
186  '$options[\'rcTypes\']',
187  'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
188  );
189  Assert::parameter(
190  !isset( $options['dir'] ) || in_array( $options['dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
191  '$options[\'dir\']',
192  'must be DIR_OLDER or DIR_NEWER'
193  );
194  Assert::parameter(
195  !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
196  || isset( $options['dir'] ),
197  '$options[\'dir\']',
198  'must be provided when providing the "start" or "end" options or the $startFrom parameter'
199  );
200  Assert::parameter(
201  !isset( $options['startFrom'] ),
202  '$options[\'startFrom\']',
203  'must not be provided, use $startFrom instead'
204  );
205  Assert::parameter(
206  !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
207  '$startFrom',
208  'must be a two-element array'
209  );
210  if ( array_key_exists( 'watchlistOwner', $options ) ) {
211  Assert::parameterType(
212  User::class,
213  $options['watchlistOwner'],
214  '$options[\'watchlistOwner\']'
215  );
216  Assert::parameter(
217  isset( $options['watchlistOwnerToken'] ),
218  '$options[\'watchlistOwnerToken\']',
219  'must be provided when providing watchlistOwner option'
220  );
221  }
222 
223  $db = $this->getConnection();
224 
225  $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
226  $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
227  $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
228  $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
229  $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
230 
231  if ( $startFrom !== null ) {
232  $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
233  }
234 
235  foreach ( $this->getExtensions() as $extension ) {
236  $extension->modifyWatchedItemsWithRCInfoQuery(
237  $user, $options, $db,
238  $tables,
239  $fields,
240  $conds,
241  $dbOptions,
242  $joinConds
243  );
244  }
245 
246  $res = $db->select(
247  $tables,
248  $fields,
249  $conds,
250  __METHOD__,
251  $dbOptions,
252  $joinConds
253  );
254 
255  $limit = $dbOptions['LIMIT'] ?? INF;
256  $items = [];
257  $startFrom = null;
258  foreach ( $res as $row ) {
259  if ( --$limit <= 0 ) {
260  $startFrom = [ $row->rc_timestamp, $row->rc_id ];
261  break;
262  }
263 
264  $target = new TitleValue( (int)$row->rc_namespace, $row->rc_title );
265  $items[] = [
266  new WatchedItem(
267  $user,
268  $target,
269  $this->watchedItemStore->getLatestNotificationTimestamp(
270  $row->wl_notificationtimestamp, $user, $target
271  )
272  ),
273  $this->getRecentChangeFieldsFromRow( $row )
274  ];
275  }
276 
277  foreach ( $this->getExtensions() as $extension ) {
278  $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
279  }
280 
281  return $items;
282  }
283 
303  public function getWatchedItemsForUser( UserIdentity $user, array $options = [] ) {
304  if ( !$user->isRegistered() ) {
305  // TODO: should this just return an empty array or rather complain loud at this point
306  // as e.g. ApiBase::getWatchlistUser does?
307  return [];
308  }
309 
310  $options += [ 'namespaceIds' => [] ];
311 
312  Assert::parameter(
313  !isset( $options['sort'] ) || in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
314  '$options[\'sort\']',
315  'must be SORT_ASC or SORT_DESC'
316  );
317  Assert::parameter(
318  !isset( $options['filter'] ) || in_array(
319  $options['filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
320  ),
321  '$options[\'filter\']',
322  'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
323  );
324  Assert::parameter(
325  !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
326  || isset( $options['sort'] ),
327  '$options[\'sort\']',
328  'must be provided if any of "from", "until", "startFrom" options is provided'
329  );
330 
331  $db = $this->getConnection();
332 
333  $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
334  $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
335 
336  $tables = 'watchlist';
337  $joinConds = [];
338  if ( $this->expiryEnabled ) {
339  // If expiries are enabled, join with the watchlist_expiry table and exclude expired items.
340  $tables = [ 'watchlist', 'watchlist_expiry' ];
341  $conds[] = $db->makeList(
342  [ 'we_expiry' => null, 'we_expiry > ' . $db->addQuotes( $db->timestamp() ) ],
344  );
345  $joinConds['watchlist_expiry'] = [ 'LEFT JOIN', 'wl_id = we_item' ];
346  }
347  $res = $db->select(
348  $tables,
349  [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
350  $conds,
351  __METHOD__,
352  $dbOptions,
353  $joinConds
354  );
355 
356  $watchedItems = [];
357  foreach ( $res as $row ) {
358  $target = new TitleValue( (int)$row->wl_namespace, $row->wl_title );
359  // todo these could all be cached at some point?
360  $watchedItems[] = new WatchedItem(
361  $user,
362  $target,
363  $this->watchedItemStore->getLatestNotificationTimestamp(
364  $row->wl_notificationtimestamp, $user, $target
365  )
366  );
367  }
368 
369  return $watchedItems;
370  }
371 
372  private function getRecentChangeFieldsFromRow( stdClass $row ) {
373  // FIXME: This can be simplified to single array_filter call filtering by key value,
374  // now we have stopped supporting PHP 5.5
375  $allFields = get_object_vars( $row );
376  $rcKeys = array_filter(
377  array_keys( $allFields ),
378  function ( $key ) {
379  return substr( $key, 0, 3 ) === 'rc_';
380  }
381  );
382  return array_intersect_key( $allFields, array_flip( $rcKeys ) );
383  }
384 
385  private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
386  $tables = [ 'recentchanges', 'watchlist' ];
387 
388  if ( $this->expiryEnabled ) {
389  $tables[] = 'watchlist_expiry';
390  }
391 
392  if ( !$options['allRevisions'] ) {
393  $tables[] = 'page';
394  }
395  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
396  $tables += $this->commentStore->getJoin( 'rc_comment' )['tables'];
397  }
398  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ||
399  in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ||
400  in_array( self::FILTER_ANON, $options['filters'] ) ||
401  in_array( self::FILTER_NOT_ANON, $options['filters'] ) ||
402  array_key_exists( 'onlyByUser', $options ) || array_key_exists( 'notByUser', $options )
403  ) {
404  $tables += $this->actorMigration->getJoin( 'rc_user' )['tables'];
405  }
406  return $tables;
407  }
408 
409  private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
410  $fields = [
411  'rc_id',
412  'rc_namespace',
413  'rc_title',
414  'rc_timestamp',
415  'rc_type',
416  'rc_deleted',
417  'wl_notificationtimestamp'
418  ];
419 
420  if ( $this->expiryEnabled ) {
421  $fields[] = 'we_expiry';
422  }
423 
424  $rcIdFields = [
425  'rc_cur_id',
426  'rc_this_oldid',
427  'rc_last_oldid',
428  ];
429  if ( $options['usedInGenerator'] ) {
430  if ( $options['allRevisions'] ) {
431  $rcIdFields = [ 'rc_this_oldid' ];
432  } else {
433  $rcIdFields = [ 'rc_cur_id' ];
434  }
435  }
436  $fields = array_merge( $fields, $rcIdFields );
437 
438  if ( in_array( self::INCLUDE_FLAGS, $options['includeFields'] ) ) {
439  $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
440  }
441  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ) {
442  $fields['rc_user_text'] = $this->actorMigration->getJoin( 'rc_user' )['fields']['rc_user_text'];
443  }
444  if ( in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ) {
445  $fields['rc_user'] = $this->actorMigration->getJoin( 'rc_user' )['fields']['rc_user'];
446  }
447  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
448  $fields += $this->commentStore->getJoin( 'rc_comment' )['fields'];
449  }
450  if ( in_array( self::INCLUDE_PATROL_INFO, $options['includeFields'] ) ) {
451  $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
452  }
453  if ( in_array( self::INCLUDE_SIZES, $options['includeFields'] ) ) {
454  $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
455  }
456  if ( in_array( self::INCLUDE_LOG_INFO, $options['includeFields'] ) ) {
457  $fields = array_merge( $fields, [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ] );
458  }
459  if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
460  // prefixed with rc_ to include the field in getRecentChangeFieldsFromRow
461  $fields['rc_tags'] = ChangeTags::makeTagSummarySubquery( 'recentchanges' );
462  }
463 
464  return $fields;
465  }
466 
468  IDatabase $db,
469  User $user,
470  array $options
471  ) {
472  $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
473  $conds = [ 'wl_user' => $watchlistOwnerId ];
474 
475  if ( $this->expiryEnabled ) {
476  $conds[] = 'we_expiry IS NULL OR we_expiry > ' . $db->addQuotes( $db->timestamp() );
477  }
478 
479  if ( !$options['allRevisions'] ) {
480  $conds[] = $db->makeList(
481  [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG ],
482  LIST_OR
483  );
484  }
485 
486  if ( $options['namespaceIds'] ) {
487  $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
488  }
489 
490  if ( array_key_exists( 'rcTypes', $options ) ) {
491  $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
492  }
493 
494  $conds = array_merge(
495  $conds,
496  $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
497  );
498 
499  $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
500 
501  if ( !isset( $options['start'] ) && !isset( $options['end'] ) && $db->getType() === 'mysql' ) {
502  // This is an index optimization for mysql
503  $conds[] = 'rc_timestamp > ' . $db->addQuotes( '' );
504  }
505 
506  $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
507 
508  $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
509  if ( $deletedPageLogCond ) {
510  $conds[] = $deletedPageLogCond;
511  }
512 
513  return $conds;
514  }
515 
516  private function getWatchlistOwnerId( UserIdentity $user, array $options ) {
517  if ( array_key_exists( 'watchlistOwner', $options ) ) {
519  $watchlistOwner = $options['watchlistOwner'];
520  $ownersToken =
521  $watchlistOwner->getOption( 'watchlisttoken' );
522  $token = $options['watchlistOwnerToken'];
523  if ( $ownersToken == '' || !hash_equals( $ownersToken, $token ) ) {
524  throw ApiUsageException::newWithMessage( null, 'apierror-bad-watchlist-token', 'bad_wltoken' );
525  }
526  return $watchlistOwner->getId();
527  }
528  return $user->getId();
529  }
530 
531  private function getWatchedItemsWithRCInfoQueryFilterConds( User $user, array $options ) {
532  $conds = [];
533 
534  if ( in_array( self::FILTER_MINOR, $options['filters'] ) ) {
535  $conds[] = 'rc_minor != 0';
536  } elseif ( in_array( self::FILTER_NOT_MINOR, $options['filters'] ) ) {
537  $conds[] = 'rc_minor = 0';
538  }
539 
540  if ( in_array( self::FILTER_BOT, $options['filters'] ) ) {
541  $conds[] = 'rc_bot != 0';
542  } elseif ( in_array( self::FILTER_NOT_BOT, $options['filters'] ) ) {
543  $conds[] = 'rc_bot = 0';
544  }
545 
546  if ( in_array( self::FILTER_ANON, $options['filters'] ) ) {
547  $conds[] = $this->actorMigration->isAnon(
548  $this->actorMigration->getJoin( 'rc_user' )['fields']['rc_user']
549  );
550  } elseif ( in_array( self::FILTER_NOT_ANON, $options['filters'] ) ) {
551  $conds[] = $this->actorMigration->isNotAnon(
552  $this->actorMigration->getJoin( 'rc_user' )['fields']['rc_user']
553  );
554  }
555 
556  if ( $user->useRCPatrol() || $user->useNPPatrol() ) {
557  // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
558  // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
559  if ( in_array( self::FILTER_PATROLLED, $options['filters'] ) ) {
560  $conds[] = 'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED;
561  } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options['filters'] ) ) {
562  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
563  }
564 
565  if ( in_array( self::FILTER_AUTOPATROLLED, $options['filters'] ) ) {
566  $conds['rc_patrolled'] = RecentChange::PRC_AUTOPATROLLED;
567  } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options['filters'] ) ) {
568  $conds[] = 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED;
569  }
570  }
571 
572  if ( in_array( self::FILTER_UNREAD, $options['filters'] ) ) {
573  $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
574  } elseif ( in_array( self::FILTER_NOT_UNREAD, $options['filters'] ) ) {
575  // TODO: should this be changed to use Database::makeList?
576  $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
577  }
578 
579  return $conds;
580  }
581 
582  private function getStartEndConds( IDatabase $db, array $options ) {
583  if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
584  return [];
585  }
586 
587  $conds = [];
588 
589  if ( isset( $options['start'] ) ) {
590  $after = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
591  $conds[] = 'rc_timestamp ' . $after . ' ' .
592  $db->addQuotes( $db->timestamp( $options['start'] ) );
593  }
594  if ( isset( $options['end'] ) ) {
595  $before = $options['dir'] === self::DIR_OLDER ? '>=' : '<=';
596  $conds[] = 'rc_timestamp ' . $before . ' ' .
597  $db->addQuotes( $db->timestamp( $options['end'] ) );
598  }
599 
600  return $conds;
601  }
602 
603  private function getUserRelatedConds( IDatabase $db, UserIdentity $user, array $options ) {
604  if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
605  return [];
606  }
607 
608  $conds = [];
609 
610  if ( array_key_exists( 'onlyByUser', $options ) ) {
611  $byUser = $this->userFactory->newFromName(
612  $options['onlyByUser'],
613  UserFactory::RIGOR_NONE
614  );
615  $conds[] = $this->actorMigration->getWhere( $db, 'rc_user', $byUser )['conds'];
616  } elseif ( array_key_exists( 'notByUser', $options ) ) {
617  $byUser = $this->userFactory->newFromName(
618  $options['notByUser'],
619  UserFactory::RIGOR_NONE
620  );
621  $conds[] = 'NOT(' . $this->actorMigration->getWhere( $db, 'rc_user', $byUser )['conds'] . ')';
622  }
623 
624  // Avoid brute force searches (T19342)
625  $bitmask = 0;
626  if ( !$this->permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
627  $bitmask = RevisionRecord::DELETED_USER;
628  } elseif ( !$this->permissionManager
629  ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
630  ) {
631  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
632  }
633  if ( $bitmask ) {
634  $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
635  }
636 
637  return $conds;
638  }
639 
641  // LogPage::DELETED_ACTION hides the affected page, too. So hide those
642  // entirely from the watchlist, or someone could guess the title.
643  $bitmask = 0;
644  if ( !$this->permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
645  $bitmask = LogPage::DELETED_ACTION;
646  } elseif ( !$this->permissionManager
647  ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
648  ) {
650  }
651  if ( $bitmask ) {
652  return $db->makeList( [
653  'rc_type != ' . RC_LOG,
654  $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
655  ], LIST_OR );
656  }
657  return '';
658  }
659 
660  private function getStartFromConds( IDatabase $db, array $options, array $startFrom ) {
661  $op = $options['dir'] === self::DIR_OLDER ? '<' : '>';
662  list( $rcTimestamp, $rcId ) = $startFrom;
663  $rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
664  $rcId = (int)$rcId;
665  return $db->makeList(
666  [
667  "rc_timestamp $op $rcTimestamp",
668  $db->makeList(
669  [
670  "rc_timestamp = $rcTimestamp",
671  "rc_id $op= $rcId"
672  ],
673  LIST_AND
674  )
675  ],
676  LIST_OR
677  );
678  }
679 
681  IDatabase $db, UserIdentity $user, array $options
682  ) {
683  $conds = [ 'wl_user' => $user->getId() ];
684  if ( $options['namespaceIds'] ) {
685  $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
686  }
687  if ( isset( $options['filter'] ) ) {
688  $filter = $options['filter'];
689  if ( $filter === self::FILTER_CHANGED ) {
690  $conds[] = 'wl_notificationtimestamp IS NOT NULL';
691  } else {
692  $conds[] = 'wl_notificationtimestamp IS NULL';
693  }
694  }
695 
696  if ( isset( $options['from'] ) ) {
697  $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
698  $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
699  }
700  if ( isset( $options['until'] ) ) {
701  $op = $options['sort'] === self::SORT_ASC ? '<' : '>';
702  $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
703  }
704  if ( isset( $options['startFrom'] ) ) {
705  $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
706  $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
707  }
708 
709  return $conds;
710  }
711 
721  private function getFromUntilTargetConds( IDatabase $db, LinkTarget $target, $op ) {
722  return $db->makeList(
723  [
724  "wl_namespace $op " . $target->getNamespace(),
725  $db->makeList(
726  [
727  'wl_namespace = ' . $target->getNamespace(),
728  "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
729  ],
730  LIST_AND
731  )
732  ],
733  LIST_OR
734  );
735  }
736 
737  private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
738  $dbOptions = [];
739 
740  if ( array_key_exists( 'dir', $options ) ) {
741  $sort = $options['dir'] === self::DIR_OLDER ? ' DESC' : '';
742  $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
743  }
744 
745  if ( array_key_exists( 'limit', $options ) ) {
746  $dbOptions['LIMIT'] = (int)$options['limit'] + 1;
747  }
748 
749  return $dbOptions;
750  }
751 
752  private function getWatchedItemsForUserQueryDbOptions( array $options ) {
753  $dbOptions = [];
754  if ( array_key_exists( 'sort', $options ) ) {
755  $dbOptions['ORDER BY'] = [
756  "wl_namespace {$options['sort']}",
757  "wl_title {$options['sort']}"
758  ];
759  if ( count( $options['namespaceIds'] ) === 1 ) {
760  $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
761  }
762  }
763  if ( array_key_exists( 'limit', $options ) ) {
764  $dbOptions['LIMIT'] = (int)$options['limit'];
765  }
766  return $dbOptions;
767  }
768 
769  private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
770  $joinConds = [
771  'watchlist' => [ 'JOIN',
772  [
773  'wl_namespace=rc_namespace',
774  'wl_title=rc_title'
775  ]
776  ]
777  ];
778 
779  if ( $this->expiryEnabled ) {
780  $joinConds['watchlist_expiry'] = [ 'LEFT JOIN', 'wl_id = we_item' ];
781  }
782 
783  if ( !$options['allRevisions'] ) {
784  $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
785  }
786  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
787  $joinConds += $this->commentStore->getJoin( 'rc_comment' )['joins'];
788  }
789  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ||
790  in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ||
791  in_array( self::FILTER_ANON, $options['filters'] ) ||
792  in_array( self::FILTER_NOT_ANON, $options['filters'] ) ||
793  array_key_exists( 'onlyByUser', $options ) || array_key_exists( 'notByUser', $options )
794  ) {
795  $joinConds += $this->actorMigration->getJoin( 'rc_user' )['joins'];
796  }
797  return $joinConds;
798  }
799 
800 }
WatchedItemQueryService\getConnection
getConnection()
Definition: WatchedItemQueryService.php:125
ChangeTags\makeTagSummarySubquery
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:863
WatchedItemQueryService\getFromUntilTargetConds
getFromUntilTargetConds(IDatabase $db, LinkTarget $target, $op)
Creates a query condition part for getting only items before or after the given link target (while or...
Definition: WatchedItemQueryService.php:721
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:134
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryConds
getWatchedItemsWithRCInfoQueryConds(IDatabase $db, User $user, array $options)
Definition: WatchedItemQueryService.php:467
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryJoinConds
getWatchedItemsWithRCInfoQueryJoinConds(array $options)
Definition: WatchedItemQueryService.php:769
WatchedItemQueryServiceExtension
Definition: WatchedItemQueryServiceExtension.php:17
ApiUsageException\newWithMessage
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, Throwable $previous=null)
Definition: ApiUsageException.php:68
WatchedItemQueryService\$commentStore
CommentStore $commentStore
Definition: WatchedItemQueryService.php:69
WatchedItemQueryService\INCLUDE_COMMENT
const INCLUDE_COMMENT
Definition: WatchedItemQueryService.php:32
WatchedItemQueryService\FILTER_NOT_AUTOPATROLLED
const FILTER_NOT_AUTOPATROLLED
Definition: WatchedItemQueryService.php:51
WatchedItemQueryService\FILTER_PATROLLED
const FILTER_PATROLLED
Definition: WatchedItemQueryService.php:48
WatchedItemQueryService\$userFactory
UserFactory $userFactory
Definition: WatchedItemQueryService.php:84
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryFields
getWatchedItemsWithRCInfoQueryFields(array $options)
Definition: WatchedItemQueryService.php:409
WatchedItemQueryService\getStartEndConds
getStartEndConds(IDatabase $db, array $options)
Definition: WatchedItemQueryService.php:582
RC_LOG
const RC_LOG
Definition: Defines.php:133
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryDbOptions
getWatchedItemsWithRCInfoQueryDbOptions(array $options)
Definition: WatchedItemQueryService.php:737
CommentStore
Handle database storage of comments such as edit summaries and log reasons.
Definition: CommentStore.php:42
WatchedItemQueryService\$hookRunner
HookRunner $hookRunner
Definition: WatchedItemQueryService.php:81
RC_EDIT
const RC_EDIT
Definition: Defines.php:131
WatchedItemQueryService\getWatchedItemsForUserQueryDbOptions
getWatchedItemsForUserQueryDbOptions(array $options)
Definition: WatchedItemQueryService.php:752
User\useNPPatrol
useNPPatrol()
Check whether to enable new pages patrol features for this user.
Definition: User.php:3056
WatchedItemQueryService\getWatchedItemsWithRecentChangeInfo
getWatchedItemsWithRecentChangeInfo(User $user, array $options=[], &$startFrom=null)
Definition: WatchedItemQueryService.php:172
ActorMigration
This class handles the logic for the actor table migration and should always be used in lieu of direc...
Definition: ActorMigration.php:41
$res
$res
Definition: testCompression.php:57
User\useRCPatrol
useRCPatrol()
Check whether to enable recent changes patrol features for this user.
Definition: User.php:3047
WatchedItemQueryService\$actorMigration
ActorMigration $actorMigration
Definition: WatchedItemQueryService.php:72
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
LIST_AND
const LIST_AND
Definition: Defines.php:48
WatchedItemQueryService\INCLUDE_TAGS
const INCLUDE_TAGS
Definition: WatchedItemQueryService.php:37
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
WatchedItemQueryService\getWatchlistOwnerId
getWatchlistOwnerId(UserIdentity $user, array $options)
Definition: WatchedItemQueryService.php:516
WatchedItemQueryService\getWatchedItemsForUserQueryConds
getWatchedItemsForUserQueryConds(IDatabase $db, UserIdentity $user, array $options)
Definition: WatchedItemQueryService.php:680
WatchedItemQueryService\INCLUDE_LOG_INFO
const INCLUDE_LOG_INFO
Definition: WatchedItemQueryService.php:36
MediaWiki\Linker\LinkTarget\getNamespace
getNamespace()
Get the namespace index.
Wikimedia\Rdbms\IDatabase\timestamp
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by ConvertibleTimestamp to the format used for ins...
LIST_OR
const LIST_OR
Definition: Defines.php:51
WatchedItemQueryService\$watchedItemStore
WatchedItemStoreInterface $watchedItemStore
Definition: WatchedItemQueryService.php:75
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryFilterConds
getWatchedItemsWithRCInfoQueryFilterConds(User $user, array $options)
Definition: WatchedItemQueryService.php:531
MediaWiki\User\UserIdentity\isRegistered
isRegistered()
WatchedItemQueryService\FILTER_MINOR
const FILTER_MINOR
Definition: WatchedItemQueryService.php:42
WatchedItemQueryService\FILTER_NOT_CHANGED
const FILTER_NOT_CHANGED
Definition: WatchedItemQueryService.php:55
WatchedItemQueryService\INCLUDE_AUTOPATROL_INFO
const INCLUDE_AUTOPATROL_INFO
Definition: WatchedItemQueryService.php:34
WatchedItemQueryService\INCLUDE_USER
const INCLUDE_USER
Definition: WatchedItemQueryService.php:30
WatchedItemQueryService\FILTER_NOT_BOT
const FILTER_NOT_BOT
Definition: WatchedItemQueryService.php:45
WatchedItemQueryService
Definition: WatchedItemQueryService.php:24
WatchedItemQueryService\INCLUDE_PATROL_INFO
const INCLUDE_PATROL_INFO
Definition: WatchedItemQueryService.php:33
WatchedItemQueryService\$permissionManager
PermissionManager $permissionManager
Definition: WatchedItemQueryService.php:78
WatchedItemQueryService\getWatchedItemsForUser
getWatchedItemsForUser(UserIdentity $user, array $options=[])
For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser.
Definition: WatchedItemQueryService.php:303
WatchedItemQueryService\$expiryEnabled
bool $expiryEnabled
Correlates to $wgWatchlistExpiry feature flag.
Definition: WatchedItemQueryService.php:89
WatchedItemQueryService\FILTER_BOT
const FILTER_BOT
Definition: WatchedItemQueryService.php:44
WatchedItemQueryService\INCLUDE_USER_ID
const INCLUDE_USER_ID
Definition: WatchedItemQueryService.php:31
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:38
WatchedItemQueryService\$loadBalancer
ILoadBalancer $loadBalancer
Definition: WatchedItemQueryService.php:63
WatchedItemQueryService\DIR_NEWER
const DIR_NEWER
Definition: WatchedItemQueryService.php:27
MediaWiki\Permissions\PermissionManager
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Definition: PermissionManager.php:50
WatchedItemQueryService\SORT_DESC
const SORT_DESC
Definition: WatchedItemQueryService.php:58
WatchedItemQueryService\getRecentChangeFieldsFromRow
getRecentChangeFieldsFromRow(stdClass $row)
Definition: WatchedItemQueryService.php:372
WatchedItem
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:35
WatchedItemQueryService\FILTER_NOT_PATROLLED
const FILTER_NOT_PATROLLED
Definition: WatchedItemQueryService.php:49
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
WatchedItemQueryService\SORT_ASC
const SORT_ASC
Definition: WatchedItemQueryService.php:57
WatchedItemQueryService\FILTER_NOT_MINOR
const FILTER_NOT_MINOR
Definition: WatchedItemQueryService.php:43
RC_NEW
const RC_NEW
Definition: Defines.php:132
WatchedItemQueryService\INCLUDE_FLAGS
const INCLUDE_FLAGS
Definition: WatchedItemQueryService.php:29
RecentChange\PRC_AUTOPATROLLED
const PRC_AUTOPATROLLED
Definition: RecentChange.php:84
WatchedItemQueryService\__construct
__construct(ILoadBalancer $loadBalancer, CommentStore $commentStore, ActorMigration $actorMigration, WatchedItemStoreInterface $watchedItemStore, PermissionManager $permissionManager, HookContainer $hookContainer, UserFactory $userFactory, bool $expiryEnabled=false)
Definition: WatchedItemQueryService.php:91
Wikimedia\Rdbms\IDatabase\bitAnd
bitAnd( $fieldLeft, $fieldRight)
WatchedItemQueryService\DIR_OLDER
const DIR_OLDER
Definition: WatchedItemQueryService.php:26
WatchedItemQueryService\getUserRelatedConds
getUserRelatedConds(IDatabase $db, UserIdentity $user, array $options)
Definition: WatchedItemQueryService.php:603
WatchedItemQueryService\getExtensions
getExtensions()
Definition: WatchedItemQueryService.php:114
MediaWiki\User\UserIdentity\getId
getId()
Wikimedia\Rdbms\IDatabase\addQuotes
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
WatchedItemQueryService\FILTER_CHANGED
const FILTER_CHANGED
Definition: WatchedItemQueryService.php:54
WatchedItemQueryService\FILTER_AUTOPATROLLED
const FILTER_AUTOPATROLLED
Definition: WatchedItemQueryService.php:50
WatchedItemQueryService\FILTER_NOT_UNREAD
const FILTER_NOT_UNREAD
Definition: WatchedItemQueryService.php:53
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:82
WatchedItemQueryService\FILTER_NOT_ANON
const FILTER_NOT_ANON
Definition: WatchedItemQueryService.php:47
Wikimedia\Rdbms\IDatabase\getType
getType()
Get the type of the DBMS (e.g.
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryTables
getWatchedItemsWithRCInfoQueryTables(array $options)
Definition: WatchedItemQueryService.php:385
LogPage\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: LogPage.php:41
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
RC_CATEGORIZE
const RC_CATEGORIZE
Definition: Defines.php:135
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:566
WatchedItemQueryService\INCLUDE_SIZES
const INCLUDE_SIZES
Definition: WatchedItemQueryService.php:35
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
WatchedItemQueryService\getExtraDeletedPageLogEntryRelatedCond
getExtraDeletedPageLogEntryRelatedCond(IDatabase $db, UserIdentity $user)
Definition: WatchedItemQueryService.php:640
WatchedItemStoreInterface
Definition: WatchedItemStoreInterface.php:30
WatchedItemQueryService\FILTER_ANON
const FILTER_ANON
Definition: WatchedItemQueryService.php:46
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:56
WatchedItemQueryService\FILTER_UNREAD
const FILTER_UNREAD
Definition: WatchedItemQueryService.php:52
WatchedItemQueryService\$extensions
WatchedItemQueryServiceExtension[] null $extensions
Definition: WatchedItemQueryService.php:66
Wikimedia\Rdbms\IDatabase\makeList
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
MediaWiki\User\UserFactory
Creates User objects.
Definition: UserFactory.php:40
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:81
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:39
WatchedItemQueryService\getStartFromConds
getStartFromConds(IDatabase $db, array $options, array $startFrom)
Definition: WatchedItemQueryService.php:660