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 PopulateSha1
extends Maintenance
{
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 );
34 public function execute() {
35 $method = $this->getOption( 'method', 'normal' );
36 $file = $this->getOption( 'file' );
38 $t = -microtime( true );
39 $dbw = wfGetDB( DB_MASTER
);
41 $res = $dbw->selectRow(
44 array( 'img_name' => $dbw->addQuotes( $file ) ),
48 $this->error( "No such file: $file", true );
52 $res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__
);
54 $imageTable = $dbw->tableName( 'image' );
55 $oldimageTable = $dbw->tableName( 'oldimage' );
58 if ( $method == 'pipe' ) {
59 // @fixme kill this and replace with a second unbuffered DB connection.
60 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
61 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
62 ' -h' . wfEscapeShellArg( $wgDBserver ) .
63 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
64 $this->output( "Using pipe method\n" );
65 $pipe = popen( $cmd, 'w' );
68 $numRows = $res->numRows();
70 foreach ( $res as $row ) {
71 if ( $i %
100 == 0 ) {
72 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
75 $file = wfLocalFile( $row->img_name
);
79 $sha1 = File
::sha1Base36( $file->getPath() );
80 if ( strval( $sha1 ) !== '' ) {
81 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
82 " WHERE img_name=" . $dbw->addQuotes( $row->img_name
);
83 if ( $method == 'pipe' ) {
84 fwrite( $pipe, "$sql;\n" );
86 $dbw->query( $sql, __METHOD__
);
91 if ( $method == 'pipe' ) {
95 $t +
= microtime( true );
96 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
100 $maintClass = "PopulateSha1";
101 require_once( DO_MAINTENANCE
);