]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/cleanupImages.php
MediaWiki 1.11.0
[autoinstalls/mediawiki.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                 if ( !isset( $this->repo ) ) {
93                         $this->repo = RepoGroup::singleton()->getLocalRepo();
94                 }
95                 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
96         }
97         
98         function pokeFile( $orig, $new ) {
99                 $path = $this->filePath( $orig );
100                 if( !file_exists( $path ) ) {
101                         $this->log( "missing file: $path" );
102                         return $this->killRow( $orig );
103                 }
104                 
105                 $db = wfGetDB( DB_MASTER );
106                 $version = 0;
107                 $final = $new;
108                 
109                 while( $db->selectField( 'image', 'img_name',
110                         array( 'img_name' => $final ), __METHOD__ ) ) {
111                         $this->log( "Rename conflicts with '$final'..." );
112                         $version++;
113                         $final = $this->appendTitle( $new, "_$version" );
114                 }
115                 
116                 $finalPath = $this->filePath( $final );
117                 
118                 if( $this->dryrun ) {
119                         $this->log( "DRY RUN: would rename $path to $finalPath" );
120                 } else {
121                         $this->log( "renaming $path to $finalPath" );
122                         $db->begin();
123                         $db->update( 'image',
124                                 array( 'img_name' => $final ),
125                                 array( 'img_name' => $orig ),
126                                 __METHOD__ );
127                         $dir = dirname( $finalPath );
128                         if( !file_exists( $dir ) ) {
129                                 if( !mkdir( $dir, 0777, true ) ) {
130                                         $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
131                                         $db->rollback();
132                                         return;
133                                 }
134                         }
135                         if( rename( $path, $finalPath ) ) {
136                                 $db->commit();
137                         } else {
138                                 $this->log( "RENAME FAILED" );
139                                 $db->rollback();
140                         }
141                 }
142         }
143         
144         function appendTitle( $name, $suffix ) {
145                 return preg_replace( '/^(.*)(\..*?)$/',
146                         "\\1$suffix\\2", $name );
147         }
148         
149         function buildSafeTitle( $name ) {
150                 global $wgLegalTitleChars;
151                 $x = preg_replace_callback(
152                         "/([^$wgLegalTitleChars])/",
153                         array( $this, 'hexChar' ),
154                         $name );
155                 
156                 $test = Title::makeTitleSafe( NS_IMAGE, $x );
157                 if( is_null( $test ) || $test->getDbKey() !== $x ) {
158                         $this->log( "Unable to generate safe title from '$name', got '$x'" );
159                         return false;
160                 }
161                 
162                 return $x;
163         }
164 }
165
166 $wgUser->setName( 'Conversion script' );
167 $caps = new ImageCleanup( !isset( $options['fix'] ) );
168 $caps->cleanup();
169
170