2005-06-28 05:30:21 +00:00
|
|
|
<?php
|
2010-09-10 16:47:42 +00:00
|
|
|
/**
|
2012-08-09 16:06:18 +00:00
|
|
|
* Update image metadata records.
|
2005-06-28 21:50:26 +00:00
|
|
|
*
|
|
|
|
|
* Usage: php rebuildImages.php [--missing] [--dry-run]
|
|
|
|
|
* Options:
|
|
|
|
|
* --missing Crawl the uploads dir for images without records, and
|
|
|
|
|
* add them only.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2010-09-10 16:47:42 +00:00
|
|
|
* Copyright © 2005 Brion Vibber <brion@pobox.com>
|
2014-03-20 15:45:01 +00:00
|
|
|
* https://www.mediawiki.org/
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-06-28 21:50:26 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2006-01-07 13:09:30 +00:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
2005-06-28 21:50:26 +00:00
|
|
|
* (at your option) any later version.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-06-28 21:50:26 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2006-01-07 13:09:30 +00:00
|
|
|
*
|
2005-06-28 21:50:26 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
2006-04-05 07:43:17 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-06-28 21:50:26 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
2005-06-28 21:50:26 +00:00
|
|
|
* @author Brion Vibber <brion at pobox.com>
|
2012-02-08 16:55:54 +00:00
|
|
|
* @ingroup Maintenance
|
2005-06-28 21:50:26 +00:00
|
|
|
*/
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2005-06-28 05:30:21 +00:00
|
|
|
|
2018-07-29 12:24:54 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2017-03-30 20:46:06 +00:00
|
|
|
use Wikimedia\Rdbms\IMaintainableDatabase;
|
|
|
|
|
|
2012-08-09 16:06:18 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script to update image metadata records.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2010-09-10 16:47:42 +00:00
|
|
|
class ImageBuilder extends Maintenance {
|
2011-04-12 00:16:04 +00:00
|
|
|
/**
|
2017-03-30 20:46:06 +00:00
|
|
|
* @var IMaintainableDatabase
|
2011-04-12 00:16:04 +00:00
|
|
|
*/
|
|
|
|
|
protected $dbw;
|
|
|
|
|
|
2019-09-09 09:11:50 +00:00
|
|
|
/** @var bool */
|
|
|
|
|
private $dryrun;
|
|
|
|
|
|
|
|
|
|
/** @var LocalRepo|null */
|
|
|
|
|
private $repo;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $updated;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $processed;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $count;
|
|
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
|
private $startTime;
|
|
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
|
private $table;
|
|
|
|
|
|
2019-10-09 18:41:33 +00:00
|
|
|
public function __construct() {
|
2010-09-10 16:47:42 +00:00
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Script to update image metadata records' );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->addOption( 'missing', 'Check for files without associated database record' );
|
|
|
|
|
$this->addOption( 'dry-run', 'Only report, don\'t update the database' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2021-04-29 02:37:11 +00:00
|
|
|
$this->dbw = $this->getDB( DB_PRIMARY );
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->dryrun = $this->hasOption( 'dry-run' );
|
|
|
|
|
if ( $this->dryrun ) {
|
2017-04-10 05:34:30 +00:00
|
|
|
MediaWiki\MediaWikiServices::getInstance()->getReadOnlyMode()
|
|
|
|
|
->setReason( 'Dry run mode, image upgrades are suppressed' );
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
2010-09-10 16:47:42 +00:00
|
|
|
|
|
|
|
|
if ( $this->hasOption( 'missing' ) ) {
|
|
|
|
|
$this->crawlMissing();
|
|
|
|
|
} else {
|
|
|
|
|
$this->build();
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-01 02:25:19 +00:00
|
|
|
/**
|
2019-08-31 16:14:38 +00:00
|
|
|
* @return LocalRepo
|
2011-07-01 02:25:19 +00:00
|
|
|
*/
|
2019-10-11 19:07:32 +00:00
|
|
|
private function getRepo() {
|
2019-09-09 09:11:50 +00:00
|
|
|
if ( $this->repo === null ) {
|
2021-06-04 06:46:47 +00:00
|
|
|
$this->repo = MediaWikiServices::getInstance()->getRepoGroup()
|
|
|
|
|
->newCustomLocalRepo( [
|
|
|
|
|
// make sure to update old, but compatible img_metadata fields.
|
|
|
|
|
'updateCompatibleMetadata' => true
|
|
|
|
|
] );
|
2007-05-30 21:02:32 +00:00
|
|
|
}
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
return $this->repo;
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function build() {
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->buildImage();
|
|
|
|
|
$this->buildOldImage();
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-09-08 16:59:52 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $count
|
|
|
|
|
* @param string $table
|
|
|
|
|
*/
|
2019-10-11 19:07:32 +00:00
|
|
|
private function init( $count, $table ) {
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->processed = 0;
|
|
|
|
|
$this->updated = 0;
|
|
|
|
|
$this->count = $count;
|
2012-09-04 19:05:40 +00:00
|
|
|
$this->startTime = microtime( true );
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->table = $table;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function progress( $updated ) {
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->updated += $updated;
|
|
|
|
|
$this->processed++;
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $this->processed % 100 != 0 ) {
|
2005-06-28 05:30:21 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$portion = $this->processed / $this->count;
|
|
|
|
|
$updateRate = $this->updated / $this->processed;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2012-09-04 19:05:40 +00:00
|
|
|
$now = microtime( true );
|
2005-06-28 05:30:21 +00:00
|
|
|
$delta = $now - $this->startTime;
|
|
|
|
|
$estimatedTotalTime = $delta / $portion;
|
|
|
|
|
$eta = $this->startTime + $estimatedTotalTime;
|
2010-09-10 16:47:42 +00:00
|
|
|
$rate = $this->processed / $delta;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( sprintf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
|
2005-06-28 05:30:21 +00:00
|
|
|
wfTimestamp( TS_DB, intval( $now ) ),
|
|
|
|
|
$portion * 100.0,
|
|
|
|
|
$this->table,
|
|
|
|
|
wfTimestamp( TS_DB, intval( $eta ) ),
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->processed,
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->count,
|
2010-09-10 16:47:42 +00:00
|
|
|
$rate,
|
|
|
|
|
$updateRate * 100.0 ) );
|
2005-06-28 05:30:21 +00:00
|
|
|
flush();
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function buildTable( $table, $key, $queryInfo, $callback ) {
|
2010-09-10 16:47:42 +00:00
|
|
|
$count = $this->dbw->selectField( $table, 'count(*)', '', __METHOD__ );
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->init( $count, $table );
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( "Processing $table...\n" );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2017-10-06 17:03:55 +00:00
|
|
|
$result = $this->getDB( DB_REPLICA )->select(
|
|
|
|
|
$queryInfo['tables'], $queryInfo['fields'], [], __METHOD__, [], $queryInfo['joins']
|
|
|
|
|
);
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-09-10 16:47:42 +00:00
|
|
|
foreach ( $result as $row ) {
|
2010-02-10 20:26:07 +00:00
|
|
|
$update = call_user_func( $callback, $row, null );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $update ) {
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->progress( 1 );
|
|
|
|
|
} else {
|
|
|
|
|
$this->progress( 0 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( "Finished $table... $this->updated of $this->processed rows updated\n" );
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function buildImage() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$callback = [ $this, 'imageCallback' ];
|
2017-10-06 17:03:55 +00:00
|
|
|
$this->buildTable( 'image', 'img_name', LocalFile::getQueryInfo(), $callback );
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function imageCallback( $row, $copy ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
// Create a File object from the row
|
|
|
|
|
// This will also upgrade it
|
|
|
|
|
$file = $this->getRepo()->newFileFromRow( $row );
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
return $file->getUpgraded();
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function buildOldImage() {
|
2017-10-06 17:03:55 +00:00
|
|
|
$this->buildTable( 'oldimage', 'oi_archive_name', OldLocalFile::getQueryInfo(),
|
2017-10-12 18:34:15 +00:00
|
|
|
[ $this, 'oldimageCallback' ] );
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function oldimageCallback( $row, $copy ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
// Create a File object from the row
|
|
|
|
|
// This will also upgrade it
|
|
|
|
|
if ( $row->oi_archive_name == '' ) {
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( "Empty oi_archive_name for oi_name={$row->oi_name}\n" );
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
return false;
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
$file = $this->getRepo()->newFileFromRow( $row );
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
return $file->getUpgraded();
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function crawlMissing() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->getRepo()->enumFiles( [ $this, 'checkMissingImage' ] );
|
2005-06-28 21:50:26 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2020-11-03 15:48:57 +00:00
|
|
|
public function checkMissingImage( $fullpath ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
$filename = wfBaseName( $fullpath );
|
2005-06-28 21:50:26 +00:00
|
|
|
$row = $this->dbw->selectRow( 'image',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'img_name' ],
|
|
|
|
|
[ 'img_name' => $filename ],
|
2010-09-10 16:47:42 +00:00
|
|
|
__METHOD__ );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2020-05-19 22:10:27 +00:00
|
|
|
if ( !$row ) {
|
|
|
|
|
// file not registered
|
2005-06-28 21:50:26 +00:00
|
|
|
$this->addMissingImage( $filename, $fullpath );
|
|
|
|
|
}
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2019-10-11 19:07:32 +00:00
|
|
|
private function addMissingImage( $filename, $fullpath ) {
|
2012-01-04 08:07:52 +00:00
|
|
|
$timestamp = $this->dbw->timestamp( $this->getRepo()->getFileTimestamp( $fullpath ) );
|
2019-05-14 17:00:34 +00:00
|
|
|
$services = MediaWikiServices::getInstance();
|
2012-01-04 08:07:52 +00:00
|
|
|
|
2019-05-14 17:00:34 +00:00
|
|
|
$altname = $services->getContentLanguage()->checkTitleEncoding( $filename );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $altname != $filename ) {
|
|
|
|
|
if ( $this->dryrun ) {
|
2005-06-28 21:50:26 +00:00
|
|
|
$filename = $altname;
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( "Estimating transcoding... $altname\n" );
|
2005-06-28 21:50:26 +00:00
|
|
|
} else {
|
2019-08-31 16:14:38 +00:00
|
|
|
// @fixme create renameFile()
|
|
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod See comment above...
|
2005-06-28 21:50:26 +00:00
|
|
|
$filename = $this->renameFile( $filename );
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $filename == '' ) {
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( "Empty filename for $fullpath\n" );
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2005-07-11 00:47:00 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( !$this->dryrun ) {
|
2019-05-14 17:00:34 +00:00
|
|
|
$file = $services->getRepoGroup()->getLocalRepo()->newFile( $filename );
|
2020-01-26 10:12:59 +00:00
|
|
|
$pageText = SpecialUpload::getInitialPageText(
|
|
|
|
|
'(recovered file, missing upload log entry)'
|
|
|
|
|
);
|
2021-05-03 21:34:26 +00:00
|
|
|
$user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
|
2020-06-16 01:45:46 +00:00
|
|
|
$status = $file->recordUpload3(
|
2014-04-23 18:09:26 +00:00
|
|
|
'',
|
|
|
|
|
'(recovered file, missing upload log entry)',
|
2020-01-26 10:12:59 +00:00
|
|
|
$pageText,
|
2020-06-16 01:45:46 +00:00
|
|
|
$user,
|
2014-04-23 18:09:26 +00:00
|
|
|
false,
|
2020-06-16 01:45:46 +00:00
|
|
|
$timestamp
|
2020-01-26 10:12:59 +00:00
|
|
|
);
|
|
|
|
|
if ( !$status->isOK() ) {
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( "Error uploading file $fullpath\n" );
|
2014-04-23 18:09:26 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2005-06-28 21:50:26 +00:00
|
|
|
}
|
2010-09-10 16:47:42 +00:00
|
|
|
$this->output( $fullpath . "\n" );
|
2005-06-28 21:50:26 +00:00
|
|
|
}
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = ImageBuilder::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|