You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
9.2 KiB
PHP
255 lines
9.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services\UserCopy;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
/**
|
|
* Projects the source-owned Manifest into the administrator editing ledger.
|
|
*
|
|
* Source Chinese remains authoritative in the Manifest. Database rows only
|
|
* mirror that contract so drafts and immutable publication evidence can be
|
|
* checked without becoming an alternative source of runtime copy.
|
|
*/
|
|
final class UserCopyManifestSynchronizer
|
|
{
|
|
private const SOURCE_LOCALE = 'zh-CN';
|
|
private const DEFAULT_TARGET_LOCALES = ['en' => 'English'];
|
|
|
|
/** @var array<string,mixed> */
|
|
private readonly array $manifest;
|
|
|
|
private readonly UserCopySourceCatalog $source;
|
|
|
|
public function __construct(
|
|
private readonly ConnectionInterface $database,
|
|
array $manifest
|
|
) {
|
|
$this->manifest = UserCopyManifest::validate($manifest);
|
|
$this->source = new UserCopySourceCatalog($this->manifest);
|
|
}
|
|
|
|
/**
|
|
* @return array{active:int,created:int,updated:int,orphaned:int,stale_drafts:int,manifest_hash:string}
|
|
*/
|
|
public function synchronize(int $actorId = 0): array
|
|
{
|
|
if ($actorId < 0) {
|
|
throw new UserCopyAdminException('同步操作者身份无效。', 400);
|
|
}
|
|
|
|
return $this->database->transaction(function () use ($actorId): array {
|
|
$now = time();
|
|
$this->synchronizeSourceLocale($actorId, $now);
|
|
$this->synchronizeTargetLocales($actorId, $now);
|
|
|
|
$sourceEntries = $this->source->entries();
|
|
$manifestEntries = [];
|
|
foreach ($this->manifest['entries'] as $entry) {
|
|
$manifestEntries[$entry['key']] = $entry;
|
|
}
|
|
|
|
$created = 0;
|
|
$updated = 0;
|
|
$staleDrafts = 0;
|
|
foreach ($sourceEntries as $copyKey => $sourceEntry) {
|
|
$manifestEntry = $manifestEntries[$copyKey];
|
|
$placeholderContract = $manifestEntry['parameters'];
|
|
$placeholderJson = UserCopySourceCatalog::canonicalJson(
|
|
$placeholderContract
|
|
);
|
|
$placeholderHash = hash('sha256', $placeholderJson);
|
|
$sourceHash = hash('sha256', $sourceEntry['source']);
|
|
$entry = $this->database->table('ui_copy_entries')
|
|
->where('copy_key', $copyKey)
|
|
->lockForUpdate()
|
|
->first();
|
|
|
|
$values = [
|
|
'scope' => $sourceEntry['scope'],
|
|
'page_key' => $manifestEntry['page'],
|
|
'location' => $copyKey,
|
|
'kind' => $sourceEntry['kind'],
|
|
'format' => $sourceEntry['format'],
|
|
'criticality' => $sourceEntry['criticality'],
|
|
'required' => $sourceEntry['required'] ? 1 : 0,
|
|
'allow_hide' => $sourceEntry['allow_hide'] ? 1 : 0,
|
|
'max_length' => $sourceEntry['max_length'],
|
|
'source_text_zh' => $sourceEntry['source'],
|
|
'source_hash_zh' => $sourceHash,
|
|
'placeholder_contract_json' => $placeholderJson,
|
|
'placeholder_hash' => $placeholderHash,
|
|
'manifest_revision' => (int) $this->manifest['version'],
|
|
'state' => 'active',
|
|
'last_seen_at' => $now,
|
|
'updated_by' => $actorId,
|
|
'updated_at' => $now,
|
|
];
|
|
|
|
if ($entry === null) {
|
|
$this->database->table('ui_copy_entries')->insert($values + [
|
|
'copy_key' => $copyKey,
|
|
'first_seen_at' => $now,
|
|
'created_by' => $actorId,
|
|
'created_at' => $now,
|
|
]);
|
|
$created++;
|
|
continue;
|
|
}
|
|
|
|
$contractChanged = !hash_equals(
|
|
(string) $entry->source_hash_zh,
|
|
$sourceHash
|
|
) || !hash_equals(
|
|
(string) $entry->placeholder_hash,
|
|
$placeholderHash
|
|
);
|
|
$this->database->table('ui_copy_entries')
|
|
->where('id', (int) $entry->id)
|
|
->update($values);
|
|
$updated++;
|
|
|
|
if ($contractChanged) {
|
|
$staleDrafts += $this->markDraftsStale(
|
|
[(int) $entry->id],
|
|
'source_contract_changed',
|
|
$now
|
|
);
|
|
}
|
|
}
|
|
|
|
$orphanQuery = $this->database->table('ui_copy_entries')
|
|
->where('state', 'active');
|
|
$activeKeys = array_keys($sourceEntries);
|
|
if ($activeKeys !== []) {
|
|
$orphanQuery->whereNotIn('copy_key', $activeKeys);
|
|
}
|
|
$orphanIds = array_map(
|
|
static fn ($row): int => (int) $row->id,
|
|
$orphanQuery->lockForUpdate()->get(['id'])->all()
|
|
);
|
|
if ($orphanIds !== []) {
|
|
$this->database->table('ui_copy_entries')
|
|
->whereIn('id', $orphanIds)
|
|
->update([
|
|
'state' => 'orphaned',
|
|
'updated_by' => $actorId,
|
|
'updated_at' => $now,
|
|
]);
|
|
$staleDrafts += $this->markDraftsStale(
|
|
$orphanIds,
|
|
'source_entry_orphaned',
|
|
$now
|
|
);
|
|
}
|
|
|
|
return [
|
|
'active' => count($sourceEntries),
|
|
'created' => $created,
|
|
'updated' => $updated,
|
|
'orphaned' => count($orphanIds),
|
|
'stale_drafts' => $staleDrafts,
|
|
'manifest_hash' => $this->source->sourceHash(),
|
|
];
|
|
});
|
|
}
|
|
|
|
private function synchronizeSourceLocale(int $actorId, int $now): void
|
|
{
|
|
$claimed = $this->database->table('ui_copy_locales')
|
|
->whereNotNull('source_claim')
|
|
->lockForUpdate()
|
|
->first();
|
|
if ($claimed !== null
|
|
&& (string) $claimed->locale !== self::SOURCE_LOCALE) {
|
|
throw new UserCopyAdminException(
|
|
'中文源语言声明已被其他语言占用。',
|
|
409
|
|
);
|
|
}
|
|
|
|
$locale = $this->database->table('ui_copy_locales')
|
|
->where('locale', self::SOURCE_LOCALE)
|
|
->lockForUpdate()
|
|
->first();
|
|
$values = [
|
|
'display_name' => '简体中文',
|
|
'enabled' => 1,
|
|
'source_claim' => 1,
|
|
'updated_by' => $actorId,
|
|
'updated_at' => $now,
|
|
];
|
|
if ($locale === null) {
|
|
$this->database->table('ui_copy_locales')->insert($values + [
|
|
'locale' => self::SOURCE_LOCALE,
|
|
'created_by' => $actorId,
|
|
'created_at' => $now,
|
|
]);
|
|
return;
|
|
}
|
|
$this->database->table('ui_copy_locales')
|
|
->where('id', (int) $locale->id)
|
|
->update($values);
|
|
}
|
|
|
|
private function synchronizeTargetLocales(int $actorId, int $now): void
|
|
{
|
|
foreach (self::DEFAULT_TARGET_LOCALES as $locale => $displayName) {
|
|
$row = $this->database->table('ui_copy_locales')
|
|
->where('locale', $locale)
|
|
->lockForUpdate()
|
|
->first();
|
|
if ($row !== null) {
|
|
if ($row->source_claim !== null) {
|
|
throw new UserCopyAdminException(
|
|
'目标语言错误地占用了源语言声明。',
|
|
409
|
|
);
|
|
}
|
|
if ($locale === 'en'
|
|
&& (string) $row->display_name === '英语') {
|
|
$this->database->table('ui_copy_locales')
|
|
->where('id', (int) $row->id)
|
|
->update([
|
|
'display_name' => $displayName,
|
|
'updated_by' => $actorId,
|
|
'updated_at' => $now,
|
|
]);
|
|
}
|
|
continue;
|
|
}
|
|
$this->database->table('ui_copy_locales')->insert([
|
|
'locale' => $locale,
|
|
'display_name' => $displayName,
|
|
'enabled' => 1,
|
|
'source_claim' => null,
|
|
'created_by' => $actorId,
|
|
'updated_by' => $actorId,
|
|
'created_at' => $now,
|
|
'updated_at' => $now,
|
|
]);
|
|
}
|
|
}
|
|
|
|
/** @param list<int> $entryIds */
|
|
private function markDraftsStale(
|
|
array $entryIds,
|
|
string $reason,
|
|
int $now
|
|
): int {
|
|
if ($entryIds === []) {
|
|
return 0;
|
|
}
|
|
return $this->database->table('ui_copy_drafts')
|
|
->whereIn('entry_id', $entryIds)
|
|
->where('validation_state', '<>', 'stale')
|
|
->update([
|
|
'validation_state' => 'stale',
|
|
'validation_errors_json' => UserCopySourceCatalog::canonicalJson([
|
|
$reason,
|
|
]),
|
|
'updated_at' => $now,
|
|
]);
|
|
}
|
|
}
|