45 'index' =>
'img_name',
46 'callback' =>
'processRow',
53 parent::__construct();
54 $this->
addDescription(
'Script to clean up broken, unparseable upload filenames' );
70 $cleaned = rawurldecode( $cleaned );
73 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
78 $cleaned = $contLang->checkTitleEncoding( $cleaned );
81 $cleaned = $contLang->normalize( $cleaned );
83 $title = Title::makeTitleSafe(
NS_FILE, $cleaned );
85 if ( $title ===
null ) {
86 $this->
output(
"page $source ($cleaned) is illegal.\n" );
87 $safe = $this->buildSafeTitle( $cleaned );
88 if ( $safe ===
false ) {
92 $this->pokeFile(
$source, $safe );
98 if ( $title->getDBkey() !==
$source ) {
99 $munged = $title->getDBkey();
100 $this->
output(
"page $source ($munged) doesn't match self.\n" );
101 $this->pokeFile(
$source, $munged );
113 private function killRow( $name ) {
114 if ( $this->dryrun ) {
115 $this->
output(
"DRY RUN: would delete bogus row '$name'\n" );
117 $this->
output(
"deleting bogus row '$name'\n" );
119 $db->newDeleteQueryBuilder()
120 ->deleteFrom(
'image' )
121 ->where( [
'img_name' => $name ] )
122 ->caller( __METHOD__ )
131 private function filePath( $name ) {
132 if ( $this->repo ===
null ) {
136 return $this->repo->getRootDirectory() .
'/' . $this->repo->getHashPath( $name ) . $name;
139 private function imageExists( $name, $db ) {
140 return (
bool)$db->newSelectQueryBuilder()
143 ->where( [
'img_name' => $name ] )
144 ->caller( __METHOD__ )
148 private function pageExists( $name, $db ) {
149 return (
bool)$db->newSelectQueryBuilder()
154 'page_title' => $name,
156 ->caller( __METHOD__ )
160 private function pokeFile( $orig, $new ) {
161 $path = $this->filePath( $orig );
162 if ( !file_exists(
$path ) ) {
163 $this->
output(
"missing file: $path\n" );
164 $this->killRow( $orig );
180 $conflict = ( $this->imageExists( $final, $db ) ||
181 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
183 while ( $conflict ) {
184 $this->
output(
"Rename conflicts with '$final'...\n" );
186 $final = $this->appendTitle( $new,
"_$version" );
187 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
190 $finalPath = $this->filePath( $final );
192 if ( $this->dryrun ) {
193 $this->
output(
"DRY RUN: would rename $path to $finalPath\n" );
195 $this->
output(
"renaming $path to $finalPath\n" );
198 $db->newUpdateQueryBuilder()
200 ->set( [
'img_name' => $final ] )
201 ->where( [
'img_name' => $orig ] )
202 ->caller( __METHOD__ )
204 $db->newUpdateQueryBuilder()
205 ->update(
'oldimage' )
206 ->set( [
'oi_name' => $final ] )
207 ->where( [
'oi_name' => $orig ] )
208 ->caller( __METHOD__ )
210 $db->newUpdateQueryBuilder()
212 ->set( [
'page_title' => $final ] )
213 ->where( [
'page_title' => $orig,
'page_namespace' =>
NS_FILE ] )
214 ->caller( __METHOD__ )
216 $dir = dirname( $finalPath );
217 if ( !file_exists( $dir ) ) {
219 $this->
output(
"RENAME FAILED, COULD NOT CREATE $dir" );
225 if ( rename(
$path, $finalPath ) ) {
228 $this->
error(
"RENAME FAILED" );
234 private function appendTitle( $name, $suffix ) {
235 return preg_replace(
'/^(.*)(\..*?)$/',
236 "\\1$suffix\\2", $name );
239 private function buildSafeTitle( $name ) {
240 $x = preg_replace_callback(
241 '/([^' . Title::legalChars() .
']|~)/',
242 [ $this,
'hexChar' ],
245 $test = Title::makeTitleSafe(
NS_FILE, $x );
246 if ( $test ===
null || $test->getDBkey() !== $x ) {
247 $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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
rollbackTransaction(IDatabase $dbw, $fname)
Rollback the transaction on a DB handle.