diff --git a/DB.go b/DB.go index bd54a65..05223c3 100644 --- a/DB.go +++ b/DB.go @@ -78,6 +78,7 @@ type Article struct { Modified int64 Body []byte Author string + Title string } func (this *Article) FillModifiedTimestamp() { @@ -89,17 +90,12 @@ func (this *Article) FillAuthor(r *http.Request) { this.Author = r.RemoteAddr + "-" + hex.EncodeToString(userAgentHash[:])[:6] } -type ArticleWithTitle struct { - Article - Title string -} - func (this *WikiDB) GetArticleById(articleId int) (*Article, error) { row := this.db.QueryRow(`SELECT articles.* FROM articles WHERE id = ?`, articleId) return this.parseArticle(row) } -func (this *WikiDB) GetRevision(revId int) (*ArticleWithTitle, error) { +func (this *WikiDB) GetRevision(revId int) (*Article, error) { row := this.db.QueryRow(`SELECT articles.*, titles.title FROM articles JOIN titles ON articles.article=titles.id WHERE articles.id = ?`, revId) return this.parseArticleWithTitle(row) } @@ -109,6 +105,47 @@ func (this *WikiDB) GetLatestVersion(title string) (*Article, error) { return this.parseArticle(row) } +func (this *WikiDB) GetRevisionHistory(title string) ([]Article, error) { + rows, err := this.db.Query(`SELECT articles.id, articles.modified, articles.author FROM articles WHERE article = (SELECT id FROM titles WHERE title = ?) ORDER BY modified DESC`, title) + if err != nil { + return nil, err + } + defer rows.Close() + + ret := make([]Article, 0) + for rows.Next() { + a := Article{} + err := rows.Scan(&a.ID, &a.Modified, &a.Author) + if err != nil { + return nil, err + } + ret = append(ret, a) + } + 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 ` + + fmt.Sprintf(`LIMIT %d OFFSET %d`, limit, offset), + ) + if err != nil { + return nil, err + } + defer rows.Close() + + ret := make([]Article, 0, limit) + for rows.Next() { + a := Article{} + err := rows.Scan(&a.ID, &a.Modified, &a.Author, &a.Title) + if err != nil { + return nil, err + } + ret = append(ret, a) + } + return ret, nil +} + func (this *WikiDB) TotalRevisions() (int64, error) { row := this.db.QueryRow(`SELECT COUNT(*) c FROM articles`) var ret int64 @@ -125,8 +162,8 @@ func (this *WikiDB) ListTitles() ([]string, error) { if err != nil { return nil, err } - defer rows.Close() + ret := make([]string, 0) for rows.Next() { var title string @@ -173,8 +210,8 @@ func (this *WikiDB) parseArticle(row *sql.Row) (*Article, error) { return &a, nil } -func (this *WikiDB) parseArticleWithTitle(row *sql.Row) (*ArticleWithTitle, error) { - a := ArticleWithTitle{} +func (this *WikiDB) parseArticleWithTitle(row *sql.Row) (*Article, error) { + a := Article{} var gzBody []byte err := row.Scan(&a.ID, &a.TitleID, &a.Modified, &gzBody, &a.Author, &a.Title) if err != nil { diff --git a/TODO.txt b/TODO.txt index d7d343c..d58d749 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,10 +1,6 @@ Save changes -Article history - -Recent history - Diffs RSS diff --git a/WikiServer.go b/WikiServer.go index 590ea76..aec2b8f 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -93,6 +93,15 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { this.routeModify(w, r, articleTitle) return + } else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"history/") { + articleTitle, err := url.QueryUnescape(r.URL.Path[len(this.opts.ExpectBaseURL+"history/"):]) + if err != nil { + this.serveErrorMessage(w, err) + return + } + this.routeHistory(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 { @@ -113,6 +122,16 @@ func (this *WikiServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { this.routeArchive(w, r, revId) return + } else if strings.HasPrefix(r.URL.Path, this.opts.ExpectBaseURL+"recent/") { + pageNum, err := strconv.Atoi(r.URL.Path[len(this.opts.ExpectBaseURL+"recent/"):]) + if err != nil { + this.serveErrorMessage(w, err) + return + } + + this.routeRecentChanges(w, r, pageNum) + return + } } diff --git a/pageTemplate.go b/pageTemplate.go index 32791f1..5be41be 100644 --- a/pageTemplate.go +++ b/pageTemplate.go @@ -97,7 +97,7 @@ function els(e,s){ // no js exec in innerHTML