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