3 * Clean up broken, unparseable titles.
5 * Copyright © 2005 Brion Vibber <brion@pobox.com>
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @author Brion Vibber <brion at pobox.com>
25 * @ingroup Maintenance
28 use MediaWiki\MediaWikiServices
;
30 require_once __DIR__
. '/cleanupTable.inc';
33 * Maintenance script to clean up broken, unparseable titles.
35 * @ingroup Maintenance
37 class TitleCleanup
extends TableCleanup
{
38 public function __construct() {
39 parent
::__construct();
40 $this->addDescription( 'Script to clean up broken, unparseable titles' );
41 $this->batchSize
= 1000;
47 protected function processRow( $row ) {
48 $display = Title
::makeName( $row->page_namespace
, $row->page_title
);
49 $verified = MediaWikiServices
::getInstance()->getContentLanguage()->normalize( $display );
50 $title = Title
::newFromText( $verified );
52 if ( !is_null( $title )
54 && $title->getNamespace() == $row->page_namespace
55 && $title->getDBkey() === $row->page_title
57 $this->progress( 0 ); // all is fine
62 if ( $row->page_namespace
== NS_FILE
&& $this->fileExists( $row->page_title
) ) {
63 $this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
65 } elseif ( is_null( $title ) ) {
66 $this->output( "page $row->page_id ($display) is illegal.\n" );
67 $this->moveIllegalPage( $row );
70 $this->output( "page $row->page_id ($display) doesn't match self.\n" );
71 $this->moveInconsistentPage( $row, $title );
80 protected function fileExists( $name ) {
81 // XXX: Doesn't actually check for file existence, just presence of image record.
82 // This is reasonable, since cleanupImages.php only iterates over the image table.
83 $dbr = $this->getDB( DB_REPLICA
);
84 $row = $dbr->selectRow( 'image', [ 'img_name' ], [ 'img_name' => $name ], __METHOD__
);
86 return $row !== false;
92 protected function moveIllegalPage( $row ) {
93 $legal = 'A-Za-z0-9_/\\\\-';
94 $legalized = preg_replace_callback( "!([^$legal])!",
97 if ( $legalized == '.' ) {
100 if ( $legalized == '_' ) {
101 $legalized = '(space)';
103 $legalized = 'Broken/' . $legalized;
105 $title = Title
::newFromText( $legalized );
106 if ( is_null( $title ) ) {
107 $clean = 'Broken/id:' . $row->page_id
;
108 $this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
109 $title = Title
::newFromText( $clean );
110 } elseif ( $title->exists() ) {
111 $clean = 'Broken/id:' . $row->page_id
;
112 $this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
113 $title = Title
::newFromText( $clean );
116 $dest = $title->getDBkey();
117 if ( $this->dryrun
) {
118 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
119 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
121 $this->output( "renaming $row->page_id ($row->page_namespace," .
122 "'$row->page_title') to ($row->page_namespace,'$dest')\n" );
123 $dbw = $this->getDB( DB_MASTER
);
124 $dbw->update( 'page',
125 [ 'page_title' => $dest ],
126 [ 'page_id' => $row->page_id
],
133 * @param Title $title
135 protected function moveInconsistentPage( $row, Title
$title ) {
136 if ( $title->exists( Title
::GAID_FOR_UPDATE
)
137 ||
$title->getInterwiki()
138 ||
!$title->canExist()
140 $titleImpossible = $title->getInterwiki() ||
!$title->canExist();
141 if ( $titleImpossible ) {
142 $prior = $title->getPrefixedDBkey();
144 $prior = $title->getDBkey();
147 # Old cleanupTitles could move articles there. See T25147.
148 $ns = $row->page_namespace
;
153 # Namespace which no longer exists. Put the page in the main namespace
154 # since we don't have any idea of the old namespace name. See T70501.
155 if ( !MediaWikiServices
::getInstance()->getNamespaceInfo()->exists( $ns ) ) {
159 if ( !$titleImpossible && !$title->exists() ) {
160 // Looks like the current title, after cleaning it up, is valid and available
163 $clean = 'Broken/' . $prior;
165 $verified = Title
::makeTitleSafe( $ns, $clean );
166 if ( !$verified ||
$verified->exists() ) {
167 $blah = "Broken/id:" . $row->page_id
;
168 $this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
169 $verified = Title
::makeTitleSafe( $ns, $blah );
173 if ( is_null( $title ) ) {
174 $this->fatalError( "Something awry; empty title." );
176 $ns = $title->getNamespace();
177 $dest = $title->getDBkey();
179 if ( $this->dryrun
) {
180 $this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
181 "'$row->page_title') to ($ns,'$dest')\n" );
183 $this->output( "renaming $row->page_id ($row->page_namespace," .
184 "'$row->page_title') to ($ns,'$dest')\n" );
185 $dbw = $this->getDB( DB_MASTER
);
186 $dbw->update( 'page',
188 'page_namespace' => $ns,
189 'page_title' => $dest
191 [ 'page_id' => $row->page_id
],
193 MediaWikiServices
::getInstance()->getLinkCache()->clear();
198 $maintClass = TitleCleanup
::class;
199 require_once RUN_MAINTENANCE_IF_MAIN
;