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
27 * @author Brion Vibber <brion at pobox.com>
29 * @subpackage maintenance
32 $options = array( 'fix' );
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
37 class WatchlistCleanup
extends FiveUpgrade
{
38 function WatchlistCleanup( $dryrun = false ) {
39 parent
::FiveUpgrade();
41 $this->maxLag
= 10; # if slaves are lagged more than 10 secs, wait
42 $this->dryrun
= $dryrun;
46 $this->runTable( 'watchlist',
48 array( &$this, 'processEntry' ) );
51 function init( $count, $table ) {
54 $this->count
= $count;
55 $this->startTime
= wfTime();
56 $this->table
= $table;
59 function progress( $updated ) {
60 $this->updated +
= $updated;
62 if( $this->processed %
100 != 0 ) {
65 $portion = $this->processed
/ $this->count
;
66 $updateRate = $this->updated
/ $this->processed
;
69 $delta = $now - $this->startTime
;
70 $estimatedTotalTime = $delta / $portion;
71 $eta = $this->startTime +
$estimatedTotalTime;
73 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
75 wfTimestamp( TS_DB
, intval( $now ) ),
78 wfTimestamp( TS_DB
, intval( $eta ) ),
81 $this->processed
/ $delta,
82 $updateRate * 100.0 );
86 function runTable( $table, $where, $callback ) {
87 $fname = 'WatchlistCleanup::runTable';
89 $count = $this->dbw
->selectField( $table, 'count(*)', '', $fname );
90 $this->init( $count, 'watchlist' );
91 $this->log( "Processing $table..." );
93 $tableName = $this->dbr
->tableName( $table );
94 $sql = "SELECT * FROM $tableName $where";
95 $result = $this->dbr
->query( $sql, $fname );
97 while( $row = $this->dbr
->fetchObject( $result ) ) {
98 $updated = call_user_func( $callback, $row );
100 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
101 $this->dbr
->freeResult( $result );
104 function processEntry( $row ) {
107 $current = Title
::makeTitle( $row->wl_namespace
, $row->wl_title
);
108 $display = $current->getPrefixedText();
110 $verified = UtfNormal
::cleanUp( $display );
112 $title = Title
::newFromText( $verified );
114 if( $row->wl_user
== 0 ||
is_null( $title ) ||
!$title->equals( $current ) ) {
115 $this->log( "invalid watch by {$row->wl_user} for ({$row->wl_namespace}, \"{$row->wl_title}\")" );
116 $this->removeWatch( $row );
117 return $this->progress( 1 );
120 $this->progress( 0 );
123 function removeWatch( $row ) {
124 if( !$this->dryrun
) {
125 $dbw =& wfGetDB( DB_MASTER
);
126 $dbw->delete( 'watchlist', array(
127 'wl_user' => $row->wl_user
,
128 'wl_namespace' => $row->wl_namespace
,
129 'wl_title' => $row->wl_title
),
130 'WatchlistCleanup::removeWatch' );
131 $this->log( '- removed' );
136 $wgUser->setName( 'Conversion script' );
137 $caps = new WatchlistCleanup( !isset( $options['fix'] ) );