* Add user_text index to filearchives (for renames at least)
[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( $options['method'] ) ? $options['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 ) .
18 ' -h' . wfEscapeShellArg( $wgDBserver ) .
19 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
20 if ( $method == 'pipe' ) {
21 echo "Using pipe method\n";
22 $pipe = popen( $cmd, 'w' );
23 }
24
25 $numRows = $res->numRows();
26 $i = 0;
27 foreach ( $res as $row ) {
28 if ( $i % 100 == 0 ) {
29 printf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 );
30 wfWaitForSlaves( 5 );
31 }
32 $file = wfLocalFile( $row->img_name );
33 if ( !$file ) {
34 continue;
35 }
36 $sha1 = File::sha1Base36( $file->getPath() );
37 if ( strval( $sha1 ) !== '' ) {
38 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
39 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
40 if ( $method == 'pipe' ) {
41 fwrite( $pipe, "$sql;\n" );
42 } else {
43 $dbw->query( $sql, $fname );
44 }
45 }
46 $i++;
47 }
48 if ( $method == 'pipe' ) {
49 fflush( $pipe );
50 pclose( $pipe );
51 }
52 $t += microtime( true );
53 printf( "\nDone %d files in %.1f seconds\n", $numRows, $t );
54
55 ?>