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 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @author Brooke Vibber <bvibber@wikimedia.org>
29 * @ingroup Maintenance
30 */
31
32use MediaWiki\MainConfigNames;
33use MediaWiki\Title\Title;
34
35// @codeCoverageIgnoreStart
36require_once __DIR__ . '/TableCleanup.php';
37// @codeCoverageIgnoreEnd
38
39/**
40 * Maintenance script to remove broken, unparseable titles in the watchlist table.
41 *
42 * @ingroup Maintenance
43 */
44class CleanupWatchlist extends TableCleanup {
45    /** @inheritDoc */
46    protected $defaultParams = [
47        'table' => 'watchlist',
48        'index' => [ 'wl_id' ],
49        'conds' => [],
50        'callback' => 'processRow'
51    ];
52
53    public function __construct() {
54        parent::__construct();
55        $this->addDescription( 'Script to remove broken, unparseable titles in the Watchlist' );
56        $this->addOption( 'fix', 'Actually remove entries; without will only report.' );
57    }
58
59    public function execute() {
60        if ( !$this->hasOption( 'fix' ) ) {
61            $this->output( "Dry run only: use --fix to enable updates\n" );
62        }
63        parent::execute();
64    }
65
66    protected function processRow( $row ) {
67        $current = Title::makeTitle( $row->wl_namespace, $row->wl_title );
68        $display = $current->getPrefixedText();
69        $verified = $this->getServiceContainer()->getContentLanguage()->normalize( $display );
70        $title = Title::newFromText( $verified );
71
72        if ( $row->wl_user == 0 || $title === null || !$title->equals( $current ) ) {
73            $this->output( "invalid watch by {$row->wl_user} for "
74                . "({$row->wl_namespace}, \"{$row->wl_title}\")\n" );
75            $updated = $this->removeWatch( $row );
76            $this->progress( $updated );
77
78            return;
79        }
80        $this->progress( 0 );
81    }
82
83    private function removeWatch( $row ) {
84        if ( !$this->dryrun && $this->hasOption( 'fix' ) ) {
85            $dbw = $this->getPrimaryDB();
86            $dbw->newDeleteQueryBuilder()
87                ->deleteFrom( 'watchlist' )
88                ->where( [ 'wl_id' => $row->wl_id ] )
89                ->caller( __METHOD__ )
90                ->execute();
91            if ( $this->getConfig()->get( MainConfigNames::WatchlistExpiry ) ) {
92                $dbw->newDeleteQueryBuilder()
93                    ->deleteFrom( 'watchlist_expiry' )
94                    ->where( [ 'we_item' => $row->wl_id ] )
95                    ->caller( __METHOD__ )
96                    ->execute();
97            }
98
99            $this->output( "- removed\n" );
100
101            return 1;
102        } else {
103            return 0;
104        }
105    }
106}
107
108// @codeCoverageIgnoreStart
109$maintClass = CleanupWatchlist::class;
110require_once RUN_MAINTENANCE_IF_MAIN;
111// @codeCoverageIgnoreEnd