api#326 Числовые UTM и новый шум каждого перехода

This commit is contained in:
Panfilov Konstantin committed 2026-10-25 02:05:48 +03:00
1 parent f30742c2fb
commit 81e189ca67
16 files changed
+303 -160

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
<?php
/** api#326: только согласованные имена/enum UTM; чужие поля/JSON-типы сохраняются. */
declare(strict_types=1);
if ($argc!==2) { fwrite(STDERR,"Использование: php bin/ensure-utm-config.php config.json\n"); exit(2); }
try {
$path=$argv[1]; $raw=file_get_contents($path);
$config=json_decode($raw,false,512,JSON_THROW_ON_ERROR);
if (!$config instanceof stdClass || !($config->utm ?? null) instanceof stdClass) throw new RuntimeException('Отсутствует объект utm');
$before=serialize($config);
foreach (['registration'=>'reg','bchoice'=>'chob'] as $old=>$new) {
if (property_exists($config->utm,$old)) {
if (property_exists($config->utm,$new)) throw new RuntimeException('Конфликт старого и нового UTM: '.$new);
$config->utm->$new=$config->utm->$old; unset($config->utm->$old);
}
}
foreach (['theme'=>['dark'=>'1','light'=>'2'],'reg'=>['before'=>'1','after'=>'2'],'chob'=>['b'=>'1','a'=>'2'],'focus'=>['face'=>'1','center'=>'2']] as $key=>$map) {
if (!property_exists($config->utm,$key)) continue;
$setting=$config->utm->$key;
if (!$setting instanceof stdClass || !($setting->weights ?? null) instanceof stdClass || !(is_string($setting->default ?? null) || is_int($setting->default ?? null))) throw new RuntimeException('Некорректный UTM: '.$key);
$weights=new stdClass;
foreach ($setting->weights as $variant=>$weight) {
$digit=$map[$variant] ?? (string)$variant;
if (!in_array($digit,['1','2'],true) || property_exists($weights,$digit)) throw new RuntimeException('Неоднозначный вариант UTM: '.$key);
$weights->$digit=$weight;
}
$default=$map[$setting->default] ?? $setting->default;
if (!in_array((string)$default,['1','2'],true)) throw new RuntimeException('Некорректный default UTM: '.$key);
$setting->weights=$weights; $setting->default=$default;
}
if (serialize($config)===$before) { echo "Числовые UTM уже настроены; config не изменён.\n"; exit(0); }
$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');
echo "Мигрированы только согласованные имена и enum UTM.\n"; exit(10);
} catch(Throwable $error) { fwrite(STDERR,$error->getMessage()."\n"); exit(2); }