wiki.techinc.nl/maintenance/rebuildImages.php

212 lines
5.7 KiB
PHP
Raw Normal View History

<?php
/*
* 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
*
* Copyright (C) 2005 Brion Vibber <brion@pobox.com>
* http://www.mediawiki.org/
2006-01-07 13:09:30 +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
* (at your option) any later version.
2006-01-07 13:09:30 +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
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @author Brion Vibber <brion at pobox.com>
2010-06-29 06:32:18 +00:00
* @ingroup maintenance
*/
$options = array( 'missing', 'dry-run' );
2010-05-22 16:50:39 +00:00
require_once( dirname( __FILE__ ) . '/commandLine.inc' );
require_once( 'FiveUpgrade.inc' );
class ImageBuilder extends FiveUpgrade {
2010-08-30 16:52:51 +00:00
function __construct( $dryrun = false ) {
parent::FiveUpgrade();
2006-01-07 13:31:29 +00:00
$this->maxLag = 10; # if slaves are lagged more than 10 secs, wait
$this->dryrun = $dryrun;
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;
}
2006-01-07 13:31:29 +00:00
function build() {
$this->buildImage();
$this->buildOldImage();
}
2006-01-07 13:31:29 +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
function progress( $updated ) {
$this->updated += $updated;
$this->processed++;
2010-05-22 16:50:39 +00:00
if ( $this->processed % 100 != 0 ) {
return;
}
$portion = $this->processed / $this->count;
$updateRate = $this->updated / $this->processed;
2006-01-07 13:31:29 +00:00
$now = wfTime();
$delta = $now - $this->startTime;
$estimatedTotalTime = $delta / $portion;
$eta = $this->startTime + $estimatedTotalTime;
2006-01-07 13:31:29 +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 ) ),
$completed, // $completed does not appear to be defined.
$this->count,
$rate, // $rate does not appear to be defined.
$updateRate * 100.0 );
flush();
}
2006-01-07 13:31:29 +00:00
function buildTable( $table, $key, $callback ) {
$fname = 'ImageBuilder::buildTable';
2006-01-07 13:31:29 +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
$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 ) ) {
$update = call_user_func( $callback, $row, null );
2010-05-22 16:50:39 +00:00
if ( $update ) {
$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
function buildImage() {
$callback = array( &$this, 'imageCallback' );
$this->buildTable( 'image', 'img_name', $callback );
}
2006-01-07 13:31:29 +00:00
function imageCallback( $row, $copy ) {
// Create a File object from the row
// This will also upgrade it
$file = $this->getRepo()->newFileFromRow( $row );
return $file->getUpgraded();
}
2006-01-07 13:31:29 +00:00
function buildOldImage() {
$this->buildTable( 'oldimage', 'oi_archive_name',
array( &$this, 'oldimageCallback' ) );
}
2006-01-07 13:31:29 +00:00
function oldimageCallback( $row, $copy ) {
// 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;
}
$file = $this->getRepo()->newFileFromRow( $row );
return $file->getUpgraded();
}
2006-01-07 13:31:29 +00:00
function crawlMissing() {
$repo = RepoGroup::singleton()->getLocalRepo();
$repo->enumFilesInFS( array( $this, 'checkMissingImage' ) );
}
2006-01-07 13:31:29 +00:00
function checkMissingImage( $fullpath ) {
$fname = 'ImageBuilder::checkMissingImage';
$filename = wfBaseName( $fullpath );
2010-05-22 16:50:39 +00:00
if ( is_dir( $fullpath ) ) {
return;
}
2010-05-22 16:50:39 +00:00
if ( is_link( $fullpath ) ) {
$this->log( "skipping symlink at $fullpath" );
return;
}
$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 ) {
// already known, move on
return;
} else {
$this->addMissingImage( $filename, $fullpath );
}
}
2006-01-07 13:31:29 +00:00
function addMissingImage( $filename, $fullpath ) {
$timestamp = $this->dbw->timestamp( filemtime( $fullpath ) );
2006-01-07 13:31:29 +00:00
global $wgContLang;
$altname = $wgContLang->checkTitleEncoding( $filename );
2010-05-22 16:50:39 +00:00
if ( $altname != $filename ) {
if ( $this->dryrun ) {
$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 == '' ) {
$this->log( "Empty filename for $fullpath" );
return;
}
if ( !$this->dryrun ) {
$file = wfLocalFile( $filename );
2010-05-22 16:50:39 +00:00
if ( !$file->recordUpload( '', '(recovered file, missing upload log entry)', '', '', '',
false, $timestamp ) )
{
$this->log( "Error uploading file $fullpath" );
return;
}
}
$this->log( $fullpath );
}
}
$builder = new ImageBuilder( isset( $options['dry-run'] ) );
2010-05-22 16:50:39 +00:00
if ( isset( $options['missing'] ) ) {
$builder->crawlMissing();
} else {
$builder->build();
}