-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikibase.go
237 lines (193 loc) · 5.97 KB
/
wikibase.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2018 Content Mine Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package provides some mediawiki convenience functions along with a pseudo-ORM for creating wikibase items and their
// associated properties.
package wikibase
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"sync"
)
// The Wikibase/media wiki client struct. Create this with a call to NewClient, passing it a valid network
// client.
type Client struct {
client NetworkClientInterface
// Don't read directly - use GetEditingToken()
editToken *string
editTokenLock sync.RWMutex
// Mapping of labels to IDs for Items and Properties.
PropertyMap map[string]string
ItemMap map[string]ItemPropertyType
}
// NewClient is a factory method for creating a new Client object.
func NewClient(oauthClient NetworkClientInterface) *Client {
return &Client{
client: oauthClient,
PropertyMap: make(map[string]string, 0),
ItemMap: make(map[string]ItemPropertyType, 0),
}
}
// GetEditingToken returns an already acquired editing token for this session, or fetches a new one if necessary. This
// method is thread safe.
func (c *Client) GetEditingToken() (string, error) {
c.editTokenLock.RLock()
initVal := c.editToken
c.editTokenLock.RUnlock()
if initVal != nil {
return *initVal, nil
}
c.editTokenLock.Lock()
defer c.editTokenLock.Unlock()
// at start of day there's a big risk all go-routines race on getting
// the edit token, so bail early if someone else has won
if c.editToken != nil {
return *c.editToken, nil
}
response, err := c.client.Get(
map[string]string{
"action": "query",
"meta": "tokens",
},
)
if err != nil {
return "", err
}
defer response.Close()
var token tokenRequestResponse
err = json.NewDecoder(response).Decode(&token)
if err != nil {
return "", err
}
if token.Query.Tokens.CSRFToken == nil {
return "", fmt.Errorf("Failed to get token in response from server: %v", token)
}
c.editToken = token.Query.Tokens.CSRFToken
return *c.editToken, nil
}
func (c *Client) getWikibaseThingIDForLabel(thing WikiBaseType, label string) ([]string, error) {
response, err := c.client.Get(
map[string]string{
"action": "query",
"list": "wbsearch",
"wbssearch": label,
"wbstype": string(thing),
"wbslanguage": "en",
},
)
if err != nil {
return nil, err
}
defer response.Close()
var search searchQueryResponse
err = json.NewDecoder(response).Decode(&search)
if err != nil {
return nil, err
}
// the search will return close matches not actual matches potentially, so make sure we get exactly
// matches only
filtered_items := make([]string, 0)
for _, item := range search.Query.Items {
if item.DisplayText == label {
parts := strings.Split(item.Title, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("We expected type:value in reply, but got: %v", item.Title)
}
filtered_items = append(filtered_items, parts[1])
}
}
return filtered_items, nil
}
// FetchPropertyIDsForLabel will find Wikibase properties with the exact matching label and return them as a list of
// P numbers.
func (c *Client) FetchPropertyIDsForLabel(label string) ([]string, error) {
return c.getWikibaseThingIDForLabel(WikiBaseProperty, label)
}
// FetchPropertyIDsForLabel will find Wikibase items with the exact matching label and return them as a list of
// Q numbers.
func (c *Client) FetchItemIDsForLabel(label string) ([]string, error) {
return c.getWikibaseThingIDForLabel(WikiBaseItem, label)
}
// CreateOrUpdateArticle will create a new mediawiki page if necessary, and set its content to the provided body text.
// The body should be in wikitext format, or if your Mediawiki instance supports it, parsoidHTML.
func (c *Client) CreateOrUpdateArticle(title string, body string) (int, error) {
if len(title) == 0 {
return 0, fmt.Errorf("Article title must not be an empty string.")
}
editToken, terr := c.GetEditingToken()
if terr != nil {
return 0, terr
}
response, err := c.client.Post(
map[string]string{
"action": "edit",
"token": editToken,
"title": fmt.Sprintf("article:%s", title),
"text": body,
},
)
if err != nil {
return 0, err
}
defer response.Close()
var res articleEditResponse
err = json.NewDecoder(response).Decode(&res)
if err != nil {
return 0, err
}
if res.Error != nil {
return 0, res.Error
}
if res.Edit == nil {
return 0, fmt.Errorf("Unexpected response from server: %v", res)
}
return res.Edit.PageID, nil
}
func (c *Client) protectPage(key string, value string) error {
editToken, terr := c.GetEditingToken()
if terr != nil {
return terr
}
response, err := c.client.Post(
map[string]string{
"action": "protect",
"token": editToken,
key: value,
"protections": "edit=sysop",
"expiry": "never",
},
)
if err != nil {
return err
}
defer response.Close()
var res protectResponse
err = json.NewDecoder(response).Decode(&res)
if err != nil {
return err
}
if res.Error != nil {
return res.Error
}
return nil
}
// ProtectPageByTitle will attempt to set the edit protection on a page with the given title to admin. Will fail if page does not exist.
func (c *Client) ProtectPageByTitle(title string) error {
return c.protectPage("title", title)
}
// ProtectPageByID will attempt to set the edit protection on a page with the given title to admin. Will fail if page does not exist.
func (c *Client) ProtectPageByID(page_id int) error {
return c.protectPage("pageid", strconv.Itoa(page_id))
}