Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[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 * @author Brion Vibber <brion at pobox.com>
28 * @addtogroup maintenance
29 */
30
31 $options = array( 'fix' );
32
33 require_once( 'commandLine.inc' );
34 require_once( 'FiveUpgrade.inc' );
35
36 class WatchlistCleanup extends FiveUpgrade {
37 function WatchlistCleanup( $dryrun = false ) {
38 parent::FiveUpgrade();
39
40 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
41 $this->dryrun = $dryrun;
42 }
43
44 function cleanup() {
45 $this->runTable( 'watchlist',
46 '',
47 array( &$this, 'processEntry' ) );
48 }
49
50 function init( $count, $table ) {
51 $this->processed = 0;
52 $this->updated = 0;
53 $this->count = $count;
54 $this->startTime = wfTime();
55 $this->table = $table;
56 }
57
58 function progress( $updated ) {
59 $this->updated += $updated;
60 $this->processed++;
61 if( $this->processed % 100 != 0 ) {
62 return;
63 }
64 $portion = $this->processed / $this->count;
65 $updateRate = $this->updated / $this->processed;
66
67 $now = wfTime();
68 $delta = $now - $this->startTime;
69 $estimatedTotalTime = $delta / $portion;
70 $eta = $this->startTime + $estimatedTotalTime;
71
72 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
73 wfWikiID(),
74 wfTimestamp( TS_DB, intval( $now ) ),
75 $portion * 100.0,
76 $this->table,
77 wfTimestamp( TS_DB, intval( $eta ) ),
78 $this->processed,
79 $this->count,
80 $this->processed / $delta,
81 $updateRate * 100.0 );
82 flush();
83 }
84
85 function runTable( $table, $where, $callback ) {
86 $fname = 'WatchlistCleanup::runTable';
87
88 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
89 $this->init( $count, 'watchlist' );
90 $this->log( "Processing $table..." );
91
92 $tableName = $this->dbr->tableName( $table );
93 $sql = "SELECT * FROM $tableName $where";
94 $result = $this->dbr->query( $sql, $fname );
95
96 while( $row = $this->dbr->fetchObject( $result ) ) {
97 call_user_func( $callback, $row );
98 }
99 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
100 $this->dbr->freeResult( $result );
101 }
102
103 function processEntry( $row ) {
104 $current = Title::makeTitle( $row->wl_namespace, $row->wl_title );
105 $display = $current->getPrefixedText();
106
107 $verified = UtfNormal::cleanUp( $display );
108
109 $title = Title::newFromText( $verified );
110
111 if( $row->wl_user == 0 || is_null( $title ) || !$title->equals( $current ) ) {
112 $this->log( "invalid watch by {$row->wl_user} for ({$row->wl_namespace}, \"{$row->wl_title}\")" );
113 $this->removeWatch( $row );
114 return $this->progress( 1 );
115 }
116
117 $this->progress( 0 );
118 }
119
120 function removeWatch( $row ) {
121 if( !$this->dryrun) {
122 $dbw =& wfGetDB( DB_MASTER );
123 $dbw->delete( 'watchlist', array(
124 'wl_user' => $row->wl_user,
125 'wl_namespace' => $row->wl_namespace,
126 'wl_title' => $row->wl_title ),
127 'WatchlistCleanup::removeWatch' );
128 $this->log( '- removed' );
129 }
130 }
131 }
132
133 $wgUser->setName( 'Conversion script' );
134 $caps = new WatchlistCleanup( !isset( $options['fix'] ) );
135 $caps->cleanup();
136
137 ?>