* Use double quoted consistently on attributes in linker output; preparing for new...
[mediawiki.git] / maintenance / cleanupTitles.php
blob930072de4d1965f2901222a87722a2faa3fe7b56
1 <?php
2 /*
3 * Script to clean up broken, unparseable titles.
5 * Usage: php cleanupTitles.php [--dry-run]
6 * Options:
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
27 * @author Brion Vibber <brion at pobox.com>
28 * @package MediaWiki
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;
45 function cleanup() {
46 $this->runTable( 'page',
47 '', //'WHERE page_namespace=0',
48 array( &$this, 'processPage' ) );
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;
59 function progress( $updated ) {
60 $this->updated += $updated;
61 $this->processed++;
62 if( $this->processed % 100 != 0 ) {
63 return;
65 $portion = $this->processed / $this->count;
66 $updateRate = $this->updated / $this->processed;
68 $now = wfTime();
69 $delta = $now - $this->startTime;
70 $estimatedTotalTime = $delta / $portion;
71 $eta = $this->startTime + $estimatedTotalTime;
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();
87 function runTable( $table, $where, $callback ) {
88 $fname = 'CapsCleanup::buildTable';
90 $count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
91 $this->init( $count, 'page' );
92 $this->log( "Processing $table..." );
94 $tableName = $this->dbr->tableName( $table );
95 $sql = "SELECT * FROM $tableName $where";
96 $result = $this->dbr->query( $sql, $fname );
98 while( $row = $this->dbr->fetchObject( $result ) ) {
99 $updated = call_user_func( $callback, $row );
101 $this->log( "Finished $table... $this->updated of $this->processed rows updated" );
102 $this->dbr->freeResult( $result );
105 function processPage( $row ) {
106 global $wgContLang;
108 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
109 $display = $current->getPrefixedText();
111 $verified = UtfNormal::cleanUp( $display );
113 $title = Title::newFromText( $verified );
115 if( is_null( $title ) ) {
116 $this->log( "page $row->page_id ($display) is illegal." );
117 $this->moveIllegalPage( $row );
118 return $this->progress( 1 );
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 );
127 $this->progress( 0 );
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;
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 );
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' );
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();
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 );
177 $title = $verified;
179 if( is_null( $title ) ) {
180 wfDie( "Something awry; empty title.\n" );
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
194 array( 'page_id' => $row->page_id ),
195 'cleanupTitles::moveInconsistentPage' );
196 $linkCache =& LinkCache::singleton();
197 $linkCache->clear();
201 function hexChar( $matches ) {
202 return sprintf( "\\x%02x", ord( $matches[1] ) );
206 $wgUser->setName( 'Conversion script' );
207 $caps = new TitleCleanup( isset( $options['dry-run'] ) );
208 $caps->cleanup();