-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquery.go
More file actions
132 lines (109 loc) · 4.41 KB
/
Copy pathquery.go
File metadata and controls
132 lines (109 loc) · 4.41 KB
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
package mediawiki
import (
"strings"
"time"
)
// This contains structs that are used as response building blocks for multiple API commands.
type QueryResponse struct {
CoreResponse
BatchComplete any `json:"batchcomplete"`
}
type QueryResponseNormalized struct {
Fromencoded bool `json:"fromencoded,omitempty"`
From string `json:"from"`
To string `json:"to"`
}
type QueryResponseQuery struct {
Normalized []QueryResponseNormalized `json:"normalized,omitempty"`
Pages []QueryResponseQueryPage `json:"pages"`
Tokens map[string]string `json:"tokens,omitempty"`
}
type QueryResponseQueryPage struct {
PageId int `json:"pageid,omitempty"`
Namespace Namespace `json:"ns"`
Title string `json:"title"`
Revisions []QueryResponseQueryPageRevision `json:"revisions,omitempty"`
Missing any `json:"missing,omitempty"`
CategoryInfo map[string]int `json:"categoryinfo,omitempty"`
Contentmodel string `json:"contentmodel,omitempty"`
Pagelanguage string `json:"pagelanguage,omitempty"`
Pagelanguagehtmlcode string `json:"pagelanguagehtmlcode,omitempty"`
Pagelanguagedir string `json:"pagelanguagedir,omitempty"`
Touched *time.Time `json:"touched,omitempty"`
Lastrevid int `json:"lastrevid,omitempty"`
Length int `json:"length,omitempty"`
}
type QueryResponseQueryPageRevision struct {
RevId int `json:"revid,omitempty"`
ParentId int `json:"parentid"`
User string `json:"user,omitempty"`
Timestamp *time.Time `json:"timestamp,omitempty"`
Comment string `json:"comment"`
Slots map[string]QueryResponseQueryPageRevisionSlot `json:"slots,omitempty"`
}
type QueryResponseQueryPageRevisionSlot struct {
Content string `json:"content"`
ContentModel string `json:"contentmodel"`
ContentFormat string `json:"contentformat"`
}
// Namespace and full page title (including all subpage levels).
func (p QueryResponseQueryPage) FullPageName() string {
return strings.ReplaceAll(p.Title, "_", " ")
}
// Full page title (including all subpage levels) without the namespace.
func (p QueryResponseQueryPage) PageName() string {
title := strings.ReplaceAll(p.Title, "_", " ")
if p.Namespace == NamespaceMain {
return title
}
if i := strings.Index(title, ":"); i >= 0 {
return title[i+1:]
}
return title
}
// Page title of the page in the immediately superior subpage level without the namespace. Would return Title/Foo on page Help:Title/Foo/Bar.
func (p QueryResponseQueryPage) BasePageName() string {
name := p.PageName()
split := strings.Split(name, "/")
if len(split) == 1 {
return name
}
return strings.Join(split[:len(split)-1], "/")
}
// Name of the root of the current page. Would return Title on page Help:Title/Foo/Bar.
func (p QueryResponseQueryPage) RootPageName() string {
name := p.PageName()
split := strings.Split(name, "/")
return split[0]
}
// The subpage title. Would return Bar on page Help:Title/Foo/Bar. If no subpage exists the value of PageName() is returned.
func (p QueryResponseQueryPage) SubPageName() string {
name := p.PageName()
split := strings.Split(name, "/")
return split[len(split)-1]
}
// Full page name of the associated subject (e.g. article or file). Useful on talk pages.
func (p QueryResponseQueryPage) ArticlePageName() string {
title := strings.ReplaceAll(p.Title, "_", " ")
switch {
case p.Namespace%2 == 0:
return title
case p.Namespace == NamespaceTalk:
return strings.Replace(title, "Talk:", "", 1)
default:
return strings.Replace(title, " talk:", ":", 1)
}
}
// Full page name of the associated talk page.
func (p QueryResponseQueryPage) TalkPageName() string {
title := strings.ReplaceAll(p.Title, "_", " ")
switch {
case p.Namespace%2 == 1:
return title
case p.Namespace == 0:
return "Talk:" + title
default:
return strings.Replace(title, ":", " talk:", 1)
}
}
type QueryOption func(map[string]string)