HTMLForm: throw a warning when no callback is set
[mediawiki.git] / maintenance / cleanupTitles.php
blob4fc6415e51e1858ce23e9b6e454bc45deb09a3c6
1 <?php
2 /**
3 * Script to 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 * http://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( dirname( __FILE__ ) . '/cleanupTable.inc' );
34 class TitleCleanup extends TableCleanup {
35 public function __construct() {
36 parent::__construct();
37 $this->mDescription = "Script to clean up broken, unparseable titles";
40 protected function processRow( $row ) {
41 global $wgContLang;
42 $display = Title::makeName( $row->page_namespace, $row->page_title );
43 $verified = $wgContLang->normalize( $display );
44 $title = Title::newFromText( $verified );
46 if ( !is_null( $title )
47 && $title->canExist()
48 && $title->getNamespace() == $row->page_namespace
49 && $title->getDBkey() === $row->page_title )
51 return $this->progress( 0 ); // all is fine
54 if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
55 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
56 return $this->progress( 0 );
57 } elseif ( is_null( $title ) ) {
58 $this->output( "page $row->page_id ($display) is illegal.\n" );
59 $this->moveIllegalPage( $row );
60 return $this->progress( 1 );
61 } else {
62 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
63 $this->moveInconsistentPage( $row, $title );
64 return $this->progress( 1 );
68 protected function fileExists( $name ) {
69 // XXX: Doesn't actually check for file existence, just presence of image record.
70 // This is reasonable, since cleanupImages.php only iterates over the image table.
71 $dbr = wfGetDB( DB_SLAVE );
72 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
73 return $row !== false;
76 protected function moveIllegalPage( $row ) {
77 $legal = 'A-Za-z0-9_/\\\\-';
78 $legalized = preg_replace_callback( "!([^$legal])!",
79 array( &$this, 'hexChar' ),
80 $row->page_title );
81 if ( $legalized == '.' ) $legalized = '(dot)';
82 if ( $legalized == '_' ) $legalized = '(space)';
83 $legalized = 'Broken/' . $legalized;
85 $title = Title::newFromText( $legalized );
86 if ( is_null( $title ) ) {
87 $clean = 'Broken/id:' . $row->page_id;
88 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
89 $title = Title::newFromText( $clean );
90 } elseif ( $title->exists() ) {
91 $clean = 'Broken/id:' . $row->page_id;
92 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
93 $title = Title::newFromText( $clean );
96 $dest = $title->getDBkey();
97 if ( $this->dryrun ) {
98 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
99 } else {
100 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
101 $dbw = wfGetDB( DB_MASTER );
102 $dbw->update( 'page',
103 array( 'page_title' => $dest ),
104 array( 'page_id' => $row->page_id ),
105 __METHOD__ );
109 protected function moveInconsistentPage( $row, $title ) {
110 if ( $title->exists() || $title->getInterwiki() || !$title->canExist() ) {
111 if ( $title->getInterwiki() || !$title->canExist() ) {
112 $prior = $title->getPrefixedDbKey();
113 } else {
114 $prior = $title->getDBkey();
117 # Old cleanupTitles could move articles there. See bug 23147.
118 $ns = $row->page_namespace;
119 if ( $ns < 0 ) $ns = 0;
121 $clean = 'Broken/' . $prior;
122 $verified = Title::makeTitleSafe( $ns, $clean );
123 if ( $verified->exists() ) {
124 $blah = "Broken/id:" . $row->page_id;
125 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
126 $verified = Title::makeTitleSafe( $ns, $blah );
128 $title = $verified;
130 if ( is_null( $title ) ) {
131 $this->error( "Something awry; empty title.", true );
133 $ns = $title->getNamespace();
134 $dest = $title->getDBkey();
136 if ( $this->dryrun ) {
137 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
138 } else {
139 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
140 $dbw = wfGetDB( DB_MASTER );
141 $dbw->update( 'page',
142 array(
143 'page_namespace' => $ns,
144 'page_title' => $dest
146 array( 'page_id' => $row->page_id ),
147 __METHOD__ );
148 LinkCache::singleton()->clear();
153 $maintClass = "TitleCleanup";
154 require_once( RUN_MAINTENANCE_IF_MAIN );