MediaWiki 1.40.4
cleanupTitles.php
Go to the documentation of this file.
1<?php
30
31require_once __DIR__ . '/TableCleanup.php';
32
39 public function __construct() {
40 parent::__construct();
41 $this->addDescription( 'Script to clean up broken, unparseable titles' );
42 $this->setBatchSize( 1000 );
43 }
44
48 protected function processRow( $row ) {
49 $display = Title::makeName( $row->page_namespace, $row->page_title );
50 $verified = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $display );
51 $title = Title::newFromText( $verified );
52
53 if ( $title !== null
54 && $title->canExist()
55 && $title->getNamespace() == $row->page_namespace
56 && $title->getDBkey() === $row->page_title
57 ) {
58 // all is fine
59 $this->progress( 0 );
60
61 return;
62 }
63
64 if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
65 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
66 $this->progress( 0 );
67 } elseif ( $title === null ) {
68 $this->output( "page $row->page_id ($display) is illegal.\n" );
69 $this->moveIllegalPage( $row );
70 $this->progress( 1 );
71 } else {
72 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
73 $this->moveInconsistentPage( $row, $title );
74 $this->progress( 1 );
75 }
76 }
77
82 protected function fileExists( $name ) {
83 // XXX: Doesn't actually check for file existence, just presence of image record.
84 // This is reasonable, since cleanupImages.php only iterates over the image table.
85 $dbr = $this->getDB( DB_REPLICA );
86 $row = $dbr->newSelectQueryBuilder()
87 ->select( '*' )
88 ->from( 'image' )
89 ->where( [ 'img_name' => $name ] )
90 ->caller( __METHOD__ )
91 ->fetchRow();
92
93 return $row !== false;
94 }
95
99 protected function moveIllegalPage( $row ) {
100 $legal = 'A-Za-z0-9_/\\\\-';
101 $legalized = preg_replace_callback( "!([^$legal])!",
102 [ $this, 'hexChar' ],
103 $row->page_title );
104 if ( $legalized == '.' ) {
105 $legalized = '(dot)';
106 }
107 if ( $legalized == '_' ) {
108 $legalized = '(space)';
109 }
110 $legalized = 'Broken/' . $legalized;
111
112 $title = Title::newFromText( $legalized );
113 if ( $title === null ) {
114 $clean = 'Broken/id:' . $row->page_id;
115 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
116 $title = Title::newFromText( $clean );
117 } elseif ( $title->exists() ) {
118 $clean = 'Broken/id:' . $row->page_id;
119 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
120 $title = Title::newFromText( $clean );
121 }
122
123 $dest = $title->getDBkey();
124 if ( $this->dryrun ) {
125 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
126 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
127 } else {
128 $this->output( "renaming $row->page_id ($row->page_namespace," .
129 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
130 $dbw = $this->getDB( DB_PRIMARY );
131 $dbw->update( 'page',
132 [ 'page_title' => $dest ],
133 [ 'page_id' => $row->page_id ],
134 __METHOD__ );
135 }
136 }
137
142 protected function moveInconsistentPage( $row, Title $title ) {
143 if ( $title->exists( Title::READ_LATEST )
144 || $title->getInterwiki()
145 || !$title->canExist()
146 ) {
147 $titleImpossible = $title->getInterwiki() || !$title->canExist();
148 if ( $titleImpossible ) {
149 $prior = $title->getPrefixedDBkey();
150 } else {
151 $prior = $title->getDBkey();
152 }
153
154 # Old cleanupTitles could move articles there. See T25147.
155 $ns = $row->page_namespace;
156 if ( $ns < 0 ) {
157 $ns = 0;
158 }
159
160 # Namespace which no longer exists. Put the page in the main namespace
161 # since we don't have any idea of the old namespace name. See T70501.
162 if ( !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $ns ) ) {
163 $ns = 0;
164 }
165
166 if ( !$titleImpossible && !$title->exists() ) {
167 // Looks like the current title, after cleaning it up, is valid and available
168 $clean = $prior;
169 } else {
170 $clean = 'Broken/' . $prior;
171 }
172 $verified = Title::makeTitleSafe( $ns, $clean );
173 if ( !$verified || $verified->exists() ) {
174 $blah = "Broken/id:" . $row->page_id;
175 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
176 $verified = Title::makeTitleSafe( $ns, $blah );
177 }
178 $title = $verified;
179 }
180 if ( $title === null ) {
181 $this->fatalError( "Something awry; empty title." );
182 }
183 $ns = $title->getNamespace();
184 $dest = $title->getDBkey();
185
186 if ( $this->dryrun ) {
187 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
188 "'$row->page_title') to ($ns,'$dest')\n" );
189 } else {
190 $this->output( "renaming $row->page_id ($row->page_namespace," .
191 "'$row->page_title') to ($ns,'$dest')\n" );
192 $dbw = $this->getDB( DB_PRIMARY );
193 $dbw->update( 'page',
194 [
195 'page_namespace' => $ns,
196 'page_title' => $dest
197 ],
198 [ 'page_id' => $row->page_id ],
199 __METHOD__ );
200 MediaWikiServices::getInstance()->getLinkCache()->clear();
201 }
202 }
203}
204
205$maintClass = TitleCleanup::class;
206require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const NS_FILE
Definition Defines.php:70
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
Generic class to cleanup a database table.
progress( $updated)
Maintenance script to clean up broken, unparseable titles.
moveIllegalPage( $row)
moveInconsistentPage( $row, Title $title)
__construct()
Default constructor.
fileExists( $name)
$maintClass
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28