33 require_once __DIR__ .
'/Maintenance.php';
72 parent::__construct();
73 $this->
addDescription(
'Script to update image metadata records' );
75 $this->
addOption(
'missing',
'Check for files without associated database record' );
76 $this->
addOption(
'dry-run',
'Only report, don\'t update the database' );
81 $this->dryrun = $this->
hasOption(
'dry-run' );
82 if ( $this->dryrun ) {
84 ->setReason(
'Dry run mode, image upgrades are suppressed' );
88 $this->crawlMissing();
97 private function getRepo() {
98 if ( $this->repo ===
null ) {
100 ->newCustomLocalRepo( [
102 'updateCompatibleMetadata' =>
true
109 private function build() {
111 $this->buildOldImage();
118 private function init( $count, $table ) {
119 $this->processed = 0;
121 $this->count = $count;
122 $this->startTime = microtime(
true );
123 $this->table = $table;
126 private function progress( $updated ) {
127 $this->updated += $updated;
129 if ( $this->processed % 100 != 0 ) {
132 $portion = $this->processed / $this->count;
133 $updateRate = $this->updated / $this->processed;
135 $now = microtime(
true );
136 $delta = $now - $this->startTime;
137 $estimatedTotalTime = $delta / $portion;
138 $eta = $this->startTime + $estimatedTotalTime;
139 $rate = $this->processed / $delta;
141 $this->
output( sprintf(
"%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
149 $updateRate * 100.0 ) );
153 private function buildTable( $table, $queryInfo, $callback ) {
154 $count = $this->dbw->newSelectQueryBuilder()
155 ->select(
'count(*)' )
157 ->caller( __METHOD__ )->fetchField();
158 $this->init( $count, $table );
159 $this->
output(
"Processing $table...\n" );
162 $queryInfo[
'tables'], $queryInfo[
'fields'], [], __METHOD__, [], $queryInfo[
'joins']
165 foreach ( $result as $row ) {
166 $update = call_user_func( $callback, $row );
168 $this->progress( 1 );
170 $this->progress( 0 );
173 $this->
output(
"Finished $table... $this->updated of $this->processed rows updated\n" );
176 private function buildImage() {
177 $callback = [ $this,
'imageCallback' ];
181 private function imageCallback( $row ) {
184 $file = $this->getRepo()->newFileFromRow( $row );
186 return $file->getUpgraded();
189 private function buildOldImage() {
191 [ $this,
'oldimageCallback' ] );
194 private function oldimageCallback( $row ) {
197 if ( $row->oi_archive_name ==
'' ) {
198 $this->
output(
"Empty oi_archive_name for oi_name={$row->oi_name}\n" );
202 $file = $this->getRepo()->newFileFromRow( $row );
204 return $file->getUpgraded();
207 private function crawlMissing() {
208 $this->getRepo()->enumFiles( [ $this,
'checkMissingImage' ] );
213 $row = $this->dbw->newSelectQueryBuilder()
214 ->select( [
'img_name' ] )
216 ->where( [
'img_name' => $filename ] )
217 ->caller( __METHOD__ )->fetchRow();
221 $this->addMissingImage( $filename, $fullpath );
225 private function addMissingImage( $filename, $fullpath ) {
226 $timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
229 $altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
230 if ( $altname != $filename ) {
231 if ( $this->dryrun ) {
232 $filename = $altname;
233 $this->
output(
"Estimating transcoding... $altname\n" );
237 $filename = $this->renameFile( $filename );
241 if ( $filename ==
'' ) {
242 $this->
output(
"Empty filename for $fullpath\n" );
246 if ( !$this->dryrun ) {
247 $file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
248 $pageText = SpecialUpload::getInitialPageText(
249 '(recovered file, missing upload log entry)'
251 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [
'steal' =>
true ] );
252 $status =
$file->recordUpload3(
254 '(recovered file, missing upload log entry)',
260 if ( !$status->isOK() ) {
261 $this->
output(
"Error uploading file $fullpath\n" );
266 $this->
output( $fullpath .
"\n" );
271 require_once RUN_MAINTENANCE_IF_MAIN;
wfBaseName( $path, $suffix='')
Return the final portion of a pathname.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script to update image metadata records.
__construct()
Default constructor.
checkMissingImage( $fullpath)
IMaintainableDatabase $dbw
execute()
Do the actual work.
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new localfile object.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
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.
static getQueryInfo(array $options=[])
Return the tables, fields, and join conditions to be selected to create a new oldlocalfile object.
Advanced database interface for IDatabase handles that include maintenance methods.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.