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';
32 use Wikimedia\Rdbms\IDatabase
;
33 use Wikimedia\Rdbms\IMaintainableDatabase
;
36 * Maintenance script to refresh image metadata fields.
38 * @ingroup Maintenance
40 class RefreshImageMetadata
extends Maintenance
{
43 * @var IMaintainableDatabase
47 function __construct() {
48 parent
::__construct();
50 $this->addDescription( 'Script to update image metadata records' );
51 $this->setBatchSize( 200 );
55 'Reload metadata from file even if the metadata looks ok',
62 'Only fix really broken records, leave old but still compatible records alone.'
66 'Output extra information about each upgraded/non-upgraded file.',
71 $this->addOption( 'start', 'Name of file to start with', false, true );
72 $this->addOption( 'end', 'Name of file to end with', false, true );
76 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
82 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
83 . "Potentially inefficient unless 'mediatype' is also specified",
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.',
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 );
108 $dbw = $this->getDB( DB_MASTER
);
109 if ( $this->mBatchSize
<= 0 ) {
110 $this->error( "Batch size is too low...", 12 );
113 $repo = RepoGroup
::singleton()->getLocalRepo();
114 $conds = $this->getConditions( $dbw );
116 // For the WHERE img_name > 'foo' condition that comes after doing a batch
118 if ( $start !== false ) {
119 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
123 'LIMIT' => $this->mBatchSize
,
124 'ORDER BY' => 'img_name ASC',
130 LocalFile
::selectFields(),
131 array_merge( $conds, $conds2 ),
136 if ( $res->numRows() > 0 ) {
137 $row1 = $res->current();
138 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->img_name}.\n" );
142 foreach ( $res as $row ) {
144 // LocalFile will upgrade immediately here if obsolete
145 $file = $repo->newFileFromRow( $row );
146 if ( $file->getUpgraded() ) {
147 // File was 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.
160 "Warning: File:{$row->img_name} used to have " .
161 "$oldLength bytes of metadata but now has $newLength bytes.\n"
163 } elseif ( $verbose ) {
164 $this->output( "Refreshed File:{$row->img_name}.\n" );
170 $newLength = strlen( $file->getMetadata() );
171 $oldLength = strlen( $row->img_metadata
);
172 if ( $newLength < $oldLength - 5 ) {
175 "Warning: File:{$row->img_name} used to have " .
176 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n"
180 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
184 $this->output( "Skipping File:{$row->img_name}.\n" );
188 } catch ( Exception
$e ) {
189 $this->output( "{$row->img_name} failed. {$e->getMessage()}\n" );
192 $conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name
) ];
194 } while ( $res->numRows() === $this->mBatchSize
);
196 $total = $upgraded +
$leftAlone;
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" );
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" );
209 * @param IDatabase $dbw
212 function getConditions( $dbw ) {
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 );
220 if ( $end !== false ) {
221 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
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;
230 if ( $mediatype !== false ) {
231 $conds['img_media_type'] = $mediatype;
234 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
242 * @param bool $brokenOnly
244 function setupParameters( $force, $brokenOnly ) {
245 global $wgUpdateCompatibleMetadata;
248 $wgUpdateCompatibleMetadata = false;
250 $wgUpdateCompatibleMetadata = true;
253 if ( $brokenOnly && $force ) {
254 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
259 $maintClass = 'RefreshImageMetadata';
260 require_once RUN_MAINTENANCE_IF_MAIN
;