Followup to r44641: Only skip file pages that actually have a corresponding file.
[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 ) && $title->equals( $current ) ) {
52 return $this->progress( 0 ); // all is fine
53 }
54
55 if( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
56 $this->log( "file $row->page_title needs cleanup, please run cleanupImages.php." );
57 return $this->progress( 0 );
58 } elseif( is_null( $title ) ) {
59 $this->log( "page $row->page_id ($display) is illegal." );
60 $this->moveIllegalPage( $row );
61 return $this->progress( 1 );
62 } else {
63 $this->log( "page $row->page_id ($display) doesn't match self." );
64 $this->moveInconsistentPage( $row, $title );
65 return $this->progress( 1 );
66 }
67 }
68
69 function fileExists( $name ) {
70 // XXX: Doesn't actually check for file existence, just presence of image record.
71 // This is reasonable, since cleanupImages.php only iterates over the image table.
72 $dbr = wfGetDB( DB_SLAVE );
73 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
74 return $row !== false;
75 }
76
77 function moveIllegalPage( $row ) {
78 $legal = 'A-Za-z0-9_/\\\\-';
79 $legalized = preg_replace_callback( "!([^$legal])!",
80 array( &$this, 'hexChar' ),
81 $row->page_title );
82 if( $legalized == '.' ) $legalized = '(dot)';
83 if( $legalized == '_' ) $legalized = '(space)';
84 $legalized = 'Broken/' . $legalized;
85
86 $title = Title::newFromText( $legalized );
87 if( is_null( $title ) ) {
88 $clean = 'Broken/id:' . $row->page_id;
89 $this->log( "Couldn't legalize; form '$legalized' still invalid; using '$clean'" );
90 $title = Title::newFromText( $clean );
91 } elseif( $title->exists() ) {
92 $clean = 'Broken/id:' . $row->page_id;
93 $this->log( "Legalized for '$legalized' exists; using '$clean'" );
94 $title = Title::newFromText( $clean );
95 }
96
97 $dest = $title->getDBkey();
98 if( $this->dryrun ) {
99 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
100 } else {
101 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
102 $dbw = wfGetDB( DB_MASTER );
103 $dbw->update( 'page',
104 array( 'page_title' => $dest ),
105 array( 'page_id' => $row->page_id ),
106 'cleanupTitles::moveInconsistentPage' );
107 }
108 }
109
110 function moveInconsistentPage( $row, $title ) {
111 if( $title->exists() || $title->getInterwiki() ) {
112 if( $title->getInterwiki() ) {
113 $prior = $title->getPrefixedDbKey();
114 } else {
115 $prior = $title->getDBkey();
116 }
117 $clean = 'Broken/' . $prior;
118 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
119 if( $verified->exists() ) {
120 $blah = "Broken/id:" . $row->page_id;
121 $this->log( "Couldn't legalize; form '$clean' exists; using '$blah'" );
122 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
123 }
124 $title = $verified;
125 }
126 if( is_null( $title ) ) {
127 wfDie( "Something awry; empty title.\n" );
128 }
129 $ns = $title->getNamespace();
130 $dest = $title->getDBkey();
131 if( $this->dryrun ) {
132 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
133 } else {
134 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')" );
135 $dbw = wfGetDB( DB_MASTER );
136 $dbw->update( 'page',
137 array(
138 'page_namespace' => $ns,
139 'page_title' => $dest
140 ),
141 array( 'page_id' => $row->page_id ),
142 'cleanupTitles::moveInconsistentPage' );
143 $linkCache = LinkCache::singleton();
144 $linkCache->clear();
145 }
146 }
147 }
148
149 $wgUser->setName( 'Conversion script' );
150 $caps = new TitleCleanup( !isset( $options['fix'] ) );
151 $caps->cleanup();
152
153