3 * Clean up broken, unparseable upload filenames.
5 * Copyright © 2005-2006 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 upload filenames.
35 * @ingroup Maintenance
37 class CleanupImages
extends TableCleanup
{
38 protected $defaultParams = [
41 'index' => 'img_name',
42 'callback' => 'processRow',
45 public function __construct() {
46 parent
::__construct();
47 $this->addDescription( 'Script to clean up broken, unparseable upload filenames' );
50 protected function processRow( $row ) {
51 $source = $row->img_name
;
52 if ( $source == '' ) {
53 // Ye olde empty rows. Just kill them.
54 $this->killRow( $source );
56 return $this->progress( 1 );
61 // About half of old bad image names have percent-codes
62 $cleaned = rawurldecode( $cleaned );
64 // We also have some HTML entities there
65 $cleaned = Sanitizer
::decodeCharReferences( $cleaned );
67 $contLang = MediaWikiServices
::getInstance()->getContentLanguage();
69 // Some are old latin-1
70 $cleaned = $contLang->checkTitleEncoding( $cleaned );
72 // Many of remainder look like non-normalized unicode
73 $cleaned = $contLang->normalize( $cleaned );
75 $title = Title
::makeTitleSafe( NS_FILE
, $cleaned );
77 if ( is_null( $title ) ) {
78 $this->output( "page $source ($cleaned) is illegal.\n" );
79 $safe = $this->buildSafeTitle( $cleaned );
80 if ( $safe === false ) {
81 return $this->progress( 0 );
83 $this->pokeFile( $source, $safe );
85 return $this->progress( 1 );
88 if ( $title->getDBkey() !== $source ) {
89 $munged = $title->getDBkey();
90 $this->output( "page $source ($munged) doesn't match self.\n" );
91 $this->pokeFile( $source, $munged );
93 return $this->progress( 1 );
96 return $this->progress( 0 );
100 * @param string $name
102 private function killRow( $name ) {
103 if ( $this->dryrun
) {
104 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
106 $this->output( "deleting bogus row '$name'\n" );
107 $db = $this->getDB( DB_MASTER
);
108 $db->delete( 'image',
109 [ 'img_name' => $name ],
115 * @param string $name
118 private function filePath( $name ) {
119 if ( !isset( $this->repo
) ) {
120 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
123 return $this->repo
->getRootDirectory() . '/' . $this->repo
->getHashPath( $name ) . $name;
126 private function imageExists( $name, $db ) {
127 return $db->selectField( 'image', '1', [ 'img_name' => $name ], __METHOD__
);
130 private function pageExists( $name, $db ) {
131 return $db->selectField(
134 [ 'page_namespace' => NS_FILE
, 'page_title' => $name ],
139 private function pokeFile( $orig, $new ) {
140 $path = $this->filePath( $orig );
141 if ( !file_exists( $path ) ) {
142 $this->output( "missing file: $path\n" );
143 $this->killRow( $orig );
148 $db = $this->getDB( DB_MASTER
);
151 * To prevent key collisions in the update() statements below,
152 * if the target title exists in the image table, or if both the
153 * original and target titles exist in the page table, append
154 * increasing version numbers until the target title exists in
155 * neither. (See also T18916.)
159 $conflict = ( $this->imageExists( $final, $db ) ||
160 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
162 while ( $conflict ) {
163 $this->output( "Rename conflicts with '$final'...\n" );
165 $final = $this->appendTitle( $new, "_$version" );
166 $conflict = ( $this->imageExists( $final, $db ) ||
$this->pageExists( $final, $db ) );
169 $finalPath = $this->filePath( $final );
171 if ( $this->dryrun
) {
172 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
174 $this->output( "renaming $path to $finalPath\n" );
175 // @todo FIXME: Should this use File::move()?
176 $this->beginTransaction( $db, __METHOD__
);
177 $db->update( 'image',
178 [ 'img_name' => $final ],
179 [ 'img_name' => $orig ],
181 $db->update( 'oldimage',
182 [ 'oi_name' => $final ],
183 [ 'oi_name' => $orig ],
186 [ 'page_title' => $final ],
187 [ 'page_title' => $orig, 'page_namespace' => NS_FILE
],
189 $dir = dirname( $finalPath );
190 if ( !file_exists( $dir ) ) {
191 if ( !wfMkdirParents( $dir, null, __METHOD__
) ) {
192 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
193 $this->rollbackTransaction( $db, __METHOD__
);
198 if ( rename( $path, $finalPath ) ) {
199 $this->commitTransaction( $db, __METHOD__
);
201 $this->error( "RENAME FAILED" );
202 $this->rollbackTransaction( $db, __METHOD__
);
207 private function appendTitle( $name, $suffix ) {
208 return preg_replace( '/^(.*)(\..*?)$/',
209 "\\1$suffix\\2", $name );
212 private function buildSafeTitle( $name ) {
213 $x = preg_replace_callback(
214 '/([^' . Title
::legalChars() . ']|~)/',
215 [ $this, 'hexChar' ],
218 $test = Title
::makeTitleSafe( NS_FILE
, $x );
219 if ( is_null( $test ) ||
$test->getDBkey() !== $x ) {
220 $this->error( "Unable to generate safe title from '$name', got '$x'" );
229 $maintClass = CleanupImages
::class;
230 require_once RUN_MAINTENANCE_IF_MAIN
;