(bug 40829) Show cascading protection info on action=info
[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 protected function processRow( $row ) {
46 global $wgContLang;
47 $display = Title::makeName( $row->page_namespace, $row->page_title );
48 $verified = $wgContLang->normalize( $display );
49 $title = Title::newFromText( $verified );
50
51 if ( !is_null( $title )
52 && $title->canExist()
53 && $title->getNamespace() == $row->page_namespace
54 && $title->getDBkey() === $row->page_title )
55 {
56 return $this->progress( 0 ); // all is fine
57 }
58
59 if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
60 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
61 return $this->progress( 0 );
62 } elseif ( is_null( $title ) ) {
63 $this->output( "page $row->page_id ($display) is illegal.\n" );
64 $this->moveIllegalPage( $row );
65 return $this->progress( 1 );
66 } else {
67 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
68 $this->moveInconsistentPage( $row, $title );
69 return $this->progress( 1 );
70 }
71 }
72
73 protected function fileExists( $name ) {
74 // XXX: Doesn't actually check for file existence, just presence of image record.
75 // This is reasonable, since cleanupImages.php only iterates over the image table.
76 $dbr = wfGetDB( DB_SLAVE );
77 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
78 return $row !== false;
79 }
80
81 protected function moveIllegalPage( $row ) {
82 $legal = 'A-Za-z0-9_/\\\\-';
83 $legalized = preg_replace_callback( "!([^$legal])!",
84 array( &$this, 'hexChar' ),
85 $row->page_title );
86 if ( $legalized == '.' ) $legalized = '(dot)';
87 if ( $legalized == '_' ) $legalized = '(space)';
88 $legalized = 'Broken/' . $legalized;
89
90 $title = Title::newFromText( $legalized );
91 if ( is_null( $title ) ) {
92 $clean = 'Broken/id:' . $row->page_id;
93 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
94 $title = Title::newFromText( $clean );
95 } elseif ( $title->exists() ) {
96 $clean = 'Broken/id:' . $row->page_id;
97 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
98 $title = Title::newFromText( $clean );
99 }
100
101 $dest = $title->getDBkey();
102 if ( $this->dryrun ) {
103 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
104 } else {
105 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
106 $dbw = wfGetDB( DB_MASTER );
107 $dbw->update( 'page',
108 array( 'page_title' => $dest ),
109 array( 'page_id' => $row->page_id ),
110 __METHOD__ );
111 }
112 }
113
114 protected function moveInconsistentPage( $row, $title ) {
115 if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
116 if ( $title->getInterwiki() || !$title->canExist() ) {
117 $prior = $title->getPrefixedDbKey();
118 } else {
119 $prior = $title->getDBkey();
120 }
121
122 # Old cleanupTitles could move articles there. See bug 23147.
123 $ns = $row->page_namespace;
124 if ( $ns < 0 ) $ns = 0;
125
126 $clean = 'Broken/' . $prior;
127 $verified = Title::makeTitleSafe( $ns, $clean );
128 if ( $verified->exists() ) {
129 $blah = "Broken/id:" . $row->page_id;
130 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
131 $verified = Title::makeTitleSafe( $ns, $blah );
132 }
133 $title = $verified;
134 }
135 if ( is_null( $title ) ) {
136 $this->error( "Something awry; empty title.", true );
137 }
138 $ns = $title->getNamespace();
139 $dest = $title->getDBkey();
140
141 if ( $this->dryrun ) {
142 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
143 } else {
144 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
145 $dbw = wfGetDB( DB_MASTER );
146 $dbw->update( 'page',
147 array(
148 'page_namespace' => $ns,
149 'page_title' => $dest
150 ),
151 array( 'page_id' => $row->page_id ),
152 __METHOD__ );
153 LinkCache::singleton()->clear();
154 }
155 }
156 }
157
158 $maintClass = "TitleCleanup";
159 require_once( RUN_MAINTENANCE_IF_MAIN );