MediaWiki master
refreshImageMetadata.php
Go to the documentation of this file.
1<?php
30// @codeCoverageIgnoreStart
31require_once __DIR__ . '/Maintenance.php';
32// @codeCoverageIgnoreEnd
33
40
46class RefreshImageMetadata extends Maintenance {
47
51 protected $dbw;
52
53 public function __construct() {
54 parent::__construct();
55
56 $this->addDescription( 'Script to update image metadata records' );
57 $this->setBatchSize( 200 );
58
59 $this->addOption(
60 'force',
61 'Reload metadata from file even if the metadata looks ok',
62 false,
63 false,
64 'f'
65 );
66 $this->addOption(
67 'broken-only',
68 'Only fix really broken records, leave old but still compatible records alone.'
69 );
70 $this->addOption(
71 'convert-to-json',
72 'Fix records with an out of date serialization format.'
73 );
74 $this->addOption(
75 'split',
76 'Enable splitting out large metadata items to the text table. Implies --convert-to-json.'
77 );
78 $this->addOption(
79 'verbose',
80 'Output extra information about each upgraded/non-upgraded file.',
81 false,
82 false,
83 'v'
84 );
85 $this->addOption( 'start', 'Name of file to start with', false, true );
86 $this->addOption( 'end', 'Name of file to end with', false, true );
87
88 $this->addOption(
89 'mediatype',
90 'Only refresh files with this media type, e.g. BITMAP, UNKNOWN etc.',
91 false,
92 true
93 );
94 $this->addOption(
95 'mime',
96 "Only refresh files with this MIME type. Can accept wild-card 'image/*'. "
97 . "Potentially inefficient unless 'mediatype' is also specified",
98 false,
99 true
100 );
101 $this->addOption(
102 'metadata-contains',
103 '(Inefficient!) Only refresh files where the img_metadata field '
104 . 'contains this string. Can be used if its known a specific '
105 . 'property was being extracted incorrectly.',
106 false,
107 true
108 );
109 $this->addOption(
110 'sleep',
111 'Time to sleep between each batch (in seconds). Default: 0',
112 false,
113 true
114 );
115 $this->addOption( 'oldimage', 'Run and refresh on oldimage table.' );
116 }
117
118 public function execute() {
119 $force = $this->hasOption( 'force' );
120 $brokenOnly = $this->hasOption( 'broken-only' );
121 $verbose = $this->hasOption( 'verbose' );
122 $start = $this->getOption( 'start', false );
123 $split = $this->hasOption( 'split' );
124 $sleep = (int)$this->getOption( 'sleep', 0 );
125 $reserialize = $this->hasOption( 'convert-to-json' );
126 $oldimage = $this->hasOption( 'oldimage' );
127
128 $dbw = $this->getPrimaryDB();
129 if ( $oldimage ) {
130 $fieldPrefix = 'oi_';
131 $queryBuilderTemplate = FileSelectQueryBuilder::newForOldFile( $dbw );
132 } else {
133 $fieldPrefix = 'img_';
134 $queryBuilderTemplate = FileSelectQueryBuilder::newForFile( $dbw );
135 }
136
137 $upgraded = 0;
138 $leftAlone = 0;
139 $error = 0;
140 $batchSize = intval( $this->getBatchSize() );
141 if ( $batchSize <= 0 ) {
142 $this->fatalError( "Batch size is too low...", 12 );
143 }
144 $repo = $this->newLocalRepo( $force, $brokenOnly, $reserialize, $split );
145 $this->setConditions( $dbw, $queryBuilderTemplate, $fieldPrefix );
146 $queryBuilderTemplate
147 ->orderBy( $fieldPrefix . 'name', SelectQueryBuilder::SORT_ASC )
148 ->limit( $batchSize );
149
150 $batchCondition = [];
151 // For the WHERE img_name > 'foo' condition that comes after doing a batch
152 if ( $start !== false ) {
153 $batchCondition[] = $dbw->expr( $fieldPrefix . 'name', '>=', $start );
154 }
155 do {
156 $queryBuilder = clone $queryBuilderTemplate;
157 $res = $queryBuilder->andWhere( $batchCondition )
158 ->caller( __METHOD__ )->fetchResultSet();
159 $nameField = $fieldPrefix . 'name';
160 if ( $res->numRows() > 0 ) {
161 $row1 = $res->current();
162 $this->output( "Processing next {$res->numRows()} row(s) starting with {$row1->$nameField}.\n" );
163 $res->rewind();
164 }
165
166 foreach ( $res as $row ) {
167 try {
168 // LocalFile will upgrade immediately here if obsolete
169 $file = $repo->newFileFromRow( $row );
170 $file->maybeUpgradeRow();
171 if ( $file->getUpgraded() ) {
172 // File was upgraded.
173 $upgraded++;
174 $this->output( "Refreshed File:{$row->$nameField}.\n" );
175 } else {
176 $leftAlone++;
177 if ( $force ) {
178 $file->upgradeRow();
179 if ( $verbose ) {
180 $this->output( "Forcibly refreshed File:{$row->$nameField}.\n" );
181 }
182 } else {
183 if ( $verbose ) {
184 $this->output( "Skipping File:{$row->$nameField}.\n" );
185 }
186 }
187 }
188 } catch ( Exception $e ) {
189 $this->output( "{$row->$nameField} failed. {$e->getMessage()}\n" );
190 }
191 }
192 if ( $res->numRows() > 0 ) {
193 // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
194 $batchCondition = [ $dbw->expr( $fieldPrefix . 'name', '>', $row->$nameField ) ];
195 }
196 $this->waitForReplication();
197 if ( $sleep ) {
198 sleep( $sleep );
199 }
200 } while ( $res->numRows() === $batchSize );
201
202 $total = $upgraded + $leftAlone;
203 if ( $force ) {
204 $this->output( "\nFinished refreshing file metadata for $total files. "
205 . "$upgraded needed to be refreshed, $leftAlone did not need to "
206 . "be but were refreshed anyways, and $error refreshes were suspicious.\n" );
207 } else {
208 $this->output( "\nFinished refreshing file metadata for $total files. "
209 . "$upgraded were refreshed, $leftAlone were already up to date, "
210 . "and $error refreshes were suspicious.\n" );
211 }
212 }
213
220 private function setConditions( IReadableDatabase $dbw, SelectQueryBuilder $queryBuilder, $fieldPrefix ) {
221 $end = $this->getOption( 'end', false );
222 $mime = $this->getOption( 'mime', false );
223 $mediatype = $this->getOption( 'mediatype', false );
224 $like = $this->getOption( 'metadata-contains', false );
225
226 if ( $end !== false ) {
227 $queryBuilder->andWhere( $dbw->expr( $fieldPrefix . 'name', '<=', $end ) );
228 }
229 if ( $mime !== false ) {
230 [ $major, $minor ] = File::splitMime( $mime );
231 $queryBuilder->andWhere( [ $fieldPrefix . 'major_mime' => $major ] );
232 if ( $minor !== '*' ) {
233 $queryBuilder->andWhere( [ $fieldPrefix . 'minor_mime' => $minor ] );
234 }
235 }
236 if ( $mediatype !== false ) {
237 $queryBuilder->andWhere( [ $fieldPrefix . 'media_type' => $mediatype ] );
238 }
239 if ( $like ) {
240 $queryBuilder->andWhere(
241 $dbw->expr( $fieldPrefix . 'metadata', IExpression::LIKE,
242 new LikeValue( $dbw->anyString(), $like, $dbw->anyString() ) )
243 );
244 }
245 }
246
255 private function newLocalRepo( $force, $brokenOnly, $reserialize, $split ): LocalRepo {
256 if ( $brokenOnly && $force ) {
257 $this->fatalError( 'Cannot use --broken-only and --force together. ', 2 );
258 }
259 $reserialize = $reserialize || $split;
260 if ( $brokenOnly && $reserialize ) {
261 $this->fatalError( 'Cannot use --broken-only with --convert-to-json or --split. ',
262 2 );
263 }
264
265 $overrides = [
266 'updateCompatibleMetadata' => !$brokenOnly,
267 ];
268 if ( $reserialize ) {
269 $overrides['reserializeMetadata'] = true;
270 $overrides['useJsonMetadata'] = true;
271 }
272 if ( $split ) {
273 $overrides['useSplitMetadata'] = true;
274 }
275
276 return $this->getServiceContainer()->getRepoGroup()
277 ->newCustomLocalRepo( $overrides );
278 }
279}
280
281// @codeCoverageIgnoreStart
282$maintClass = RefreshImageMetadata::class;
283require_once RUN_MAINTENANCE_IF_MAIN;
284// @codeCoverageIgnoreEnd
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
static splitMime(?string $mime)
Split an internet media type into its two components; if not a two-part name, set the minor type to '...
Definition File.php:320
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:49
Maintenance script to refresh image metadata fields.
IMaintainableDatabase $dbw
Content of like value.
Definition LikeValue.php:14
Build SELECT queries with a fluent interface.
andWhere( $conds)
Add conditions to the query.
Advanced database interface for IDatabase handles that include maintenance methods.
A database connection without write operations.
expr(string $field, string $op, $value)
See Expression::__construct()
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.