MediaWiki REL1_39
rebuildImages.php
Go to the documentation of this file.
1<?php
33require_once __DIR__ . '/Maintenance.php';
34
37
47 protected $dbw;
48
50 private $dryrun;
51
53 private $repo;
54
56 private $updated;
57
59 private $processed;
60
62 private $count;
63
65 private $startTime;
66
68 private $table;
69
70 public function __construct() {
71 parent::__construct();
72 $this->addDescription( 'Script to update image metadata records' );
73
74 $this->addOption( 'missing', 'Check for files without associated database record' );
75 $this->addOption( 'dry-run', 'Only report, don\'t update the database' );
76 }
77
78 public function execute() {
79 $this->dbw = $this->getDB( DB_PRIMARY );
80 $this->dryrun = $this->hasOption( 'dry-run' );
81 if ( $this->dryrun ) {
82 MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
83 ->setReason( 'Dry run mode, image upgrades are suppressed' );
84 }
85
86 if ( $this->hasOption( 'missing' ) ) {
87 $this->crawlMissing();
88 } else {
89 $this->build();
90 }
91 }
92
96 private function getRepo() {
97 if ( $this->repo === null ) {
98 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()
99 ->newCustomLocalRepo( [
100 // make sure to update old, but compatible img_metadata fields.
101 'updateCompatibleMetadata' => true
102 ] );
103 }
104
105 return $this->repo;
106 }
107
108 private function build() {
109 $this->buildImage();
110 $this->buildOldImage();
111 }
112
117 private function init( $count, $table ) {
118 $this->processed = 0;
119 $this->updated = 0;
120 $this->count = $count;
121 $this->startTime = microtime( true );
122 $this->table = $table;
123 }
124
125 private function progress( $updated ) {
126 $this->updated += $updated;
127 $this->processed++;
128 if ( $this->processed % 100 != 0 ) {
129 return;
130 }
131 $portion = $this->processed / $this->count;
132 $updateRate = $this->updated / $this->processed;
133
134 $now = microtime( true );
135 $delta = $now - $this->startTime;
136 $estimatedTotalTime = $delta / $portion;
137 $eta = $this->startTime + $estimatedTotalTime;
138 $rate = $this->processed / $delta;
139
140 $this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
141 wfTimestamp( TS_DB, intval( $now ) ),
142 $portion * 100.0,
143 $this->table,
144 wfTimestamp( TS_DB, intval( $eta ) ),
145 $this->processed,
146 $this->count,
147 $rate,
148 $updateRate * 100.0 ) );
149 flush();
150 }
151
152 private function buildTable( $table, $key, $queryInfo, $callback ) {
153 $count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
154 $this->init( $count, $table );
155 $this->output( "Processing $table...\n" );
156
157 $result = $this->getDB( DB_REPLICA )->select(
158 $queryInfo['tables'], $queryInfo['fields'], [], __METHOD__, [], $queryInfo['joins']
159 );
160
161 foreach ( $result as $row ) {
162 $update = call_user_func( $callback, $row, null );
163 if ( $update ) {
164 $this->progress( 1 );
165 } else {
166 $this->progress( 0 );
167 }
168 }
169 $this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
170 }
171
172 private function buildImage() {
173 $callback = [ $this, 'imageCallback' ];
174 $this->buildTable( 'image', 'img_name', LocalFile::getQueryInfo(), $callback );
175 }
176
177 private function imageCallback( $row, $copy ) {
178 // Create a File object from the row
179 // This will also upgrade it
180 $file = $this->getRepo()->newFileFromRow( $row );
181
182 return $file->getUpgraded();
183 }
184
185 private function buildOldImage() {
186 $this->buildTable( 'oldimage', 'oi_archive_name', OldLocalFile::getQueryInfo(),
187 [ $this, 'oldimageCallback' ] );
188 }
189
190 private function oldimageCallback( $row, $copy ) {
191 // Create a File object from the row
192 // This will also upgrade it
193 if ( $row->oi_archive_name == '' ) {
194 $this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
195
196 return false;
197 }
198 $file = $this->getRepo()->newFileFromRow( $row );
199
200 return $file->getUpgraded();
201 }
202
203 private function crawlMissing() {
204 $this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] );
205 }
206
207 public function checkMissingImage( $fullpath ) {
208 $filename = wfBaseName( $fullpath );
209 $row = $this->dbw->selectRow( 'image',
210 [ 'img_name' ],
211 [ 'img_name' => $filename ],
212 __METHOD__ );
213
214 if ( !$row ) {
215 // file not registered
216 $this->addMissingImage( $filename, $fullpath );
217 }
218 }
219
220 private function addMissingImage( $filename, $fullpath ) {
221 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
222 $services = MediaWikiServices::getInstance();
223
224 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
225 if ( $altname != $filename ) {
226 if ( $this->dryrun ) {
227 $filename = $altname;
228 $this->output( "Estimating transcoding... $altname\n" );
229 } else {
230 // @fixme create renameFile()
231 // @phan-suppress-next-line PhanUndeclaredMethod See comment above...
232 $filename = $this->renameFile( $filename );
233 }
234 }
235
236 if ( $filename == '' ) {
237 $this->output( "Empty filename for $fullpath\n" );
238
239 return;
240 }
241 if ( !$this->dryrun ) {
242 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
244 '(recovered file, missing upload log entry)'
245 );
246 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
247 $status = $file->recordUpload3(
248 '',
249 '(recovered file, missing upload log entry)',
250 $pageText,
251 $user,
252 false,
253 $timestamp
254 );
255 if ( !$status->isOK() ) {
256 $this->output( "Error uploading file $fullpath\n" );
257
258 return;
259 }
260 }
261 $this->output( $fullpath . "\n" );
262 }
263}
264
265$maintClass = ImageBuilder::class;
266require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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:39
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.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Service locator for MediaWiki core services.
static getInitialPageText( $comment='', $license='', $copyStatus='', $source='', Config $config=null)
Get the initial image page text based on a comment and optional file status information.
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition User.php:806
const MAINTENANCE_SCRIPT_USER
Username used for various maintenance scripts.
Definition User.php:116
Advanced database interface for IDatabase handles that include maintenance methods.
const DB_REPLICA
Definition defines.php:26
const DB_PRIMARY
Definition defines.php:28
$maintClass
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42