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 require_once __DIR__
. '/cleanupTable.inc';
31 * Maintenance script to clean up broken, unparseable upload filenames.
33 * @ingroup Maintenance
35 class ImageCleanup
extends TableCleanup
{
36 protected $defaultParams = [
39 'index' => 'img_name',
40 'callback' => 'processRow',
43 public function __construct() {
44 parent
::__construct();
45 $this->addDescription( 'Script to clean up broken, unparseable upload filenames' );
48 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 // Some are old latin-1
68 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
70 // Many of remainder look like non-normalized unicode
71 $cleaned = $wgContLang->normalize( $cleaned );
73 $title = Title
::makeTitleSafe( NS_FILE
, $cleaned );
75 if ( is_null( $title ) ) {
76 $this->output( "page $source ($cleaned) is illegal.\n" );
77 $safe = $this->buildSafeTitle( $cleaned );
78 if ( $safe === false ) {
79 return $this->progress( 0 );
81 $this->pokeFile( $source, $safe );
83 return $this->progress( 1 );
86 if ( $title->getDBkey() !== $source ) {
87 $munged = $title->getDBkey();
88 $this->output( "page $source ($munged) doesn't match self.\n" );
89 $this->pokeFile( $source, $munged );
91 return $this->progress( 1 );
94 return $this->progress( 0 );
100 private function killRow( $name ) {
101 if ( $this->dryrun
) {
102 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
104 $this->output( "deleting bogus row '$name'\n" );
105 $db = $this->getDB( DB_MASTER
);
106 $db->delete( 'image',
107 [ 'img_name' => $name ],
112 private function filePath( $name ) {
113 if ( !isset( $this->repo
) ) {
114 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
117 return $this->repo
->getRootDirectory() . '/' . $this->repo
->getHashPath( $name ) . $name;
120 private function imageExists( $name, $db ) {
121 return $db->selectField( 'image', '1', [ 'img_name' => $name ], __METHOD__
);
124 private function pageExists( $name, $db ) {
125 return $db->selectField(
128 [ 'page_namespace' => NS_FILE
, 'page_title' => $name ],
133 private function pokeFile( $orig, $new ) {
134 $path = $this->filePath( $orig );
135 if ( !file_exists( $path ) ) {
136 $this->output( "missing file: $path\n" );
137 $this->killRow( $orig );
142 $db = $this->getDB( DB_MASTER
);
145 * To prevent key collisions in the update() statements below,
146 * if the target title exists in the image table, or if both the
147 * original and target titles exist in the page table, append
148 * increasing version numbers until the target title exists in
149 * neither. (See also bug 16916.)
153 $conflict = ( $this->imageExists( $final, $db ) ||
154 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
156 while ( $conflict ) {
157 $this->output( "Rename conflicts with '$final'...\n" );
159 $final = $this->appendTitle( $new, "_$version" );
160 $conflict = ( $this->imageExists( $final, $db ) ||
$this->pageExists( $final, $db ) );
163 $finalPath = $this->filePath( $final );
165 if ( $this->dryrun
) {
166 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
168 $this->output( "renaming $path to $finalPath\n" );
169 // @todo FIXME: Should this use File::move()?
170 $this->beginTransaction( $db, __METHOD__
);
171 $db->update( 'image',
172 [ 'img_name' => $final ],
173 [ 'img_name' => $orig ],
175 $db->update( 'oldimage',
176 [ 'oi_name' => $final ],
177 [ 'oi_name' => $orig ],
180 [ 'page_title' => $final ],
181 [ 'page_title' => $orig, 'page_namespace' => NS_FILE
],
183 $dir = dirname( $finalPath );
184 if ( !file_exists( $dir ) ) {
185 if ( !wfMkdirParents( $dir, null, __METHOD__
) ) {
186 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
187 $this->rollbackTransaction( $db, __METHOD__
);
192 if ( rename( $path, $finalPath ) ) {
193 $this->commitTransaction( $db, __METHOD__
);
195 $this->error( "RENAME FAILED" );
196 $this->rollbackTransaction( $db, __METHOD__
);
201 private function appendTitle( $name, $suffix ) {
202 return preg_replace( '/^(.*)(\..*?)$/',
203 "\\1$suffix\\2", $name );
206 private function buildSafeTitle( $name ) {
207 $x = preg_replace_callback(
208 '/([^' . Title
::legalChars() . ']|~)/',
209 [ $this, 'hexChar' ],
212 $test = Title
::makeTitleSafe( NS_FILE
, $x );
213 if ( is_null( $test ) ||
$test->getDBkey() !== $x ) {
214 $this->error( "Unable to generate safe title from '$name', got '$x'" );
223 $maintClass = "ImageCleanup";
224 require_once RUN_MAINTENANCE_IF_MAIN
;