3 * Refresh image metadata fields. See also rebuildImages.php
5 * Usage: php refreshImageMetadata.php
7 * Copyright © 2011 Brian Wolff
8 * https://www.mediawiki.org/
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
27 * @ingroup Maintenance
30 require_once __DIR__
. '/Maintenance.php';
33 * Maintenance script to refresh image metadata fields.
35 * @ingroup Maintenance
37 class RefreshImageMetadata
extends Maintenance
{
44 function __construct() {
45 parent
::__construct();
47 $this->addDescription( 'Script to update image metadata records' );
48 $this->setBatchSize( 200 );
52 'Reload metadata from file even if the metadata looks ok',
59 'Only fix really broken records, leave old but still compatible records alone.'
63 'Output extra information about each upgraded/non-upgraded file.',
68 $this->addOption( 'start', 'Name of file to start with', false, true );
69 $this->addOption( 'end', 'Name of file to end with', false, true );
73 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
79 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
80 . "Potentially inefficient unless 'mediatype' is also specified",
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.',
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 );
105 $dbw = $this->getDB( DB_MASTER
);
106 if ( $this->mBatchSize
<= 0 ) {
107 $this->error( "Batch size is too low...", 12 );
110 $repo = RepoGroup
::singleton()->getLocalRepo();
111 $conds = $this->getConditions( $dbw );
113 // For the WHERE img_name > 'foo' condition that comes after doing a batch
115 if ( $start !== false ) {
116 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
120 'LIMIT' => $this->mBatchSize
,
121 'ORDER BY' => 'img_name ASC',
128 array_merge( $conds, $conds2 ),
133 if ( $res->numRows() > 0 ) {
134 $row1 = $res->current();
135 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
138 $this->error( "No images to process.", 4 );
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.
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.
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" );
166 $newLength = strlen( $file->getMetadata() );
167 $oldLength = strlen( $row->img_metadata
);
168 if ( $newLength < $oldLength - 5 ) {
170 $this->output( "Warning: File:{$row->img_name} used to have " .
171 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
174 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
178 $this->output( "Skipping File:{$row->img_name}.\n" );
183 $conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name
) ];
185 } while ( $res->numRows() === $this->mBatchSize
);
187 $total = $upgraded +
$leftAlone;
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" );
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" );
200 * @param Database $dbw
203 function getConditions( $dbw ) {
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 );
211 if ( $end !== false ) {
212 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
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;
221 if ( $mediatype !== false ) {
222 $conds['img_media_type'] = $mediatype;
225 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
233 * @param bool $brokenOnly
235 function setupParameters( $force, $brokenOnly ) {
236 global $wgUpdateCompatibleMetadata;
239 $wgUpdateCompatibleMetadata = false;
241 $wgUpdateCompatibleMetadata = true;
244 if ( $brokenOnly && $force ) {
245 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
250 $maintClass = 'RefreshImageMetadata';
251 require_once RUN_MAINTENANCE_IF_MAIN
;