MediaWiki 1.42.0-rc.0
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->delete( 'image',
117 [ 'img_name' => $name ],
118 __METHOD__ );
119 }
120 }
121
126 private function filePath( $name ) {
127 if ( $this->repo === null ) {
128 $this->repo = $this->getServiceContainer()->getRepoGroup()->getLocalRepo();
129 }
130
131 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
132 }
133
134 private function imageExists( $name, $db ) {
135 return (bool)$db->newSelectQueryBuilder()
136 ->select( '1' )
137 ->from( 'image' )
138 ->where( [ 'img_name' => $name ] )
139 ->caller( __METHOD__ )
140 ->fetchField();
141 }
142
143 private function pageExists( $name, $db ) {
144 return (bool)$db->newSelectQueryBuilder()
145 ->select( '1' )
146 ->from( 'page' )
147 ->where( [
148 'page_namespace' => NS_FILE,
149 'page_title' => $name,
150 ] )
151 ->caller( __METHOD__ )
152 ->fetchField();
153 }
154
155 private function pokeFile( $orig, $new ) {
156 $path = $this->filePath( $orig );
157 if ( !file_exists( $path ) ) {
158 $this->output( "missing file: $path\n" );
159 $this->killRow( $orig );
160
161 return;
162 }
163
164 $db = $this->getPrimaryDB();
165
166 /*
167 * To prevent key collisions in the update() statements below,
168 * if the target title exists in the image table, or if both the
169 * original and target titles exist in the page table, append
170 * increasing version numbers until the target title exists in
171 * neither. (See also T18916.)
172 */
173 $version = 0;
174 $final = $new;
175 $conflict = ( $this->imageExists( $final, $db ) ||
176 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
177
178 while ( $conflict ) {
179 $this->output( "Rename conflicts with '$final'...\n" );
180 $version++;
181 $final = $this->appendTitle( $new, "_$version" );
182 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
183 }
184
185 $finalPath = $this->filePath( $final );
186
187 if ( $this->dryrun ) {
188 $this->output( "DRY RUN: would rename $path to $finalPath\n" );
189 } else {
190 $this->output( "renaming $path to $finalPath\n" );
191 // @todo FIXME: Should this use File::move()?
192 $this->beginTransaction( $db, __METHOD__ );
193 $db->update( 'image',
194 [ 'img_name' => $final ],
195 [ 'img_name' => $orig ],
196 __METHOD__ );
197 $db->update( 'oldimage',
198 [ 'oi_name' => $final ],
199 [ 'oi_name' => $orig ],
200 __METHOD__ );
201 $db->update( 'page',
202 [ 'page_title' => $final ],
203 [ 'page_title' => $orig, 'page_namespace' => NS_FILE ],
204 __METHOD__ );
205 $dir = dirname( $finalPath );
206 if ( !file_exists( $dir ) ) {
207 if ( !wfMkdirParents( $dir, null, __METHOD__ ) ) {
208 $this->output( "RENAME FAILED, COULD NOT CREATE $dir" );
209 $this->rollbackTransaction( $db, __METHOD__ );
210
211 return;
212 }
213 }
214 if ( rename( $path, $finalPath ) ) {
215 $this->commitTransaction( $db, __METHOD__ );
216 } else {
217 $this->error( "RENAME FAILED" );
218 $this->rollbackTransaction( $db, __METHOD__ );
219 }
220 }
221 }
222
223 private function appendTitle( $name, $suffix ) {
224 return preg_replace( '/^(.*)(\..*?)$/',
225 "\\1$suffix\\2", $name );
226 }
227
228 private function buildSafeTitle( $name ) {
229 $x = preg_replace_callback(
230 '/([^' . Title::legalChars() . ']|~)/',
231 [ $this, 'hexChar' ],
232 $name );
233
234 $test = Title::makeTitleSafe( NS_FILE, $x );
235 if ( $test === null || $test->getDBkey() !== $x ) {
236 $this->error( "Unable to generate safe title from '$name', got '$x'" );
237
238 return false;
239 }
240
241 return $x;
242 }
243}
244
245$maintClass = CleanupImages::class;
246require_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