Merge "Fix revision table cleanup on delete"
[lhc/web/wiklou.git] / maintenance / cleanupTitles.php
1 <?php
2 /**
3 * 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 © 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 __DIR__ . '/cleanupTable.inc';
33
34 /**
35 * Maintenance script to clean up broken, unparseable titles.
36 *
37 * @ingroup Maintenance
38 */
39 class TitleCleanup extends TableCleanup {
40 public function __construct() {
41 parent::__construct();
42 $this->mDescription = "Script to clean up broken, unparseable titles";
43 }
44
45 /**
46 * @param object $row
47 */
48 protected function processRow( $row ) {
49 global $wgContLang;
50 $display = Title::makeName( $row->page_namespace, $row->page_title );
51 $verified = $wgContLang->normalize( $display );
52 $title = Title::newFromText( $verified );
53
54 if ( !is_null( $title )
55 && $title->canExist()
56 && $title->getNamespace() == $row->page_namespace
57 && $title->getDBkey() === $row->page_title )
58 {
59 $this->progress( 0 ); // all is fine
60 return;
61 }
62
63 if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
64 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
65 $this->progress( 0 );
66 } elseif ( is_null( $title ) ) {
67 $this->output( "page $row->page_id ($display) is illegal.\n" );
68 $this->moveIllegalPage( $row );
69 $this->progress( 1 );
70 } else {
71 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
72 $this->moveInconsistentPage( $row, $title );
73 $this->progress( 1 );
74 }
75 }
76
77 /**
78 * @param string $name
79 * @return bool
80 */
81 protected function fileExists( $name ) {
82 // XXX: Doesn't actually check for file existence, just presence of image record.
83 // This is reasonable, since cleanupImages.php only iterates over the image table.
84 $dbr = wfGetDB( DB_SLAVE );
85 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
86 return $row !== false;
87 }
88
89 /**
90 * @param object $row
91 */
92 protected function moveIllegalPage( $row ) {
93 $legal = 'A-Za-z0-9_/\\\\-';
94 $legalized = preg_replace_callback( "!([^$legal])!",
95 array( &$this, 'hexChar' ),
96 $row->page_title );
97 if ( $legalized == '.' ) {
98 $legalized = '(dot)';
99 }
100 if ( $legalized == '_' ) {
101 $legalized = '(space)';
102 }
103 $legalized = 'Broken/' . $legalized;
104
105 $title = Title::newFromText( $legalized );
106 if ( is_null( $title ) ) {
107 $clean = 'Broken/id:' . $row->page_id;
108 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
109 $title = Title::newFromText( $clean );
110 } elseif ( $title->exists() ) {
111 $clean = 'Broken/id:' . $row->page_id;
112 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
113 $title = Title::newFromText( $clean );
114 }
115
116 $dest = $title->getDBkey();
117 if ( $this->dryrun ) {
118 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
119 } else {
120 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
121 $dbw = wfGetDB( DB_MASTER );
122 $dbw->update( 'page',
123 array( 'page_title' => $dest ),
124 array( 'page_id' => $row->page_id ),
125 __METHOD__ );
126 }
127 }
128
129 /**
130 * @param object $row
131 * @param Title $title
132 */
133 protected function moveInconsistentPage( $row, $title ) {
134 if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
135 if ( $title->getInterwiki() || !$title->canExist() ) {
136 $prior = $title->getPrefixedDBkey();
137 } else {
138 $prior = $title->getDBkey();
139 }
140
141 # Old cleanupTitles could move articles there. See bug 23147.
142 $ns = $row->page_namespace;
143 if ( $ns < 0 ) {
144 $ns = 0;
145 }
146
147 $clean = 'Broken/' . $prior;
148 $verified = Title::makeTitleSafe( $ns, $clean );
149 if ( $verified->exists() ) {
150 $blah = "Broken/id:" . $row->page_id;
151 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
152 $verified = Title::makeTitleSafe( $ns, $blah );
153 }
154 $title = $verified;
155 }
156 if ( is_null( $title ) ) {
157 $this->error( "Something awry; empty title.", true );
158 }
159 $ns = $title->getNamespace();
160 $dest = $title->getDBkey();
161
162 if ( $this->dryrun ) {
163 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
164 } else {
165 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
166 $dbw = wfGetDB( DB_MASTER );
167 $dbw->update( 'page',
168 array(
169 'page_namespace' => $ns,
170 'page_title' => $dest
171 ),
172 array( 'page_id' => $row->page_id ),
173 __METHOD__ );
174 LinkCache::singleton()->clear();
175 }
176 }
177 }
178
179 $maintClass = "TitleCleanup";
180 require_once RUN_MAINTENANCE_IF_MAIN;