30require_once __DIR__ .
'/Maintenance.php';
50 parent::__construct();
52 $this->
addDescription(
'Script to update image metadata records' );
57 'Reload metadata from file even if the metadata looks ok',
64 'Only fix really broken records, leave old but still compatible records alone.'
68 'Fix records with an out of date serialization format.'
72 'Enable splitting out large metadata items to the text table. Implies --convert-to-json.'
76 'Output extra information about each upgraded/non-upgraded file.',
81 $this->
addOption(
'start',
'Name of file to start with',
false,
true );
82 $this->
addOption(
'end',
'Name of file to end with',
false,
true );
86 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
92 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
93 .
"Potentially inefficient unless 'mediatype' is also specified",
99 '(Inefficient!) Only refresh files where the img_metadata field '
100 .
'contains this string. Can be used if its known a specific '
101 .
'property was being extracted incorrectly.',
107 'Time to sleep between each batch (in seconds). Default: 0',
111 $this->
addOption(
'oldimage',
'Run and refresh on oldimage table.' );
116 $brokenOnly = $this->
hasOption(
'broken-only' );
117 $verbose = $this->
hasOption(
'verbose' );
118 $start = $this->
getOption(
'start',
false );
120 $sleep = (int)$this->
getOption(
'sleep', 0 );
121 $reserialize = $this->
hasOption(
'convert-to-json' );
122 $oldimage = $this->
hasOption(
'oldimage' );
126 $fieldPrefix =
'oi_';
127 $queryBuilderTemplate = FileSelectQueryBuilder::newForOldFile(
$dbw );
129 $fieldPrefix =
'img_';
130 $queryBuilderTemplate = FileSelectQueryBuilder::newForFile(
$dbw );
137 if ( $batchSize <= 0 ) {
138 $this->
fatalError(
"Batch size is too low...", 12 );
140 $repo = $this->newLocalRepo( $force, $brokenOnly, $reserialize, $split );
141 $this->setConditions(
$dbw, $queryBuilderTemplate, $fieldPrefix );
142 $queryBuilderTemplate
143 ->orderBy( $fieldPrefix .
'name', SelectQueryBuilder::SORT_ASC )
144 ->limit( $batchSize );
146 $batchCondition = [];
148 if ( $start !==
false ) {
149 $batchCondition[] = $fieldPrefix .
'name >= ' .
$dbw->
addQuotes( $start );
152 $queryBuilder = clone $queryBuilderTemplate;
153 $res = $queryBuilder->
andWhere( $batchCondition )
154 ->caller( __METHOD__ )->fetchResultSet();
155 $nameField = $fieldPrefix .
'name';
156 if ( $res->numRows() > 0 ) {
157 $row1 = $res->current();
158 $this->
output(
"Processing next {$res->numRows()} row(s) starting with {$row1->$nameField}.\n" );
162 foreach ( $res as $row ) {
165 $file = $repo->newFileFromRow( $row );
166 $file->maybeUpgradeRow();
167 if (
$file->getUpgraded() ) {
170 $this->
output(
"Refreshed File:{$row->$nameField}.\n" );
176 $this->
output(
"Forcibly refreshed File:{$row->$nameField}.\n" );
180 $this->
output(
"Skipping File:{$row->$nameField}.\n" );
184 }
catch ( Exception $e ) {
185 $this->
output(
"{$row->$nameField} failed. {$e->getMessage()}\n" );
188 if ( $res->numRows() > 0 ) {
190 $batchCondition = [ $fieldPrefix .
'name > ' .
$dbw->
addQuotes( $row->$nameField ) ];
196 }
while ( $res->numRows() === $batchSize );
198 $total = $upgraded + $leftAlone;
200 $this->
output(
"\nFinished refreshing file metadata for $total files. "
201 .
"$upgraded needed to be refreshed, $leftAlone did not need to "
202 .
"be but were refreshed anyways, and $error refreshes were suspicious.\n" );
204 $this->
output(
"\nFinished refreshing file metadata for $total files. "
205 .
"$upgraded were refreshed, $leftAlone were already up to date, "
206 .
"and $error refreshes were suspicious.\n" );
219 $mediatype = $this->
getOption(
'mediatype',
false );
220 $like = $this->
getOption(
'metadata-contains',
false );
222 if ( $end !==
false ) {
225 if (
$mime !==
false ) {
227 $queryBuilder->
andWhere( [ $fieldPrefix .
'major_mime' => $major ] );
228 if ( $minor !==
'*' ) {
229 $queryBuilder->
andWhere( [ $fieldPrefix .
'minor_mime' => $minor ] );
232 if ( $mediatype !==
false ) {
233 $queryBuilder->
andWhere( [ $fieldPrefix .
'media_type' => $mediatype ] );
250 private function newLocalRepo( $force, $brokenOnly, $reserialize, $split ):
LocalRepo {
251 if ( $brokenOnly && $force ) {
252 $this->
fatalError(
'Cannot use --broken-only and --force together. ', 2 );
254 $reserialize = $reserialize || $split;
255 if ( $brokenOnly && $reserialize ) {
256 $this->
fatalError(
'Cannot use --broken-only with --convert-to-json or --split. ',
261 'updateCompatibleMetadata' => !$brokenOnly,
263 if ( $reserialize ) {
264 $overrides[
'reserializeMetadata'] =
true;
265 $overrides[
'useJsonMetadata'] =
true;
268 $overrides[
'useSplitMetadata'] =
true;
272 ->newCustomLocalRepo( $overrides );
277require_once RUN_MAINTENANCE_IF_MAIN;
if(!defined('MW_SETUP_CALLBACK'))
static splitMime(?string $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Advanced database interface for IDatabase handles that include maintenance methods.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.