1# Annotations Plugin — Design & Architecture 2 3A developer reference for the annotations plugin. For installation and end-user 4behaviour see [README.md](README.md); for the wider review/environment 5conventions see `CLAUDE.md` in the plugins root. 6 7## Concept 8 9Word- and sentence-level annotations on wiki pages, in the spirit of 10Hypothes.is and `ep_comments_page`: 11 12- **Out-of-band.** Annotations live in a separate per-page JSON file, never in 13 the page text or the wiki changelog. Creating one needs only `AUTH_READ`, so 14 a group whose page *edit* access is blocked can still annotate. 15- **Text-quote anchored.** Each annotation is tied to the quoted text plus a 16 little surrounding context, not to a character position, so it survives minor 17 edits and is re-found in the rendered DOM on each page load. 18- **Threaded.** Annotations carry replies; both have open/resolved status at the 19 annotation level. 20- **Orphan-aware.** When the quoted text disappears from the page the annotation 21 becomes an *orphan* — still stored, surfaced through a counter, and bulk- 22 removable by an admin. 23 24## Components 25 26| File | Owns | 27|------|------| 28| `helper.php` | The per-page store, all CRUD, server-side orphan detection, and the **permission rules as the single source of truth**. Pure logic — permission methods take facts (user, admin flag, ACL level) as parameters and read no globals. | 29| `action.php` | Event registration; injecting the page payload into `JSINFO`; the AJAX endpoint and **permission enforcement** (gathers facts from DokuWiki globals, calls the helper). | 30| `script.js` | All front-end behaviour: boot/gate, load + re-anchor, highlights, gutter markers, counter, selection→new-annotation flow, thread panels, and AJAX. Plain IIFE, vanilla JS. | 31| `style.css` | Styling via DokuWiki theme tokens (`__background__`, `__text__`, …). Only the amber (open) / green (resolved) highlight colours are hard-coded. | 32| `lang/en/lang.php` | The usersettings toggle label/description (used) plus a set of UI strings that are **not yet wired into the JS** — see *Known gaps*. | 33 34## Data model & storage 35 36One pretty-printed JSON file per page at `metaFN($id, '.annotations')` 37(`data/meta/<namespace>/<page>.annotations`): 38 39```json 40{ 41 "version": 1, 42 "annotations": [ 43 { 44 "id": "a1b2c3d4e5f6g7h8", 45 "anchor": { "exact": "...", "prefix": "...", "suffix": "...", "start": 123 }, 46 "author": "alice", 47 "created": 1716336000, 48 "modified": 1716336000, 49 "body": "Does this cover remuxes?", 50 "status": "open", 51 "resolved_by": "", 52 "resolved_at": 0, 53 "replies": [ 54 { 55 "id": "x1y2z3a4b5c6d7e8", 56 "author": "bob", 57 "created": 1716336100, 58 "modified": 1716336100, 59 "body": "Yes, remuxes count." 60 } 61 ] 62 } 63 ] 64} 65``` 66 67Limits and identifiers (`helper.php` constants): `SCHEMA_VERSION = 1`, 68`MAX_QUOTE = 1000`, `MAX_CONTEXT = 64`, `MAX_BODY = 10000`. IDs are 69`bin2hex(random_bytes(8))` — 16 hex chars. Writes go through `io_lock()` → 70modify → `io_saveFile()` → `io_unlock()` (the `mutate()` helper); a modifier 71returning `false` aborts the write (used for "target not found"). 72 73## Text-quote anchoring 74 75An anchor is `{exact, prefix, suffix, start}`: 76 77- `exact` — the selected text, whitespace-normalised (runs collapsed to one 78 space, trimmed). The same normalisation is applied on capture (JS), on 79 storage (PHP), and on matching, so client and server agree. 80- `prefix` / `suffix` — context on each side to disambiguate a quote that 81 appears more than once. Client captures ~30 chars; server caps at 64. 82- `start` — a character-offset hint into the page text, used only as a 83 tie-breaker. 84 85**Re-anchoring (client, `findRange`)**: collect the content text with a 86`TreeWalker`, search for the normalised `exact`, disambiguate repeats with 87`prefix`/`suffix`, tie-break with the `start` hint, then map the chosen 88character offset back to a DOM `Range` and wrap it in a highlight `<span>`. A 89quote that cannot be located is an orphan (no highlight, no gutter marker). 90 91## Orphan detection (two layers) 92 93- **Client (live UI).** Anything `findRange` cannot anchor on page load is 94 counted as orphaned; the count feeds the counter bar, and the orphaned link 95 opens a drawer at the bottom of the content area with those threads. 96- **Server (authoritative, `findOrphaned`).** For the admin "clear orphaned" 97 action the page is rendered with `p_wiki_xhtml`, block-closing tags are turned 98 into spaces, tags/entities are stripped, whitespace normalised, and each 99 annotation's `exact` is searched with `mb_strpos`. This re-check is the source 100 of truth for deletion, so a stale client can't cause data loss. 101 102## JSINFO injection (important gotcha) 103 104`script.js` needs per-page facts at boot without an extra round-trip, but you 105**cannot** add them by writing `$JSINFO` inside `TPL_METAHEADER_OUTPUT`: 106`tpl_metaheaders()` calls `jsinfo()` and serialises `$JSINFO` into the inline 107`var JSINFO = …;` script **before** firing that event. Instead `handleMetaHeader` 108finds that inline `<script>` in `$event->data['script']` and appends a 109`JSINFO.annotations = {…};` statement so it runs in the same scope. Injection is 110gated to `show` / `export_xhtml` views. 111 112Payload: `{ enabled, pageId, stats, user, isAdmin, token }`. `user`, `isAdmin` 113and `token` are included because stock `JSINFO` exposes no user identity and no 114security token — the script reads them from `JSINFO.annotations`, not from 115`JSINFO.userinfo` (which does not exist) or the `#dw__token` field. 116 117## Per-user toggle 118 119Registered with the **usersettings** plugin via `PLUGIN_USERSETTINGS_REGISTER` 120(key `annotations_enabled`, checkbox, default on). `isEnabledForUser()` reads the 121preference through the usersettings helper; if that plugin is absent, or the 122toggle has not been registered yet, the feature defaults to **on**. When a user 123turns it off, `boot()` returns early and nothing is rendered (annotations are 124still stored). 125 126## Permission model 127 128The rules live in `helper.php` and are pure; `action.php` gathers the facts and 129calls them. `isAdmin` is true for the `admin` group or DokuWiki's `$INFO['isadmin']`. 130 131| Action | Rule (helper method) | 132|--------|----------------------| 133| Create annotation / reply / resolve / reopen | logged in **and** `AUTH_READ` on the page — *not* `AUTH_EDIT` (`canAnnotate`) | 134| Edit / delete own annotation | author (`canEditAnnotation`) | 135| Edit / delete own reply | author (`canEditReply`) | 136| Edit / delete **any** annotation or reply | admin (`canEditAnnotation` / `canEditReply`) | 137| Clear resolved / clear orphaned (per page) | admin (`canClear`) | 138| Load (read) annotations | `AUTH_READ` on the page | 139 140## Security 141 142- **CSRF.** Every state-changing action requires a valid DokuWiki security 143 token. The token is injected into `JSINFO.annotations.token` and sent back as 144 `sectok` in the JSON body. Because `checkSecurityToken()` reads `$_REQUEST` 145 (empty for a JSON body), `handleAjax` copies `sectok` into `$_POST`/`$_REQUEST` 146 before validating. The read-only `load` action is exempt (GET, no token) but 147 still ACL-checked. 148- **ACL.** `auth_quickaclcheck($id)` gates both reading and writing. 149- **Output.** Bodies are stored as plain text (newlines kept, length-capped) and 150 rendered client-side via `textContent`, so user content is never interpolated 151 as HTML. 152 153## AJAX endpoint 154 155`…/lib/exe/ajax.php?call=annotations` (handled on `AJAX_CALL_UNKNOWN`). The 156`load` action is a GET with query params; everything else is `POST` with an 157`application/json` body. Every response is `{ "success": true, … }` or 158`{ "success": false, "error": "…" }`. 159 160| Action | Method | Token | Extra fields | 161|--------|--------|-------|--------------| 162| `load` | GET | — | — | 163| `create` | POST | ✓ | `anchor`, `body` | 164| `reply` | POST | ✓ | `annId`, `body` | 165| `edit_annotation` | POST | ✓ | `annId`, `body` | 166| `edit_reply` | POST | ✓ | `annId`, `replyId`, `body` | 167| `delete_annotation` | POST | ✓ | `annId` | 168| `delete_reply` | POST | ✓ | `annId`, `replyId` | 169| `resolve` | POST | ✓ | `annId`, `status` (`open`\|`resolved`) | 170| `clear_resolved` | POST | ✓ | — | 171| `clear_orphaned` | POST | ✓ | — | 172 173All actions also take the page `id`. 174 175## Constraints 176 177- **JS/CSS floor: Firefox 78 ESR.** No `#private` fields, `??=`/`||=`/`&&=`, 178 `Array.at`, `structuredClone`, `Object.hasOwn`, native `<dialog>`; no CSS 179 `:has()`, selector `:not()`, `aspect-ratio`, container queries, or nesting. 180 `async`/`await`, `fetch`, classes, `?.`, `??`, `Map`/`Set` are fine. 181- **PHP:** developed against 8.3; requires the `mbstring` extension. 182 183## Known gaps / next steps 184 185- **UI localisation.** `script.js` renders hardcoded English; `annInfo.lang` is 186 never populated, so the `counter_*`, `btn_*`, `status_*`, `placeholder_*`, 187 `tooltip_*`, `orphaned_*`, `error_*` and `confirm_*` strings in 188 `lang/en/lang.php` are currently dead. To localise: inject `lang` into the 189 `JSINFO.annotations` payload in `handleMetaHeader` and read `_lang` in the JS 190 string sites. Only `toggle_label` / `toggle_desc` are wired (via `getLang`). 191- **Translations.** No `de` / `ru` / `ja` yet (depends on the localisation work 192 above). 193- **Tests.** No `_test/` suite. Candidates: helper CRUD, input cleaning, 194 permission rules, and `findOrphaned` against a rendered page. 195- **Config.** No `conf/` — nothing is configurable (highlight colours, context 196 length, body cap are all constants/CSS). 197- **Cleanup.** The `ann-highlight-orphaned` JS constant has no CSS rule and no 198 call site (orphans have no in-page range to highlight). 199