Abstract out some of the cleanupTitles code to reuse it
[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 * @author Brion Vibber <brion at pobox.com>
28 * @package MediaWiki
29 * @subpackage maintenance
30 */
31
32 require_once( 'commandLine.inc' );
33 require_once( 'cleanupTable.inc' );
34
35 class TitleCleanup extends TableCleanup {
36 function __construct( $dryrun = false ) {
37 parent::__construct( 'page', $dryrun );
38 }
39
40 function processPage( $row ) {
41 global $wgContLang;
42
43 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
44 $display = $current->getPrefixedText();
45
46 $verified = UtfNormal::cleanUp( $display );
47
48 $title = Title::newFromText( $verified );
49
50 if( is_null( $title ) ) {
51 $this->log( "page $row->page_id ($display) is illegal." );
52 $this->moveIllegalPage( $row );
53 return $this->progress( 1 );
54 }
55
56 if( !$title->equals( $current ) ) {
57 $this->log( "page $row->page_id ($display) doesn't match self." );
58 $this->moveInconsistentPage( $row, $title );
59 return $this->progress( 1 );
60 }
61
62 $this->progress( 0 );
63 }
64
65 function moveIllegalPage( $row ) {
66 $legal = 'A-Za-z0-9_/\\\\-';
67 $legalized = preg_replace_callback( "!([^$legal])!",
68 array( &$this, 'hexChar' ),
69 $row->page_title );
70 if( $legalized == '.' ) $legalized = '(dot)';
71 if( $legalized == '_' ) $legalized = '(space)';
72 $legalized = 'Broken/' . $legalized;
73
74 $title = Title::newFromText( $legalized );
75 if( is_null( $title ) ) {
76 $clean = 'Broken/id:' . $row->page_id;
77 $this->log( "Couldn't legalize; form '$legalized' still invalid; using '$clean'" );
78 $title = Title::newFromText( $clean );
79 } elseif( $title->exists() ) {
80 $clean = 'Broken/id:' . $row->page_id;
81 $this->log( "Legalized for '$legalized' exists; using '$clean'" );
82 $title = Title::newFromText( $clean );
83 }
84
85 $dest = $title->getDbKey();
86 if( $this->dryrun ) {
87 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
88 } else {
89 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
90 $dbw =& wfGetDB( DB_MASTER );
91 $dbw->update( 'page',
92 array( 'page_title' => $dest ),
93 array( 'page_id' => $row->page_id ),
94 'cleanupTitles::moveInconsistentPage' );
95 }
96 }
97
98 function moveInconsistentPage( $row, $title ) {
99 if( $title->exists() || $title->getInterwiki() ) {
100 if( $title->getInterwiki() ) {
101 $prior = $title->getPrefixedDbKey();
102 } else {
103 $prior = $title->getDbKey();
104 }
105 $clean = 'Broken/' . $prior;
106 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
107 if( $verified->exists() ) {
108 $blah = "Broken/id:" . $row->page_id;
109 $this->log( "Couldn't legalize; form '$clean' exists; using '$blah'" );
110 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
111 }
112 $title = $verified;
113 }
114 if( is_null( $title ) ) {
115 wfDie( "Something awry; empty title.\n" );
116 }
117 $ns = $title->getNamespace();
118 $dest = $title->getDbKey();
119 if( $this->dryrun ) {
120 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
121 } else {
122 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')" );
123 $dbw =& wfGetDB( DB_MASTER );
124 $dbw->update( 'page',
125 array(
126 'page_namespace' => $ns,
127 'page_title' => $dest
128 ),
129 array( 'page_id' => $row->page_id ),
130 'cleanupTitles::moveInconsistentPage' );
131 $linkCache =& LinkCache::singleton();
132 $linkCache->clear();
133 }
134 }
135
136 function hexChar( $matches ) {
137 return sprintf( "\\x%02x", ord( $matches[1] ) );
138 }
139 }
140
141 $wgUser->setName( 'Conversion script' );
142 $caps = new TitleCleanup( !isset( $options['fix'] ) );
143 $caps->cleanup();
144
145 ?>