76 parent::__construct();
77 $this->
addDescription(
'Script to update image metadata records' );
79 $this->
addOption(
'missing',
'Check for files without associated database record' );
80 $this->
addOption(
'dry-run',
'Only report, don\'t update the database' );
85 $this->dryrun = $this->
hasOption(
'dry-run' );
86 if ( $this->dryrun ) {
88 ->setReason(
'Dry run mode, image upgrades are suppressed' );
92 $this->crawlMissing();
101 private function getRepo() {
102 if ( $this->repo ===
null ) {
104 ->newCustomLocalRepo( [
106 'updateCompatibleMetadata' =>
true
113 private function build() {
115 $this->buildOldImage();
122 private function init( $count, $table ) {
123 $this->processed = 0;
125 $this->count = $count;
126 $this->startTime = microtime(
true );
127 $this->table = $table;
130 private function progress( $updated ) {
131 $this->updated += $updated;
133 if ( $this->processed % 100 != 0 ) {
136 $portion = $this->processed / $this->count;
137 $updateRate = $this->updated / $this->processed;
139 $now = microtime(
true );
140 $delta = $now - $this->startTime;
141 $estimatedTotalTime = $delta / $portion;
142 $eta = $this->startTime + $estimatedTotalTime;
143 $rate = $this->processed / $delta;
145 $this->
output( sprintf(
"%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
153 $updateRate * 100.0 ) );
157 private function buildTable( $table, $queryBuilder, $callback ) {
158 $count = $this->dbw->newSelectQueryBuilder()
159 ->select(
'count(*)' )
161 ->caller( __METHOD__ )->fetchField();
162 $this->init( $count, $table );
163 $this->
output(
"Processing $table...\n" );
165 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
167 foreach ( $result as $row ) {
168 $update = call_user_func( $callback, $row );
170 $this->progress( 1 );
172 $this->progress( 0 );
175 $this->
output(
"Finished $table... $this->updated of $this->processed rows updated\n" );
178 private function buildImage() {
179 $callback = [ $this,
'imageCallback' ];
180 $this->buildTable(
'image', FileSelectQueryBuilder::newForFile( $this->
getReplicaDB() ), $callback );
183 private function imageCallback( $row ) {
186 $file = $this->getRepo()->newFileFromRow( $row );
188 return $file->getUpgraded();
191 private function buildOldImage() {
192 $this->buildTable(
'oldimage', FileSelectQueryBuilder::newForOldFile( $this->
getReplicaDB() ),
193 [ $this,
'oldimageCallback' ] );
196 private function oldimageCallback( $row ) {
199 if ( $row->oi_archive_name ==
'' ) {
200 $this->
output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
204 $file = $this->getRepo()->newFileFromRow( $row );
206 return $file->getUpgraded();
209 private function crawlMissing() {
210 $this->getRepo()->enumFiles( [ $this,
'checkMissingImage' ] );
215 $row = $this->dbw->newSelectQueryBuilder()
216 ->select( [
'img_name' ] )
218 ->where( [
'img_name' => $filename ] )
219 ->caller( __METHOD__ )->fetchRow();
223 $this->addMissingImage( $filename, $fullpath );
227 private function addMissingImage( $filename, $fullpath ) {
228 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
231 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
232 if ( $altname != $filename ) {
233 if ( $this->dryrun ) {
234 $filename = $altname;
235 $this->
output(
"Estimating transcoding... $altname\n" );
239 $filename = $this->renameFile( $filename );
243 if ( $filename ==
'' ) {
244 $this->
output(
"Empty filename for $fullpath\n" );
248 if ( !$this->dryrun ) {
249 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
250 $pageText = SpecialUpload::getInitialPageText(
251 '(recovered file, missing upload log entry)'
253 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
254 $status = $file->recordUpload3(
256 '(recovered file, missing upload log entry)',
262 if ( !$status->isOK() ) {
263 $this->
output(
"Error uploading file $fullpath\n" );
268 $this->
output( $fullpath .
"\n" );
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.