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:
@@ -82,6 +82,22 @@ CREATE TABLE IF NOT EXISTS wiki_links (
|
||||
INDEX idx_wiki_links_target (target_slug)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- Per-save content snapshots for history / diff / restore.
|
||||
CREATE TABLE IF NOT EXISTS wiki_revisions (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
page_id INT NOT NULL,
|
||||
title VARCHAR(200) NOT NULL,
|
||||
body MEDIUMTEXT NULL,
|
||||
excerpt VARCHAR(400) NULL,
|
||||
category_id INT NULL,
|
||||
editor_id INT NULL,
|
||||
change_note VARCHAR(280) NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT fk_wiki_rev_page FOREIGN KEY (page_id) REFERENCES wiki_pages(id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_wiki_rev_editor FOREIGN KEY (editor_id) REFERENCES users(id) ON DELETE SET NULL,
|
||||
INDEX idx_wiki_rev_page (page_id, id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS settings (
|
||||
`key` VARCHAR(64) PRIMARY KEY,
|
||||
value TEXT NULL,
|
||||
|
||||
@@ -63,6 +63,17 @@ async function findPublishedBySlug(slug) {
|
||||
return rows[0] || null
|
||||
}
|
||||
|
||||
// Full-text search over title + body, ordered by relevance.
|
||||
async function searchSummaries(q, { publishedOnly = true } = {}) {
|
||||
const pub = publishedOnly ? 'AND p.published = 1' : ''
|
||||
return query(
|
||||
`SELECT ${SUMMARY_COLS} ${FROM}
|
||||
WHERE MATCH(p.title, p.body) AGAINST (? IN NATURAL LANGUAGE MODE) ${pub}
|
||||
ORDER BY MATCH(p.title, p.body) AGAINST (?) DESC, p.title ASC`,
|
||||
[q, q],
|
||||
)
|
||||
}
|
||||
|
||||
// ── Page writes ────────────────────────────────────────────────────────
|
||||
async function insert({
|
||||
slug,
|
||||
@@ -233,6 +244,29 @@ async function getExistingSlugs(slugs) {
|
||||
return new Set(rows.map((r) => r.slug))
|
||||
}
|
||||
|
||||
// ── Revisions ──────────────────────────────────────────────────────────
|
||||
async function insertRevision({ pageId, title, body, excerpt, categoryId, editorId, changeNote }) {
|
||||
return query(
|
||||
'INSERT INTO wiki_revisions (page_id, title, body, excerpt, category_id, editor_id, change_note) ' +
|
||||
'VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
[pageId, title, body || null, excerpt || null, categoryId ?? null, editorId ?? null, changeNote || null],
|
||||
)
|
||||
}
|
||||
|
||||
async function listRevisions(pageId) {
|
||||
return query(
|
||||
`SELECT r.id, r.change_note, r.created_at, r.editor_id, u.username AS editor
|
||||
FROM wiki_revisions r LEFT JOIN users u ON u.id = r.editor_id
|
||||
WHERE r.page_id = ? ORDER BY r.id DESC`,
|
||||
[pageId],
|
||||
)
|
||||
}
|
||||
|
||||
async function findRevision(id) {
|
||||
const rows = await query('SELECT * FROM wiki_revisions WHERE id = ? LIMIT 1', [id])
|
||||
return rows[0] || null
|
||||
}
|
||||
|
||||
// ── Seeding (idempotent) ───────────────────────────────────────────────
|
||||
async function seedDefault(slug, title, body) {
|
||||
await query('INSERT IGNORE INTO wiki_pages (slug, title, body) VALUES (?, ?, ?)', [
|
||||
@@ -264,6 +298,7 @@ module.exports = {
|
||||
listAllSummaries,
|
||||
findBySlug,
|
||||
findPublishedBySlug,
|
||||
searchSummaries,
|
||||
insert,
|
||||
updateBySlug,
|
||||
deleteBySlug,
|
||||
@@ -283,6 +318,9 @@ module.exports = {
|
||||
insertLink,
|
||||
getBacklinks,
|
||||
getExistingSlugs,
|
||||
insertRevision,
|
||||
listRevisions,
|
||||
findRevision,
|
||||
seedDefault,
|
||||
seedDefaultCategory,
|
||||
assignCategoryBySlug,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -172,6 +172,9 @@ async function uploadFile(req, res) {
|
||||
// ── Wiki pages ─────────────────────────────────────────────────────────
|
||||
async function listWiki(req, res) {
|
||||
try {
|
||||
const q = (req.query.q || '').trim()
|
||||
if (q) return res.json(await wiki.search(q, { publishedOnly: false }))
|
||||
|
||||
const filters = {}
|
||||
if (req.query.category) {
|
||||
const category = await wiki.getCategoryBySlug(req.query.category)
|
||||
@@ -246,6 +249,7 @@ async function updateWiki(req, res) {
|
||||
if ('body' in req.body) input.body = req.body.body || null
|
||||
if ('excerpt' in req.body) input.excerpt = req.body.excerpt || null
|
||||
if ('published' in req.body) input.published = Boolean(req.body.published)
|
||||
if ('change_note' in req.body) input.changeNote = req.body.change_note
|
||||
if ('tags' in req.body) input.tags = Array.isArray(req.body.tags) ? req.body.tags : []
|
||||
if ('category_id' in req.body) {
|
||||
const cat = await resolveCategoryId(req.body)
|
||||
@@ -288,6 +292,45 @@ async function deleteWiki(req, res) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Wiki revisions ─────────────────────────────────────────────────────
|
||||
async function listWikiRevisions(req, res) {
|
||||
try {
|
||||
const revisions = await wiki.listRevisions(req.params.slug)
|
||||
if (revisions == null) return res.status(404).json({ message: 'Not found' })
|
||||
return res.json(revisions)
|
||||
} catch (err) {
|
||||
log.error('listWikiRevisions', err)
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
}
|
||||
|
||||
async function getWikiRevision(req, res) {
|
||||
try {
|
||||
const rev = await wiki.getRevision(req.params.slug, Number(req.params.id))
|
||||
if (!rev) return res.status(404).json({ message: 'Not found' })
|
||||
return res.json(rev)
|
||||
} catch (err) {
|
||||
log.error('getWikiRevision', err)
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
}
|
||||
|
||||
async function restoreWikiRevision(req, res) {
|
||||
try {
|
||||
const page = await wiki.restoreRevision(req.params.slug, Number(req.params.id), req.user.id)
|
||||
if (!page) return res.status(404).json({ message: 'Not found' })
|
||||
await activity.log({
|
||||
req,
|
||||
action: 'wiki.revision.restore',
|
||||
detail: { slug: req.params.slug, revision: Number(req.params.id) },
|
||||
})
|
||||
return res.json(page)
|
||||
} catch (err) {
|
||||
log.error('restoreWikiRevision', err)
|
||||
return res.status(500).json({ message: 'Internal Server Error' })
|
||||
}
|
||||
}
|
||||
|
||||
// ── Wiki tags ──────────────────────────────────────────────────────────
|
||||
async function listWikiTags(req, res) {
|
||||
try {
|
||||
@@ -488,6 +531,9 @@ module.exports = {
|
||||
updateWiki,
|
||||
publishWiki,
|
||||
deleteWiki,
|
||||
listWikiRevisions,
|
||||
getWikiRevision,
|
||||
restoreWikiRevision,
|
||||
listWikiTags,
|
||||
listWikiCategories,
|
||||
createWikiCategory,
|
||||
|
||||
@@ -114,6 +114,7 @@ adminRouter.put(
|
||||
body('category_id').optional({ values: 'null' }).isInt(),
|
||||
body('published').optional().isBoolean(),
|
||||
body('tags').optional().isArray(),
|
||||
body('change_note').optional({ values: 'falsy' }).isString().isLength({ max: 280 }),
|
||||
validate,
|
||||
ctrl.updateWiki,
|
||||
)
|
||||
@@ -123,6 +124,14 @@ adminRouter.patch(
|
||||
validate,
|
||||
ctrl.publishWiki,
|
||||
)
|
||||
adminRouter.get('/wiki/:slug/revisions', ctrl.listWikiRevisions)
|
||||
adminRouter.get('/wiki/:slug/revisions/:id', param('id').isInt(), validate, ctrl.getWikiRevision)
|
||||
adminRouter.post(
|
||||
'/wiki/:slug/revisions/:id/restore',
|
||||
param('id').isInt(),
|
||||
validate,
|
||||
ctrl.restoreWikiRevision,
|
||||
)
|
||||
adminRouter.delete('/wiki/:slug', ctrl.deleteWiki)
|
||||
|
||||
// ── Settings ──────────────────────────────────────────────────────────
|
||||
|
||||
@@ -68,6 +68,10 @@ async function getWikiTags(req, res) {
|
||||
|
||||
async function getWikiList(req, res) {
|
||||
try {
|
||||
// Full-text search takes precedence over category/tag filters.
|
||||
const q = (req.query.q || '').trim()
|
||||
if (q) return res.json(await wiki.search(q, { publishedOnly: true }))
|
||||
|
||||
const filters = {}
|
||||
if (req.query.category) {
|
||||
const category = await wiki.getCategoryBySlug(req.query.category)
|
||||
|
||||
Reference in New Issue
Block a user