-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitem.go
381 lines (326 loc) · 10.9 KB
/
item.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// 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 wikibase
import (
"encoding"
"encoding/json"
"fmt"
"reflect"
"strings"
)
// ItemHeader must be embedded in all structs that are to be uploaded to Wikibase. If you give this embedded struct
// a JSON Tag then you can save and restore the Wikibase ID state for the entire struct, which can be used to avoid
// creating the item multiple times when you run.
//
// Each field that you want to sync as a property on the item in Wikibase must have a "property" tag, with the name
// of the label of a property on Wikibase. We use labels rather than P numbers as it's impossible to guarantee that
// production, staging, and test servers will use the same P numbers as they are allocated automatically by the
// Wikibase server; labels on the other hand can be managed by humans/bots. You should always call the client function
// MapPropertyAndItemConfiguration to populate it's internal map before attempting to create/update Items and their
// properties. If you add an "omitoncreate" clause then the Property will not be added to the item at create time,
// only later on during property sync.
type ItemHeader struct {
ID ItemPropertyType `json:"wikibase_id,omitempty"`
PropertyIDs map[string]string `json:"wikibase_property_ids,omitempty"`
}
type dataValue struct {
Type string `json:"type"`
Value interface{} `json:"value"`
}
type snakCreateInfo struct {
DataValue *dataValue `json:"datavalue"`
Property string `json:"property"`
SnakType string `json:"snaktype"`
}
type claimCreate struct {
MainSnak snakCreateInfo `json:"mainsnak"`
Rank string `json:"rank"`
Type string `json:"type"`
}
type itemCreateData struct {
Labels map[string]itemLabel `json:"labels"`
Claims []claimCreate `json:"claims"`
}
func getItemCreateClaimValue(f reflect.StructField, value reflect.Value) (*dataValue, error) {
full_type_name := fmt.Sprintf("%v", f.Type)
if value.Kind() == reflect.Ptr {
if value.IsNil() {
return nil, nil
} else {
value = value.Elem()
if full_type_name[0] != '*' {
return nil, fmt.Errorf("We expected a pointer type: %s", full_type_name)
}
full_type_name = full_type_name[1:]
}
}
data := dataValue{}
datatype, err := goTypeToWikibaseType(f)
if err != nil {
return nil, err
}
switch full_type_name {
case "time.Time":
m, ok := value.Interface().(encoding.TextMarshaler)
if !ok {
return nil, fmt.Errorf("time.Time does not respect JSON marshalling any more.")
}
b, berr := m.MarshalText()
if berr != nil {
return nil, berr
}
t, err := TimeDataClaimToAPIData(string(b))
if err != nil {
return nil, err
}
data.Value = &t
data.Type = datatype
case "string":
t, err := StringClaimToAPIData(value.String())
if err != nil {
return nil, err
}
if t == nil {
return nil, nil
}
data.Value = &t
data.Type = datatype
case "int":
t, err := QuantityClaimToAPIData(int(value.Int()))
if err != nil {
return nil, err
}
data.Value = &t
data.Type = datatype
case "wikibase.ItemPropertyType":
t, err := ItemClaimToAPIData(ItemPropertyType(value.String()))
if err != nil {
return nil, err
}
data.Value = &t
data.Type = "wikibase-entityid"
default:
return nil, fmt.Errorf("Tried to upload property of unrecognised type %s", full_type_name)
}
return &data, nil
}
// CreateItemInstance will take a pointer to a Go structure that has the embedded wikibase header and
// item and property tags on its fields and create a new item with the provided label. Any fields in the structure
// with a Property tag that does not contain the "omitoncreate" clause will also be created as item claims at the
// same time.
func (c *Client) CreateItemInstance(label string, i interface{}) error {
if len(label) == 0 {
return fmt.Errorf("Item label must not be an empty string.")
}
// Can we find the headers used to record bits?
v := reflect.ValueOf(i)
if v.Kind() != reflect.Ptr {
return fmt.Errorf("Expected a pointer to the item to upload, not %v", v.Kind())
}
s := v.Elem()
if s.Kind() != reflect.Struct {
return fmt.Errorf("Expected a struct for item to upload, got %v.", s.Kind())
}
header := s.FieldByName("ItemHeader")
if !header.IsValid() {
return fmt.Errorf("Expected struct to have item header")
}
// Are there any properties that we should create at this venture as part of initial
// upload?
claims := make([]claimCreate, 0)
t := s.Type()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
value := s.Field(i)
tag := f.Tag.Get("property")
if len(tag) > 0 {
// There may be multiple tags, the first one of which is the property name
parts := strings.Split(tag, ",")
tag = parts[0]
// if there's a omitoncreate then skip this
skiptag := false
for _, t := range parts[1:] {
if t == "omitoncreate" {
skiptag = true
break
}
}
if skiptag {
continue
}
property_id, ok := c.PropertyMap[tag]
if ok == false {
return fmt.Errorf("No property map for property label %s", tag)
}
claim, err := getItemCreateClaimValue(f, value)
if err != nil {
return fmt.Errorf("Failed to marshal %s during create: %v", property_id, err)
}
snaktype := "value"
if claim == nil {
snaktype = "novalue"
}
create := claimCreate{
MainSnak: snakCreateInfo{
DataValue: claim,
Property: property_id,
SnakType: snaktype,
},
Rank: "normal",
Type: "statement",
}
claims = append(claims, create)
}
}
labels := make(map[string]itemLabel, 0)
labels["en"] = itemLabel{Language: "en", Value: label}
item := itemCreateData{Labels: labels, Claims: claims}
b, berr := json.Marshal(&item)
if berr != nil {
return berr
}
// Having got things
editToken, terr := c.GetEditingToken()
if terr != nil {
return terr
}
response, err := c.client.Post(
map[string]string{
"action": "wbeditentity",
"token": editToken,
"new": "item",
"data": string(b),
},
)
if err != nil {
return err
}
defer response.Close()
var res itemEditResponse
err = json.NewDecoder(response).Decode(&res)
if err != nil {
return err
}
if res.Error != nil {
return res.Error
}
if res.Success != 1 {
return fmt.Errorf("We got an unexpected success value: %v", res)
}
if res.Entity == nil {
return fmt.Errorf("Unexpected response from server: %v", res)
}
// We now need to extract the ID and all the property IDs we created
id_field := header.FieldByName("ID")
if !id_field.IsValid() || id_field.Kind() != reflect.String {
return fmt.Errorf("Expected header to have string ID field")
}
if !id_field.CanSet() {
return fmt.Errorf("Expected item header to be mutable!")
}
id_field.SetString(string(res.Entity.ID))
// we need the map used to store property IDs
property_map_field := header.FieldByName("PropertyIDs")
if !property_map_field.IsValid() || property_map_field.Kind() != reflect.Map {
return fmt.Errorf("Expected header to have a property map")
}
if property_map_field.IsNil() {
property_map_field.Set(reflect.MakeMap(property_map_field.Type()))
}
for property, claims := range res.Entity.Claims {
// In theory there can be multiple claims per property, but we only support creating one at the moment
// so error if there's more than one
if len(claims) > 1 {
return fmt.Errorf("Unexpected list of claims for %s after we created just one: %v", property, claims)
} else if len(claims) == 1 {
property_map_field.SetMapIndex(reflect.ValueOf(property), reflect.ValueOf(claims[0].ID))
}
}
return nil
}
// UploadClaimsForItem will take a pointer to a Go structure that has the embedded wikibase header and
// item and property tags on its fields and set the claims on the item to match. The item must have been created
// already. If allow_refresh is set to true, all properties will be written, regardless of whether they've been
// uploaded before; if set to false only items with no existing Wikibase Property ID in the map will be updated.
func (c *Client) UploadClaimsForItem(i interface{}, allow_refresh bool) error {
// Can we find the headers used to record bits?
v := reflect.ValueOf(i)
if v.Kind() != reflect.Ptr {
return fmt.Errorf("Expected a pointer to the item to upload, not %v", v.Kind())
}
s := v.Elem()
if s.Kind() != reflect.Struct {
return fmt.Errorf("Expected a struct for item to upload, got %v.", s.Kind())
}
header := s.FieldByName("ItemHeader")
if !header.IsValid() {
return fmt.Errorf("Expected struct to have item header")
}
// Having got the header, get the item ID
id_field := header.FieldByName("ID")
if !id_field.IsValid() || id_field.Kind() != reflect.String {
return fmt.Errorf("Expected header to have string ID field")
}
item_id := ItemPropertyType(id_field.String())
if len(item_id) == 0 {
return fmt.Errorf("Item ID is nil in item")
}
// we need the map used to store property IDs
property_map_field := header.FieldByName("PropertyIDs")
if !property_map_field.IsValid() || property_map_field.Kind() != reflect.Map {
return fmt.Errorf("Expected header to have a property map")
}
if property_map_field.IsNil() {
property_map_field.Set(reflect.MakeMap(property_map_field.Type()))
}
t := s.Type()
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
value := s.Field(i)
tag := f.Tag.Get("property")
if len(tag) > 0 {
// There may be multiple tags, the first one of which is the property name
parts := strings.Split(tag, ",")
tag = parts[0]
property_id, ok := c.PropertyMap[tag]
if ok == false {
return fmt.Errorf("No property map for property label %s", tag)
}
// In future we should make this update the claim, but for now if we've set it once
// don't set it again
id_val := property_map_field.MapIndex(reflect.ValueOf(property_id))
have_existing_claim := false
if id_val.IsValid() && id_val.Kind() == reflect.String && len(id_val.String()) > 0 {
have_existing_claim = true
}
data, err := getDataForClaim(f, value)
if err != nil {
return fmt.Errorf("Failed to marshal %s on %s: %v", property_id, item_id, err)
}
if !have_existing_claim {
id, err := c.CreateClaimOnItem(item_id, property_id, data)
if err != nil {
return err
}
property_map_field.SetMapIndex(reflect.ValueOf(property_id), reflect.ValueOf(id))
} else if allow_refresh {
err := c.updateClaim(id_val.String(), data)
if err != nil {
return err
}
}
}
}
return nil
}