WatchlistCleanup::processRow()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 2
nop 1
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 39 and the first side effect is on line 32.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 Brion Vibber <[email protected]>
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 Brion Vibber <brion at pobox.com>
29
 * @ingroup Maintenance
30
 */
31
32
require_once __DIR__ . '/cleanupTable.inc';
33
34
/**
35
 * Maintenance script to remove broken, unparseable titles in the watchlist table.
36
 *
37
 * @ingroup Maintenance
38
 */
39
class WatchlistCleanup extends TableCleanup {
40
	protected $defaultParams = [
41
		'table' => 'watchlist',
42
		'index' => [ 'wl_user', 'wl_namespace', 'wl_title' ],
43
		'conds' => [],
44
		'callback' => 'processRow'
45
	];
46
47
	public function __construct() {
48
		parent::__construct();
49
		$this->addDescription( 'Script to remove broken, unparseable titles in the Watchlist' );
50
		$this->addOption( 'fix', 'Actually remove entries; without will only report.' );
51
	}
52
53
	function execute() {
54
		if ( !$this->hasOption( 'fix' ) ) {
55
			$this->output( "Dry run only: use --fix to enable updates\n" );
56
		}
57
		parent::execute();
58
	}
59
60
	protected function processRow( $row ) {
61
		global $wgContLang;
62
		$current = Title::makeTitle( $row->wl_namespace, $row->wl_title );
63
		$display = $current->getPrefixedText();
64
		$verified = $wgContLang->normalize( $display );
65
		$title = Title::newFromText( $verified );
66
67
		if ( $row->wl_user == 0 || is_null( $title ) || !$title->equals( $current ) ) {
68
			$this->output( "invalid watch by {$row->wl_user} for "
69
				. "({$row->wl_namespace}, \"{$row->wl_title}\")\n" );
70
			$updated = $this->removeWatch( $row );
71
			$this->progress( $updated );
72
73
			return;
74
		}
75
		$this->progress( 0 );
76
	}
77
78
	private function removeWatch( $row ) {
79
		if ( !$this->dryrun && $this->hasOption( 'fix' ) ) {
80
			$dbw = $this->getDB( DB_MASTER );
81
			$dbw->delete(
82
				'watchlist', [
83
				'wl_user' => $row->wl_user,
84
				'wl_namespace' => $row->wl_namespace,
85
				'wl_title' => $row->wl_title ],
86
				__METHOD__
87
			);
88
89
			$this->output( "- removed\n" );
90
91
			return 1;
92
		} else {
93
			return 0;
94
		}
95
	}
96
}
97
98
$maintClass = "WatchlistCleanup";
99
require_once RUN_MAINTENANCE_IF_MAIN;
100