From f16a4ede93de2173f9e0807e8df3435b2fab46ed Mon Sep 17 00:00:00 2001 From: PP1 <74917296+pengpengyi92@users.noreply.github.com> Date: Thu, 6 Aug 2026 02:09:26 +0800 Subject: [PATCH] fix(wiki): exclude archived pages from stats queries --- internal/application/repository/wiki_page.go | 8 ++-- .../application/repository/wiki_page_test.go | 37 +++++++++++++++++++ internal/application/service/wiki_page.go | 2 +- internal/types/interfaces/wiki_page.go | 4 +- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/internal/application/repository/wiki_page.go b/internal/application/repository/wiki_page.go index 4cbdc97c..519dde17 100644 --- a/internal/application/repository/wiki_page.go +++ b/internal/application/repository/wiki_page.go @@ -1055,11 +1055,11 @@ func (r *wikiPageRepository) FindSimilarPages( return out, nil } -// ListAll retrieves all wiki pages in a knowledge base +// ListAll retrieves all non-archived wiki pages in a knowledge base. func (r *wikiPageRepository) ListAll(ctx context.Context, kbID string) ([]*types.WikiPage, error) { var pages []*types.WikiPage if err := r.db.WithContext(ctx). - Where("knowledge_base_id = ?", kbID). + Where("knowledge_base_id = ? AND status <> ?", kbID, types.WikiPageStatusArchived). Order("page_type ASC, title ASC"). Find(&pages).Error; err != nil { return nil, err @@ -1193,7 +1193,7 @@ func (r *wikiPageRepository) CountByType(ctx context.Context, kbID string) (map[ if err := r.db.WithContext(ctx). Model(&types.WikiPage{}). Select("page_type, count(*) as count"). - Where("knowledge_base_id = ?", kbID). + Where("knowledge_base_id = ? AND status <> ?", kbID, types.WikiPageStatusArchived). Group("page_type"). Scan(&results).Error; err != nil { return nil, err @@ -1211,7 +1211,7 @@ func (r *wikiPageRepository) CountOrphans(ctx context.Context, kbID string) (int var count int64 if err := r.db.WithContext(ctx). Model(&types.WikiPage{}). - Where("knowledge_base_id = ?", kbID). + Where("knowledge_base_id = ? AND status <> ?", kbID, types.WikiPageStatusArchived). Where(r.wikiEmptyInLinksPredicate()). // Exclude the index page because it is naturally a root page. Where("page_type <> ?", types.WikiPageTypeIndex). diff --git a/internal/application/repository/wiki_page_test.go b/internal/application/repository/wiki_page_test.go index ef8f0952..66d82bb5 100644 --- a/internal/application/repository/wiki_page_test.go +++ b/internal/application/repository/wiki_page_test.go @@ -429,6 +429,43 @@ func TestCountOrphans_SQLiteCountsEmptyInLinks(t *testing.T) { assert.Equal(t, int64(1), got) } +func TestWikiStatsQueriesExcludeArchivedPages(t *testing.T) { + db := setupWikiPagesTestDB(t) + repo := NewWikiPageRepository(db) + ctx := context.Background() + + live := makeWikiPage("kb-stats", "entity/live", types.WikiPageTypeEntity, types.WikiPageStatusPublished) + live.InLinks = types.StringArray{"index"} + live.OutLinks = types.StringArray{"concept/live-target"} + archived := makeWikiPage("kb-stats", "entity/archived", types.WikiPageTypeEntity, types.WikiPageStatusArchived) + archived.InLinks = types.StringArray{} + archived.OutLinks = types.StringArray{"concept/archived-target"} + indexPage := makeWikiPage("kb-stats", "index", types.WikiPageTypeIndex, types.WikiPageStatusPublished) + indexPage.InLinks = types.StringArray{} + otherKB := makeWikiPage("kb-other", "entity/other", types.WikiPageTypeEntity, types.WikiPageStatusPublished) + + for _, p := range []*types.WikiPage{live, archived, indexPage, otherKB} { + require.NoError(t, repo.Create(ctx, p)) + } + + counts, err := repo.CountByType(ctx, "kb-stats") + require.NoError(t, err) + assert.Equal(t, int64(1), counts[types.WikiPageTypeEntity]) + assert.Equal(t, int64(1), counts[types.WikiPageTypeIndex]) + + orphans, err := repo.CountOrphans(ctx, "kb-stats") + require.NoError(t, err) + assert.Equal(t, int64(0), orphans) + + pages, err := repo.ListAll(ctx, "kb-stats") + require.NoError(t, err) + assert.Len(t, pages, 2) + for _, page := range pages { + assert.NotEqual(t, types.WikiPageStatusArchived, page.Status) + assert.Equal(t, "kb-stats", page.KnowledgeBaseID) + } +} + // makeWikiRevision builds a snapshot row for the given page state. func makeWikiRevision(page *types.WikiPage, version int, editSource string) *types.WikiPageRevision { return &types.WikiPageRevision{ diff --git a/internal/application/service/wiki_page.go b/internal/application/service/wiki_page.go index f523c312..49812d1a 100644 --- a/internal/application/service/wiki_page.go +++ b/internal/application/service/wiki_page.go @@ -906,7 +906,7 @@ func (s *wikiPageService) RebuildLinks(ctx context.Context, kbID string) error { return nil } -// ListAllPages retrieves all wiki pages without pagination. +// ListAllPages retrieves all non-archived wiki pages without pagination. func (s *wikiPageService) ListAllPages(ctx context.Context, kbID string) ([]*types.WikiPage, error) { return s.repo.ListAll(ctx, kbID) } diff --git a/internal/types/interfaces/wiki_page.go b/internal/types/interfaces/wiki_page.go index 9d2c9662..985062d7 100644 --- a/internal/types/interfaces/wiki_page.go +++ b/internal/types/interfaces/wiki_page.go @@ -97,7 +97,7 @@ type WikiPageService interface { // RebuildIndexPage regenerates the index page. RebuildIndexPage(ctx context.Context, kbID string) error - // ListAllPages retrieves all wiki pages in a knowledge base without pagination. + // ListAllPages retrieves all non-archived wiki pages in a knowledge base without pagination. // Used for index rebuild, graph generation, cross-link injection, etc. ListAllPages(ctx context.Context, kbID string) ([]*types.WikiPage, error) @@ -354,7 +354,7 @@ type WikiPageRepository interface { // Used to recompute cached paths when a folder subtree is moved/renamed. ListPagesByFolderIDs(ctx context.Context, kbID string, folderIDs []string) ([]*types.WikiPage, error) - // ListAll retrieves all wiki pages in a knowledge base (for link rebuilding, graph generation). + // ListAll retrieves all non-archived wiki pages in a knowledge base (for link rebuilding, graph generation). ListAll(ctx context.Context, kbID string) ([]*types.WikiPage, error) // ListRecentForSuggestions returns recent user-visible wiki pages under the given