Starter for a re-jig of hit counting (in a per-day table).
[lhc/web/wiklou.git] / maintenance / archives / populateSha1.php
1 <?php
2 /**
3 * Optional upgrade script to populate the img_sha1 field
4 *
5 * @file
6 * @ingroup MaintenanceArchive
7 */
8
9 $optionsWithArgs = array( 'method' );
10 require_once( dirname(__FILE__).'/../commandLine.inc' );
11 $method = isset( $options['method'] ) ? $options['method'] : 'normal';
12
13 $t = -microtime( true );
14 $fname = 'populateSha1.php';
15 $dbw = wfGetDB( DB_MASTER );
16 $res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), $fname );
17 $imageTable = $dbw->tableName( 'image' );
18 $oldimageTable = $dbw->tableName( 'oldimage' );
19 $batch = array();
20
21 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
22 ' -h' . wfEscapeShellArg( $wgDBserver ) .
23 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
24 if ( $method == 'pipe' ) {
25 echo "Using pipe method\n";
26 $pipe = popen( $cmd, 'w' );
27 }
28
29 $numRows = $res->numRows();
30 $i = 0;
31 foreach ( $res as $row ) {
32 if ( $i % 100 == 0 ) {
33 printf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 );
34 wfWaitForSlaves( 5 );
35 }
36 $file = wfLocalFile( $row->img_name );
37 if ( !$file ) {
38 continue;
39 }
40 $sha1 = File::sha1Base36( $file->getPath() );
41 if ( strval( $sha1 ) !== '' ) {
42 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
43 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
44 if ( $method == 'pipe' ) {
45 fwrite( $pipe, "$sql;\n" );
46 } else {
47 $dbw->query( $sql, $fname );
48 }
49 }
50 $i++;
51 }
52 if ( $method == 'pipe' ) {
53 fflush( $pipe );
54 pclose( $pipe );
55 }
56 $t += microtime( true );
57 printf( "\nDone %d files in %.1f seconds\n", $numRows, $t );
58
59 ?>