kevacoin-cli/cli/get.php

169 lines
2.3 KiB
PHP
Raw Normal View History

2023-11-16 13:26:01 +02:00
<?php
2024-06-03 22:46:52 +03:00
// Init version
define(
'_CLITOR_IS_',
'1.3.0'
);
2023-11-16 13:26:01 +02:00
// Init helper
function _exec(
string $processor,
string $command
): mixed
{
if (false !== exec(sprintf('%s %s', $processor, $command), $output))
{
$rows = [];
foreach($output as $row)
{
$rows[] = $row;
}
if ($result = @json_decode(implode(PHP_EOL, $rows)))
{
return $result;
}
}
return false;
}
2024-06-03 22:46:52 +03:00
// Validate arguments
if (empty($argv[1]))
{
print(
'processor required' . PHP_EOL
);
2023-11-16 13:26:01 +02:00
2024-06-03 22:46:52 +03:00
exit;
}
2023-11-16 13:26:01 +02:00
2024-06-03 22:46:52 +03:00
if (!file_exists($argv[1]))
2023-11-16 13:26:01 +02:00
{
2024-06-03 22:46:52 +03:00
print(
'processor does not exist' . PHP_EOL
);
exit;
}
if (empty($argv[2]))
{
print(
'namespace required' . PHP_EOL
2023-11-16 22:17:37 +02:00
);
2024-06-03 22:46:52 +03:00
exit;
2023-11-16 13:26:01 +02:00
}
2024-06-03 22:46:52 +03:00
if (empty($argv[3]))
2023-11-16 22:17:37 +02:00
{
2024-06-03 22:46:52 +03:00
print(
'destination file required' . PHP_EOL
);
exit;
2023-11-16 22:17:37 +02:00
}
2023-11-16 13:26:01 +02:00
2024-06-03 22:46:52 +03:00
if (file_exists($argv[3]))
2023-11-16 22:17:37 +02:00
{
2024-06-03 22:46:52 +03:00
print(
'destination file already exists' . PHP_EOL
2023-11-16 22:17:37 +02:00
);
2024-06-03 22:46:52 +03:00
exit;
2023-11-16 22:17:37 +02:00
}
2023-11-16 13:26:01 +02:00
2024-06-03 22:46:52 +03:00
// Get clitoris
$clitoris = _exec(
$argv[1],
sprintf(
'keva_get %s "_CLITOR_IS_"',
$argv[2]
)
);
// Validate protocol
if (empty($clitoris->value))
2023-11-16 22:17:37 +02:00
{
2024-06-03 22:46:52 +03:00
print(
'_CLITOR_IS_ not found' . PHP_EOL
2023-11-16 22:17:37 +02:00
);
2024-06-03 22:46:52 +03:00
exit;
2023-11-16 22:17:37 +02:00
}
2023-11-16 13:26:01 +02:00
// Merge content data
$chain = [];
2024-06-03 22:46:52 +03:00
2023-11-16 22:17:37 +02:00
foreach (
2023-11-16 22:25:58 +02:00
(array)
2023-11-16 22:17:37 +02:00
_exec(
$argv[1],
sprintf(
2024-06-03 22:46:52 +03:00
'keva_filter %s "\d+"',
2023-11-16 22:17:37 +02:00
$argv[2]
)
) as $piece)
2023-11-16 13:26:01 +02:00
{
if (
!isset($piece->key) ||
!isset($piece->value) ||
!isset($piece->height)
)
2023-11-16 22:25:58 +02:00
{
2024-06-03 22:46:52 +03:00
print(
'please wait for all pieces sending complete!' . PHP_EOL
2023-11-16 22:25:58 +02:00
);
2024-06-03 22:46:52 +03:00
exit;
2023-11-16 22:25:58 +02:00
}
// Keep all key versions in memory
$chain[$piece->key][$piece->height] = $piece->value;
2023-11-16 22:17:37 +02:00
print_r(
$piece
);
2023-11-16 13:26:01 +02:00
}
// Select last piece value by it max block height
//
// piece could have many of versions (with same key)
// this feature related to data reading correction after recovery #1
$pieces = [];
foreach ($chain as $key => $height)
{
ksort(
$height
);
2024-06-03 22:46:52 +03:00
$pieces[$key] = $height
[
array_key_last(
$height
)
];
}
2023-11-16 22:17:37 +02:00
ksort(
$pieces
);
2023-11-16 13:26:01 +02:00
// Save file to destination
2023-11-16 13:26:01 +02:00
file_put_contents(
2024-06-03 22:46:52 +03:00
$argv[3],
2023-11-16 13:26:01 +02:00
base64_decode(
2023-11-16 22:17:37 +02:00
implode('', $pieces)
2023-11-16 13:26:01 +02:00
)
);
2024-06-03 22:46:52 +03:00
printf(
'namespace "%s" saved to "%s"' . PHP_EOL,
$argv[2],
$argv[3]
2023-11-16 13:26:01 +02:00
);