Followup to r59869, add to MySQL section, and copy patch to SQLite directory
[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 * @ingroup Maintenance
29 */
30
31 require_once( dirname(__FILE__) . '/cleanupTable.inc' );
32
33 class TitleCleanup extends TableCleanup {
34 public function __construct() {
35 parent::__construct();
36 $this->mDescription = "Script to clean up broken, unparseable titles";
37 }
38
39 protected function processRow( $row ) {
40 $display = Title::makeName( $row->page_namespace, $row->page_title );
41 $verified = UtfNormal::cleanUp( $display );
42 $title = Title::newFromText( $verified );
43
44 if( !is_null( $title )
45 && $title->canExist()
46 && $title->getNamespace() == $row->page_namespace
47 && $title->getDBkey() === $row->page_title )
48 {
49 return $this->progress( 0 ); // all is fine
50 }
51
52 if( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
53 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
54 return $this->progress( 0 );
55 } elseif( is_null( $title ) ) {
56 $this->output( "page $row->page_id ($display) is illegal.\n" );
57 $this->moveIllegalPage( $row );
58 return $this->progress( 1 );
59 } else {
60 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
61 $this->moveInconsistentPage( $row, $title );
62 return $this->progress( 1 );
63 }
64 }
65
66 protected function fileExists( $name ) {
67 // XXX: Doesn't actually check for file existence, just presence of image record.
68 // This is reasonable, since cleanupImages.php only iterates over the image table.
69 $dbr = wfGetDB( DB_SLAVE );
70 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
71 return $row !== false;
72 }
73
74 protected function moveIllegalPage( $row ) {
75 $legal = 'A-Za-z0-9_/\\\\-';
76 $legalized = preg_replace_callback( "!([^$legal])!",
77 array( &$this, 'hexChar' ),
78 $row->page_title );
79 if( $legalized == '.' ) $legalized = '(dot)';
80 if( $legalized == '_' ) $legalized = '(space)';
81 $legalized = 'Broken/' . $legalized;
82
83 $title = Title::newFromText( $legalized );
84 if( is_null( $title ) ) {
85 $clean = 'Broken/id:' . $row->page_id;
86 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
87 $title = Title::newFromText( $clean );
88 } elseif( $title->exists() ) {
89 $clean = 'Broken/id:' . $row->page_id;
90 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
91 $title = Title::newFromText( $clean );
92 }
93
94 $dest = $title->getDBkey();
95 if( $this->dryrun ) {
96 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
97 } else {
98 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
99 $dbw = wfGetDB( DB_MASTER );
100 $dbw->update( 'page',
101 array( 'page_title' => $dest ),
102 array( 'page_id' => $row->page_id ),
103 __METHOD__ );
104 }
105 }
106
107 protected function moveInconsistentPage( $row, $title ) {
108 if( $title->exists() || $title->getInterwiki() ) {
109 if( $title->getInterwiki() ) {
110 $prior = $title->getPrefixedDbKey();
111 } else {
112 $prior = $title->getDBkey();
113 }
114 $clean = 'Broken/' . $prior;
115 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
116 if( $verified->exists() ) {
117 $blah = "Broken/id:" . $row->page_id;
118 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
119 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
120 }
121 $title = $verified;
122 }
123 if( is_null( $title ) ) {
124 $this->error( "Something awry; empty title.", true );
125 }
126 $ns = $title->getNamespace();
127 $dest = $title->getDBkey();
128 if( $this->dryrun ) {
129 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
130 } else {
131 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
132 $dbw = wfGetDB( DB_MASTER );
133 $dbw->update( 'page',
134 array(
135 'page_namespace' => $ns,
136 'page_title' => $dest
137 ),
138 array( 'page_id' => $row->page_id ),
139 __METHOD__ );
140 $linkCache = LinkCache::singleton();
141 $linkCache->clear();
142 }
143 }
144 }
145
146 $maintClass = "TitleCleanup";
147 require_once( DO_MAINTENANCE );