95dc10bdf808940c3aa96dc904283aee79162793
[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 DatabaseBase
41 */
42 protected $dbw;
43
44 function __construct() {
45 parent::__construct();
46
47 $this->mDescription = '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 'mime',
73 '(Inefficient!) Only refresh files with this mime type. Can accept wild-card image/*',
74 false,
75 true
76 );
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
84 );
85
86 }
87
88 public function execute() {
89 $force = $this->hasOption( 'force' );
90 $brokenOnly = $this->hasOption( 'broken-only' );
91 $verbose = $this->hasOption( 'verbose' );
92 $start = $this->getOption( 'start', false );
93 $this->setupParameters( $force, $brokenOnly );
94
95 $upgraded = 0;
96 $leftAlone = 0;
97 $error = 0;
98
99 $dbw = wfGetDB( DB_MASTER );
100 if ( $this->mBatchSize <= 0 ) {
101 $this->error( "Batch size is too low...", 12 );
102 }
103
104 $repo = RepoGroup::singleton()->getLocalRepo();
105 $conds = $this->getConditions( $dbw );
106
107 // For the WHERE img_name > 'foo' condition that comes after doing a batch
108 $conds2 = array();
109 if ( $start !== false ) {
110 $conds2[] = 'img_name >= ' . $dbw->addQuotes( $start );
111 }
112
113 $options = array(
114 'LIMIT' => $this->mBatchSize,
115 'ORDER BY' => 'img_name ASC',
116 );
117
118 do {
119 $res = $dbw->select(
120 'image',
121 '*',
122 array_merge( $conds, $conds2 ),
123 __METHOD__,
124 $options
125 );
126
127 if ( $res->numRows() > 0 ) {
128 $row1 = $res->current();
129 $this->output( "Processing next {$this->mBatchSize} rows starting with {$row1->img_name}.\n" );
130 $res->rewind();
131 } else {
132 $this->error( "No images to process.", 4 );
133 }
134
135 foreach ( $res as $row ) {
136 $file = $repo->newFileFromRow( $row );
137 if ( $file->getUpgraded() ) {
138 // File was upgraded.
139 $upgraded++;
140 $newLength = strlen( $file->getMetadata() );
141 $oldLength = strlen( $row->img_metadata );
142 if ( $newLength < $oldLength - 5 ) {
143 // If after updating, the metadata is smaller then
144 // what it was before, that's probably not a good thing
145 // because we extract more data with time, not less.
146 // Thus this probably indicates an error of some sort,
147 // or at the very least is suspicious. Have the - 5 just
148 // to weed out any inconsequential changes.
149 $error++;
150 $this->output( "Warning: File:{$row->img_name} used to have " .
151 "$oldLength bytes of metadata but now has $newLength bytes.\n" );
152 } elseif ( $verbose ) {
153 $this->output( "Refreshed File:{$row->img_name}.\n" );
154 }
155 } else {
156 $leftAlone++;
157 if ( $force ) {
158 $file->upgradeRow();
159 $newLength = strlen( $file->getMetadata() );
160 $oldLength = strlen( $row->img_metadata );
161 if ( $newLength < $oldLength - 5 ) {
162 $error++;
163 $this->output( "Warning: File:{$row->img_name} used to have " .
164 "$oldLength bytes of metadata but now has $newLength bytes. (forced)\n" );
165
166 }
167 if ( $verbose ) {
168 $this->output( "Forcibly refreshed File:{$row->img_name}.\n" );
169 }
170 }
171 else {
172 if ( $verbose ) {
173 $this->output( "Skipping File:{$row->img_name}.\n" );
174 }
175 }
176 }
177
178 }
179 $conds2 = array( 'img_name > ' . $dbw->addQuotes( $row->img_name ) );
180 wfWaitForSlaves();
181 } while ( $res->numRows() === $this->mBatchSize );
182
183 $total = $upgraded + $leftAlone;
184 if ( $force ) {
185 $this->output( "\nFinished refreshing file metadata for $total files. "
186 . "$upgraded needed to be refreshed, $leftAlone did not need to "
187 . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
188 } else {
189 $this->output( "\nFinished refreshing file metadata for $total files. "
190 . "$upgraded were refreshed, $leftAlone were already up to date, "
191 . "and $error refreshes were suspicious.\n" );
192 }
193 }
194
195 /**
196 * @param DatabaseBase $dbw
197 * @return array
198 */
199 function getConditions( $dbw ) {
200 $conds = array();
201
202 $end = $this->getOption( 'end', false );
203 $mime = $this->getOption( 'mime', false );
204 $like = $this->getOption( 'metadata-contains', false );
205
206 if ( $end !== false ) {
207 $conds[] = 'img_name <= ' . $dbw->addQuotes( $end );
208 }
209 if ( $mime !== false ) {
210 list( $major, $minor ) = File::splitMime( $mime );
211 $conds['img_major_mime'] = $major;
212 if ( $minor !== '*' ) {
213 $conds['img_minor_mime'] = $minor;
214 }
215 }
216 if ( $like ) {
217 $conds[] = 'img_metadata ' . $dbw->buildLike( $dbw->anyString(), $like, $dbw->anyString() );
218 }
219 return $conds;
220 }
221
222 /**
223 * @param bool $force
224 * @param bool $brokenOnly
225 */
226 function setupParameters( $force, $brokenOnly ) {
227 global $wgUpdateCompatibleMetadata;
228
229 if ( $brokenOnly ) {
230 $wgUpdateCompatibleMetadata = false;
231 } else {
232 $wgUpdateCompatibleMetadata = true;
233 }
234
235 if ( $brokenOnly && $force ) {
236 $this->error( 'Cannot use --broken-only and --force together. ', 2 );
237 }
238 }
239 }
240
241 $maintClass = 'RefreshImageMetadata';
242 require_once RUN_MAINTENANCE_IF_MAIN;