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