3 * Script to clean up broken, unparseable titles.
5 * Usage: php cleanupTitles.php [--dry-run]
7 * --dry-run don't actually try moving them
9 * Copyright (C) 2005 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * http://www.gnu.org/copyleft/gpl.html
27 * @author Brion Vibber <brion at pobox.com>
29 * @subpackage maintenance
32 $options = array( 'dry-run' );
34 require_once( 'commandLine.inc' );
35 require_once( 'FiveUpgrade.inc' );
37 class TitleCleanup
extends FiveUpgrade
{
38 function TitleCleanup( $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( 'page', 'WHERE page_namespace=0',
47 array( &$this, 'processPage' ) );
50 function init( $count, $table ) {
53 $this->count
= $count;
54 $this->startTime
= wfTime();
55 $this->table
= $table;
58 function progress( $updated ) {
59 $this->updated +
= $updated;
61 if( $this->processed %
100 != 0 ) {
64 $portion = $this->processed
/ $this->count
;
65 $updateRate = $this->updated
/ $this->processed
;
68 $delta = $now - $this->startTime
;
69 $estimatedTotalTime = $delta / $portion;
70 $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 = 'CapsCleanup::buildTable';
89 $count = $this->dbw
->selectField( $table, 'count(*)', '', $fname );
90 $this->init( $count, 'page' );
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 processPage( $row ) {
107 $current = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
108 $display = $current->getPrefixedText();
110 $verified = UtfNormal
::cleanUp( $display );
111 $title = Title
::newFromText( $verified );
113 if( is_null( $title ) ) {
114 $this->log( "page $row->page_id ($display) is illegal." );
115 $this->moveIllegalPage( $row );
116 return $this->progress( 1 );
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 );
125 $this->progress( 0 );
128 function moveIllegalPage( $row ) {
129 $legal = 'A-Za-z0-9_/\\\\-';
130 $legalized = preg_replace_callback( "!([^$legal])!",
131 array( &$this, 'hexChar' ),
133 if( $legalized == '.' ) $legalized = '(dot)';
134 if( $legalized == '_' ) $legalized = '(space)';
135 $legalized = 'Broken/' . $legalized;
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 );
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')" );
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' );
161 function moveInconsistentPage( $row, $title ) {
162 if( $title->exists() ||
$title->getInterwiki() ) {
163 if( $title->getInterwiki() ) {
164 $prior = $title->getPrefixedDbKey();
166 $prior = $title->getDbKey();
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 );
177 if( is_null( $title ) ) {
178 die( "Something awry; empty title.\n" );
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')" );
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' );
193 function hexChar( $matches ) {
194 return sprintf( "\\x%02x", ord( $matches[1] ) );
198 $wgUser->setName( 'Conversion script' );
199 $caps = new TitleCleanup( isset( $options['dry-run'] ) );