Merge "Make update.php file executable"
[mediawiki.git] / maintenance / cleanupImages.php
blob915a2c0801a6483f6abe19afdb2cabcda4c88646
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 * https://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 );
60 return $this->progress( 1 );
63 $cleaned = $source;
65 // About half of old bad image names have percent-codes
66 $cleaned = rawurldecode( $cleaned );
68 // We also have some HTML entities there
69 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
71 // Some are old latin-1
72 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
74 // Many of remainder look like non-normalized unicode
75 $cleaned = $wgContLang->normalize( $cleaned );
77 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
79 if ( is_null( $title ) ) {
80 $this->output( "page $source ($cleaned) is illegal.\n" );
81 $safe = $this->buildSafeTitle( $cleaned );
82 if ( $safe === false ) {
83 return $this->progress( 0 );
85 $this->pokeFile( $source, $safe );
87 return $this->progress( 1 );
90 if ( $title->getDBkey() !== $source ) {
91 $munged = $title->getDBkey();
92 $this->output( "page $source ($munged) doesn't match self.\n" );
93 $this->pokeFile( $source, $munged );
95 return $this->progress( 1 );
98 return $this->progress( 0 );
102 * @param string $name
104 private function killRow( $name ) {
105 if ( $this->dryrun ) {
106 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
107 } else {
108 $this->output( "deleting bogus row '$name'\n" );
109 $db = wfGetDB( DB_MASTER );
110 $db->delete( 'image',
111 array( 'img_name' => $name ),
112 __METHOD__ );
116 private function filePath( $name ) {
117 if ( !isset( $this->repo ) ) {
118 $this->repo = RepoGroup::singleton()->getLocalRepo();
121 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
124 private function imageExists( $name, $db ) {
125 return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__ );
128 private function pageExists( $name, $db ) {
129 return $db->selectField(
130 'page',
131 '1',
132 array( 'page_namespace' => NS_FILE, 'page_title' => $name ),
133 __METHOD__
137 private function pokeFile( $orig, $new ) {
138 $path = $this->filePath( $orig );
139 if ( !file_exists( $path ) ) {
140 $this->output( "missing file: $path\n" );
141 $this->killRow( $orig );
143 return;
146 $db = wfGetDB( DB_MASTER );
149 * To prevent key collisions in the update() statements below,
150 * if the target title exists in the image table, or if both the
151 * original and target titles exist in the page table, append
152 * increasing version numbers until the target title exists in
153 * neither. (See also bug 16916.)
155 $version = 0;
156 $final = $new;
157 $conflict = ( $this->imageExists( $final, $db ) ||
158 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
160 while ( $conflict ) {
161 $this->output( "Rename conflicts with '$final'...\n" );
162 $version++;
163 $final = $this->appendTitle( $new, "_$version" );
164 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
167 $finalPath = $this->filePath( $final );
169 if ( $this->dryrun ) {
170 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
171 } else {
172 $this->output( "renaming $path to $finalPath\n" );
173 // @todo FIXME: Should this use File::move()?
174 $db->begin( __METHOD__ );
175 $db->update( 'image',
176 array( 'img_name' => $final ),
177 array( 'img_name' => $orig ),
178 __METHOD__ );
179 $db->update( 'oldimage',
180 array( 'oi_name' => $final ),
181 array( 'oi_name' => $orig ),
182 __METHOD__ );
183 $db->update( 'page',
184 array( 'page_title' => $final ),
185 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
186 __METHOD__ );
187 $dir = dirname( $finalPath );
188 if ( !file_exists( $dir ) ) {
189 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
190 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
191 $db->rollback( __METHOD__ );
193 return;
196 if ( rename( $path, $finalPath ) ) {
197 $db->commit( __METHOD__ );
198 } else {
199 $this->error( "RENAME FAILED" );
200 $db->rollback( __METHOD__ );
205 private function appendTitle( $name, $suffix ) {
206 return preg_replace( '/^(.*)(\..*?)$/',
207 "\\1$suffix\\2", $name );
210 private function buildSafeTitle( $name ) {
211 $x = preg_replace_callback(
212 '/([^' . Title::legalChars() . ']|~)/',
213 array( $this, 'hexChar' ),
214 $name );
216 $test = Title::makeTitleSafe( NS_FILE, $x );
217 if ( is_null( $test ) || $test->getDBkey() !== $x ) {
218 $this->error( "Unable to generate safe title from '$name', got '$x'" );
220 return false;
223 return $x;
227 $maintClass = "ImageCleanup";
228 require_once RUN_MAINTENANCE_IF_MAIN;