Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CleanupWatchlist
100.00% covered (success)
100.00%
33 / 33
100.00% covered (success)
100.00%
4 / 4
11
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 processRow
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 removeWatch
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2/**
3 * Remove broken, unparseable titles in the watchlist table.
4 *
5 * Usage: php cleanupWatchlist.php [--fix]
6 * Options:
7 *   --fix  Actually remove entries; without will only report.
8 *
9 * Copyright © 2005,2006 Brooke Vibber <bvibber@wikimedia.org>
10 * https://www.mediawiki.org/
11 *
12 * @license GPL-2.0-or-later
13 * @file
14 * @author Brooke Vibber <bvibber@wikimedia.org>
15 * @ingroup Maintenance
16 */
17
18use MediaWiki\MainConfigNames;
19use MediaWiki\Title\Title;
20
21// @codeCoverageIgnoreStart
22require_once __DIR__ . '/TableCleanup.php';
23// @codeCoverageIgnoreEnd
24
25/**
26 * Maintenance script to remove broken, unparseable titles in the watchlist table.
27 *
28 * @ingroup Maintenance
29 */
30class CleanupWatchlist extends TableCleanup {
31    /** @inheritDoc */
32    protected $defaultParams = [
33        'table' => 'watchlist',
34        'index' => [ 'wl_id' ],
35        'conds' => [],
36        'callback' => 'processRow'
37    ];
38
39    public function __construct() {
40        parent::__construct();
41        $this->addDescription( 'Script to remove broken, unparseable titles in the Watchlist' );
42        $this->addOption( 'fix', 'Actually remove entries; without will only report.' );
43    }
44
45    public function execute() {
46        if ( !$this->hasOption( 'fix' ) ) {
47            $this->output( "Dry run only: use --fix to enable updates\n" );
48        }
49        parent::execute();
50    }
51
52    protected function processRow( \stdClass $row ) {
53        $current = Title::makeTitle( $row->wl_namespace, $row->wl_title );
54        $display = $current->getPrefixedText();
55        $verified = $this->getServiceContainer()->getContentLanguage()->normalize( $display );
56        $title = Title::newFromText( $verified );
57
58        if ( $row->wl_user == 0 || $title === null || !$title->equals( $current ) ) {
59            $this->output( "invalid watch by {$row->wl_user} for "
60                . "({$row->wl_namespace}, \"{$row->wl_title}\")\n" );
61            $updated = $this->removeWatch( $row );
62            $this->progress( $updated );
63
64            return;
65        }
66        $this->progress( 0 );
67    }
68
69    private function removeWatch( \stdClass $row ): int {
70        if ( !$this->dryrun && $this->hasOption( 'fix' ) ) {
71            $dbw = $this->getPrimaryDB();
72            $dbw->newDeleteQueryBuilder()
73                ->deleteFrom( 'watchlist' )
74                ->where( [ 'wl_id' => $row->wl_id ] )
75                ->caller( __METHOD__ )
76                ->execute();
77            if ( $this->getConfig()->get( MainConfigNames::WatchlistExpiry ) ) {
78                $dbw->newDeleteQueryBuilder()
79                    ->deleteFrom( 'watchlist_expiry' )
80                    ->where( [ 'we_item' => $row->wl_id ] )
81                    ->caller( __METHOD__ )
82                    ->execute();
83            }
84
85            $this->output( "- removed\n" );
86
87            return 1;
88        } else {
89            return 0;
90        }
91    }
92}
93
94// @codeCoverageIgnoreStart
95$maintClass = CleanupWatchlist::class;
96require_once RUN_MAINTENANCE_IF_MAIN;
97// @codeCoverageIgnoreEnd