summaryrefslogtreecommitdiff
path: root/maintenance/archives/populateSha1.php
blob: 487d3bad5c806863a77e76da40e571d4e3fda55f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
 * Optional upgrade script to populate the img_sha1 field
 *
 * @file
 * @ingroup MaintenanceArchive
 */

$optionsWithArgs = array( 'method' );
require_once( dirname(__FILE__).'/../commandLine.inc' );
$method = isset( $options['method'] ) ? $options['method'] : 'normal';

$t = -microtime( true );
$fname = 'populateSha1.php';
$dbw = wfGetDB( DB_MASTER );
$res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), $fname );
$imageTable = $dbw->tableName( 'image' );
$oldimageTable = $dbw->tableName( 'oldimage' );
$batch = array();

$cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) . 
	' -h' . wfEscapeShellArg( $wgDBserver ) .
	' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
if ( $method == 'pipe' ) {
	echo "Using pipe method\n";
	$pipe = popen( $cmd, 'w' );
}

$numRows = $res->numRows();
$i = 0;
foreach ( $res as $row ) {
	if ( $i % 100 == 0 ) {
		printf( "Done %d of %d, %5.3f%%  \r", $i, $numRows, $i / $numRows * 100 );
		wfWaitForSlaves( 5 );
	}
	$file = wfLocalFile( $row->img_name );
	if ( !$file ) {
		continue;
	}
	$sha1 = File::sha1Base36( $file->getPath() );
	if ( strval( $sha1 ) !== '' ) {
		$sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
			" WHERE img_name=" . $dbw->addQuotes( $row->img_name );
		if ( $method == 'pipe' ) {
			fwrite( $pipe, "$sql;\n" );
		} else {
			$dbw->query( $sql, $fname );
		}
	}
	$i++;
}
if ( $method == 'pipe' ) {
	fflush( $pipe );
	pclose( $pipe );
}
$t += microtime( true );
printf( "\nDone %d files in %.1f seconds\n", $numRows, $t );

?>