46 'index' =>
'img_name',
47 'callback' =>
'processRow',
54 parent::__construct();
55 $this->
addDescription(
'Script to clean up broken, unparseable upload filenames' );
71 $cleaned = rawurldecode( $cleaned );
74 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
79 $cleaned = $contLang->checkTitleEncoding( $cleaned );
82 $cleaned = $contLang->normalize( $cleaned );
84 $title = Title::makeTitleSafe(
NS_FILE, $cleaned );
86 if ( $title ===
null ) {
87 $this->
output(
"page $source ($cleaned) is illegal.\n" );
88 $safe = $this->buildSafeTitle( $cleaned );
89 if ( $safe ===
false ) {
93 $this->pokeFile(
$source, $safe );
99 if ( $title->getDBkey() !==
$source ) {
100 $munged = $title->getDBkey();
101 $this->
output(
"page $source ($munged) doesn't match self.\n" );
102 $this->pokeFile(
$source, $munged );
114 private function killRow( $name ) {
115 if ( $this->dryrun ) {
116 $this->
output(
"DRY RUN: would delete bogus row '$name'\n" );
118 $this->
output(
"deleting bogus row '$name'\n" );
120 $db->newDeleteQueryBuilder()
121 ->deleteFrom(
'image' )
122 ->where( [
'img_name' => $name ] )
123 ->caller( __METHOD__ )
132 private function filePath( $name ) {
133 if ( $this->repo ===
null ) {
137 return $this->repo->getRootDirectory() .
'/' . $this->repo->getHashPath( $name ) . $name;
140 private function imageExists( $name, $db ) {
141 return (
bool)$db->newSelectQueryBuilder()
144 ->where( [
'img_name' => $name ] )
145 ->caller( __METHOD__ )
149 private function pageExists( $name, $db ) {
150 return (
bool)$db->newSelectQueryBuilder()
155 'page_title' => $name,
157 ->caller( __METHOD__ )
161 private function pokeFile( $orig, $new ) {
162 $path = $this->filePath( $orig );
163 if ( !file_exists(
$path ) ) {
164 $this->
output(
"missing file: $path\n" );
165 $this->killRow( $orig );
181 $conflict = ( $this->imageExists( $final, $db ) ||
182 ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) );
184 while ( $conflict ) {
185 $this->
output(
"Rename conflicts with '$final'...\n" );
187 $final = $this->appendTitle( $new,
"_$version" );
188 $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) );
191 $finalPath = $this->filePath( $final );
193 if ( $this->dryrun ) {
194 $this->
output(
"DRY RUN: would rename $path to $finalPath\n" );
196 $this->
output(
"renaming $path to $finalPath\n" );
199 $db->newUpdateQueryBuilder()
201 ->set( [
'img_name' => $final ] )
202 ->where( [
'img_name' => $orig ] )
203 ->caller( __METHOD__ )
205 $db->newUpdateQueryBuilder()
206 ->update(
'oldimage' )
207 ->set( [
'oi_name' => $final ] )
208 ->where( [
'oi_name' => $orig ] )
209 ->caller( __METHOD__ )
211 $db->newUpdateQueryBuilder()
213 ->set( [
'page_title' => $final ] )
214 ->where( [
'page_title' => $orig,
'page_namespace' =>
NS_FILE ] )
215 ->caller( __METHOD__ )
217 $dir = dirname( $finalPath );
218 if ( !file_exists( $dir ) ) {
220 $this->
output(
"RENAME FAILED, COULD NOT CREATE $dir" );
226 if ( rename(
$path, $finalPath ) ) {
229 $this->
error(
"RENAME FAILED" );
235 private function appendTitle( $name, $suffix ) {
236 return preg_replace(
'/^(.*)(\..*?)$/',
237 "\\1$suffix\\2", $name );
240 private function buildSafeTitle( $name ) {
241 $x = preg_replace_callback(
242 '/([^' . Title::legalChars() .
']|~)/',
243 [ $this,
'hexChar' ],
246 $test = Title::makeTitleSafe(
NS_FILE, $x );
247 if ( $test ===
null || $test->getDBkey() !== $x ) {
248 $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.