Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / maintenance / refreshImageMetadata.php
blob4f2341ce01135239e8ad602f7b507cbd6abab407
1 <?php
2 /**
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
25 * @file
26 * @author Brian Wolff
27 * @ingroup Maintenance
30 require_once __DIR__ . '/Maintenance.php';
32 /**
33 * Maintenance script to refresh image metadata fields.
35 * @ingroup Maintenance
37 class RefreshImageMetadata extends Maintenance {
39 /**
40 * @var DatabaseBase
42 protected $dbw;
44 function __construct() {
45 parent::__construct();
47 $this->mDescription = 'Script to update image metadata records';
48 $this->setBatchSize( 200 );
50 $this->addOption(
51 'force',
52 'Reload metadata from file even if the metadata looks ok',
53 false,
54 false,
55 'f'
57 $this->addOption(
58 'broken-only',
59 'Only fix really broken records, leave old but still compatible records alone.'
61 $this->addOption(
62 'verbose',
63 'Output extra information about each upgraded/non-upgraded file.',
64 false,
65 false,
66 'v'
68 $this->addOption( 'start', 'Name of file to start with', false, true );
69 $this->addOption( 'end', 'Name of file to end with', false, true );
71 $this->addOption(
72 'mime',
73 '(Inefficient!) Only refresh files with this MIME type. Can accept wild-card image/*',
74 false,
75 true
77 $this->addOption(
78 'metadata-contains',
79 '(Inefficient!) Only refresh files where the img_metadata field '
80 . 'contains this string. Can be used if its known a specific '
81 . 'property was being extracted incorrectly.',
82 false,
83 true
87 public function execute() {
88 $force = $this->hasOption( 'force' );
89 $brokenOnly = $this->hasOption( 'broken-only' );
90 $verbose = $this->hasOption( 'verbose' );
91 $start = $this->getOption( 'start', false );
92 $this->setupParameters( $force, $brokenOnly );
94 $upgraded = 0;
95 $leftAlone = 0;
96 $error = 0;
98 $dbw = $this->getDB( DB_MASTER );
99 if ( $this->mBatchSize <= 0 ) {
100 $this->error( "Batch size is too low...", 12 );
103 $repo = RepoGroup::singleton()->getLocalRepo();
104 $conds = $this->getConditions( $dbw );
106 // For the WHERE img_name > 'foo' condition that comes after doing a batch
107 $conds2 = array();
108 if ( $start !== false ) {
109 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
112 $options = array(
113 'LIMIT' => $this->mBatchSize,
114 'ORDER BY' => 'img_name ASC',
117 do {
118 $res = $dbw->select(
119 'image',
120 '*',
121 array_merge( $conds, $conds2 ),
122 __METHOD__,
123 $options
126 if ( $res->numRows() > 0 ) {
127 $row1 = $res->current();
128 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
129 $res->rewind();
130 } else {
131 $this->error( "No images to process.", 4 );
134 foreach ( $res as $row ) {
135 $file = $repo->newFileFromRow( $row );
136 if ( $file->getUpgraded() ) {
137 // File was upgraded.
138 $upgraded++;
139 $newLength = strlen( $file->getMetadata() );
140 $oldLength = strlen( $row->img_metadata );
141 if ( $newLength < $oldLength - 5 ) {
142 // If after updating, the metadata is smaller then
143 // what it was before, that's probably not a good thing
144 // because we extract more data with time, not less.
145 // Thus this probably indicates an error of some sort,
146 // or at the very least is suspicious. Have the - 5 just
147 // to weed out any inconsequential changes.
148 $error++;
149 $this->output( "Warning: File:{$row->img_name} used to have " .
150 "$oldLength bytes of metadata but now has $newLength bytes.\n" );
151 } elseif ( $verbose ) {
152 $this->output( "Refreshed File:{$row->img_name}.\n" );
154 } else {
155 $leftAlone++;
156 if ( $force ) {
157 $file->upgradeRow();
158 $newLength = strlen( $file->getMetadata() );
159 $oldLength = strlen( $row->img_metadata );
160 if ( $newLength < $oldLength - 5 ) {
161 $error++;
162 $this->output( "Warning: File:{$row->img_name} used to have " .
163 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
165 if ( $verbose ) {
166 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
168 } else {
169 if ( $verbose ) {
170 $this->output( "Skipping File:{$row->img_name}.\n" );
175 $conds2 = array( 'img_name > ' . $dbw->addQuotes( $row->img_name ) );
176 wfWaitForSlaves();
177 } while ( $res->numRows() === $this->mBatchSize );
179 $total = $upgraded + $leftAlone;
180 if ( $force ) {
181 $this->output( "\nFinished refreshing file metadata for $total files. "
182 . "$upgraded needed to be refreshed, $leftAlone did not need to "
183 . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
184 } else {
185 $this->output( "\nFinished refreshing file metadata for $total files. "
186 . "$upgraded were refreshed, $leftAlone were already up to date, "
187 . "and $error refreshes were suspicious.\n" );
192 * @param DatabaseBase $dbw
193 * @return array
195 function getConditions( $dbw ) {
196 $conds = array();
198 $end = $this->getOption( 'end', false );
199 $mime = $this->getOption( 'mime', false );
200 $like = $this->getOption( 'metadata-contains', false );
202 if ( $end !== false ) {
203 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
205 if ( $mime !== false ) {
206 list( $major, $minor ) = File::splitMime( $mime );
207 $conds['img_major_mime'] = $major;
208 if ( $minor !== '*' ) {
209 $conds['img_minor_mime'] = $minor;
212 if ( $like ) {
213 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
216 return $conds;
220 * @param bool $force
221 * @param bool $brokenOnly
223 function setupParameters( $force, $brokenOnly ) {
224 global $wgUpdateCompatibleMetadata;
226 if ( $brokenOnly ) {
227 $wgUpdateCompatibleMetadata = false;
228 } else {
229 $wgUpdateCompatibleMetadata = true;
232 if ( $brokenOnly && $force ) {
233 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
238 $maintClass = 'RefreshImageMetadata';
239 require_once RUN_MAINTENANCE_IF_MAIN;