3 * Clean up broken, unparseable titles.
5 * Usage: php cleanupTitles.php [--fix]
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
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 require_once __DIR__
. '/cleanupTable.inc';
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 protected function processRow( $row ) {
47 $display = Title
::makeName( $row->page_namespace
, $row->page_title
);
48 $verified = $wgContLang->normalize( $display );
49 $title = Title
::newFromText( $verified );
51 if ( !is_null( $title )
53 && $title->getNamespace() == $row->page_namespace
54 && $title->getDBkey() === $row->page_title
)
56 return $this->progress( 0 ); // all is fine
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 );
67 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
68 $this->moveInconsistentPage( $row, $title );
69 return $this->progress( 1 );
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;
81 protected function moveIllegalPage( $row ) {
82 $legal = 'A-Za-z0-9_/\\\\-';
83 $legalized = preg_replace_callback( "!([^$legal])!",
84 array( &$this, 'hexChar' ),
86 if ( $legalized == '.' ) {
89 if ( $legalized == '_' ) {
90 $legalized = '(space)';
92 $legalized = 'Broken/' . $legalized;
94 $title = Title
::newFromText( $legalized );
95 if ( is_null( $title ) ) {
96 $clean = 'Broken/id:' . $row->page_id
;
97 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
98 $title = Title
::newFromText( $clean );
99 } elseif ( $title->exists() ) {
100 $clean = 'Broken/id:' . $row->page_id
;
101 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
102 $title = Title
::newFromText( $clean );
105 $dest = $title->getDBkey();
106 if ( $this->dryrun
) {
107 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
109 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
110 $dbw = wfGetDB( DB_MASTER
);
111 $dbw->update( 'page',
112 array( 'page_title' => $dest ),
113 array( 'page_id' => $row->page_id
),
118 protected function moveInconsistentPage( $row, $title ) {
119 if ( $title->exists() ||
$title->getInterwiki() ||
!$title->canExist() ) {
120 if ( $title->getInterwiki() ||
!$title->canExist() ) {
121 $prior = $title->getPrefixedDBkey();
123 $prior = $title->getDBkey();
126 # Old cleanupTitles could move articles there. See bug 23147.
127 $ns = $row->page_namespace
;
132 $clean = 'Broken/' . $prior;
133 $verified = Title
::makeTitleSafe( $ns, $clean );
134 if ( $verified->exists() ) {
135 $blah = "Broken/id:" . $row->page_id
;
136 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
137 $verified = Title
::makeTitleSafe( $ns, $blah );
141 if ( is_null( $title ) ) {
142 $this->error( "Something awry; empty title.", true );
144 $ns = $title->getNamespace();
145 $dest = $title->getDBkey();
147 if ( $this->dryrun
) {
148 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
150 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
151 $dbw = wfGetDB( DB_MASTER
);
152 $dbw->update( 'page',
154 'page_namespace' => $ns,
155 'page_title' => $dest
157 array( 'page_id' => $row->page_id
),
159 LinkCache
::singleton()->clear();
164 $maintClass = "TitleCleanup";
165 require_once RUN_MAINTENANCE_IF_MAIN
;