2017-07-12 06:43:11 +00:00
|
|
|
package yatwiki
|
2017-07-11 05:52:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
|
2017-07-12 06:43:11 +00:00
|
|
|
"code.ivysaur.me/yatwiki/diff"
|
2017-07-11 05:52:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|