MediaWiki  1.23.14
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->mDescription = 'Script to update image metadata records';
48  $this->setBatchSize( 200 );
49 
50  $this->addOption( 'force', 'Reload metadata from file even if the metadata looks ok', false, false, 'f' );
51  $this->addOption( 'broken-only', 'Only fix really broken records, leave old but still compatible records alone.' );
52  $this->addOption( 'verbose', 'Output extra information about each upgraded/non-upgraded file.', false, false, 'v' );
53  $this->addOption( 'start', 'Name of file to start with', false, true );
54  $this->addOption( 'end', 'Name of file to end with', false, true );
55 
56  $this->addOption( 'mime', '(Inefficient!) Only refresh files with this mime type. Can accept wild-card image/*', false, true );
57  $this->addOption( 'metadata-contains', '(Inefficient!) Only refresh files where the img_metadata field contains this string. Can be used if its known a specific property was being extracted incorrectly.', false, true );
58 
59  }
60 
61  public function execute() {
62  $force = $this->hasOption( 'force' );
63  $brokenOnly = $this->hasOption( 'broken-only' );
64  $verbose = $this->hasOption( 'verbose' );
65  $start = $this->getOption( 'start', false );
66  $this->setupParameters( $force, $brokenOnly );
67 
68  $upgraded = 0;
69  $leftAlone = 0;
70  $error = 0;
71 
72  $dbw = wfGetDB( DB_MASTER );
73  if ( $this->mBatchSize <= 0 ) {
74  $this->error( "Batch size is too low...", 12 );
75  }
76 
77  $repo = RepoGroup::singleton()->getLocalRepo();
78  $conds = $this->getConditions( $dbw );
79 
80  // For the WHERE img_name > 'foo' condition that comes after doing a batch
81  $conds2 = array();
82  if ( $start !== false ) {
83  $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
84  }
85 
86  $options = array(
87  'LIMIT' => $this->mBatchSize,
88  'ORDER BY' => 'img_name ASC',
89  );
90 
91  do {
92  $res = $dbw->select(
93  'image',
94  '*',
95  array_merge( $conds, $conds2 ),
96  __METHOD__,
97  $options
98  );
99 
100  if ( $res->numRows() > 0 ) {
101  $row1 = $res->current();
102  $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
103  $res->rewind();
104  } else {
105  $this->error( "No images to process.", 4 );
106  }
107 
108  foreach ( $res as $row ) {
109  $file = $repo->newFileFromRow( $row );
110  if ( $file->getUpgraded() ) {
111  // File was upgraded.
112  $upgraded++;
113  $newLength = strlen( $file->getMetadata() );
114  $oldLength = strlen( $row->img_metadata );
115  if ( $newLength < $oldLength - 5 ) {
116  // If after updating, the metadata is smaller then
117  // what it was before, that's probably not a good thing
118  // because we extract more data with time, not less.
119  // Thus this probably indicates an error of some sort,
120  // or at the very least is suspicious. Have the - 5 just
121  // to weed out any inconsequential changes.
122  $error++;
123  $this->output( "Warning: File:{$row->img_name} used to have " .
124  "$oldLength bytes of metadata but now has $newLength bytes.\n" );
125  } elseif ( $verbose ) {
126  $this->output( "Refreshed File:{$row->img_name}.\n" );
127  }
128  } else {
129  $leftAlone++;
130  if ( $force ) {
131  $file->upgradeRow();
132  $newLength = strlen( $file->getMetadata() );
133  $oldLength = strlen( $row->img_metadata );
134  if ( $newLength < $oldLength - 5 ) {
135  $error++;
136  $this->output( "Warning: File:{$row->img_name} used to have " .
137  "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
138 
139  }
140  if ( $verbose ) {
141  $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
142  }
143  }
144  else {
145  if ( $verbose ) {
146  $this->output( "Skipping File:{$row->img_name}.\n" );
147  }
148  }
149  }
150 
151  }
152  $conds2 = array( 'img_name > ' . $dbw->addQuotes( $row->img_name ) );
153  wfWaitForSlaves();
154  } while ( $res->numRows() === $this->mBatchSize );
155 
156  $total = $upgraded + $leftAlone;
157  if ( $force ) {
158  $this->output( "\nFinished refreshing file metadata for $total files. $upgraded needed to be refreshed, $leftAlone did not need to be but were refreshed anyways, and $error refreshes were suspicious.\n" );
159  } else {
160  $this->output( "\nFinished refreshing file metadata for $total files. $upgraded were refreshed, $leftAlone were already up to date, and $error refreshes were suspicious.\n" );
161  }
162  }
163 
168  function getConditions( $dbw ) {
169  $conds = array();
170 
171  $end = $this->getOption( 'end', false );
172  $mime = $this->getOption( 'mime', false );
173  $like = $this->getOption( 'metadata-contains', false );
174 
175  if ( $end !== false ) {
176  $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
177  }
178  if ( $mime !== false ) {
179  list( $major, $minor ) = File::splitMime( $mime );
180  $conds['img_major_mime'] = $major;
181  if ( $minor !== '*' ) {
182  $conds['img_minor_mime'] = $minor;
183  }
184  }
185  if ( $like ) {
186  $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
187  }
188  return $conds;
189  }
190 
195  function setupParameters( $force, $brokenOnly ) {
196  global $wgUpdateCompatibleMetadata;
197 
198  if ( $brokenOnly ) {
199  $wgUpdateCompatibleMetadata = false;
200  } else {
201  $wgUpdateCompatibleMetadata = true;
202  }
203 
204  if ( $brokenOnly && $force ) {
205  $this->error( 'Cannot use --broken-only and --force together. ', 2 );
206  }
207  }
208 }
209 
210 $maintClass = 'RefreshImageMetadata';
211 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:97
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$mime
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string $mime
Definition: hooks.txt:2584
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3714
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
DatabaseBase\buildLike
buildLike()
LIKE statement wrapper, receives a variable-length argument list with parts of pattern to match conta...
Definition: Database.php:2541
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
DatabaseBase\anyString
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.
Definition: Database.php:2575
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
File\splitMime
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:249
RefreshImageMetadata\__construct
__construct()
Default constructor.
Definition: refreshImageMetadata.php:43
RefreshImageMetadata
Maintenance script to refresh image metadata fields.
Definition: refreshImageMetadata.php:37
$verbose
$verbose
Definition: Utf8Test.php:39
DatabaseBase\addQuotes
addQuotes( $s)
Adds quotes and backslashes.
Definition: Database.php:2477
$total
$total
Definition: Utf8Test.php:92
DatabaseBase\select
select( $table, $vars, $conds='', $fname=__METHOD__, $options=array(), $join_conds=array())
Execute a SELECT query constructed using the various parameters provided.
Definition: Database.php:1575
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfWaitForSlaves
wfWaitForSlaves( $maxLag=false, $wiki=false, $cluster=false)
Modern version of wfWaitForSlaves().
Definition: GlobalFunctions.php:3859
RefreshImageMetadata\getConditions
getConditions( $dbw)
Definition: refreshImageMetadata.php:167
list
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
$options
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:1530
RefreshImageMetadata\$dbw
DatabaseBase $dbw
Definition: refreshImageMetadata.php:41
DatabaseBase
Database abstraction object.
Definition: Database.php:219
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
RefreshImageMetadata\setupParameters
setupParameters( $force, $brokenOnly)
Definition: refreshImageMetadata.php:194
as
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
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2584
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
$maintClass
$maintClass
Definition: refreshImageMetadata.php:209
$res
$res
Definition: database.txt:21
RefreshImageMetadata\execute
execute()
Do the actual work.
Definition: refreshImageMetadata.php:60
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:254