* s~\t+$~~
[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',
47 '', //'WHERE page_namespace=0',
48 array( &$this, 'processPage' ) );
49 }
50
51 function init( $count, $table ) {
52 $this->processed = 0;
53 $this->updated = 0;
54 $this->count = $count;
55 $this->startTime = wfTime();
56 $this->table = $table;
57 }
58
59 function progress( $updated ) {
60 $this->updated += $updated;
61 $this->processed++;
62 if( $this->processed % 100 != 0 ) {
63 return;
64 }
65 $portion = $this->processed / $this->count;
66 $updateRate = $this->updated / $this->processed;
67
68 $now = wfTime();
69 $delta = $now - $this->startTime;
70 $estimatedTotalTime = $delta / $portion;
71 $eta = $this->startTime + $estimatedTotalTime;
72
73 global $wgDBname;
74 printf( "%s %s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
75 $wgDBname,
76 wfTimestamp( TS_DB, intval( $now ) ),
77 $portion * 100.0,
78 $this->table,
79 wfTimestamp( TS_DB, intval( $eta ) ),
80 $this->processed,
81 $this->count,
82 $this->processed / $delta,
83 $updateRate * 100.0 );
84 flush();
85 }
86
87 function runTable( $table, $where, $callback ) {
88 $fname = 'CapsCleanup::buildTable';
89
90 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
91 $this->init( $count, 'page' );
92 $this->log( "Processing $table..." );
93
94 $tableName = $this->dbr->tableName( $table );
95 $sql = "SELECT * FROM $tableName $where";
96 $result = $this->dbr->query( $sql, $fname );
97
98 while( $row = $this->dbr->fetchObject( $result ) ) {
99 $updated = call_user_func( $callback, $row );
100 }
101 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
102 $this->dbr->freeResult( $result );
103 }
104
105 function processPage( $row ) {
106 global $wgContLang;
107
108 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
109 $display = $current->getPrefixedText();
110
111 $verified = UtfNormal::cleanUp( $display );
112
113 $title = Title::newFromText( $verified );
114
115 if( is_null( $title ) ) {
116 $this->log( "page $row->page_id ($display) is illegal." );
117 $this->moveIllegalPage( $row );
118 return $this->progress( 1 );
119 }
120
121 if( !$title->equals( $current ) ) {
122 $this->log( "page $row->page_id ($display) doesn't match self." );
123 $this->moveInconsistentPage( $row, $title );
124 return $this->progress( 1 );
125 }
126
127 $this->progress( 0 );
128 }
129
130 function moveIllegalPage( $row ) {
131 $legal = 'A-Za-z0-9_/\\\\-';
132 $legalized = preg_replace_callback( "!([^$legal])!",
133 array( &$this, 'hexChar' ),
134 $row->page_title );
135 if( $legalized == '.' ) $legalized = '(dot)';
136 if( $legalized == '_' ) $legalized = '(space)';
137 $legalized = 'Broken/' . $legalized;
138
139 $title = Title::newFromText( $legalized );
140 if( is_null( $title ) ) {
141 $clean = 'Broken/id:' . $row->page_id;
142 $this->log( "Couldn't legalize; form '$legalized' still invalid; using '$clean'" );
143 $title = Title::newFromText( $clean );
144 } elseif( $title->exists() ) {
145 $clean = 'Broken/id:' . $row->page_id;
146 $this->log( "Legalized for '$legalized' exists; using '$clean'" );
147 $title = Title::newFromText( $clean );
148 }
149
150 $dest = $title->getDbKey();
151 if( $this->dryrun ) {
152 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
153 } else {
154 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
155 $dbw =& wfGetDB( DB_MASTER );
156 $dbw->update( 'page',
157 array( 'page_title' => $dest ),
158 array( 'page_id' => $row->page_id ),
159 'cleanupTitles::moveInconsistentPage' );
160 }
161 }
162
163 function moveInconsistentPage( $row, $title ) {
164 if( $title->exists() || $title->getInterwiki() ) {
165 if( $title->getInterwiki() ) {
166 $prior = $title->getPrefixedDbKey();
167 } else {
168 $prior = $title->getDbKey();
169 }
170 $clean = 'Broken/' . $prior;
171 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
172 if( $verified->exists() ) {
173 $blah = "Broken/id:" . $row->page_id;
174 $this->log( "Couldn't legalize; form '$clean' exists; using '$blah'" );
175 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
176 }
177 $title = $verified;
178 }
179 if( is_null( $title ) ) {
180 die( "Something awry; empty title.\n" );
181 }
182 $ns = $title->getNamespace();
183 $dest = $title->getDbKey();
184 if( $this->dryrun ) {
185 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
186 } else {
187 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')" );
188 $dbw =& wfGetDB( DB_MASTER );
189 $dbw->update( 'page',
190 array(
191 'page_namespace' => $ns,
192 'page_title' => $dest
193 ),
194 array( 'page_id' => $row->page_id ),
195 'cleanupTitles::moveInconsistentPage' );
196 $linkCache =& LinkCache::singleton();
197 $linkCache->clear();
198 }
199 }
200
201 function hexChar( $matches ) {
202 return sprintf( "\\x%02x", ord( $matches[1] ) );
203 }
204 }
205
206 $wgUser->setName( 'Conversion script' );
207 $caps = new TitleCleanup( isset( $options['dry-run'] ) );
208 $caps->cleanup();
209
210 ?>