MediaWiki master
ClearWatchlistNotificationsJob.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Watchlist;
22
24use InvalidArgumentException;
25use Job;
28
42 public function __construct( array $params ) {
43 parent::__construct( 'clearWatchlistNotifications', $params );
44
45 static $required = [ 'userId', 'casTime' ];
46 $missing = implode( ', ', array_diff( $required, array_keys( $this->params ) ) );
47 if ( $missing != '' ) {
48 throw new InvalidArgumentException( "Missing parameter(s) $missing" );
49 }
50
51 $this->removeDuplicates = true;
52 }
53
54 public function run() {
56 $dbProvider = $services->getConnectionProvider();
57 $rowsPerQuery = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
58
59 $dbw = $dbProvider->getPrimaryDatabase();
60 $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
61 if ( !isset( $this->params['timestamp'] ) ) {
62 $timestamp = null;
63 $timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', null );
64 } else {
65 $timestamp = $dbw->timestamp( $this->params['timestamp'] );
66 $timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', $timestamp )
67 ->or( 'wl_notificationtimestamp', '=', null );
68 }
69 // New notifications since the reset should not be cleared
70 $casTimeCond = $dbw->expr( 'wl_notificationtimestamp', '<', $dbw->timestamp( $this->params['casTime'] ) )
71 ->or( 'wl_notificationtimestamp', '=', null );
72
73 $firstBatch = true;
74 do {
75 $idsToUpdate = $dbw->newSelectQueryBuilder()
76 ->select( 'wl_id' )
77 ->from( 'watchlist' )
78 ->where( [ 'wl_user' => $this->params['userId'] ] )
79 ->andWhere( $timestampCond )
80 ->andWhere( $casTimeCond )
81 ->limit( $rowsPerQuery )
82 ->caller( __METHOD__ )->fetchFieldValues();
83
84 if ( $idsToUpdate ) {
85 $dbw->newUpdateQueryBuilder()
86 ->update( 'watchlist' )
87 ->set( [ 'wl_notificationtimestamp' => $timestamp ] )
88 ->where( [ 'wl_id' => $idsToUpdate ] )
89 ->andWhere( $casTimeCond )
90 ->caller( __METHOD__ )->execute();
91 if ( !$firstBatch ) {
92 $dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
93 }
94 $firstBatch = false;
95 }
96 } while ( $idsToUpdate );
97 return true;
98 }
99}
101class_alias( ClearWatchlistNotificationsJob::class, 'ClearWatchlistNotificationsJob' );
Describe and execute a background job.
Definition Job.php:41
array $params
Array of job parameters.
Definition Job.php:46
A class containing constants representing the names of configuration variables.
const UpdateRowsPerQuery
Name constant for the UpdateRowsPerQuery setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Job for clearing all of the "last viewed" timestamps for a user's watchlist, or setting them all to t...
Interface for generic jobs only uses the parameters field and are JSON serializable.