X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/mediawiki.git/blobdiff_plain/87219ebd28426c6d21cb545233ee52f5f7af7dfd..18a6620945d02687fbcfc4c27355d952fd748b41:/maintenance/cleanupImages.php diff --git a/maintenance/cleanupImages.php b/maintenance/cleanupImages.php index db13f4c9..b25b9bbe 100644 --- a/maintenance/cleanupImages.php +++ b/maintenance/cleanupImages.php @@ -1,5 +1,5 @@ * @ingroup Maintenance */ -require_once( dirname(__FILE__) . '/cleanupTable.inc' ); +require_once( dirname( __FILE__ ) . '/cleanupTable.inc' ); class ImageCleanup extends TableCleanup { protected $defaultParams = array( @@ -47,38 +48,38 @@ class ImageCleanup extends TableCleanup { global $wgContLang; $source = $row->img_name; - if( $source == '' ) { + if ( $source == '' ) { // Ye olde empty rows. Just kill them. $this->killRow( $source ); return $this->progress( 1 ); } - + $cleaned = $source; - + // About half of old bad image names have percent-codes $cleaned = rawurldecode( $cleaned ); // We also have some HTML entities there $cleaned = Sanitizer::decodeCharReferences( $cleaned ); - + // Some are old latin-1 $cleaned = $wgContLang->checkTitleEncoding( $cleaned ); - + // Many of remainder look like non-normalized unicode $cleaned = $wgContLang->normalize( $cleaned ); - + $title = Title::makeTitleSafe( NS_FILE, $cleaned ); - - if( is_null( $title ) ) { + + if ( is_null( $title ) ) { $this->output( "page $source ($cleaned) is illegal.\n" ); $safe = $this->buildSafeTitle( $cleaned ); - if( $safe === false ) + if ( $safe === false ) return $this->progress( 0 ); $this->pokeFile( $source, $safe ); return $this->progress( 1 ); } - if( $title->getDBkey() !== $source ) { + if ( $title->getDBkey() !== $source ) { $munged = $title->getDBkey(); $this->output( "page $source ($munged) doesn't match self.\n" ); $this->pokeFile( $source, $munged ); @@ -89,7 +90,7 @@ class ImageCleanup extends TableCleanup { } private function killRow( $name ) { - if( $this->dryrun ) { + if ( $this->dryrun ) { $this->output( "DRY RUN: would delete bogus row '$name'\n" ); } else { $this->output( "deleting bogus row '$name'\n" ); @@ -99,7 +100,7 @@ class ImageCleanup extends TableCleanup { __METHOD__ ); } } - + private function filePath( $name ) { if ( !isset( $this->repo ) ) { $this->repo = RepoGroup::singleton()->getLocalRepo(); @@ -114,14 +115,14 @@ class ImageCleanup extends TableCleanup { private function pageExists( $name, $db ) { return $db->selectField( 'page', '1', array( 'page_namespace' => NS_FILE, 'page_title' => $name ), __METHOD__ ); } - + private function pokeFile( $orig, $new ) { $path = $this->filePath( $orig ); - if( !file_exists( $path ) ) { + if ( !file_exists( $path ) ) { $this->output( "missing file: $path\n" ); return $this->killRow( $orig ); } - + $db = wfGetDB( DB_MASTER ); /* @@ -134,18 +135,18 @@ class ImageCleanup extends TableCleanup { $version = 0; $final = $new; $conflict = ( $this->imageExists( $final, $db ) || - ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) ); - - while( $conflict ) { + ( $this->pageExists( $orig, $db ) && $this->pageExists( $final, $db ) ) ); + + while ( $conflict ) { $this->output( "Rename conflicts with '$final'...\n" ); $version++; $final = $this->appendTitle( $new, "_$version" ); $conflict = ( $this->imageExists( $final, $db ) || $this->pageExists( $final, $db ) ); } - + $finalPath = $this->filePath( $final ); - - if( $this->dryrun ) { + + if ( $this->dryrun ) { $this->output( "DRY RUN: would rename $path to $finalPath\n" ); } else { $this->output( "renaming $path to $finalPath\n" ); @@ -164,14 +165,14 @@ class ImageCleanup extends TableCleanup { array( 'page_title' => $orig, 'page_namespace' => NS_FILE ), __METHOD__ ); $dir = dirname( $finalPath ); - if( !file_exists( $dir ) ) { - if( !wfMkdirParents( $dir ) ) { + if ( !file_exists( $dir ) ) { + if ( !wfMkdirParents( $dir ) ) { $this->log( "RENAME FAILED, COULD NOT CREATE $dir" ); $db->rollback(); return; } } - if( rename( $path, $finalPath ) ) { + if ( rename( $path, $finalPath ) ) { $db->commit(); } else { $this->error( "RENAME FAILED" ); @@ -191,16 +192,16 @@ class ImageCleanup extends TableCleanup { "/([^$wgLegalTitleChars]|~)/", array( $this, 'hexChar' ), $name ); - + $test = Title::makeTitleSafe( NS_FILE, $x ); - if( is_null( $test ) || $test->getDBkey() !== $x ) { + if ( is_null( $test ) || $test->getDBkey() !== $x ) { $this->error( "Unable to generate safe title from '$name', got '$x'" ); return false; } - + return $x; } } $maintClass = "ImageCleanup"; -require_once( DO_MAINTENANCE ); +require_once( RUN_MAINTENANCE_IF_MAIN );