41 'index' =>
'img_name',
42 'callback' =>
'processRow',
49 parent::__construct();
50 $this->
addDescription(
'Script to clean up broken, unparseable upload filenames' );
65 $cleaned = rawurldecode( $cleaned );
68 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
70 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
73 $cleaned = $contLang->checkTitleEncoding( $cleaned );
76 $cleaned = $contLang->normalize( $cleaned );
81 $this->
output(
"page $source ($cleaned) is illegal.\n" );
82 $safe = $this->buildSafeTitle( $cleaned );
83 if ( $safe ===
false ) {
86 $this->pokeFile(
$source, $safe );
92 $munged =
$title->getDBkey();
93 $this->
output(
"page $source ($munged) doesn't match self.\n" );
94 $this->pokeFile(
$source, $munged );
105 private function killRow( $name ) {
106 if ( $this->dryrun ) {
107 $this->
output(
"DRY RUN: would delete bogus row '$name'\n" );
109 $this->
output(
"deleting bogus row '$name'\n" );
111 $db->delete(
'image',
112 [
'img_name' => $name ],
121 private function filePath( $name ) {
122 if ( $this->repo ===
null ) {
123 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
126 return $this->repo->getRootDirectory() .
'/' . $this->repo->getHashPath( $name ) . $name;
129 private function imageExists( $name, $db ) {
130 return (
bool)$db->newSelectQueryBuilder()
133 ->where( [
'img_name' => $name ] )
134 ->caller( __METHOD__ )
138 private function pageExists( $name, $db ) {
139 return (
bool)$db->newSelectQueryBuilder()
144 'page_title' => $name,
146 ->caller( __METHOD__ )
150 private function pokeFile( $orig, $new ) {
151 $path = $this->filePath( $orig );
152 if ( !file_exists(
$path ) ) {
153 $this->
output(
"missing file: $path\n" );
154 $this->killRow( $orig );
170 $conflict = ( $this->imageExists( $final, $db ) ||
171 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
173 while ( $conflict ) {
174 $this->
output(
"Rename conflicts with '$final'...\n" );
176 $final = $this->appendTitle( $new,
"_$version" );
177 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
180 $finalPath = $this->filePath( $final );
182 if ( $this->dryrun ) {
183 $this->
output(
"DRY RUN: would rename $path to $finalPath\n" );
185 $this->
output(
"renaming $path to $finalPath\n" );
188 $db->update(
'image',
189 [
'img_name' => $final ],
190 [
'img_name' => $orig ],
192 $db->update(
'oldimage',
193 [
'oi_name' => $final ],
194 [
'oi_name' => $orig ],
197 [
'page_title' => $final ],
198 [
'page_title' => $orig,
'page_namespace' =>
NS_FILE ],
200 $dir = dirname( $finalPath );
201 if ( !file_exists( $dir ) ) {
203 $this->
output(
"RENAME FAILED, COULD NOT CREATE $dir" );
209 if ( rename(
$path, $finalPath ) ) {
212 $this->
error(
"RENAME FAILED" );
218 private function appendTitle( $name, $suffix ) {
219 return preg_replace(
'/^(.*)(\..*?)$/',
220 "\\1$suffix\\2", $name );
223 private function buildSafeTitle( $name ) {
224 $x = preg_replace_callback(
225 '/([^' . Title::legalChars() .
']|~)/',
226 [ $this,
'hexChar' ],
230 if ( $test ===
null || $test->getDBkey() !== $x ) {
231 $this->
error(
"Unable to generate safe title from '$name', got '$x'" );
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
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.
addDescription( $text)
Set the description text.
rollbackTransaction(IDatabase $dbw, $fname)
Rollback the transaction on a DB handle.
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.