Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClearWatchlistNotificationsJob
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 run
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Watchlist;
22
23use GenericParameterJob;
24use InvalidArgumentException;
25use Job;
26use MediaWiki\MainConfigNames;
27use MediaWiki\MediaWikiServices;
28
29/**
30 * Job for clearing all of the "last viewed" timestamps for a user's watchlist, or setting them all
31 * to the same value.
32 *
33 * Job parameters include:
34 *   - userId: affected user ID [required]
35 *   - casTime: UNIX timestamp of the event that triggered this job [required]
36 *   - timestamp: value to set all of the "last viewed" timestamps to [optional, defaults to null]
37 *
38 * @since 1.31
39 * @ingroup JobQueue
40 */
41class ClearWatchlistNotificationsJob extends Job implements GenericParameterJob {
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() {
55        $services = MediaWikiServices::getInstance();
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}
100/** @deprecated class alias since 1.43 */
101class_alias( ClearWatchlistNotificationsJob::class, 'ClearWatchlistNotificationsJob' );