Don't needlessly shy from reuniting lost files with their description pages.
[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 $has_page = Title::makeTitle( NS_FILE, $orig )->exists();
118
119 while( true ) {
120 $test = $db->selectField( 'image', 'img_name', array( 'img_name' => $final ), __METHOD__ );
121 $conflict = ( $test !== false );
122
123 if( !$conflict && ( $has_page || $version > 0 ) ) {
124 // If the file has a description page, don't try to move it to a
125 // title that already has one. But if we have a misnamed file
126 // with no corresponding page, and there just happens to exist a
127 // page with no file at the corrected title, it's probably not a
128 // coincidence and we shouldn't shy from reuniting them.
129 $conflict = Title::makeTitle( NS_FILE, $final )->exists();
130 }
131 if( !$conflict ) {
132 break;
133 }
134 $this->log( "Rename conflicts with '$final'..." );
135 $version++;
136 $final = $this->appendTitle( $new, "_$version" );
137 }
138
139 $finalPath = $this->filePath( $final );
140
141 if( $this->dryrun ) {
142 $this->log( "DRY RUN: would rename $path to $finalPath" );
143 } else {
144 $this->log( "renaming $path to $finalPath" );
145 // XXX: should this use File::move()? FIXME?
146 $db->begin();
147 $db->update( 'image',
148 array( 'img_name' => $final ),
149 array( 'img_name' => $orig ),
150 __METHOD__ );
151 $db->update( 'oldimage',
152 array( 'oi_name' => $final ),
153 array( 'oi_name' => $orig ),
154 __METHOD__ );
155 $db->update( 'page',
156 array( 'page_title' => $final ),
157 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
158 __METHOD__ );
159 $dir = dirname( $finalPath );
160 if( !file_exists( $dir ) ) {
161 if( !wfMkdirParents( $dir ) ) {
162 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
163 $db->rollback();
164 return;
165 }
166 }
167 if( rename( $path, $finalPath ) ) {
168 $db->commit();
169 } else {
170 $this->log( "RENAME FAILED" );
171 $db->rollback();
172 }
173 }
174 }
175
176 function appendTitle( $name, $suffix ) {
177 return preg_replace( '/^(.*)(\..*?)$/',
178 "\\1$suffix\\2", $name );
179 }
180
181 function buildSafeTitle( $name ) {
182 global $wgLegalTitleChars;
183 $x = preg_replace_callback(
184 "/([^$wgLegalTitleChars]|~)/",
185 array( $this, 'hexChar' ),
186 $name );
187
188 $test = Title::makeTitleSafe( NS_FILE, $x );
189 if( is_null( $test ) || $test->getDBkey() !== $x ) {
190 $this->log( "Unable to generate safe title from '$name', got '$x'" );
191 return false;
192 }
193
194 return $x;
195 }
196 }
197
198 $wgUser->setName( 'Conversion script' );
199 $caps = new ImageCleanup( !isset( $options['fix'] ) );
200 $caps->cleanup();
201
202