| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
> |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
require_once __DIR__ . '/Maintenance.php'; |
| 35 |
|
| 36 |
|
| 37 |
use MediaWiki\FileRepo\File\FileSelectQueryBuilder; |
| 38 |
use MediaWiki\Specials\SpecialUpload; |
| 39 |
use MediaWiki\User\User; |
| 40 |
use Wikimedia\Rdbms\IMaintainableDatabase; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class ImageBuilder extends Maintenance { |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
protected $dbw; |
| 52 |
|
| 53 |
|
| 54 |
private $dryrun; |
| 55 |
|
| 56 |
|
| 57 |
private $repo; |
| 58 |
|
| 59 |
|
| 60 |
private $updated; |
| 61 |
|
| 62 |
|
| 63 |
private $processed; |
| 64 |
|
| 65 |
|
| 66 |
private $count; |
| 67 |
|
| 68 |
|
| 69 |
private $startTime; |
| 70 |
|
| 71 |
|
| 72 |
private $table; |
| 73 |
|
| 74 |
public function __construct() { |
| 75 |
parent::__construct(); |
| 76 |
$this->addDescription( 'Script to update image metadata records' ); |
| 77 |
|
| 78 |
$this->addOption( 'missing', 'Check for files without associated database record' ); |
| 79 |
$this->addOption( 'dry-run', 'Only report, don\'t update the database' ); |
| 80 |
} |
| 81 |
|
| 82 |
public function execute() { |
| 83 |
$this->dbw = $this->getPrimaryDB(); |
| 84 |
$this->dryrun = $this->hasOption( 'dry-run' ); |
| 85 |
if ( $this->dryrun ) { |
| 86 |
$this->getServiceContainer()->getReadOnlyMode() |
| 87 |
->setReason( 'Dry run mode, image upgrades are suppressed' ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( $this->hasOption( 'missing' ) ) { |
| 91 |
$this->crawlMissing(); |
| 92 |
} else { |
| 93 |
$this->build(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private function getRepo() { |
| 101 |
if ( $this->repo === null ) { |
| 102 |
$this->repo = $this->getServiceContainer()->getRepoGroup() |
| 103 |
->newCustomLocalRepo( [ |
| 104 |
|
| 105 |
'updateCompatibleMetadata' => true |
| 106 |
] ); |
| 107 |
} |
| 108 |
|
| 109 |
return $this->repo; |
| 110 |
} |
| 111 |
|
| 112 |
private function build() { |
| 113 |
$this->buildImage(); |
| 114 |
$this->buildOldImage(); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
private function init( $count, $table ) { |
| 122 |
$this->processed = 0; |
| 123 |
$this->updated = 0; |
| 124 |
$this->count = $count; |
| 125 |
$this->startTime = microtime( true ); |
| 126 |
$this->table = $table; |
| 127 |
} |
| 128 |
|
| 129 |
private function progress( $updated ) { |
| 130 |
$this->updated += $updated; |
| 131 |
$this->processed++; |
| 132 |
if ( $this->processed % 100 != 0 ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
$portion = $this->processed / $this->count; |
| 136 |
$updateRate = $this->updated / $this->processed; |
| 137 |
|
| 138 |
$now = microtime( true ); |
| 139 |
$delta = $now - $this->startTime; |
| 140 |
$estimatedTotalTime = $delta / $portion; |
| 141 |
$eta = $this->startTime + $estimatedTotalTime; |
| 142 |
$rate = $this->processed / $delta; |
| 143 |
|
| 144 |
$this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n", |
| 145 |
wfTimestamp( TS_DB, intval( $now ) ), |
| 146 |
$portion * 100.0, |
| 147 |
$this->table, |
| 148 |
wfTimestamp( TS_DB, intval( $eta ) ), |
| 149 |
$this->processed, |
| 150 |
$this->count, |
| 151 |
$rate, |
| 152 |
$updateRate * 100.0 ) ); |
| 153 |
flush(); |
| 154 |
} |
| 155 |
|
| 156 |
private function buildTable( $table, $queryBuilder, $callback ) { |
| 157 |
$count = $this->dbw->newSelectQueryBuilder() |
| 158 |
->select( 'count(*)' ) |
| 159 |
->from( $table ) |
| 160 |
->caller( __METHOD__ )->fetchField(); |
| 161 |
$this->init( $count, $table ); |
| 162 |
$this->output( "Processing $table...\n" ); |
| 163 |
|
| 164 |
$result = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 165 |
|
| 166 |
foreach ( $result as $row ) { |
| 167 |
$update = call_user_func( $callback, $row ); |
| 168 |
if ( $update ) { |
| 169 |
$this->progress( 1 ); |
| 170 |
} else { |
| 171 |
$this->progress( 0 ); |
| 172 |
} |
| 173 |
} |
| 174 |
$this->output( "Finished $table... $this->updated of $this->processed rows updated\n" ); |
| 175 |
} |
| 176 |
|
| 177 |
private function buildImage() { |
| 178 |
$callback = [ $this, 'imageCallback' ]; |
| 179 |
$this->buildTable( 'image', FileSelectQueryBuilder::newForFile( $this->getReplicaDB() ), $callback ); |
| 180 |
} |
| 181 |
|
| 182 |
private function imageCallback( $row ) { |
| 183 |
|
| 184 |
|
| 185 |
$file = $this->getRepo()->newFileFromRow( $row ); |
| 186 |
|
| 187 |
return $file->getUpgraded(); |
| 188 |
} |
| 189 |
|
| 190 |
private function buildOldImage() { |
| 191 |
$this->buildTable( 'oldimage', FileSelectQueryBuilder::newForOldFile( $this->getReplicaDB() ), |
| 192 |
[ $this, 'oldimageCallback' ] ); |
| 193 |
} |
| 194 |
|
| 195 |
private function oldimageCallback( $row ) { |
| 196 |
|
| 197 |
|
| 198 |
if ( $row->oi_archive_name == '' ) { |
| 199 |
$this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" ); |
| 200 |
|
| 201 |
return false; |
| 202 |
} |
| 203 |
$file = $this->getRepo()->newFileFromRow( $row ); |
| 204 |
|
| 205 |
return $file->getUpgraded(); |
| 206 |
} |
| 207 |
|
| 208 |
private function crawlMissing() { |
| 209 |
$this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] ); |
| 210 |
} |
| 211 |
|
| 212 |
public function checkMissingImage( $fullpath ) { |
| 213 |
$filename = wfBaseName( $fullpath ); |
| 214 |
$row = $this->dbw->newSelectQueryBuilder() |
| 215 |
->select( [ 'img_name' ] ) |
| 216 |
->from( 'image' ) |
| 217 |
->where( [ 'img_name' => $filename ] ) |
| 218 |
->caller( __METHOD__ )->fetchRow(); |
| 219 |
|
| 220 |
if ( !$row ) { |
| 221 |
|
| 222 |
$this->addMissingImage( $filename, $fullpath ); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
private function addMissingImage( $filename, $fullpath ) { |
| 227 |
$timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) ); |
| 228 |
$services = $this->getServiceContainer(); |
| 229 |
|
| 230 |
$altname = $services->getContentLanguage()->checkTitleEncoding( $filename ); |
| 231 |
if ( $altname != $filename ) { |
| 232 |
if ( $this->dryrun ) { |
| 233 |
$filename = $altname; |
| 234 |
$this->output( "Estimating transcoding... $altname\n" ); |
| 235 |
} else { |
| 236 |
|
| 237 |
|
| 238 |
$filename = $this->renameFile( $filename ); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
if ( $filename == '' ) { |
| 243 |
$this->output( "Empty filename for $fullpath\n" ); |
| 244 |
|
| 245 |
return; |
| 246 |
} |
| 247 |
if ( !$this->dryrun ) { |
| 248 |
$file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename ); |
| 249 |
$pageText = SpecialUpload::getInitialPageText( |
| 250 |
'(recovered file, missing upload log entry)' |
| 251 |
); |
| 252 |
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] ); |
| 253 |
$status = $file->recordUpload3( |
| 254 |
'', |
| 255 |
'(recovered file, missing upload log entry)', |
| 256 |
$pageText, |
| 257 |
$user, |
| 258 |
false, |
| 259 |
$timestamp |
| 260 |
); |
| 261 |
if ( !$status->isOK() ) { |
| 262 |
$this->output( "Error uploading file $fullpath\n" ); |
| 263 |
|
| 264 |
return; |
| 265 |
} |
| 266 |
} |
| 267 |
$this->output( $fullpath . "\n" ); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
$maintClass = ImageBuilder::class; |
| 273 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 274 |
|