WARNING: HUGE COMMIT
[lhc/web/wiklou.git] / maintenance / cleanupWatchlist.php
1 <?php
2 /*
3 * Script to remove broken, unparseable titles in the Watchlist.
4 *
5 * Usage: php cleanupWatchlist.php [--fix]
6 * Options:
7 * --fix Actually remove entries; without will only report.
8 *
9 * Copyright (C) 2005,2006 Brion Vibber <brion@pobox.com>
10 * http://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 $options = array( 'fix' );
33
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
36
37 /**
38 * @ingroup Maintenance
39 */
40 class WatchlistCleanup extends FiveUpgrade {
41 function WatchlistCleanup( $dryrun = false ) {
42 parent::FiveUpgrade();
43
44 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
45 $this->dryrun = $dryrun;
46 }
47
48 function cleanup() {
49 $this->runTable( 'watchlist',
50 '',
51 array( &$this, 'processEntry' ) );
52 }
53
54 function init( $count, $table ) {
55 $this->processed = 0;
56 $this->updated = 0;
57 $this->count = $count;
58 $this->startTime = wfTime();
59 $this->table = $table;
60 }
61
62 function progress( $updated ) {
63 $this->updated += $updated;
64 $this->processed++;
65 if( $this->processed % 100 != 0 ) {
66 return;
67 }
68 $portion = $this->processed / $this->count;
69 $updateRate = $this->updated / $this->processed;
70
71 $now = wfTime();
72 $delta = $now - $this->startTime;
73 $estimatedTotalTime = $delta / $portion;
74 $eta = $this->startTime + $estimatedTotalTime;
75
76 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
77 wfWikiID(),
78 wfTimestamp( TS_DB, intval( $now ) ),
79 $portion * 100.0,
80 $this->table,
81 wfTimestamp( TS_DB, intval( $eta ) ),
82 $this->processed,
83 $this->count,
84 $this->processed / $delta,
85 $updateRate * 100.0 );
86 flush();
87 }
88
89 function runTable( $table, $where, $callback ) {
90 $fname = 'WatchlistCleanup::runTable';
91
92 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
93 $this->init( $count, 'watchlist' );
94 $this->log( "Processing $table..." );
95
96 $tableName = $this->dbr->tableName( $table );
97 $sql = "SELECT * FROM $tableName $where";
98 $result = $this->dbr->query( $sql, $fname );
99
100 while( $row = $this->dbr->fetchObject( $result ) ) {
101 call_user_func( $callback, $row );
102 }
103 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
104 $this->dbr->freeResult( $result );
105 }
106
107 function processEntry( $row ) {
108 $current = Title::makeTitle( $row->wl_namespace, $row->wl_title );
109 $display = $current->getPrefixedText();
110
111 $verified = UtfNormal::cleanUp( $display );
112
113 $title = Title::newFromText( $verified );
114
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 );
119 }
120
121 $this->progress( 0 );
122 }
123
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' );
133 }
134 }
135 }
136
137 $wgUser->setName( 'Conversion script' );
138 $caps = new WatchlistCleanup( !isset( $options['fix'] ) );
139 $caps->cleanup();
140
141