* Standardised file description headers
[lhc/web/wiklou.git] / maintenance / cleanupImages.php
1 <?php
2 /**
3 * Script to clean up broken, unparseable upload filenames.
4 *
5 * Usage: php cleanupImages.php [--fix]
6 * Options:
7 * --fix Actually clean up titles; otherwise just checks for them
8 *
9 * Copyright (C) 2005-2006 Brion Vibber <brion@pobox.com>
10 * http://www.mediawiki.org/
11 *
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.
16 *
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.
21 *
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
26 *
27 * @file
28 * @author Brion Vibber <brion at pobox.com>
29 * @ingroup Maintenance
30 */
31
32 require_once( dirname( __FILE__ ) . '/cleanupTable.inc' );
33
34 class ImageCleanup extends TableCleanup {
35 protected $defaultParams = array(
36 'table' => 'image',
37 'conds' => array(),
38 'index' => 'img_name',
39 'callback' => 'processRow',
40 );
41
42 public function __construct() {
43 parent::__construct();
44 $this->mDescription = "Script to clean up broken, unparseable upload filenames";
45 }
46
47 protected function processRow( $row ) {
48 global $wgContLang;
49
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 );
55 }
56
57 $cleaned = $source;
58
59 // About half of old bad image names have percent-codes
60 $cleaned = rawurldecode( $cleaned );
61
62 // We also have some HTML entities there
63 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
64
65 // Some are old latin-1
66 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
67
68 // Many of remainder look like non-normalized unicode
69 $cleaned = $wgContLang->normalize( $cleaned );
70
71 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
72
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 );
80 }
81
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 );
87 }
88
89 $this->progress( 0 );
90 }
91
92 private function killRow( $name ) {
93 if ( $this->dryrun ) {
94 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
95 } else {
96 $this->output( "deleting bogus row '$name'\n" );
97 $db = wfGetDB( DB_MASTER );
98 $db->delete( 'image',
99 array( 'img_name' => $name ),
100 __METHOD__ );
101 }
102 }
103
104 private function filePath( $name ) {
105 if ( !isset( $this->repo ) ) {
106 $this->repo = RepoGroup::singleton()->getLocalRepo();
107 }
108 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
109 }
110
111 private function imageExists( $name, $db ) {
112 return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__ );
113 }
114
115 private function pageExists( $name, $db ) {
116 return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ );
117 }
118
119 private function pokeFile( $orig, $new ) {
120 $path = $this->filePath( $orig );
121 if ( !file_exists( $path ) ) {
122 $this->output( "missing file: $path\n" );
123 return $this->killRow( $orig );
124 }
125
126 $db = wfGetDB( DB_MASTER );
127
128 /*
129 * To prevent key collisions in the update() statements below,
130 * if the target title exists in the image table, or if both the
131 * original and target titles exist in the page table, append
132 * increasing version numbers until the target title exists in
133 * neither. (See also bug 16916.)
134 */
135 $version = 0;
136 $final = $new;
137 $conflict = ( $this->imageExists( $final, $db ) ||
138 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
139
140 while ( $conflict ) {
141 $this->output( "Rename conflicts with '$final'...\n" );
142 $version++;
143 $final = $this->appendTitle( $new, "_$version" );
144 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
145 }
146
147 $finalPath = $this->filePath( $final );
148
149 if ( $this->dryrun ) {
150 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
151 } else {
152 $this->output( "renaming $path to $finalPath\n" );
153 // XXX: should this use File::move()? FIXME?
154 $db->begin();
155 $db->update( 'image',
156 array( 'img_name' => $final ),
157 array( 'img_name' => $orig ),
158 __METHOD__ );
159 $db->update( 'oldimage',
160 array( 'oi_name' => $final ),
161 array( 'oi_name' => $orig ),
162 __METHOD__ );
163 $db->update( 'page',
164 array( 'page_title' => $final ),
165 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
166 __METHOD__ );
167 $dir = dirname( $finalPath );
168 if ( !file_exists( $dir ) ) {
169 if ( !wfMkdirParents( $dir ) ) {
170 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
171 $db->rollback();
172 return;
173 }
174 }
175 if ( rename( $path, $finalPath ) ) {
176 $db->commit();
177 } else {
178 $this->error( "RENAME FAILED" );
179 $db->rollback();
180 }
181 }
182 }
183
184 private function appendTitle( $name, $suffix ) {
185 return preg_replace( '/^(.*)(\..*?)$/',
186 "\\1$suffix\\2", $name );
187 }
188
189 private function buildSafeTitle( $name ) {
190 global $wgLegalTitleChars;
191 $x = preg_replace_callback(
192 "/([^$wgLegalTitleChars]|~)/",
193 array( $this, 'hexChar' ),
194 $name );
195
196 $test = Title::makeTitleSafe( NS_FILE, $x );
197 if ( is_null( $test ) || $test->getDBkey() !== $x ) {
198 $this->error( "Unable to generate safe title from '$name', got '$x'" );
199 return false;
200 }
201
202 return $x;
203 }
204 }
205
206 $maintClass = "ImageCleanup";
207 require_once( DO_MAINTENANCE );