42 'index' =>
'img_name',
43 'callback' =>
'processRow',
50 parent::__construct();
51 $this->
addDescription(
'Script to clean up broken, unparseable upload filenames' );
67 $cleaned = rawurldecode( $cleaned );
70 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
75 $cleaned = $contLang->checkTitleEncoding( $cleaned );
78 $cleaned = $contLang->normalize( $cleaned );
80 $title = Title::makeTitleSafe(
NS_FILE, $cleaned );
82 if ( $title ===
null ) {
83 $this->
output(
"page $source ($cleaned) is illegal.\n" );
84 $safe = $this->buildSafeTitle( $cleaned );
85 if ( $safe ===
false ) {
89 $this->pokeFile(
$source, $safe );
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 );
110 private function killRow( $name ) {
111 if ( $this->dryrun ) {
112 $this->
output(
"DRY RUN: would delete bogus row '$name'\n" );
114 $this->
output(
"deleting bogus row '$name'\n" );
116 $db->delete(
'image',
117 [
'img_name' => $name ],
126 private function filePath( $name ) {
127 if ( $this->repo ===
null ) {
131 return $this->repo->getRootDirectory() .
'/' . $this->repo->getHashPath( $name ) . $name;
134 private function imageExists( $name, $db ) {
135 return (
bool)$db->newSelectQueryBuilder()
138 ->where( [
'img_name' => $name ] )
139 ->caller( __METHOD__ )
143 private function pageExists( $name, $db ) {
144 return (
bool)$db->newSelectQueryBuilder()
149 'page_title' => $name,
151 ->caller( __METHOD__ )
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 );
175 $conflict = ( $this->imageExists( $final, $db ) ||
176 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
178 while ( $conflict ) {
179 $this->
output(
"Rename conflicts with '$final'...\n" );
181 $final = $this->appendTitle( $new,
"_$version" );
182 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
185 $finalPath = $this->filePath( $final );
187 if ( $this->dryrun ) {
188 $this->
output(
"DRY RUN: would rename $path to $finalPath\n" );
190 $this->
output(
"renaming $path to $finalPath\n" );
193 $db->update(
'image',
194 [
'img_name' => $final ],
195 [
'img_name' => $orig ],
197 $db->update(
'oldimage',
198 [
'oi_name' => $final ],
199 [
'oi_name' => $orig ],
202 [
'page_title' => $final ],
203 [
'page_title' => $orig,
'page_namespace' =>
NS_FILE ],
205 $dir = dirname( $finalPath );
206 if ( !file_exists( $dir ) ) {
208 $this->
output(
"RENAME FAILED, COULD NOT CREATE $dir" );
214 if ( rename(
$path, $finalPath ) ) {
217 $this->
error(
"RENAME FAILED" );
223 private function appendTitle( $name, $suffix ) {
224 return preg_replace(
'/^(.*)(\..*?)$/',
225 "\\1$suffix\\2", $name );
228 private function buildSafeTitle( $name ) {
229 $x = preg_replace_callback(
230 '/([^' . Title::legalChars() .
']|~)/',
231 [ $this,
'hexChar' ],
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'" );
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.