78 parent::__construct();
79 $this->
addDescription(
'Script to update image metadata records' );
81 $this->
addOption(
'missing',
'Check for files without associated database record' );
82 $this->
addOption(
'dry-run',
'Only report, don\'t update the database' );
87 $this->dryrun = $this->
hasOption(
'dry-run' );
88 if ( $this->dryrun ) {
90 ->setReason(
'Dry run mode, image upgrades are suppressed' );
94 $this->crawlMissing();
103 private function getRepo() {
104 if ( $this->repo ===
null ) {
106 ->newCustomLocalRepo( [
108 'updateCompatibleMetadata' =>
true
115 private function build() {
117 $this->buildOldImage();
124 private function init( $count, $table ) {
125 $this->processed = 0;
127 $this->count = $count;
128 $this->startTime = microtime(
true );
129 $this->table = $table;
132 private function progress(
int $updated ) {
133 $this->updated += $updated;
135 if ( $this->processed % 100 != 0 ) {
138 $portion = $this->processed / $this->count;
139 $updateRate = $this->updated / $this->processed;
141 $now = microtime(
true );
142 $delta = $now - $this->startTime;
143 $estimatedTotalTime = $delta / $portion;
144 $eta = $this->startTime + $estimatedTotalTime;
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() {
181 $callback = [ $this,
'imageCallback' ];
182 $this->buildTable(
'image', FileSelectQueryBuilder::newForFile( $this->
getReplicaDB() ), $callback );
185 private function imageCallback( \stdClass $row ): bool {
188 $file = $this->getRepo()->newFileFromRow( $row );
190 return $file->getUpgraded();
193 private function buildOldImage() {
194 $this->buildTable(
'oldimage', FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ),
195 [ $this,
'oldimageCallback' ] );
198 private function oldimageCallback( \stdClass $row ): bool {
201 if ( $row->oi_archive_name ==
'' ) {
202 $this->output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
206 $file = $this->getRepo()->newFileFromRow( $row );
208 return $file->getUpgraded();
211 private function crawlMissing() {
212 $this->getRepo()->enumFiles( [ $this,
'checkMissingImage' ] );
217 $row = $this->dbw->newSelectQueryBuilder()
218 ->select( [
'img_name' ] )
220 ->where( [
'img_name' => $filename ] )
221 ->caller( __METHOD__ )->fetchRow();
225 $this->addMissingImage( $filename, $fullpath );
229 private function addMissingImage(
string $filename,
string $fullpath ) {
230 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
231 $services = $this->getServiceContainer();
233 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
234 if ( $altname != $filename ) {
235 if ( $this->dryrun ) {
236 $filename = $altname;
237 $this->output(
"Estimating transcoding... $altname\n" );
241 $filename = $this->renameFile( $filename );
245 if ( $filename ==
'' ) {
246 $this->output(
"Empty filename for $fullpath\n" );
250 if ( !$this->dryrun ) {
251 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
252 $pageText = SpecialUpload::getInitialPageText(
253 '(recovered file, missing upload log entry)'
255 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
256 $status = $file->recordUpload3(
258 '(recovered file, missing upload log entry)',
264 if ( !$status->isOK() ) {
265 $this->output(
"Error uploading file $fullpath\n" );
270 $this->output( $fullpath .
"\n" );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.