28 require_once __DIR__ .
'/Maintenance.php';
37 parent::__construct();
39 $this->
addOption(
'force',
"Recalculate sha1 for rows that already have a value" );
40 $this->
addOption(
'multiversiononly',
"Calculate only for files with several versions" );
41 $this->
addOption(
'method',
"Use 'pipe' to pipe to mysql command line,\n" .
42 "\t\tdefault uses Database class",
false,
true );
45 'Fix for a specific file, without File: namespace prefixed',
52 return 'populate img_sha1';
56 return 'img_sha1 column of image table already populated.';
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 foreach (
$res as $row ) {
127 "Done %d of %d, %5.3f%% \r", $i, $numRows, $i / $numRows * 100 ) );
131 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
132 ->newFile( $row->img_name );
138 $sha1 =
$file->getRepo()->getFileSha1(
$file->getPath() );
139 if ( strval( $sha1 ) !==
'' ) {
140 if ( $isRegen &&
$file->getSha1() !== $sha1 ) {
145 $sql =
"UPDATE $imageTable SET img_sha1=" . $dbw->addQuotes( $sha1 ) .
146 " WHERE img_name=" . $dbw->addQuotes(
$file->getName() );
147 if ( $method ==
'pipe' ) {
150 fwrite( $pipe,
"$sql;\n" );
152 $dbw->query( $sql, __METHOD__ );
157 foreach (
$file->getHistory() as $oldFile ) {
159 '@phan-var OldLocalFile $oldFile';
160 $sha1 = $oldFile->getRepo()->getFileSha1( $oldFile->getPath() );
161 if ( strval( $sha1 ) !==
'' ) {
162 if ( $isRegen && $oldFile->getSha1() !== $sha1 ) {
165 $oldFile->upgradeRow();
167 $sql =
"UPDATE $oldImageTable SET oi_sha1=" . $dbw->addQuotes( $sha1 ) .
168 " WHERE (oi_name=" . $dbw->addQuotes( $oldFile->getName() ) .
" AND" .
169 " oi_archive_name=" . $dbw->addQuotes( $oldFile->getArchiveName() ) .
")";
170 if ( $method ==
'pipe' ) {
173 fwrite( $pipe,
"$sql;\n" );
175 $dbw->query( $sql, __METHOD__ );
182 if ( $method ==
'pipe' ) {
188 $t += microtime(
true );
189 $this->
output( sprintf(
"\nDone %d files in %.1f seconds\n", $numRows,
$t ) );
196 require_once RUN_MAINTENANCE_IF_MAIN;
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
hasOption( $name)
Checks to see if a particular option was set.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
A class containing constants representing the names of configuration variables.
Maintenance script to populate the img_sha1 field.
execute()
Do the actual work.
doDBUpdates()
Do the actual work.
__construct()
Default constructor.
getUpdateKey()
Get the update key name to go in the update log table.
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.