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