2021-05-09 10:42:12 +00:00
|
|
|
package web
|
2021-05-09 09:18:21 +00:00
|
|
|
|
2021-05-11 08:33:00 +00:00
|
|
|
// stuff.go is used for meta stuff about the wiki or all hyphae at once.
|
2021-05-09 09:18:21 +00:00
|
|
|
import (
|
2021-12-20 21:08:21 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae/backlinks"
|
2021-05-09 09:18:21 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
2021-07-12 09:58:20 +00:00
|
|
|
"strings"
|
2021-05-09 09:18:21 +00:00
|
|
|
|
2021-07-15 17:46:35 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
2021-06-19 04:51:10 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/files"
|
2021-07-15 17:46:35 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/help"
|
2021-05-09 09:18:21 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
2021-09-06 17:46:34 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/l18n"
|
2021-05-09 09:18:21 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/shroom"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
2021-07-15 17:46:35 +00:00
|
|
|
|
2021-10-05 20:10:28 +00:00
|
|
|
"github.com/bouncepaw/mycomarkup/v3"
|
|
|
|
"github.com/bouncepaw/mycomarkup/v3/mycocontext"
|
2021-05-09 09:18:21 +00:00
|
|
|
)
|
|
|
|
|
2021-07-15 17:46:35 +00:00
|
|
|
func initStuff(r *mux.Router) {
|
2021-07-24 11:30:28 +00:00
|
|
|
r.PathPrefix("/help").HandlerFunc(handlerHelp)
|
2021-07-15 17:46:35 +00:00
|
|
|
r.HandleFunc("/list", handlerList)
|
|
|
|
r.HandleFunc("/reindex", handlerReindex)
|
|
|
|
r.HandleFunc("/update-header-links", handlerUpdateHeaderLinks)
|
|
|
|
r.HandleFunc("/random", handlerRandom)
|
|
|
|
r.HandleFunc("/about", handlerAbout)
|
|
|
|
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
|
2021-06-15 20:35:24 +00:00
|
|
|
http.Redirect(w, rq, "/static/favicon.ico", http.StatusSeeOther)
|
|
|
|
})
|
2021-05-09 09:18:21 +00:00
|
|
|
}
|
|
|
|
|
2021-07-10 20:02:16 +00:00
|
|
|
// handlerHelp gets the appropriate documentation or tells you where you (personally) have failed.
|
|
|
|
func handlerHelp(w http.ResponseWriter, rq *http.Request) {
|
2021-09-06 17:46:34 +00:00
|
|
|
lc := l18n.FromRequest(rq)
|
2021-07-24 11:30:28 +00:00
|
|
|
articlePath := strings.TrimPrefix(strings.TrimPrefix(rq.URL.Path, "/help/"), "/help")
|
2022-02-01 10:44:17 +00:00
|
|
|
lang := "en" // replace with lc.Locale once Russian docs are back
|
2021-07-24 11:30:28 +00:00
|
|
|
if articlePath == "" {
|
2022-02-01 10:44:17 +00:00
|
|
|
articlePath = "en" // replace with lc.Locale once Russian docs are back
|
2021-09-06 17:46:34 +00:00
|
|
|
} else {
|
|
|
|
var slashIndex = strings.Index(articlePath, "/")
|
|
|
|
if slashIndex == -1 {
|
|
|
|
lang = articlePath
|
|
|
|
} else {
|
|
|
|
lang = articlePath[:slashIndex]
|
|
|
|
}
|
2021-07-24 11:30:28 +00:00
|
|
|
}
|
|
|
|
content, err := help.Get(articlePath)
|
2021-07-12 09:58:20 +00:00
|
|
|
if err != nil && strings.HasPrefix(err.Error(), "open") {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
_, _ = io.WriteString(
|
|
|
|
w,
|
2021-09-06 17:46:34 +00:00
|
|
|
views.BaseHTML(lc.Get("help.entry_not_found"),
|
|
|
|
views.HelpHTML(views.HelpEmptyErrorHTML(lc), lang, lc),
|
|
|
|
lc,
|
2021-07-12 09:58:20 +00:00
|
|
|
user.FromRequest(rq)),
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
2021-07-10 20:02:16 +00:00
|
|
|
if err != nil {
|
2021-07-12 09:58:20 +00:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
_, _ = io.WriteString(
|
|
|
|
w,
|
|
|
|
views.BaseHTML(err.Error(),
|
2021-09-06 17:46:34 +00:00
|
|
|
views.HelpHTML(err.Error(), lang, lc),
|
|
|
|
lc,
|
2021-07-12 09:58:20 +00:00
|
|
|
user.FromRequest(rq)),
|
|
|
|
)
|
2021-07-10 20:02:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: change for the function that uses byte array when there is such function in mycomarkup.
|
2021-07-24 11:30:28 +00:00
|
|
|
ctx, _ := mycocontext.ContextFromStringInput(articlePath, string(content))
|
2021-07-10 20:02:16 +00:00
|
|
|
ast := mycomarkup.BlockTree(ctx)
|
|
|
|
result := mycomarkup.BlocksToHTML(ctx, ast)
|
2021-07-12 09:58:20 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
_, _ = io.WriteString(
|
|
|
|
w,
|
2021-09-06 17:46:34 +00:00
|
|
|
views.BaseHTML(lc.Get("help.title"),
|
|
|
|
views.HelpHTML(result, lang, lc),
|
|
|
|
lc,
|
2021-07-12 09:58:20 +00:00
|
|
|
user.FromRequest(rq)),
|
|
|
|
)
|
2021-07-10 20:02:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 09:18:21 +00:00
|
|
|
// handlerList shows a list of all hyphae in the wiki in random order.
|
|
|
|
func handlerList(w http.ResponseWriter, rq *http.Request) {
|
2021-07-10 19:04:21 +00:00
|
|
|
u := user.FromRequest(rq)
|
2021-09-06 17:46:34 +00:00
|
|
|
var lc = l18n.FromRequest(rq)
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-09-06 17:46:34 +00:00
|
|
|
util.HTTP200Page(w, views.BaseHTML(lc.Get("ui.list_title"), views.HyphaListHTML(lc), lc, u))
|
2021-05-09 09:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlerReindex reindexes all hyphae by checking the wiki storage directory anew.
|
|
|
|
func handlerReindex(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-05-09 09:18:21 +00:00
|
|
|
if ok := user.CanProceed(rq, "reindex"); !ok {
|
2021-09-06 17:46:34 +00:00
|
|
|
var lc = l18n.FromRequest(rq)
|
|
|
|
httpErr(w, lc, http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.no_rights"), lc.Get("ui.reindex_no_rights"))
|
2021-05-09 09:18:21 +00:00
|
|
|
log.Println("Rejected", rq.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hyphae.ResetCount()
|
2021-06-19 04:51:10 +00:00
|
|
|
log.Println("Reindexing hyphae in", files.HyphaeDir())
|
|
|
|
hyphae.Index(files.HyphaeDir())
|
2021-12-20 21:08:21 +00:00
|
|
|
backlinks.IndexBacklinks()
|
2021-05-09 09:18:21 +00:00
|
|
|
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerUpdateHeaderLinks updates header links by reading the configured hypha, if there is any, or resorting to default values.
|
|
|
|
//
|
2021-07-05 04:22:17 +00:00
|
|
|
// See https://mycorrhiza.wiki/hypha/configuration/header
|
2021-05-09 09:18:21 +00:00
|
|
|
func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-05-09 09:18:21 +00:00
|
|
|
if ok := user.CanProceed(rq, "update-header-links"); !ok {
|
2021-09-06 17:46:34 +00:00
|
|
|
var lc = l18n.FromRequest(rq)
|
|
|
|
httpErr(w, lc, http.StatusForbidden, cfg.HomeHypha, lc.Get("ui.no_rights"), lc.Get("ui.header_no_rights"))
|
2021-05-09 09:18:21 +00:00
|
|
|
log.Println("Rejected", rq.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shroom.SetHeaderLinks()
|
|
|
|
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerRandom redirects to a random hypha.
|
|
|
|
func handlerRandom(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-05-09 09:18:21 +00:00
|
|
|
var (
|
|
|
|
randomHyphaName string
|
|
|
|
amountOfHyphae = hyphae.Count()
|
|
|
|
)
|
|
|
|
if amountOfHyphae == 0 {
|
2021-09-06 17:46:34 +00:00
|
|
|
var lc = l18n.FromRequest(rq)
|
|
|
|
httpErr(w, lc, http.StatusNotFound, cfg.HomeHypha, lc.Get("ui.random_no_hyphae"), lc.Get("ui.random_no_hyphae_tip"))
|
2021-05-09 09:18:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
i := rand.Intn(amountOfHyphae)
|
|
|
|
for h := range hyphae.YieldExistingHyphae() {
|
|
|
|
if i == 0 {
|
|
|
|
randomHyphaName = h.Name
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
http.Redirect(w, rq, "/hypha/"+randomHyphaName, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerAbout shows a summary of wiki's software.
|
|
|
|
func handlerAbout(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-09-06 17:46:34 +00:00
|
|
|
var (
|
2021-09-29 14:56:17 +00:00
|
|
|
lc = l18n.FromRequest(rq)
|
2021-09-06 17:46:34 +00:00
|
|
|
title = lc.Get("ui.about_title", &l18n.Replacements{"name": cfg.WikiName})
|
|
|
|
)
|
|
|
|
_, err := io.WriteString(w, views.BaseHTML(title, views.AboutHTML(lc), lc, user.FromRequest(rq)))
|
2021-05-09 09:18:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|