For the maintenance/ directory files:
[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 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
42 $display = $current->getPrefixedText();
43
44 $verified = UtfNormal::cleanUp( $display );
45
46 $title = Title::newFromText( $verified );
47
48 if( is_null( $title ) ) {
49 $this->log( "page $row->page_id ($display) is illegal." );
50 $this->moveIllegalPage( $row );
51 return $this->progress( 1 );
52 }
53
54 if( !$title->equals( $current ) ) {
55 $this->log( "page $row->page_id ($display) doesn't match self." );
56 $this->moveInconsistentPage( $row, $title );
57 return $this->progress( 1 );
58 }
59
60 $this->progress( 0 );
61 }
62
63 function moveIllegalPage( $row ) {
64 $legal = 'A-Za-z0-9_/\\\\-';
65 $legalized = preg_replace_callback( "!([^$legal])!",
66 array( &$this, 'hexChar' ),
67 $row->page_title );
68 if( $legalized == '.' ) $legalized = '(dot)';
69 if( $legalized == '_' ) $legalized = '(space)';
70 $legalized = 'Broken/' . $legalized;
71
72 $title = Title::newFromText( $legalized );
73 if( is_null( $title ) ) {
74 $clean = 'Broken/id:' . $row->page_id;
75 $this->log( "Couldn't legalize; form '$legalized' still invalid; using '$clean'" );
76 $title = Title::newFromText( $clean );
77 } elseif( $title->exists() ) {
78 $clean = 'Broken/id:' . $row->page_id;
79 $this->log( "Legalized for '$legalized' exists; using '$clean'" );
80 $title = Title::newFromText( $clean );
81 }
82
83 $dest = $title->getDbKey();
84 if( $this->dryrun ) {
85 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
86 } else {
87 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
88 $dbw =& wfGetDB( DB_MASTER );
89 $dbw->update( 'page',
90 array( 'page_title' => $dest ),
91 array( 'page_id' => $row->page_id ),
92 'cleanupTitles::moveInconsistentPage' );
93 }
94 }
95
96 function moveInconsistentPage( $row, $title ) {
97 if( $title->exists() || $title->getInterwiki() ) {
98 if( $title->getInterwiki() ) {
99 $prior = $title->getPrefixedDbKey();
100 } else {
101 $prior = $title->getDbKey();
102 }
103 $clean = 'Broken/' . $prior;
104 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
105 if( $verified->exists() ) {
106 $blah = "Broken/id:" . $row->page_id;
107 $this->log( "Couldn't legalize; form '$clean' exists; using '$blah'" );
108 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
109 }
110 $title = $verified;
111 }
112 if( is_null( $title ) ) {
113 wfDie( "Something awry; empty title.\n" );
114 }
115 $ns = $title->getNamespace();
116 $dest = $title->getDbKey();
117 if( $this->dryrun ) {
118 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
119 } else {
120 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')" );
121 $dbw =& wfGetDB( DB_MASTER );
122 $dbw->update( 'page',
123 array(
124 'page_namespace' => $ns,
125 'page_title' => $dest
126 ),
127 array( 'page_id' => $row->page_id ),
128 'cleanupTitles::moveInconsistentPage' );
129 $linkCache =& LinkCache::singleton();
130 $linkCache->clear();
131 }
132 }
133 }
134
135 $wgUser->setName( 'Conversion script' );
136 $caps = new TitleCleanup( !isset( $options['fix'] ) );
137 $caps->cleanup();
138
139 ?>