MediaWiki master
cleanupImages.php
Go to the documentation of this file.
1<?php
30
31require_once __DIR__ . '/TableCleanup.php';
32
39 protected $defaultParams = [
40 'table' => 'image',
41 'conds' => [],
42 'index' => 'img_name',
43 'callback' => 'processRow',
44 ];
45
47 private $repo;
48
49 public function __construct() {
50 parent::__construct();
51 $this->addDescription( 'Script to clean up broken, unparseable upload filenames' );
52 }
53
54 protected function processRow( $row ) {
55 $source = $row->img_name;
56 if ( $source == '' ) {
57 // Ye olde empty rows. Just kill them.
58 $this->killRow( $source );
59
60 $this->progress( 1 );
61 return;
62 }
63
64 $cleaned = $source;
65
66 // About half of old bad image names have percent-codes
67 $cleaned = rawurldecode( $cleaned );
68
69 // We also have some HTML entities there
70 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
71
72 $contLang = $this->getServiceContainer()->getContentLanguage();
73
74 // Some are old latin-1
75 $cleaned = $contLang->checkTitleEncoding( $cleaned );
76
77 // Many of remainder look like non-normalized unicode
78 $cleaned = $contLang->normalize( $cleaned );
79
80 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
81
82 if ( $title === null ) {
83 $this->output( "page $source ($cleaned) is illegal.\n" );
84 $safe = $this->buildSafeTitle( $cleaned );
85 if ( $safe === false ) {
86 $this->progress( 0 );
87 return;
88 }
89 $this->pokeFile( $source, $safe );
90
91 $this->progress( 1 );
92 return;
93 }
94
95 if ( $title->getDBkey() !== $source ) {
96 $munged = $title->getDBkey();
97 $this->output( "page $source ($munged) doesn't match self.\n" );
98 $this->pokeFile( $source, $munged );
99
100 $this->progress( 1 );
101 return;
102 }
103
104 $this->progress( 0 );
105 }
106
110 private function killRow( $name ) {
111 if ( $this->dryrun ) {
112 $this->output( "DRY RUN: would delete bogus row '$name'\n" );
113 } else {
114 $this->output( "deleting bogus row '$name'\n" );
115 $db = $this->getPrimaryDB();
116 $db->newDeleteQueryBuilder()
117 ->deleteFrom( 'image' )
118 ->where( [ 'img_name' => $name ] )
119 ->caller( __METHOD__ )
120 ->execute();
121 }
122 }
123
128 private function filePath( $name ) {
129 if ( $this->repo === null ) {
130 $this->repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
131 }
132
133 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
134 }
135
136 private function imageExists( $name, $db ) {
137 return (bool)$db->newSelectQueryBuilder()
138 ->select( '1' )
139 ->from( 'image' )
140 ->where( [ 'img_name' => $name ] )
141 ->caller( __METHOD__ )
142 ->fetchField();
143 }
144
145 private function pageExists( $name, $db ) {
146 return (bool)$db->newSelectQueryBuilder()
147 ->select( '1' )
148 ->from( 'page' )
149 ->where( [
150 'page_namespace' => NS_FILE,
151 'page_title' => $name,
152 ] )
153 ->caller( __METHOD__ )
154 ->fetchField();
155 }
156
157 private function pokeFile( $orig, $new ) {
158 $path = $this->filePath( $orig );
159 if ( !file_exists( $path ) ) {
160 $this->output( "missing file: $path\n" );
161 $this->killRow( $orig );
162
163 return;
164 }
165
166 $db = $this->getPrimaryDB();
167
168 /*
169 * To prevent key collisions in the update() statements below,
170 * if the target title exists in the image table, or if both the
171 * original and target titles exist in the page table, append
172 * increasing version numbers until the target title exists in
173 * neither. (See also T18916.)
174 */
175 $version = 0;
176 $final = $new;
177 $conflict = ( $this->imageExists( $final, $db ) ||
178 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
179
180 while ( $conflict ) {
181 $this->output( "Rename conflicts with '$final'...\n" );
182 $version++;
183 $final = $this->appendTitle( $new, "_$version" );
184 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
185 }
186
187 $finalPath = $this->filePath( $final );
188
189 if ( $this->dryrun ) {
190 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
191 } else {
192 $this->output( "renaming $path to $finalPath\n" );
193 // @todo FIXME: Should this use File::move()?
194 $this->beginTransaction( $db, __METHOD__ );
195 $db->newUpdateQueryBuilder()
196 ->update( 'image' )
197 ->set( [ 'img_name' => $final ] )
198 ->where( [ 'img_name' => $orig ] )
199 ->caller( __METHOD__ )
200 ->execute();
201 $db->newUpdateQueryBuilder()
202 ->update( 'oldimage' )
203 ->set( [ 'oi_name' => $final ] )
204 ->where( [ 'oi_name' => $orig ] )
205 ->caller( __METHOD__ )
206 ->execute();
207 $db->newUpdateQueryBuilder()
208 ->update( 'page' )
209 ->set( [ 'page_title' => $final ] )
210 ->where( [ 'page_title' => $orig, 'page_namespace' => NS_FILE ] )
211 ->caller( __METHOD__ )
212 ->execute();
213 $dir = dirname( $finalPath );
214 if ( !file_exists( $dir ) ) {
215 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
216 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
217 $this->rollbackTransaction( $db, __METHOD__ );
218
219 return;
220 }
221 }
222 if ( rename( $path, $finalPath ) ) {
223 $this->commitTransaction( $db, __METHOD__ );
224 } else {
225 $this->error( "RENAME FAILED" );
226 $this->rollbackTransaction( $db, __METHOD__ );
227 }
228 }
229 }
230
231 private function appendTitle( $name, $suffix ) {
232 return preg_replace( '/^(.*)(\..*?)$/',
233 "\\1$suffix\\2", $name );
234 }
235
236 private function buildSafeTitle( $name ) {
237 $x = preg_replace_callback(
238 '/([^' . Title::legalChars() . ']|~)/',
239 [ $this, 'hexChar' ],
240 $name );
241
242 $test = Title::makeTitleSafe( NS_FILE, $x );
243 if ( $test === null || $test->getDBkey() !== $x ) {
244 $this->error( "Unable to generate safe title from '$name', got '$x'" );
245
246 return false;
247 }
248
249 return $x;
250 }
251}
252
253$maintClass = CleanupImages::class;
254require_once RUN_MAINTENANCE_IF_MAIN;
const NS_FILE
Definition Defines.php:70
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
Maintenance script to clean up broken, unparseable upload filenames.
__construct()
Default constructor.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:49
error( $err, $die=0)
Throw an error to the user.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
rollbackTransaction(IDatabase $dbw, $fname)
Rollback the transaction on a DB handle.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
Represents a title within MediaWiki.
Definition Title.php:78
Generic class to cleanup a database table.
progress( $updated)
$maintClass
$source