31require_once __DIR__ .
'/Maintenance.php';
55 parent::__construct();
57 $this->
addDescription(
'Script to update image metadata records' );
62 'Reload metadata from file even if the metadata looks ok',
69 'Only fix really broken records, leave old but still compatible records alone.'
73 'Fix records with an out of date serialization format.'
77 'Enable splitting out large metadata items to the text table. Implies --convert-to-json.'
81 'Output extra information about each upgraded/non-upgraded file.',
86 $this->
addOption(
'start',
'Name of file to start with',
false,
true );
87 $this->
addOption(
'end',
'Name of file to end with',
false,
true );
91 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
97 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
98 .
"Potentially inefficient unless 'mediatype' is also specified",
104 '(Inefficient!) Only refresh files where the img_metadata field '
105 .
'contains this string. Can be used if its known a specific '
106 .
'property was being extracted incorrectly.',
112 'Time to sleep between each batch (in seconds). Default: 0',
116 $this->
addOption(
'oldimage',
'Run and refresh on oldimage table.' );
121 $brokenOnly = $this->
hasOption(
'broken-only' );
122 $verbose = $this->
hasOption(
'verbose' );
123 $start = $this->
getOption(
'start',
false );
125 $sleep = (int)$this->
getOption(
'sleep', 0 );
126 $reserialize = $this->
hasOption(
'convert-to-json' );
127 $oldimage = $this->
hasOption(
'oldimage' );
131 $fieldPrefix =
'oi_';
132 $queryBuilderTemplate = FileSelectQueryBuilder::newForOldFile( $dbw );
134 $fieldPrefix =
'img_';
135 $queryBuilderTemplate = FileSelectQueryBuilder::newForFile( $dbw );
142 if ( $batchSize <= 0 ) {
143 $this->
fatalError(
"Batch size is too low...", 12 );
145 $repo = $this->newLocalRepo( $force, $brokenOnly, $reserialize, $split );
146 $this->setConditions( $dbw, $queryBuilderTemplate, $fieldPrefix );
147 $queryBuilderTemplate
148 ->orderBy( $fieldPrefix .
'name', SelectQueryBuilder::SORT_ASC )
149 ->limit( $batchSize );
151 $batchCondition = [];
153 if ( $start !==
false ) {
154 $batchCondition[] = $dbw->
expr( $fieldPrefix .
'name',
'>=', $start );
157 $queryBuilder = clone $queryBuilderTemplate;
158 $res = $queryBuilder->
andWhere( $batchCondition )
159 ->caller( __METHOD__ )->fetchResultSet();
160 $nameField = $fieldPrefix .
'name';
161 if ( $res->numRows() > 0 ) {
162 $row1 = $res->current();
163 $this->
output(
"Processing next {$res->numRows()} row(s) starting with {$row1->$nameField}.\n" );
167 foreach ( $res as $row ) {
170 $file = $repo->newFileFromRow( $row );
171 $file->maybeUpgradeRow();
172 if ( $file->getUpgraded() ) {
175 $this->
output(
"Refreshed File:{$row->$nameField}.\n" );
181 $this->
output(
"Forcibly refreshed File:{$row->$nameField}.\n" );
185 $this->
output(
"Skipping File:{$row->$nameField}.\n" );
189 }
catch ( Exception $e ) {
190 $this->
output(
"{$row->$nameField} failed. {$e->getMessage()}\n" );
193 if ( $res->numRows() > 0 ) {
195 $batchCondition = [ $dbw->
expr( $fieldPrefix .
'name',
'>', $row->$nameField ) ];
201 }
while ( $res->numRows() === $batchSize );
203 $total = $upgraded + $leftAlone;
205 $this->
output(
"\nFinished refreshing file metadata for $total files. "
206 .
"$upgraded needed to be refreshed, $leftAlone did not need to "
207 .
"be but were refreshed anyways, and $error refreshes were suspicious.\n" );
209 $this->
output(
"\nFinished refreshing file metadata for $total files. "
210 .
"$upgraded were refreshed, $leftAlone were already up to date, "
211 .
"and $error refreshes were suspicious.\n" );
222 $end = $this->getOption(
'end',
false );
223 $mime = $this->getOption(
'mime',
false );
224 $mediatype = $this->getOption(
'mediatype',
false );
225 $like = $this->getOption(
'metadata-contains',
false );
227 if ( $end !==
false ) {
228 $queryBuilder->
andWhere( $dbw->
expr( $fieldPrefix .
'name',
'<=', $end ) );
230 if ( $mime !==
false ) {
231 [ $major, $minor ] = File::splitMime( $mime );
232 $queryBuilder->
andWhere( [ $fieldPrefix .
'major_mime' => $major ] );
233 if ( $minor !==
'*' ) {
234 $queryBuilder->
andWhere( [ $fieldPrefix .
'minor_mime' => $minor ] );
237 if ( $mediatype !==
false ) {
238 $queryBuilder->
andWhere( [ $fieldPrefix .
'media_type' => $mediatype ] );
242 $dbw->
expr( $fieldPrefix .
'metadata', IExpression::LIKE,
256 private function newLocalRepo( $force, $brokenOnly, $reserialize, $split ): LocalRepo {
257 if ( $brokenOnly && $force ) {
258 $this->
fatalError(
'Cannot use --broken-only and --force together. ', 2 );
260 $reserialize = $reserialize || $split;
261 if ( $brokenOnly && $reserialize ) {
262 $this->
fatalError(
'Cannot use --broken-only with --convert-to-json or --split. ',
267 'updateCompatibleMetadata' => !$brokenOnly,
269 if ( $reserialize ) {
270 $overrides[
'reserializeMetadata'] =
true;
271 $overrides[
'useJsonMetadata'] =
true;
274 $overrides[
'useSplitMetadata'] =
true;
278 ->newCustomLocalRepo( $overrides );
284require_once RUN_MAINTENANCE_IF_MAIN;
if(!defined('MW_SETUP_CALLBACK'))
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
waitForReplication()
Wait for replica DB servers to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Advanced database interface for IDatabase handles that include maintenance methods.