wp-go/internal/actions/index.go

347 lines
9.3 KiB
Go
Raw Normal View History

2022-09-18 04:34:48 +00:00
package actions
2022-09-01 02:31:11 +00:00
import (
2022-09-15 14:35:32 +00:00
"fmt"
2022-09-18 03:57:43 +00:00
"github.com/gin-contrib/sessions"
2022-09-01 02:31:11 +00:00
"github.com/gin-gonic/gin"
2022-09-15 14:44:45 +00:00
"github/fthvgb1/wp-go/helper"
2023-01-14 04:21:31 +00:00
cache2 "github/fthvgb1/wp-go/internal/pkg/cache"
dao "github/fthvgb1/wp-go/internal/pkg/dao"
2023-01-13 06:04:32 +00:00
"github/fthvgb1/wp-go/internal/pkg/models"
2023-01-12 12:42:16 +00:00
"github/fthvgb1/wp-go/internal/plugins"
2023-01-14 04:21:31 +00:00
"github/fthvgb1/wp-go/internal/templates"
2023-01-12 10:17:34 +00:00
"github/fthvgb1/wp-go/internal/wpconfig"
2023-01-12 12:42:16 +00:00
"github/fthvgb1/wp-go/model"
2022-09-15 14:35:32 +00:00
"math"
2022-09-01 02:31:11 +00:00
"net/http"
2022-09-16 06:46:51 +00:00
"regexp"
2022-09-14 05:28:31 +00:00
"strconv"
2022-09-15 14:35:32 +00:00
"strings"
2022-12-28 06:24:59 +00:00
"sync/atomic"
2022-09-01 02:31:11 +00:00
)
2022-09-18 14:06:27 +00:00
type indexHandle struct {
2022-09-17 09:11:10 +00:00
c *gin.Context
2022-09-18 03:57:43 +00:00
session sessions.Session
2022-09-17 09:11:10 +00:00
page int
pageSize int
title string
titleL string
titleR string
search string
2022-11-17 03:22:29 +00:00
author string
2022-09-17 09:11:10 +00:00
totalPage int
category string
categoryType string
2023-01-12 12:42:16 +00:00
where model.SqlBuilder
2022-09-28 08:23:20 +00:00
orderBy string
2022-09-17 09:11:10 +00:00
order string
2023-01-12 12:42:16 +00:00
join model.SqlBuilder
2022-09-20 14:37:31 +00:00
postType []any
status []any
2022-09-17 09:11:10 +00:00
header string
paginationStep int
2022-09-23 13:46:34 +00:00
scene uint
2022-09-17 09:05:43 +00:00
}
2022-09-18 14:06:27 +00:00
func newIndexHandle(ctx *gin.Context) *indexHandle {
2022-11-17 08:29:39 +00:00
size := wpconfig.Options.Value("posts_per_page")
2022-11-03 07:41:38 +00:00
si, _ := strconv.Atoi(size)
2022-09-18 14:06:27 +00:00
return &indexHandle{
2022-09-17 09:11:10 +00:00
c: ctx,
2022-09-18 03:57:43 +00:00
session: sessions.Default(ctx),
2022-09-17 09:11:10 +00:00
page: 1,
2022-11-03 07:41:38 +00:00
pageSize: si,
2022-09-17 09:11:10 +00:00
paginationStep: 1,
2022-11-17 08:29:39 +00:00
titleL: wpconfig.Options.Value("blogname"),
titleR: wpconfig.Options.Value("blogdescription"),
2023-01-12 12:42:16 +00:00
where: model.SqlBuilder{
2022-09-17 09:05:43 +00:00
{"post_type", "in", ""},
{"post_status", "in", ""},
},
2022-09-28 08:23:20 +00:00
orderBy: "post_date",
2023-01-12 12:42:16 +00:00
join: model.SqlBuilder{},
2022-09-20 14:37:31 +00:00
postType: []any{"post"},
status: []any{"publish"},
2022-09-23 13:46:34 +00:00
scene: plugins.Home,
2022-09-17 09:05:43 +00:00
}
}
2022-09-18 14:06:27 +00:00
func (h *indexHandle) setTitleLR(l, r string) {
2022-09-17 09:05:43 +00:00
h.titleL = l
h.titleR = r
}
2022-09-18 14:06:27 +00:00
func (h *indexHandle) getTitle() string {
2022-09-17 09:05:43 +00:00
h.title = fmt.Sprintf("%s-%s", h.titleL, h.titleR)
return h.title
}
2022-09-28 08:23:20 +00:00
func (h *indexHandle) getSearchKey() string {
2022-11-17 03:22:29 +00:00
return fmt.Sprintf("action:%s|%s|%s|%s|%s|%s|%d|%d", h.author, h.search, h.orderBy, h.order, h.category, h.categoryType, h.page, h.pageSize)
2022-09-28 08:23:20 +00:00
}
2022-11-17 03:22:29 +00:00
func (h *indexHandle) parseParams() (err error) {
2022-09-17 09:05:43 +00:00
h.order = h.c.Query("order")
if !helper.IsContainInArr(h.order, []string{"asc", "desc"}) {
h.order = "asc"
}
year := h.c.Param("year")
2022-09-16 06:46:51 +00:00
if year != "" {
2022-09-17 09:05:43 +00:00
h.where = append(h.where, []string{
2022-09-16 06:46:51 +00:00
"year(post_date)", year,
})
}
2022-09-17 09:05:43 +00:00
month := h.c.Param("month")
2022-09-16 06:46:51 +00:00
if month != "" {
2022-09-17 09:05:43 +00:00
h.where = append(h.where, []string{
2022-09-16 06:46:51 +00:00
"month(post_date)", month,
})
2022-09-17 13:45:22 +00:00
ss := fmt.Sprintf("%s年%s月", year, strings.TrimLeft(month, "0"))
2022-09-17 09:05:43 +00:00
h.header = fmt.Sprintf("月度归档: <span>%s</span>", ss)
2022-11-17 08:29:39 +00:00
h.setTitleLR(ss, wpconfig.Options.Value("blogname"))
2022-09-23 13:46:34 +00:00
h.scene = plugins.Archive
2022-09-16 06:46:51 +00:00
}
2022-09-17 09:05:43 +00:00
category := h.c.Param("category")
2022-09-16 09:59:17 +00:00
if category == "" {
2022-09-17 09:05:43 +00:00
category = h.c.Param("tag")
2022-09-16 09:59:17 +00:00
if category != "" {
2022-09-17 09:05:43 +00:00
h.categoryType = "post_tag"
h.header = fmt.Sprintf("标签: <span>%s</span>", category)
2022-09-16 09:59:17 +00:00
}
} else {
2022-09-17 09:05:43 +00:00
h.categoryType = "category"
h.header = fmt.Sprintf("分类: <span>%s</span>", category)
2022-09-16 09:59:17 +00:00
}
2022-09-17 09:05:43 +00:00
h.category = category
2022-11-17 03:22:29 +00:00
username := h.c.Param("author")
if username != "" {
2023-01-14 04:21:31 +00:00
user, er := cache2.GetUserByName(h.c, username)
2022-11-17 03:22:29 +00:00
if er != nil {
err = er
return
}
h.author = username
h.where = append(h.where, []string{
"post_author", "=", strconv.FormatUint(user.Id, 10), "int",
})
}
2022-09-16 06:46:51 +00:00
if category != "" {
2022-09-17 09:05:43 +00:00
h.where = append(h.where, []string{
2022-09-16 06:46:51 +00:00
"d.name", category,
2022-09-17 09:05:43 +00:00
}, []string{"taxonomy", h.categoryType})
h.join = append(h.join, []string{
2022-09-16 09:59:17 +00:00
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
}, []string{
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
}, []string{
"left join", "wp_terms d", "c.term_id=d.term_id",
})
2022-11-17 08:29:39 +00:00
h.setTitleLR(category, wpconfig.Options.Value("blogname"))
2022-09-23 13:46:34 +00:00
h.scene = plugins.Category
2022-09-17 09:05:43 +00:00
}
s := h.c.Query("s")
if s != "" && strings.Replace(s, " ", "", -1) != "" {
q := helper.StrJoin("%", s, "%")
h.where = append(h.where, []string{
"and", "post_title", "like", q, "",
"or", "post_content", "like", q, "",
"or", "post_excerpt", "like", q, "",
}, []string{"post_password", ""})
h.postType = append(h.postType, "page", "attachment")
h.header = fmt.Sprintf("%s的搜索结果", s)
2022-11-17 08:29:39 +00:00
h.setTitleLR(helper.StrJoin(`"`, s, `"`, "的搜索结果"), wpconfig.Options.Value("blogname"))
2022-09-17 09:05:43 +00:00
h.search = s
2022-09-23 13:46:34 +00:00
h.scene = plugins.Search
2022-09-16 06:46:51 +00:00
}
2022-09-17 09:05:43 +00:00
p := h.c.Query("paged")
2022-09-15 14:35:32 +00:00
if p == "" {
2022-09-17 09:05:43 +00:00
p = h.c.Param("page")
2022-09-14 05:28:31 +00:00
}
2022-09-15 14:35:32 +00:00
if p != "" {
if pa, err := strconv.Atoi(p); err == nil {
2022-09-17 09:05:43 +00:00
h.page = pa
2022-09-15 14:35:32 +00:00
}
}
2023-01-12 12:42:16 +00:00
total := int(atomic.LoadInt64(&dao.TotalRaw))
2022-12-28 06:24:59 +00:00
if total > 0 && total < (h.page-1)*h.pageSize {
2022-10-04 03:13:14 +00:00
h.page = 1
}
2022-09-17 09:05:43 +00:00
if h.page > 1 && (h.category != "" || h.search != "" || month != "") {
2022-11-17 08:29:39 +00:00
h.setTitleLR(fmt.Sprintf("%s-第%d页", h.titleL, h.page), wpconfig.Options.Value("blogname"))
2022-09-17 07:37:23 +00:00
}
2022-11-17 03:22:29 +00:00
return
2022-09-17 09:05:43 +00:00
}
2022-09-15 14:35:32 +00:00
2022-09-18 14:06:27 +00:00
func (h *indexHandle) getTotalPage(totalRaws int) int {
2022-09-17 09:05:43 +00:00
h.totalPage = int(math.Ceil(float64(totalRaws) / float64(h.pageSize)))
return h.totalPage
}
2022-09-18 04:34:48 +00:00
func Index(c *gin.Context) {
2022-09-18 14:06:27 +00:00
h := newIndexHandle(c)
2023-01-12 12:42:16 +00:00
var postIds []models.Posts
2022-11-17 03:22:29 +00:00
var totalRaw int
var err error
2023-01-14 04:21:31 +00:00
archive := cache2.Archives(c)
recent := cache2.RecentPosts(c, 5)
categoryItems := cache2.Categories(c)
recentComments := cache2.RecentComments(c, 5)
2022-09-19 12:15:10 +00:00
ginH := gin.H{
2022-11-17 08:29:39 +00:00
"options": wpconfig.Options,
2022-09-21 13:49:14 +00:00
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"search": h.search,
"header": h.header,
"recentComments": recentComments,
2022-09-19 12:15:10 +00:00
}
2022-11-17 03:22:29 +00:00
defer func() {
stat := http.StatusOK
if err != nil {
c.Error(err)
stat = http.StatusInternalServerError
2022-12-28 08:40:18 +00:00
return
2022-11-17 03:22:29 +00:00
}
2023-01-14 04:21:31 +00:00
tmlp := wpconfig.Options.Value("template")
if i, err := templates.IsTemplateIsExist(tmlp); err != nil || !i {
tmlp = "twentyfifteen"
}
c.HTML(stat, helper.StrJoin(tmlp, "/posts/index.gohtml"), ginH)
2022-11-17 03:22:29 +00:00
}()
err = h.parseParams()
if err != nil {
return
}
ginH["title"] = h.getTitle()
2022-09-27 13:52:15 +00:00
if c.Param("month") != "" {
2023-01-14 04:21:31 +00:00
postIds, totalRaw, err = cache2.GetMonthPostIds(c, c.Param("year"), c.Param("month"), h.page, h.pageSize, h.order)
2022-09-27 13:52:15 +00:00
if err != nil {
return
}
2022-09-28 12:02:43 +00:00
} else if h.search != "" {
2023-01-14 04:21:31 +00:00
postIds, totalRaw, err = cache2.SearchPost(c, h.getSearchKey(), c, h.where, h.page, h.pageSize, model.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status)
2022-09-27 13:52:15 +00:00
} else {
2023-01-14 04:21:31 +00:00
postIds, totalRaw, err = cache2.PostLists(c, h.getSearchKey(), c, h.where, h.page, h.pageSize, model.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.status)
2022-09-27 13:52:15 +00:00
}
2022-09-17 09:05:43 +00:00
if err != nil {
return
}
if len(postIds) < 1 && h.category != "" {
h.titleL = "未找到页面"
}
2022-09-27 13:52:15 +00:00
2022-09-18 04:34:48 +00:00
pw := h.session.Get("post_password")
2022-09-23 13:46:34 +00:00
plug := plugins.NewPostPlugin(c, h.scene)
2022-10-08 08:37:27 +00:00
for i, post := range postIds {
2023-01-12 12:42:16 +00:00
plugins.PasswordProjectTitle(&postIds[i])
2022-09-27 07:35:34 +00:00
if post.PostPassword != "" && pw != post.PostPassword {
2023-01-12 12:42:16 +00:00
plugins.PasswdProjectContent(&postIds[i])
2022-09-24 09:52:06 +00:00
} else {
plugins.ApplyPlugin(plug, &postIds[i])
2022-09-18 04:34:48 +00:00
}
2022-09-14 05:28:31 +00:00
}
2022-09-18 04:34:48 +00:00
for i, post := range recent {
if post.PostPassword != "" && pw != post.PostPassword {
2023-01-12 12:42:16 +00:00
plugins.PasswdProjectContent(&recent[i])
2022-09-18 04:34:48 +00:00
}
}
2022-09-15 14:35:32 +00:00
q := c.Request.URL.Query().Encode()
2022-09-27 13:52:15 +00:00
if q != "" {
q = fmt.Sprintf("?%s", q)
}
2022-09-19 12:15:10 +00:00
ginH["posts"] = postIds
ginH["totalPage"] = h.getTotalPage(totalRaw)
ginH["pagination"] = pagination(h.page, h.totalPage, h.paginationStep, c.Request.URL.Path, q)
ginH["title"] = h.getTitle()
2022-09-01 02:31:11 +00:00
}
2022-09-14 13:30:59 +00:00
2022-09-23 13:46:34 +00:00
var complie = regexp.MustCompile(`(/page)/(\d+)`)
2022-09-16 06:46:51 +00:00
func pagination(currentPage, totalPage, step int, path, query string) (html string) {
if totalPage < 2 {
return
}
pathx := path
if !strings.Contains(path, "/page/") {
pathx = fmt.Sprintf("%s%s", path, "/page/1")
}
2022-09-15 14:35:32 +00:00
s := strings.Builder{}
if currentPage > totalPage {
currentPage = totalPage
}
2022-09-23 13:46:34 +00:00
r := complie
2022-09-15 14:35:32 +00:00
start := currentPage - step
end := currentPage + step
if start < 1 {
2022-09-16 14:32:05 +00:00
start = 1
2022-09-15 14:35:32 +00:00
}
if currentPage > 1 {
pp := ""
2022-09-16 06:46:51 +00:00
if currentPage >= 2 {
pp = replacePage(r, pathx, currentPage-1)
2022-09-15 14:35:32 +00:00
}
2022-09-16 06:46:51 +00:00
s.WriteString(fmt.Sprintf(`<a class="prev page-numbers" href="%s%s">上一页</a>`, pp, query))
2022-09-15 14:35:32 +00:00
}
if currentPage >= step+2 {
d := ""
if currentPage > step+2 {
d = `<span class="page-numbers dots">…</span>`
}
2022-09-16 06:46:51 +00:00
e := replacePage(r, path, 1)
2022-09-15 14:35:32 +00:00
s.WriteString(fmt.Sprintf(`
2022-09-16 06:46:51 +00:00
<a class="page-numbers" href="%s%s"><span class="meta-nav screen-reader-text"> </span>1</a>
2022-09-15 14:35:32 +00:00
%s
2022-09-16 06:46:51 +00:00
`, e, query, d))
2022-09-15 14:35:32 +00:00
}
if totalPage < end {
end = totalPage
}
for page := start; page <= end; page++ {
h := ""
if currentPage == page {
h = fmt.Sprintf(`
<span aria-current="page" class="page-numbers current">
<span class="meta-nav screen-reader-text"> </span>%d</span>
`, page)
} else {
2022-09-16 06:46:51 +00:00
d := replacePage(r, pathx, page)
2022-09-15 14:35:32 +00:00
h = fmt.Sprintf(`
<a class="page-numbers" href="%s%s">
<span class="meta-nav screen-reader-text"> </span>%d</a>
`, d, query, page)
}
s.WriteString(h)
}
2022-09-16 06:46:51 +00:00
if totalPage >= currentPage+step+1 {
if totalPage > currentPage+step+1 {
s.WriteString(`<span class="page-numbers dots">…</span>`)
}
dd := replacePage(r, pathx, totalPage)
2022-09-15 14:35:32 +00:00
s.WriteString(fmt.Sprintf(`
2022-09-16 06:46:51 +00:00
<a class="page-numbers" href="%s%s"><span class="meta-nav screen-reader-text"> </span>%d</a>`, dd, query, totalPage))
2022-09-15 14:35:32 +00:00
}
if currentPage < totalPage {
2022-09-16 06:46:51 +00:00
dd := replacePage(r, pathx, currentPage+1)
s.WriteString(fmt.Sprintf(`<a class="next page-numbers" href="%s%s">下一页</a>`, dd, query))
2022-09-15 14:35:32 +00:00
}
html = s.String()
return
}
2022-09-16 06:46:51 +00:00
func replacePage(r *regexp.Regexp, path string, page int) (src string) {
if page == 1 {
src = r.ReplaceAllString(path, "")
} else {
s := fmt.Sprintf("$1/%d", page)
src = r.ReplaceAllString(path, s)
}
src = strings.Replace(src, "//", "/", -1)
if src == "" {
src = "/"
}
return
}