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