Optional upgrade script to populate the img_sha1 field
[lhc/web/wiklou.git] / maintenance / archives / populateSha1.php
1 <?php
2
3 # Optional upgrade script to populate the img_sha1 field
4
5 $optionsWithArgs = array( 'method' );
6 require_once( dirname(__FILE__).'/../commandLine.inc' );
7 $method = isset( $args['method'] ) ? $args['method'] : 'normal';
8
9 $t = -microtime( true );
10 $fname = 'populateSha1.php';
11 $dbw = wfGetDB( DB_MASTER );
12 $res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), $fname );
13 $imageTable = $dbw->tableName( 'image' );
14 $oldimageTable = $dbw->tableName( 'oldimage' );
15 $batch = array();
16
17 $cmd = 'mysql -u ' . wfEscapeShellArg( $wgDBuser ) . ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
18 if ( $method == 'pipe' ) {
19 $pipe = popen( $cmd, 'w' );
20 fwrite( $pipe, "-- hello\n" );
21 }
22
23 foreach ( $res as $row ) {
24 $file = wfLocalFile( $row->img_name );
25 $sha1 = File::sha1Base36( $file->getPath() );
26 if ( strval( $sha1 ) !== '' ) {
27 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
28 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
29 if ( $method == 'pipe' ) {
30 fwrite( $pipe, $sql );
31 } else {
32 $dbw->query( $sql, $fname );
33 }
34 }
35 }
36 if ( $method == 'pipe' ) {
37 fflush( $pipe );
38 pclose( $pipe );
39 }
40 $t += microtime( true );
41 print "Done in $t seconds\n";
42
43 ?>