73 parent::__construct();
74 $this->
addDescription(
'Script to update image metadata records' );
76 $this->
addOption(
'missing',
'Check for files without associated database record' );
77 $this->
addOption(
'dry-run',
'Only report, don\'t update the database' );
82 $this->dryrun = $this->
hasOption(
'dry-run' );
83 if ( $this->dryrun ) {
85 ->setReason(
'Dry run mode, image upgrades are suppressed' );
89 $this->crawlMissing();
98 private function getRepo() {
99 if ( $this->repo ===
null ) {
101 ->newCustomLocalRepo( [
103 'updateCompatibleMetadata' =>
true
110 private function build() {
112 $this->buildOldImage();
119 private function init( $count, $table ) {
120 $this->processed = 0;
122 $this->count = $count;
123 $this->startTime = microtime(
true );
124 $this->table = $table;
127 private function progress( $updated ) {
128 $this->updated += $updated;
130 if ( $this->processed % 100 != 0 ) {
133 $portion = $this->processed / $this->count;
134 $updateRate = $this->updated / $this->processed;
136 $now = microtime(
true );
137 $delta = $now - $this->startTime;
138 $estimatedTotalTime = $delta / $portion;
139 $eta = $this->startTime + $estimatedTotalTime;
140 $rate = $this->processed / $delta;
142 $this->
output( sprintf(
"%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
150 $updateRate * 100.0 ) );
154 private function buildTable( $table, $queryBuilder, $callback ) {
155 $count = $this->dbw->newSelectQueryBuilder()
156 ->select(
'count(*)' )
158 ->caller( __METHOD__ )->fetchField();
159 $this->init( $count, $table );
160 $this->
output(
"Processing $table...\n" );
162 $result = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
164 foreach ( $result as $row ) {
165 $update = call_user_func( $callback, $row );
167 $this->progress( 1 );
169 $this->progress( 0 );
172 $this->
output(
"Finished $table... $this->updated of $this->processed rows updated\n" );
175 private function buildImage() {
176 $callback = [ $this,
'imageCallback' ];
177 $this->buildTable(
'image', FileSelectQueryBuilder::newForFile( $this->
getReplicaDB() ), $callback );
180 private function imageCallback( $row ) {
183 $file = $this->getRepo()->newFileFromRow( $row );
185 return $file->getUpgraded();
188 private function buildOldImage() {
189 $this->buildTable(
'oldimage', FileSelectQueryBuilder::newForOldFile( $this->
getReplicaDB() ),
190 [ $this,
'oldimageCallback' ] );
193 private function oldimageCallback( $row ) {
196 if ( $row->oi_archive_name ==
'' ) {
197 $this->
output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
201 $file = $this->getRepo()->newFileFromRow( $row );
203 return $file->getUpgraded();
206 private function crawlMissing() {
207 $this->getRepo()->enumFiles( [ $this,
'checkMissingImage' ] );
212 $row = $this->dbw->newSelectQueryBuilder()
213 ->select( [
'img_name' ] )
215 ->where( [
'img_name' => $filename ] )
216 ->caller( __METHOD__ )->fetchRow();
220 $this->addMissingImage( $filename, $fullpath );
224 private function addMissingImage( $filename, $fullpath ) {
225 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
228 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
229 if ( $altname != $filename ) {
230 if ( $this->dryrun ) {
231 $filename = $altname;
232 $this->
output(
"Estimating transcoding... $altname\n" );
236 $filename = $this->renameFile( $filename );
240 if ( $filename ==
'' ) {
241 $this->
output(
"Empty filename for $fullpath\n" );
245 if ( !$this->dryrun ) {
246 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
247 $pageText = SpecialUpload::getInitialPageText(
248 '(recovered file, missing upload log entry)'
250 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
251 $status = $file->recordUpload3(
253 '(recovered file, missing upload log entry)',
259 if ( !$status->isOK() ) {
260 $this->
output(
"Error uploading file $fullpath\n" );
265 $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.