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
20 * @ingroup Maintenance
23 require_once( dirname( __FILE__
) . '/Maintenance.php' );
25 class PopulateImageSha1
extends LoggedUpdateMaintenance
{
26 public function __construct() {
27 parent
::__construct();
28 $this->mDescription
= "Populate the img_sha1 field";
29 $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
30 "\t\tdefault uses Database class", false, true );
31 $this->addOption( 'file', 'Fix for a specific file, without File: namespace prefixed', false, true );
32 $this->setBatchSize( 200 );
35 protected function getUpdateKey() {
36 return 'populate img_sha1';
39 protected function updateSkippedMessage() {
40 return 'img_sha1 column of image table already populated.';
43 protected function updatelogFailedMessage() {
44 return 'Could not insert img_sha1 population row.';
47 public function doDBUpdates() {
48 $method = $this->getOption( 'method', 'normal' );
49 $file = $this->getOption( 'file' );
51 $t = -microtime( true );
52 $dbw = wfGetDB( DB_MASTER
);
54 $res = $dbw->selectRow(
57 array( 'img_name' => $dbw->addQuotes( $file ) ),
61 $this->error( "No such file: $file", true );
64 $this->output( "Populating img_sha1 field for specified files\n" );
66 $res = $dbw->select( 'image',
67 array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__
);
68 $this->output( "Populating img_sha1 field\n" );
71 $imageTable = $dbw->tableName( 'image' );
73 if ( $method == 'pipe' ) {
74 // @todo FIXME: Kill this and replace with a second unbuffered DB connection.
75 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
76 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
77 ' -h' . wfEscapeShellArg( $wgDBserver ) .
78 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
79 $this->output( "Using pipe method\n" );
80 $pipe = popen( $cmd, 'w' );
83 $numRows = $res->numRows();
85 foreach ( $res as $row ) {
86 if ( $i %
$this->mBatchSize
== 0 ) {
87 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
90 $file = wfLocalFile( $row->img_name
);
94 $sha1 = File
::sha1Base36( $file->getPath() );
95 if ( strval( $sha1 ) !== '' ) {
96 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
97 " WHERE img_name=" . $dbw->addQuotes( $row->img_name
);
98 if ( $method == 'pipe' ) {
99 fwrite( $pipe, "$sql;\n" );
101 $dbw->query( $sql, __METHOD__
);
106 if ( $method == 'pipe' ) {
110 $t +
= microtime( true );
111 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
114 return false; // we only updated *some* files, don't log
121 $maintClass = "PopulateImageSha1";
122 require_once( RUN_MAINTENANCE_IF_MAIN
);