MediaWiki  master
WatchedItemQueryService.php
Go to the documentation of this file.
1 <?php
2 
9 use Wikimedia\Assert\Assert;
12 
24 
25  public const DIR_OLDER = 'older';
26  public const DIR_NEWER = 'newer';
27 
28  public const INCLUDE_FLAGS = 'flags';
29  public const INCLUDE_USER = 'user';
30  public const INCLUDE_USER_ID = 'userid';
31  public const INCLUDE_COMMENT = 'comment';
32  public const INCLUDE_PATROL_INFO = 'patrol';
33  public const INCLUDE_AUTOPATROL_INFO = 'autopatrol';
34  public const INCLUDE_SIZES = 'sizes';
35  public const INCLUDE_LOG_INFO = 'loginfo';
36  public const INCLUDE_TAGS = 'tags';
37 
38  // FILTER_* constants are part of public API (are used in ApiQueryWatchlist and
39  // ApiQueryWatchlistRaw classes) and should not be changed.
40  // Changing values of those constants will result in a breaking change in the API
41  public const FILTER_MINOR = 'minor';
42  public const FILTER_NOT_MINOR = '!minor';
43  public const FILTER_BOT = 'bot';
44  public const FILTER_NOT_BOT = '!bot';
45  public const FILTER_ANON = 'anon';
46  public const FILTER_NOT_ANON = '!anon';
47  public const FILTER_PATROLLED = 'patrolled';
48  public const FILTER_NOT_PATROLLED = '!patrolled';
49  public const FILTER_AUTOPATROLLED = 'autopatrolled';
50  public const FILTER_NOT_AUTOPATROLLED = '!autopatrolled';
51  public const FILTER_UNREAD = 'unread';
52  public const FILTER_NOT_UNREAD = '!unread';
53  public const FILTER_CHANGED = 'changed';
54  public const FILTER_NOT_CHANGED = '!changed';
55 
56  public const SORT_ASC = 'ASC';
57  public const SORT_DESC = 'DESC';
58 
62  private $loadBalancer;
63 
65  private $extensions = null;
66 
68  private $commentStore;
69 
72 
74  private $hookRunner;
75 
79  private $expiryEnabled;
80 
81  public function __construct(
85  HookContainer $hookContainer,
86  bool $expiryEnabled = false
87  ) {
88  $this->loadBalancer = $loadBalancer;
89  $this->commentStore = $commentStore;
90  $this->watchedItemStore = $watchedItemStore;
91  $this->hookRunner = new HookRunner( $hookContainer );
92  $this->expiryEnabled = $expiryEnabled;
93  }
94 
98  private function getExtensions() {
99  if ( $this->extensions === null ) {
100  $this->extensions = [];
101  $this->hookRunner->onWatchedItemQueryServiceExtensions( $this->extensions, $this );
102  }
103  return $this->extensions;
104  }
105 
109  private function getConnection() {
110  return $this->loadBalancer->getConnectionRef( DB_REPLICA, [ 'watchlist' ] );
111  }
112 
157  User $user, array $options = [], &$startFrom = null
158  ) {
159  $options += [
160  'includeFields' => [],
161  'namespaceIds' => [],
162  'filters' => [],
163  'allRevisions' => false,
164  'usedInGenerator' => false
165  ];
166 
167  Assert::parameter(
168  !isset( $options['rcTypes'] )
169  || !array_diff( $options['rcTypes'], [ RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL, RC_CATEGORIZE ] ),
170  '$options[\'rcTypes\']',
171  'must be an array containing only: RC_EDIT, RC_NEW, RC_LOG, RC_EXTERNAL and/or RC_CATEGORIZE'
172  );
173  Assert::parameter(
174  !isset( $options['dir'] ) || in_array( $options['dir'], [ self::DIR_OLDER, self::DIR_NEWER ] ),
175  '$options[\'dir\']',
176  'must be DIR_OLDER or DIR_NEWER'
177  );
178  Assert::parameter(
179  !isset( $options['start'] ) && !isset( $options['end'] ) && $startFrom === null
180  || isset( $options['dir'] ),
181  '$options[\'dir\']',
182  'must be provided when providing the "start" or "end" options or the $startFrom parameter'
183  );
184  Assert::parameter(
185  !isset( $options['startFrom'] ),
186  '$options[\'startFrom\']',
187  'must not be provided, use $startFrom instead'
188  );
189  Assert::parameter(
190  !isset( $startFrom ) || ( is_array( $startFrom ) && count( $startFrom ) === 2 ),
191  '$startFrom',
192  'must be a two-element array'
193  );
194  if ( array_key_exists( 'watchlistOwner', $options ) ) {
195  Assert::parameterType(
196  User::class,
197  $options['watchlistOwner'],
198  '$options[\'watchlistOwner\']'
199  );
200  Assert::parameter(
201  isset( $options['watchlistOwnerToken'] ),
202  '$options[\'watchlistOwnerToken\']',
203  'must be provided when providing watchlistOwner option'
204  );
205  }
206 
207  $db = $this->getConnection();
208 
209  $tables = $this->getWatchedItemsWithRCInfoQueryTables( $options );
210  $fields = $this->getWatchedItemsWithRCInfoQueryFields( $options );
211  $conds = $this->getWatchedItemsWithRCInfoQueryConds( $db, $user, $options );
212  $dbOptions = $this->getWatchedItemsWithRCInfoQueryDbOptions( $options );
213  $joinConds = $this->getWatchedItemsWithRCInfoQueryJoinConds( $options );
214 
215  if ( $startFrom !== null ) {
216  $conds[] = $this->getStartFromConds( $db, $options, $startFrom );
217  }
218 
219  foreach ( $this->getExtensions() as $extension ) {
220  $extension->modifyWatchedItemsWithRCInfoQuery(
221  $user, $options, $db,
222  $tables,
223  $fields,
224  $conds,
225  $dbOptions,
226  $joinConds
227  );
228  }
229 
230  $res = $db->select(
231  $tables,
232  $fields,
233  $conds,
234  __METHOD__,
235  $dbOptions,
236  $joinConds
237  );
238 
239  $limit = $dbOptions['LIMIT'] ?? INF;
240  $items = [];
241  $startFrom = null;
242  foreach ( $res as $row ) {
243  if ( --$limit <= 0 ) {
244  $startFrom = [ $row->rc_timestamp, $row->rc_id ];
245  break;
246  }
247 
248  $target = new TitleValue( (int)$row->rc_namespace, $row->rc_title );
249  $items[] = [
250  new WatchedItem(
251  $user,
252  $target,
253  $this->watchedItemStore->getLatestNotificationTimestamp(
254  $row->wl_notificationtimestamp, $user, $target
255  ),
256  $row->we_expiry ?? null
257  ),
258  $this->getRecentChangeFieldsFromRow( $row )
259  ];
260  }
261 
262  foreach ( $this->getExtensions() as $extension ) {
263  $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
264  }
265 
266  return $items;
267  }
268 
288  public function getWatchedItemsForUser( UserIdentity $user, array $options = [] ) {
289  if ( !$user->isRegistered() ) {
290  // TODO: should this just return an empty array or rather complain loud at this point
291  // as e.g. ApiBase::getWatchlistUser does?
292  return [];
293  }
294 
295  $options += [ 'namespaceIds' => [] ];
296 
297  Assert::parameter(
298  !isset( $options['sort'] ) || in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
299  '$options[\'sort\']',
300  'must be SORT_ASC or SORT_DESC'
301  );
302  Assert::parameter(
303  !isset( $options['filter'] ) || in_array(
304  $options['filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
305  ),
306  '$options[\'filter\']',
307  'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
308  );
309  Assert::parameter(
310  !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
311  || isset( $options['sort'] ),
312  '$options[\'sort\']',
313  'must be provided if any of "from", "until", "startFrom" options is provided'
314  );
315 
316  $db = $this->getConnection();
317 
318  $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
319  $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
320 
321  $tables = 'watchlist';
322  $joinConds = [];
323  if ( $this->expiryEnabled ) {
324  // If expiries are enabled, join with the watchlist_expiry table and exclude expired items.
325  $tables = [ 'watchlist', 'watchlist_expiry' ];
326  $conds[] = $db->makeList(
327  [ 'we_expiry' => null, 'we_expiry > ' . $db->addQuotes( $db->timestamp() ) ],
329  );
330  $joinConds['watchlist_expiry'] = [ 'LEFT JOIN', 'wl_id = we_item' ];
331  }
332  $res = $db->select(
333  $tables,
334  [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
335  $conds,
336  __METHOD__,
337  $dbOptions,
338  $joinConds
339  );
340 
341  $watchedItems = [];
342  foreach ( $res as $row ) {
343  $target = new TitleValue( (int)$row->wl_namespace, $row->wl_title );
344  // todo these could all be cached at some point?
345  $watchedItems[] = new WatchedItem(
346  $user,
347  $target,
348  $this->watchedItemStore->getLatestNotificationTimestamp(
349  $row->wl_notificationtimestamp, $user, $target
350  ),
351  $row->we_expiry ?? null
352  );
353  }
354 
355  return $watchedItems;
356  }
357 
358  private function getRecentChangeFieldsFromRow( stdClass $row ) {
359  return array_filter(
360  get_object_vars( $row ),
361  static function ( $key ) {
362  return substr( $key, 0, 3 ) === 'rc_';
363  },
364  ARRAY_FILTER_USE_KEY
365  );
366  }
367 
368  private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
369  $tables = [ 'recentchanges', 'watchlist' ];
370 
371  if ( $this->expiryEnabled ) {
372  $tables[] = 'watchlist_expiry';
373  }
374 
375  if ( !$options['allRevisions'] ) {
376  $tables[] = 'page';
377  }
378  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
379  $tables += $this->commentStore->getJoin( 'rc_comment' )['tables'];
380  }
381  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ||
382  in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ||
383  in_array( self::FILTER_ANON, $options['filters'] ) ||
384  in_array( self::FILTER_NOT_ANON, $options['filters'] ) ||
385  array_key_exists( 'onlyByUser', $options ) || array_key_exists( 'notByUser', $options )
386  ) {
387  $tables['watchlist_actor'] = 'actor';
388  }
389  return $tables;
390  }
391 
392  private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
393  $fields = [
394  'rc_id',
395  'rc_namespace',
396  'rc_title',
397  'rc_timestamp',
398  'rc_type',
399  'rc_deleted',
400  'wl_notificationtimestamp'
401  ];
402 
403  if ( $this->expiryEnabled ) {
404  $fields[] = 'we_expiry';
405  }
406 
407  $rcIdFields = [
408  'rc_cur_id',
409  'rc_this_oldid',
410  'rc_last_oldid',
411  ];
412  if ( $options['usedInGenerator'] ) {
413  if ( $options['allRevisions'] ) {
414  $rcIdFields = [ 'rc_this_oldid' ];
415  } else {
416  $rcIdFields = [ 'rc_cur_id' ];
417  }
418  }
419  $fields = array_merge( $fields, $rcIdFields );
420 
421  if ( in_array( self::INCLUDE_FLAGS, $options['includeFields'] ) ) {
422  $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
423  }
424  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ) {
425  $fields['rc_user_text'] = 'watchlist_actor.actor_name';
426  }
427  if ( in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ) {
428  $fields['rc_user'] = 'watchlist_actor.actor_user';
429  }
430  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
431  $fields += $this->commentStore->getJoin( 'rc_comment' )['fields'];
432  }
433  if ( in_array( self::INCLUDE_PATROL_INFO, $options['includeFields'] ) ) {
434  $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
435  }
436  if ( in_array( self::INCLUDE_SIZES, $options['includeFields'] ) ) {
437  $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
438  }
439  if ( in_array( self::INCLUDE_LOG_INFO, $options['includeFields'] ) ) {
440  $fields = array_merge( $fields, [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ] );
441  }
442  if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
443  // prefixed with rc_ to include the field in getRecentChangeFieldsFromRow
444  $fields['rc_tags'] = ChangeTags::makeTagSummarySubquery( 'recentchanges' );
445  }
446 
447  return $fields;
448  }
449 
451  IDatabase $db,
452  User $user,
453  array $options
454  ) {
455  $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
456  $conds = [ 'wl_user' => $watchlistOwnerId ];
457 
458  if ( $this->expiryEnabled ) {
459  $conds[] = 'we_expiry IS NULL OR we_expiry > ' . $db->addQuotes( $db->timestamp() );
460  }
461 
462  if ( !$options['allRevisions'] ) {
463  $conds[] = $db->makeList(
464  [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG ],
465  LIST_OR
466  );
467  }
468 
469  if ( $options['namespaceIds'] ) {
470  $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
471  }
472 
473  if ( array_key_exists( 'rcTypes', $options ) ) {
474  $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
475  }
476 
477  $conds = array_merge(
478  $conds,
479  $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
480  );
481 
482  $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
483 
484  if ( !isset( $options['start'] ) && !isset( $options['end'] ) && $db->getType() === 'mysql' ) {
485  // This is an index optimization for mysql
486  $conds[] = 'rc_timestamp > ' . $db->addQuotes( '' );
487  }
488 
489  $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
490 
491  $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
492  if ( $deletedPageLogCond ) {
493  $conds[] = $deletedPageLogCond;
494  }
495 
496  return $conds;
497  }
498 
499  private function getWatchlistOwnerId( UserIdentity $user, array $options ) {
500  if ( array_key_exists( 'watchlistOwner', $options ) ) {
502  $watchlistOwner = $options['watchlistOwner'];
503  $ownersToken =
504  $watchlistOwner->getOption( 'watchlisttoken' );
505  $token = $options['watchlistOwnerToken'];
506  if ( $ownersToken == '' || !hash_equals( $ownersToken, $token ) ) {
507  throw ApiUsageException::newWithMessage( null, 'apierror-bad-watchlist-token', 'bad_wltoken' );
508  }
509  return $watchlistOwner->getId();
510  }
511  return $user->getId();
512  }
513 
514  private function getWatchedItemsWithRCInfoQueryFilterConds( User $user, array $options ) {
515  $conds = [];
516 
517  if ( in_array( self::FILTER_MINOR, $options['filters'] ) ) {
518  $conds[] = 'rc_minor != 0';
519  } elseif ( in_array( self::FILTER_NOT_MINOR, $options['filters'] ) ) {
520  $conds[] = 'rc_minor = 0';
521  }
522 
523  if ( in_array( self::FILTER_BOT, $options['filters'] ) ) {
524  $conds[] = 'rc_bot != 0';
525  } elseif ( in_array( self::FILTER_NOT_BOT, $options['filters'] ) ) {
526  $conds[] = 'rc_bot = 0';
527  }
528 
529  if ( in_array( self::FILTER_ANON, $options['filters'] ) ) {
530  $conds[] = 'watchlist_actor.actor_user IS NULL';
531  } elseif ( in_array( self::FILTER_NOT_ANON, $options['filters'] ) ) {
532  $conds[] = 'watchlist_actor.actor_user IS NOT NULL';
533  }
534 
535  if ( $user->useRCPatrol() || $user->useNPPatrol() ) {
536  // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
537  // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
538  if ( in_array( self::FILTER_PATROLLED, $options['filters'] ) ) {
539  $conds[] = 'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED;
540  } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options['filters'] ) ) {
541  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
542  }
543 
544  if ( in_array( self::FILTER_AUTOPATROLLED, $options['filters'] ) ) {
545  $conds['rc_patrolled'] = RecentChange::PRC_AUTOPATROLLED;
546  } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options['filters'] ) ) {
547  $conds[] = 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED;
548  }
549  }
550 
551  if ( in_array( self::FILTER_UNREAD, $options['filters'] ) ) {
552  $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
553  } elseif ( in_array( self::FILTER_NOT_UNREAD, $options['filters'] ) ) {
554  // TODO: should this be changed to use Database::makeList?
555  $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
556  }
557 
558  return $conds;
559  }
560 
561  private function getStartEndConds( IDatabase $db, array $options ) {
562  if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
563  return [];
564  }
565 
566  $conds = [];
567 
568  if ( isset( $options['start'] ) ) {
569  $after = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
570  $conds[] = 'rc_timestamp ' . $after . ' ' .
571  $db->addQuotes( $db->timestamp( $options['start'] ) );
572  }
573  if ( isset( $options['end'] ) ) {
574  $before = $options['dir'] === self::DIR_OLDER ? '>=' : '<=';
575  $conds[] = 'rc_timestamp ' . $before . ' ' .
576  $db->addQuotes( $db->timestamp( $options['end'] ) );
577  }
578 
579  return $conds;
580  }
581 
582  private function getUserRelatedConds( IDatabase $db, Authority $user, array $options ) {
583  if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
584  return [];
585  }
586 
587  $conds = [];
588 
589  if ( array_key_exists( 'onlyByUser', $options ) ) {
590  $conds['watchlist_actor.actor_name'] = $options['onlyByUser'];
591  } elseif ( array_key_exists( 'notByUser', $options ) ) {
592  $conds[] = 'watchlist_actor.actor_name<>' . $db->addQuotes( $options['notByUser'] );
593  }
594 
595  // Avoid brute force searches (T19342)
596  $bitmask = 0;
597  if ( !$user->isAllowed( 'deletedhistory' ) ) {
598  $bitmask = RevisionRecord::DELETED_USER;
599  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
600  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
601  }
602  if ( $bitmask ) {
603  $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
604  }
605 
606  return $conds;
607  }
608 
610  // LogPage::DELETED_ACTION hides the affected page, too. So hide those
611  // entirely from the watchlist, or someone could guess the title.
612  $bitmask = 0;
613  if ( !$user->isAllowed( 'deletedhistory' ) ) {
614  $bitmask = LogPage::DELETED_ACTION;
615  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
617  }
618  if ( $bitmask ) {
619  return $db->makeList( [
620  'rc_type != ' . RC_LOG,
621  $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
622  ], LIST_OR );
623  }
624  return '';
625  }
626 
627  private function getStartFromConds( IDatabase $db, array $options, array $startFrom ) {
628  $op = $options['dir'] === self::DIR_OLDER ? '<' : '>';
629  list( $rcTimestamp, $rcId ) = $startFrom;
630  $rcTimestamp = $db->addQuotes( $db->timestamp( $rcTimestamp ) );
631  $rcId = (int)$rcId;
632  return $db->makeList(
633  [
634  "rc_timestamp $op $rcTimestamp",
635  $db->makeList(
636  [
637  "rc_timestamp = $rcTimestamp",
638  "rc_id $op= $rcId"
639  ],
640  LIST_AND
641  )
642  ],
643  LIST_OR
644  );
645  }
646 
648  IDatabase $db, UserIdentity $user, array $options
649  ) {
650  $conds = [ 'wl_user' => $user->getId() ];
651  if ( $options['namespaceIds'] ) {
652  $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
653  }
654  if ( isset( $options['filter'] ) ) {
655  $filter = $options['filter'];
656  if ( $filter === self::FILTER_CHANGED ) {
657  $conds[] = 'wl_notificationtimestamp IS NOT NULL';
658  } else {
659  $conds[] = 'wl_notificationtimestamp IS NULL';
660  }
661  }
662 
663  if ( isset( $options['from'] ) ) {
664  $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
665  $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
666  }
667  if ( isset( $options['until'] ) ) {
668  $op = $options['sort'] === self::SORT_ASC ? '<' : '>';
669  $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
670  }
671  if ( isset( $options['startFrom'] ) ) {
672  $op = $options['sort'] === self::SORT_ASC ? '>' : '<';
673  $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
674  }
675 
676  return $conds;
677  }
678 
688  private function getFromUntilTargetConds( IDatabase $db, LinkTarget $target, $op ) {
689  return $db->makeList(
690  [
691  "wl_namespace $op " . $target->getNamespace(),
692  $db->makeList(
693  [
694  'wl_namespace = ' . $target->getNamespace(),
695  "wl_title $op= " . $db->addQuotes( $target->getDBkey() )
696  ],
697  LIST_AND
698  )
699  ],
700  LIST_OR
701  );
702  }
703 
704  private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
705  $dbOptions = [];
706 
707  if ( array_key_exists( 'dir', $options ) ) {
708  $sort = $options['dir'] === self::DIR_OLDER ? ' DESC' : '';
709  $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
710  }
711 
712  if ( array_key_exists( 'limit', $options ) ) {
713  $dbOptions['LIMIT'] = (int)$options['limit'] + 1;
714  }
715 
716  return $dbOptions;
717  }
718 
719  private function getWatchedItemsForUserQueryDbOptions( array $options ) {
720  $dbOptions = [];
721  if ( array_key_exists( 'sort', $options ) ) {
722  $dbOptions['ORDER BY'] = [
723  "wl_namespace {$options['sort']}",
724  "wl_title {$options['sort']}"
725  ];
726  if ( count( $options['namespaceIds'] ) === 1 ) {
727  $dbOptions['ORDER BY'] = "wl_title {$options['sort']}";
728  }
729  }
730  if ( array_key_exists( 'limit', $options ) ) {
731  $dbOptions['LIMIT'] = (int)$options['limit'];
732  }
733  return $dbOptions;
734  }
735 
736  private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
737  $joinConds = [
738  'watchlist' => [ 'JOIN',
739  [
740  'wl_namespace=rc_namespace',
741  'wl_title=rc_title'
742  ]
743  ]
744  ];
745 
746  if ( $this->expiryEnabled ) {
747  $joinConds['watchlist_expiry'] = [ 'LEFT JOIN', 'wl_id = we_item' ];
748  }
749 
750  if ( !$options['allRevisions'] ) {
751  $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
752  }
753  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
754  $joinConds += $this->commentStore->getJoin( 'rc_comment' )['joins'];
755  }
756  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ||
757  in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ||
758  in_array( self::FILTER_ANON, $options['filters'] ) ||
759  in_array( self::FILTER_NOT_ANON, $options['filters'] ) ||
760  array_key_exists( 'onlyByUser', $options ) || array_key_exists( 'notByUser', $options )
761  ) {
762  $joinConds['watchlist_actor'] = [ 'JOIN', 'actor_id=rc_actor' ];
763  }
764  return $joinConds;
765  }
766 
767 }
WatchedItemQueryService\getConnection
getConnection()
Definition: WatchedItemQueryService.php:109
LIST_OR
const LIST_OR
Definition: Defines.php:46
ChangeTags\makeTagSummarySubquery
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:983
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:688
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryConds
getWatchedItemsWithRCInfoQueryConds(IDatabase $db, User $user, array $options)
Definition: WatchedItemQueryService.php:450
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryJoinConds
getWatchedItemsWithRCInfoQueryJoinConds(array $options)
Definition: WatchedItemQueryService.php:736
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:68
RC_EDIT
const RC_EDIT
Definition: Defines.php:115
WatchedItemQueryService\INCLUDE_COMMENT
const INCLUDE_COMMENT
Definition: WatchedItemQueryService.php:31
WatchedItemQueryService\FILTER_NOT_AUTOPATROLLED
const FILTER_NOT_AUTOPATROLLED
Definition: WatchedItemQueryService.php:50
WatchedItemQueryService\FILTER_PATROLLED
const FILTER_PATROLLED
Definition: WatchedItemQueryService.php:47
WatchedItemQueryService\__construct
__construct(ILoadBalancer $loadBalancer, CommentStore $commentStore, WatchedItemStoreInterface $watchedItemStore, HookContainer $hookContainer, bool $expiryEnabled=false)
Definition: WatchedItemQueryService.php:81
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryFields
getWatchedItemsWithRCInfoQueryFields(array $options)
Definition: WatchedItemQueryService.php:392
LIST_AND
const LIST_AND
Definition: Defines.php:43
WatchedItemQueryService\getStartEndConds
getStartEndConds(IDatabase $db, array $options)
Definition: WatchedItemQueryService.php:561
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryDbOptions
getWatchedItemsWithRCInfoQueryDbOptions(array $options)
Definition: WatchedItemQueryService.php:704
CommentStore
Handle database storage of comments such as edit summaries and log reasons.
Definition: CommentStore.php:42
WatchedItemQueryService\$hookRunner
HookRunner $hookRunner
Definition: WatchedItemQueryService.php:74
WatchedItemQueryService\getWatchedItemsForUserQueryDbOptions
getWatchedItemsForUserQueryDbOptions(array $options)
Definition: WatchedItemQueryService.php:719
User\useNPPatrol
useNPPatrol()
Check whether to enable new pages patrol features for this user.
Definition: User.php:3049
MediaWiki\User\UserIdentity\getId
getId( $wikiId=self::LOCAL)
WatchedItemQueryService\getWatchedItemsWithRecentChangeInfo
getWatchedItemsWithRecentChangeInfo(User $user, array $options=[], &$startFrom=null)
Definition: WatchedItemQueryService.php:156
WatchedItemQueryService\getExtraDeletedPageLogEntryRelatedCond
getExtraDeletedPageLogEntryRelatedCond(IDatabase $db, Authority $user)
Definition: WatchedItemQueryService.php:609
RC_NEW
const RC_NEW
Definition: Defines.php:116
$res
$res
Definition: testCompression.php:57
User\useRCPatrol
useRCPatrol()
Check whether to enable recent changes patrol features for this user.
Definition: User.php:3040
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
MediaWiki\Permissions\Authority\isAllowedAny
isAllowedAny(... $permissions)
Checks whether this authority has any of the given permissions in general.
WatchedItemQueryService\INCLUDE_TAGS
const INCLUDE_TAGS
Definition: WatchedItemQueryService.php:36
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:499
WatchedItemQueryService\getWatchedItemsForUserQueryConds
getWatchedItemsForUserQueryConds(IDatabase $db, UserIdentity $user, array $options)
Definition: WatchedItemQueryService.php:647
WatchedItemQueryService\INCLUDE_LOG_INFO
const INCLUDE_LOG_INFO
Definition: WatchedItemQueryService.php:35
RC_LOG
const RC_LOG
Definition: Defines.php:117
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...
WatchedItemQueryService\$watchedItemStore
WatchedItemStoreInterface $watchedItemStore
Definition: WatchedItemQueryService.php:71
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryFilterConds
getWatchedItemsWithRCInfoQueryFilterConds(User $user, array $options)
Definition: WatchedItemQueryService.php:514
MediaWiki\User\UserIdentity\isRegistered
isRegistered()
WatchedItemQueryService\FILTER_MINOR
const FILTER_MINOR
Definition: WatchedItemQueryService.php:41
WatchedItemQueryService\FILTER_NOT_CHANGED
const FILTER_NOT_CHANGED
Definition: WatchedItemQueryService.php:54
WatchedItemQueryService\INCLUDE_AUTOPATROL_INFO
const INCLUDE_AUTOPATROL_INFO
Definition: WatchedItemQueryService.php:33
WatchedItemQueryService\INCLUDE_USER
const INCLUDE_USER
Definition: WatchedItemQueryService.php:29
WatchedItemQueryService\FILTER_NOT_BOT
const FILTER_NOT_BOT
Definition: WatchedItemQueryService.php:44
WatchedItemQueryService
Definition: WatchedItemQueryService.php:23
WatchedItemQueryService\INCLUDE_PATROL_INFO
const INCLUDE_PATROL_INFO
Definition: WatchedItemQueryService.php:32
WatchedItemQueryService\getWatchedItemsForUser
getWatchedItemsForUser(UserIdentity $user, array $options=[])
For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser.
Definition: WatchedItemQueryService.php:288
WatchedItemQueryService\$expiryEnabled
bool $expiryEnabled
Correlates to $wgWatchlistExpiry feature flag.
Definition: WatchedItemQueryService.php:79
WatchedItemQueryService\FILTER_BOT
const FILTER_BOT
Definition: WatchedItemQueryService.php:43
WatchedItemQueryService\INCLUDE_USER_ID
const INCLUDE_USER_ID
Definition: WatchedItemQueryService.php:30
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LogPage\DELETED_ACTION
const DELETED_ACTION
Definition: LogPage.php:39
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
WatchedItemQueryService\$loadBalancer
ILoadBalancer $loadBalancer
Definition: WatchedItemQueryService.php:62
WatchedItemQueryService\DIR_NEWER
const DIR_NEWER
Definition: WatchedItemQueryService.php:26
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:118
WatchedItemQueryService\SORT_DESC
const SORT_DESC
Definition: WatchedItemQueryService.php:57
WatchedItemQueryService\getRecentChangeFieldsFromRow
getRecentChangeFieldsFromRow(stdClass $row)
Definition: WatchedItemQueryService.php:358
WatchedItem
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:36
WatchedItemQueryService\FILTER_NOT_PATROLLED
const FILTER_NOT_PATROLLED
Definition: WatchedItemQueryService.php:48
MediaWiki\Linker\LinkTarget\getDBkey
getDBkey()
Get the main part with underscores.
WatchedItemQueryService\SORT_ASC
const SORT_ASC
Definition: WatchedItemQueryService.php:56
WatchedItemQueryService\FILTER_NOT_MINOR
const FILTER_NOT_MINOR
Definition: WatchedItemQueryService.php:42
WatchedItemQueryService\INCLUDE_FLAGS
const INCLUDE_FLAGS
Definition: WatchedItemQueryService.php:28
RecentChange\PRC_AUTOPATROLLED
const PRC_AUTOPATROLLED
Definition: RecentChange.php:93
Wikimedia\Rdbms\IDatabase\bitAnd
bitAnd( $fieldLeft, $fieldRight)
WatchedItemQueryService\DIR_OLDER
const DIR_OLDER
Definition: WatchedItemQueryService.php:25
WatchedItemQueryService\getExtensions
getExtensions()
Definition: WatchedItemQueryService.php:98
MediaWiki\Permissions\Authority\isAllowed
isAllowed(string $permission)
Checks whether this authority has the given permission in general.
RC_CATEGORIZE
const RC_CATEGORIZE
Definition: Defines.php:119
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:53
WatchedItemQueryService\FILTER_AUTOPATROLLED
const FILTER_AUTOPATROLLED
Definition: WatchedItemQueryService.php:49
WatchedItemQueryService\FILTER_NOT_UNREAD
const FILTER_NOT_UNREAD
Definition: WatchedItemQueryService.php:52
RecentChange\PRC_UNPATROLLED
const PRC_UNPATROLLED
Definition: RecentChange.php:91
WatchedItemQueryService\FILTER_NOT_ANON
const FILTER_NOT_ANON
Definition: WatchedItemQueryService.php:46
Wikimedia\Rdbms\IDatabase\getType
getType()
Get the RDBMS type of the server (e.g.
WatchedItemQueryService\getWatchedItemsWithRCInfoQueryTables
getWatchedItemsWithRCInfoQueryTables(array $options)
Definition: WatchedItemQueryService.php:368
LogPage\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: LogPage.php:42
MediaWiki\HookContainer\HookContainer
HookContainer class.
Definition: HookContainer.php:45
MediaWiki\HookContainer\HookRunner
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:556
WatchedItemQueryService\INCLUDE_SIZES
const INCLUDE_SIZES
Definition: WatchedItemQueryService.php:34
WatchedItemQueryService\getUserRelatedConds
getUserRelatedConds(IDatabase $db, Authority $user, array $options)
Definition: WatchedItemQueryService.php:582
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
WatchedItemStoreInterface
Definition: WatchedItemStoreInterface.php:31
WatchedItemQueryService\FILTER_ANON
const FILTER_ANON
Definition: WatchedItemQueryService.php:45
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
WatchedItemQueryService\FILTER_UNREAD
const FILTER_UNREAD
Definition: WatchedItemQueryService.php:51
WatchedItemQueryService\$extensions
WatchedItemQueryServiceExtension[] null $extensions
Definition: WatchedItemQueryService.php:65
Wikimedia\Rdbms\IDatabase\makeList
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
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:40
WatchedItemQueryService\getStartFromConds
getStartFromConds(IDatabase $db, array $options, array $startFrom)
Definition: WatchedItemQueryService.php:627