Core operations for editable wiki databases (WIKIDB convention v3).
Every function here works on a plain context table and performs no
terminal I/O, so the whole module is testable against a temp database.
See "Editable wikis" in docs/WIKIDB.md for the convention contract.
A context is bound to one collection. Entry operations touch that collection's content and meta keyspaces; media, events and sequences are database-wide.
context builds one. A context assembled by hand needs the same fields:
| Field | Holds |
|---|---|
db | the open MNEME database |
manifest | the table read_manifest returns |
collection | the descriptor collection_of returns |
scope | the collection's name, which keys its events |
ft | the collection's full-text content keyspace |
meta_ks | the collection's meta keyspace, when it has one |
media_ks, events_ks | the database-wide keyspaces the manifest names |
schema | the schema find_schema prepares, when the collection has one |
author | optional: who an event names when the caller names nobody |
A context without scope is taken as the primary collection's.
| Module | Description |
|---|---|
| wikidb.tracker | The tracker profile of the WIKIDB convention: a project's issues and internal documents in one .tracker.mneme file at the repo root. |
| Name | Signature |
|---|---|
| slug_ok | slug_ok(name) -> ok |
| key_ok | key_ok(key) -> ok |
| sanitize_name | sanitize_name(name) -> slug |
| alpha_score | alpha_score(title) -> score |
| title_of | title_of(content) -> title |
| parse_link | parse_link(target) -> collection, key |
| links_of | links_of(content) -> links |
| read_manifest | read_manifest(wiki_ks) -> manifest |
| editable | editable(manifest) -> ok |
| collection_of | collection_of(manifest, name) -> collection |
| collections_of | collections_of(manifest) -> collections |
| find_schema | find_schema(manifest, name) -> schema |
| context | context(db, manifest, collection_name) -> ctx, err |
| create_wiki | create_wiki(path, opts) -> ok, err |
| set_manifest_field | set_manifest_field(ctx, field, value) -> ok, err |
| next_seq | next_seq(ctx, name) -> n, err |
| validate_fields | validate_fields(schema, fields) -> coerced, err |
| append_event | append_event(ctx, key, event) -> record, err |
| events | events(ctx, key) -> events |
| get_meta | get_meta(ctx, key) -> meta |
| save_entry | save_entry(ctx, key, content, opts) -> ok, err |
| set_fields | set_fields(ctx, key, fields, opts) -> ok, changes_or_err |
| delete_entry | delete_entry(ctx, key) -> ok, dropped_or_err |
| backlinks | backlinks(ctx, key) -> keys |
| rename_entry | rename_entry(ctx, old, new) -> stale, err |
| attach_media | attach_media(ctx, name, blob, mime) -> ok, err |
| media_info | media_info(ctx, name) -> info, err |
| get_media | get_media(ctx, name) -> blob, info_or_err |
| remove_media | remove_media(ctx, name) -> ok, err |
| list_media | list_media(ctx) -> items |
| facet_values | facet_values(ctx, field_name) -> values |
| browse_facet | browse_facet(ctx, field_name, value, opts) -> records, total |
| reindex | reindex(ctx) -> count, err |
slug_ok(
name) ->ok
Check a database name
A database name is a single slug: letters, digits, ., _ and -, at
most 200 bytes. It becomes a file name under the wiki directory, so a
separator in it would escape that directory.
key_ok(
key) ->ok
Check an entry key or a media name
A key is slugs joined by /. Content can be laid out like a file tree,
and a key stays unambiguous in wikilinks, hrefs and completion. Empty,
. and .. segments are refused, so every key is canonical and none is
path-like.
sanitize_name(
name) ->slug
Fold a file name into a valid media name
Every run of characters outside the slug class collapses to one
underscore. A / collapses too, so the result is a single segment.
Returns nil when nothing usable is left.
alpha_score(
title) ->score
Score a title for the by_title index
The score packs the first 6 bytes of the lowercased title, and stays exact in a float64. MNEME sorted sets break score ties by member name, so the index order is exactly alphabetical.
title_of(
content) ->title
Find the first ATX heading in a document
This reads lines, not the parse tree. A # line inside a fenced code
block counts when it comes before the real heading.
parse_link(
target) ->collection,key
Split a wiki link target into collection and key
: cannot occur in an entry key, so a qualified target is unambiguous.
An unqualified target answers a nil collection: it resolves in the
collection it was written in. A target that is not a string answers two
nils.
links_of(
content) ->links
Collect the wiki link targets of a document
Each target appears once, in order of first appearance. The markdown parser does the reading, so links inside code blocks and code spans are ignored. A qualified target keeps its collection prefix.
read_manifest(
wiki_ks) ->manifest
Read the manifest from a __wiki keyspace
The JSON fields come back decoded: indexes, entry_point, tss,
collections and schemas. An absent field is nil. A malformed JSON
field is nil too, and raises no error.
editable(
manifest) ->ok
Tell whether a manifest describes an editable wiki
Editable means convention v2 or later, with a meta keyspace. The editor maintains the standard indexes and the managed meta fields, and both need the two.
collection_of(
manifest,name) ->collection
Resolve a collection descriptor by name
nil selects the primary collection. An additional collection answers to
its name and to its content keyspace. A qualified link never has to know
whether its target is primary. The descriptor is a copy, and it
takes the manifest's content_format when it names none of its own.
collections_of(
manifest) ->collections
List every collection in the database, primary first
find_schema(
manifest,name) ->schema
Find a schema by name and prepare it for validation
The prepared schema keeps fields in declared order, for forms, and adds
by_name for lookups. A nil name, an unknown one and a schema with no
fields list all answer nil.
context(
db,manifest,collection_name) ->ctx,err
Build an operation context bound to one collection
nil selects the primary collection, so a v2 caller is unaffected. The
context carries no author: set one on it before a write that logs
events.
create_wiki(
path,opts) ->ok,err
Bootstrap a new wiki database
A path that exists is refused. opts sets the manifest: title,
description, the keyspace names, schema, schemas, collections,
indexes and entry_point. That is how a profile such as the tracker
declares its layout.
set_manifest_field(
ctx,field,value) ->ok,err
Write a scalar manifest field
The context's manifest changes only after the store takes the write. A
JSON field such as indexes does not belong here: the in-memory manifest
holds it decoded, and this stores the value as given.
next_seq(
ctx,name) ->n,err
Allocate the next number in a named sequence
A sequence is database-wide and starts at 1. No number is handed out twice.
validate_fields(
schema,fields) ->coerced,err
Check field values against a schema
Unknown and managed field names are refused, so a typo never becomes a silent new field. Without a schema, every field is refused.
An empty string removes a field, and so does an empty table for a list.
A required field takes its default instead, and refuses the removal when
it has none. In coerced, a removal reads as "", and a number or epoch
field holds a number.
append_event(
ctx,key,event) ->record,err
Append one record to an entry's history
The stored record is a copy of event. t defaults to now and who to
ctx.author. The stored record is what comes back. The entry need not
exist. A wiki without an events keyspace refuses the call.
events(
ctx,key) ->events
Read an entry's history in chronological order
Each record carries the seq its key encodes. Timestamps have one-second
resolution, so seq is the only thing that orders events within a
second. A wiki without an events keyspace has no history.
get_meta(
ctx,key) ->meta
Read an entry's meta record
An absent or malformed record reads as an empty table, so a caller never checks for nil. The table is fresh on every call, and changing it writes nothing. The context needs a meta keyspace.
save_entry(
ctx,key,content,opts) ->ok,err
Store an entry and refresh its meta and indexes
The managed fields title, created, updated and links come from
the content. Unknown meta fields survive. opts.fields sets schema
fields in the same call, and validate_fields checks them before
anything is written. opts.author names who the events carry.
A new entry takes its schema defaults and logs one created event, which
holds the fields it was filed with. A save over an existing entry logs a
field event per field that moved. A change to the body logs no event.
set_fields(
ctx,key,fields,opts) ->ok,changes_or_err
Change schema fields without rewriting the document
On success the second value lists one { field, from, to } record per
field that moved. A call that moves nothing writes nothing, not even
updated, and answers true, {}. opts.author names who the events
carry.
The meta record is written first and the events last. A refused event leaves the field changed with no event behind it.
delete_entry(
ctx,key) ->ok,dropped_or_err
Remove an entry with its meta, index entries and history
On success the second value counts the events dropped with the entry. The event sequence goes too, so a later entry under the same key numbers its history from 1. Links to the key in other entries stay as written.
backlinks(
ctx,key) ->keys
List the entries that link to a key
Every collection is searched. A hit in the context's own collection comes back bare. A hit from another one keeps its collection prefix, so the result reads as the link was written. The search reads every meta record in the database.
rename_entry(
ctx,old,new) ->stale,err
Move an entry to a new key
Meta and index scores move with the entry. Links are not rewritten: on success the count says how many entries still link to the old key.
The history does not move. Its events stay under the old key, and the new key starts with none.
The entry is written under the new key before the old one is removed. A rename that stops part way leaves the entry under both keys.
attach_media(
ctx,name,blob,mime) ->ok,err
Store a media blob under a name
The name follows the entry key rules, so pass a file name through
sanitize_name first. mime defaults to application/octet-stream. A
name that exists is overwritten.
media_info(
ctx,name) ->info,err
Read a media item's metadata
info carries name, mime, size and added.
get_media(
ctx,name) ->blob,info_or_err
Read a media blob and its metadata
A blob whose metadata record is missing comes back with a nil info.
remove_media(
ctx,name) ->ok,err
Remove a media blob and its metadata
list_media(
ctx) ->items
List every media item, sorted by name
Each item is the table media_info returns. A wiki without a media
keyspace lists nothing.
facet_values(
ctx,field_name) ->values
List a facet field's values with their entry counts
Each item is { value, count }, most used first, with ties broken by
value. The list comes from the facet dictionary, which is the only
enumeration of an open-ended field's values.
browse_facet(
ctx,field_name,value,opts) ->records,total
List the entries carrying one value of a facet field
The list runs newest first, and opts.order = "asc" turns it round.
opts.offset and opts.limit (default 25) page through the set, and
total counts the whole set. Each record has the { member, score }
shape of a sorted-set range: the entry key and its updated time.
A context without a meta keyspace answers {} and no total.
reindex(
ctx) ->count,err
Rebuild managed meta fields and every index from stored content
This is the repair for an interrupted write: entry operations span keyspaces and share no transaction. Entries that lack a schema default take it, which is how entries that predate a field gain it. History is left alone. The context needs a meta keyspace.