Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
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
21use MediaWiki\MainConfigNames;
22use MediaWiki\MediaWikiServices;
23
24/**
25 * Job for clearing all of the "last viewed" timestamps for a user's watchlist, or setting them all
26 * to the same value.
27 *
28 * Job parameters include:
29 *   - userId: affected user ID [required]
30 *   - casTime: UNIX timestamp of the event that triggered this job [required]
31 *   - timestamp: value to set all of the "last viewed" timestamps to [optional, defaults to null]
32 *
33 * @since 1.31
34 * @ingroup JobQueue
35 */
36class ClearWatchlistNotificationsJob extends Job implements GenericParameterJob {
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}