Changing name of Nauruan from “Ekakairũ Naoero” to “Dorerin Naoero”; this was pointed...
[lhc/web/wiklou.git] / maintenance / cleanupTitles.php
1 <?php
2 /*
3 * Script to clean up broken, unparseable titles.
4 *
5 * Usage: php cleanupTitles.php [--fix]
6 * Options:
7 * --fix Actually clean up titles; otherwise just checks for 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
26 *
27 * @file
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
30 */
31
32 require_once( 'commandLine.inc' );
33 require_once( 'cleanupTable.inc' );
34
35 /**
36 * @ingroup Maintenance
37 */
38 class TitleCleanup extends TableCleanup {
39 function __construct( $dryrun = false ) {
40 parent::__construct( 'page', $dryrun );
41 }
42
43 function processPage( $row ) {
44 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
45 $display = $current->getPrefixedText();
46
47 $verified = UtfNormal::cleanUp( $display );
48
49 $title = Title::newFromText( $verified );
50
51 if( is_null( $title ) ) {
52 $this->log( "page $row->page_id ($display) is illegal." );
53 $this->moveIllegalPage( $row );
54 return $this->progress( 1 );
55 }
56
57 if( !$title->equals( $current ) ) {
58 $this->log( "page $row->page_id ($display) doesn't match self." );
59 $this->moveInconsistentPage( $row, $title );
60 return $this->progress( 1 );
61 }
62
63 $this->progress( 0 );
64 }
65
66 function moveIllegalPage( $row ) {
67 $legal = 'A-Za-z0-9_/\\\\-';
68 $legalized = preg_replace_callback( "!([^$legal])!",
69 array( &$this, 'hexChar' ),
70 $row->page_title );
71 if( $legalized == '.' ) $legalized = '(dot)';
72 if( $legalized == '_' ) $legalized = '(space)';
73 $legalized = 'Broken/' . $legalized;
74
75 $title = Title::newFromText( $legalized );
76 if( is_null( $title ) ) {
77 $clean = 'Broken/id:' . $row->page_id;
78 $this->log( "Couldn't legalize; form '$legalized' still invalid; using '$clean'" );
79 $title = Title::newFromText( $clean );
80 } elseif( $title->exists() ) {
81 $clean = 'Broken/id:' . $row->page_id;
82 $this->log( "Legalized for '$legalized' exists; using '$clean'" );
83 $title = Title::newFromText( $clean );
84 }
85
86 $dest = $title->getDBkey();
87 if( $this->dryrun ) {
88 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
89 } else {
90 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
91 $dbw = wfGetDB( DB_MASTER );
92 $dbw->update( 'page',
93 array( 'page_title' => $dest ),
94 array( 'page_id' => $row->page_id ),
95 'cleanupTitles::moveInconsistentPage' );
96 }
97 }
98
99 function moveInconsistentPage( $row, $title ) {
100 if( $title->exists() || $title->getInterwiki() ) {
101 if( $title->getInterwiki() ) {
102 $prior = $title->getPrefixedDbKey();
103 } else {
104 $prior = $title->getDBkey();
105 }
106 $clean = 'Broken/' . $prior;
107 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
108 if( $verified->exists() ) {
109 $blah = "Broken/id:" . $row->page_id;
110 $this->log( "Couldn't legalize; form '$clean' exists; using '$blah'" );
111 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
112 }
113 $title = $verified;
114 }
115 if( is_null( $title ) ) {
116 wfDie( "Something awry; empty title.\n" );
117 }
118 $ns = $title->getNamespace();
119 $dest = $title->getDBkey();
120 if( $this->dryrun ) {
121 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
122 } else {
123 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')" );
124 $dbw = wfGetDB( DB_MASTER );
125 $dbw->update( 'page',
126 array(
127 'page_namespace' => $ns,
128 'page_title' => $dest
129 ),
130 array( 'page_id' => $row->page_id ),
131 'cleanupTitles::moveInconsistentPage' );
132 $linkCache = LinkCache::singleton();
133 $linkCache->clear();
134 }
135 }
136 }
137
138 $wgUser->setName( 'Conversion script' );
139 $caps = new TitleCleanup( !isset( $options['fix'] ) );
140 $caps->cleanup();
141
142