Localisation updates from http://translatewiki.net.
[mediawiki.git] / maintenance / cleanupImages.php
blob0e0b6194a0ba6ede4f22725f4cc2aeda1c429b39
1 <?php
2 /**
3 * Clean up broken, unparseable upload filenames.
5 * Usage: php cleanupImages.php [--fix]
6 * Options:
7 * --fix Actually clean up titles; otherwise just checks for them
9 * Copyright © 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 * @file
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 require_once __DIR__ . '/cleanupTable.inc';
34 /**
35 * Maintenance script to clean up broken, unparseable upload filenames.
37 * @ingroup Maintenance
39 class ImageCleanup extends TableCleanup {
40 protected $defaultParams = array(
41 'table' => 'image',
42 'conds' => array(),
43 'index' => 'img_name',
44 'callback' => 'processRow',
47 public function __construct() {
48 parent::__construct();
49 $this->mDescription = "Script to clean up broken, unparseable upload filenames";
52 protected function processRow( $row ) {
53 global $wgContLang;
55 $source = $row->img_name;
56 if ( $source == '' ) {
57 // Ye olde empty rows. Just kill them.
58 $this->killRow( $source );
59 return $this->progress( 1 );
62 $cleaned = $source;
64 // About half of old bad image names have percent-codes
65 $cleaned = rawurldecode( $cleaned );
67 // We also have some HTML entities there
68 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
70 // Some are old latin-1
71 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
73 // Many of remainder look like non-normalized unicode
74 $cleaned = $wgContLang->normalize( $cleaned );
76 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
78 if ( is_null( $title ) ) {
79 $this->output( "page $source ($cleaned) is illegal.\n" );
80 $safe = $this->buildSafeTitle( $cleaned );
81 if ( $safe === false ) {
82 return $this->progress( 0 );
84 $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 );
92 return $this->progress( 1 );
95 return $this->progress( 0 );
98 /**
99 * @param $name string
101 private function killRow( $name ) {
102 if ( $this->dryrun ) {
103 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
104 } else {
105 $this->output( "deleting bogus row '$name'\n" );
106 $db = wfGetDB( DB_MASTER );
107 $db->delete( 'image',
108 array( 'img_name' => $name ),
109 __METHOD__ );
113 private function filePath( $name ) {
114 if ( !isset( $this->repo ) ) {
115 $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', array( 'img_name' => $name ), __METHOD__ );
124 private function pageExists( $name, $db ) {
125 return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ );
128 private function pokeFile( $orig, $new ) {
129 $path = $this->filePath( $orig );
130 if ( !file_exists( $path ) ) {
131 $this->output( "missing file: $path\n" );
132 $this->killRow( $orig );
133 return;
136 $db = wfGetDB( DB_MASTER );
139 * To prevent key collisions in the update() statements below,
140 * if the target title exists in the image table, or if both the
141 * original and target titles exist in the page table, append
142 * increasing version numbers until the target title exists in
143 * neither. (See also bug 16916.)
145 $version = 0;
146 $final = $new;
147 $conflict = ( $this->imageExists( $final, $db ) ||
148 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
150 while ( $conflict ) {
151 $this->output( "Rename conflicts with '$final'...\n" );
152 $version++;
153 $final = $this->appendTitle( $new, "_$version" );
154 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
157 $finalPath = $this->filePath( $final );
159 if ( $this->dryrun ) {
160 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
161 } else {
162 $this->output( "renaming $path to $finalPath\n" );
163 // @todo FIXME: Should this use File::move()?
164 $db->begin( __METHOD__ );
165 $db->update( 'image',
166 array( 'img_name' => $final ),
167 array( 'img_name' => $orig ),
168 __METHOD__ );
169 $db->update( 'oldimage',
170 array( 'oi_name' => $final ),
171 array( 'oi_name' => $orig ),
172 __METHOD__ );
173 $db->update( 'page',
174 array( 'page_title' => $final ),
175 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
176 __METHOD__ );
177 $dir = dirname( $finalPath );
178 if ( !file_exists( $dir ) ) {
179 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
180 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
181 $db->rollback( __METHOD__ );
182 return;
185 if ( rename( $path, $finalPath ) ) {
186 $db->commit( __METHOD__ );
187 } else {
188 $this->error( "RENAME FAILED" );
189 $db->rollback( __METHOD__ );
194 private function appendTitle( $name, $suffix ) {
195 return preg_replace( '/^(.*)(\..*?)$/',
196 "\\1$suffix\\2", $name );
199 private function buildSafeTitle( $name ) {
200 $x = preg_replace_callback(
201 '/([^' . Title::legalChars() . ']|~)/',
202 array( $this, 'hexChar' ),
203 $name );
205 $test = Title::makeTitleSafe( NS_FILE, $x );
206 if ( is_null( $test ) || $test->getDBkey() !== $x ) {
207 $this->error( "Unable to generate safe title from '$name', got '$x'" );
208 return false;
211 return $x;
215 $maintClass = "ImageCleanup";
216 require_once RUN_MAINTENANCE_IF_MAIN;