2006-07-13 21:54:13 +00:00
|
|
|
<?php
|
2010-09-05 13:15:48 +00:00
|
|
|
/**
|
2012-06-25 19:54:41 +00:00
|
|
|
* Clean up broken, unparseable upload filenames.
|
2006-07-13 21:54:13 +00:00
|
|
|
*
|
2012-06-25 19:54:41 +00:00
|
|
|
* Copyright © 2005-2006 Brion Vibber <brion@pobox.com>
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2006-07-13 21:54:13 +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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-09-05 13:15:48 +00:00
|
|
|
* @file
|
2006-07-13 21:54:13 +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
|
2006-07-13 21:54:13 +00:00
|
|
|
*/
|
|
|
|
|
|
2018-07-29 12:24:54 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2020-09-23 10:49:16 +00:00
|
|
|
require_once __DIR__ . '/TableCleanup.php';
|
2006-07-13 21:54:13 +00:00
|
|
|
|
2012-06-25 19:54:41 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script to clean up broken, unparseable upload filenames.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2018-05-24 01:34:01 +00:00
|
|
|
class CleanupImages extends TableCleanup {
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $defaultParams = [
|
2009-09-24 04:19:25 +00:00
|
|
|
'table' => 'image',
|
2016-02-17 09:09:32 +00:00
|
|
|
'conds' => [],
|
2009-09-24 04:19:25 +00:00
|
|
|
'index' => 'img_name',
|
|
|
|
|
'callback' => 'processRow',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2009-09-24 04:19:25 +00:00
|
|
|
|
2019-09-09 09:11:50 +00:00
|
|
|
/** @var LocalRepo|null */
|
|
|
|
|
private $repo;
|
|
|
|
|
|
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 upload filenames' );
|
2006-07-13 21:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
2009-09-24 04:19:25 +00:00
|
|
|
protected function processRow( $row ) {
|
2006-07-13 21:54:13 +00:00
|
|
|
$source = $row->img_name;
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $source == '' ) {
|
2006-07-13 21:54:13 +00:00
|
|
|
// Ye olde empty rows. Just kill them.
|
|
|
|
|
$this->killRow( $source );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
return $this->progress( 1 );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
$cleaned = $source;
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
// About half of old bad image names have percent-codes
|
2006-07-13 23:23:08 +00:00
|
|
|
$cleaned = rawurldecode( $cleaned );
|
2008-11-21 04:54:47 +00:00
|
|
|
|
|
|
|
|
// We also have some HTML entities there
|
|
|
|
|
$cleaned = Sanitizer::decodeCharReferences( $cleaned );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2018-07-29 12:24:54 +00:00
|
|
|
$contLang = MediaWikiServices::getInstance()->getContentLanguage();
|
|
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
// Some are old latin-1
|
2018-07-29 12:24:54 +00:00
|
|
|
$cleaned = $contLang->checkTitleEncoding( $cleaned );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
// Many of remainder look like non-normalized unicode
|
2018-07-29 12:24:54 +00:00
|
|
|
$cleaned = $contLang->normalize( $cleaned );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2008-12-01 17:14:30 +00:00
|
|
|
$title = Title::makeTitleSafe( NS_FILE, $cleaned );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $title === null ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "page $source ($cleaned) is illegal.\n" );
|
2006-07-13 21:54:13 +00:00
|
|
|
$safe = $this->buildSafeTitle( $cleaned );
|
2011-10-18 17:31:54 +00:00
|
|
|
if ( $safe === false ) {
|
2008-11-21 04:54:47 +00:00
|
|
|
return $this->progress( 0 );
|
2011-10-18 17:31:54 +00:00
|
|
|
}
|
2006-07-13 21:54:13 +00:00
|
|
|
$this->pokeFile( $source, $safe );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
return $this->progress( 1 );
|
|
|
|
|
}
|
|
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $title->getDBkey() !== $source ) {
|
2008-01-14 09:26:36 +00:00
|
|
|
$munged = $title->getDBkey();
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "page $source ($munged) doesn't match self.\n" );
|
2006-07-13 21:54:13 +00:00
|
|
|
$this->pokeFile( $source, $munged );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
return $this->progress( 1 );
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-18 17:31:54 +00:00
|
|
|
return $this->progress( 0 );
|
2006-07-13 21:54:13 +00:00
|
|
|
}
|
2009-08-24 02:14:52 +00:00
|
|
|
|
2011-10-14 21:18:38 +00:00
|
|
|
/**
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param string $name
|
2011-10-14 21:18:38 +00:00
|
|
|
*/
|
2009-08-24 02:14:52 +00:00
|
|
|
private function killRow( $name ) {
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->dryrun ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "DRY RUN: would delete bogus row '$name'\n" );
|
2006-07-13 21:54:13 +00:00
|
|
|
} else {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "deleting bogus row '$name'\n" );
|
2021-04-29 02:37:11 +00:00
|
|
|
$db = $this->getDB( DB_PRIMARY );
|
2006-07-13 21:54:13 +00:00
|
|
|
$db->delete( 'image',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'img_name' => $name ],
|
2006-07-13 21:54:13 +00:00
|
|
|
__METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2019-09-08 16:59:52 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2009-08-24 02:14:52 +00:00
|
|
|
private function filePath( $name ) {
|
2019-09-09 09:11:50 +00:00
|
|
|
if ( $this->repo === null ) {
|
2020-02-04 15:44:08 +00:00
|
|
|
$this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
|
2006-07-13 23:23:08 +00:00
|
|
|
}
|
2009-08-24 02:14:52 +00:00
|
|
|
|
2009-08-31 14:01:04 +00:00
|
|
|
private function imageExists( $name, $db ) {
|
2021-08-09 06:57:44 +00:00
|
|
|
return (bool)$db->selectField( 'image', '1', [ 'img_name' => $name ], __METHOD__ );
|
2009-08-31 13:58:28 +00:00
|
|
|
}
|
|
|
|
|
|
2009-08-31 14:01:04 +00:00
|
|
|
private function pageExists( $name, $db ) {
|
2021-08-09 06:57:44 +00:00
|
|
|
return (bool)$db->selectField( 'page', '1',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page_namespace' => NS_FILE, 'page_title' => $name ],
|
2014-04-23 09:40:50 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2009-08-31 13:58:28 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-24 02:14:52 +00:00
|
|
|
private function pokeFile( $orig, $new ) {
|
2006-07-13 23:23:08 +00:00
|
|
|
$path = $this->filePath( $orig );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !file_exists( $path ) ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "missing file: $path\n" );
|
2011-10-18 17:31:54 +00:00
|
|
|
$this->killRow( $orig );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2011-10-18 17:31:54 +00:00
|
|
|
return;
|
2006-07-13 23:23:08 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2021-04-29 02:37:11 +00:00
|
|
|
$db = $this->getDB( DB_PRIMARY );
|
2009-08-31 13:58:28 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* To prevent key collisions in the update() statements below,
|
|
|
|
|
* if the target title exists in the image table, or if both the
|
|
|
|
|
* original and target titles exist in the page table, append
|
|
|
|
|
* increasing version numbers until the target title exists in
|
2017-02-20 22:48:21 +00:00
|
|
|
* neither. (See also T18916.)
|
2009-08-31 13:58:28 +00:00
|
|
|
*/
|
2006-07-13 21:54:13 +00:00
|
|
|
$version = 0;
|
|
|
|
|
$final = $new;
|
2009-08-31 13:58:28 +00:00
|
|
|
$conflict = ( $this->imageExists( $final, $db ) ||
|
2014-04-23 18:08:42 +00:00
|
|
|
( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $conflict ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "Rename conflicts with '$final'...\n" );
|
2006-07-13 21:54:13 +00:00
|
|
|
$version++;
|
2006-07-13 23:23:08 +00:00
|
|
|
$final = $this->appendTitle( $new, "_$version" );
|
2009-08-31 13:58:28 +00:00
|
|
|
$conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
|
2006-07-13 21:54:13 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-13 23:23:08 +00:00
|
|
|
$finalPath = $this->filePath( $final );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->dryrun ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "DRY RUN: would rename $path to $finalPath\n" );
|
2006-07-13 21:54:13 +00:00
|
|
|
} else {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->output( "renaming $path to $finalPath\n" );
|
2011-05-17 22:03:20 +00:00
|
|
|
// @todo FIXME: Should this use File::move()?
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->beginTransaction( $db, __METHOD__ );
|
2006-07-13 21:54:13 +00:00
|
|
|
$db->update( 'image',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'img_name' => $final ],
|
|
|
|
|
[ 'img_name' => $orig ],
|
2006-07-13 21:54:13 +00:00
|
|
|
__METHOD__ );
|
2008-11-21 04:54:47 +00:00
|
|
|
$db->update( 'oldimage',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'oi_name' => $final ],
|
|
|
|
|
[ 'oi_name' => $orig ],
|
2008-11-21 04:54:47 +00:00
|
|
|
__METHOD__ );
|
|
|
|
|
$db->update( 'page',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'page_title' => $final ],
|
|
|
|
|
[ 'page_title' => $orig, 'page_namespace' => NS_FILE ],
|
2008-11-21 04:54:47 +00:00
|
|
|
__METHOD__ );
|
2006-07-13 23:23:08 +00:00
|
|
|
$dir = dirname( $finalPath );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !file_exists( $dir ) ) {
|
2011-07-25 22:01:19 +00:00
|
|
|
if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
|
2011-10-18 17:31:54 +00:00
|
|
|
$this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->rollbackTransaction( $db, __METHOD__ );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2006-07-13 23:23:08 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( rename( $path, $finalPath ) ) {
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->commitTransaction( $db, __METHOD__ );
|
2006-07-13 23:23:08 +00:00
|
|
|
} else {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->error( "RENAME FAILED" );
|
2015-12-22 08:51:42 +00:00
|
|
|
$this->rollbackTransaction( $db, __METHOD__ );
|
2006-07-13 23:23:08 +00:00
|
|
|
}
|
2006-07-13 21:54:13 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-24 02:14:52 +00:00
|
|
|
|
|
|
|
|
private function appendTitle( $name, $suffix ) {
|
2006-07-13 21:54:13 +00:00
|
|
|
return preg_replace( '/^(.*)(\..*?)$/',
|
2006-07-13 23:23:08 +00:00
|
|
|
"\\1$suffix\\2", $name );
|
2006-07-13 21:54:13 +00:00
|
|
|
}
|
2009-08-24 02:14:52 +00:00
|
|
|
|
|
|
|
|
private function buildSafeTitle( $name ) {
|
2006-07-13 21:54:13 +00:00
|
|
|
$x = preg_replace_callback(
|
2012-05-04 19:47:00 +00:00
|
|
|
'/([^' . Title::legalChars() . ']|~)/',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ $this, 'hexChar' ],
|
2006-07-13 23:53:04 +00:00
|
|
|
$name );
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2008-12-01 17:14:30 +00:00
|
|
|
$test = Title::makeTitleSafe( NS_FILE, $x );
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $test === null || $test->getDBkey() !== $x ) {
|
2009-08-24 02:14:52 +00:00
|
|
|
$this->error( "Unable to generate safe title from '$name', got '$x'" );
|
2014-04-23 18:08:42 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2006-07-13 21:54:13 +00:00
|
|
|
return $x;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-24 01:34:01 +00:00
|
|
|
$maintClass = CleanupImages::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|