MediaWiki master
ClearWatchlistNotificationsJob.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Watchlist;
8
9use InvalidArgumentException;
14
28 public function __construct( array $params ) {
29 parent::__construct( 'clearWatchlistNotifications', $params );
30
31 static $required = [ 'userId', 'casTime' ];
32 $missing = implode( ', ', array_diff( $required, array_keys( $this->params ) ) );
33 if ( $missing != '' ) {
34 throw new InvalidArgumentException( "Missing parameter(s) $missing" );
35 }
36
37 $this->removeDuplicates = true;
38 }
39
41 public function run() {
43 $dbProvider = $services->getConnectionProvider();
44 $rowsPerQuery = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
45
46 $dbw = $dbProvider->getPrimaryDatabase();
47 $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
48 if ( !isset( $this->params['timestamp'] ) ) {
49 $timestamp = null;
50 $timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', null );
51 } else {
52 $timestamp = $dbw->timestamp( $this->params['timestamp'] );
53 $timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', $timestamp )
54 ->or( 'wl_notificationtimestamp', '=', null );
55 }
56 // New notifications since the reset should not be cleared
57 $casTimeCond = $dbw->expr( 'wl_notificationtimestamp', '<', $dbw->timestamp( $this->params['casTime'] ) )
58 ->or( 'wl_notificationtimestamp', '=', null );
59
60 $firstBatch = true;
61 do {
62 $idsToUpdate = $dbw->newSelectQueryBuilder()
63 ->select( 'wl_id' )
64 ->from( 'watchlist' )
65 ->where( [ 'wl_user' => $this->params['userId'] ] )
66 ->andWhere( $timestampCond )
67 ->andWhere( $casTimeCond )
68 ->limit( $rowsPerQuery )
69 ->caller( __METHOD__ )->fetchFieldValues();
70
71 if ( $idsToUpdate ) {
72 $dbw->newUpdateQueryBuilder()
73 ->update( 'watchlist' )
74 ->set( [ 'wl_notificationtimestamp' => $timestamp ] )
75 ->where( [ 'wl_id' => $idsToUpdate ] )
76 ->andWhere( $casTimeCond )
77 ->caller( __METHOD__ )->execute();
78 if ( !$firstBatch ) {
79 $dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
80 }
81 $firstBatch = false;
82 }
83 } while ( $idsToUpdate );
84 return true;
85 }
86}
88class_alias( ClearWatchlistNotificationsJob::class, 'ClearWatchlistNotificationsJob' );
Describe and execute a background job.
Definition Job.php:28
array $params
Array of job parameters.
Definition Job.php:33
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...
run()
Run the job.If this method returns false or completes exceptionally, the job runner will retry execut...
Interface for generic jobs only uses the parameters field and are JSON serializable.