MediaWiki  master
ClearWatchlistNotificationsJob.php
Go to the documentation of this file.
1 <?php
24 
38  public function __construct( array $params ) {
39  parent::__construct( 'clearWatchlistNotifications', $params );
40 
41  static $required = [ 'userId', 'casTime' ];
42  $missing = implode( ', ', array_diff( $required, array_keys( $this->params ) ) );
43  if ( $missing != '' ) {
44  throw new InvalidArgumentException( "Missing parameter(s) $missing" );
45  }
46 
47  $this->removeDuplicates = true;
48  }
49 
50  public function run() {
51  $services = MediaWikiServices::getInstance();
52  $lbFactory = $services->getDBLoadBalancerFactory();
53  $rowsPerQuery = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
54 
55  $dbw = $lbFactory->getMainLB()->getConnectionRef( DB_PRIMARY );
56  $ticket = $lbFactory->getEmptyTransactionTicket( __METHOD__ );
57  $timestamp = $this->params['timestamp'] ?? null;
58  if ( $timestamp === null ) {
59  $timestampCond = 'wl_notificationtimestamp IS NOT NULL';
60  } else {
61  $timestamp = $dbw->timestamp( $timestamp );
62  $timestampCond = 'wl_notificationtimestamp != ' . $dbw->addQuotes( $timestamp ) .
63  ' OR wl_notificationtimestamp IS NULL';
64  }
65  // New notifications since the reset should not be cleared
66  $casTimeCond = 'wl_notificationtimestamp < ' .
67  $dbw->addQuotes( $dbw->timestamp( $this->params['casTime'] ) ) .
68  ' OR wl_notificationtimestamp IS NULL';
69 
70  $firstBatch = true;
71  do {
72  $idsToUpdate = $dbw->selectFieldValues(
73  'watchlist',
74  'wl_id',
75  [
76  'wl_user' => $this->params['userId'],
77  $timestampCond,
78  $casTimeCond,
79  ],
80  __METHOD__,
81  [ 'LIMIT' => $rowsPerQuery ]
82  );
83  if ( $idsToUpdate ) {
84  $dbw->update(
85  'watchlist',
86  [ 'wl_notificationtimestamp' => $timestamp ],
87  [
88  'wl_id' => $idsToUpdate,
89  // For paranoia, enforce the CAS time condition here too
90  $casTimeCond
91  ],
92  __METHOD__
93  );
94  if ( !$firstBatch ) {
95  $lbFactory->commitAndWaitForReplication( __METHOD__, $ticket );
96  }
97  $firstBatch = false;
98  }
99  } while ( $idsToUpdate );
100  return true;
101  }
102 }
Job for clearing all of the "last viewed" timestamps for a user's watchlist, or setting them all to t...
Class to both describe a background job and handle jobs.
Definition: Job.php:39
array $params
Array of job parameters.
Definition: Job.php:44
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Interface for generic jobs only uses the parameters field and are JSON serializable.
const DB_PRIMARY
Definition: defines.php:28