3 * Script to clean up broken, unparseable upload filenames.
5 * Usage: php cleanupImages.php [--fix]
7 * --fix Actually clean up titles; otherwise just checks for them
9 * Copyright (C) 2005-2006 Brion Vibber <brion@pobox.com>
10 * http://www.mediawiki.org/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
27 * @author Brion Vibber <brion at pobox.com>
28 * @ingroup Maintenance
31 require_once( dirname(__FILE__
) . '/cleanupTable.inc' );
33 class ImageCleanup
extends TableCleanup
{
34 protected $targetTable = 'image';
35 public function __construct() {
36 parent
::__construct();
37 $this->mDescription
= "Script to clean up broken, unparseable upload filenames";
40 protected function processPage( $row ) {
43 $source = $row->img_name
;
45 // Ye olde empty rows. Just kill them.
46 $this->killRow( $source );
47 return $this->progress( 1 );
52 // About half of old bad image names have percent-codes
53 $cleaned = rawurldecode( $cleaned );
55 // We also have some HTML entities there
56 $cleaned = Sanitizer
::decodeCharReferences( $cleaned );
58 // Some are old latin-1
59 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
61 // Many of remainder look like non-normalized unicode
62 $cleaned = UtfNormal
::cleanUp( $cleaned );
64 $title = Title
::makeTitleSafe( NS_FILE
, $cleaned );
66 if( is_null( $title ) ) {
67 $this->output( "page $source ($cleaned) is illegal.\n" );
68 $safe = $this->buildSafeTitle( $cleaned );
70 return $this->progress( 0 );
71 $this->pokeFile( $source, $safe );
72 return $this->progress( 1 );
75 if( $title->getDBkey() !== $source ) {
76 $munged = $title->getDBkey();
77 $this->output( "page $source ($munged) doesn't match self.\n" );
78 $this->pokeFile( $source, $munged );
79 return $this->progress( 1 );
85 private function killRow( $name ) {
87 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
89 $this->output( "deleting bogus row '$name'\n" );
90 $db = wfGetDB( DB_MASTER
);
92 array( 'img_name' => $name ),
97 private function filePath( $name ) {
98 if ( !isset( $this->repo
) ) {
99 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
101 return $this->repo
->getRootDirectory() . '/' . $this->repo
->getHashPath( $name ) . $name;
104 private function imageExists( $name, $db ) {
105 return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__
);
108 private function pageExists( $name, $db ) {
109 return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE
, 'page_title' => $name ), __METHOD__
);
112 private function pokeFile( $orig, $new ) {
113 $path = $this->filePath( $orig );
114 if( !file_exists( $path ) ) {
115 $this->output( "missing file: $path\n" );
116 return $this->killRow( $orig );
119 $db = wfGetDB( DB_MASTER
);
122 * To prevent key collisions in the update() statements below,
123 * if the target title exists in the image table, or if both the
124 * original and target titles exist in the page table, append
125 * increasing version numbers until the target title exists in
126 * neither. (See also bug 16916.)
130 $conflict = ( $this->imageExists( $final, $db ) ||
131 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
134 $this->output( "Rename conflicts with '$final'...\n" );
136 $final = $this->appendTitle( $new, "_$version" );
137 $conflict = ( $this->imageExists( $final, $db ) ||
$this->pageExists( $final, $db ) );
140 $finalPath = $this->filePath( $final );
142 if( $this->dryrun
) {
143 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
145 $this->output( "renaming $path to $finalPath\n" );
146 // XXX: should this use File::move()? FIXME?
148 $db->update( 'image',
149 array( 'img_name' => $final ),
150 array( 'img_name' => $orig ),
152 $db->update( 'oldimage',
153 array( 'oi_name' => $final ),
154 array( 'oi_name' => $orig ),
157 array( 'page_title' => $final ),
158 array( 'page_title' => $orig, 'page_namespace' => NS_FILE
),
160 $dir = dirname( $finalPath );
161 if( !file_exists( $dir ) ) {
162 if( !wfMkdirParents( $dir ) ) {
163 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
168 if( rename( $path, $finalPath ) ) {
171 $this->error( "RENAME FAILED" );
177 private function appendTitle( $name, $suffix ) {
178 return preg_replace( '/^(.*)(\..*?)$/',
179 "\\1$suffix\\2", $name );
182 private function buildSafeTitle( $name ) {
183 global $wgLegalTitleChars;
184 $x = preg_replace_callback(
185 "/([^$wgLegalTitleChars]|~)/",
186 array( $this, 'hexChar' ),
189 $test = Title
::makeTitleSafe( NS_FILE
, $x );
190 if( is_null( $test ) ||
$test->getDBkey() !== $x ) {
191 $this->error( "Unable to generate safe title from '$name', got '$x'" );
199 $maintClass = "ImageCleanup";
200 require_once( DO_MAINTENANCE
);