2007-08-25 15:15:15 +00:00
|
|
|
<?php
|
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
|
|
|
/**
|
|
|
|
|
* Optional upgrade script to populate the img_sha1 field
|
|
|
|
|
*
|
2009-08-03 23:46:22 +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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
2012-08-02 19:33:47 +00:00
|
|
|
* @file
|
2009-08-03 23:46:22 +00:00
|
|
|
* @ingroup Maintenance
|
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
|
|
|
*/
|
2007-08-25 15:15:15 +00:00
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2007-08-25 15:15:15 +00:00
|
|
|
|
2012-08-02 19:33:47 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script to populate the img_sha1 field.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2011-08-12 20:37:35 +00:00
|
|
|
class PopulateImageSha1 extends LoggedUpdateMaintenance {
|
2009-08-03 23:46:22 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->mDescription = "Populate the img_sha1 field";
|
2012-02-29 22:45:17 +00:00
|
|
|
$this->addOption( 'force', "Recalculate sha1 for rows that already have a value" );
|
2014-04-22 18:44:47 +00:00
|
|
|
$this->addOption( 'multiversiononly', "Calculate only for files with several versions" );
|
2009-08-03 23:46:22 +00:00
|
|
|
$this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
|
|
|
|
|
"\t\tdefault uses Database class", false, true );
|
2014-04-22 21:07:08 +00:00
|
|
|
$this->addOption(
|
|
|
|
|
'file',
|
|
|
|
|
'Fix for a specific file, without File: namespace prefixed',
|
|
|
|
|
false,
|
|
|
|
|
true
|
|
|
|
|
);
|
2009-08-03 23:46:22 +00:00
|
|
|
}
|
2007-08-25 15:15:15 +00:00
|
|
|
|
2011-08-12 20:37:35 +00:00
|
|
|
protected function getUpdateKey() {
|
|
|
|
|
return 'populate img_sha1';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function updateSkippedMessage() {
|
|
|
|
|
return 'img_sha1 column of image table already populated.';
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-16 20:38:24 +00:00
|
|
|
public function execute() {
|
2014-04-22 18:44:47 +00:00
|
|
|
if ( $this->getOption( 'file' ) || $this->hasOption( 'multiversiononly' ) ) {
|
2011-11-16 20:38:24 +00:00
|
|
|
$this->doDBUpdates(); // skip update log checks/saves
|
|
|
|
|
} else {
|
|
|
|
|
parent::execute();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-12 20:37:35 +00:00
|
|
|
public function doDBUpdates() {
|
2009-08-03 23:46:22 +00:00
|
|
|
$method = $this->getOption( 'method', 'normal' );
|
2012-05-23 19:04:29 +00:00
|
|
|
$file = $this->getOption( 'file', '' );
|
2012-02-29 22:45:17 +00:00
|
|
|
$force = $this->getOption( 'force' );
|
2012-05-23 19:04:29 +00:00
|
|
|
$isRegen = ( $force || $file != '' ); // forced recalculation?
|
2007-08-25 15:15:15 +00:00
|
|
|
|
2009-08-03 23:46:22 +00:00
|
|
|
$t = -microtime( true );
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2012-05-23 19:04:29 +00:00
|
|
|
if ( $file != '' ) {
|
2011-11-16 20:38:24 +00:00
|
|
|
$res = $dbw->select(
|
2010-05-22 16:50:39 +00:00
|
|
|
'image',
|
|
|
|
|
array( 'img_name' ),
|
2011-11-16 20:38:24 +00:00
|
|
|
array( 'img_name' => $file ),
|
2009-08-03 23:46:22 +00:00
|
|
|
__METHOD__
|
|
|
|
|
);
|
2010-05-22 16:50:39 +00:00
|
|
|
if ( !$res ) {
|
2009-08-03 23:46:22 +00:00
|
|
|
$this->error( "No such file: $file", true );
|
2011-08-12 20:37:35 +00:00
|
|
|
return false;
|
2009-08-03 23:46:22 +00:00
|
|
|
}
|
2011-08-12 20:37:35 +00:00
|
|
|
$this->output( "Populating img_sha1 field for specified files\n" );
|
2007-08-25 15:15:15 +00:00
|
|
|
} else {
|
2012-02-29 22:45:17 +00:00
|
|
|
if ( $force ) {
|
|
|
|
|
$conds = array();
|
|
|
|
|
$this->output( "Populating and recalculating img_sha1 field\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$conds = array( 'img_sha1' => '' );
|
|
|
|
|
$this->output( "Populating img_sha1 field\n" );
|
|
|
|
|
}
|
2014-04-22 18:44:47 +00:00
|
|
|
if ( $this->hasOption( 'multiversiononly' ) ) {
|
|
|
|
|
$res = $dbw->select( 'oldimage',
|
|
|
|
|
array( 'img_name' => 'DISTINCT(oi_name)' ), $conds, __METHOD__ );
|
|
|
|
|
} else {
|
|
|
|
|
$res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__ );
|
|
|
|
|
}
|
2007-08-25 15:15:15 +00:00
|
|
|
}
|
2011-08-12 20:37:35 +00:00
|
|
|
|
2009-08-03 23:46:22 +00:00
|
|
|
$imageTable = $dbw->tableName( 'image' );
|
2012-02-29 22:45:17 +00:00
|
|
|
$oldImageTable = $dbw->tableName( 'oldimage' );
|
2010-07-25 21:44:29 +00:00
|
|
|
|
2009-08-03 23:46:22 +00:00
|
|
|
if ( $method == 'pipe' ) {
|
2012-05-23 19:04:29 +00:00
|
|
|
// Opening a pipe allows the SHA-1 operation to be done in parallel
|
2012-02-29 22:30:23 +00:00
|
|
|
// with the database write operation, because the writes are queued
|
2012-05-23 19:04:29 +00:00
|
|
|
// in the pipe buffer. This can improve performance by up to a
|
|
|
|
|
// factor of 2.
|
2009-08-04 23:09:43 +00:00
|
|
|
global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
|
2010-05-22 16:50:39 +00:00
|
|
|
$cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
|
2009-08-04 23:09:43 +00:00
|
|
|
' -h' . wfEscapeShellArg( $wgDBserver ) .
|
|
|
|
|
' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
|
2009-08-03 23:46:22 +00:00
|
|
|
$this->output( "Using pipe method\n" );
|
|
|
|
|
$pipe = popen( $cmd, 'w' );
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-03 23:46:22 +00:00
|
|
|
$numRows = $res->numRows();
|
|
|
|
|
$i = 0;
|
|
|
|
|
foreach ( $res as $row ) {
|
2011-08-12 20:37:35 +00:00
|
|
|
if ( $i % $this->mBatchSize == 0 ) {
|
2012-02-29 22:45:17 +00:00
|
|
|
$this->output( sprintf(
|
|
|
|
|
"Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
|
2011-04-20 00:12:06 +00:00
|
|
|
wfWaitForSlaves();
|
2009-08-03 23:46:22 +00:00
|
|
|
}
|
2014-04-22 18:44:47 +00:00
|
|
|
|
2009-08-03 23:46:22 +00:00
|
|
|
$file = wfLocalFile( $row->img_name );
|
|
|
|
|
if ( !$file ) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2014-04-22 18:44:47 +00:00
|
|
|
|
2012-02-29 22:45:17 +00:00
|
|
|
// Upgrade the current file version...
|
2012-01-12 20:50:14 +00:00
|
|
|
$sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
|
2012-02-29 22:45:17 +00:00
|
|
|
if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
|
2012-05-23 19:04:29 +00:00
|
|
|
if ( $isRegen && $file->getSha1() !== $sha1 ) {
|
|
|
|
|
// The population was probably done already. If the old SHA1
|
|
|
|
|
// does not match, then both fix the SHA1 and the metadata.
|
|
|
|
|
$file->upgradeRow();
|
2009-08-03 23:46:22 +00:00
|
|
|
} else {
|
2012-05-23 19:04:29 +00:00
|
|
|
$sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
|
|
|
|
|
" WHERE img_name=" . $dbw->addQuotes( $file->getName() );
|
|
|
|
|
if ( $method == 'pipe' ) {
|
|
|
|
|
fwrite( $pipe, "$sql;\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$dbw->query( $sql, __METHOD__ );
|
|
|
|
|
}
|
2009-08-03 23:46:22 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-02-29 22:45:17 +00:00
|
|
|
// Upgrade the old file versions...
|
|
|
|
|
foreach ( $file->getHistory() as $oldFile ) {
|
|
|
|
|
$sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
|
|
|
|
|
if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
|
2012-05-23 19:04:29 +00:00
|
|
|
if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
|
|
|
|
|
// The population was probably done already. If the old SHA1
|
|
|
|
|
// does not match, then both fix the SHA1 and the metadata.
|
|
|
|
|
$oldFile->upgradeRow();
|
2012-02-29 22:45:17 +00:00
|
|
|
} else {
|
2012-05-23 19:04:29 +00:00
|
|
|
$sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
|
|
|
|
|
" WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
|
|
|
|
|
" oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
|
|
|
|
|
if ( $method == 'pipe' ) {
|
|
|
|
|
fwrite( $pipe, "$sql;\n" );
|
|
|
|
|
} else {
|
|
|
|
|
$dbw->query( $sql, __METHOD__ );
|
|
|
|
|
}
|
2012-02-29 22:45:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-08-03 23:46:22 +00:00
|
|
|
$i++;
|
|
|
|
|
}
|
|
|
|
|
if ( $method == 'pipe' ) {
|
|
|
|
|
fflush( $pipe );
|
|
|
|
|
pclose( $pipe );
|
|
|
|
|
}
|
|
|
|
|
$t += microtime( true );
|
|
|
|
|
$this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
|
2011-08-12 20:37:35 +00:00
|
|
|
|
2011-10-18 17:32:20 +00:00
|
|
|
return !$file; // we only updated *some* files, don't log
|
2007-08-25 15:15:15 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-08-03 23:46:22 +00:00
|
|
|
|
2011-08-11 22:00:42 +00:00
|
|
|
$maintClass = "PopulateImageSha1";
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|