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