Rewrite various TableCleanup scripts to subclass Maintenance instead of FiveUpgrade
[lhc/web/wiklou.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 processPage( $row ) {
40 $current = Title::makeTitle( $row->page_namespace, $row->page_title );
41 $display = $current->getPrefixedText();
42
43 $verified = UtfNormal::cleanUp( $display );
44
45 $title = Title::newFromText( $verified );
46
47 if( !is_null( $title ) && $title->equals( $current ) && $title->canExist() ) {
48 return $this->progress( 0 ); // all is fine
49 }
50
51 if( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
52 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
53 return $this->progress( 0 );
54 } elseif( is_null( $title ) ) {
55 $this->output( "page $row->page_id ($display) is illegal.\n" );
56 $this->moveIllegalPage( $row );
57 return $this->progress( 1 );
58 } else {
59 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
60 $this->moveInconsistentPage( $row, $title );
61 return $this->progress( 1 );
62 }
63 }
64
65 protected function fileExists( $name ) {
66 // XXX: Doesn't actually check for file existence, just presence of image record.
67 // This is reasonable, since cleanupImages.php only iterates over the image table.
68 $dbr = wfGetDB( DB_SLAVE );
69 $row = $dbr->selectRow( 'image', array( 'img_name' ), array( 'img_name' => $name ), __METHOD__ );
70 return $row !== false;
71 }
72
73 protected function moveIllegalPage( $row ) {
74 $legal = 'A-Za-z0-9_/\\\\-';
75 $legalized = preg_replace_callback( "!([^$legal])!",
76 array( &$this, 'hexChar' ),
77 $row->page_title );
78 if( $legalized == '.' ) $legalized = '(dot)';
79 if( $legalized == '_' ) $legalized = '(space)';
80 $legalized = 'Broken/' . $legalized;
81
82 $title = Title::newFromText( $legalized );
83 if( is_null( $title ) ) {
84 $clean = 'Broken/id:' . $row->page_id;
85 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
86 $title = Title::newFromText( $clean );
87 } elseif( $title->exists() ) {
88 $clean = 'Broken/id:' . $row->page_id;
89 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
90 $title = Title::newFromText( $clean );
91 }
92
93 $dest = $title->getDBkey();
94 if( $this->dryrun ) {
95 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
96 } else {
97 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
98 $dbw = wfGetDB( DB_MASTER );
99 $dbw->update( 'page',
100 array( 'page_title' => $dest ),
101 array( 'page_id' => $row->page_id ),
102 __METHOD__ );
103 }
104 }
105
106 protected function moveInconsistentPage( $row, $title ) {
107 if( $title->exists() || $title->getInterwiki() ) {
108 if( $title->getInterwiki() ) {
109 $prior = $title->getPrefixedDbKey();
110 } else {
111 $prior = $title->getDBkey();
112 }
113 $clean = 'Broken/' . $prior;
114 $verified = Title::makeTitleSafe( $row->page_namespace, $clean );
115 if( $verified->exists() ) {
116 $blah = "Broken/id:" . $row->page_id;
117 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
118 $verified = Title::makeTitleSafe( $row->page_namespace, $blah );
119 }
120 $title = $verified;
121 }
122 if( is_null( $title ) ) {
123 $this->error( "Something awry; empty title.", true );
124 }
125 $ns = $title->getNamespace();
126 $dest = $title->getDBkey();
127 if( $this->dryrun ) {
128 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace,'$row->page_title') to ($row->page_namespace,'$dest')\n" );
129 } else {
130 $this->output( "renaming $row->page_id ($row->page_namespace,'$row->page_title') to ($ns,'$dest')\n" );
131 $dbw = wfGetDB( DB_MASTER );
132 $dbw->update( 'page',
133 array(
134 'page_namespace' => $ns,
135 'page_title' => $dest
136 ),
137 array( 'page_id' => $row->page_id ),
138 __METHOD__ );
139 $linkCache = LinkCache::singleton();
140 $linkCache->clear();
141 }
142 }
143 }
144
145 $maintClass = "TitleCleanup";
146 require_once( DO_MAINTENANCE );