PrefixSearch: Avoid notice when no subpage exists
[mediawiki.git] / maintenance / cleanupTitles.php
blob72b6aa59a006ddac7c2391915ce48d46f3a9178f
1 <?php
2 /**
3 * Clean up broken, unparseable titles.
5 * Usage: php cleanupTitles.php [--fix]
6 * Options:
7 * --fix Actually clean up titles; otherwise just checks for them
9 * Copyright © 2005 Brion Vibber <brion@pobox.com>
10 * https://www.mediawiki.org/
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.
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.
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
27 * @file
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 require_once __DIR__ . '/cleanupTable.inc';
34 /**
35 * Maintenance script to clean up broken, unparseable titles.
37 * @ingroup Maintenance
39 class TitleCleanup extends TableCleanup {
40 public function __construct() {
41 parent::__construct();
42 $this->mDescription = "Script to clean up broken, unparseable titles";
45 /**
46 * @param object $row
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 );
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
61 return;
64 if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
65 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
66 $this->progress( 0 );
67 } elseif ( is_null( $title ) ) {
68 $this->output( "page $row->page_id ($display) is illegal.\n" );
69 $this->moveIllegalPage( $row );
70 $this->progress( 1 );
71 } else {
72 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
73 $this->moveInconsistentPage( $row, $title );
74 $this->progress( 1 );
78 /**
79 * @param string $name
80 * @return bool
82 protected function fileExists( $name ) {
83 // XXX: Doesn't actually check for file existence, just presence of image record.
84 // This is reasonable, since cleanupImages.php only iterates over the image table.
85 $dbr = wfGetDB( DB_SLAVE );
86 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
88 return $row !== false;
91 /**
92 * @param object $row
94 protected function moveIllegalPage( $row ) {
95 $legal = 'A-Za-z0-9_/\\\\-';
96 $legalized = preg_replace_callback( "!([^$legal])!",
97 array( &$this, 'hexChar' ),
98 $row->page_title );
99 if ( $legalized == '.' ) {
100 $legalized = '(dot)';
102 if ( $legalized == '_' ) {
103 $legalized = '(space)';
105 $legalized = 'Broken/' . $legalized;
107 $title = Title::newFromText( $legalized );
108 if ( is_null( $title ) ) {
109 $clean = 'Broken/id:' . $row->page_id;
110 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
111 $title = Title::newFromText( $clean );
112 } elseif ( $title->exists() ) {
113 $clean = 'Broken/id:' . $row->page_id;
114 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
115 $title = Title::newFromText( $clean );
118 $dest = $title->getDBkey();
119 if ( $this->dryrun ) {
120 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
121 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
122 } else {
123 $this->output( "renaming $row->page_id ($row->page_namespace," .
124 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
125 $dbw = wfGetDB( DB_MASTER );
126 $dbw->update( 'page',
127 array( 'page_title' => $dest ),
128 array( 'page_id' => $row->page_id ),
129 __METHOD__ );
134 * @param object $row
135 * @param Title $title
137 protected function moveInconsistentPage( $row, $title ) {
138 if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
139 if ( $title->getInterwiki() || !$title->canExist() ) {
140 $prior = $title->getPrefixedDBkey();
141 } else {
142 $prior = $title->getDBkey();
145 # Old cleanupTitles could move articles there. See bug 23147.
146 $ns = $row->page_namespace;
147 if ( $ns < 0 ) {
148 $ns = 0;
151 $clean = 'Broken/' . $prior;
152 $verified = Title::makeTitleSafe( $ns, $clean );
153 if ( $verified->exists() ) {
154 $blah = "Broken/id:" . $row->page_id;
155 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
156 $verified = Title::makeTitleSafe( $ns, $blah );
158 $title = $verified;
160 if ( is_null( $title ) ) {
161 $this->error( "Something awry; empty title.", true );
163 $ns = $title->getNamespace();
164 $dest = $title->getDBkey();
166 if ( $this->dryrun ) {
167 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
168 "'$row->page_title') to ($ns,'$dest')\n" );
169 } else {
170 $this->output( "renaming $row->page_id ($row->page_namespace," .
171 "'$row->page_title') to ($ns,'$dest')\n" );
172 $dbw = wfGetDB( DB_MASTER );
173 $dbw->update( 'page',
174 array(
175 'page_namespace' => $ns,
176 'page_title' => $dest
178 array( 'page_id' => $row->page_id ),
179 __METHOD__ );
180 LinkCache::singleton()->clear();
185 $maintClass = "TitleCleanup";
186 require_once RUN_MAINTENANCE_IF_MAIN;