2005-06-28 05:30:21 +00:00
|
|
|
<?php
|
2005-06-28 21:50:26 +00:00
|
|
|
/*
|
|
|
|
|
* Script to update image metadata records
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
*
|
2005-06-28 21:50:26 +00:00
|
|
|
* Copyright (C) 2005 Brion Vibber <brion@pobox.com>
|
|
|
|
|
* http://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>
|
2010-06-29 06:32:18 +00:00
|
|
|
* @ingroup maintenance
|
2005-06-28 21:50:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
$options = array( 'missing', 'dry-run' );
|
2005-06-28 05:30:21 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
require_once( dirname( __FILE__ ) . '/commandLine.inc' );
|
2005-06-28 21:50:26 +00:00
|
|
|
require_once( 'FiveUpgrade.inc' );
|
2005-06-28 05:30:21 +00:00
|
|
|
|
2005-06-28 21:50:26 +00:00
|
|
|
class ImageBuilder extends FiveUpgrade {
|
2010-08-30 16:52:51 +00:00
|
|
|
function __construct( $dryrun = false ) {
|
2005-06-28 21:50:26 +00:00
|
|
|
parent::FiveUpgrade();
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 21:50:26 +00:00
|
|
|
$this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
|
|
|
|
|
$this->dryrun = $dryrun;
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( $dryrun ) {
|
|
|
|
|
$GLOBALS['wgReadOnly'] = 'Dry run mode, image upgrades are suppressed';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getRepo() {
|
|
|
|
|
if ( !isset( $this->repo ) ) {
|
|
|
|
|
$this->repo = RepoGroup::singleton()->getLocalRepo();
|
|
|
|
|
}
|
|
|
|
|
return $this->repo;
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
function build() {
|
|
|
|
|
$this->buildImage();
|
|
|
|
|
$this->buildOldImage();
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
function init( $count, $table ) {
|
|
|
|
|
$this->processed = 0;
|
|
|
|
|
$this->updated = 0;
|
|
|
|
|
$this->count = $count;
|
|
|
|
|
$this->startTime = wfTime();
|
|
|
|
|
$this->table = $table;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
function progress( $updated ) {
|
|
|
|
|
$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
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
$now = wfTime();
|
|
|
|
|
$delta = $now - $this->startTime;
|
|
|
|
|
$estimatedTotalTime = $delta / $portion;
|
|
|
|
|
$eta = $this->startTime + $estimatedTotalTime;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
printf( "%s: %6.2f%% done on %s; ETA %s [%d/%d] %.2f/sec <%.2f%% updated>\n",
|
|
|
|
|
wfTimestamp( TS_DB, intval( $now ) ),
|
|
|
|
|
$portion * 100.0,
|
|
|
|
|
$this->table,
|
|
|
|
|
wfTimestamp( TS_DB, intval( $eta ) ),
|
2007-01-12 05:50:03 +00:00
|
|
|
$completed, // $completed does not appear to be defined.
|
2005-06-28 05:30:21 +00:00
|
|
|
$this->count,
|
2007-01-12 05:50:03 +00:00
|
|
|
$rate, // $rate does not appear to be defined.
|
2005-06-28 05:30:21 +00:00
|
|
|
$updateRate * 100.0 );
|
|
|
|
|
flush();
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
function buildTable( $table, $key, $callback ) {
|
|
|
|
|
$fname = 'ImageBuilder::buildTable';
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
$count = $this->dbw->selectField( $table, 'count(*)', '', $fname );
|
|
|
|
|
$this->init( $count, $table );
|
|
|
|
|
$this->log( "Processing $table..." );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
$tableName = $this->dbr->tableName( $table );
|
|
|
|
|
$sql = "SELECT * FROM $tableName";
|
|
|
|
|
$result = $this->dbr->query( $sql, $fname );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $row = $this->dbr->fetchObject( $result ) ) {
|
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 );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->log( "Finished $table... $this->updated of $this->processed rows updated" );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
function buildImage() {
|
|
|
|
|
$callback = array( &$this, 'imageCallback' );
|
|
|
|
|
$this->buildTable( 'image', 'img_name', $callback );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-02-10 20:26:07 +00:00
|
|
|
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 );
|
|
|
|
|
return $file->getUpgraded();
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 05:30:21 +00:00
|
|
|
function buildOldImage() {
|
|
|
|
|
$this->buildTable( 'oldimage', 'oi_archive_name',
|
|
|
|
|
array( &$this, 'oldimageCallback' ) );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-02-10 20:26:07 +00:00
|
|
|
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 == '' ) {
|
|
|
|
|
$this->log( "Empty oi_archive_name for oi_name={$row->oi_name}" );
|
|
|
|
|
return false;
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
$file = $this->getRepo()->newFileFromRow( $row );
|
|
|
|
|
return $file->getUpgraded();
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 21:50:26 +00:00
|
|
|
function crawlMissing() {
|
2007-05-30 21:02:32 +00:00
|
|
|
$repo = RepoGroup::singleton()->getLocalRepo();
|
|
|
|
|
$repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
|
2005-06-28 21:50:26 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2007-05-30 21:02:32 +00:00
|
|
|
function checkMissingImage( $fullpath ) {
|
|
|
|
|
$fname = 'ImageBuilder::checkMissingImage';
|
|
|
|
|
$filename = wfBaseName( $fullpath );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( is_dir( $fullpath ) ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
return;
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( is_link( $fullpath ) ) {
|
2007-05-30 21:02:32 +00:00
|
|
|
$this->log( "skipping symlink at $fullpath" );
|
|
|
|
|
return;
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2005-06-28 21:50:26 +00:00
|
|
|
$row = $this->dbw->selectRow( 'image',
|
|
|
|
|
array( 'img_name' ),
|
|
|
|
|
array( 'img_name' => $filename ),
|
|
|
|
|
$fname );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $row ) {
|
2005-06-28 21:50:26 +00:00
|
|
|
// already known, move on
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
$this->addMissingImage( $filename, $fullpath );
|
|
|
|
|
}
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 21:50:26 +00:00
|
|
|
function addMissingImage( $filename, $fullpath ) {
|
|
|
|
|
$timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-06-28 21:50:26 +00:00
|
|
|
global $wgContLang;
|
|
|
|
|
$altname = $wgContLang->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;
|
|
|
|
|
$this->log( "Estimating transcoding... $altname" );
|
|
|
|
|
} else {
|
|
|
|
|
$filename = $this->renameFile( $filename );
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( $filename == '' ) {
|
2005-07-11 00:47:00 +00:00
|
|
|
$this->log( "Empty filename for $fullpath" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-05-30 21:02:32 +00:00
|
|
|
if ( !$this->dryrun ) {
|
|
|
|
|
$file = wfLocalFile( $filename );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
|
2007-05-30 21:02:32 +00:00
|
|
|
false, $timestamp ) )
|
|
|
|
|
{
|
|
|
|
|
$this->log( "Error uploading file $fullpath" );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2005-06-28 21:50:26 +00:00
|
|
|
}
|
|
|
|
|
$this->log( $fullpath );
|
|
|
|
|
}
|
2005-06-28 05:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
2005-06-28 21:50:26 +00:00
|
|
|
$builder = new ImageBuilder( isset( $options['dry-run'] ) );
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( isset( $options['missing'] ) ) {
|
2005-06-28 21:50:26 +00:00
|
|
|
$builder->crawlMissing();
|
|
|
|
|
} else {
|
|
|
|
|
$builder->build();
|
|
|
|
|
}
|
2005-06-28 05:30:21 +00:00
|
|
|
|
2007-06-29 01:19:14 +00:00
|
|
|
|