r68756 - Make the use of MW_CONFIG_CALLBACK compatible with PHP 5.1
[lhc/web/wiklou.git] / maintenance / populateSha1.php
1 <?php
2 /**
3 * Optional upgrade script to populate the img_sha1 field
4 *
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.
9 *
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.
14 *
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
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
24
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 );
32 }
33
34 public function execute() {
35 $method = $this->getOption( 'method', 'normal' );
36 $file = $this->getOption( 'file' );
37
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__
46 );
47 if ( !$res ) {
48 $this->error( "No such file: $file", true );
49 return;
50 }
51 } else {
52 $res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__ );
53 }
54 $imageTable = $dbw->tableName( 'image' );
55 $oldimageTable = $dbw->tableName( 'oldimage' );
56 $batch = array();
57
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' );
66 }
67
68 $numRows = $res->numRows();
69 $i = 0;
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 ) );
73 wfWaitForSlaves( 5 );
74 }
75 $file = wfLocalFile( $row->img_name );
76 if ( !$file ) {
77 continue;
78 }
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" );
85 } else {
86 $dbw->query( $sql, __METHOD__ );
87 }
88 }
89 $i++;
90 }
91 if ( $method == 'pipe' ) {
92 fflush( $pipe );
93 pclose( $pipe );
94 }
95 $t += microtime( true );
96 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
97 }
98 }
99
100 $maintClass = "PopulateSha1";
101 require_once( DO_MAINTENANCE );