From 3e44a718cd4bd222474e86dfdabaeba58620bcd4 Mon Sep 17 00:00:00 2001 From: codex Date: Sat, 7 Nov 2026 14:03:36 +0800 Subject: [PATCH] feat(i18n): project page and advanced workspaces --- .../UserCopy/UserCopyAdminService.php | 137 ++++++++++++++---- web/tests/UserCopyAdminServiceTest.php | 84 ++++++++++- 2 files changed, 189 insertions(+), 32 deletions(-) diff --git a/web/app/Services/UserCopy/UserCopyAdminService.php b/web/app/Services/UserCopy/UserCopyAdminService.php index 9e602037..b77f5a32 100644 --- a/web/app/Services/UserCopy/UserCopyAdminService.php +++ b/web/app/Services/UserCopy/UserCopyAdminService.php @@ -67,13 +67,35 @@ final class UserCopyAdminService */ public function workspace(array $query = []): array { + $manifestByKey = []; + foreach ($this->manifest['entries'] as $manifestEntry) { + $manifestByKey[$manifestEntry['key']] = $manifestEntry; + } + $pages = []; + $pageLabels = []; foreach ($this->manifest['pages'] as $pageKey => $definition) { + $pageLabels[$pageKey] = (string) $definition['label']; $pages[] = [ 'id' => $pageKey, 'label' => (string) $definition['label'], ]; } + $groupIds = []; + foreach ($this->manifest['entries'] as $manifestEntry) { + $groupIds[(string) $manifestEntry['editor_group']] = true; + } + ksort($groupIds, SORT_STRING); + $groups = []; + foreach (array_keys($groupIds) as $groupId) { + $groups[] = [ + 'id' => $groupId, + 'label' => $pageLabels[$groupId] + ?? ($groupId === 'common.shared' + ? '公共框架' : $groupId), + ]; + } + $selectedPage = isset($query['page']) && is_string($query['page']) ? trim($query['page']) : ''; if ($selectedPage === '') { @@ -82,6 +104,29 @@ final class UserCopyAdminService if (!isset($this->manifest['pages'][$selectedPage])) { throw new UserCopyCatalogException('Unknown user copy page.'); } + $channel = 'legacy'; + if (array_key_exists('channel', $query)) { + if (!is_string($query['channel'])) { + throw new UserCopyAdminException('文案编辑通道无效。'); + } + $candidate = trim($query['channel']); + if ($candidate !== '') { + $channel = $candidate; + } + } + if (!in_array($channel, ['legacy', 'page', 'advanced', 'all'], true)) { + throw new UserCopyAdminException('文案编辑通道无效。'); + } + $selectedGroup = ''; + if (array_key_exists('group', $query)) { + if (!is_string($query['group'])) { + throw new UserCopyAdminException('文案编辑分组无效。'); + } + $selectedGroup = trim($query['group']); + if ($selectedGroup !== '' && !isset($groupIds[$selectedGroup])) { + throw new UserCopyAdminException('文案编辑分组无效。'); + } + } $localeRows = $this->database->table('ui_copy_locales') ->whereNull('source_claim') @@ -106,45 +151,71 @@ final class UserCopyAdminService } } - $entryRows = $this->database->table('ui_copy_entries') + $allEntryRows = $this->database->table('ui_copy_entries') ->where('state', 'active') - ->whereIn( - 'scope', - $this->manifest['pages'][$selectedPage]['scopes'] - ) ->orderBy('copy_key') ->get() ->all(); - $entriesByKey = []; - foreach ($entryRows as $entry) { - $entriesByKey[(string) $entry->copy_key] = $entry; + $allEntries = []; + foreach ($allEntryRows as $entry) { + $allEntries[(string) $entry->copy_key] = $entry; } $projectionReady = true; try { - $allEntries = []; - foreach ($this->database->table('ui_copy_entries') - ->where('state', 'active')->orderBy('copy_key')->get()->all() - as $entry) { - $allEntries[(string) $entry->copy_key] = $entry; - } $this->assertManifestProjection($allEntries); } catch (UserCopyAdminException) { $projectionReady = false; } + $selectedScopes = array_fill_keys( + $this->manifest['pages'][$selectedPage]['scopes'], + true + ); + $entryRows = []; + foreach ($allEntryRows as $entry) { + $copyKey = (string) $entry->copy_key; + $manifestEntry = $manifestByKey[$copyKey] ?? null; + $inSelectedPage = isset($selectedScopes[(string) $entry->scope]); + $matchesChannel = match ($channel) { + 'legacy' => $inSelectedPage, + 'page' => $inSelectedPage + && is_array($manifestEntry) + && $manifestEntry['editor_surface'] === 'page', + 'advanced' => is_array($manifestEntry) + && $manifestEntry['editor_surface'] !== 'page', + 'all' => is_array($manifestEntry), + }; + if (!$matchesChannel) { + continue; + } + if ($selectedGroup !== '' + && (!is_array($manifestEntry) + || $manifestEntry['editor_group'] !== $selectedGroup)) { + continue; + } + $entryRows[] = $entry; + } + $entriesByKey = []; + foreach ($entryRows as $entry) { + $entriesByKey[(string) $entry->copy_key] = $entry; + } + $drafts = []; $publishedItems = []; $currentGeneration = null; + $entryIds = array_map( + static fn ($entry): int => (int) $entry->id, + $entryRows + ); if ($selectedLocaleRow !== null) { - foreach ($this->database->table('ui_copy_drafts') - ->where('locale_id', (int) $selectedLocaleRow->id) - ->whereIn('entry_id', array_map( - static fn ($entry): int => (int) $entry->id, - $entryRows - )) - ->get()->all() as $draft) { - $drafts[(int) $draft->entry_id] = $draft; + if ($entryIds !== []) { + foreach ($this->database->table('ui_copy_drafts') + ->where('locale_id', (int) $selectedLocaleRow->id) + ->whereIn('entry_id', $entryIds) + ->get()->all() as $draft) { + $drafts[(int) $draft->entry_id] = $draft; + } } $current = $this->store->current(); $candidate = $current['locales'][$selectedLocale]['generation'] @@ -156,13 +227,10 @@ final class UserCopyAdminService ->where('catalog_generation', $candidate) ->where('state', 'ready') ->first(); - if ($publication !== null) { + if ($publication !== null && $entryIds !== []) { foreach ($this->database->table('ui_copy_revision_items') ->where('publication_id', (int) $publication->id) - ->whereIn('entry_id', array_map( - static fn ($entry): int => (int) $entry->id, - $entryRows - )) + ->whereIn('entry_id', $entryIds) ->get()->all() as $item) { $publishedItems[(int) $item->entry_id] = [ 'text' => (string) $item->published_text, @@ -177,10 +245,6 @@ final class UserCopyAdminService } } - $manifestByKey = []; - foreach ($this->manifest['entries'] as $manifestEntry) { - $manifestByKey[$manifestEntry['key']] = $manifestEntry; - } $entries = []; $coverage = [ 'total' => count($entryRows), @@ -232,6 +296,12 @@ final class UserCopyAdminService ? array_keys($contract) : [], 'severity' => is_string($manifestEntry['toast_tone'] ?? null) ? $manifestEntry['toast_tone'] : 'info', + 'editor_surface' => is_string( + $manifestEntry['editor_surface'] ?? null + ) ? $manifestEntry['editor_surface'] : null, + 'editor_group' => is_string( + $manifestEntry['editor_group'] ?? null + ) ? $manifestEntry['editor_group'] : null, 'state' => $state, 'hidden' => $hidden, 'draft_version' => $draftDto['version'] ?? 0, @@ -261,6 +331,7 @@ final class UserCopyAdminService return [ 'schema' => 1, 'pages' => $pages, + 'groups' => $groups, 'locales' => array_map(static fn (array $locale): array => [ 'id' => $locale['locale'], 'label' => $locale['display_name'], @@ -270,6 +341,10 @@ final class UserCopyAdminService 'page' => $selectedPage, 'locale' => $selectedLocale, ], + 'authoring' => [ + 'channel' => $channel, + 'group' => $selectedGroup, + ], 'coverage' => $coverage, 'entries' => $entries, 'current_generation' => $currentGeneration, diff --git a/web/tests/UserCopyAdminServiceTest.php b/web/tests/UserCopyAdminServiceTest.php index 18050594..7663b90a 100644 --- a/web/tests/UserCopyAdminServiceTest.php +++ b/web/tests/UserCopyAdminServiceTest.php @@ -193,7 +193,7 @@ $manifest = [ 'source' => '高风险操作提醒', 'format' => 'plain_text', 'parameters' => [], 'criticality' => 'critical', 'allow_hide' => false, 'toast_tone' => null, - 'editor_surface' => 'feedback', 'editor_group' => 'test.page', + 'editor_surface' => 'feedback', 'editor_group' => 'test.advanced', ], ], ]; @@ -328,12 +328,94 @@ try { 'page' => 'test.page', 'locale' => 'en', ] + && $workspace['authoring'] === [ + 'channel' => 'legacy', + 'group' => '', + ] + && $workspace['groups'] === [ + [ + 'id' => 'test.advanced', + 'label' => 'test.advanced', + ], + [ + 'id' => 'test.page', + 'label' => '测试页面', + ], + ] && $workspace['coverage']['published'] === 4 && count($workspace['entries']) === 4 && $workspace['write_ready'] === true && $workspace['current_generation'] === $first['generation'], 'Administrator workspace omitted selection, coverage or current generation.'); + $pageWorkspace = $service->workspace([ + 'page' => 'test.page', + 'locale' => 'en', + 'channel' => 'page', + ]); + $assert($pageWorkspace['authoring']['channel'] === 'page' + && $pageWorkspace['coverage']['total'] === 3 + && $pageWorkspace['coverage']['published'] === 3 + && array_column($pageWorkspace['entries'], 'key') === [ + 'test.count', 'test.help', 'test.title', + ], + 'Page editing channel did not isolate visible page copy.'); + $advancedWorkspace = $service->workspace([ + 'page' => 'test.page', + 'locale' => 'en', + 'channel' => 'advanced', + 'group' => 'test.advanced', + ]); + $assert($advancedWorkspace['authoring'] === [ + 'channel' => 'advanced', + 'group' => 'test.advanced', + ] + && $advancedWorkspace['coverage']['total'] === 1 + && $advancedWorkspace['coverage']['published'] === 1 + && count($advancedWorkspace['entries']) === 1 + && $advancedWorkspace['entries'][0]['key'] === 'test.warning' + && $advancedWorkspace['entries'][0]['editor_surface'] === 'feedback' + && $advancedWorkspace['entries'][0]['editor_group'] === 'test.advanced', + 'Advanced editing channel did not isolate non-page copy.'); + $emptyPageWorkspace = $service->workspace([ + 'page' => 'test.page', + 'locale' => 'en', + 'channel' => 'page', + 'group' => 'test.advanced', + ]); + $assert($emptyPageWorkspace['entries'] === [] + && $emptyPageWorkspace['coverage']['total'] === 0 + && $emptyPageWorkspace['current_generation'] === $first['generation'] + && $emptyPageWorkspace['write_ready'] === true, + 'Empty authoring projection lost the locale publication state.'); + $allWorkspace = $service->workspace([ + 'page' => 'test.page', + 'locale' => 'en', + 'channel' => 'all', + ]); + $assert($allWorkspace['coverage']['total'] === 4 + && count($allWorkspace['entries']) === 4, + 'All-copy authoring channel did not return the complete projection.'); + $reject( + static fn () => $service->workspace([ + 'page' => 'test.page', + 'locale' => 'en', + 'channel' => 'invalid', + ]), + 422, + 'Unknown authoring channel was accepted.' + ); + $reject( + static fn () => $service->workspace([ + 'page' => 'test.page', + 'locale' => 'en', + 'channel' => 'advanced', + 'group' => 'missing.group', + ]), + 422, + 'Unknown authoring group was accepted.' + ); + $titleTwo = $service->saveDraft( $super, 'test.title',