* (bug 2792) Update rebuildrecentchanges.inc for new schema
[lhc/web/wiklou.git] / maintenance / cleanupTitles.php
1 <?php
2 /*
3 * Script to clean up broken, unparseable titles.
4 *
5 * Usage: php cleanupTitles.php [--dry-run]
6 * Options:
7 * --dry-run don't actually try moving them
8 *
9 * Copyright (C) 2005 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @author Brion Vibber <brion at pobox.com>
28 * @package MediaWiki
29 * @subpackage maintenance
30 */
31
32 $options = array( 'dry-run' );
33
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
36
37 class TitleCleanup extends FiveUpgrade {
38 function TitleCleanup( $dryrun = false ) {
39 parent::FiveUpgrade();
40
41 $this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
42 $this->dryrun = $dryrun;
43 }
44
45 function cleanup() {
46 $this->runTable( 'page', 'WHERE page_namespace=0',
47 array( &$this, 'processPage' ) );
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 global $wgDBname;
73 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
74 $wgDBname,
75 wfTimestamp( TS_DB, intval( $now ) ),
76 $portion * 100.0,
77 $this->table,
78 wfTimestamp( TS_DB, intval( $eta ) ),
79 $this->processed,
80 $this->count,
81 $this->processed / $delta,
82 $updateRate * 100.0 );
83 flush();
84 }
85
86 function runTable( $table, $where, $callback ) {
87 $fname = 'CapsCleanup::buildTable';
88
89 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
90 $this->init( $count, 'page' );
91 $this->log( "Processing $table..." );
92
93 $tableName = $this->dbr->tableName( $table );
94 $sql = "SELECT * FROM $tableName $where";
95 $result = $this->dbr->query( $sql, $fname );
96
97 while( $row = $this->dbr->fetchObject( $result ) ) {
98 $updated = call_user_func( $callback, $row );
99 }
100 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
101 $this->dbr->freeResult( $result );
102 }
103
104 function processPage( $row ) {
105 global $wgContLang;
106
107 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
108 $display = $current->getPrefixedText();
109
110 $verified = UtfNormal::cleanUp( $display );
111 $title = Title::newFromText( $verified );
112
113 if( is_null( $title ) ) {
114 $this->log( "page $row->page_id ($display) is illegal." );
115 $this->moveIllegalPage( $row );
116 return $this->progress( 1 );
117 }
118
119 if( !$title->equals( $current ) ) {
120 $this->log( "page $row->page_id ($display) doesn't match self." );
121 $this->moveInconsistentPage( $row, $title );
122 return $this->progress( 1 );
123 }
124
125 $this->progress( 0 );
126 }
127
128 function moveIllegalPage( $row ) {
129 $legal = 'A-Za-z0-9_/\\\\-';
130 $legalized = preg_replace_callback( "!([^$legal])!",
131 array( &$this, 'hexChar' ),
132 $row->page_title );
133 if( $legalized == '.' ) $legalized = '(dot)';
134 if( $legalized == '_' ) $legalized = '(space)';
135 $legalized = 'Broken/' . $legalized;
136
137 $title = Title::newFromText( $legalized );
138 if( is_null( $title ) ) {
139 $clean = 'Broken/id:' . $row->page_id;
140 $this->log( "Couldn't legalize; form '$legalized' still invalid; using '$clean'" );
141 $title = Title::newFromText( $clean );
142 } elseif( $title->exists() ) {
143 $clean = 'Broken/id:' . $row->page_id;
144 $this->log( "Legalized for '$legalized' exists; using '$clean'" );
145 $title = Title::newFromText( $clean );
146 }
147
148 $dest = $title->getDbKey();
149 if( $this->dryrun ) {
150 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
151 } else {
152 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
153 $dbw =& wfGetDB( DB_MASTER );
154 $dbw->update( 'page',
155 array( 'page_title' => $dest ),
156 array( 'page_id' => $row->page_id ),
157 'cleanupTitles::moveInconsistentPage' );
158 }
159 }
160
161 function moveInconsistentPage( $row, $title ) {
162 if( $title->exists() || $title->getInterwiki() ) {
163 if( $title->getInterwiki() ) {
164 $prior = $title->getPrefixedDbKey();
165 } else {
166 $prior = $title->getDbKey();
167 }
168 $clean = 'Broken/' . $prior;
169 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
170 if( $verified->exists() ) {
171 $blah = "Broken/id:" . $row->page_id;
172 $this->log( "Couldn't legalize; form '$clean' exists; using '$blah'" );
173 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
174 }
175 $title = $verified;
176 }
177 if( is_null( $title ) ) {
178 die( "Something awry; empty title.\n" );
179 }
180 $dest = $title->getDbKey();
181 if( $this->dryrun ) {
182 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
183 } else {
184 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
185 $dbw =& wfGetDB( DB_MASTER );
186 $dbw->update( 'page',
187 array( 'page_title' => $dest ),
188 array( 'page_id' => $row->page_id ),
189 'cleanupTitles::moveInconsistentPage' );
190 }
191 }
192
193 function hexChar( $matches ) {
194 return sprintf( "\\x%02x", ord( $matches[1] ) );
195 }
196 }
197
198 $wgUser->setName( 'Conversion script' );
199 $caps = new TitleCleanup( isset( $options['dry-run'] ) );
200 $caps->cleanup();
201
202 ?>