From 73becc8729e182f74f323282d1f03cee40e0b4e6 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 11 Jul 2017 19:19:42 +1200 Subject: [PATCH] implement diff/parent/%d endpoint --- DB.go | 15 +++++++++++++++ WikiServer.go | 16 ++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/DB.go b/DB.go index d493ebf..c1bb977 100644 --- a/DB.go +++ b/DB.go @@ -165,6 +165,21 @@ func (this *WikiDB) GetRevisionHistory(title string) ([]Article, error) { return ret, nil } +func (this *WikiDB) GetNextOldestRevision(revision int) (int, error) { + row := this.db.QueryRow( + `SELECT articles.id FROM articles WHERE articles.article = (SELECT article FROM articles WHERE id = ?) AND id < ? ORDER BY id DESC LIMIT 1`, + revision, revision, + ) + + var ret int + err := row.Scan(&ret) + if err != nil { + return 0, err + } + + return ret, nil +} + func (this *WikiDB) GetRecentChanges(offset int, limit int) ([]Article, error) { rows, err := this.db.Query( `SELECT articles.id, articles.modified, articles.author, titles.title FROM articles JOIN titles ON articles.article=titles.id ORDER BY modified DESC ` + diff --git a/WikiServer.go b/WikiServer.go index d5130c0..fde6b4f 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -194,6 +194,22 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { this.serveRedirect(w, this.opts.ExpectBaseURL+`diff/`+r.URL.Query().Get("f")+`/`+r.URL.Query().Get("t")) return + } else if strings.HasPrefix(remainingPath, "diff/parent/") { + toRev, err := strconv.Atoi(remainingPath[len("diff/parent/"):]) + if err != nil { + this.serveErrorMessage(w, err) + return + } + + fromRev, err := this.db.GetNextOldestRevision(toRev) + if err != nil { + this.serveErrorMessage(w, err) + return + } + + this.serveRedirect(w, this.opts.ExpectBaseURL+`diff/`+fmt.Sprintf("%d/%d", fromRev, toRev)) + return + } else if match := this.rxDiff.FindStringSubmatch(remainingPath); len(match) == 3 { fromRev, err := strconv.Atoi(match[1])