Update.
[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 * @package MediaWiki
29 * @subpackage maintenance
30 */
31
32 require_once( 'commandLine.inc' );
33 require_once( 'cleanupTable.inc' );
34
35 class ImageCleanup extends TableCleanup {
36 function __construct( $dryrun = false ) {
37 parent::__construct( 'image', $dryrun );
38 }
39
40 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 // Some are old latin-1
56 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
57
58 // Many of remainder look like non-normalized unicode
59 $cleaned = UtfNormal::cleanUp( $cleaned );
60
61 $title = Title::makeTitleSafe( NS_IMAGE, $cleaned );
62
63 if( is_null( $title ) ) {
64 $this->log( "page $source ($cleaned) is illegal." );
65 $safe = $this->buildSafeTitle( $cleaned );
66 $this->pokeFile( $source, $safe );
67 return $this->progress( 1 );
68 }
69
70 if( $title->getDbKey() !== $source ) {
71 $munged = $title->getDbKey();
72 $this->log( "page $source ($munged) doesn't match self." );
73 $this->pokeFile( $source, $munged );
74 return $this->progress( 1 );
75 }
76
77 $this->progress( 0 );
78 }
79
80 function killRow( $name ) {
81 if( $this->dryrun ) {
82 $this->log( "DRY RUN: would delete bogus row '$name'" );
83 } else {
84 $this->log( "deleting bogus row '$name'" );
85 $db = wfGetDB( DB_MASTER );
86 $db->delete( 'image',
87 array( 'img_name' => $name ),
88 __METHOD__ );
89 }
90 }
91
92 function filePath( $name ) {
93 return wfImageDir( $name ) . "/$name";
94 }
95
96 function pokeFile( $orig, $new ) {
97 $path = $this->filePath( $orig );
98 if( !file_exists( $path ) ) {
99 $this->log( "missing file: $path" );
100 return $this->killRow( $orig );
101 }
102
103 $db = wfGetDB( DB_MASTER );
104 $version = 0;
105 $final = $new;
106
107 while( $db->selectField( 'image', 'img_name',
108 array( 'img_name' => $final ), __METHOD__ ) ) {
109 $this->log( "Rename conflicts with '$final'..." );
110 $version++;
111 $final = $this->appendTitle( $new, "_$version" );
112 }
113
114 $finalPath = $this->filePath( $final );
115
116 if( $this->dryrun ) {
117 $this->log( "DRY RUN: would rename $path to $finalPath" );
118 } else {
119 $this->log( "renaming $path to $finalPath" );
120 $db->begin();
121 $db->update( 'image',
122 array( 'img_name' => $final ),
123 array( 'img_name' => $orig ),
124 __METHOD__ );
125 $dir = dirname( $finalPath );
126 if( !file_exists( $dir ) ) {
127 if( !mkdir( $dir, 0777, true ) ) {
128 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
129 $db->rollback();
130 return;
131 }
132 }
133 if( rename( $path, $finalPath ) ) {
134 $db->commit();
135 } else {
136 $this->log( "RENAME FAILED" );
137 $db->rollback();
138 }
139 }
140 }
141
142 function appendTitle( $name, $suffix ) {
143 return preg_replace( '/^(.*)(\..*?)$/',
144 "\\1$suffix\\2", $name );
145 }
146
147 function buildSafeTitle( $name ) {
148 global $wgLegalTitleChars;
149 $x = preg_replace_callback(
150 "/([^$wgLegalTitleChars])/",
151 array( $this, 'hexChar' ),
152 $name );
153
154 $test = Title::makeTitleSafe( NS_IMAGE, $x );
155 if( is_null( $test ) || $test->getDbKey() !== $x ) {
156 $this->log( "Unable to generate safe title from '$name', got '$x'" );
157 return false;
158 }
159
160 return $x;
161 }
162 }
163
164 $wgUser->setName( 'Conversion script' );
165 $caps = new ImageCleanup( !isset( $options['fix'] ) );
166 $caps->cleanup();
167
168 ?>