67 parent::__construct();
68 $this->addDescription(
'Script to update image metadata records' );
70 $this->addOption(
'missing',
'Check for files without associated database record' );
71 $this->addOption(
'dry-run',
'Only report, don\'t update the database' );
75 $this->dbw = $this->getPrimaryDB();
76 $this->dryrun = $this->hasOption(
'dry-run' );
77 if ( $this->dryrun ) {
78 $this->getServiceContainer()->getReadOnlyMode()
79 ->setReason(
'Dry run mode, image upgrades are suppressed' );
82 if ( $this->hasOption(
'missing' ) ) {
83 $this->crawlMissing();
92 private function getRepo() {
93 if ( $this->repo ===
null ) {
94 $this->repo = $this->getServiceContainer()->getRepoGroup()
95 ->newCustomLocalRepo( [
97 'updateCompatibleMetadata' =>
true
104 private function build() {
105 $migrationStage = $this->getServiceContainer()->getMainConfig()->get(
111 $this->buildOldImage();
121 private function init( $count, $table ) {
122 $this->processed = 0;
124 $this->count = $count;
125 $this->startTimer = ConvertibleTimestamp::hrtime();
126 $this->table = $table;
129 private function progress(
int $updated ) {
130 $this->updated += $updated;
132 if ( $this->processed % 100 != 0 ) {
135 $portion = $this->processed / $this->count;
136 $updateRate = $this->updated / $this->processed;
138 $now = microtime(
true );
139 $delta = ( ConvertibleTimestamp::hrtime() - $this->startTimer ) / 1e9;
140 $estimatedTotalTime = $delta / $portion;
144 $eta = $now + ( $estimatedTotalTime - $delta );
145 $rate = $this->processed / $delta;
147 $this->output( sprintf(
"%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
155 $updateRate * 100.0 ) );
159 private function buildTable(
string $table, SelectQueryBuilder $queryBuilder, callable $callback ) {
160 $count = $this->dbw->newSelectQueryBuilder()
161 ->select(
'count(*)' )
163 ->caller( __METHOD__ )->fetchField();
164 $this->init( $count, $table );
165 $this->output(
"Processing $table...\n" );
167 $result = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
169 foreach ( $result as $row ) {
170 $update = $callback( $row );
172 $this->progress( 1 );
174 $this->progress( 0 );
177 $this->output(
"Finished $table... $this->updated of $this->processed rows updated\n" );
180 private function buildImage() {
184 $this->imageCallback( ... )
188 private function imageCallback( \stdClass $row ): bool {
191 $file = $this->getRepo()->newFileFromRow( $row );
193 return $file->getUpgraded();
196 private function buildOldImage() {
199 FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ),
200 $this->oldimageCallback( ... )
204 private function oldimageCallback( \stdClass $row ): bool {
207 if ( $row->oi_archive_name ==
'' ) {
208 $this->output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
212 $file = $this->getRepo()->newFileFromRow( $row );
214 return $file->getUpgraded();
217 private function buildFile() {
220 FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ),
221 $this->fileCallback( ... )
225 private function fileCallback( \stdClass $row ): bool {
228 $file = $this->getRepo()->newFileFromRow( $row );
230 return $file->getUpgraded();
233 private function crawlMissing() {
234 $this->getRepo()->enumFiles( $this->checkMissingImage( ... ) );
237 private function checkMissingImage(
string $fullpath ) {
240 $row = FileSelectQueryBuilder::newForFile( $this->getReplicaDB() )
241 ->where( [
'img_name' => $filename ] )
242 ->caller( __METHOD__ )->fetchRow();
246 $this->addMissingImage( $filename, $fullpath );
250 private function addMissingImage(
string $filename,
string $fullpath ) {
251 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
252 $services = $this->getServiceContainer();
254 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
255 if ( $altname != $filename ) {
256 if ( $this->dryrun ) {
257 $filename = $altname;
258 $this->output(
"Estimating transcoding... $altname\n" );
262 $filename = $this->renameFile( $filename );
266 if ( $filename ==
'' ) {
267 $this->output(
"Empty filename for $fullpath\n" );
271 if ( !$this->dryrun ) {
272 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
273 $pageText = SpecialUpload::getInitialPageText(
274 '(recovered file, missing upload log entry)'
276 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
277 $status = $file->recordUpload3(
279 '(recovered file, missing upload log entry)',
285 if ( !$status->isOK() ) {
286 $this->output(
"Error uploading file $fullpath\n" );
291 $this->output( $fullpath .
"\n" );