68 $method = $this->
getOption(
'method',
'normal' );
71 $isRegen = ( $force ||
$file !=
'' );
73 $t = -microtime(
true );
79 [
'img_name' =>
$file ],
85 $this->
output(
"Populating img_sha1 field for specified files\n" );
87 if ( $this->
hasOption(
'multiversiononly' ) ) {
89 $this->
output(
"Populating and recalculating img_sha1 field for versioned files\n" );
92 $this->
output(
"Populating and recalculating img_sha1 field\n" );
94 $conds = [
'img_sha1' =>
'' ];
95 $this->
output(
"Populating img_sha1 field\n" );
97 if ( $this->
hasOption(
'multiversiononly' ) ) {
98 $res = $dbw->select(
'oldimage',
99 [
'img_name' =>
'DISTINCT(oi_name)' ], $conds, __METHOD__ );
101 $res = $dbw->select(
'image', [
'img_name' ], $conds, __METHOD__ );
105 $imageTable = $dbw->tableName(
'image' );
106 $oldImageTable = $dbw->tableName(
'oldimage' );
108 if ( $method ==
'pipe' ) {
114 $cmd =
'mysql -u' . Shell::escape( $config->get( MainConfigNames::DBuser ) ) .
115 ' -h' . Shell::escape( $config->get( MainConfigNames::DBserver ) ) .
116 ' -p' . Shell::escape( $config->get( MainConfigNames::DBpassword ),
117 $config->get( MainConfigNames::DBname ) );
118 $this->
output(
"Using pipe method\n" );
119 $pipe = popen( $cmd,
'w' );
122 $numRows =
$res->numRows();
124 $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
125 foreach (
$res as $row ) {
128 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
129 $lbFactory->waitForReplication();
132 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
133 ->newFile( $row->img_name );
139 $sha1 =
$file->getRepo()->getFileSha1(
$file->getPath() );
140 if ( strval( $sha1 ) !==
'' ) {
141 if ( $isRegen &&
$file->getSha1() !== $sha1 ) {
146 $sql =
"UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
147 " WHERE img_name=" . $dbw->addQuotes(
$file->getName() );
148 if ( $method ==
'pipe' ) {
151 fwrite( $pipe,
"$sql;\n" );
153 $dbw->query( $sql, __METHOD__ );
158 foreach (
$file->getHistory() as $oldFile ) {
160 '@phan-var OldLocalFile $oldFile';
161 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
162 if ( strval( $sha1 ) !==
'' ) {
163 if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
166 $oldFile->upgradeRow();
168 $sql =
"UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
169 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) .
" AND" .
170 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) .
")";
171 if ( $method ==
'pipe' ) {
174 fwrite( $pipe,
"$sql;\n" );
176 $dbw->query( $sql, __METHOD__ );
183 if ( $method ==
'pipe' ) {
189 $t += microtime(
true );
190 $this->
output( sprintf(
"\nDone %d files in %.1f seconds\n", $numRows,
$t ) );