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