Remove unused global $wgLoadScript
[mediawiki.git] / maintenance / cleanupImages.php
blob267061009ac7ade2cd940fed36b81906b10bb6aa
1 <?php
2 /**
3 * Script to 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 (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 * @file
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
32 require_once( dirname( __FILE__ ) . '/cleanupTable.inc' );
34 class ImageCleanup extends TableCleanup {
35 protected $defaultParams = array(
36 'table' => 'image',
37 'conds' => array(),
38 'index' => 'img_name',
39 'callback' => 'processRow',
42 public function __construct() {
43 parent::__construct();
44 $this->mDescription = "Script to clean up broken, unparseable upload filenames";
47 protected function processRow( $row ) {
48 global $wgContLang;
50 $source = $row->img_name;
51 if ( $source == '' ) {
52 // Ye olde empty rows. Just kill them.
53 $this->killRow( $source );
54 return $this->progress( 1 );
57 $cleaned = $source;
59 // About half of old bad image names have percent-codes
60 $cleaned = rawurldecode( $cleaned );
62 // We also have some HTML entities there
63 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
65 // Some are old latin-1
66 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
68 // Many of remainder look like non-normalized unicode
69 $cleaned = $wgContLang->normalize( $cleaned );
71 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
73 if ( is_null( $title ) ) {
74 $this->output( "page $source ($cleaned) is illegal.\n" );
75 $safe = $this->buildSafeTitle( $cleaned );
76 if ( $safe === false )
77 return $this->progress( 0 );
78 $this->pokeFile( $source, $safe );
79 return $this->progress( 1 );
82 if ( $title->getDBkey() !== $source ) {
83 $munged = $title->getDBkey();
84 $this->output( "page $source ($munged) doesn't match self.\n" );
85 $this->pokeFile( $source, $munged );
86 return $this->progress( 1 );
89 $this->progress( 0 );
92 /**
93 * @param $name string
95 private function killRow( $name ) {
96 if ( $this->dryrun ) {
97 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
98 } else {
99 $this->output( "deleting bogus row '$name'\n" );
100 $db = wfGetDB( DB_MASTER );
101 $db->delete( 'image',
102 array( 'img_name' => $name ),
103 __METHOD__ );
107 private function filePath( $name ) {
108 if ( !isset( $this->repo ) ) {
109 $this->repo = RepoGroup::singleton()->getLocalRepo();
111 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
114 private function imageExists( $name, $db ) {
115 return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__ );
118 private function pageExists( $name, $db ) {
119 return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ );
122 private function pokeFile( $orig, $new ) {
123 $path = $this->filePath( $orig );
124 if ( !file_exists( $path ) ) {
125 $this->output( "missing file: $path\n" );
126 return $this->killRow( $orig );
129 $db = wfGetDB( DB_MASTER );
132 * To prevent key collisions in the update() statements below,
133 * if the target title exists in the image table, or if both the
134 * original and target titles exist in the page table, append
135 * increasing version numbers until the target title exists in
136 * neither. (See also bug 16916.)
138 $version = 0;
139 $final = $new;
140 $conflict = ( $this->imageExists( $final, $db ) ||
141 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
143 while ( $conflict ) {
144 $this->output( "Rename conflicts with '$final'...\n" );
145 $version++;
146 $final = $this->appendTitle( $new, "_$version" );
147 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
150 $finalPath = $this->filePath( $final );
152 if ( $this->dryrun ) {
153 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
154 } else {
155 $this->output( "renaming $path to $finalPath\n" );
156 // @todo FIXME: Should this use File::move()?
157 $db->begin();
158 $db->update( 'image',
159 array( 'img_name' => $final ),
160 array( 'img_name' => $orig ),
161 __METHOD__ );
162 $db->update( 'oldimage',
163 array( 'oi_name' => $final ),
164 array( 'oi_name' => $orig ),
165 __METHOD__ );
166 $db->update( 'page',
167 array( 'page_title' => $final ),
168 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
169 __METHOD__ );
170 $dir = dirname( $finalPath );
171 if ( !file_exists( $dir ) ) {
172 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
173 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
174 $db->rollback();
175 return;
178 if ( rename( $path, $finalPath ) ) {
179 $db->commit();
180 } else {
181 $this->error( "RENAME FAILED" );
182 $db->rollback();
187 private function appendTitle( $name, $suffix ) {
188 return preg_replace( '/^(.*)(\..*?)$/',
189 "\\1$suffix\\2", $name );
192 private function buildSafeTitle( $name ) {
193 global $wgLegalTitleChars;
194 $x = preg_replace_callback(
195 "/([^$wgLegalTitleChars]|~)/",
196 array( $this, 'hexChar' ),
197 $name );
199 $test = Title::makeTitleSafe( NS_FILE, $x );
200 if ( is_null( $test ) || $test->getDBkey() !== $x ) {
201 $this->error( "Unable to generate safe title from '$name', got '$x'" );
202 return false;
205 return $x;
209 $maintClass = "ImageCleanup";
210 require_once( RUN_MAINTENANCE_IF_MAIN );