Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CleanupWatchlist
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 processRow
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
20
 removeWatch
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
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
35require_once __DIR__ . '/TableCleanup.php';
36
37/**
38 * Maintenance script to remove broken, unparseable titles in the watchlist table.
39 *
40 * @ingroup Maintenance
41 */
42class CleanupWatchlist extends TableCleanup {
43    protected $defaultParams = [
44        'table' => 'watchlist',
45        'index' => [ 'wl_id' ],
46        'conds' => [],
47        'callback' => 'processRow'
48    ];
49
50    public function __construct() {
51        parent::__construct();
52        $this->addDescription( 'Script to remove broken, unparseable titles in the Watchlist' );
53        $this->addOption( 'fix', 'Actually remove entries; without will only report.' );
54    }
55
56    public function execute() {
57        if ( !$this->hasOption( 'fix' ) ) {
58            $this->output( "Dry run only: use --fix to enable updates\n" );
59        }
60        parent::execute();
61    }
62
63    protected function processRow( $row ) {
64        $current = Title::makeTitle( $row->wl_namespace, $row->wl_title );
65        $display = $current->getPrefixedText();
66        $verified = $this->getServiceContainer()->getContentLanguage()->normalize( $display );
67        $title = Title::newFromText( $verified );
68
69        if ( $row->wl_user == 0 || $title === null || !$title->equals( $current ) ) {
70            $this->output( "invalid watch by {$row->wl_user} for "
71                . "({$row->wl_namespace}, \"{$row->wl_title}\")\n" );
72            $updated = $this->removeWatch( $row );
73            $this->progress( $updated );
74
75            return;
76        }
77        $this->progress( 0 );
78    }
79
80    private function removeWatch( $row ) {
81        if ( !$this->dryrun && $this->hasOption( 'fix' ) ) {
82            $dbw = $this->getPrimaryDB();
83            $dbw->newDeleteQueryBuilder()
84                ->deleteFrom( 'watchlist' )
85                ->where( [ 'wl_id' => $row->wl_id ] )
86                ->caller( __METHOD__ )
87                ->execute();
88            if ( $this->getConfig()->get( MainConfigNames::WatchlistExpiry ) ) {
89                $dbw->newDeleteQueryBuilder()
90                    ->deleteFrom( 'watchlist_expiry' )
91                    ->where( [ 'we_item' => $row->wl_id ] )
92                    ->caller( __METHOD__ )
93                    ->execute();
94            }
95
96            $this->output( "- removed\n" );
97
98            return 1;
99        } else {
100            return 0;
101        }
102    }
103}
104
105$maintClass = CleanupWatchlist::class;
106require_once RUN_MAINTENANCE_IF_MAIN;