Step 2 in NS_IMAGE -> NS_FILE transition (bug 44) (WARNING: huge commit).
[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( 'commandLine.inc' );
33 require_once( 'cleanupTable.inc' );
34
35 /**
36 * @ingroup Maintenance
37 */
38 class ImageCleanup extends TableCleanup {
39 function __construct( $dryrun = false ) {
40 parent::__construct( 'image', $dryrun );
41 }
42
43 function processPage( $row ) {
44 global $wgContLang;
45
46 $source = $row->img_name;
47 if( $source == '' ) {
48 // Ye olde empty rows. Just kill them.
49 $this->killRow( $source );
50 return $this->progress( 1 );
51 }
52
53 $cleaned = $source;
54
55 // About half of old bad image names have percent-codes
56 $cleaned = rawurldecode( $cleaned );
57
58 // We also have some HTML entities there
59 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
60
61 // Some are old latin-1
62 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
63
64 // Many of remainder look like non-normalized unicode
65 $cleaned = UtfNormal::cleanUp( $cleaned );
66
67 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
68
69 if( is_null( $title ) ) {
70 $this->log( "page $source ($cleaned) is illegal." );
71 $safe = $this->buildSafeTitle( $cleaned );
72 if( $safe === false )
73 return $this->progress( 0 );
74 $this->pokeFile( $source, $safe );
75 return $this->progress( 1 );
76 }
77
78 if( $title->getDBkey() !== $source ) {
79 $munged = $title->getDBkey();
80 $this->log( "page $source ($munged) doesn't match self." );
81 $this->pokeFile( $source, $munged );
82 return $this->progress( 1 );
83 }
84
85 $this->progress( 0 );
86 }
87
88 function killRow( $name ) {
89 if( $this->dryrun ) {
90 $this->log( "DRY RUN: would delete bogus row '$name'" );
91 } else {
92 $this->log( "deleting bogus row '$name'" );
93 $db = wfGetDB( DB_MASTER );
94 $db->delete( 'image',
95 array( 'img_name' => $name ),
96 __METHOD__ );
97 }
98 }
99
100 function filePath( $name ) {
101 if ( !isset( $this->repo ) ) {
102 $this->repo = RepoGroup::singleton()->getLocalRepo();
103 }
104 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
105 }
106
107 function pokeFile( $orig, $new ) {
108 $path = $this->filePath( $orig );
109 if( !file_exists( $path ) ) {
110 $this->log( "missing file: $path" );
111 return $this->killRow( $orig );
112 }
113
114 $db = wfGetDB( DB_MASTER );
115 $version = 0;
116 $final = $new;
117
118 while( $db->selectField( 'image', 'img_name', array( 'img_name' => $final ), __METHOD__ ) ||
119 Title::makeTitle( NS_FILE, $final )->exists() ) {
120 $this->log( "Rename conflicts with '$final'..." );
121 $version++;
122 $final = $this->appendTitle( $new, "_$version" );
123 }
124
125 $finalPath = $this->filePath( $final );
126
127 if( $this->dryrun ) {
128 $this->log( "DRY RUN: would rename $path to $finalPath" );
129 } else {
130 $this->log( "renaming $path to $finalPath" );
131 // XXX: should this use File::move()? FIXME?
132 $db->begin();
133 $db->update( 'image',
134 array( 'img_name' => $final ),
135 array( 'img_name' => $orig ),
136 __METHOD__ );
137 $db->update( 'oldimage',
138 array( 'oi_name' => $final ),
139 array( 'oi_name' => $orig ),
140 __METHOD__ );
141 $db->update( 'page',
142 array( 'page_title' => $final ),
143 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
144 __METHOD__ );
145 $dir = dirname( $finalPath );
146 if( !file_exists( $dir ) ) {
147 if( !wfMkdirParents( $dir ) ) {
148 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
149 $db->rollback();
150 return;
151 }
152 }
153 if( rename( $path, $finalPath ) ) {
154 $db->commit();
155 } else {
156 $this->log( "RENAME FAILED" );
157 $db->rollback();
158 }
159 }
160 }
161
162 function appendTitle( $name, $suffix ) {
163 return preg_replace( '/^(.*)(\..*?)$/',
164 "\\1$suffix\\2", $name );
165 }
166
167 function buildSafeTitle( $name ) {
168 global $wgLegalTitleChars;
169 $x = preg_replace_callback(
170 "/([^$wgLegalTitleChars]|~)/",
171 array( $this, 'hexChar' ),
172 $name );
173
174 $test = Title::makeTitleSafe( NS_FILE, $x );
175 if( is_null( $test ) || $test->getDBkey() !== $x ) {
176 $this->log( "Unable to generate safe title from '$name', got '$x'" );
177 return false;
178 }
179
180 return $x;
181 }
182 }
183
184 $wgUser->setName( 'Conversion script' );
185 $caps = new ImageCleanup( !isset( $options['fix'] ) );
186 $caps->cleanup();
187
188