66 parent::__construct();
67 $this->
addDescription(
'Script to update image metadata records' );
69 $this->
addOption(
'missing',
'Check for files without associated database record' );
70 $this->
addOption(
'dry-run',
'Only report, don\'t update the database' );
75 $this->dryrun = $this->
hasOption(
'dry-run' );
76 if ( $this->dryrun ) {
78 ->setReason(
'Dry run mode, image upgrades are suppressed' );
82 $this->crawlMissing();
91 private function getRepo() {
92 if ( $this->repo ===
null ) {
94 ->newCustomLocalRepo( [
96 'updateCompatibleMetadata' =>
true
103 private function build() {
105 MainConfigNames::FileSchemaMigrationStage
110 $this->buildOldImage();
120 private function init( $count, $table ) {
121 $this->processed = 0;
123 $this->count = $count;
124 $this->startTime = microtime(
true );
125 $this->table = $table;
128 private function progress(
int $updated ) {
129 $this->updated += $updated;
131 if ( $this->processed % 100 != 0 ) {
134 $portion = $this->processed / $this->count;
135 $updateRate = $this->updated / $this->processed;
137 $now = microtime(
true );
138 $delta = $now - $this->startTime;
139 $estimatedTotalTime = $delta / $portion;
140 $eta = $this->startTime + $estimatedTotalTime;
141 $rate = $this->processed / $delta;
143 $this->
output( sprintf(
"%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
151 $updateRate * 100.0 ) );
155 private function buildTable(
string $table,
SelectQueryBuilder $queryBuilder, callable $callback ) {
156 $count = $this->dbw->newSelectQueryBuilder()
157 ->select(
'count(*)' )
159 ->caller( __METHOD__ )->fetchField();
160 $this->init( $count, $table );
161 $this->
output(
"Processing $table...\n" );
163 $result = $queryBuilder->
caller( __METHOD__ )->fetchResultSet();
165 foreach ( $result as $row ) {
166 $update = $callback( $row );
168 $this->progress( 1 );
170 $this->progress( 0 );
173 $this->
output(
"Finished $table... $this->updated of $this->processed rows updated\n" );
176 private function buildImage() {
179 FileSelectQueryBuilder::newForFile( $this->
getReplicaDB() ),
180 $this->imageCallback( ... )
184 private function imageCallback( \stdClass $row ): bool {
187 $file = $this->getRepo()->newFileFromRow( $row );
189 return $file->getUpgraded();
192 private function buildOldImage() {
195 FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ),
196 $this->oldimageCallback( ... )
200 private function oldimageCallback( \stdClass $row ): bool {
203 if ( $row->oi_archive_name ==
'' ) {
204 $this->output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
208 $file = $this->getRepo()->newFileFromRow( $row );
210 return $file->getUpgraded();
213 private function buildFile() {
216 FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ),
217 $this->fileCallback( ... )
221 private function fileCallback( \stdClass $row ): bool {
224 $file = $this->getRepo()->newFile( $row->file_name );
226 return $file->getUpgraded();
229 private function crawlMissing() {
230 $this->getRepo()->enumFiles( $this->checkMissingImage( ... ) );
233 private function checkMissingImage(
string $fullpath ) {
236 $row = FileSelectQueryBuilder::newForFile( $this->getReplicaDB() )
237 ->where( [
'img_name' => $filename ] )
238 ->caller( __METHOD__ )->fetchRow();
242 $this->addMissingImage( $filename, $fullpath );
246 private function addMissingImage(
string $filename,
string $fullpath ) {
247 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
248 $services = $this->getServiceContainer();
250 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
251 if ( $altname != $filename ) {
252 if ( $this->dryrun ) {
253 $filename = $altname;
254 $this->output(
"Estimating transcoding... $altname\n" );
258 $filename = $this->renameFile( $filename );
262 if ( $filename ==
'' ) {
263 $this->output(
"Empty filename for $fullpath\n" );
267 if ( !$this->dryrun ) {
268 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
269 $pageText = SpecialUpload::getInitialPageText(
270 '(recovered file, missing upload log entry)'
272 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
273 $status = $file->recordUpload3(
275 '(recovered file, missing upload log entry)',
281 if ( !$status->isOK() ) {
282 $this->output(
"Error uploading file $fullpath\n" );
287 $this->output( $fullpath .
"\n" );