MediaWiki  1.27.2
refreshImageMetadata.php
Go to the documentation of this file.
1 <?php
30 require_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__,
130  $options
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  $file = $repo->newFileFromRow( $row );
143  if ( $file->getUpgraded() ) {
144  // File was upgraded.
145  $upgraded++;
146  $newLength = strlen( $file->getMetadata() );
147  $oldLength = strlen( $row->img_metadata );
148  if ( $newLength < $oldLength - 5 ) {
149  // If after updating, the metadata is smaller then
150  // what it was before, that's probably not a good thing
151  // because we extract more data with time, not less.
152  // Thus this probably indicates an error of some sort,
153  // or at the very least is suspicious. Have the - 5 just
154  // to weed out any inconsequential changes.
155  $error++;
156  $this->output( "Warning: File:{$row->img_name} used to have " .
157  "$oldLength bytes of metadata but now has $newLength bytes.\n" );
158  } elseif ( $verbose ) {
159  $this->output( "Refreshed File:{$row->img_name}.\n" );
160  }
161  } else {
162  $leftAlone++;
163  if ( $force ) {
164  $file->upgradeRow();
165  $newLength = strlen( $file->getMetadata() );
166  $oldLength = strlen( $row->img_metadata );
167  if ( $newLength < $oldLength - 5 ) {
168  $error++;
169  $this->output( "Warning: File:{$row->img_name} used to have " .
170  "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
171  }
172  if ( $verbose ) {
173  $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
174  }
175  } else {
176  if ( $verbose ) {
177  $this->output( "Skipping File:{$row->img_name}.\n" );
178  }
179  }
180  }
181  }
182  $conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name ) ];
183  wfWaitForSlaves();
184  } while ( $res->numRows() === $this->mBatchSize );
185 
186  $total = $upgraded + $leftAlone;
187  if ( $force ) {
188  $this->output( "\nFinished refreshing file metadata for $total files. "
189  . "$upgraded needed to be refreshed, $leftAlone did not need to "
190  . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
191  } else {
192  $this->output( "\nFinished refreshing file metadata for $total files. "
193  . "$upgraded were refreshed, $leftAlone were already up to date, "
194  . "and $error refreshes were suspicious.\n" );
195  }
196  }
197 
202  function getConditions( $dbw ) {
203  $conds = [];
204 
205  $end = $this->getOption( 'end', false );
206  $mime = $this->getOption( 'mime', false );
207  $mediatype = $this->getOption( 'mediatype', false );
208  $like = $this->getOption( 'metadata-contains', false );
209 
210  if ( $end !== false ) {
211  $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
212  }
213  if ( $mime !== false ) {
214  list( $major, $minor ) = File::splitMime( $mime );
215  $conds['img_major_mime'] = $major;
216  if ( $minor !== '*' ) {
217  $conds['img_minor_mime'] = $minor;
218  }
219  }
220  if ( $mediatype !== false ) {
221  $conds['img_media_type'] = $mediatype;
222  }
223  if ( $like ) {
224  $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
225  }
226 
227  return $conds;
228  }
229 
234  function setupParameters( $force, $brokenOnly ) {
236 
237  if ( $brokenOnly ) {
238  $wgUpdateCompatibleMetadata = false;
239  } else {
240  $wgUpdateCompatibleMetadata = true;
241  }
242 
243  if ( $brokenOnly && $force ) {
244  $this->error( 'Cannot use --broken-only and --force together. ', 2 );
245  }
246  }
247 }
248 
249 $maintClass = 'RefreshImageMetadata';
250 require_once RUN_MAINTENANCE_IF_MAIN;
select($table, $vars, $conds= '', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
Definition: Database.php:1230
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
wfWaitForSlaves($ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the slaves 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:272
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
if($ext== 'php'||$ext== 'php5') $mime
Definition: router.php:65
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.
setupParameters($force, $brokenOnly)
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
addOption($name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
Definition: Database.php:2031
buildLike()
LIKE statement wrapper, receives a variable-length argument list with parts of pattern to match conta...
Definition: Database.php:2007
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:1004
$res
Definition: database.txt:21
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
addQuotes($s)
Adds quotes and backslashes.
Definition: Database.php:1958
addDescription($text)
Set the description text.
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
Definition: distributors.txt:9
getOption($name, $default=null)
Get an option, or return the default.
output($out, $channel=null)
Throw some output to the user.
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:35
int $mBatchSize
Batch size.
Definition: Maintenance.php:99
error($err, $die=0)
Throw an error to the user.
Maintenance script to refresh image metadata fields.
const DB_MASTER
Definition: Defines.php:47
setBatchSize($s=0)
Set the batch size.
$wgUpdateCompatibleMetadata
If to automatically update the img_metadata field if the metadata field is outdated but compatible wi...