-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcategory.go
173 lines (138 loc) · 3.72 KB
/
category.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package wordpress
import (
"go.opencensus.io/trace"
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/elgris/sqrl"
"golang.org/x/net/context"
"strings"
)
// Category represents a WordPress category
type Category struct {
Term
Link string `json:"url"`
}
// MarshalJSON marshals itself into json
func (cat *Category) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"id": cat.Id,
"parent": cat.Parent,
"name": cat.Name,
"url": cat.Link})
}
// GetChildId returns the category id of the child looked up by it's slug
func (cat *Category) GetChildId(c context.Context, slug string) (int64, error) {
c, span := trace.StartSpan(c, "/wordpress.Category.GetChildId")
defer span.End()
stmt, args, err := sqrl.Select("term_id").
From(table(c, "terms") + " AS t").
Join(table(c, "term_taxonomy") + " AS tt ON t.term_id = tt.term_id").
Where(sqrl.Eq{"tt.parent": cat.Id, "t.slug": slug}).ToSql()
if err != nil {
return 0, err
}
span.AddAttributes(trace.StringAttribute("wp/query", stmt))
var id int64
if err := database(c).QueryRow(stmt, args...).Scan(&id); err != nil && err != sql.ErrNoRows {
return 0, nil
}
return id, nil
}
// GetChildrenIds returns all the ids of the category and it's children
func (cat *Category) GetChildrenIds(c context.Context) ([]int64, error) {
c, span := trace.StartSpan(c, "/wordpress.Category.GetChildrenIds")
defer span.End()
ret := []int64{cat.Id}
ids := ret[:]
for len(ids) > 0 {
stmt, args, err := sqrl.Select("term_id").
From(table(c, "term_taxonomy")).
Where(sqrl.Eq{"parent": ids}).ToSql()
if err != nil {
return nil, err
}
rows, err := database(c).Query(stmt, args...)
if err != nil {
return nil, err
}
ids = nil
for rows.Next() {
var id int64
if err := rows.Scan(&id); err != nil {
return nil, err
}
ids = append(ids, id)
}
ret = append(ret, ids...)
}
return ret, nil
}
// GetCategoryIdBySlug returns the id of the category that matches the given slug
func GetCategoryIdBySlug(c context.Context, slug string) (int64, error) {
c, span := trace.StartSpan(c, "/wordpress.GetCategoryIdBySlug")
defer span.End()
parts := strings.Split(slug, "/")
var catId int64
for _, part := range parts {
it, err := queryTerms(c, &TermQueryOptions{
Taxonomy: TaxonomyCategory,
Slug: part,
ParentId: catId})
if err != nil {
return 0, err
}
if catId, err = it.Next(); err != nil {
return 0, errors.New("wordpress: non-existent category slug")
}
}
return catId, nil
}
// GetCategories gets all category data from the database
func GetCategories(c context.Context, categoryIds ...int64) ([]*Category, error) {
c, span := trace.StartSpan(c, "/wordpress.GetCategories")
defer span.End()
if len(categoryIds) == 0 {
return []*Category{}, nil
}
ids, idMap := dedupe(categoryIds)
terms, err := getTerms(c, ids...)
if err != nil {
return nil, err
}
counter := 0
done := make(chan error)
ret := make([]*Category, len(categoryIds))
for _, term := range terms {
cat := Category{Term: *term}
if cat.Parent > 0 {
counter++
go func() {
parents, err := GetCategories(c, cat.Parent)
if err != nil {
done <- fmt.Errorf("failed to get parent category for %d: %d\n%v", cat.Id, cat.Parent, err)
return
}
if len(parents) == 0 {
done <- fmt.Errorf("parent category for %d not found: %d", cat.Id, cat.Parent)
return
}
cat.Link = parents[0].Link + "/" + cat.Slug
done <- nil
}()
} else {
cat.Link = "/category/" + cat.Slug
}
// insert into return set
for _, index := range idMap[cat.Id] {
ret[index] = &cat
}
}
for ; counter > 0; counter-- {
if err := <-done; err != nil {
return nil, err
}
}
return ret, nil
}