oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "sort"
6
7 "amadan.net/oficina/mapa/internal/mapa/markup"
8)
9
10// Unlinked mentions (§6.4): a team page's title or alias in a page's text
11// without a link to it shows on the team page as "Mentioned without a
12// link", with "Link it". Computed in memory, never stored, scoped.
13
14// phrasesOf is a team page's title and aliases, as folded words.
15func phrasesOf(title string, aliases []AliasView) [][]string {
16 var out [][]string
17 for _, text := range append([]string{title}, aliasTitles(aliases)...) {
18 if phrase := folds(text); len(phrase) > 0 {
19 out = append(out, phrase)
20 }
21 }
22 return out
23}
24
25func aliasTitles(aliases []AliasView) []string {
26 out := make([]string, len(aliases))
27 for number, alias := range aliases {
28 out[number] = alias.Title
29 }
30 return out
31}
32
33// mentions reads the team partition and the scope's own, never a whole
34// to filter, and skips pages that already link here.
35func (index *Index) mentions(scope Scope, page *entry,
36 linked []Linked) []Linked {
37 phrases := phrasesOf(page.record.Title, page.aliases)
38 skip := map[int64]bool{page.id: true}
39 for _, source := range linked {
40 skip[source.ID] = true
41 }
42 candidates := map[int64]string{}
43 for _, pageID := range index.mentionCandidates(scope, phrases) {
44 if !skip[pageID] {
45 candidates[pageID] = ""
46 }
47 }
48 // In display order, stopping once the list is full: the phrase
49 // check is the costly part, and a common title has thousands of
50 // candidates (review L5).
51 images := index.imageLookup(context.Background())
52 var out []Linked
53 for _, source := range index.ordered(candidates) {
54 numbers := source.mentioning(phrases)
55 if numbers == nil {
56 continue
57 }
58 linked := Linked{ID: source.id, Public: publicID(source.nonce),
59 Label: source.label(scope.Locale), Href: source.href(),
60 Kind: source.kind, Look: source.look()}
61 draw := resolver{index: index, scope: scope, page: source.id,
62 images: images}
63 for _, number := range numbers {
64 linked.Contexts = append(linked.Contexts, Context{Block: number,
65 HTML: markup.Render(source.blocks[number:number+1], draw)})
66 }
67 if out = append(out, linked); len(out) == maxLinked {
68 break
69 }
70 }
71 return out
72}
73
74// mentionCandidates is the pages in scope whose words hold every word
75// of some phrase, from the team postings and the scope's own: the
76// phrase check then runs on those alone.
77func (index *Index) mentionCandidates(scope Scope,
78 phrases [][]string) []int64 {
79 seen := map[int64]bool{}
80 var out []int64
81 for _, part := range index.searchPartitions(scope) {
82 for _, phrase := range phrases {
83 for _, pageID := range part.containing(phrase) {
84 if !seen[pageID] {
85 seen[pageID] = true
86 out = append(out, pageID)
87 }
88 }
89 }
90 }
91 return out
92}
93
94// mentioning is the blocks whose unlinked text holds any phrase.
95func (page *entry) mentioning(phrases [][]string) []int {
96 var out []int
97 for number, words := range page.unlinked {
98 for _, phrase := range phrases {
99 if start, _ := phraseAt(words, phrase); start >= 0 {
100 out = append(out, number)
101 break
102 }
103 }
104 }
105 return out
106}
107
108// AliasLinkers is the pages in scope that link to a team page through
109// one of its aliases: "Update links"' candidates (§3.4), already scoped.
110func (index *Index) AliasLinkers(ctx context.Context, scope Scope,
111 pageID int64) ([]int64, error) {
112 if err := index.Sync(ctx); err != nil {
113 return nil, err
114 }
115 index.mutex.RLock()
116 defer index.mutex.RUnlock()
117 page := index.pages[pageID]
118 if page == nil || !page.team() {
119 return nil, ErrNotFound
120 }
121 seen := map[int64]bool{}
122 for _, alias := range page.aliases {
123 if found := index.linkers[Fold(alias.Title)]; found != nil {
124 for source := range found.inScope(scope) {
125 seen[source] = true
126 }
127 }
128 }
129 out := make([]int64, 0, len(seen))
130 for source := range seen {
131 out = append(out, source)
132 }
133 sort.Slice(out, func(left, right int) bool {
134 return out[left] < out[right]
135 })
136 return out, nil
137}
138
139// LinkMention is "Link it": the first unlinked mention of a team page in
140// one block of a page becomes a link, as an edit by the person.
141func (store *Store) LinkMention(ctx context.Context, who string, sourceID,
142 targetID int64, number int) error {
143 return quiet(store.write(ctx, func(tx *Tx) error {
144 actor, err := tx.member(who)
145 if err != nil {
146 return err
147 }
148 _, target, err := tx.teamPage(actor, targetID)
149 if err != nil {
150 return err
151 }
152 aliases, err := store.aliases(tx.db, targetID)
153 if err != nil {
154 return err
155 }
156 source, err := tx.visiblePage(actor, sourceID)
157 if err != nil {
158 return err
159 }
160 record, err := store.OpenPage(source)
161 if err != nil {
162 return err
163 }
164 body, ok := linkFirstMention(record.Body, number, target.Title,
165 aliases)
166 if !ok {
167 return errNoChange
168 }
169 record.Body = body
170 return tx.commit(&source, record, actor,
171 linkVerb(source, VerbRelinked), attachNothing)
172 }))
173}
174
175// linkFirstMention wraps the first mention in block number's text runs.
176func linkFirstMention(body string, number int, title string,
177 aliases []AliasView) (string, bool) {
178 blocks := markup.Parse(body)
179 if number < 0 || number >= len(blocks) ||
180 blocks[number].Kind == markup.Code ||
181 markup.IsFence(blocks[number].Kind) {
182 return body, false
183 }
184 phrases := phrasesOf(title, aliases)
185 source := blocks[number].Source
186 offset := 0
187 for _, span := range markup.Spans(source) {
188 if span.Kind == markup.Text {
189 words := wordsOf(span.Source)
190 for _, phrase := range phrases {
191 if start, end := phraseAt(words, phrase); start >= 0 {
192 text := span.Source[start:end]
193 edited := source[:offset+start] + linkTo(title, text) +
194 source[offset+end:]
195 return markup.Replace(body, blocks,
196 map[int]string{number: edited}), true
197 }
198 }
199 }
200 offset += len(span.Source)
201 }
202 return body, false
203}
204
205// linkTo is a link to title showing text, as short as it can be.
206func linkTo(title, text string) string {
207 if Fold(text) == Fold(title) {
208 return "[[" + text + "]]"
209 }
210 return "[[" + title + "|" + text + "]]"
211}
212