75 parent::__construct();
76 $this->
addDescription(
'Script to update image metadata records' );
78 $this->
addOption(
'missing',
'Check for files without associated database record' );
79 $this->
addOption(
'dry-run',
'Only report, don\'t update the database' );
84 $this->dryrun = $this->
hasOption(
'dry-run' );
85 if ( $this->dryrun ) {
87 ->setReason(
'Dry run mode, image upgrades are suppressed' );
91 $this->crawlMissing();
100 private function getRepo() {
101 if ( $this->repo ===
null ) {
103 ->newCustomLocalRepo( [
105 'updateCompatibleMetadata' =>
true
112 private function build() {
114 $this->buildOldImage();
121 private function init( $count, $table ) {
122 $this->processed = 0;
124 $this->count = $count;
125 $this->startTime = microtime(
true );
126 $this->table = $table;
129 private function progress( $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 = $now - $this->startTime;
140 $estimatedTotalTime = $delta / $portion;
141 $eta = $this->startTime + $estimatedTotalTime;
142 $rate = $this->processed / $delta;
144 $this->
output( sprintf(
"%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
152 $updateRate * 100.0 ) );
156 private function buildTable( $table, $queryBuilder, $callback ) {
157 $count = $this->dbw->newSelectQueryBuilder()
158 ->select(
'count(*)' )
160 ->caller( __METHOD__ )->fetchField();
161 $this->init( $count, $table );
162 $this->
output(
"Processing $table...\n" );
164 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
166 foreach ( $result as $row ) {
167 $update = call_user_func( $callback, $row );
169 $this->progress( 1 );
171 $this->progress( 0 );
174 $this->
output(
"Finished $table... $this->updated of $this->processed rows updated\n" );
177 private function buildImage() {
178 $callback = [ $this,
'imageCallback' ];
179 $this->buildTable(
'image', FileSelectQueryBuilder::newForFile( $this->
getReplicaDB() ), $callback );
182 private function imageCallback( $row ) {
185 $file = $this->getRepo()->newFileFromRow( $row );
187 return $file->getUpgraded();
190 private function buildOldImage() {
191 $this->buildTable(
'oldimage', FileSelectQueryBuilder::newForOldFile( $this->
getReplicaDB() ),
192 [ $this,
'oldimageCallback' ] );
195 private function oldimageCallback( $row ) {
198 if ( $row->oi_archive_name ==
'' ) {
199 $this->
output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
203 $file = $this->getRepo()->newFileFromRow( $row );
205 return $file->getUpgraded();
208 private function crawlMissing() {
209 $this->getRepo()->enumFiles( [ $this,
'checkMissingImage' ] );
214 $row = $this->dbw->newSelectQueryBuilder()
215 ->select( [
'img_name' ] )
217 ->where( [
'img_name' => $filename ] )
218 ->caller( __METHOD__ )->fetchRow();
222 $this->addMissingImage( $filename, $fullpath );
226 private function addMissingImage( $filename, $fullpath ) {
227 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
230 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
231 if ( $altname != $filename ) {
232 if ( $this->dryrun ) {
233 $filename = $altname;
234 $this->
output(
"Estimating transcoding... $altname\n" );
238 $filename = $this->renameFile( $filename );
242 if ( $filename ==
'' ) {
243 $this->
output(
"Empty filename for $fullpath\n" );
247 if ( !$this->dryrun ) {
248 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
249 $pageText = SpecialUpload::getInitialPageText(
250 '(recovered file, missing upload log entry)'
252 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
253 $status = $file->recordUpload3(
255 '(recovered file, missing upload log entry)',
261 if ( !$status->isOK() ) {
262 $this->
output(
"Error uploading file $fullpath\n" );
267 $this->
output( $fullpath .
"\n" );
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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.