2005-09-01 06:42:20 +00:00
|
|
|
<?php
|
2010-09-05 13:15:48 +00:00
|
|
|
/**
|
2012-06-25 19:54:41 +00:00
|
|
|
* Clean up broken, unparseable titles.
|
2005-09-01 06:42:20 +00:00
|
|
|
*
|
2010-09-05 13:15:48 +00:00
|
|
|
* Copyright © 2005 Brion Vibber <brion@pobox.com>
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-09-01 06:42:20 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2006-01-07 13:09:30 +00:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
2005-09-01 06:42:20 +00:00
|
|
|
* (at your option) any later version.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-09-01 06:42:20 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-09-01 06:42:20 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2006-04-05 07:43:17 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-09-01 06:42:20 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-09-05 13:15:48 +00:00
|
|
|
* @file
|
2005-09-01 06:42:20 +00:00
|
|
|
* @author Brion Vibber <brion at pobox.com>
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2005-09-01 06:42:20 +00:00
|
|
|
*/
|
|
|
|
|
|
2017-07-10 22:07:07 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2020-09-23 10:49:16 +00:00
|
|
|
require_once __DIR__ . '/TableCleanup.php';
|
2005-09-01 06:42:20 +00:00
|
|
|
|
2012-06-25 19:54:41 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script to clean up broken, unparseable titles.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2006-07-13 17:38:01 +00:00
|
|
|
class TitleCleanup extends TableCleanup {
|
2009-08-24 02:14:52 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Script to clean up broken, unparseable titles' );
|
2020-04-21 06:57:21 +00:00
|
|
|
$this->setBatchSize( 1000 );
|
2005-09-01 06:42:20 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
/**
|
2020-11-12 22:22:22 +00:00
|
|
|
* @param stdClass $row
|
2013-09-02 15:49:22 +00:00
|
|
|
*/
|
2009-09-24 04:19:25 +00:00
|
|
|
protected function processRow( $row ) {
|
|
|
|
|
$display = Title::makeName( $row->page_namespace, $row->page_title );
|
2018-07-29 12:24:54 +00:00
|
|
|
$verified = MediaWikiServices::getInstance()->getContentLanguage()->normalize( $display );
|
2005-09-01 06:42:20 +00:00
|
|
|
$title = Title::newFromText( $verified );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $title !== null
|
2009-09-24 04:19:25 +00:00
|
|
|
&& $title->canExist()
|
|
|
|
|
&& $title->getNamespace() == $row->page_namespace
|
2013-10-08 17:11:35 +00:00
|
|
|
&& $title->getDBkey() === $row->page_title
|
|
|
|
|
) {
|
2020-05-19 22:10:27 +00:00
|
|
|
// all is fine
|
|
|
|
|
$this->progress( 0 );
|
2013-10-08 17:11:35 +00:00
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
return;
|
2008-12-16 02:33:38 +00:00
|
|
|
}
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $row->page_namespace == NS_FILE && $this->fileExists( $row->page_title ) ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "file $row->page_title needs cleanup, please run cleanupImages.php.\n" );
|
2013-09-02 15:49:22 +00:00
|
|
|
$this->progress( 0 );
|
2020-01-09 23:48:34 +00:00
|
|
|
} elseif ( $title === null ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "page $row->page_id ($display) is illegal.\n" );
|
2005-09-01 06:42:20 +00:00
|
|
|
$this->moveIllegalPage( $row );
|
2013-09-02 15:49:22 +00:00
|
|
|
$this->progress( 1 );
|
2008-12-16 02:33:38 +00:00
|
|
|
} else {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "page $row->page_id ($display) doesn't match self.\n" );
|
2005-09-01 06:42:20 +00:00
|
|
|
$this->moveInconsistentPage( $row, $title );
|
2013-09-02 15:49:22 +00:00
|
|
|
$this->progress( 1 );
|
2005-09-01 06:42:20 +00:00
|
|
|
}
|
2008-12-16 02:33:38 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2009-08-24 02:14:52 +00:00
|
|
|
protected function fileExists( $name ) {
|
2008-12-16 02:33:38 +00:00
|
|
|
// XXX: Doesn't actually check for file existence, just presence of image record.
|
|
|
|
|
// This is reasonable, since cleanupImages.php only iterates over the image table.
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA );
|
2016-02-17 09:09:32 +00:00
|
|
|
$row = $dbr->selectRow( 'image', [ 'img_name' ], [ 'img_name' => $name ], __METHOD__ );
|
2013-10-08 17:11:35 +00:00
|
|
|
|
2008-12-16 02:33:38 +00:00
|
|
|
return $row !== false;
|
2005-09-01 06:42:20 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
/**
|
2020-11-12 22:22:22 +00:00
|
|
|
* @param stdClass $row
|
2013-09-02 15:49:22 +00:00
|
|
|
*/
|
2009-08-24 02:14:52 +00:00
|
|
|
protected function moveIllegalPage( $row ) {
|
2005-09-01 06:42:20 +00:00
|
|
|
$legal = 'A-Za-z0-9_/\\\\-';
|
|
|
|
|
$legalized = preg_replace_callback( "!([^$legal])!",
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $this, 'hexChar' ],
|
2005-09-01 06:42:20 +00:00
|
|
|
$row->page_title );
|
2013-04-18 18:48:44 +00:00
|
|
|
if ( $legalized == '.' ) {
|
|
|
|
|
$legalized = '(dot)';
|
|
|
|
|
}
|
|
|
|
|
if ( $legalized == '_' ) {
|
|
|
|
|
$legalized = '(space)';
|
|
|
|
|
}
|
2005-09-01 06:42:20 +00:00
|
|
|
$legalized = 'Broken/' . $legalized;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-09-01 06:42:20 +00:00
|
|
|
$title = Title::newFromText( $legalized );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $title === null ) {
|
2005-09-01 06:42:20 +00:00
|
|
|
$clean = 'Broken/id:' . $row->page_id;
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "Couldn't legalize; form '$legalized' still invalid; using '$clean'\n" );
|
2005-09-01 06:42:20 +00:00
|
|
|
$title = Title::newFromText( $clean );
|
2010-05-22 16:50:39 +00:00
|
|
|
} elseif ( $title->exists() ) {
|
2005-09-01 06:42:20 +00:00
|
|
|
$clean = 'Broken/id:' . $row->page_id;
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "Legalized for '$legalized' exists; using '$clean'\n" );
|
2005-09-01 06:42:20 +00:00
|
|
|
$title = Title::newFromText( $clean );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2008-01-14 09:26:36 +00:00
|
|
|
$dest = $title->getDBkey();
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->dryrun ) {
|
2013-10-08 17:11:35 +00:00
|
|
|
$this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
|
|
|
|
|
"'$row->page_title') to ($row->page_namespace,'$dest')\n" );
|
2005-09-01 06:42:20 +00:00
|
|
|
} else {
|
2013-10-08 17:11:35 +00:00
|
|
|
$this->output( "renaming $row->page_id ($row->page_namespace," .
|
|
|
|
|
"'$row->page_title') to ($row->page_namespace,'$dest')\n" );
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2005-09-01 06:42:20 +00:00
|
|
|
$dbw->update( 'page',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page_title' => $dest ],
|
|
|
|
|
[ 'page_id' => $row->page_id ],
|
2009-08-24 02:14:52 +00:00
|
|
|
__METHOD__ );
|
2005-09-01 06:42:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2013-09-02 15:49:22 +00:00
|
|
|
/**
|
2020-11-12 22:22:22 +00:00
|
|
|
* @param stdClass $row
|
2013-09-02 15:49:22 +00:00
|
|
|
* @param Title $title
|
|
|
|
|
*/
|
2017-07-10 22:07:07 +00:00
|
|
|
protected function moveInconsistentPage( $row, Title $title ) {
|
2019-07-04 21:24:34 +00:00
|
|
|
if ( $title->exists( Title::READ_LATEST )
|
2017-07-10 22:07:07 +00:00
|
|
|
|| $title->getInterwiki()
|
|
|
|
|
|| !$title->canExist()
|
|
|
|
|
) {
|
2018-01-26 03:17:01 +00:00
|
|
|
$titleImpossible = $title->getInterwiki() || !$title->canExist();
|
|
|
|
|
if ( $titleImpossible ) {
|
2013-03-09 20:14:22 +00:00
|
|
|
$prior = $title->getPrefixedDBkey();
|
2005-09-01 06:51:40 +00:00
|
|
|
} else {
|
2008-01-14 09:26:36 +00:00
|
|
|
$prior = $title->getDBkey();
|
2005-09-01 06:51:40 +00:00
|
|
|
}
|
2010-04-12 17:55:44 +00:00
|
|
|
|
2017-02-20 22:48:21 +00:00
|
|
|
# Old cleanupTitles could move articles there. See T25147.
|
2010-05-22 16:50:39 +00:00
|
|
|
$ns = $row->page_namespace;
|
2013-04-18 18:48:44 +00:00
|
|
|
if ( $ns < 0 ) {
|
|
|
|
|
$ns = 0;
|
|
|
|
|
}
|
2010-04-12 17:55:44 +00:00
|
|
|
|
2014-08-12 18:46:31 +00:00
|
|
|
# Namespace which no longer exists. Put the page in the main namespace
|
2017-02-20 22:48:21 +00:00
|
|
|
# since we don't have any idea of the old namespace name. See T70501.
|
2018-08-05 17:58:51 +00:00
|
|
|
if ( !MediaWikiServices::getInstance()->getNamespaceInfo()->exists( $ns ) ) {
|
2014-08-12 18:46:31 +00:00
|
|
|
$ns = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 03:17:01 +00:00
|
|
|
if ( !$titleImpossible && !$title->exists() ) {
|
|
|
|
|
// Looks like the current title, after cleaning it up, is valid and available
|
|
|
|
|
$clean = $prior;
|
|
|
|
|
} else {
|
|
|
|
|
$clean = 'Broken/' . $prior;
|
|
|
|
|
}
|
2010-04-12 17:55:44 +00:00
|
|
|
$verified = Title::makeTitleSafe( $ns, $clean );
|
2014-09-08 18:54:53 +00:00
|
|
|
if ( !$verified || $verified->exists() ) {
|
2005-09-01 06:42:20 +00:00
|
|
|
$blah = "Broken/id:" . $row->page_id;
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "Couldn't legalize; form '$clean' exists; using '$blah'\n" );
|
2010-04-12 17:55:44 +00:00
|
|
|
$verified = Title::makeTitleSafe( $ns, $blah );
|
2005-09-01 06:42:20 +00:00
|
|
|
}
|
|
|
|
|
$title = $verified;
|
|
|
|
|
}
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $title === null ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Something awry; empty title." );
|
2005-09-01 06:42:20 +00:00
|
|
|
}
|
2005-10-02 12:42:29 +00:00
|
|
|
$ns = $title->getNamespace();
|
2008-01-14 09:26:36 +00:00
|
|
|
$dest = $title->getDBkey();
|
2010-04-12 17:55:44 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->dryrun ) {
|
2013-10-08 17:11:35 +00:00
|
|
|
$this->output( "DRY RUN: would rename $row->page_id ($row->page_namespace," .
|
|
|
|
|
"'$row->page_title') to ($ns,'$dest')\n" );
|
2005-09-01 06:42:20 +00:00
|
|
|
} else {
|
2013-10-08 17:11:35 +00:00
|
|
|
$this->output( "renaming $row->page_id ($row->page_namespace," .
|
|
|
|
|
"'$row->page_title') to ($ns,'$dest')\n" );
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2005-09-01 06:42:20 +00:00
|
|
|
$dbw->update( 'page',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2005-10-02 12:42:29 +00:00
|
|
|
'page_namespace' => $ns,
|
|
|
|
|
'page_title' => $dest
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'page_id' => $row->page_id ],
|
2009-08-24 02:14:52 +00:00
|
|
|
__METHOD__ );
|
2017-07-10 22:07:07 +00:00
|
|
|
MediaWikiServices::getInstance()->getLinkCache()->clear();
|
2005-09-01 06:42:20 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = TitleCleanup::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|