1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
|
<?php
/**
* Clean up broken, unparseable upload filenames.
*
* Copyright © 2005-2006 Brooke Vibber <bvibber@wikimedia.org>
* https://www.mediawiki.org/
*
* 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
*
* @file
* @author Brooke Vibber <bvibber@wikimedia.org>
* @ingroup Maintenance
*/
use MediaWiki\Parser\Sanitizer;
use MediaWiki\Title\Title;
// @codeCoverageIgnoreStart
require_once __DIR__ . '/TableCleanup.php';
// @codeCoverageIgnoreEnd
/**
* Maintenance script to clean up broken, unparseable upload filenames.
*
* @ingroup Maintenance
*/
class CleanupImages extends TableCleanup {
/** @inheritDoc */
protected $defaultParams = [
'table' => 'image',
'conds' => [],
'index' => 'img_name',
'callback' => 'processRow',
];
/** @var LocalRepo|null */
private $repo;
public function __construct() {
parent::__construct();
$this->addDescription( 'Script to clean up broken, unparseable upload filenames' );
}
protected function processRow( $row ) {
$source = $row->img_name;
if ( $source == '' ) {
// Ye olde empty rows. Just kill them.
$this->killRow( $source );
$this->progress( 1 );
return;
}
$cleaned = $source;
// About half of old bad image names have percent-codes
$cleaned = rawurldecode( $cleaned );
// We also have some HTML entities there
$cleaned = Sanitizer::decodeCharReferences( $cleaned );
$contLang = $this->getServiceContainer()->getContentLanguage();
// Some are old latin-1
$cleaned = $contLang->checkTitleEncoding( $cleaned );
// Many of remainder look like non-normalized unicode
$cleaned = $contLang->normalize( $cleaned );
$title = Title::makeTitleSafe( NS_FILE, $cleaned );
if ( $title === null ) {
$this->output( "page $source ($cleaned) is illegal.\n" );
$safe = $this->buildSafeTitle( $cleaned );
if ( $safe === false ) {
$this->progress( 0 );
return;
}
$this->pokeFile( $source, $safe );
$this->progress( 1 );
return;
}
if ( $title->getDBkey() !== $source ) {
$munged = $title->getDBkey();
$this->output( "page $source ($munged) doesn't match self.\n" );
$this->pokeFile( $source, $munged );
$this->progress( 1 );
return;
}
$this->progress( 0 );
}
/**
* @param string $name
*/
private function killRow( $name ) {
if ( $this->dryrun ) {
$this->output( "DRY RUN: would delete bogus row '$name'\n" );
} else {
$this->output( "deleting bogus row '$name'\n" );
$db = $this->getPrimaryDB();
$db->newDeleteQueryBuilder()
->deleteFrom( 'image' )
->where( [ 'img_name' => $name ] )
->caller( __METHOD__ )
->execute();
}
}
/**
* @param string $name
* @return string
*/
private function filePath( $name ) {
if ( $this->repo === null ) {
$this->repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
}
return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
}
private function imageExists( $name, $db ) {
return (bool)$db->newSelectQueryBuilder()
->select( '1' )
->from( 'image' )
->where( [ 'img_name' => $name ] )
->caller( __METHOD__ )
->fetchField();
}
private function pageExists( $name, $db ) {
return (bool)$db->newSelectQueryBuilder()
->select( '1' )
->from( 'page' )
->where( [
'page_namespace' => NS_FILE,
'page_title' => $name,
] )
->caller( __METHOD__ )
->fetchField();
}
private function pokeFile( $orig, $new ) {
$path = $this->filePath( $orig );
if ( !file_exists( $path ) ) {
$this->output( "missing file: $path\n" );
$this->killRow( $orig );
return;
}
$db = $this->getPrimaryDB();
/*
* 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
* neither. (See also T18916.)
*/
$version = 0;
$final = $new;
$conflict = ( $this->imageExists( $final, $db ) ||
( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
while ( $conflict ) {
$this->output( "Rename conflicts with '$final'...\n" );
$version++;
$final = $this->appendTitle( $new, "_$version" );
$conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
}
$finalPath = $this->filePath( $final );
if ( $this->dryrun ) {
$this->output( "DRY RUN: would rename $path to $finalPath\n" );
} else {
$this->output( "renaming $path to $finalPath\n" );
// @todo FIXME: Should this use File::move()?
$this->beginTransaction( $db, __METHOD__ );
$db->newUpdateQueryBuilder()
->update( 'image' )
->set( [ 'img_name' => $final ] )
->where( [ 'img_name' => $orig ] )
->caller( __METHOD__ )
->execute();
$db->newUpdateQueryBuilder()
->update( 'oldimage' )
->set( [ 'oi_name' => $final ] )
->where( [ 'oi_name' => $orig ] )
->caller( __METHOD__ )
->execute();
$db->newUpdateQueryBuilder()
->update( 'page' )
->set( [ 'page_title' => $final ] )
->where( [ 'page_title' => $orig, 'page_namespace' => NS_FILE ] )
->caller( __METHOD__ )
->execute();
$dir = dirname( $finalPath );
if ( !file_exists( $dir ) ) {
if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
$this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
$this->rollbackTransaction( $db, __METHOD__ );
return;
}
}
if ( rename( $path, $finalPath ) ) {
$this->commitTransaction( $db, __METHOD__ );
} else {
$this->error( "RENAME FAILED" );
$this->rollbackTransaction( $db, __METHOD__ );
}
}
}
private function appendTitle( $name, $suffix ) {
return preg_replace( '/^(.*)(\..*?)$/',
"\\1$suffix\\2", $name );
}
private function buildSafeTitle( $name ) {
$x = preg_replace_callback(
'/([^' . Title::legalChars() . ']|~)/',
[ $this, 'hexChar' ],
$name );
$test = Title::makeTitleSafe( NS_FILE, $x );
if ( $test === null || $test->getDBkey() !== $x ) {
$this->error( "Unable to generate safe title from '$name', got '$x'" );
return false;
}
return $x;
}
}
// @codeCoverageIgnoreStart
$maintClass = CleanupImages::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd
|