MediaWiki  master
WatchedItemQueryService.php
Go to the documentation of this file.
1 <?php
2 
13 use Wikimedia\Assert\Assert;
16 
28 
29  public const DIR_OLDER = 'older';
30  public const DIR_NEWER = 'newer';
31 
32  public const INCLUDE_FLAGS = 'flags';
33  public const INCLUDE_USER = 'user';
34  public const INCLUDE_USER_ID = 'userid';
35  public const INCLUDE_COMMENT = 'comment';
36  public const INCLUDE_PATROL_INFO = 'patrol';
37  public const INCLUDE_AUTOPATROL_INFO = 'autopatrol';
38  public const INCLUDE_SIZES = 'sizes';
39  public const INCLUDE_LOG_INFO = 'loginfo';
40  public const INCLUDE_TAGS = 'tags';
41 
42  // FILTER_* constants are part of public API (are used in ApiQueryWatchlist and
43  // ApiQueryWatchlistRaw classes) and should not be changed.
44  // Changing values of those constants will result in a breaking change in the API
45  public const FILTER_MINOR = 'minor';
46  public const FILTER_NOT_MINOR = '!minor';
47  public const FILTER_BOT = 'bot';
48  public const FILTER_NOT_BOT = '!bot';
49  public const FILTER_ANON = 'anon';
50  public const FILTER_NOT_ANON = '!anon';
51  public const FILTER_PATROLLED = 'patrolled';
52  public const FILTER_NOT_PATROLLED = '!patrolled';
53  public const FILTER_AUTOPATROLLED = 'autopatrolled';
54  public const FILTER_NOT_AUTOPATROLLED = '!autopatrolled';
55  public const FILTER_UNREAD = 'unread';
56  public const FILTER_NOT_UNREAD = '!unread';
57  public const FILTER_CHANGED = 'changed';
58  public const FILTER_NOT_CHANGED = '!changed';
59 
60  public const SORT_ASC = 'ASC';
61  public const SORT_DESC = 'DESC';
62 
66  private $loadBalancer;
67 
69  private $extensions = null;
70 
72  private $commentStore;
73 
75  private $watchedItemStore;
76 
78  private $hookRunner;
79 
81  private $userOptionsLookup;
82 
86  private $expiryEnabled;
87 
91  private $maxQueryExecutionTime;
92 
93  public function __construct(
94  ILoadBalancer $loadBalancer,
95  CommentStore $commentStore,
96  WatchedItemStoreInterface $watchedItemStore,
97  HookContainer $hookContainer,
98  UserOptionsLookup $userOptionsLookup,
99  bool $expiryEnabled = false,
100  int $maxQueryExecutionTime = 0
101  ) {
102  $this->loadBalancer = $loadBalancer;
103  $this->commentStore = $commentStore;
104  $this->watchedItemStore = $watchedItemStore;
105  $this->hookRunner = new HookRunner( $hookContainer );
106  $this->userOptionsLookup = $userOptionsLookup;
107  $this->expiryEnabled = $expiryEnabled;
108  $this->maxQueryExecutionTime = $maxQueryExecutionTime;
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 );
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  UserIdentity::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  $row->we_expiry ?? null
273  ),
274  $this->getRecentChangeFieldsFromRow( $row )
275  ];
276  }
277 
278  foreach ( $this->getExtensions() as $extension ) {
279  $extension->modifyWatchedItemsWithRCInfo( $user, $options, $db, $items, $res, $startFrom );
280  }
281 
282  return $items;
283  }
284 
304  public function getWatchedItemsForUser( UserIdentity $user, array $options = [] ) {
305  if ( !$user->isRegistered() ) {
306  // TODO: should this just return an empty array or rather complain loud at this point
307  // as e.g. ApiBase::getWatchlistUser does?
308  return [];
309  }
310 
311  $options += [ 'namespaceIds' => [] ];
312 
313  Assert::parameter(
314  !isset( $options['sort'] ) || in_array( $options['sort'], [ self::SORT_ASC, self::SORT_DESC ] ),
315  '$options[\'sort\']',
316  'must be SORT_ASC or SORT_DESC'
317  );
318  Assert::parameter(
319  !isset( $options['filter'] ) || in_array(
320  $options['filter'], [ self::FILTER_CHANGED, self::FILTER_NOT_CHANGED ]
321  ),
322  '$options[\'filter\']',
323  'must be FILTER_CHANGED or FILTER_NOT_CHANGED'
324  );
325  Assert::parameter(
326  !isset( $options['from'] ) && !isset( $options['until'] ) && !isset( $options['startFrom'] )
327  || isset( $options['sort'] ),
328  '$options[\'sort\']',
329  'must be provided if any of "from", "until", "startFrom" options is provided'
330  );
331 
332  $db = $this->getConnection();
333 
334  $conds = $this->getWatchedItemsForUserQueryConds( $db, $user, $options );
335  $dbOptions = $this->getWatchedItemsForUserQueryDbOptions( $options );
336 
337  $tables = 'watchlist';
338  $joinConds = [];
339  if ( $this->expiryEnabled ) {
340  // If expiries are enabled, join with the watchlist_expiry table and exclude expired items.
341  $tables = [ 'watchlist', 'watchlist_expiry' ];
342  $conds[] = $db->makeList(
343  [ 'we_expiry' => null, 'we_expiry > ' . $db->addQuotes( $db->timestamp() ) ],
345  );
346  $joinConds['watchlist_expiry'] = [ 'LEFT JOIN', 'wl_id = we_item' ];
347  }
348  $res = $db->select(
349  $tables,
350  [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
351  $conds,
352  __METHOD__,
353  $dbOptions,
354  $joinConds
355  );
356 
357  $watchedItems = [];
358  foreach ( $res as $row ) {
359  $target = new TitleValue( (int)$row->wl_namespace, $row->wl_title );
360  // todo these could all be cached at some point?
361  $watchedItems[] = new WatchedItem(
362  $user,
363  $target,
364  $this->watchedItemStore->getLatestNotificationTimestamp(
365  $row->wl_notificationtimestamp, $user, $target
366  ),
367  $row->we_expiry ?? null
368  );
369  }
370 
371  return $watchedItems;
372  }
373 
374  private function getRecentChangeFieldsFromRow( stdClass $row ) {
375  return array_filter(
376  get_object_vars( $row ),
377  static function ( $key ) {
378  return str_starts_with( $key, 'rc_' );
379  },
380  ARRAY_FILTER_USE_KEY
381  );
382  }
383 
384  private function getWatchedItemsWithRCInfoQueryTables( array $options ) {
385  $tables = [ 'recentchanges', 'watchlist' ];
386 
387  if ( $this->expiryEnabled ) {
388  $tables[] = 'watchlist_expiry';
389  }
390 
391  if ( !$options['allRevisions'] ) {
392  $tables[] = 'page';
393  }
394  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
395  $tables += $this->commentStore->getJoin( 'rc_comment' )['tables'];
396  }
397  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ||
398  in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ||
399  in_array( self::FILTER_ANON, $options['filters'] ) ||
400  in_array( self::FILTER_NOT_ANON, $options['filters'] ) ||
401  array_key_exists( 'onlyByUser', $options ) || array_key_exists( 'notByUser', $options )
402  ) {
403  $tables['watchlist_actor'] = 'actor';
404  }
405  return $tables;
406  }
407 
408  private function getWatchedItemsWithRCInfoQueryFields( array $options ) {
409  $fields = [
410  'rc_id',
411  'rc_namespace',
412  'rc_title',
413  'rc_timestamp',
414  'rc_type',
415  'rc_deleted',
416  'wl_notificationtimestamp'
417  ];
418 
419  if ( $this->expiryEnabled ) {
420  $fields[] = 'we_expiry';
421  }
422 
423  $rcIdFields = [
424  'rc_cur_id',
425  'rc_this_oldid',
426  'rc_last_oldid',
427  ];
428  if ( $options['usedInGenerator'] ) {
429  if ( $options['allRevisions'] ) {
430  $rcIdFields = [ 'rc_this_oldid' ];
431  } else {
432  $rcIdFields = [ 'rc_cur_id' ];
433  }
434  }
435  $fields = array_merge( $fields, $rcIdFields );
436 
437  if ( in_array( self::INCLUDE_FLAGS, $options['includeFields'] ) ) {
438  $fields = array_merge( $fields, [ 'rc_type', 'rc_minor', 'rc_bot' ] );
439  }
440  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ) {
441  $fields['rc_user_text'] = 'watchlist_actor.actor_name';
442  }
443  if ( in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ) {
444  $fields['rc_user'] = 'watchlist_actor.actor_user';
445  }
446  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
447  $fields += $this->commentStore->getJoin( 'rc_comment' )['fields'];
448  }
449  if ( in_array( self::INCLUDE_PATROL_INFO, $options['includeFields'] ) ) {
450  $fields = array_merge( $fields, [ 'rc_patrolled', 'rc_log_type' ] );
451  }
452  if ( in_array( self::INCLUDE_SIZES, $options['includeFields'] ) ) {
453  $fields = array_merge( $fields, [ 'rc_old_len', 'rc_new_len' ] );
454  }
455  if ( in_array( self::INCLUDE_LOG_INFO, $options['includeFields'] ) ) {
456  $fields = array_merge( $fields, [ 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ] );
457  }
458  if ( in_array( self::INCLUDE_TAGS, $options['includeFields'] ) ) {
459  // prefixed with rc_ to include the field in getRecentChangeFieldsFromRow
460  $fields['rc_tags'] = ChangeTags::makeTagSummarySubquery( 'recentchanges' );
461  }
462 
463  return $fields;
464  }
465 
466  private function getWatchedItemsWithRCInfoQueryConds(
467  IDatabase $db,
468  User $user,
469  array $options
470  ) {
471  $watchlistOwnerId = $this->getWatchlistOwnerId( $user, $options );
472  $conds = [ 'wl_user' => $watchlistOwnerId ];
473 
474  if ( $this->expiryEnabled ) {
475  $conds[] = 'we_expiry IS NULL OR we_expiry > ' . $db->addQuotes( $db->timestamp() );
476  }
477 
478  if ( !$options['allRevisions'] ) {
479  $conds[] = $db->makeList(
480  [ 'rc_this_oldid=page_latest', 'rc_type=' . RC_LOG ],
481  LIST_OR
482  );
483  }
484 
485  if ( $options['namespaceIds'] ) {
486  $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
487  }
488 
489  if ( array_key_exists( 'rcTypes', $options ) ) {
490  $conds['rc_type'] = array_map( 'intval', $options['rcTypes'] );
491  }
492 
493  $conds = array_merge(
494  $conds,
495  $this->getWatchedItemsWithRCInfoQueryFilterConds( $user, $options )
496  );
497 
498  $conds = array_merge( $conds, $this->getStartEndConds( $db, $options ) );
499 
500  if ( !isset( $options['start'] ) && !isset( $options['end'] ) && $db->getType() === 'mysql' ) {
501  // This is an index optimization for mysql
502  $conds[] = 'rc_timestamp > ' . $db->addQuotes( '' );
503  }
504 
505  $conds = array_merge( $conds, $this->getUserRelatedConds( $db, $user, $options ) );
506 
507  $deletedPageLogCond = $this->getExtraDeletedPageLogEntryRelatedCond( $db, $user );
508  if ( $deletedPageLogCond ) {
509  $conds[] = $deletedPageLogCond;
510  }
511 
512  return $conds;
513  }
514 
515  private function getWatchlistOwnerId( UserIdentity $user, array $options ) {
516  if ( array_key_exists( 'watchlistOwner', $options ) ) {
518  $watchlistOwner = $options['watchlistOwner'];
519  $ownersToken =
520  $this->userOptionsLookup->getOption( $watchlistOwner, 'watchlisttoken' );
521  $token = $options['watchlistOwnerToken'];
522  if ( $ownersToken == '' || !hash_equals( $ownersToken, $token ) ) {
523  throw ApiUsageException::newWithMessage( null, 'apierror-bad-watchlist-token', 'bad_wltoken' );
524  }
525  return $watchlistOwner->getId();
526  }
527  return $user->getId();
528  }
529 
530  private function getWatchedItemsWithRCInfoQueryFilterConds( User $user, array $options ) {
531  $conds = [];
532 
533  if ( in_array( self::FILTER_MINOR, $options['filters'] ) ) {
534  $conds[] = 'rc_minor != 0';
535  } elseif ( in_array( self::FILTER_NOT_MINOR, $options['filters'] ) ) {
536  $conds[] = 'rc_minor = 0';
537  }
538 
539  if ( in_array( self::FILTER_BOT, $options['filters'] ) ) {
540  $conds[] = 'rc_bot != 0';
541  } elseif ( in_array( self::FILTER_NOT_BOT, $options['filters'] ) ) {
542  $conds[] = 'rc_bot = 0';
543  }
544 
545  if ( in_array( self::FILTER_ANON, $options['filters'] ) ) {
546  $conds[] = 'watchlist_actor.actor_user IS NULL';
547  } elseif ( in_array( self::FILTER_NOT_ANON, $options['filters'] ) ) {
548  $conds[] = 'watchlist_actor.actor_user IS NOT NULL';
549  }
550 
551  if ( $user->useRCPatrol() || $user->useNPPatrol() ) {
552  // TODO: not sure if this should simply ignore patrolled filters if user does not have the patrol
553  // right, or maybe rather fail loud at this point, same as e.g. ApiQueryWatchlist does?
554  if ( in_array( self::FILTER_PATROLLED, $options['filters'] ) ) {
555  $conds[] = 'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED;
556  } elseif ( in_array( self::FILTER_NOT_PATROLLED, $options['filters'] ) ) {
557  $conds['rc_patrolled'] = RecentChange::PRC_UNPATROLLED;
558  }
559 
560  if ( in_array( self::FILTER_AUTOPATROLLED, $options['filters'] ) ) {
561  $conds['rc_patrolled'] = RecentChange::PRC_AUTOPATROLLED;
562  } elseif ( in_array( self::FILTER_NOT_AUTOPATROLLED, $options['filters'] ) ) {
563  $conds[] = 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED;
564  }
565  }
566 
567  if ( in_array( self::FILTER_UNREAD, $options['filters'] ) ) {
568  $conds[] = 'rc_timestamp >= wl_notificationtimestamp';
569  } elseif ( in_array( self::FILTER_NOT_UNREAD, $options['filters'] ) ) {
570  // TODO: should this be changed to use Database::makeList?
571  $conds[] = 'wl_notificationtimestamp IS NULL OR rc_timestamp < wl_notificationtimestamp';
572  }
573 
574  return $conds;
575  }
576 
577  private function getStartEndConds( IDatabase $db, array $options ) {
578  if ( !isset( $options['start'] ) && !isset( $options['end'] ) ) {
579  return [];
580  }
581 
582  $conds = [];
583 
584  if ( isset( $options['start'] ) ) {
585  $after = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
586  $conds[] = 'rc_timestamp ' . $after . ' ' .
587  $db->addQuotes( $db->timestamp( $options['start'] ) );
588  }
589  if ( isset( $options['end'] ) ) {
590  $before = $options['dir'] === self::DIR_OLDER ? '>=' : '<=';
591  $conds[] = 'rc_timestamp ' . $before . ' ' .
592  $db->addQuotes( $db->timestamp( $options['end'] ) );
593  }
594 
595  return $conds;
596  }
597 
598  private function getUserRelatedConds( IDatabase $db, Authority $user, array $options ) {
599  if ( !array_key_exists( 'onlyByUser', $options ) && !array_key_exists( 'notByUser', $options ) ) {
600  return [];
601  }
602 
603  $conds = [];
604 
605  if ( array_key_exists( 'onlyByUser', $options ) ) {
606  $conds['watchlist_actor.actor_name'] = $options['onlyByUser'];
607  } elseif ( array_key_exists( 'notByUser', $options ) ) {
608  $conds[] = 'watchlist_actor.actor_name<>' . $db->addQuotes( $options['notByUser'] );
609  }
610 
611  // Avoid brute force searches (T19342)
612  $bitmask = 0;
613  if ( !$user->isAllowed( 'deletedhistory' ) ) {
614  $bitmask = RevisionRecord::DELETED_USER;
615  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
616  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
617  }
618  if ( $bitmask ) {
619  $conds[] = $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask";
620  }
621 
622  return $conds;
623  }
624 
625  private function getExtraDeletedPageLogEntryRelatedCond( IDatabase $db, Authority $user ) {
626  // LogPage::DELETED_ACTION hides the affected page, too. So hide those
627  // entirely from the watchlist, or someone could guess the title.
628  $bitmask = 0;
629  if ( !$user->isAllowed( 'deletedhistory' ) ) {
630  $bitmask = LogPage::DELETED_ACTION;
631  } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
633  }
634  if ( $bitmask ) {
635  return $db->makeList( [
636  'rc_type != ' . RC_LOG,
637  $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
638  ], LIST_OR );
639  }
640  return '';
641  }
642 
643  private function getStartFromConds( IDatabase $db, array $options, array $startFrom ) {
644  $op = $options['dir'] === self::DIR_OLDER ? '<=' : '>=';
645  [ $rcTimestamp, $rcId ] = $startFrom;
646  $rcTimestamp = $db->timestamp( $rcTimestamp );
647  $rcId = (int)$rcId;
648  return $db->buildComparison( $op, [
649  'rc_timestamp' => $rcTimestamp,
650  'rc_id' => $rcId,
651  ] );
652  }
653 
654  private function getWatchedItemsForUserQueryConds(
655  IDatabase $db, UserIdentity $user, array $options
656  ) {
657  $conds = [ 'wl_user' => $user->getId() ];
658  if ( $options['namespaceIds'] ) {
659  $conds['wl_namespace'] = array_map( 'intval', $options['namespaceIds'] );
660  }
661  if ( isset( $options['filter'] ) ) {
662  $filter = $options['filter'];
663  if ( $filter === self::FILTER_CHANGED ) {
664  $conds[] = 'wl_notificationtimestamp IS NOT NULL';
665  } else {
666  $conds[] = 'wl_notificationtimestamp IS NULL';
667  }
668  }
669 
670  if ( isset( $options['from'] ) ) {
671  $op = $options['sort'] === self::SORT_ASC ? '>=' : '<=';
672  $conds[] = $this->getFromUntilTargetConds( $db, $options['from'], $op );
673  }
674  if ( isset( $options['until'] ) ) {
675  $op = $options['sort'] === self::SORT_ASC ? '<=' : '>=';
676  $conds[] = $this->getFromUntilTargetConds( $db, $options['until'], $op );
677  }
678  if ( isset( $options['startFrom'] ) ) {
679  $op = $options['sort'] === self::SORT_ASC ? '>=' : '<=';
680  $conds[] = $this->getFromUntilTargetConds( $db, $options['startFrom'], $op );
681  }
682 
683  return $conds;
684  }
685 
695  private function getFromUntilTargetConds( IDatabase $db, LinkTarget $target, $op ) {
696  return $db->buildComparison( $op, [
697  'wl_namespace' => $target->getNamespace(),
698  'wl_title' => $target->getDBkey(),
699  ] );
700  }
701 
702  private function getWatchedItemsWithRCInfoQueryDbOptions( array $options ) {
703  $dbOptions = [];
704 
705  if ( array_key_exists( 'dir', $options ) ) {
706  $sort = $options['dir'] === self::DIR_OLDER ? ' DESC' : '';
707  $dbOptions['ORDER BY'] = [ 'rc_timestamp' . $sort, 'rc_id' . $sort ];
708  }
709 
710  if ( array_key_exists( 'limit', $options ) ) {
711  $dbOptions['LIMIT'] = (int)$options['limit'] + 1;
712  }
713  if ( $this->maxQueryExecutionTime ) {
714  $dbOptions['MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
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  if ( $this->maxQueryExecutionTime ) {
734  $dbOptions['MAX_EXECUTION_TIME'] = $this->maxQueryExecutionTime;
735  }
736  return $dbOptions;
737  }
738 
739  private function getWatchedItemsWithRCInfoQueryJoinConds( array $options ) {
740  $joinConds = [
741  'watchlist' => [ 'JOIN',
742  [
743  'wl_namespace=rc_namespace',
744  'wl_title=rc_title'
745  ]
746  ]
747  ];
748 
749  if ( $this->expiryEnabled ) {
750  $joinConds['watchlist_expiry'] = [ 'LEFT JOIN', 'wl_id = we_item' ];
751  }
752 
753  if ( !$options['allRevisions'] ) {
754  $joinConds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
755  }
756  if ( in_array( self::INCLUDE_COMMENT, $options['includeFields'] ) ) {
757  $joinConds += $this->commentStore->getJoin( 'rc_comment' )['joins'];
758  }
759  if ( in_array( self::INCLUDE_USER, $options['includeFields'] ) ||
760  in_array( self::INCLUDE_USER_ID, $options['includeFields'] ) ||
761  in_array( self::FILTER_ANON, $options['filters'] ) ||
762  in_array( self::FILTER_NOT_ANON, $options['filters'] ) ||
763  array_key_exists( 'onlyByUser', $options ) || array_key_exists( 'notByUser', $options )
764  ) {
765  $joinConds['watchlist_actor'] = [ 'JOIN', 'actor_id=rc_actor' ];
766  }
767  return $joinConds;
768  }
769 
770 }
const RC_NEW
Definition: Defines.php:117
const LIST_OR
Definition: Defines.php:46
const RC_LOG
Definition: Defines.php:118
const RC_EXTERNAL
Definition: Defines.php:119
const RC_EDIT
Definition: Defines.php:116
const RC_CATEGORIZE
Definition: Defines.php:120
static newWithMessage(?ApiBase $module, $msg, $code=null, $data=null, $httpCode=0, Throwable $previous=null)
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:664
const DELETED_RESTRICTED
Definition: LogPage.php:47
const DELETED_ACTION
Definition: LogPage.php:44
Handle database storage of comments such as edit summaries and log reasons.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:567
Page revision base class.
Represents the target of a wiki link.
Definition: TitleValue.php:44
Provides access to user options.
internal since 1.36
Definition: User.php:98
useRCPatrol()
Check whether to enable recent changes patrol features for this user.
Definition: User.php:2361
useNPPatrol()
Check whether to enable new pages patrol features for this user.
Definition: User.php:2371
const PRC_UNPATROLLED
const PRC_AUTOPATROLLED
__construct(ILoadBalancer $loadBalancer, CommentStore $commentStore, WatchedItemStoreInterface $watchedItemStore, HookContainer $hookContainer, UserOptionsLookup $userOptionsLookup, bool $expiryEnabled=false, int $maxQueryExecutionTime=0)
getWatchedItemsForUser(UserIdentity $user, array $options=[])
For simple listing of user's watchlist items, see WatchedItemStore::getWatchedItemsForUser.
getWatchedItemsWithRecentChangeInfo(User $user, array $options=[], &$startFrom=null)
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:38
Represents the target of a wiki link.
Definition: LinkTarget.php:30
getNamespace()
Get the namespace index.
getDBkey()
Get the main part of the link target, in canonical database form.
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
isAllowed(string $permission, PermissionStatus $status=null)
Checks whether this authority has the given permission in general.
isAllowedAny(... $permissions)
Checks whether this authority has any of the given permissions in general.
Interface for objects representing user identity.
isRegistered()
This must be equivalent to getId() != 0 and is provided for code readability.
getId( $wikiId=self::LOCAL)
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:36
This class is a delegate to ILBFactory for a given database cluster.
getType()
Get the RDBMS type of the server (e.g.
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
bitAnd( $fieldLeft, $fieldRight)
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by ConvertibleTimestamp to the format used for ins...
buildComparison(string $op, array $conds)
Build a condition comparing multiple values, for use with indexes that cover multiple fields,...
const DB_REPLICA
Definition: defines.php:26