initial /modify/ endpoint implementation

This commit is contained in:
mappu 2017-07-09 13:50:13 +12:00
parent 41dcc75254
commit af849d8bc6
2 changed files with 77 additions and 0 deletions

View File

@ -76,6 +76,15 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeView(w, r, articleTitle) this.routeView(w, r, articleTitle)
return 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/") { } else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"raw/") {
revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"raw/"):]) revId, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"raw/"):])
if err != nil { if err != nil {

68
rModify.go Normal file
View File

@ -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 &quot;<a href="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`view/`+url.QueryEscape(articleTitle)) + `">` + template.HTMLEscapeString(articleTitle) + `</a>&quot;`
baseRev = a.ID
existingBody = string(a.Body)
}
content := `
<h2>` + pageTitleHTML + `</h2><br>
<form method="POST" action="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`save`) + `" class="editor" accept-charset="UTF-8">
<div class="frm">
<label>
Save as:
<input type="hidden" name="baserev" value="` + fmt.Sprintf("%d", baseRev) + `" />
<input type="text" name="pname" value="` + template.HTMLEscapeString(articleTitle) + `"/>
</label>
<input type="submit" value="Save &raquo;">
| <a href="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`formatting`) + `" target="_blank">formatting&nbsp;help</a>
</div>
<div id="contentctr"><textarea name="content">` + template.HTMLEscapeString(existingBody) + `</textarea></div>
</form>
`
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
}