Followup to r52671, fix redirection syntax to work with windows as well.
[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 pokeFile( $orig, $new ) {
105 $path = $this->filePath( $orig );
106 if( !file_exists( $path ) ) {
107 $this->output( "missing file: $path\n" );
108 return $this->killRow( $orig );
109 }
110
111 $db = wfGetDB( DB_MASTER );
112 $version = 0;
113 $final = $new;
114
115 while( $db->selectField( 'image', 'img_name', array( 'img_name' => $final ), __METHOD__ ) ||
116 Title::makeTitle( NS_FILE, $final )->exists() ) {
117 $this->output( "Rename conflicts with '$final'...\n" );
118 $version++;
119 $final = $this->appendTitle( $new, "_$version" );
120 }
121
122 $finalPath = $this->filePath( $final );
123
124 if( $this->dryrun ) {
125 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
126 } else {
127 $this->output( "renaming $path to $finalPath\n" );
128 // XXX: should this use File::move()? FIXME?
129 $db->begin();
130 $db->update( 'image',
131 array( 'img_name' => $final ),
132 array( 'img_name' => $orig ),
133 __METHOD__ );
134 $db->update( 'oldimage',
135 array( 'oi_name' => $final ),
136 array( 'oi_name' => $orig ),
137 __METHOD__ );
138 $db->update( 'page',
139 array( 'page_title' => $final ),
140 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
141 __METHOD__ );
142 $dir = dirname( $finalPath );
143 if( !file_exists( $dir ) ) {
144 if( !wfMkdirParents( $dir ) ) {
145 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
146 $db->rollback();
147 return;
148 }
149 }
150 if( rename( $path, $finalPath ) ) {
151 $db->commit();
152 } else {
153 $this->error( "RENAME FAILED" );
154 $db->rollback();
155 }
156 }
157 }
158
159 private function appendTitle( $name, $suffix ) {
160 return preg_replace( '/^(.*)(\..*?)$/',
161 "\\1$suffix\\2", $name );
162 }
163
164 private function buildSafeTitle( $name ) {
165 global $wgLegalTitleChars;
166 $x = preg_replace_callback(
167 "/([^$wgLegalTitleChars]|~)/",
168 array( $this, 'hexChar' ),
169 $name );
170
171 $test = Title::makeTitleSafe( NS_FILE, $x );
172 if( is_null( $test ) || $test->getDBkey() !== $x ) {
173 $this->error( "Unable to generate safe title from '$name', got '$x'" );
174 return false;
175 }
176
177 return $x;
178 }
179 }
180
181 $maintClass = "ImageCleanup";
182 require_once( DO_MAINTENANCE );