/
wiki.go
executable file
·543 lines (442 loc) · 11.4 KB
/
wiki.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package pitchforkui
import (
"encoding/json"
"html/template"
"path/filepath"
"strconv"
"time"
pf "trident.li/pitchfork/lib"
)
func WikiUI_ApplyModOpts(cui PfUI, wiki *pf.PfWikiPage) {
opts := pf.Wiki_GetModOpts(cui)
op := wiki.FullPath
np := pf.URL_Append(opts.URLroot, op[len(opts.Pathroot):])
np = pf.URL_Append(opts.URLpfx, np)
wiki.FullPath = np
isdir := wiki.Path[len(wiki.Path)-1] == '/'
wiki.Path = filepath.Base(wiki.Path)
if isdir {
wiki.Path += "/"
}
}
func WikiUI_ApplyModOptsMulti(cui PfUI, wikis []pf.PfWikiPage) {
for i := range wikis {
WikiUI_ApplyModOpts(cui, &wikis[i])
}
}
func h_wiki_edit(cui PfUI) {
path := cui.GetSubPath()
rev := cui.GetArg("rev")
var m pf.PfWikiMarkdown
err := m.Fetch(cui, path, rev)
if err != nil && err != ErrNoRows {
H_error(cui, StatusBadRequest)
return
}
/* Output the page */
type Page struct {
*PfPage
WikiText string
}
p := Page{cui.Page_def(), m.Markdown}
p.AddJS("misc")
p.AddJS("editor")
cui.Page_show("wiki/edit.tmpl", p)
}
func h_wiki_source(cui PfUI) {
var m pf.PfWikiMarkdown
var h pf.PfWikiHTML
var err error
path := cui.GetSubPath()
rev := cui.GetArg("rev")
err = m.Fetch(cui, path, rev)
if err != nil && err != ErrNoRows {
H_error(cui, StatusBadRequest)
return
}
err = h.Fetch(cui, path, "")
if err != nil && err != ErrNoRows {
H_error(cui, StatusBadRequest)
return
}
/* Output the page */
type Page struct {
*PfPage
WikiText string
WikiHTML template.HTML
}
p := Page{cui.Page_def(), m.Markdown, h.HTML_Body}
cui.Page_show("wiki/source.tmpl", p)
}
func h_wiki_raw(cui PfUI) {
var m pf.PfWikiMarkdown
var err error
path := cui.GetSubPath()
rev := cui.GetArg("rev")
err = m.Fetch(cui, path, rev)
if err != nil && err != ErrNoRows {
H_error(cui, StatusBadRequest)
return
}
fname := pf.Wiki_Title(path) + ".md"
/* Output the page (RFC7763 for the MIMEType) */
cui.SetContentType("text/markdown")
cui.SetFileName(fname)
cui.SetExpires(60)
cui.SetRaw([]byte(m.Markdown))
}
func h_wiki_diff(cui PfUI) {
path := cui.GetSubPath()
revA := cui.GetArg("rev")
revB := cui.GetArg("revB")
if revA == "" || revB == "" {
H_error(cui, StatusBadRequest)
return
}
diff, err := pf.Wiki_Diff(cui, path, revA, revB)
if err != nil {
cui.Errf("Wiki_Diff(%q,%q,%q): %s", path, revA, revB, err.Error())
H_NoAccess(cui)
return
}
/* Output the page */
type Page struct {
*PfPage
RevA string
RevB string
Diff []pf.PfDiff
}
p := Page{cui.Page_def(), revA, revB, diff}
cui.Page_show("wiki/diff.tmpl", p)
}
func h_wiki_read(cui PfUI) {
path := cui.GetSubPath()
rev := cui.GetArg("rev")
var h pf.PfWikiHTML
err := h.Fetch(cui, path, rev)
if err != nil && err != ErrNoRows {
H_error(cui, StatusBadRequest)
return
}
/* Output the page */
type Page struct {
*PfPage
CanEdit bool
WikiTOC template.HTML
WikiHTML template.HTML
LastWhen time.Time
LastUser string
LastName string
}
p := Page{cui.Page_def(), true, h.HTML_TOC, h.HTML_Body, h.Entered, h.UserName, h.FullName}
cui.Page_show("wiki/read.tmpl", p)
}
func h_wiki_history(cui PfUI) {
var err error
var revs []pf.PfWikiRev
path := cui.GetSubPath()
total := 0
offset := 0
offset_v, err := cui.FormValue("offset")
if err == nil && offset_v != "" {
offset, _ = strconv.Atoi(offset_v)
}
total, err = pf.Wiki_RevisionMax(cui, path)
if err != nil {
H_error(cui, StatusBadRequest)
return
}
revs, err = pf.Wiki_RevisionList(cui, path, offset, total)
if err != nil {
H_error(cui, StatusBadRequest)
return
}
/* Output the page */
type Page struct {
*PfPage
PagerOffset int
PagerTotal int
Search string
Revs []pf.PfWikiRev
}
p := Page{cui.Page_def(), offset, total, "", revs}
cui.Page_show("wiki/history.tmpl", p)
}
func h_wiki_search(cui PfUI) {
var res []pf.PfWikiResult
total := 0
offset := 0
search, err := cui.FormValue("q")
if err == nil && search != "" {
offset_v, err2 := cui.FormValue("offset")
if err2 == nil && offset_v != "" {
offset, _ = strconv.Atoi(offset_v)
}
total, err = pf.Wiki_SearchMax(cui, search)
if err != nil {
H_error(cui, StatusBadRequest)
return
}
res, err = pf.Wiki_SearchList(cui, search, offset, total)
if err != nil {
H_error(cui, StatusBadRequest)
return
}
}
/* Output the page */
type popt struct {
Q string `label:"Search Query" hint:"What you are looking for" htmlclass:"search"`
Button string `label:"Search" hint:"To look for things" pftype:"submit" pfcol:""`
}
type Page struct {
*PfPage
Search popt
PagerOffset int
PagerTotal int
PathPrefix string
Results []pf.PfWikiResult
}
mopts := pf.Wiki_GetModOpts(cui)
opt := popt{search, ""}
p := Page{cui.Page_def(), opt, offset, total, mopts.URLroot, res}
cui.Page_show("wiki/search.tmpl", p)
}
func h_wiki_children(cui PfUI) {
var wikis []pf.PfWikiPage
path := cui.GetSubPath()
total := 0
offset := 0
offset_v, err := cui.FormValue("offset")
if err == nil && offset_v != "" {
offset, _ = strconv.Atoi(offset_v)
}
total, err = pf.Wiki_ChildPagesMax(cui, path)
if err != nil {
H_error(cui, StatusBadRequest)
return
}
wikis, err = pf.Wiki_ChildPagesList(cui, path, offset, total)
if err != nil {
H_error(cui, StatusBadRequest)
return
}
WikiUI_ApplyModOptsMulti(cui, wikis)
/* Output the page */
type Page struct {
*PfPage
PagerOffset int
PagerTotal int
Search string
Paths []pf.PfWikiPage
}
p := Page{cui.Page_def(), offset, total, "", wikis}
cui.Page_show("wiki/children.tmpl", p)
}
func h_wiki_options(cui PfUI) {
var err error
path := cui.GetSubPath()
type move struct {
Path string `label:"New path of the page" pfreq:"yes"`
Children bool `label:"Move all children of this page?" hint:"Only applies when the page has children"`
Confirm bool `label:"Confirm Moving" pfreq:"yes"`
Button string `label:"Move Page" pftype:"submit"`
Message string /* Used by pfform() */
Error string /* Used by pfform() */
}
type del struct {
Children bool `label:"Delete all children of this page?" hint:"Only applies when the page has children"`
Confirm bool `label:"Confirm Deletion" pfreq:"yes"`
Button string `label:"Delete Page" pftype:"submit" htmlclass:"deny"`
Message string /* Used by pfform() */
Error string /* Used by pfform() */
}
type cpy struct {
Path string `label:"Path of the page" pfreq:"yes"`
Children bool `label:"Copy all children of this page?" hint:"Only applies when the page has children"`
Confirm bool `label:"Confirm copying" pfreq:"yes"`
Button string `label:"Copy Page" pftype:"submit"`
Message string /* Used by pfform() */
Error string /* Used by pfform() */
cui PfUI /* Used by pfform() which calls FormContext() */
}
m := move{path, true, false, "", "", ""}
d := del{true, false, "", "", ""}
c := cpy{path, true, false, "", "", "", cui}
if cui.IsPOST() {
button, err1 := cui.FormValue("button")
confirmed, err2 := cui.FormValue("confirm")
children, err3 := cui.FormValue("children")
newpath, err4 := cui.FormValue("path")
if err1 != nil || err2 != nil || err3 != nil || err4 != nil {
m.Error = "Invalid input"
button = "Invalid"
}
if children == "on" {
children = "yes"
} else {
children = "no"
}
switch button {
case "Move Page":
if confirmed != "on" {
m.Error = "Did not confirm"
} else {
mopts := pf.Wiki_GetModOpts(cui)
cmd := mopts.Cmdpfx + " move"
arg := []string{path, newpath, children}
_, err = cui.HandleCmd(cmd, arg)
if err != nil {
m.Error = err.Error()
} else {
url := pf.URL_Append(mopts.URLroot, newpath)
cui.SetRedirect(url, StatusSeeOther)
return
}
}
break
case "Delete Page":
if confirmed != "on" {
d.Error = "Did not confirm"
} else {
mopts := pf.Wiki_GetModOpts(cui)
cmd := mopts.Cmdpfx + " delete"
arg := []string{path, children}
_, err = cui.HandleCmd(cmd, arg)
if err != nil {
d.Error = err.Error()
} else {
url := "../"
cui.SetRedirect(url, StatusSeeOther)
return
}
}
break
case "Copy Page":
if confirmed != "on" {
d.Error = "Did not confirm"
} else {
mopts := pf.Wiki_GetModOpts(cui)
cmd := mopts.Cmdpfx + " copy"
arg := []string{path, newpath, children}
_, err = cui.HandleCmd(cmd, arg)
if err != nil {
c.Error = err.Error()
} else {
c.Message = "Page copied"
return
}
}
break
}
}
type Page struct {
*PfPage
Move move
Delete del
Copy cpy
}
p := Page{cui.Page_def(), m, d, c}
cui.Page_show("wiki/options.tmpl", p)
}
func h_wiki_newpage(cui PfUI) {
path := cui.GetSubPath()
l := len(path)
if l > 0 && path[l-1] != '/' {
path += "/"
}
if cui.IsPOST() {
curpath, err := cui.FormValue("curpath")
page, err2 := cui.FormValue("page")
if err == nil && err2 == nil {
mopts := pf.Wiki_GetModOpts(cui)
url := pf.URL_Append(mopts.URLroot, curpath)
url = pf.URL_Append(url, page)
url += "?s=edit"
cui.SetRedirect(url, StatusSeeOther)
return
}
}
type np struct {
CurPath string `label:"Current Path of the page" pfset:"nobody" pfget:"user"`
Page string `label:"Name of new page" pfreq:"yes" hint:"Can include '/' to create multiple sub-levels"`
Button string `label:"Create New Page" pftype:"submit"`
}
type Page struct {
*PfPage
Opt np
}
p := Page{cui.Page_def(), np{path, "", ""}}
cui.Page_show("wiki/newpage.tmpl", p)
}
func wiki_post_ajax(cui PfUI, path string) {
rawbody := cui.GetBody()
type Wiki struct {
Markdown string `json:"markdown"`
Message string `json:"message"`
}
var body Wiki
err := json.Unmarshal(rawbody, &body)
if err != nil {
cui.JSONAnswer("error", "JSON parsing failed")
return
}
title := pf.Wiki_Title(path)
mopts := pf.Wiki_GetModOpts(cui)
cmd := mopts.Cmdpfx + " update"
arg := []string{path, body.Message, title, body.Markdown}
_, err = cui.CmdOut(cmd, arg)
if err != nil {
cui.JSONAnswer("error", "Update failed: "+err.Error())
return
}
cui.JSONAnswer("ok", "Updated")
}
func wiki_post_form(cui PfUI, path string) (err error) {
title := pf.Wiki_Title(path)
mopts := pf.Wiki_GetModOpts(cui)
cmd := mopts.Cmdpfx + " update"
arg := []string{path, "", title, ""}
_, err = cui.HandleCmd(cmd, arg)
return
}
func H_wiki(cui PfUI) {
var err error
/* URL of the page */
cui.SetSubPath("/" + cui.GetPathString())
for _, p := range cui.GetPath() {
cui.AddCrumb(p, p, "")
}
sub := cui.GetArg("s")
/* Ajax Post? */
if sub == "post" {
wiki_post_ajax(cui, cui.GetSubPath())
return
}
if sub == "edit" && cui.IsPOST() {
err = wiki_post_form(cui, cui.GetSubPath())
if err == nil {
cui.SetRedirect("?p=read", StatusSeeOther)
return
}
}
var menu = NewPfUIMenu([]PfUIMentry{
{"", "", PERM_USER, h_wiki_read, nil},
{"?s=read", "Read", PERM_USER, h_wiki_read, nil},
{"?s=source", "Source", PERM_USER, h_wiki_source, nil},
{"?s=raw", "Raw", PERM_USER + PERM_HIDDEN, h_wiki_raw, nil},
{"?s=edit", "Edit", PERM_USER, h_wiki_edit, nil},
{"?s=history", "History", PERM_USER, h_wiki_history, nil},
{"?s=diff", "Diff", PERM_USER | PERM_HIDDEN, h_wiki_diff, nil},
{"?s=options", "Options", PERM_USER, h_wiki_options, nil},
{"?s=children", "Child Pages", PERM_USER, h_wiki_children, nil},
{"?s=newpage", "New Page", PERM_USER, h_wiki_newpage, nil},
{"?s=search", "Search", PERM_USER, h_wiki_search, nil},
})
if sub == "read" {
sub = ""
}
if sub != "" {
sub = "?s=" + sub
}
cui.MenuPath(menu, &[]string{sub})
}