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