00001 <?php
00023 require_once( dirname(__FILE__).'/Maintenance.php' );
00024
00025 class PopulateSha1 extends Maintenance {
00026 public function __construct() {
00027 parent::__construct();
00028 $this->mDescription = "Populate the img_sha1 field";
00029 $this->addOption( 'method', "Use 'pipe' to pipe to mysql command line,\n" .
00030 "\t\tdefault uses Database class", false, true );
00031 $this->addOption( 'file', 'Fix for a specific file, without File: namespace prefixed', false, true );
00032 }
00033
00034 public function execute() {
00035 $method = $this->getOption( 'method', 'normal' );
00036 $file = $this->getOption( 'file' );
00037
00038 $t = -microtime( true );
00039 $dbw = wfGetDB( DB_MASTER );
00040 if( $file ) {
00041 $res = $dbw->selectRow(
00042 'image',
00043 array( 'img_name' ),
00044 array( 'img_name' => $dbw->addQuotes( $file ) ),
00045 __METHOD__
00046 );
00047 if( !$res ) {
00048 $this->error( "No such file: $file", true );
00049 return;
00050 }
00051 } else {
00052 $res = $dbw->select( 'image', array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__ );
00053 }
00054 $imageTable = $dbw->tableName( 'image' );
00055 $oldimageTable = $dbw->tableName( 'oldimage' );
00056 $batch = array();
00057
00058 if ( $method == 'pipe' ) {
00059
00060 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname;
00061 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) .
00062 ' -h' . wfEscapeShellArg( $wgDBserver ) .
00063 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname );
00064 $this->output( "Using pipe method\n" );
00065 $pipe = popen( $cmd, 'w' );
00066 }
00067
00068 $numRows = $res->numRows();
00069 $i = 0;
00070 foreach ( $res as $row ) {
00071 if ( $i % 100 == 0 ) {
00072 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
00073 wfWaitForSlaves( 5 );
00074 }
00075 $file = wfLocalFile( $row->img_name );
00076 if ( !$file ) {
00077 continue;
00078 }
00079 $sha1 = File::sha1Base36( $file->getPath() );
00080 if ( strval( $sha1 ) !== '' ) {
00081 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
00082 " WHERE img_name=" . $dbw->addQuotes( $row->img_name );
00083 if ( $method == 'pipe' ) {
00084 fwrite( $pipe, "$sql;\n" );
00085 } else {
00086 $dbw->query( $sql, __METHOD__ );
00087 }
00088 }
00089 $i++;
00090 }
00091 if ( $method == 'pipe' ) {
00092 fflush( $pipe );
00093 pclose( $pipe );
00094 }
00095 $t += microtime( true );
00096 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) );
00097 }
00098 }
00099
00100 $maintClass = "PopulateSha1";
00101 require_once( DO_MAINTENANCE );