feat: replace chb with consent groups and preserve runtime config (api#317, #17)
deploy-preland / deploy zoomstage.online (push) Successful in 42s

This commit is contained in:
Panfilov Konstantin committed 2026-10-24 04:35:44 +03:00
1 parent 188b78a115
commit 89cc16eb4a
12 files changed
+208 -9

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
<?php
/** preland#17: миграция только utm.chb → utm.cons; остальные значения владельца сохраняются. */
declare(strict_types=1);
if ($argc !== 2) {
fwrite(STDERR, "Использование: php bin/ensure-cons-config.php /path/to/config.json\n");
exit(2);
}
try {
$path = $argv[1];
$raw = @file_get_contents($path);
if ($raw === false) throw new RuntimeException('Не удалось прочитать config.json.');
// Object decode сохраняет типы соседних {} и объектов с числовыми ключами.
$config = json_decode($raw, false, 512, JSON_THROW_ON_ERROR);
if (!$config instanceof stdClass || !($config->utm ?? null) instanceof stdClass) throw new RuntimeException('В config.json отсутствует объект utm.');
$hasCons = property_exists($config->utm, 'cons');
$cons = $hasCons ? $config->utm->cons : (object) ['weights' => (object) ['1' => 100, '2' => 0], 'default' => '2'];
if (!$cons instanceof stdClass || !(is_array($cons->weights ?? null) || ($cons->weights ?? null) instanceof stdClass)
|| !(is_string($cons->default ?? null) || is_int($cons->default ?? null))
|| !preg_match('/^[1-9][0-9]*$/D', (string) $cons->default)
|| (float) $cons->default > 2147483647) {
throw new RuntimeException('Существующий utm.cons некорректен; автоматическая перезапись запрещена.');
}
if (!property_exists($config->utm, 'chb') && $hasCons) {
fwrite(STDOUT, "cons уже настроен: config не изменён.\n");
exit(0);
}
unset($config->utm->chb);
$config->utm->cons = $cons;
$encoded = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR) . "\n";
if (file_put_contents($path, $encoded, LOCK_EX) === false) throw new RuntimeException('Не удалось записать config.json.');
fwrite(STDOUT, "Мигрированы только utm.chb/cons; остальные поля и существующие настройки cons сохранены.\n");
exit(10);
} catch (Throwable $error) {
fwrite(STDERR, $error->getMessage() . "\n");
exit(2);
}