package index
import (
"database/sql"
"regexp"
"strings"
)
// Fossil's backlink table (src/backlink.c): a wiki page or a ticket
// comment that names a check-in is listed under References on that
// check-in's /info page.
//
// The rule is Fossil's, and it is looser than the render pass's. A target
// is a hex run of 4 to 64 characters, lowercased, written bare in a
// bracketed link or as /info/HASH; it is kept as written and matched as a
// PREFIX of a check-in's hash at read (Fossil: `target GLOB hash-prefix*
// AND hash GLOB target*`). Nothing demands the prefix be unique, so a
// short one is listed under every check-in it begins — Fossil does the
// same, and a quarry that listed fewer would disagree with the origin.
// Links of other shapes (/doc, /file) carry no such target in Fossil
// and make no backlink here either.
const backlinkMaxLen = 64 // Fossil's HNAME_MAX
var (
hexTargetRe = regexp.MustCompile(`^[0-9a-fA-F]+$`)
bracketRe = regexp.MustCompile(`\[([^\[\]|]*)(?:\|[^\]]*)?\]`)
mdLinkRe = regexp.MustCompile(`\]\(\s*<?([^)\s>]*)`)
fencedCodeRe = regexp.MustCompile("(?s)```.*?(?:```|\\z)|~~~.*?(?:~~~|\\z)")
codeSpanRe = regexp.MustCompile("`[^`\n]*`")
verbatimRe = regexp.MustCompile(`(?is)<verbatim[^>]*>.*?(?:</verbatim[^>]*>|\z)`)
)
// backlinkTarget is Fossil's backlink_create: the canonical hash prefix a
// link target names, or "" when it names none.
func backlinkTarget(t string) string {
t = strings.TrimRight(t, " ")
if len(t) >= 10 && strings.HasPrefix(t, "/info/") {
t = t[6:]
}
if len(t) < 4 || len(t) > backlinkMaxLen || !hexTargetRe.MatchString(t) {
return ""
}
return strings.ToLower(t)
}
// BacklinkTargets lists the hash prefixes a page or comment body names,
// read the way Fossil reads its mimetype: Fossil wiki (and an unmarked
// body) by [target] and [target|label]; Markdown by link targets and by
// the bracketed hash quarry's authors write; every other mimetype names
// nothing.
func BacklinkTargets(mimetype, body string) []string {
var out []string
seen := map[string]bool{}
add := func(raw string) {
if t := backlinkTarget(raw); t != "" && !seen[t] {
seen[t] = true
out = append(out, t)
}
}
switch {
case mimetype == "" || strings.Contains(mimetype, "wiki"):
body = verbatimRe.ReplaceAllString(body, "")
for _, m := range bracketRe.FindAllStringSubmatch(body, -1) {
add(m[1])
}
case strings.Contains(mimetype, "markdown"):
body = codeSpanRe.ReplaceAllString(fencedCodeRe.ReplaceAllString(body, ""), "")
for _, m := range mdLinkRe.FindAllStringSubmatch(body, -1) {
add(m[1])
}
for _, m := range bracketRe.FindAllStringSubmatchIndex(body, -1) {
if end := m[1]; end < len(body) && body[end] == '(' {
continue // a link: its target was read above
}
add(body[m[2]:m[3]])
}
}
return out
}
// execer is the write surface the fold and the backfill share.
type execer interface {
Exec(query string, args ...any) (sql.Result, error)
}
// foldBacklinks replaces what one artifact names. src is the page title
// or ticket id; edit is the artifact, so a redelivery overwrites itself.
func foldBacklinks(tx execer, srctype, src, edit, mimetype, body string) error {
if _, err := tx.Exec(`DELETE FROM backlink WHERE edit = ?`, edit); err != nil {
return err
}
for _, target := range BacklinkTargets(mimetype, body) {
if _, err := tx.Exec(`INSERT OR IGNORE INTO backlink (srctype, src, edit, target)
VALUES (?, ?, ?, ?)`, srctype, src, edit, target); err != nil {
return err
}
}
return nil
}
// backfillBacklinks reads every stored wiki edit and ticket change, once,
// for a store made before the table existed. Additive: nothing else moves.
func backfillBacklinks(db *sql.DB) error {
type row struct{ src, edit, mime, body string }
var wiki, tickets []row
rows, err := db.Query(`SELECT title, hash, mimetype, CAST(body AS TEXT) FROM wiki_edit WHERE live = 1`)
if err != nil {
return err
}
for rows.Next() {
var r row
if err := rows.Scan(&r.src, &r.edit, &r.mime, &r.body); err != nil {
rows.Close()
return err
}
wiki = append(wiki, r)
}
if err := rows.Close(); err != nil {
return err
}
rows, err = db.Query(`SELECT c.ticket, c.hash, COALESCE(max(m.value), ''), group_concat(f.value, char(10))
FROM ticket_change c JOIN ticket_field f ON f.hash = c.hash AND f.name IN ` + ticketTextFields + `
LEFT JOIN ticket_field m ON m.hash = c.hash AND m.name = 'mimetype'
GROUP BY c.hash`)
if err != nil {
return err
}
for rows.Next() {
var r row
if err := rows.Scan(&r.src, &r.edit, &r.mime, &r.body); err != nil {
rows.Close()
return err
}
tickets = append(tickets, r)
}
if err := rows.Close(); err != nil {
return err
}
for _, r := range wiki {
if err := foldBacklinks(db, "wiki", r.src, r.edit, r.mime, r.body); err != nil {
return err
}
}
for _, r := range tickets {
if err := foldBacklinks(db, "ticket", r.src, r.edit, r.mime, r.body); err != nil {
return err
}
}
return nil
}
// ticketTextFields are the fields a ticket change carries prose in.
const ticketTextFields = `('comment', 'icomment')`
// Backref is one page or ticket that names a check-in.
type Backref struct {
Kind string `json:"kind"` // wiki | ticket
Ref string `json:"ref"` // page title, or ticket id
Title string `json:"title"`
Updated string `json:"updated"`
}
// ReferencedBy lists what names a check-in, newest first: the wiki pages
// whose CURRENT version does, and the tickets that ever did. A private
// ticket is never named.
func (ix *Index) ReferencedBy(hash string) ([]Backref, error) {
var out []Backref
rows, err := ix.db.Query(`SELECT w.title, w.date FROM backlink b
JOIN wiki_edit w ON w.hash = b.edit
WHERE b.srctype = 'wiki' AND w.live = 1 AND ? LIKE b.target || '%'
AND w.date || ' ' || w.hash = (SELECT max(w2.date || ' ' || w2.hash) FROM wiki_edit w2 WHERE w2.title = w.title)
GROUP BY w.title ORDER BY w.date DESC, w.title`, hash)
if err != nil {
return nil, err
}
for rows.Next() {
b := Backref{Kind: "wiki"}
if err := rows.Scan(&b.Ref, &b.Updated); err != nil {
rows.Close()
return nil, err
}
b.Title = b.Ref
out = append(out, b)
}
if err := rows.Close(); err != nil {
return nil, err
}
rows, err = ix.db.Query(`SELECT t.id, t.title, t.updated FROM ticket t
WHERE t.private = 0 AND t.id IN (SELECT b.src FROM backlink b
WHERE b.srctype = 'ticket' AND ? LIKE b.target || '%')
ORDER BY t.updated DESC, t.id`, hash)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
b := Backref{Kind: "ticket"}
if err := rows.Scan(&b.Ref, &b.Title, &b.Updated); err != nil {
return nil, err
}
out = append(out, b)
}
return out, rows.Err()
}