Wiki Phase 4: full-text search + revision history

Final phase of the wiki upgrade (see WIKI_UPGRADE.md).

Schema (additive): wiki_revisions table (per-save content snapshots).
The FULLTEXT index on wiki_pages(title, body) shipped in Phase 1.

Search:
- MATCH ... AGAINST natural-language search over title + body, ordered by
  relevance
- public: GET /public/wiki?q= (published only); admin: GET /admin/wiki?q=
  (all statuses)
- public wiki index gains a search box; admin list gains a search field

Revision history:
- every create/update snapshots the page into wiki_revisions
- admin endpoints: list revisions, get one, and restore (restore overwrites
  the page, rebuilds links, and appends a new revision — history stays
  append-only); logged as wiki.revision.restore
- editor gains a History modal: revision list + word-level diff (jsdiff) of a
  chosen revision against the current page, with one-click restore

Verified end-to-end: search matches body and title; two edits produce three
revisions; diff renders added/removed words; restore reverts and records a new
revision. No console errors.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 15:49:05 -05:00
parent 7c081ae749
commit 7bb992f58d
15 changed files with 492 additions and 14 deletions

View File

@@ -34,6 +34,19 @@ async function rebuildLinks(pageId, html) {
}
}
// Snapshot the current content of a page into the revision history.
async function writeRevision(page, editorId, changeNote = null) {
await wikiDb.insertRevision({
pageId: page.id,
title: page.title,
body: page.body,
excerpt: page.excerpt,
categoryId: page.category_id,
editorId,
changeNote,
})
}
// ── Pages ──────────────────────────────────────────────────────────────
async function listPublished(filters = {}) {
return wikiDb.listPublishedSummaries(filters)
@@ -43,6 +56,10 @@ async function listAll(filters = {}) {
return wikiDb.listAllSummaries(filters)
}
async function search(q, opts = {}) {
return wikiDb.searchSummaries(q, opts)
}
// Admin detail: page + its tags.
async function getBySlug(slug) {
const page = await wikiDb.findBySlug(slug)
@@ -77,6 +94,7 @@ async function create({ slug, title, body, excerpt, categoryId, published, updat
const page = await wikiDb.findBySlug(slug)
if (Array.isArray(tags)) await syncTags(page.id, tags)
await rebuildLinks(page.id, clean)
await writeRevision(page, updatedBy, 'Created')
return getBySlug(slug)
}
@@ -102,6 +120,8 @@ async function update(slug, input) {
await wikiDb.updateBySlug(slug, fields)
if (Array.isArray(input.tags)) await syncTags(current.id, input.tags)
if (cleanForLinks != null) await rebuildLinks(current.id, cleanForLinks)
const page = await wikiDb.findBySlug(slug)
await writeRevision(page, input.updatedBy ?? null, input.changeNote || null)
return getBySlug(slug)
}
@@ -120,6 +140,42 @@ async function remove(slug) {
return res
}
// ── Revisions ──────────────────────────────────────────────────────────
async function listRevisions(slug) {
const page = await wikiDb.findBySlug(slug)
if (!page) return null
return wikiDb.listRevisions(page.id)
}
async function getRevision(slug, revId) {
const page = await wikiDb.findBySlug(slug)
if (!page) return null
const rev = await wikiDb.findRevision(revId)
if (!rev || rev.page_id !== page.id) return null
return rev
}
// Restore an old revision: overwrite the page with the snapshot, rebuild links,
// then record a new revision (history stays append-only).
async function restoreRevision(slug, revId, editorId) {
const page = await wikiDb.findBySlug(slug)
if (!page) return null
const rev = await wikiDb.findRevision(revId)
if (!rev || rev.page_id !== page.id) return null
await wikiDb.updateBySlug(slug, {
title: rev.title,
body: rev.body,
excerpt: rev.excerpt,
category_id: rev.category_id,
updated_by: editorId,
})
await rebuildLinks(page.id, rev.body)
const restored = await wikiDb.findBySlug(slug)
await writeRevision(restored, editorId, `Restored from revision #${revId}`)
return getBySlug(slug)
}
// ── Tags ───────────────────────────────────────────────────────────────
async function listTags() {
return wikiDb.listTags()
@@ -164,12 +220,16 @@ async function removeCategory(id) {
module.exports = {
listPublished,
listAll,
search,
getBySlug,
getPublishedBySlug,
create,
update,
setPublished,
remove,
listRevisions,
getRevision,
restoreRevision,
listTags,
getTagBySlug,
listCategories,