3 * Script to remove broken, unparseable titles in the Watchlist.
5 * Usage: php cleanupWatchlist.php [--fix]
7 * --fix Actually remove entries; without will only report.
9 * Copyright (C) 2005,2006 Brion Vibber <brion@pobox.com>
10 * http://www.mediawiki.org/
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.
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.
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
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 $options = array( 'fix' );
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
38 * @ingroup Maintenance
40 class WatchlistCleanup
extends FiveUpgrade
{
41 function WatchlistCleanup( $dryrun = false ) {
42 parent
::FiveUpgrade();
44 $this->maxLag
= 10; # if slaves are lagged more than 10 secs, wait
45 $this->dryrun
= $dryrun;
49 $this->runTable( 'watchlist',
51 array( &$this, 'processEntry' ) );
54 function init( $count, $table ) {
57 $this->count
= $count;
58 $this->startTime
= wfTime();
59 $this->table
= $table;
62 function progress( $updated ) {
63 $this->updated +
= $updated;
65 if( $this->processed %
100 != 0 ) {
68 $portion = $this->processed
/ $this->count
;
69 $updateRate = $this->updated
/ $this->processed
;
72 $delta = $now - $this->startTime
;
73 $estimatedTotalTime = $delta / $portion;
74 $eta = $this->startTime +
$estimatedTotalTime;
76 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
78 wfTimestamp( TS_DB
, intval( $now ) ),
81 wfTimestamp( TS_DB
, intval( $eta ) ),
84 $this->processed
/ $delta,
85 $updateRate * 100.0 );
89 function runTable( $table, $where, $callback ) {
90 $fname = 'WatchlistCleanup::runTable';
92 $count = $this->dbw
->selectField( $table, 'count(*)', '', $fname );
93 $this->init( $count, 'watchlist' );
94 $this->log( "Processing $table..." );
96 $tableName = $this->dbr
->tableName( $table );
97 $sql = "SELECT * FROM $tableName $where";
98 $result = $this->dbr
->query( $sql, $fname );
100 while( $row = $this->dbr
->fetchObject( $result ) ) {
101 call_user_func( $callback, $row );
103 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
104 $this->dbr
->freeResult( $result );
107 function processEntry( $row ) {
108 $current = Title
::makeTitle( $row->wl_namespace
, $row->wl_title
);
109 $display = $current->getPrefixedText();
111 $verified = UtfNormal
::cleanUp( $display );
113 $title = Title
::newFromText( $verified );
115 if( $row->wl_user
== 0 ||
is_null( $title ) ||
!$title->equals( $current ) ) {
116 $this->log( "invalid watch by {$row->wl_user} for ({$row->wl_namespace}, \"{$row->wl_title}\")" );
117 $this->removeWatch( $row );
118 return $this->progress( 1 );
121 $this->progress( 0 );
124 function removeWatch( $row ) {
125 if( !$this->dryrun
) {
126 $dbw = wfGetDB( DB_MASTER
);
127 $dbw->delete( 'watchlist', array(
128 'wl_user' => $row->wl_user
,
129 'wl_namespace' => $row->wl_namespace
,
130 'wl_title' => $row->wl_title
),
131 'WatchlistCleanup::removeWatch' );
132 $this->log( '- removed' );
137 $wgUser->setName( 'Conversion script' );
138 $caps = new WatchlistCleanup( !isset( $options['fix'] ) );