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
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 require_once( 'commandLine.inc' );
33 require_once( 'cleanupTable.inc' );
36 * @ingroup Maintenance
38 class ImageCleanup
extends TableCleanup
{
39 function __construct( $dryrun = false ) {
40 parent
::__construct( 'image', $dryrun );
43 function processPage( $row ) {
46 $source = $row->img_name
;
48 // Ye olde empty rows. Just kill them.
49 $this->killRow( $source );
50 return $this->progress( 1 );
55 // About half of old bad image names have percent-codes
56 $cleaned = rawurldecode( $cleaned );
58 // We also have some HTML entities there
59 $cleaned = Sanitizer
::decodeCharReferences( $cleaned );
61 // Some are old latin-1
62 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
64 // Many of remainder look like non-normalized unicode
65 $cleaned = UtfNormal
::cleanUp( $cleaned );
67 $title = Title
::makeTitleSafe( NS_IMAGE
, $cleaned );
69 if( is_null( $title ) ) {
70 $this->log( "page $source ($cleaned) is illegal." );
71 $safe = $this->buildSafeTitle( $cleaned );
73 return $this->progress( 0 );
74 $this->pokeFile( $source, $safe );
75 return $this->progress( 1 );
78 if( $title->getDBkey() !== $source ) {
79 $munged = $title->getDBkey();
80 $this->log( "page $source ($munged) doesn't match self." );
81 $this->pokeFile( $source, $munged );
82 return $this->progress( 1 );
88 function killRow( $name ) {
90 $this->log( "DRY RUN: would delete bogus row '$name'" );
92 $this->log( "deleting bogus row '$name'" );
93 $db = wfGetDB( DB_MASTER
);
95 array( 'img_name' => $name ),
100 function filePath( $name ) {
101 if ( !isset( $this->repo
) ) {
102 $this->repo
= RepoGroup
::singleton()->getLocalRepo();
104 return $this->repo
->getRootDirectory() . '/' . $this->repo
->getHashPath( $name ) . $name;
107 function pokeFile( $orig, $new ) {
108 $path = $this->filePath( $orig );
109 if( !file_exists( $path ) ) {
110 $this->log( "missing file: $path" );
111 return $this->killRow( $orig );
114 $db = wfGetDB( DB_MASTER
);
118 while( $db->selectField( 'image', 'img_name', array( 'img_name' => $final ), __METHOD__
) ||
119 Title
::makeTitle( NS_IMAGE
, $final )->exists() ) {
120 $this->log( "Rename conflicts with '$final'..." );
122 $final = $this->appendTitle( $new, "_$version" );
125 $finalPath = $this->filePath( $final );
127 if( $this->dryrun
) {
128 $this->log( "DRY RUN: would rename $path to $finalPath" );
130 $this->log( "renaming $path to $finalPath" );
131 // XXX: should this use File::move()? FIXME?
133 $db->update( 'image',
134 array( 'img_name' => $final ),
135 array( 'img_name' => $orig ),
137 $db->update( 'oldimage',
138 array( 'oi_name' => $final ),
139 array( 'oi_name' => $orig ),
142 array( 'page_title' => $final ),
143 array( 'page_title' => $orig, 'page_namespace' => NS_IMAGE
),
145 $dir = dirname( $finalPath );
146 if( !file_exists( $dir ) ) {
147 if( !mkdir( $dir, 0777, true ) ) {
148 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
153 if( rename( $path, $finalPath ) ) {
156 $this->log( "RENAME FAILED" );
162 function appendTitle( $name, $suffix ) {
163 return preg_replace( '/^(.*)(\..*?)$/',
164 "\\1$suffix\\2", $name );
167 function buildSafeTitle( $name ) {
168 global $wgLegalTitleChars;
169 $x = preg_replace_callback(
170 "/([^$wgLegalTitleChars]|~)/",
171 array( $this, 'hexChar' ),
174 $test = Title
::makeTitleSafe( NS_IMAGE
, $x );
175 if( is_null( $test ) ||
$test->getDBkey() !== $x ) {
176 $this->log( "Unable to generate safe title from '$name', got '$x'" );
184 $wgUser->setName( 'Conversion script' );
185 $caps = new ImageCleanup( !isset( $options['fix'] ) );