a009223412e148de545dd782c479fc197913915f
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 );
34 protected function getUpdateKey() {
35 return 'populate img_sha1';
38 protected function updateSkippedMessage() {
39 return 'img_sha1 column of image table already populated.';
42 public function execute() {
43 if ( $this->getOption( 'file' ) ) {
44 $this->doDBUpdates(); // skip update log checks/saves
50 public function doDBUpdates() {
51 $method = $this->getOption( 'method', 'normal' );
52 $file = $this->getOption( 'file' );
54 $t = -microtime( true );
55 $dbw = wfGetDB( DB_MASTER
);
60 array( 'img_name' => $file ),
64 $this->error( "No such file: $file", true );
67 $this->output( "Populating img_sha1 field for specified files\n" );
69 $res = $dbw->select( 'image',
70 array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__
);
71 $this->output( "Populating img_sha1 field\n" );
74 $imageTable = $dbw->tableName( 'image' );
76 if ( $method == 'pipe' ) {
77 // Opening a pipe allows the SHA-1 operation to be done in parallel
78 // with the database write operation, because the writes are queued
79 // in the pipe buffer. This can improve performance by up to a
81 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
82 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
83 ' -h' . wfEscapeShellArg( $wgDBserver ) .
84 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
85 $this->output( "Using pipe method\n" );
86 $pipe = popen( $cmd, 'w' );
89 $numRows = $res->numRows();
91 foreach ( $res as $row ) {
92 if ( $i %
$this->mBatchSize
== 0 ) {
93 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
96 $file = wfLocalFile( $row->img_name
);
100 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
101 if ( strval( $sha1 ) !== '' ) {
102 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
103 " WHERE img_name=" . $dbw->addQuotes( $row->img_name
);
104 if ( $method == 'pipe' ) {
105 fwrite( $pipe, "$sql;\n" );
107 $dbw->query( $sql, __METHOD__
);
112 if ( $method == 'pipe' ) {
116 $t +
= microtime( true );
117 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
119 return !$file; // we only updated *some* files, don't log
123 $maintClass = "PopulateImageSha1";
124 require_once( RUN_MAINTENANCE_IF_MAIN
);