-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
440 lines (354 loc) · 10.9 KB
/
main.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* Copyright 2015 Édouard Hue
*
* 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 main
import (
"bufio"
"bytes"
"cgt.name/pkg/go-mwclient"
"cgt.name/pkg/go-mwclient/params"
"encoding/csv"
"flag"
"github.com/jmcvetta/napping"
"io"
"io/ioutil"
"log"
"os"
"sort"
"strconv"
"strings"
"text/template"
)
/*
* Some nearly-constants.
*/
const APP_VERSION = "ProsperoBot/Collections/1.0"
const COMMONS_API_URL = "https://commons.wikimedia.org/w/api.php"
const WDQ_API_URL = "https://wdq.wmflabs.org/api"
const COMMONS_CAT_PROPERTY = "373"
const CATEGORY_NAMESPACE = "Category:"
/*
* Target data structure. Will be passed to the page template.
*/
type specimen struct {
OriginalName string // Scientific name from catalog column #0 (used when there is no Wikidata item)
VernacularName string // Folk name from catalog column #1
WikidataItemId string // From catalog column #7; item's nature should be taxon
CommonsCategoryName string // Retrieved from Wikidata item
FileCount int // Retrieved from Commons
SubCats int // Retrieved from Commons
SubCatsFileCounts int // Retrieved from Commons
TotalFiles int // Retrieved from Commons
Treatment string // From catalog columns #2 and #3
AccessionNumber string // From catalog column #6
SpecimenCategory string // From catalog column #8
}
/*
* Structures for Wikidata Query interaction
*/
type wdqStatus struct {
Error string `json:"error"`
Items int `json:"items"`
QueryTime string `json:"querytime"`
ParsedQuery string `json:"parsed_query"`
}
type wdqResult struct {
Status wdqStatus `json:"status"`
Items []int `json:"items"`
Props map[string][][]interface{} `json:"props"`
}
type wdqQuery struct {
q string
props string
}
/*
* Intermediary structure for Commons interaction
*/
type categoryInfo struct {
files int
subCats int
subCatsFiles int
}
/*
* Command line arguments
*/
var csvLocation string
var templateLocation string
var wikiApiUrl string
var pageTitle string
var sectionNumber string
var commons, wiki *mwclient.Client
func check(e error) {
if e != nil {
log.Fatal(e)
}
}
func initFlags() *flag.FlagSet {
set := flag.NewFlagSet("collections", flag.ExitOnError)
set.StringVar(&csvLocation, "f", "", "CSV file location.")
set.StringVar(&templateLocation, "t", "", "Template file location.")
set.StringVar(&wikiApiUrl, "w", "", "Wiki API URL.")
set.StringVar(&pageTitle, "p", "", "Page title.")
set.StringVar(§ionNumber, "s", "", "Section number.")
return set
}
func main() {
flags := initFlags()
check(flags.Parse(os.Args[1:]))
// Anonymous connection to Commons
var commonsErr error
commons, commonsErr = mwclient.New(COMMONS_API_URL, APP_VERSION)
check(commonsErr)
// Authenticated connection to the target wiki
var wikiErr error
wiki, wikiErr = mwclient.New(wikiApiUrl, APP_VERSION)
check(wikiErr)
login := os.Getenv("BOT_LOGIN")
password := os.Getenv("BOT_PASSWORD")
log.Printf("Will connect to %s with account %s\n", wikiApiUrl, login)
err := wiki.Login(login, password)
check(err)
// Open specimens channel
specimens := make(chan specimen)
// Read specimens
go readCsvFile(specimens)
// Receive specimens
c := make(chan int)
go queryWdq(specimens, c)
// Wait
<-c
log.Println("All done, logging out")
// Say goodbye
wiki.Logout()
}
// Read main CSV file and build incomplete items from it
func readCsvFile(specimens chan specimen) {
log.Printf("Opening catalog file %s\n", csvLocation)
f, err := os.Open(csvLocation)
check(err)
defer f.Close()
defer close(specimens)
br := bufio.NewReader(f)
r := csv.NewReader(br)
r.FieldsPerRecord = 9
for {
record, err := r.Read()
if err == io.EOF {
break
}
check(err)
specimen := specimen{
OriginalName: record[0],
VernacularName: record[1],
Treatment: record[2] + " / " + record[3],
AccessionNumber: record[6],
WikidataItemId: record[7],
SpecimenCategory: record[8],
}
specimens <- specimen
}
log.Println("Done reading catalog")
}
/*
* Buffer all item IDs,
* then query WDQ for Category names (P373),
* then query Commons for file counts,
* then merge information into items,
* then generate target wiki page and publish it.
*/
func queryWdq(specimens chan specimen, c chan int) {
// We will need to retrieve items from their Wikidata item id.
// We might have several items for the same Wikidata item.
specimensByWikidataId := make(map[string][]specimen)
// We will also need to retrieve categories by their name
categoryInfo := make(map[string]categoryInfo)
// Join all Wikidata item ids and build a lookup map in the same loop
var wikidataIds []string
for i := range specimens {
wikidataId := strings.TrimPrefix(i.WikidataItemId, "Q")
wikidataIds = append(wikidataIds, wikidataId)
specimensByWikidataId[wikidataId] = append(specimensByWikidataId[wikidataId], i)
}
query := napping.Params{
"q": "ITEMS[" + strings.Join(wikidataIds, ",") + "]",
"props": COMMONS_CAT_PROPERTY,
}
result := wdqResult{}
// Query WDQ
log.Printf("Querying WDQ with params %s\n", &query)
resp, err := napping.Get(WDQ_API_URL, &query, &result, nil)
check(err)
if resp.Status() == 200 {
if result.Status.Error != "OK" {
panic(result.Status.Error)
}
log.Println("Handling answer from WDQ")
// We build an array of all Commons category names, then slice it up in multiple Commons queries.
var categoryNames []string
// For Commons queries synchronisation
var commonsQueries []chan int
// Position in categoryNames
cursor := 0
// Loop on WDQ result
for i, itemProp := range result.Props[COMMONS_CAT_PROPERTY] {
itemId := int(itemProp[0].(float64))
categoryName := itemProp[2].(string)
// Set specimens Commons category name
itemSpecimens := specimensByWikidataId[strconv.Itoa(itemId)]
for i, specimen := range itemSpecimens {
specimen.CommonsCategoryName = CATEGORY_NAMESPACE + categoryName
itemSpecimens[i] = specimen
}
// Accumulate Commons category names and query when reaching API limit
categoryNames = append(categoryNames, CATEGORY_NAMESPACE+categoryName)
if i > 0 && i%50 == 0 {
cursor = i
q := make(chan int)
commonsQueries = append(commonsQueries, q)
go queryCommons(categoryInfo, categoryNames[i-50:i], q)
}
}
// Make the last query with remaining categories
q := make(chan int)
commonsQueries = append(commonsQueries, q)
go queryCommons(categoryInfo, categoryNames[cursor:], q)
// Wait for queries to terminate
for _, q := range commonsQueries {
<-q
}
} else {
panic(resp.Status)
}
// Now we can update specimens with Commons information
updatedSpecimens := make(chan specimen)
defer close(updatedSpecimens)
// Start page update routine
go updateWikiPage(updatedSpecimens, c)
for _, itemSpecimens := range specimensByWikidataId {
for _, specimen := range itemSpecimens {
// Lookup category information
thisCategoryInfo := categoryInfo[specimen.CommonsCategoryName]
// Update specimen
specimen.FileCount = thisCategoryInfo.files
specimen.SubCats = thisCategoryInfo.subCats
specimen.SubCatsFileCounts = thisCategoryInfo.subCatsFiles
specimen.TotalFiles = specimen.FileCount + specimen.SubCatsFileCounts
// Send it to page update routine
updatedSpecimens <- specimen
}
}
}
/*
* Query Commons for categoryinfo about a bunch of categories.
* If a category has subcategories, also query for each subcat's categoryinfo
*/
func queryCommons(categoryMembers map[string]categoryInfo, categoryNames []string, c chan int) {
defer close(c)
log.Printf("Querying Commons for %d categories\n", len(categoryNames))
parameters := params.Values{
"action": "query",
"prop": "categoryinfo",
"titles": strings.Join(categoryNames, "|"),
"continue": "",
"formatversion": "2",
}
q := commons.NewQuery(parameters)
for q.Next() {
resp := q.Resp()
pages, err := resp.GetObjectArray("query", "pages")
check(err)
for _, page := range pages {
title, err := page.GetString("title")
check(err)
thisCategoryInfo := categoryMembers[title]
files, err := page.GetInt64("categoryinfo", "files")
if err == nil {
thisCategoryInfo.files = int(files)
} else {
thisCategoryInfo.files = 0
}
subcats, err := page.GetInt64("categoryinfo", "subcats")
if err == nil {
thisCategoryInfo.subCats = int(subcats)
} else {
thisCategoryInfo.subCats = 0
}
if thisCategoryInfo.subCats > 0 {
thisCategoryInfo.subCatsFiles = queryCommonsSubcats(title)
}
categoryMembers[title] = thisCategoryInfo
}
}
}
/*
* Count files in one's category subcategories.
*/
func queryCommonsSubcats(categoryName string) int {
log.Printf("Querying Commons for subcategories of %s\n", categoryName)
parameters := params.Values{
"action": "query",
"prop": "categoryinfo",
"generator": "categorymembers",
"gcmtitle": categoryName,
"gcmtype": "subcat",
"continue": "",
"formatversion": "2",
}
totalFiles := 0
q := commons.NewQuery(parameters)
for q.Next() {
resp := q.Resp()
pages, err := resp.GetObjectArray("query", "pages")
check(err)
for _, page := range pages {
files, err := page.GetInt64("categoryinfo", "files")
if err == nil {
totalFiles += int(files)
}
}
}
return totalFiles
}
type byOriginalName []specimen
func (a byOriginalName) Len() int { return len(a) }
func (a byOriginalName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byOriginalName) Less(i, j int) bool { return a[i].OriginalName < a[j].OriginalName }
/*
* Generate a page section by merging the specimens in the provided template.
*/
func updateWikiPage(specimens chan specimen, c chan int) {
defer close(c)
var specimensBuffer []specimen
for specimen := range specimens {
specimensBuffer = append(specimensBuffer, specimen)
}
sort.Sort(byOriginalName(specimensBuffer))
log.Printf("About to update page %s", pageTitle)
templateBytes, err := ioutil.ReadFile(templateLocation)
check(err)
tableTemplate, err := template.New("table").Delims("¤{", "}¤").Parse(string(templateBytes))
check(err)
var buf bytes.Buffer
tableTemplate.Execute(&buf, specimensBuffer)
parameters := params.Values{
"title": pageTitle,
"section": sectionNumber,
"text": buf.String(),
"summary": "Mise à jour",
"notminor": "",
}
e := wiki.Edit(parameters)
check(e)
}