Make refreshImageMetadata not fail completely if it doesn't like a single file
[lhc/web/wiklou.git] / maintenance / refreshImageMetadata.php
1 <?php
2 /**
3 * Refresh image metadata fields. See also rebuildImages.php
4 *
5 * Usage: php refreshImageMetadata.php
6 *
7 * Copyright © 2011 Brian Wolff
8 * https://www.mediawiki.org/
9 *
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.
14 *
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.
19 *
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
24 *
25 * @file
26 * @author Brian Wolff
27 * @ingroup Maintenance
28 */
29
30 require_once __DIR__ . '/Maintenance.php';
31
32 /**
33 * Maintenance script to refresh image metadata fields.
34 *
35 * @ingroup Maintenance
36 */
37 class RefreshImageMetadata extends Maintenance {
38
39 /**
40 * @var Database
41 */
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 try {
143 // LocalFile will upgrade immediately here if obsolete
144 $file = $repo->newFileFromRow( $row );
145 if ( $file->getUpgraded() ) {
146 // File was upgraded.
147 $upgraded++;
148 $newLength = strlen( $file->getMetadata() );
149 $oldLength = strlen( $row->img_metadata );
150 if ( $newLength < $oldLength - 5 ) {
151 // If after updating, the metadata is smaller then
152 // what it was before, that's probably not a good thing
153 // because we extract more data with time, not less.
154 // Thus this probably indicates an error of some sort,
155 // or at the very least is suspicious. Have the - 5 just
156 // to weed out any inconsequential changes.
157 $error++;
158 $this->output(
159 "Warning: File:{$row->img_name} used to have " .
160 "$oldLength bytes of metadata but now has $newLength bytes.\n"
161 );
162 } elseif ( $verbose ) {
163 $this->output( "Refreshed File:{$row->img_name}.\n" );
164 }
165 } else {
166 $leftAlone++;
167 if ( $force ) {
168 $file->upgradeRow();
169 $newLength = strlen( $file->getMetadata() );
170 $oldLength = strlen( $row->img_metadata );
171 if ( $newLength < $oldLength - 5 ) {
172 $error++;
173 $this->output(
174 "Warning: File:{$row->img_name} used to have " .
175 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n"
176 );
177 }
178 if ( $verbose ) {
179 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
180 }
181 } else {
182 if ( $verbose ) {
183 $this->output( "Skipping File:{$row->img_name}.\n" );
184 }
185 }
186 }
187 } catch ( Exception $e ) {
188 $this->output( "{$row->img_name} failed. {$e->getMessage()}\n" );
189 }
190 }
191 $conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name ) ];
192 wfWaitForSlaves();
193 } while ( $res->numRows() === $this->mBatchSize );
194
195 $total = $upgraded + $leftAlone;
196 if ( $force ) {
197 $this->output( "\nFinished refreshing file metadata for $total files. "
198 . "$upgraded needed to be refreshed, $leftAlone did not need to "
199 . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
200 } else {
201 $this->output( "\nFinished refreshing file metadata for $total files. "
202 . "$upgraded were refreshed, $leftAlone were already up to date, "
203 . "and $error refreshes were suspicious.\n" );
204 }
205 }
206
207 /**
208 * @param Database $dbw
209 * @return array
210 */
211 function getConditions( $dbw ) {
212 $conds = [];
213
214 $end = $this->getOption( 'end', false );
215 $mime = $this->getOption( 'mime', false );
216 $mediatype = $this->getOption( 'mediatype', false );
217 $like = $this->getOption( 'metadata-contains', false );
218
219 if ( $end !== false ) {
220 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
221 }
222 if ( $mime !== false ) {
223 list( $major, $minor ) = File::splitMime( $mime );
224 $conds['img_major_mime'] = $major;
225 if ( $minor !== '*' ) {
226 $conds['img_minor_mime'] = $minor;
227 }
228 }
229 if ( $mediatype !== false ) {
230 $conds['img_media_type'] = $mediatype;
231 }
232 if ( $like ) {
233 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
234 }
235
236 return $conds;
237 }
238
239 /**
240 * @param bool $force
241 * @param bool $brokenOnly
242 */
243 function setupParameters( $force, $brokenOnly ) {
244 global $wgUpdateCompatibleMetadata;
245
246 if ( $brokenOnly ) {
247 $wgUpdateCompatibleMetadata = false;
248 } else {
249 $wgUpdateCompatibleMetadata = true;
250 }
251
252 if ( $brokenOnly && $force ) {
253 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
254 }
255 }
256 }
257
258 $maintClass = 'RefreshImageMetadata';
259 require_once RUN_MAINTENANCE_IF_MAIN;