30require_once __DIR__ .
'/Maintenance.php';
49 parent::__construct();
51 $this->
addDescription(
'Script to update image metadata records' );
56 'Reload metadata from file even if the metadata looks ok',
63 'Only fix really broken records, leave old but still compatible records alone.'
67 'Output extra information about each upgraded/non-upgraded file.',
72 $this->
addOption(
'start',
'Name of file to start with',
false,
true );
73 $this->
addOption(
'end',
'Name of file to end with',
false,
true );
77 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
83 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
84 .
"Potentially inefficient unless 'mediatype' is also specified",
90 '(Inefficient!) Only refresh files where the img_metadata field '
91 .
'contains this string. Can be used if its known a specific '
92 .
'property was being extracted incorrectly.',
100 $brokenOnly = $this->
hasOption(
'broken-only' );
101 $verbose = $this->
hasOption(
'verbose' );
102 $start = $this->
getOption(
'start',
false );
111 if ( $batchSize <= 0 ) {
112 $this->
fatalError(
"Batch size is too low...", 12 );
115 $repo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
120 if ( $start !==
false ) {
125 'LIMIT' => $batchSize,
126 'ORDER BY' =>
'img_name ASC',
129 $fileQuery = LocalFile::getQueryInfo();
130 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
134 $fileQuery[
'tables'],
135 $fileQuery[
'fields'],
136 array_merge( $conds, $conds2 ),
142 if (
$res->numRows() > 0 ) {
143 $row1 =
$res->current();
144 $this->
output(
"Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
148 foreach (
$res as $row ) {
151 $file = $repo->newFileFromRow( $row );
152 if (
$file->getUpgraded() ) {
155 $newLength = strlen(
$file->getMetadata() );
156 $oldLength = strlen( $row->img_metadata );
157 if ( $newLength < $oldLength - 5 ) {
166 "Warning: File:{$row->img_name} used to have " .
167 "$oldLength bytes of metadata but now has $newLength bytes.\n"
169 } elseif ( $verbose ) {
170 $this->
output(
"Refreshed File:{$row->img_name}.\n" );
176 $newLength = strlen(
$file->getMetadata() );
177 $oldLength = strlen( $row->img_metadata );
178 if ( $newLength < $oldLength - 5 ) {
181 "Warning: File:{$row->img_name} used to have " .
182 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n"
186 $this->
output(
"Forcibly refreshed File:{$row->img_name}.\n" );
190 $this->
output(
"Skipping File:{$row->img_name}.\n" );
194 }
catch ( Exception $e ) {
195 $this->
output(
"{$row->img_name} failed. {$e->getMessage()}\n" );
198 $conds2 = [
'img_name > ' .
$dbw->
addQuotes( $row->img_name ) ];
199 $lbFactory->waitForReplication();
200 }
while (
$res->numRows() === $batchSize );
202 $total = $upgraded + $leftAlone;
204 $this->
output(
"\nFinished refreshing file metadata for $total files. "
205 .
"$upgraded needed to be refreshed, $leftAlone did not need to "
206 .
"be but were refreshed anyways, and $error refreshes were suspicious.\n" );
208 $this->
output(
"\nFinished refreshing file metadata for $total files. "
209 .
"$upgraded were refreshed, $leftAlone were already up to date, "
210 .
"and $error refreshes were suspicious.\n" );
223 $mediatype = $this->
getOption(
'mediatype',
false );
224 $like = $this->
getOption(
'metadata-contains',
false );
226 if ( $end !==
false ) {
229 if (
$mime !==
false ) {
230 list( $major, $minor ) = File::splitMime(
$mime );
231 $conds[
'img_major_mime'] = $major;
232 if ( $minor !==
'*' ) {
233 $conds[
'img_minor_mime'] = $minor;
236 if ( $mediatype !==
false ) {
237 $conds[
'img_media_type'] = $mediatype;
259 if ( $brokenOnly && $force ) {
260 $this->
fatalError(
'Cannot use --broken-only and --force together. ', 2 );
$wgUpdateCompatibleMetadata
If to automatically update the img_metadata field if the metadata field is outdated but compatible wi...
const RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
hasOption( $name)
Checks to see if a particular option was set.
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.
setBatchSize( $s=0)
Set the batch size.
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.