MediaWiki master
rebuildImages.php
Go to the documentation of this file.
1<?php
33// @codeCoverageIgnoreStart
34require_once __DIR__ . '/Maintenance.php';
35// @codeCoverageIgnoreEnd
36
44
54 protected $dbw;
55
57 private $dryrun;
58
60 private $repo;
61
63 private $updated;
64
66 private $processed;
67
69 private $count;
70
72 private $startTime;
73
75 private $table;
76
77 public function __construct() {
78 parent::__construct();
79 $this->addDescription( 'Script to update image metadata records' );
80
81 $this->addOption( 'missing', 'Check for files without associated database record' );
82 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
83 }
84
85 public function execute() {
86 $this->dbw = $this->getPrimaryDB();
87 $this->dryrun = $this->hasOption( 'dry-run' );
88 if ( $this->dryrun ) {
89 $this->getServiceContainer()->getReadOnlyMode()
90 ->setReason( 'Dry run mode, image upgrades are suppressed' );
91 }
92
93 if ( $this->hasOption( 'missing' ) ) {
94 $this->crawlMissing();
95 } else {
96 $this->build();
97 }
98 }
99
103 private function getRepo() {
104 if ( $this->repo === null ) {
105 $this->repo = $this->getServiceContainer()->getRepoGroup()
106 ->newCustomLocalRepo( [
107 // make sure to update old, but compatible img_metadata fields.
108 'updateCompatibleMetadata' => true
109 ] );
110 }
111
112 return $this->repo;
113 }
114
115 private function build() {
116 $this->buildImage();
117 $this->buildOldImage();
118 }
119
124 private function init( $count, $table ) {
125 $this->processed = 0;
126 $this->updated = 0;
127 $this->count = $count;
128 $this->startTime = microtime( true );
129 $this->table = $table;
130 }
131
132 private function progress( int $updated ) {
133 $this->updated += $updated;
134 $this->processed++;
135 if ( $this->processed % 100 != 0 ) {
136 return;
137 }
138 $portion = $this->processed / $this->count;
139 $updateRate = $this->updated / $this->processed;
140
141 $now = microtime( true );
142 $delta = $now - $this->startTime;
143 $estimatedTotalTime = $delta / $portion;
144 $eta = $this->startTime + $estimatedTotalTime;
145 $rate = $this->processed / $delta;
146
147 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
148 wfTimestamp( TS_DB, intval( $now ) ),
149 $portion * 100.0,
150 $this->table,
151 wfTimestamp( TS_DB, intval( $eta ) ),
152 $this->processed,
153 $this->count,
154 $rate,
155 $updateRate * 100.0 ) );
156 flush();
157 }
158
159 private function buildTable( string $table, SelectQueryBuilder $queryBuilder, callable $callback ) {
160 $count = $this->dbw->newSelectQueryBuilder()
161 ->select( 'count(*)' )
162 ->from( $table )
163 ->caller( __METHOD__ )->fetchField();
164 $this->init( $count, $table );
165 $this->output( "Processing $table...\n" );
166
167 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
168
169 foreach ( $result as $row ) {
170 $update = $callback( $row );
171 if ( $update ) {
172 $this->progress( 1 );
173 } else {
174 $this->progress( 0 );
175 }
176 }
177 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
178 }
179
180 private function buildImage() {
181 $callback = [ $this, 'imageCallback' ];
182 $this->buildTable( 'image', FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ), $callback );
183 }
184
185 private function imageCallback( \stdClass $row ): bool {
186 // Create a File object from the row
187 // This will also upgrade it
188 $file = $this->getRepo()->newFileFromRow( $row );
189
190 return $file->getUpgraded();
191 }
192
193 private function buildOldImage() {
194 $this->buildTable( 'oldimage', FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ),
195 [ $this, 'oldimageCallback' ] );
196 }
197
198 private function oldimageCallback( \stdClass $row ): bool {
199 // Create a File object from the row
200 // This will also upgrade it
201 if ( $row->oi_archive_name == '' ) {
202 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
203
204 return false;
205 }
206 $file = $this->getRepo()->newFileFromRow( $row );
207
208 return $file->getUpgraded();
209 }
210
211 private function crawlMissing() {
212 $this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] );
213 }
214
215 public function checkMissingImage( string $fullpath ) {
216 $filename = wfBaseName( $fullpath );
217 $row = $this->dbw->newSelectQueryBuilder()
218 ->select( [ 'img_name' ] )
219 ->from( 'image' )
220 ->where( [ 'img_name' => $filename ] )
221 ->caller( __METHOD__ )->fetchRow();
222
223 if ( !$row ) {
224 // file not registered
225 $this->addMissingImage( $filename, $fullpath );
226 }
227 }
228
229 private function addMissingImage( string $filename, string $fullpath ) {
230 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
231 $services = $this->getServiceContainer();
232
233 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
234 if ( $altname != $filename ) {
235 if ( $this->dryrun ) {
236 $filename = $altname;
237 $this->output( "Estimating transcoding... $altname\n" );
238 } else {
239 // @fixme create renameFile()
240 // @phan-suppress-next-line PhanUndeclaredMethod See comment above...
241 $filename = $this->renameFile( $filename );
242 }
243 }
244
245 if ( $filename == '' ) {
246 $this->output( "Empty filename for $fullpath\n" );
247
248 return;
249 }
250 if ( !$this->dryrun ) {
251 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
252 $pageText = SpecialUpload::getInitialPageText(
253 '(recovered file, missing upload log entry)'
254 );
255 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
256 $status = $file->recordUpload3(
257 '',
258 '(recovered file, missing upload log entry)',
259 $pageText,
260 $user,
261 false,
262 $timestamp
263 );
264 if ( !$status->isOK() ) {
265 $this->output( "Error uploading file $fullpath\n" );
266
267 return;
268 }
269 }
270 $this->output( $fullpath . "\n" );
271 }
272}
273
274// @codeCoverageIgnoreStart
275$maintClass = ImageBuilder::class;
276require_once RUN_MAINTENANCE_IF_MAIN;
277// @codeCoverageIgnoreEnd
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
Maintenance script to update image metadata records.
__construct()
Default constructor.
checkMissingImage(string $fullpath)
IMaintainableDatabase $dbw
execute()
Do the actual work.
Local repository that stores files in the local filesystem and registers them in the wiki's own datab...
Definition LocalRepo.php:57
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Form for uploading media files.
User class for the MediaWiki software.
Definition User.php:121
Build SELECT queries with a fluent interface.
caller( $fname)
Set the method name to be included in an SQL comment.
Advanced database interface for IDatabase handles that include maintenance methods.
$maintClass