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