MediaWiki REL1_28
refreshImageMetadata.php
Go to the documentation of this file.
1<?php
30require_once __DIR__ . '/Maintenance.php';
31
38
42 protected $dbw;
43
44 function __construct() {
45 parent::__construct();
46
47 $this->addDescription( 'Script to update image metadata records' );
48 $this->setBatchSize( 200 );
49
50 $this->addOption(
51 'force',
52 'Reload metadata from file even if the metadata looks ok',
53 false,
54 false,
55 'f'
56 );
57 $this->addOption(
58 'broken-only',
59 'Only fix really broken records, leave old but still compatible records alone.'
60 );
61 $this->addOption(
62 'verbose',
63 'Output extra information about each upgraded/non-upgraded file.',
64 false,
65 false,
66 'v'
67 );
68 $this->addOption( 'start', 'Name of file to start with', false, true );
69 $this->addOption( 'end', 'Name of file to end with', false, true );
70
71 $this->addOption(
72 'mediatype',
73 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
74 false,
75 true
76 );
77 $this->addOption(
78 'mime',
79 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
80 . "Potentially inefficient unless 'mediatype' is also specified",
81 false,
82 true
83 );
84 $this->addOption(
85 'metadata-contains',
86 '(Inefficient!) Only refresh files where the img_metadata field '
87 . 'contains this string. Can be used if its known a specific '
88 . 'property was being extracted incorrectly.',
89 false,
90 true
91 );
92 }
93
94 public function execute() {
95 $force = $this->hasOption( 'force' );
96 $brokenOnly = $this->hasOption( 'broken-only' );
97 $verbose = $this->hasOption( 'verbose' );
98 $start = $this->getOption( 'start', false );
99 $this->setupParameters( $force, $brokenOnly );
100
101 $upgraded = 0;
102 $leftAlone = 0;
103 $error = 0;
104
105 $dbw = $this->getDB( DB_MASTER );
106 if ( $this->mBatchSize <= 0 ) {
107 $this->error( "Batch size is too low...", 12 );
108 }
109
110 $repo = RepoGroup::singleton()->getLocalRepo();
111 $conds = $this->getConditions( $dbw );
112
113 // For the WHERE img_name > 'foo' condition that comes after doing a batch
114 $conds2 = [];
115 if ( $start !== false ) {
116 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
117 }
118
119 $options = [
120 'LIMIT' => $this->mBatchSize,
121 'ORDER BY' => 'img_name ASC',
122 ];
123
124 do {
125 $res = $dbw->select(
126 'image',
127 '*',
128 array_merge( $conds, $conds2 ),
129 __METHOD__,
131 );
132
133 if ( $res->numRows() > 0 ) {
134 $row1 = $res->current();
135 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
136 $res->rewind();
137 } else {
138 $this->error( "No images to process.", 4 );
139 }
140
141 foreach ( $res as $row ) {
142 // LocalFile will upgrade immediately here if obsolete
143 $file = $repo->newFileFromRow( $row );
144 if ( $file->getUpgraded() ) {
145 // File was upgraded.
146 $upgraded++;
147 $newLength = strlen( $file->getMetadata() );
148 $oldLength = strlen( $row->img_metadata );
149 if ( $newLength < $oldLength - 5 ) {
150 // If after updating, the metadata is smaller then
151 // what it was before, that's probably not a good thing
152 // because we extract more data with time, not less.
153 // Thus this probably indicates an error of some sort,
154 // or at the very least is suspicious. Have the - 5 just
155 // to weed out any inconsequential changes.
156 $error++;
157 $this->output( "Warning: File:{$row->img_name} used to have " .
158 "$oldLength bytes of metadata but now has $newLength bytes.\n" );
159 } elseif ( $verbose ) {
160 $this->output( "Refreshed File:{$row->img_name}.\n" );
161 }
162 } else {
163 $leftAlone++;
164 if ( $force ) {
165 $file->upgradeRow();
166 $newLength = strlen( $file->getMetadata() );
167 $oldLength = strlen( $row->img_metadata );
168 if ( $newLength < $oldLength - 5 ) {
169 $error++;
170 $this->output( "Warning: File:{$row->img_name} used to have " .
171 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
172 }
173 if ( $verbose ) {
174 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
175 }
176 } else {
177 if ( $verbose ) {
178 $this->output( "Skipping File:{$row->img_name}.\n" );
179 }
180 }
181 }
182 }
183 $conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name ) ];
185 } while ( $res->numRows() === $this->mBatchSize );
186
187 $total = $upgraded + $leftAlone;
188 if ( $force ) {
189 $this->output( "\nFinished refreshing file metadata for $total files. "
190 . "$upgraded needed to be refreshed, $leftAlone did not need to "
191 . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
192 } else {
193 $this->output( "\nFinished refreshing file metadata for $total files. "
194 . "$upgraded were refreshed, $leftAlone were already up to date, "
195 . "and $error refreshes were suspicious.\n" );
196 }
197 }
198
203 function getConditions( $dbw ) {
204 $conds = [];
205
206 $end = $this->getOption( 'end', false );
207 $mime = $this->getOption( 'mime', false );
208 $mediatype = $this->getOption( 'mediatype', false );
209 $like = $this->getOption( 'metadata-contains', false );
210
211 if ( $end !== false ) {
212 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
213 }
214 if ( $mime !== false ) {
215 list( $major, $minor ) = File::splitMime( $mime );
216 $conds['img_major_mime'] = $major;
217 if ( $minor !== '*' ) {
218 $conds['img_minor_mime'] = $minor;
219 }
220 }
221 if ( $mediatype !== false ) {
222 $conds['img_media_type'] = $mediatype;
223 }
224 if ( $like ) {
225 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
226 }
227
228 return $conds;
229 }
230
235 function setupParameters( $force, $brokenOnly ) {
237
238 if ( $brokenOnly ) {
240 } else {
242 }
243
244 if ( $brokenOnly && $force ) {
245 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
246 }
247 }
248}
249
250$maintClass = 'RefreshImageMetadata';
251require_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.
Relational database abstraction object.
Definition Database.php:36
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
buildLike()
LIKE statement wrapper, receives a variable-length argument list with parts of pattern to match conta...
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
addQuotes( $s)
Adds quotes and backslashes.
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:272
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
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.
__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
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition hooks.txt:1096
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
require_once RUN_MAINTENANCE_IF_MAIN
const DB_MASTER
Definition defines.php:23
if( $ext=='php'|| $ext=='php5') $mime
Definition router.php:65