implement diff?f=&t= endpoint

This commit is contained in:
mappu 2017-07-11 17:52:29 +12:00
parent fc51fe732b
commit f2155babe0
3 changed files with 85 additions and 0 deletions

View File

@ -139,6 +139,23 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
this.routeRecentChanges(w, r, pageNum)
return
} else if r.URL.Path == this.opts.ExpectBaseURL+"diff" {
fromRev, err := strconv.Atoi(r.URL.Query().Get("f"))
if err != nil {
this.serveErrorMessage(w, err)
return
}
toRev, err := strconv.Atoi(r.URL.Query().Get("t"))
if err != nil {
this.serveErrorMessage(w, err)
return
}
this.routeDiff(w, r, fromRev, toRev)
return
}
} else if r.Method == "POST" {

64
rDiff.go Normal file
View File

@ -0,0 +1,64 @@
package yatwiki3
import (
"bytes"
"database/sql"
"errors"
"fmt"
"html/template"
"net/http"
"code.ivysaur.me/yatwiki3/diff"
)
func (this *WikiServer) routeDiff(w http.ResponseWriter, r *http.Request, oldRev, newRev int) {
if oldRev > newRev {
oldRev, newRev = newRev, oldRev
}
oa, err := this.db.GetRevision(oldRev)
if err != nil {
if err == sql.ErrNoRows {
this.serveErrorMessage(w, errors.New("Invalid revision selected"))
return
}
this.serveErrorMessage(w, err)
return
}
na, err := this.db.GetRevision(newRev)
if err != nil {
if err == sql.ErrNoRows {
this.serveErrorMessage(w, errors.New("Invalid revision selected"))
return
}
this.serveErrorMessage(w, err)
return
}
if oa.TitleID != na.TitleID {
this.serveErrorMessage(w, fmt.Errorf("Preventing attempt to compare mismatched articles (%s ≠ %s)", oa.Title, na.Title))
return
}
b := bytes.Buffer{}
diff.HtmlDiff(string(oa.Body), string(na.Body), &b)
pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = oa.Title
pto.CurrentPageIsArticle = true
pto.Content = template.HTML(
`<h2>` +
`Comparing ` + string(this.viewLink(oa.Title)) + ` versions ` +
`<a href="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`archive/`+fmt.Sprintf("%d", oa.ID)) + `">r` + fmt.Sprintf("%d", oa.ID) + `</a>` +
` - ` +
`<a href="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`archive/`+fmt.Sprintf("%d", na.ID)) + `">r` + fmt.Sprintf("%d", na.ID) + `</a>` +
`</h2>` +
`<pre>` + string(b.Bytes()) + `</pre>`,
)
this.servePageResponse(w, r, pto)
return
}

View File

@ -44,3 +44,7 @@ func (this *WikiServer) formatTimestamp(m int64) string {
// TODO add a more detailed timestamp on hover
return template.HTMLEscapeString(time.Unix(m, 0).In(this.loc).Format(this.opts.DateFormat))
}
func (this *WikiServer) viewLink(articleTitle string) template.HTML {
return template.HTML(`&quot;<a href="` + template.HTMLEscapeString(this.opts.ExpectBaseURL+`view/`+url.QueryEscape(articleTitle)) + `">` + template.HTMLEscapeString(articleTitle) + `</a>&quot;`)
}