00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 require_once( 'commandLine.inc' );
00033 require_once( 'cleanupTable.inc' );
00034
00038 class ImageCleanup extends TableCleanup {
00039 function __construct( $dryrun = false ) {
00040 parent::__construct( 'image', $dryrun );
00041 }
00042
00043 function processPage( $row ) {
00044 global $wgContLang;
00045
00046 $source = $row->img_name;
00047 if( $source == '' ) {
00048
00049 $this->killRow( $source );
00050 return $this->progress( 1 );
00051 }
00052
00053 $cleaned = $source;
00054
00055
00056 $cleaned = rawurldecode( $cleaned );
00057
00058
00059 $cleaned = Sanitizer::decodeCharReferences( $cleaned );
00060
00061
00062 $cleaned = $wgContLang->checkTitleEncoding( $cleaned );
00063
00064
00065 $cleaned = UtfNormal::cleanUp( $cleaned );
00066
00067 $title = Title::makeTitleSafe( NS_FILE, $cleaned );
00068
00069 if( is_null( $title ) ) {
00070 $this->log( "page $source ($cleaned) is illegal." );
00071 $safe = $this->buildSafeTitle( $cleaned );
00072 if( $safe === false )
00073 return $this->progress( 0 );
00074 $this->pokeFile( $source, $safe );
00075 return $this->progress( 1 );
00076 }
00077
00078 if( $title->getDBkey() !== $source ) {
00079 $munged = $title->getDBkey();
00080 $this->log( "page $source ($munged) doesn't match self." );
00081 $this->pokeFile( $source, $munged );
00082 return $this->progress( 1 );
00083 }
00084
00085 $this->progress( 0 );
00086 }
00087
00088 function killRow( $name ) {
00089 if( $this->dryrun ) {
00090 $this->log( "DRY RUN: would delete bogus row '$name'" );
00091 } else {
00092 $this->log( "deleting bogus row '$name'" );
00093 $db = wfGetDB( DB_MASTER );
00094 $db->delete( 'image',
00095 array( 'img_name' => $name ),
00096 __METHOD__ );
00097 }
00098 }
00099
00100 function filePath( $name ) {
00101 if ( !isset( $this->repo ) ) {
00102 $this->repo = RepoGroup::singleton()->getLocalRepo();
00103 }
00104 return $this->repo->getRootDirectory() . '/' . $this->repo->getHashPath( $name ) . $name;
00105 }
00106
00107 function pokeFile( $orig, $new ) {
00108 $path = $this->filePath( $orig );
00109 if( !file_exists( $path ) ) {
00110 $this->log( "missing file: $path" );
00111 return $this->killRow( $orig );
00112 }
00113
00114 $db = wfGetDB( DB_MASTER );
00115 $version = 0;
00116 $final = $new;
00117
00118 while( $db->selectField( 'image', 'img_name', array( 'img_name' => $final ), __METHOD__ ) ||
00119 Title::makeTitle( NS_FILE, $final )->exists() ) {
00120 $this->log( "Rename conflicts with '$final'..." );
00121 $version++;
00122 $final = $this->appendTitle( $new, "_$version" );
00123 }
00124
00125 $finalPath = $this->filePath( $final );
00126
00127 if( $this->dryrun ) {
00128 $this->log( "DRY RUN: would rename $path to $finalPath" );
00129 } else {
00130 $this->log( "renaming $path to $finalPath" );
00131
00132 $db->begin();
00133 $db->update( 'image',
00134 array( 'img_name' => $final ),
00135 array( 'img_name' => $orig ),
00136 __METHOD__ );
00137 $db->update( 'oldimage',
00138 array( 'oi_name' => $final ),
00139 array( 'oi_name' => $orig ),
00140 __METHOD__ );
00141 $db->update( 'page',
00142 array( 'page_title' => $final ),
00143 array( 'page_title' => $orig, 'page_namespace' => NS_FILE ),
00144 __METHOD__ );
00145 $dir = dirname( $finalPath );
00146 if( !file_exists( $dir ) ) {
00147 if( !wfMkdirParents( $dir ) ) {
00148 $this->log( "RENAME FAILED, COULD NOT CREATE $dir" );
00149 $db->rollback();
00150 return;
00151 }
00152 }
00153 if( rename( $path, $finalPath ) ) {
00154 $db->commit();
00155 } else {
00156 $this->log( "RENAME FAILED" );
00157 $db->rollback();
00158 }
00159 }
00160 }
00161
00162 function appendTitle( $name, $suffix ) {
00163 return preg_replace( '/^(.*)(\..*?)$/',
00164 "\\1$suffix\\2", $name );
00165 }
00166
00167 function buildSafeTitle( $name ) {
00168 global $wgLegalTitleChars;
00169 $x = preg_replace_callback(
00170 "/([^$wgLegalTitleChars]|~)/",
00171 array( $this, 'hexChar' ),
00172 $name );
00173
00174 $test = Title::makeTitleSafe( NS_FILE, $x );
00175 if( is_null( $test ) || $test->getDBkey() !== $x ) {
00176 $this->log( "Unable to generate safe title from '$name', got '$x'" );
00177 return false;
00178 }
00179
00180 return $x;
00181 }
00182 }
00183
00184 $wgUser->setName( 'Conversion script' );
00185 $caps = new ImageCleanup( !isset( $options['fix'] ) );
00186 $caps->cleanup();
00187
00188