11 $usage->setDefinitionText(
"Set Vault Big Keys");
14 $vaultid =
$usage->addOptionalParameter(
"vaultid",
"restrict to single Vault id",
"", 0);
15 $stop =
$usage->addEmptyParameter(
"stop-on-error",
"Stop on first error");
20 ini_set(
"memory_limit", -1);
39 private $minIndex = 0;
40 private $maxIndex = 0;
60 $this->logFile = sprintf(
"%s/.renameVaultKeys.log",
DEFAULT_PUBDIR);
61 if (PHP_INT_SIZE >= 8) {
62 $this->minIndex = 1 << 31;
66 $this->maxIndex = PHP_INT_MAX;
70 simpleQuery(
'', sprintf(
"SELECT nextval(%s)", pg_escape_literal($seqName)) , $res,
true,
true,
true);
79 PREPARE update_docvaultindex_vaultid (bigint, bigint) AS UPDATE docvaultindex SET vaultid = $1 WHERE vaultid = $2;
84 PREPARE update_vaultdiskstorage_id_file (bigint, bigint) AS UPDATE vaultdiskstorage SET id_file = $1 where id_file = $2;
89 PREPARE update_vaultdiskstorage_teng_id_file (bigint, bigint) AS UPDATE vaultdiskstorage SET teng_id_file = $1 where teng_id_file = $2;
95 docvaultindex.vaultid,
96 count(docvaultindex.vaultid) AS
n,
97 array_agg(docfrom.id) AS docids,
98 array_agg(docfrom.fromid) AS fromids
103 docfrom.id = docvaultindex.docid AND
104 docvaultindex.vaultid < %s %s
105 GROUP BY docvaultindex.vaultid
106 ORDER BY docvaultindex.vaultid
109 $sql = sprintf(
$sql, pg_escape_literal($this->minIndex) , empty(
$vaultid) ?
"" : sprintf(
"and docvaultindex.vaultid = %s", pg_escape_literal(
$vaultid)));
111 $this->
verbose(
"Retrieve vault file to reindex...");
113 $count = count($vaultIndexes);
114 $this->
verbose(sprintf(
"%d to update.\n", $count));
117 $this->vaultFile->savePoint(
"DRY");
121 foreach ($vaultIndexes as $ki => $vaultIndex) {
122 $docids = explode(
",", substr($vaultIndex[
"docids"], 1, -1));
123 $fromids = array_unique(explode(
",", substr($vaultIndex[
"fromids"], 1, -1)));
124 $vaultId = $vaultIndex[
"vaultid"];
129 $this->
verbose(sprintf(
"Unknow file ref \"%s\" in vault database", $vaultId));
132 if ($fileInfo->public_access ===
"t") {
138 $this->vaultFile->savePoint(
"VAULTKEYS");
141 $this->
verbose(
"%d/%d) Updating vault key %s -> %s\n", $ki + 1, $count, $vaultId, $newId);
142 foreach ($fromids as $fromid) {
144 $this->
verbose(
"\tUpdating documents #%s\n", implode(
",", $docids));
152 $this->vaultFile->commitPoint(
"VAULTKEYS");
154 if ($ki % self::timeWindow === 0) {
155 $now = microtime(
true);
156 $ticTime[($kt++) % self::timeWindowFactor] = $now;
157 $tenTime = $now - min($ticTime);
158 $remaid = ($count - $ki) / self::timeWindow * $tenTime / count($ticTime);
163 catch(Exception $e) {
164 $this->vaultFile->rollbackPoint(
"VAULTKEYS");
168 $color =
"\033" .
'[01;31m';
169 $nocolor =
"\033" .
'[0m';
170 fprintf(STDERR, $e->getMessage() .
"\n");
171 print ($color .
"Error:" . $e->getMessage() . $nocolor .
"\n");
176 $this->vaultFile->rollbackPoint(
"DRY");
184 $sDelay = sprintf(
"%dh %02dmin", $delay / 3600, ($delay % 3600) / 60);
185 } elseif ($delay > 60) {
186 $sDelay = sprintf(
"%02dmin %02ds", $delay / 60, $delay % 60);
188 $sDelay = sprintf(
"%02ds", $delay);
203 throw new \Dcp\Exception(
"Vault index is corrupted. Use \"refreshVaultIndex\" wsh script to verify/repair index");
215 $vaultId = $fileInfo->id_file;
216 $currentFileName = $fileInfo->path;
217 $currentDirName = dirname($currentFileName);
218 $currentBaseName = basename($currentFileName);
219 if (!preg_match(
'/^(?P<vid>\d+)(?P<extension>\..*)$/', $currentBaseName, $m)) {
220 throw new \Dcp\Exception(sprintf(
"Malformed filename '%s'.", $currentFileName));
222 if ($vaultId !== $m[
'vid']) {
223 throw new \Dcp\Exception(sprintf(
"VID mismatch between filename '%s' and vid '%s'.", $currentFileName, $m[
'vid']));
225 $newBaseName = sprintf(
"%s%s", $newId, $m[
'extension']);
226 $newFileName = $currentDirName . DIRECTORY_SEPARATOR . $newBaseName;
227 $this->
verbose(
"\tmv %s %s \n", $currentFileName, $newFileName);
229 if (!$this->dryRun) {
230 $sql = sprintf(
"EXECUTE update_docvaultindex_vaultid(%s, %s)", pg_escape_literal($newId) , pg_escape_literal($vaultId));
232 $sql = sprintf(
"EXECUTE update_vaultdiskstorage_id_file(%s, %s)", pg_escape_literal($newId) , pg_escape_literal($vaultId));
234 $sql = sprintf(
"EXECUTE update_vaultdiskstorage_teng_id_file(%s, %s)", pg_escape_literal($newId) , pg_escape_literal($vaultId));
236 $this->
logVault($currentFileName, $newFileName);
237 if (!rename($currentFileName, $newFileName)) {
238 throw new \Dcp\Exception(
"Cannot rename $currentFileName to $newFileName");
248 protected function logVault($currentName, $newName)
250 if (!$this->dryRun) {
251 if (file_put_contents($this->logFile, sprintf(
"%s\n%s", $currentName, $newName)) ===
false) {
252 throw new \Dcp\Exception(sprintf(
"Error writing content to transaction's log file '%s'.", $this->logFile));
261 if (is_file($this->logFile)) {
262 if ((
$data = file($this->logFile)) ===
false) {
263 throw new \Dcp\Exception(sprintf(
"Error reading content from transaction's log file '%s'.", $this->logFile));
266 $newFileName = trim(
$data[1]);
267 $currentFileName = trim(
$data[0]);
269 if ($newFileName && $currentFileName && !rename($newFileName, $currentFileName)) {
270 throw new \Dcp\Exception(
"Cannot rename $newFileName to $currentFileName");
279 call_user_func_array(
"printf", func_get_args());
284 $color =
"\033" .
'[1;33;40m';
285 $nocolor =
"\033" .
'[0m';
287 $args = func_get_args();
290 $args[0] =
"\t" . $color .
$args[0] . $nocolor .
"\n";
292 $color =
"\033" .
'[1;33m';
293 $args[0] =
"\r" . $color .
$args[0] . $nocolor;
295 call_user_func_array(
"printf",
$args);
308 $s->addFilter(
"id in (%s)", implode(
",", $docids));
309 $s->setObjectReturn();
310 $dl =
$s->getDocumentList();
314 foreach ($dl as $family) {
315 $attributes = $family->getAttributes();
316 $parameters = $family->getOwnParams();
317 $defVal = $family->getOwnDefValues();
319 foreach ($attributes as $oattr) {
320 if (($oattr->type ===
"file" || $oattr->type ===
"image")) {
321 if ($oattr->usefor ===
"Q") {
322 if (!empty($parameters[$oattr->id])) {
323 $newValue = str_replace(
'|' . $vaultId .
'|',
'|' . $newId .
'|', $parameters[$oattr->id]);
324 if ($newValue !== $parameters[$oattr->id]) {
325 $err = $family->setParam($oattr->id, $newValue);
327 throw new \Dcp\Exception(sprintf(
"Cannot update family (%s) parameter (%s) : %s ", $family->name, $oattr->id,
$err));
329 $this->
verbose(
"Update family \"%s\" parameter \"%s\"", $family->name, $oattr->id);
335 if (!empty($defVal[$oattr->id])) {
336 $newValue = str_replace(
'|' . $vaultId .
'|',
'|' . $newId .
'|', $defVal[$oattr->id]);
337 if ($newValue !== $defVal[$oattr->id]) {
338 $err = $family->setDefValue($oattr->id, $newValue);
340 throw new \Dcp\Exception(sprintf(
"Cannot update family (%s) default value (%s) : %s ", $family->name, $oattr->id,
$err));
342 $this->
verbose(
"Update family \"%s\" default value \"%s\"", $family->name, $oattr->id);
350 $err = $family->modify(
false);
352 throw new \Dcp\Exception(sprintf(
"Cannot update family (%s) : %s ", $family->name,
$err));
370 $sql = sprintf(
"update doc%d ", $fromid);
372 foreach ($attrs as $attr) {
379 $sql.= sprintf(
" %s=regexp_replace(%s, E'\\\\|%s($|\\n|[\\\\|.*])',E'|%s\\\\1','g')", pg_escape_string($attr) , pg_escape_string($attr) , $vaultId, $newId);
382 $sql.= sprintf(
" where id in (%s)", implode(
",", $docids));
384 if (!$this->dryRun) {
387 $sql = sprintf(
"EXECUTE update_docvaultindex_vaultid(%s, %s)", pg_escape_literal($newId) , pg_escape_literal($vaultId));
399 if (!isset($this->attrIds[
$famId])) {
401 $this->attrIds[
$famId] = [];
402 $attributes =
$d->getFileAttributes();
403 foreach ($attributes as $oa) {
404 $this->attrIds[
$famId][] = $oa->id;
407 return $this->attrIds[
$famId];
411 if ($this->randKey === 0) {
413 mt_srand($this->vaultFile->getNewVaultId());
419 $newId = mt_rand($this->minIndex, $this->maxIndex);
420 simpleQuery(
"", sprintf(
"select true from vaultdiskstorage where id_file = %s", $newId) , $nogood,
true,
true);
if(substr($wsh, 0, 1)!= '/') $args
updateVaultData($newId, VaultFileInfo $fileInfo)
getFamilyFileAttrIds($famId)
getSequenceNextVal($seqName)
updateDocData($vaultId, $newId, $fromid, $docids)
logVault($currentName, $newName)
static getFileInfo($idfile, $teng_name="")
updateFamilyData($vaultId, $newId, $docids)
simpleQuery($dbaccess, $query, &$result=array(), $singlecolumn=false, $singleresult=false, $useStrict=null)
if($file) if($subject==""&&$file) if($subject=="") $err
Verify arguments for wsh programs.