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 // 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_IMAGE, $cleaned );
65
66 if( is_null( $title ) ) {
67 $this->log( "page $source ($cleaned) is illegal." );
68 $safe = $this->buildSafeTitle( $cleaned );
69 $this->pokeFile( $source, $safe );
70 return $this->progress( 1 );
71 }
72
73 if( $title->getDBkey() !== $source ) {
74 $munged = $title->getDBkey();
75 $this->log( "page $source ($munged) doesn't match self." );
76 $this->pokeFile( $source, $munged );
77 return $this->progress( 1 );
78 }
79
80 $this->progress( 0 );
81 }
82
83 function killRow( $name ) {
84 if( $this->dryrun ) {
85 $this->log( "DRY RUN: would delete bogus row '$name'" );
86 } else {
87 $this->log( "deleting bogus row '$name'" );
88 $db = wfGetDB( DB_MASTER );
89 $db->delete( 'image',
90 array( 'img_name' => $name ),
91 __METHOD__ );
92 }
93 }
94
95 function filePath( $name ) {
96 if ( !isset( $this->repo ) ) {
97 $this->repo = RepoGroup::singleton()->getLocalRepo();
98 }
99 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
100 }
101
102 function pokeFile( $orig, $new ) {
103 $path = $this->filePath( $orig );
104 if( !file_exists( $path ) ) {
105 $this->log( "missing file: $path" );
106 return $this->killRow( $orig );
107 }
108
109 $db = wfGetDB( DB_MASTER );
110 $version = 0;
111 $final = $new;
112
113 while( $db->selectField( 'image', 'img_name',
114 array( 'img_name' => $final ), __METHOD__ ) ) {
115 $this->log( "Rename conflicts with '$final'..." );
116 $version++;
117 $final = $this->appendTitle( $new, "_$version" );
118 }
119
120 $finalPath = $this->filePath( $final );
121
122 if( $this->dryrun ) {
123 $this->log( "DRY RUN: would rename $path to $finalPath" );
124 } else {
125 $this->log( "renaming $path to $finalPath" );
126 $db->begin();
127 $db->update( 'image',
128 array( 'img_name' => $final ),
129 array( 'img_name' => $orig ),
130 __METHOD__ );
131 $dir = dirname( $finalPath );
132 if( !file_exists( $dir ) ) {
133 if( !mkdir( $dir, 0777, true ) ) {
134 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
135 $db->rollback();
136 return;
137 }
138 }
139 if( rename( $path, $finalPath ) ) {
140 $db->commit();
141 } else {
142 $this->log( "RENAME FAILED" );
143 $db->rollback();
144 }
145 }
146 }
147
148 function appendTitle( $name, $suffix ) {
149 return preg_replace( '/^(.*)(\..*?)$/',
150 "\\1$suffix\\2", $name );
151 }
152
153 function buildSafeTitle( $name ) {
154 global $wgLegalTitleChars;
155 $x = preg_replace_callback(
156 "/([^$wgLegalTitleChars])/",
157 array( $this, 'hexChar' ),
158 $name );
159
160 $test = Title::makeTitleSafe( NS_IMAGE, $x );
161 if( is_null( $test ) || $test->getDBkey() !== $x ) {
162 $this->log( "Unable to generate safe title from '$name', got '$x'" );
163 return false;
164 }
165
166 return $x;
167 }
168 }
169
170 $wgUser->setName( 'Conversion script' );
171 $caps = new ImageCleanup( !isset( $options['fix'] ) );
172 $caps->cleanup();
173
174