]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/cleanupTitles.php
MediaWiki 1.16.4-scripts
[autoinstalls/mediawiki.git] / maintenance / cleanupTitles.php
1 <?php
2 /*
3  * Script to 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 (C) 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  * @author Brion Vibber <brion at pobox.com>
28  * @ingroup Maintenance
29  */
30
31 require_once( dirname(__FILE__) . '/cleanupTable.inc' );
32
33 class TitleCleanup extends TableCleanup {
34         public function __construct() {
35                 parent::__construct();
36                 $this->mDescription = "Script to clean up broken, unparseable titles";
37         }
38
39         protected function processRow( $row ) {
40                 global $wgContLang;
41                 $display = Title::makeName( $row->page_namespace, $row->page_title );
42                 $verified = $wgContLang->normalize( $display );
43                 $title = Title::newFromText( $verified );
44
45                 if( !is_null( $title ) 
46                         && $title->canExist()
47                         && $title->getNamespace() == $row->page_namespace
48                         && $title->getDBkey() === $row->page_title )
49                 {
50                         return $this->progress( 0 );  // all is fine
51                 }
52
53                 if( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
54                         $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
55                         return $this->progress( 0 );
56                 } elseif( is_null( $title ) ) {
57                         $this->output( "page $row->page_id ($display) is illegal.\n" );
58                         $this->moveIllegalPage( $row );
59                         return $this->progress( 1 );
60                 } else {
61                         $this->output( "page $row->page_id ($display) doesn't match self.\n" );
62                         $this->moveInconsistentPage( $row, $title );
63                         return $this->progress( 1 );
64                 }
65         }
66
67         protected function fileExists( $name ) {
68                 // XXX: Doesn't actually check for file existence, just presence of image record.
69                 // This is reasonable, since cleanupImages.php only iterates over the image table.
70                 $dbr = wfGetDB( DB_SLAVE );
71                 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
72                 return $row !== false;
73         }
74
75         protected function moveIllegalPage( $row ) {
76                 $legal = 'A-Za-z0-9_/\\\\-';
77                 $legalized = preg_replace_callback( "!([^$legal])!",
78                         array( &$this, 'hexChar' ),
79                         $row->page_title );
80                 if( $legalized == '.' ) $legalized = '(dot)';
81                 if( $legalized == '_' ) $legalized = '(space)';
82                 $legalized = 'Broken/' . $legalized;
83
84                 $title = Title::newFromText( $legalized );
85                 if( is_null( $title ) ) {
86                         $clean = 'Broken/id:' . $row->page_id;
87                         $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
88                         $title = Title::newFromText( $clean );
89                 } elseif( $title->exists() ) {
90                         $clean = 'Broken/id:' . $row->page_id;
91                         $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
92                         $title = Title::newFromText( $clean );
93                 }
94
95                 $dest = $title->getDBkey();
96                 if( $this->dryrun ) {
97                         $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
98                 } else {
99                         $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
100                         $dbw = wfGetDB( DB_MASTER );
101                         $dbw->update( 'page',
102                                 array( 'page_title' => $dest ),
103                                 array( 'page_id' => $row->page_id ),
104                                 __METHOD__ );
105                 }
106         }
107
108         protected function moveInconsistentPage( $row, $title ) {
109                 if( $title->exists() || $title->getInterwiki() ) {
110                         if( $title->getInterwiki() ) {
111                                 $prior = $title->getPrefixedDbKey();
112                         } else {
113                                 $prior = $title->getDBkey();
114                         }
115                         $clean = 'Broken/' . $prior;
116                         $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
117                         if( $verified->exists() ) {
118                                 $blah = "Broken/id:" . $row->page_id;
119                                 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
120                                 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
121                         }
122                         $title = $verified;
123                 }
124                 if( is_null( $title ) ) {
125                         $this->error( "Something awry; empty title.", true );
126                 }
127                 $ns = $title->getNamespace();
128                 $dest = $title->getDBkey();
129                 if( $this->dryrun ) {
130                         $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
131                 } else {
132                         $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
133                         $dbw = wfGetDB( DB_MASTER );
134                         $dbw->update( 'page',
135                                 array(
136                                         'page_namespace' => $ns,
137                                         'page_title' => $dest
138                                 ),
139                                 array( 'page_id' => $row->page_id ),
140                                 __METHOD__ );
141                         $linkCache = LinkCache::singleton();
142                         $linkCache->clear();
143                 }
144         }
145 }
146
147 $maintClass = "TitleCleanup";
148 require_once( DO_MAINTENANCE );