42 'index' =>
'img_name',
43 'callback' =>
'processRow',
50 parent::__construct();
51 $this->
addDescription(
'Script to clean up broken, unparseable upload filenames' );
66 $cleaned = rawurldecode( $cleaned );
69 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
71 $contLang = MediaWikiServices::getInstance()->getContentLanguage();
74 $cleaned = $contLang->checkTitleEncoding( $cleaned );
77 $cleaned = $contLang->normalize( $cleaned );
82 $this->
output(
"page $source ($cleaned) is illegal.\n" );
83 $safe = $this->buildSafeTitle( $cleaned );
84 if ( $safe ===
false ) {
87 $this->pokeFile(
$source, $safe );
93 $munged =
$title->getDBkey();
94 $this->
output(
"page $source ($munged) doesn't match self.\n" );
95 $this->pokeFile(
$source, $munged );
106 private function killRow( $name ) {
107 if ( $this->dryrun ) {
108 $this->
output(
"DRY RUN: would delete bogus row '$name'\n" );
110 $this->
output(
"deleting bogus row '$name'\n" );
112 $db->delete(
'image',
113 [
'img_name' => $name ],
122 private function filePath( $name ) {
123 if ( $this->repo ===
null ) {
124 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
127 return $this->repo->getRootDirectory() .
'/' . $this->repo->getHashPath( $name ) . $name;
130 private function imageExists( $name, $db ) {
131 return (
bool)$db->newSelectQueryBuilder()
134 ->where( [
'img_name' => $name ] )
135 ->caller( __METHOD__ )
139 private function pageExists( $name, $db ) {
140 return (
bool)$db->newSelectQueryBuilder()
145 'page_title' => $name,
147 ->caller( __METHOD__ )
151 private function pokeFile( $orig, $new ) {
152 $path = $this->filePath( $orig );
153 if ( !file_exists(
$path ) ) {
154 $this->
output(
"missing file: $path\n" );
155 $this->killRow( $orig );
171 $conflict = ( $this->imageExists( $final, $db ) ||
172 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
174 while ( $conflict ) {
175 $this->
output(
"Rename conflicts with '$final'...\n" );
177 $final = $this->appendTitle( $new,
"_$version" );
178 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
181 $finalPath = $this->filePath( $final );
183 if ( $this->dryrun ) {
184 $this->
output(
"DRY RUN: would rename $path to $finalPath\n" );
186 $this->
output(
"renaming $path to $finalPath\n" );
189 $db->update(
'image',
190 [
'img_name' => $final ],
191 [
'img_name' => $orig ],
193 $db->update(
'oldimage',
194 [
'oi_name' => $final ],
195 [
'oi_name' => $orig ],
198 [
'page_title' => $final ],
199 [
'page_title' => $orig,
'page_namespace' =>
NS_FILE ],
201 $dir = dirname( $finalPath );
202 if ( !file_exists( $dir ) ) {
204 $this->
output(
"RENAME FAILED, COULD NOT CREATE $dir" );
210 if ( rename(
$path, $finalPath ) ) {
213 $this->
error(
"RENAME FAILED" );
219 private function appendTitle( $name, $suffix ) {
220 return preg_replace(
'/^(.*)(\..*?)$/',
221 "\\1$suffix\\2", $name );
224 private function buildSafeTitle( $name ) {
225 $x = preg_replace_callback(
226 '/([^' . Title::legalChars() .
']|~)/',
227 [ $this,
'hexChar' ],
230 $test = Title::makeTitleSafe(
NS_FILE, $x );
231 if ( $test ===
null || $test->getDBkey() !== $x ) {
232 $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.