Of course didn't mean to commit var_dump
[mediawiki.git] / maintenance / populateSha1.php
blob02707ffe08588c3828d4aefeecf2bf4a0b15e30f
1 <?php
2 /**
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 );
40 if ( $file ) {
41 $res = $dbw->selectRow(
42 'image',
43 array( 'img_name' ),
44 array( 'img_name' => $dbw->addQuotes( $file ) ),
45 __METHOD__
47 if ( !$res ) {
48 $this->error( "No such file: $file", true );
49 return;
51 } else {
52 $res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__ );
54 $imageTable = $dbw->tableName( 'image' );
55 $oldimageTable = $dbw->tableName( 'oldimage' );
57 if ( $method == 'pipe' ) {
58 // @fixme kill this and replace with a second unbuffered DB connection.
59 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
60 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
61 ' -h' . wfEscapeShellArg( $wgDBserver ) .
62 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
63 $this->output( "Using pipe method\n" );
64 $pipe = popen( $cmd, 'w' );
67 $numRows = $res->numRows();
68 $i = 0;
69 foreach ( $res as $row ) {
70 if ( $i % 100 == 0 ) {
71 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
72 wfWaitForSlaves( 5 );
74 $file = wfLocalFile( $row->img_name );
75 if ( !$file ) {
76 continue;
78 $sha1 = File::sha1Base36( $file->getPath() );
79 if ( strval( $sha1 ) !== '' ) {
80 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
81 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
82 if ( $method == 'pipe' ) {
83 fwrite( $pipe, "$sql;\n" );
84 } else {
85 $dbw->query( $sql, __METHOD__ );
88 $i++;
90 if ( $method == 'pipe' ) {
91 fflush( $pipe );
92 pclose( $pipe );
94 $t += microtime( true );
95 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
99 $maintClass = "PopulateSha1";
100 require_once( DO_MAINTENANCE );