MediaWiki master
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 $dbProvider = $services->getConnectionProvider();
52 $rowsPerQuery = $services->getMainConfig()->get( MainConfigNames::UpdateRowsPerQuery );
53
54 $dbw = $dbProvider->getPrimaryDatabase();
55 $ticket = $dbProvider->getEmptyTransactionTicket( __METHOD__ );
56 if ( !isset( $this->params['timestamp'] ) ) {
57 $timestamp = null;
58 $timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', null );
59 } else {
60 $timestamp = $dbw->timestamp( $this->params['timestamp'] );
61 $timestampCond = $dbw->expr( 'wl_notificationtimestamp', '!=', $timestamp )
62 ->or( 'wl_notificationtimestamp', '=', null );
63 }
64 // New notifications since the reset should not be cleared
65 $casTimeCond = $dbw->expr( 'wl_notificationtimestamp', '<', $dbw->timestamp( $this->params['casTime'] ) )
66 ->or( 'wl_notificationtimestamp', '=', null );
67
68 $firstBatch = true;
69 do {
70 $idsToUpdate = $dbw->newSelectQueryBuilder()
71 ->select( 'wl_id' )
72 ->from( 'watchlist' )
73 ->where( [ 'wl_user' => $this->params['userId'] ] )
74 ->andWhere( $timestampCond )
75 ->andWhere( $casTimeCond )
76 ->limit( $rowsPerQuery )
77 ->caller( __METHOD__ )->fetchFieldValues();
78
79 if ( $idsToUpdate ) {
80 $dbw->newUpdateQueryBuilder()
81 ->update( 'watchlist' )
82 ->set( [ 'wl_notificationtimestamp' => $timestamp ] )
83 ->where( [ 'wl_id' => $idsToUpdate ] )
84 ->andWhere( $casTimeCond )
85 ->caller( __METHOD__ )->execute();
86 if ( !$firstBatch ) {
87 $dbProvider->commitAndWaitForReplication( __METHOD__, $ticket );
88 }
89 $firstBatch = false;
90 }
91 } while ( $idsToUpdate );
92 return true;
93 }
94}
array $params
The job parameters.
Job for clearing all of the "last viewed" timestamps for a user's watchlist, or setting them all to t...
Describe and execute a background job.
Definition Job.php:40
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.