Revert "Add date-based archives per section"

This reverts commit a89a9cafa3.
This commit is contained in:
Jan-Lukas Else 2023-02-24 21:31:13 +01:00
parent a89a9cafa3
commit 80dfaf4814
3 changed files with 49 additions and 80 deletions

View File

@ -145,7 +145,7 @@ func (a *goBlog) blogRouter(blog string, conf *configBlog) func(r chi.Router) {
r.Group(a.blogTaxonomiesRouter(conf)) r.Group(a.blogTaxonomiesRouter(conf))
// Dates // Dates
r.With(a.privateModeHandler, a.cacheMiddleware).Group(a.blogDatesRouter(conf)) r.Group(a.blogDatesRouter(conf))
// Photos // Photos
r.Group(a.blogPhotosRouter(conf)) r.Group(a.blogPhotosRouter(conf))
@ -209,15 +209,10 @@ func (a *goBlog) blogSectionsRouter(conf *configBlog) func(r chi.Router) {
r.Group(func(r chi.Router) { r.Group(func(r chi.Router) {
secPath := conf.getRelativePath(section.Name) secPath := conf.getRelativePath(section.Name)
r.Use(middleware.WithValue(indexConfigKey, &indexConfig{ r.Use(middleware.WithValue(indexConfigKey, &indexConfig{
path: secPath, path: secPath,
section: section, section: section,
title: section.Title,
description: section.Description,
})) }))
r.Route(secPath, func(r chi.Router) { r.Get(secPath, a.serveIndex)
r.Get("/", a.serveIndex)
r.Group(a.blogDatesRouter(conf))
})
r.Get(secPath+feedPath, a.serveIndex) r.Get(secPath+feedPath, a.serveIndex)
r.Get(secPath+paginationPath, a.serveIndex) r.Get(secPath+paginationPath, a.serveIndex)
}) })
@ -251,6 +246,11 @@ func (a *goBlog) blogTaxonomiesRouter(conf *configBlog) func(r chi.Router) {
// Blog - Dates // Blog - Dates
func (a *goBlog) blogDatesRouter(conf *configBlog) func(r chi.Router) { func (a *goBlog) blogDatesRouter(conf *configBlog) func(r chi.Router) {
return func(r chi.Router) { return func(r chi.Router) {
r.Use(
a.privateModeHandler,
a.cacheMiddleware,
)
yearPath := conf.getRelativePath(`/{year:(x|\d{4})}`) yearPath := conf.getRelativePath(`/{year:(x|\d{4})}`)
r.Get(yearPath, a.serveDate) r.Get(yearPath, a.serveDate)
r.Get(yearPath+feedPath, a.serveDate) r.Get(yearPath+feedPath, a.serveDate)

View File

@ -5,7 +5,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"path"
"reflect" "reflect"
"strings" "strings"
"time" "time"
@ -220,32 +219,7 @@ func (a *goBlog) serveDeleted(w http.ResponseWriter, r *http.Request) {
} }
func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) { func (a *goBlog) serveDate(w http.ResponseWriter, r *http.Request) {
year, month, day, title, datePath := a.extractDate(r) var year, month, day int
if year == 0 && month == 0 && day == 0 {
a.serve404(w, r)
return
}
var ic *indexConfig
if cv := r.Context().Value(indexConfigKey); cv != nil {
origIc := *(cv.(*indexConfig))
copyIc := origIc
ic = &copyIc
ic.path = path.Join(ic.path, datePath)
ic.title = fmt.Sprintf("%s: %s", ic.title, title)
} else {
_, bc := a.getBlog(r)
ic = &indexConfig{
path: bc.getRelativePath(datePath),
title: title,
}
}
ic.year = year
ic.month = month
ic.day = day
a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, ic)))
}
func (a *goBlog) extractDate(r *http.Request) (year, month, day int, title, datePath string) {
if ys := chi.URLParam(r, "year"); ys != "" && ys != "x" { if ys := chi.URLParam(r, "year"); ys != "" && ys != "x" {
year = stringToInt(ys) year = stringToInt(ys)
} }
@ -255,29 +229,38 @@ func (a *goBlog) extractDate(r *http.Request) (year, month, day int, title, date
if ds := chi.URLParam(r, "day"); ds != "" { if ds := chi.URLParam(r, "day"); ds != "" {
day = stringToInt(ds) day = stringToInt(ds)
} }
titleBuf, pathBuf := bufferpool.Get(), bufferpool.Get() if year == 0 && month == 0 && day == 0 {
defer bufferpool.Put(titleBuf, pathBuf) a.serve404(w, r)
return
}
title, dPath := bufferpool.Get(), bufferpool.Get()
if year != 0 { if year != 0 {
_, _ = fmt.Fprintf(titleBuf, "%0004d", year) _, _ = fmt.Fprintf(title, "%0004d", year)
_, _ = fmt.Fprintf(pathBuf, "%0004d", year) _, _ = fmt.Fprintf(dPath, "%0004d", year)
} else { } else {
_, _ = titleBuf.WriteString("XXXX") _, _ = title.WriteString("XXXX")
_, _ = pathBuf.WriteString("x") _, _ = dPath.WriteString("x")
} }
if month != 0 { if month != 0 {
_, _ = fmt.Fprintf(titleBuf, "-%02d", month) _, _ = fmt.Fprintf(title, "-%02d", month)
_, _ = fmt.Fprintf(pathBuf, "/%02d", month) _, _ = fmt.Fprintf(dPath, "/%02d", month)
} else if day != 0 { } else if day != 0 {
_, _ = titleBuf.WriteString("-XX") _, _ = title.WriteString("-XX")
_, _ = pathBuf.WriteString("/x") _, _ = dPath.WriteString("/x")
} }
if day != 0 { if day != 0 {
_, _ = fmt.Fprintf(titleBuf, "-%02d", day) _, _ = fmt.Fprintf(title, "-%02d", day)
_, _ = fmt.Fprintf(pathBuf, "/%02d", day) _, _ = fmt.Fprintf(dPath, "/%02d", day)
} }
title = titleBuf.String() _, bc := a.getBlog(r)
datePath = pathBuf.String() a.serveIndex(w, r.WithContext(context.WithValue(r.Context(), indexConfigKey, &indexConfig{
return path: bc.getRelativePath(dPath.String()),
year: year,
month: month,
day: day,
title: title.String(),
})))
bufferpool.Put(title, dPath)
} }
type indexConfig struct { type indexConfig struct {
@ -341,17 +324,17 @@ func (a *goBlog) serveIndex(w http.ResponseWriter, r *http.Request) {
a.serveError(w, r, err.Error(), http.StatusInternalServerError) a.serveError(w, r, err.Error(), http.StatusInternalServerError)
return return
} }
// Title // Meta
var title string title := ic.title
if ic.title != "" { description := ic.description
title = ic.title if ic.tax != nil {
} else if ic.tax != nil {
title = fmt.Sprintf("%s: %s", ic.tax.Title, ic.taxValue) title = fmt.Sprintf("%s: %s", ic.tax.Title, ic.taxValue)
} else if ic.section != nil {
title = ic.section.Title
description = ic.section.Description
} else if search != "" { } else if search != "" {
title = fmt.Sprintf("%s: %s", bc.Search.Title, search) title = fmt.Sprintf("%s: %s", bc.Search.Title, search)
} }
// Description
description := ic.description
// Check if feed // Check if feed
if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed { if ft := feedType(chi.URLParam(r, "feed")); ft != noFeed {
a.generateFeed(blog, ft, w, r, posts, title, description) a.generateFeed(blog, ft, w, r, posts, title, description)

View File

@ -1,11 +1,10 @@
package main package main
import ( import (
"database/sql"
"encoding/xml" "encoding/xml"
"fmt"
"io" "io"
"net/http" "net/http"
"path"
"time" "time"
"github.com/araddon/dateparse" "github.com/araddon/dateparse"
@ -118,12 +117,6 @@ func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request
sm.Add(&sitemap.URL{ sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(section.Name)), Loc: a.getFullAddress(bc.getRelativePath(section.Name)),
}) })
datePaths, _ := a.sitemapDatePaths(b, []string{section.Name})
for _, p := range datePaths {
sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(path.Join(section.Name, p))),
})
}
} }
} }
// Taxonomies // Taxonomies
@ -145,7 +138,7 @@ func (a *goBlog) serveSitemapBlogArchives(w http.ResponseWriter, r *http.Request
} }
} }
// Date based archives // Date based archives
datePaths, _ := a.sitemapDatePaths(b, []string{}) datePaths, _ := a.sitemapDatePaths(b)
for _, p := range datePaths { for _, p := range datePaths {
sm.Add(&sitemap.URL{ sm.Add(&sitemap.URL{
Loc: a.getFullAddress(bc.getRelativePath(p)), Loc: a.getFullAddress(bc.getRelativePath(p)),
@ -194,16 +187,15 @@ func (a *goBlog) writeSitemapXML(w http.ResponseWriter, _ *http.Request, sm any)
} }
const sitemapDatePathsSql = ` const sitemapDatePathsSql = `
with filteredposts as ( %s ), with alldates as (
alldates as (
select distinct select distinct
substr(published, 1, 4) as year, substr(published, 1, 4) as year,
substr(published, 6, 2) as month, substr(published, 6, 2) as month,
substr(published, 9, 2) as day substr(published, 9, 2) as day
from ( from (
select tolocal(published) as published select tolocal(coalesce(published, '')) as published
from filteredposts from posts
where coalesce(published, '') != '' where blog = @blog and status = @status and published != ''
) )
) )
select distinct '/' || year from alldates select distinct '/' || year from alldates
@ -219,14 +211,8 @@ union
select distinct '/x/x/' || day from alldates; select distinct '/x/x/' || day from alldates;
` `
func (a *goBlog) sitemapDatePaths(blog string, sections []string) (paths []string, err error) { func (a *goBlog) sitemapDatePaths(blog string) (paths []string, err error) {
query, args := buildPostsQuery(&postsRequestConfig{ rows, err := a.db.Query(sitemapDatePathsSql, sql.Named("blog", blog), sql.Named("status", statusPublished))
blog: blog,
sections: sections,
status: []postStatus{statusPublished},
visibility: []postVisibility{visibilityPublic},
}, "published")
rows, err := a.db.Query(fmt.Sprintf(sitemapDatePathsSql, query), args...)
if err != nil { if err != nil {
return nil, err return nil, err
} }