]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/cleanupImages.php
MediaWiki 1.16.4
[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  * @ingroup Maintenance
29  */
30
31 require_once( dirname(__FILE__) . '/cleanupTable.inc' );
32
33 class ImageCleanup extends TableCleanup {
34         protected $defaultParams = array(
35                 'table' => 'image',
36                 'conds' => array(),
37                 'index' => 'img_name',
38                 'callback' => 'processRow',
39         );
40
41         public function __construct() {
42                 parent::__construct();
43                 $this->mDescription = "Script to clean up broken, unparseable upload filenames";
44         }
45
46         protected function processRow( $row ) {
47                 global $wgContLang;
48
49                 $source = $row->img_name;
50                 if( $source == '' ) {
51                         // Ye olde empty rows. Just kill them.
52                         $this->killRow( $source );
53                         return $this->progress( 1 );
54                 }
55                 
56                 $cleaned = $source;
57                 
58                 // About half of old bad image names have percent-codes
59                 $cleaned = rawurldecode( $cleaned );
60
61                 // We also have some HTML entities there
62                 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
63                 
64                 // Some are old latin-1
65                 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
66                 
67                 // Many of remainder look like non-normalized unicode
68                 $cleaned = $wgContLang->normalize( $cleaned );
69                 
70                 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
71                 
72                 if( is_null( $title ) ) {
73                         $this->output( "page $source ($cleaned) is illegal.\n" );
74                         $safe = $this->buildSafeTitle( $cleaned );
75                         if( $safe === false )
76                                 return $this->progress( 0 );
77                         $this->pokeFile( $source, $safe );
78                         return $this->progress( 1 );
79                 }
80
81                 if( $title->getDBkey() !== $source ) {
82                         $munged = $title->getDBkey();
83                         $this->output( "page $source ($munged) doesn't match self.\n" );
84                         $this->pokeFile( $source, $munged );
85                         return $this->progress( 1 );
86                 }
87
88                 $this->progress( 0 );
89         }
90
91         private function killRow( $name ) {
92                 if( $this->dryrun ) {
93                         $this->output( "DRY RUN: would delete bogus row '$name'\n" );
94                 } else {
95                         $this->output( "deleting bogus row '$name'\n" );
96                         $db = wfGetDB( DB_MASTER );
97                         $db->delete( 'image',
98                                 array( 'img_name' => $name ),
99                                 __METHOD__ );
100                 }
101         }
102         
103         private function filePath( $name ) {
104                 if ( !isset( $this->repo ) ) {
105                         $this->repo = RepoGroup::singleton()->getLocalRepo();
106                 }
107                 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
108         }
109
110         private function imageExists( $name, $db ) {
111                 return $db->selectField( 'image', '1', array( 'img_name' => $name ), __METHOD__ );
112         }
113
114         private function pageExists( $name, $db ) {
115                 return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ );
116         }
117         
118         private function pokeFile( $orig, $new ) {
119                 $path = $this->filePath( $orig );
120                 if( !file_exists( $path ) ) {
121                         $this->output( "missing file: $path\n" );
122                         return $this->killRow( $orig );
123                 }
124                 
125                 $db = wfGetDB( DB_MASTER );
126
127                 /*
128                  * To prevent key collisions in the update() statements below,
129                  * if the target title exists in the image table, or if both the
130                  * original and target titles exist in the page table, append
131                  * increasing version numbers until the target title exists in
132                  * neither.  (See also bug 16916.)
133                  */
134                 $version = 0;
135                 $final = $new;
136                 $conflict = ( $this->imageExists( $final, $db ) ||
137                               ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
138                 
139                 while( $conflict ) {
140                         $this->output( "Rename conflicts with '$final'...\n" );
141                         $version++;
142                         $final = $this->appendTitle( $new, "_$version" );
143                         $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
144                 }
145                 
146                 $finalPath = $this->filePath( $final );
147                 
148                 if( $this->dryrun ) {
149                         $this->output( "DRY RUN: would rename $path to $finalPath\n" );
150                 } else {
151                         $this->output( "renaming $path to $finalPath\n" );
152                         // XXX: should this use File::move()?  FIXME?
153                         $db->begin();
154                         $db->update( 'image',
155                                 array( 'img_name' => $final ),
156                                 array( 'img_name' => $orig ),
157                                 __METHOD__ );
158                         $db->update( 'oldimage',
159                                 array( 'oi_name' => $final ),
160                                 array( 'oi_name' => $orig ),
161                                 __METHOD__ );
162                         $db->update( 'page',
163                                 array( 'page_title' => $final ),
164                                 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
165                                 __METHOD__ );
166                         $dir = dirname( $finalPath );
167                         if( !file_exists( $dir ) ) {
168                                 if( !wfMkdirParents( $dir ) ) {
169                                         $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
170                                         $db->rollback();
171                                         return;
172                                 }
173                         }
174                         if( rename( $path, $finalPath ) ) {
175                                 $db->commit();
176                         } else {
177                                 $this->error( "RENAME FAILED" );
178                                 $db->rollback();
179                         }
180                 }
181         }
182
183         private function appendTitle( $name, $suffix ) {
184                 return preg_replace( '/^(.*)(\..*?)$/',
185                         "\\1$suffix\\2", $name );
186         }
187
188         private function buildSafeTitle( $name ) {
189                 global $wgLegalTitleChars;
190                 $x = preg_replace_callback(
191                         "/([^$wgLegalTitleChars]|~)/",
192                         array( $this, 'hexChar' ),
193                         $name );
194                 
195                 $test = Title::makeTitleSafe( NS_FILE, $x );
196                 if( is_null( $test ) || $test->getDBkey() !== $x ) {
197                         $this->error( "Unable to generate safe title from '$name', got '$x'" );
198                         return false;
199                 }
200                 
201                 return $x;
202         }
203 }
204
205 $maintClass = "ImageCleanup";
206 require_once( DO_MAINTENANCE );