MediaWiki
REL1_20
|
00001 <?php 00030 require_once( __DIR__ . '/Maintenance.php' ); 00031 00037 class RefreshImageMetadata extends Maintenance { 00038 00042 protected $dbw; 00043 00044 function __construct() { 00045 parent::__construct(); 00046 00047 $this->mDescription = 'Script to update image metadata records'; 00048 $this->setBatchSize( 200 ); 00049 00050 $this->addOption( 'force', 'Reload metadata from file even if the metadata looks ok', false, false, 'f' ); 00051 $this->addOption( 'broken-only', 'Only fix really broken records, leave old but still compatible records alone.' ); 00052 $this->addOption( 'verbose', 'Output extra information about each upgraded/non-upgraded file.', false, false, 'v' ); 00053 $this->addOption( 'start', 'Name of file to start with', false, true ); 00054 $this->addOption( 'end', 'Name of file to end with', false, true ); 00055 00056 $this->addOption( 'mime', '(Inefficient!) Only refresh files with this mime type. Can accept wild-card image/*' , false, true ); 00057 $this->addOption( 'metadata-contains', '(Inefficient!) Only refresh files where the img_metadata field contains this string. Can be used if its known a specific property was being extracted incorrectly.', false, true ); 00058 00059 } 00060 00061 public function execute() { 00062 $force = $this->hasOption( 'force' ); 00063 $brokenOnly = $this->hasOption( 'broken-only' ); 00064 $verbose = $this->hasOption( 'verbose' ); 00065 $start = $this->getOption( 'start', false ); 00066 $this->setupParameters( $force, $brokenOnly ); 00067 00068 $upgraded = 0; 00069 $leftAlone = 0; 00070 $error = 0; 00071 00072 $dbw = wfGetDB( DB_MASTER ); 00073 if ( $this->mBatchSize <= 0 ) { 00074 $this->error( "Batch size is too low...", 12 ); 00075 } 00076 00077 $repo = RepoGroup::singleton()->getLocalRepo(); 00078 $conds = $this->getConditions( $dbw ); 00079 00080 // For the WHERE img_name > 'foo' condition that comes after doing a batch 00081 $conds2 = array(); 00082 if ( $start !== false ) { 00083 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start ); 00084 } 00085 00086 $options = array( 00087 'LIMIT' => $this->mBatchSize, 00088 'ORDER BY' => 'img_name ASC', 00089 ); 00090 00091 do { 00092 $res = $dbw->select( 00093 'image', 00094 '*', 00095 array_merge( $conds, $conds2 ), 00096 __METHOD__, 00097 $options 00098 ); 00099 00100 if ( $res->numRows() > 0 ) { 00101 $row1 = $res->current(); 00102 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n"); 00103 $res->rewind(); 00104 } else { 00105 $this->error( "No images to process.", 4 ); 00106 } 00107 00108 foreach ( $res as $row ) { 00109 $file = $repo->newFileFromRow( $row ); 00110 if ( $file->getUpgraded() ) { 00111 // File was upgraded. 00112 $upgraded++; 00113 $newLength = strlen( $file->getMetadata() ); 00114 $oldLength = strlen( $row->img_metadata ); 00115 if ( $newLength < $oldLength - 5 ) { 00116 // If after updating, the metadata is smaller then 00117 // what it was before, that's probably not a good thing 00118 // because we extract more data with time, not less. 00119 // Thus this probably indicates an error of some sort, 00120 // or at the very least is suspicious. Have the - 5 just 00121 // to weed out any inconsequential changes. 00122 $error++; 00123 $this->output( "Warning: File:{$row->img_name} used to have " . 00124 "$oldLength bytes of metadata but now has $newLength bytes.\n" ); 00125 } elseif ( $verbose ) { 00126 $this->output("Refreshed File:{$row->img_name}.\n" ); 00127 } 00128 } else { 00129 $leftAlone++; 00130 if ( $force ) { 00131 $file->upgradeRow(); 00132 $newLength = strlen( $file->getMetadata() ); 00133 $oldLength = strlen( $row->img_metadata ); 00134 if ( $newLength < $oldLength - 5 ) { 00135 $error++; 00136 $this->output( "Warning: File:{$row->img_name} used to have " . 00137 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" ); 00138 00139 } 00140 if ( $verbose ) { 00141 $this->output("Forcibly refreshed File:{$row->img_name}.\n" ); 00142 } 00143 } 00144 else { 00145 if ( $verbose ) { 00146 $this->output( "Skipping File:{$row->img_name}.\n" ); 00147 } 00148 } 00149 } 00150 00151 } 00152 $conds2 = array( 'img_name > ' . $dbw->addQuotes( $row->img_name ) ); 00153 wfWaitForSlaves(); 00154 } while( $res->numRows() === $this->mBatchSize ); 00155 00156 $total = $upgraded + $leftAlone; 00157 if ( $force ) { 00158 $this->output( "\nFinished refreshing file metadata for $total files. $upgraded needed to be refreshed, $leftAlone did not need to be but were refreshed anyways, and $error refreshes were suspicious.\n" ); 00159 } else { 00160 $this->output( "\nFinished refreshing file metadata for $total files. $upgraded were refreshed, $leftAlone were already up to date, and $error refreshes were suspicious.\n" ); 00161 } 00162 } 00163 00168 function getConditions( $dbw ) { 00169 $conds = array(); 00170 00171 $end = $this->getOption( 'end', false ); 00172 $mime = $this->getOption( 'mime', false ); 00173 $like = $this->getOption( 'metadata-contains', false ); 00174 00175 if ( $end !== false ) { 00176 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end ) ; 00177 } 00178 if ( $mime !== false ) { 00179 list( $major, $minor ) = File::splitMime( $mime ); 00180 $conds['img_major_mime'] = $major; 00181 if ( $minor !== '*' ) { 00182 $conds['img_minor_mime'] = $minor; 00183 } 00184 } 00185 if ( $like ) { 00186 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() ); 00187 } 00188 return $conds; 00189 } 00190 00195 function setupParameters( $force, $brokenOnly ) { 00196 global $wgUpdateCompatibleMetadata; 00197 00198 if ( $brokenOnly ) { 00199 $wgUpdateCompatibleMetadata = false; 00200 } else { 00201 $wgUpdateCompatibleMetadata = true; 00202 } 00203 00204 if ( $brokenOnly && $force ) { 00205 $this->error( 'Cannot use --broken-only and --force together. ', 2 ); 00206 } 00207 } 00208 } 00209 00210 00211 $maintClass = 'RefreshImageMetadata'; 00212 require_once( RUN_MAINTENANCE_IF_MAIN );