RC Filters: don't bind onMenuToggle twice
[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 use Wikimedia\Rdbms\IDatabase;
33 use Wikimedia\Rdbms\IMaintainableDatabase;
34
35 /**
36 * Maintenance script to refresh image metadata fields.
37 *
38 * @ingroup Maintenance
39 */
40 class RefreshImageMetadata extends Maintenance {
41
42 /**
43 * @var IMaintainableDatabase
44 */
45 protected $dbw;
46
47 function __construct() {
48 parent::__construct();
49
50 $this->addDescription( 'Script to update image metadata records' );
51 $this->setBatchSize( 200 );
52
53 $this->addOption(
54 'force',
55 'Reload metadata from file even if the metadata looks ok',
56 false,
57 false,
58 'f'
59 );
60 $this->addOption(
61 'broken-only',
62 'Only fix really broken records, leave old but still compatible records alone.'
63 );
64 $this->addOption(
65 'verbose',
66 'Output extra information about each upgraded/non-upgraded file.',
67 false,
68 false,
69 'v'
70 );
71 $this->addOption( 'start', 'Name of file to start with', false, true );
72 $this->addOption( 'end', 'Name of file to end with', false, true );
73
74 $this->addOption(
75 'mediatype',
76 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
77 false,
78 true
79 );
80 $this->addOption(
81 'mime',
82 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
83 . "Potentially inefficient unless 'mediatype' is also specified",
84 false,
85 true
86 );
87 $this->addOption(
88 'metadata-contains',
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.',
92 false,
93 true
94 );
95 }
96
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 );
103
104 $upgraded = 0;
105 $leftAlone = 0;
106 $error = 0;
107
108 $dbw = $this->getDB( DB_MASTER );
109 if ( $this->mBatchSize <= 0 ) {
110 $this->error( "Batch size is too low...", 12 );
111 }
112
113 $repo = RepoGroup::singleton()->getLocalRepo();
114 $conds = $this->getConditions( $dbw );
115
116 // For the WHERE img_name > 'foo' condition that comes after doing a batch
117 $conds2 = [];
118 if ( $start !== false ) {
119 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
120 }
121
122 $options = [
123 'LIMIT' => $this->mBatchSize,
124 'ORDER BY' => 'img_name ASC',
125 ];
126
127 do {
128 $res = $dbw->select(
129 'image',
130 '*',
131 array_merge( $conds, $conds2 ),
132 __METHOD__,
133 $options
134 );
135
136 if ( $res->numRows() > 0 ) {
137 $row1 = $res->current();
138 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
139 $res->rewind();
140 } else {
141 $this->error( "No images to process.", 4 );
142 }
143
144 foreach ( $res as $row ) {
145 try {
146 // LocalFile will upgrade immediately here if obsolete
147 $file = $repo->newFileFromRow( $row );
148 if ( $file->getUpgraded() ) {
149 // File was upgraded.
150 $upgraded++;
151 $newLength = strlen( $file->getMetadata() );
152 $oldLength = strlen( $row->img_metadata );
153 if ( $newLength < $oldLength - 5 ) {
154 // If after updating, the metadata is smaller then
155 // what it was before, that's probably not a good thing
156 // because we extract more data with time, not less.
157 // Thus this probably indicates an error of some sort,
158 // or at the very least is suspicious. Have the - 5 just
159 // to weed out any inconsequential changes.
160 $error++;
161 $this->output(
162 "Warning: File:{$row->img_name} used to have " .
163 "$oldLength bytes of metadata but now has $newLength bytes.\n"
164 );
165 } elseif ( $verbose ) {
166 $this->output( "Refreshed File:{$row->img_name}.\n" );
167 }
168 } else {
169 $leftAlone++;
170 if ( $force ) {
171 $file->upgradeRow();
172 $newLength = strlen( $file->getMetadata() );
173 $oldLength = strlen( $row->img_metadata );
174 if ( $newLength < $oldLength - 5 ) {
175 $error++;
176 $this->output(
177 "Warning: File:{$row->img_name} used to have " .
178 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n"
179 );
180 }
181 if ( $verbose ) {
182 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
183 }
184 } else {
185 if ( $verbose ) {
186 $this->output( "Skipping File:{$row->img_name}.\n" );
187 }
188 }
189 }
190 } catch ( Exception $e ) {
191 $this->output( "{$row->img_name} failed. {$e->getMessage()}\n" );
192 }
193 }
194 $conds2 = [ 'img_name > ' . $dbw->addQuotes( $row->img_name ) ];
195 wfWaitForSlaves();
196 } while ( $res->numRows() === $this->mBatchSize );
197
198 $total = $upgraded + $leftAlone;
199 if ( $force ) {
200 $this->output( "\nFinished refreshing file metadata for $total files. "
201 . "$upgraded needed to be refreshed, $leftAlone did not need to "
202 . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
203 } else {
204 $this->output( "\nFinished refreshing file metadata for $total files. "
205 . "$upgraded were refreshed, $leftAlone were already up to date, "
206 . "and $error refreshes were suspicious.\n" );
207 }
208 }
209
210 /**
211 * @param IDatabase $dbw
212 * @return array
213 */
214 function getConditions( $dbw ) {
215 $conds = [];
216
217 $end = $this->getOption( 'end', false );
218 $mime = $this->getOption( 'mime', false );
219 $mediatype = $this->getOption( 'mediatype', false );
220 $like = $this->getOption( 'metadata-contains', false );
221
222 if ( $end !== false ) {
223 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
224 }
225 if ( $mime !== false ) {
226 list( $major, $minor ) = File::splitMime( $mime );
227 $conds['img_major_mime'] = $major;
228 if ( $minor !== '*' ) {
229 $conds['img_minor_mime'] = $minor;
230 }
231 }
232 if ( $mediatype !== false ) {
233 $conds['img_media_type'] = $mediatype;
234 }
235 if ( $like ) {
236 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
237 }
238
239 return $conds;
240 }
241
242 /**
243 * @param bool $force
244 * @param bool $brokenOnly
245 */
246 function setupParameters( $force, $brokenOnly ) {
247 global $wgUpdateCompatibleMetadata;
248
249 if ( $brokenOnly ) {
250 $wgUpdateCompatibleMetadata = false;
251 } else {
252 $wgUpdateCompatibleMetadata = true;
253 }
254
255 if ( $brokenOnly && $force ) {
256 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
257 }
258 }
259 }
260
261 $maintClass = 'RefreshImageMetadata';
262 require_once RUN_MAINTENANCE_IF_MAIN;