fix: record generated wiki page versions (#17931)

This commit is contained in:
buua436
2026-08-06 16:49:50 +08:00
committed by GitHub
parent 7d0d1ab642
commit 8379165c12
7 changed files with 190 additions and 25 deletions

View File

@@ -3174,7 +3174,7 @@ async def update_wiki_page(
content_before = ""
try:
res = settings.docStoreConn.search(
select_fields=["id", "content_with_weight"],
select_fields=["id", "md_with_weight", "content_with_weight"],
highlight_fields=[],
condition={
"compile_kwd": [WIKI_PAGE_COMPILE_KWD],
@@ -3190,11 +3190,11 @@ async def update_wiki_page(
)
field_map = settings.docStoreConn.get_fields(
res,
["id", "content_with_weight"],
["id", "md_with_weight", "content_with_weight"],
)
if field_map:
row_id, row = next(iter(field_map.items()))
content_before = row.get("content_with_weight") or ""
content_before = row.get("md_with_weight") or row.get("content_with_weight") or ""
except Exception:
logging.exception(
"update_wiki_page: lookup failed for kb=%s slug=%s",
@@ -3215,6 +3215,7 @@ async def update_wiki_page(
ok = settings.docStoreConn.update(
{"id": row_id},
{
"md_with_weight": rendered,
"content_with_weight": rendered,
"summary_with_weight": summary,
"outlinks_kwd": list(outlinks),
@@ -3233,6 +3234,17 @@ async def update_wiki_page(
if not ok:
return True, None
refresh_idx = getattr(settings.docStoreConn, "refresh_idx", None)
if callable(refresh_idx):
try:
await thread_pool_exec(refresh_idx, index_nm)
except Exception:
logging.exception(
"update_wiki_page: index refresh failed for kb=%s slug=%s",
dataset_id,
full_slug,
)
# Record a file_commit row on every real change. ``record_page_edit``
# returns None for empty-diff saves, which we silently swallow.
try:
@@ -3824,4 +3836,18 @@ async def clear_wiki(dataset_id: str, tenant_id: str):
)
deleted[kwd] = False
from api.db.services.file_commit_service import FileCommitService
if all(result is not False for result in deleted.values()):
try:
deleted["file_commit_history"] = FileCommitService.delete_all_page_history(dataset_id)
except Exception:
logging.exception(
"clear_wiki: failed to delete page version history for kb=%s",
dataset_id,
)
deleted["file_commit_history"] = False
else:
deleted["file_commit_history"] = False
return True, {"deleted": deleted}

View File

@@ -801,6 +801,49 @@ class FileCommitService(CommonService):
return commit_id
@classmethod
@DB.connection_context()
def delete_page_history(cls, kb_id: str, page_type: str, slug: str) -> int:
"""Delete all stored versions for one Wiki page.
Wiki versions are represented by a ``FileCommit`` plus its single
``FileCommitItem``. They are not workspace commits, so removing the
page must remove both rows instead of leaving an orphaned history
that can reappear when the same slug is generated again.
"""
file_id = _wiki_file_id(kb_id, slug)
commit_ids = [
row.commit_id
for row in FileCommitItem.select(FileCommitItem.commit_id).where((FileCommitItem.file_id == file_id) & (FileCommitItem.slug_kwd == slug) & (FileCommitItem.page_type_kwd == page_type))
]
if not commit_ids:
return 0
with DB.atomic():
FileCommitItem.delete().where(FileCommitItem.commit_id.in_(commit_ids)).execute()
deleted = FileCommit.delete().where((FileCommit.folder_id == kb_id) & FileCommit.id.in_(commit_ids)).execute()
return deleted
@classmethod
@DB.connection_context()
def delete_all_page_history(cls, kb_id: str) -> int:
"""Delete all Wiki page versions belonging to a knowledge base."""
commit_ids = [
row.commit_id
for row in (
FileCommitItem.select(FileCommitItem.commit_id)
.join(FileCommit, on=(FileCommit.id == FileCommitItem.commit_id))
.where((FileCommit.folder_id == kb_id) & FileCommitItem.slug_kwd.is_null(False) & FileCommitItem.page_type_kwd.is_null(False))
)
]
if not commit_ids:
return 0
with DB.atomic():
FileCommitItem.delete().where(FileCommitItem.commit_id.in_(commit_ids)).execute()
deleted = FileCommit.delete().where((FileCommit.folder_id == kb_id) & FileCommit.id.in_(commit_ids)).execute()
return deleted
@classmethod
@DB.connection_context()
def list_page_commits(