MediaWiki REL1_30
refreshImageMetadata.php
Go to the documentation of this file.
1<?php
30require_once __DIR__ . '/Maintenance.php';
31
34
41
45 protected $dbw;
46
47 function __construct() {
48 parent::__construct();
49
50 $this->addDescription( 'Script to update image metadata records' );
51 $this->setBatchSize( 200 );
52
53 $this->addOption(
54 'force',
55 'Reload metadata from file even if the metadata looks ok',
56 false,
57 false,
58 'f'
59 );
60 $this->addOption(
61 'broken-only',
62 'Only fix really broken records, leave old but still compatible records alone.'
63 );
64 $this->addOption(
65 'verbose',
66 'Output extra information about each upgraded/non-upgraded file.',
67 false,
68 false,
69 'v'
70 );
71 $this->addOption( 'start', 'Name of file to start with', false, true );
72 $this->addOption( 'end', 'Name of file to end with', false, true );
73
74 $this->addOption(
75 'mediatype',
76 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
77 false,
78 true
79 );
80 $this->addOption(
81 'mime',
82 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
83 . "Potentially inefficient unless 'mediatype' is also specified",
84 false,
85 true
86 );
87 $this->addOption(
88 'metadata-contains',
89 '(Inefficient!) Only refresh files where the img_metadata field '
90 . 'contains this string. Can be used if its known a specific '
91 . 'property was being extracted incorrectly.',
92 false,
93 true
94 );
95 }
96
97 public function execute() {
98 $force = $this->hasOption( 'force' );
99 $brokenOnly = $this->hasOption( 'broken-only' );
100 $verbose = $this->hasOption( 'verbose' );
101 $start = $this->getOption( 'start', false );
102 $this->setupParameters( $force, $brokenOnly );
103
104 $upgraded = 0;
105 $leftAlone = 0;
106 $error = 0;
107
108 $dbw = $this->getDB( DB_MASTER );
109 if ( $this->mBatchSize <= 0 ) {
110 $this->error( "Batch size is too low...", 12 );
111 }
112
113 $repo = RepoGroup::singleton()->getLocalRepo();
114 $conds = $this->getConditions( $dbw );
115
116 // For the WHERE img_name > 'foo' condition that comes after doing a batch
117 $conds2 = [];
118 if ( $start !== false ) {
119 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
120 }
121
122 $options = [
123 'LIMIT' => $this->mBatchSize,
124 'ORDER BY' => 'img_name ASC',
125 ];
126
127 do {
128 $res = $dbw->select(
129 'image',
130 '*',
131 array_merge( $conds, $conds2 ),
132 __METHOD__,
134 );
135
136 if ( $res->numRows() > 0 ) {
137 $row1 = $res->current();
138 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
139 $res->rewind();
140 }
141
142 foreach ( $res as $row ) {
143 try {
144 // LocalFile will upgrade immediately here if obsolete
145 $file = $repo->newFileFromRow( $row );
146 if ( $file->getUpgraded() ) {
147 // File was upgraded.
148 $upgraded++;
149 $newLength = strlen( $file->getMetadata() );
150 $oldLength = strlen( $row->img_metadata );
151 if ( $newLength < $oldLength - 5 ) {
152 // If after updating, the metadata is smaller then
153 // what it was before, that's probably not a good thing
154 // because we extract more data with time, not less.
155 // Thus this probably indicates an error of some sort,
156 // or at the very least is suspicious. Have the - 5 just
157 // to weed out any inconsequential changes.
158 $error++;
159 $this->output(
160 "Warning: File:{$row->img_name} used to have " .
161 "$oldLength bytes of metadata but now has $newLength bytes.\n"
162 );
163 } elseif ( $verbose ) {
164 $this->output( "Refreshed File:{$row->img_name}.\n" );
165 }
166 } else {
167 $leftAlone++;
168 if ( $force ) {
169 $file->upgradeRow();
170 $newLength = strlen( $file->getMetadata() );
171 $oldLength = strlen( $row->img_metadata );
172 if ( $newLength < $oldLength - 5 ) {
173 $error++;
174 $this->output(
175 "Warning: File:{$row->img_name} used to have " .
176 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n"
177 );
178 }
179 if ( $verbose ) {
180 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
181 }
182 } else {
183 if ( $verbose ) {
184 $this->output( "Skipping File:{$row->img_name}.\n" );
185 }
186 }
187 }
188 } catch ( Exception $e ) {
189 $this->output( "{$row->img_name} failed. {$e->getMessage()}\n" );
190 }
191 }
192 $conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name ) ];
194 } while ( $res->numRows() === $this->mBatchSize );
195
196 $total = $upgraded + $leftAlone;
197 if ( $force ) {
198 $this->output( "\nFinished refreshing file metadata for $total files. "
199 . "$upgraded needed to be refreshed, $leftAlone did not need to "
200 . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
201 } else {
202 $this->output( "\nFinished refreshing file metadata for $total files. "
203 . "$upgraded were refreshed, $leftAlone were already up to date, "
204 . "and $error refreshes were suspicious.\n" );
205 }
206 }
207
212 function getConditions( $dbw ) {
213 $conds = [];
214
215 $end = $this->getOption( 'end', false );
216 $mime = $this->getOption( 'mime', false );
217 $mediatype = $this->getOption( 'mediatype', false );
218 $like = $this->getOption( 'metadata-contains', false );
219
220 if ( $end !== false ) {
221 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
222 }
223 if ( $mime !== false ) {
224 list( $major, $minor ) = File::splitMime( $mime );
225 $conds['img_major_mime'] = $major;
226 if ( $minor !== '*' ) {
227 $conds['img_minor_mime'] = $minor;
228 }
229 }
230 if ( $mediatype !== false ) {
231 $conds['img_media_type'] = $mediatype;
232 }
233 if ( $like ) {
234 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
235 }
236
237 return $conds;
238 }
239
244 function setupParameters( $force, $brokenOnly ) {
246
247 if ( $brokenOnly ) {
249 } else {
251 }
252
253 if ( $brokenOnly && $force ) {
254 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
255 }
256 }
257}
258
259$maintClass = 'RefreshImageMetadata';
260require_once RUN_MAINTENANCE_IF_MAIN;
$wgUpdateCompatibleMetadata
If to automatically update the img_metadata field if the metadata field is outdated but compatible wi...
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
static splitMime( $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition File.php:273
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
int $mBatchSize
Batch size.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
hasOption( $name)
Checks to see if a particular param exists.
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.
Maintenance script to refresh image metadata fields.
IMaintainableDatabase $dbw
__construct()
Default constructor.
execute()
Do the actual work.
setupParameters( $force, $brokenOnly)
static singleton()
Get a RepoGroup instance.
Definition RepoGroup.php:59
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2581
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1971
returning false will NOT prevent logging $e
Definition hooks.txt:2146
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:40
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
buildLike()
LIKE statement wrapper, receives a variable-length argument list with parts of pattern to match conta...
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
addQuotes( $s)
Adds quotes and backslashes.
Advanced database interface for IDatabase handles that include maintenance methods.
require_once RUN_MAINTENANCE_IF_MAIN
const DB_MASTER
Definition defines.php:26
if( $ext=='php'|| $ext=='php5') $mime
Definition router.php:59