71 parent::__construct();
72 $this->
addDescription(
'Script to update image metadata records' );
74 $this->
addOption(
'missing',
'Check for files without associated database record' );
75 $this->
addOption(
'dry-run',
'Only report, don\'t update the database' );
80 $this->dryrun = $this->
hasOption(
'dry-run' );
81 if ( $this->dryrun ) {
82 MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
83 ->setReason(
'Dry run mode, image upgrades are suppressed' );
87 $this->crawlMissing();
96 private function getRepo() {
97 if ( $this->repo ===
null ) {
98 $this->repo = MediaWikiServices::getInstance()->getRepoGroup()
99 ->newCustomLocalRepo( [
101 'updateCompatibleMetadata' =>
true
108 private function build() {
110 $this->buildOldImage();
117 private function init( $count, $table ) {
118 $this->processed = 0;
120 $this->count = $count;
121 $this->startTime = microtime(
true );
122 $this->table = $table;
125 private function progress( $updated ) {
126 $this->updated += $updated;
128 if ( $this->processed % 100 != 0 ) {
131 $portion = $this->processed / $this->count;
132 $updateRate = $this->updated / $this->processed;
134 $now = microtime(
true );
135 $delta = $now - $this->startTime;
136 $estimatedTotalTime = $delta / $portion;
137 $eta = $this->startTime + $estimatedTotalTime;
138 $rate = $this->processed / $delta;
140 $this->
output( sprintf(
"%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
148 $updateRate * 100.0 ) );
152 private function buildTable( $table, $key, $queryInfo, $callback ) {
153 $count = $this->dbw->selectField( $table,
'count(*)',
'', __METHOD__ );
154 $this->init( $count, $table );
155 $this->
output(
"Processing $table...\n" );
158 $queryInfo[
'tables'], $queryInfo[
'fields'], [], __METHOD__, [], $queryInfo[
'joins']
161 foreach ( $result as $row ) {
162 $update = call_user_func( $callback, $row,
null );
164 $this->progress( 1 );
166 $this->progress( 0 );
169 $this->
output(
"Finished $table... $this->updated of $this->processed rows updated\n" );
172 private function buildImage() {
173 $callback = [ $this,
'imageCallback' ];
174 $this->buildTable(
'image',
'img_name', LocalFile::getQueryInfo(), $callback );
177 private function imageCallback( $row, $copy ) {
180 $file = $this->getRepo()->newFileFromRow( $row );
182 return $file->getUpgraded();
185 private function buildOldImage() {
186 $this->buildTable(
'oldimage',
'oi_archive_name', OldLocalFile::getQueryInfo(),
187 [ $this,
'oldimageCallback' ] );
190 private function oldimageCallback( $row, $copy ) {
193 if ( $row->oi_archive_name ==
'' ) {
194 $this->
output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
198 $file = $this->getRepo()->newFileFromRow( $row );
200 return $file->getUpgraded();
203 private function crawlMissing() {
204 $this->getRepo()->enumFiles( [ $this,
'checkMissingImage' ] );
209 $row = $this->dbw->selectRow(
'image',
211 [
'img_name' => $filename ],
216 $this->addMissingImage( $filename, $fullpath );
220 private function addMissingImage( $filename, $fullpath ) {
221 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
222 $services = MediaWikiServices::getInstance();
224 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
225 if ( $altname != $filename ) {
226 if ( $this->dryrun ) {
227 $filename = $altname;
228 $this->
output(
"Estimating transcoding... $altname\n" );
232 $filename = $this->renameFile( $filename );
236 if ( $filename ==
'' ) {
237 $this->
output(
"Empty filename for $fullpath\n" );
241 if ( !$this->dryrun ) {
242 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
244 '(recovered file, missing upload log entry)'
247 $status =
$file->recordUpload3(
249 '(recovered file, missing upload log entry)',
255 if ( !$status->isOK() ) {
256 $this->
output(
"Error uploading file $fullpath\n" );
261 $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.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
static getInitialPageText( $comment='', $license='', $copyStatus='', $source='', Config $config=null)
Get the initial image page text based on a comment and optional file status information.