67 $method = $this->
getOption(
'method',
'normal' );
70 $isRegen = ( $force || $file !=
'' );
72 $t = -microtime(
true );
75 $res = $dbw->newSelectQueryBuilder()
76 ->select( [
'img_name' ] )
78 ->where( [
'img_name' => $file ] )
79 ->caller( __METHOD__ )->fetchResultSet();
80 if ( !$res->numRows() ) {
83 $this->
output(
"Populating img_sha1 field for specified files\n" );
85 if ( $this->
hasOption(
'multiversiononly' ) ) {
87 $this->
output(
"Populating and recalculating img_sha1 field for versioned files\n" );
90 $this->
output(
"Populating and recalculating img_sha1 field\n" );
92 $conds = [
'img_sha1' =>
'' ];
93 $this->
output(
"Populating img_sha1 field\n" );
95 if ( $this->
hasOption(
'multiversiononly' ) ) {
96 $res = $dbw->newSelectQueryBuilder()
97 ->select( [
'img_name' =>
'DISTINCT(oi_name)' ] )
100 ->caller( __METHOD__ )->fetchResultSet();
102 $res = $dbw->newSelectQueryBuilder()
103 ->select( [
'img_name' ] )
106 ->caller( __METHOD__ )->fetchResultSet();
110 $imageTable = $dbw->tableName(
'image' );
111 $oldImageTable = $dbw->tableName(
'oldimage' );
113 if ( $method ==
'pipe' ) {
119 $cmd =
'mysql -u' . Shell::escape( $config->get( MainConfigNames::DBuser ) ) .
120 ' -h' . Shell::escape( $config->get( MainConfigNames::DBserver ) ) .
121 ' -p' . Shell::escape( $config->get( MainConfigNames::DBpassword ),
122 $config->get( MainConfigNames::DBname ) );
123 $this->
output(
"Using pipe method\n" );
124 $pipe = popen( $cmd,
'w' );
127 $numRows = $res->numRows();
129 foreach ( $res as $row ) {
132 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
137 ->newFile( $row->img_name );
143 $sha1 = $file->getRepo()->getFileSha1( $file->getPath() );
144 if ( strval( $sha1 ) !==
'' ) {
145 if ( $isRegen && $file->getSha1() !== $sha1 ) {
150 $sql =
"UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
151 " WHERE img_name=" . $dbw->addQuotes( $file->getName() );
152 if ( $method ==
'pipe' ) {
155 fwrite( $pipe,
"$sql;\n" );
157 $dbw->query( $sql, __METHOD__ );
162 foreach ( $file->getHistory() as $oldFile ) {
164 '@phan-var OldLocalFile $oldFile';
165 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
166 if ( strval( $sha1 ) !==
'' ) {
167 if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
170 $oldFile->upgradeRow();
172 $sql =
"UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
173 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) .
" AND" .
174 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) .
")";
175 if ( $method ==
'pipe' ) {
178 fwrite( $pipe,
"$sql;\n" );
180 $dbw->query( $sql, __METHOD__ );
187 if ( $method ==
'pipe' ) {
193 $t += microtime(
true );
194 $this->
output( sprintf(
"\nDone %d files in %.1f seconds\n", $numRows, $t ) );