3 * Optional upgrade script to populate the img_sha1 field
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
24 require_once( __DIR__
. '/Maintenance.php' );
27 * Maintenance script to populate the img_sha1 field.
29 * @ingroup Maintenance
31 class PopulateImageSha1
extends LoggedUpdateMaintenance
{
32 public function __construct() {
33 parent
::__construct();
34 $this->mDescription
= "Populate the img_sha1 field";
35 $this->addOption( 'force', "Recalculate sha1 for rows that already have a value" );
36 $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
37 "\t\tdefault uses Database class", false, true );
38 $this->addOption( 'file', 'Fix for a specific file, without File: namespace prefixed', false, true );
41 protected function getUpdateKey() {
42 return 'populate img_sha1';
45 protected function updateSkippedMessage() {
46 return 'img_sha1 column of image table already populated.';
49 public function execute() {
50 if ( $this->getOption( 'file' ) ) {
51 $this->doDBUpdates(); // skip update log checks/saves
57 public function doDBUpdates() {
58 $method = $this->getOption( 'method', 'normal' );
59 $file = $this->getOption( 'file', '' );
60 $force = $this->getOption( 'force' );
61 $isRegen = ( $force ||
$file != '' ); // forced recalculation?
63 $t = -microtime( true );
64 $dbw = wfGetDB( DB_MASTER
);
69 array( 'img_name' => $file ),
73 $this->error( "No such file: $file", true );
76 $this->output( "Populating img_sha1 field for specified files\n" );
80 $this->output( "Populating and recalculating img_sha1 field\n" );
82 $conds = array( 'img_sha1' => '' );
83 $this->output( "Populating img_sha1 field\n" );
85 $res = $dbw->select( 'image', array( 'img_name' ), $conds, __METHOD__
);
88 $imageTable = $dbw->tableName( 'image' );
89 $oldImageTable = $dbw->tableName( 'oldimage' );
91 if ( $method == 'pipe' ) {
92 // Opening a pipe allows the SHA-1 operation to be done in parallel
93 // with the database write operation, because the writes are queued
94 // in the pipe buffer. This can improve performance by up to a
96 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
97 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
98 ' -h' . wfEscapeShellArg( $wgDBserver ) .
99 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
100 $this->output( "Using pipe method\n" );
101 $pipe = popen( $cmd, 'w' );
104 $numRows = $res->numRows();
106 foreach ( $res as $row ) {
107 if ( $i %
$this->mBatchSize
== 0 ) {
108 $this->output( sprintf(
109 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
112 $file = wfLocalFile( $row->img_name
);
116 // Upgrade the current file version...
117 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
118 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
119 if ( $isRegen && $file->getSha1() !== $sha1 ) {
120 // The population was probably done already. If the old SHA1
121 // does not match, then both fix the SHA1 and the metadata.
124 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
125 " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
126 if ( $method == 'pipe' ) {
127 fwrite( $pipe, "$sql;\n" );
129 $dbw->query( $sql, __METHOD__
);
133 // Upgrade the old file versions...
134 foreach ( $file->getHistory() as $oldFile ) {
135 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
136 if ( strval( $sha1 ) !== '' ) { // file on disk and hashed properly
137 if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
138 // The population was probably done already. If the old SHA1
139 // does not match, then both fix the SHA1 and the metadata.
140 $oldFile->upgradeRow();
142 $sql = "UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
143 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) . " AND" .
144 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) . ")";
145 if ( $method == 'pipe' ) {
146 fwrite( $pipe, "$sql;\n" );
148 $dbw->query( $sql, __METHOD__
);
155 if ( $method == 'pipe' ) {
159 $t +
= microtime( true );
160 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
162 return !$file; // we only updated *some* files, don't log
166 $maintClass = "PopulateImageSha1";
167 require_once( RUN_MAINTENANCE_IF_MAIN
);