Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / maintenance / cleanupTitles.php
blob4d76ac7ac7041a31bf9518a9a18ac0489f59033e
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 (C) 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( 'commandLine.inc' );
33 require_once( 'cleanupTable.inc' );
35 /**
36 * @ingroup Maintenance
38 class TitleCleanup extends TableCleanup {
39 function __construct( $dryrun = false ) {
40 parent::__construct( 'page', $dryrun );
43 function processPage( $row ) {
44 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
45 $display = $current->getPrefixedText();
47 $verified = UtfNormal::cleanUp( $display );
49 $title = Title::newFromText( $verified );
51 if( !is_null( $title ) && $title->equals( $current ) && $title->canExist() ) {
52 return $this->progress( 0 ); // all is fine
55 if( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
56 $this->log( "file $row->page_title needs cleanup, please run cleanupImages.php." );
57 return $this->progress( 0 );
58 } elseif( is_null( $title ) ) {
59 $this->log( "page $row->page_id ($display) is illegal." );
60 $this->moveIllegalPage( $row );
61 return $this->progress( 1 );
62 } else {
63 $this->log( "page $row->page_id ($display) doesn't match self." );
64 $this->moveInconsistentPage( $row, $title );
65 return $this->progress( 1 );
69 function fileExists( $name ) {
70 // XXX: Doesn't actually check for file existence, just presence of image record.
71 // This is reasonable, since cleanupImages.php only iterates over the image table.
72 $dbr = wfGetDB( DB_SLAVE );
73 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
74 return $row !== false;
77 function moveIllegalPage( $row ) {
78 $legal = 'A-Za-z0-9_/\\\\-';
79 $legalized = preg_replace_callback( "!([^$legal])!",
80 array( &$this, 'hexChar' ),
81 $row->page_title );
82 if( $legalized == '.' ) $legalized = '(dot)';
83 if( $legalized == '_' ) $legalized = '(space)';
84 $legalized = 'Broken/' . $legalized;
86 $title = Title::newFromText( $legalized );
87 if( is_null( $title ) ) {
88 $clean = 'Broken/id:' . $row->page_id;
89 $this->log( "Couldn't legalize; form '$legalized' still invalid; using '$clean'" );
90 $title = Title::newFromText( $clean );
91 } elseif( $title->exists() ) {
92 $clean = 'Broken/id:' . $row->page_id;
93 $this->log( "Legalized for '$legalized' exists; using '$clean'" );
94 $title = Title::newFromText( $clean );
97 $dest = $title->getDBkey();
98 if( $this->dryrun ) {
99 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
100 } else {
101 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
102 $dbw = wfGetDB( DB_MASTER );
103 $dbw->update( 'page',
104 array( 'page_title' => $dest ),
105 array( 'page_id' => $row->page_id ),
106 'cleanupTitles::moveInconsistentPage' );
110 function moveInconsistentPage( $row, $title ) {
111 if( $title->exists() || $title->getInterwiki() ) {
112 if( $title->getInterwiki() ) {
113 $prior = $title->getPrefixedDbKey();
114 } else {
115 $prior = $title->getDBkey();
117 $clean = 'Broken/' . $prior;
118 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
119 if( $verified->exists() ) {
120 $blah = "Broken/id:" . $row->page_id;
121 $this->log( "Couldn't legalize; form '$clean' exists; using '$blah'" );
122 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
124 $title = $verified;
126 if( is_null( $title ) ) {
127 wfDie( "Something awry; empty title.\n" );
129 $ns = $title->getNamespace();
130 $dest = $title->getDBkey();
131 if( $this->dryrun ) {
132 $this->log( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')" );
133 } else {
134 $this->log( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')" );
135 $dbw = wfGetDB( DB_MASTER );
136 $dbw->update( 'page',
137 array(
138 'page_namespace' => $ns,
139 'page_title' => $dest
141 array( 'page_id' => $row->page_id ),
142 'cleanupTitles::moveInconsistentPage' );
143 $linkCache = LinkCache::singleton();
144 $linkCache->clear();
149 $wgUser->setName( 'Conversion script' );
150 $caps = new TitleCleanup( !isset( $options['fix'] ) );
151 $caps->cleanup();