MediaWiki
REL1_19
|
00001 <?php 00023 require_once( dirname( __FILE__ ) . '/Maintenance.php' ); 00024 00025 class PopulateImageSha1 extends LoggedUpdateMaintenance { 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 protected function getUpdateKey() { 00035 return 'populate img_sha1'; 00036 } 00037 00038 protected function updateSkippedMessage() { 00039 return 'img_sha1 column of image table already populated.'; 00040 } 00041 00042 public function execute() { 00043 if ( $this->getOption( 'file' ) ) { 00044 $this->doDBUpdates(); // skip update log checks/saves 00045 } else { 00046 parent::execute(); 00047 } 00048 } 00049 00050 public function doDBUpdates() { 00051 $method = $this->getOption( 'method', 'normal' ); 00052 $file = $this->getOption( 'file' ); 00053 00054 $t = -microtime( true ); 00055 $dbw = wfGetDB( DB_MASTER ); 00056 if ( $file ) { 00057 $res = $dbw->select( 00058 'image', 00059 array( 'img_name' ), 00060 array( 'img_name' => $file ), 00061 __METHOD__ 00062 ); 00063 if ( !$res ) { 00064 $this->error( "No such file: $file", true ); 00065 return false; 00066 } 00067 $this->output( "Populating img_sha1 field for specified files\n" ); 00068 } else { 00069 $res = $dbw->select( 'image', 00070 array( 'img_name' ), array( 'img_sha1' => '' ), __METHOD__ ); 00071 $this->output( "Populating img_sha1 field\n" ); 00072 } 00073 00074 $imageTable = $dbw->tableName( 'image' ); 00075 00076 if ( $method == 'pipe' ) { 00077 // @todo FIXME: Kill this and replace with a second unbuffered DB connection. 00078 global $wgDBuser, $wgDBserver, $wgDBpassword, $wgDBname; 00079 $cmd = 'mysql -u' . wfEscapeShellArg( $wgDBuser ) . 00080 ' -h' . wfEscapeShellArg( $wgDBserver ) . 00081 ' -p' . wfEscapeShellArg( $wgDBpassword, $wgDBname ); 00082 $this->output( "Using pipe method\n" ); 00083 $pipe = popen( $cmd, 'w' ); 00084 } 00085 00086 $numRows = $res->numRows(); 00087 $i = 0; 00088 foreach ( $res as $row ) { 00089 if ( $i % $this->mBatchSize == 0 ) { 00090 $this->output( sprintf( "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) ); 00091 wfWaitForSlaves(); 00092 } 00093 $file = wfLocalFile( $row->img_name ); 00094 if ( !$file ) { 00095 continue; 00096 } 00097 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() ); 00098 if ( strval( $sha1 ) !== '' ) { 00099 $sql = "UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) . 00100 " WHERE img_name=" . $dbw->addQuotes( $row->img_name ); 00101 if ( $method == 'pipe' ) { 00102 fwrite( $pipe, "$sql;\n" ); 00103 } else { 00104 $dbw->query( $sql, __METHOD__ ); 00105 } 00106 } 00107 $i++; 00108 } 00109 if ( $method == 'pipe' ) { 00110 fflush( $pipe ); 00111 pclose( $pipe ); 00112 } 00113 $t += microtime( true ); 00114 $this->output( sprintf( "\nDone %d files in %.1f seconds\n", $numRows, $t ) ); 00115 00116 return !$file; // we only updated *some* files, don't log 00117 } 00118 } 00119 00120 $maintClass = "PopulateImageSha1"; 00121 require_once( RUN_MAINTENANCE_IF_MAIN );