From af849d8bc64d5b500accdacd46e15c2a2f0b92ad Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 9 Jul 2017 13:50:13 +1200 Subject: [PATCH] initial /modify/ endpoint implementation --- WikiServer.go | 9 +++++++ rModify.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 rModify.go diff --git a/WikiServer.go b/WikiServer.go index 5b98da6..f47ed7d 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -76,6 +76,15 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { this.routeView(w, r, articleTitle) return + } else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"modify/") { + articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"modify/"):]) + if err != nil { + this.serveErrorMessage(w, err) + return + } + this.routeModify(w, r, articleTitle) + return + } else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"raw/") { revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"raw/"):]) if err != nil { diff --git a/rModify.go b/rModify.go new file mode 100644 index 0000000..309e25c --- /dev/null +++ b/rModify.go @@ -0,0 +1,68 @@ +package yatwiki3 + +import ( + "database/sql" + "fmt" + "html/template" + "net/http" + "net/url" +) + +func (this *WikiServer) routeModify(w http.ResponseWriter, r *http.Request, articleTitle string) { + + isNewArticle := false + + a, err := this.db.GetLatestVersion(articleTitle) + if err != nil { + + if err == sql.ErrNoRows { + // this is fine - we're creating it for the first time + isNewArticle = true + + } else { + this.serveErrorMessage(w, err) + return + } + } + + var pageTitleHTML string + var baseRev int + var existingBody string + if isNewArticle { + pageTitleHTML = `Creating new article` + baseRev = 0 + } else { + pageTitleHTML = `Editing article "` + template.HTMLEscapeString(articleTitle) + `"` + baseRev = a.ID + existingBody = string(a.Body) + } + + content := ` +

` + pageTitleHTML + `


+ +
+
+ + + | formatting help +
+
+
+ ` + + pto := DefaultPageTemplateOptions(this.opts) + if isNewArticle { + pto.CurrentPageName = "New article" + } else { + pto.CurrentPageName = articleTitle + pto.CurrentPageIsArticle = true + } + pto.Content = template.HTML(content) + this.servePageResponse(w, r, pto) + return + +}