-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqueryService.js
312 lines (265 loc) · 9.79 KB
/
queryService.js
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
"use strict"
let queryService = (function () {
let my = {}
let callback = null
let root = ""
let oldRoots = []
let rootDetails = []
let currentTree = {}
let currentTreeDepth = 0
let functionQueue = []
const queryLimit = 10
let numQueries = 0
my.setCallback = function(newCallback) { callback = newCallback }
my.setRoot = function(newRoot) {
if (typeof newRoot === 'undefined')
return
if (callback === null) {
console.log("ERROR: No callback function defined.")
}
const bodyEl = document.querySelector('body');
bodyEl.style.cursor = 'progress';
if (root != "")
{
oldRoots.push(root)
}
root = newRoot
rootDetails = []
functionQueue.push([getRootDetails, undefined])
executeQueue()
functionQueue.push([constructTree, undefined])
currentTree = {}
currentTreeDepth = 0
executeQueue()
}
my.back = function ()
{
if (oldRoots.length != 0)
{
root = ""
my.setRoot(oldRoots.pop())
}
}
function constructTree() {
if (currentTreeDepth === 0)
{
functionQueue.push([getWikidata, undefined])
functionQueue.push([completeTree, undefined])
}
executeQueue()
}
function getWikipediaExtract(entity)
{
// TODO: do this using the fetch API
$.getJSON("https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&props=sitelinks&sitefilter=enwiki&ids=" + entity.split(":")[1] + "&callback=?", function(data){
let title = data.entities[entity.split(":")[1]].sitelinks.enwiki.title
$.getJSON("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=" + title + "&callback=?", function(data){
let pages = data.query.pages
rootDetails["extract"] = pages[Object.keys(pages)[0]].extract
})
})
}
function getWikidata(entity=root) {
const query = constructQueryPropsAndObjects(entity, queryLimit)
// TODO: do this using the fetch API
let httpRequest = new XMLHttpRequest()
httpRequest.addEventListener("load", () => { parseResponse(httpRequest.responseText) })
httpRequest.open(
"GET",
"https://query.wikidata.org/sparql?query=" + query + "&format=json", true)
httpRequest.send()
numQueries++
}
function getRootDetails(entity=root)
{
const queryRootDetails = constructQueryRootDetails(entity)
// TODO: do this using the fetch API
let httpRequest = new XMLHttpRequest()
httpRequest.addEventListener("load", () => {
let res = JSON.parse(httpRequest.responseText)
let contextNode = d3.select("#context").node()
rootDetails["label"] = res.results.bindings[0].entityLabel.value
if (typeof res.results.bindings[0]["desc"] !== 'undefined')
{
rootDetails["desc"] = res.results.bindings[0].desc.value
}
if (typeof res.results.bindings[0]["pic"] !== 'undefined')
{
let filename = res.results.bindings[0].pic.value.split("/")
// rootDetails["imageFilename"] = filename[filename.length - 1]
getPicture(filename[filename.length - 1])
getWikipediaExtract(entity)
}
// context.html = res.results.bindings[0].entityLabel.value + res.results.bindings[0].pic.value
})
httpRequest.open(
"GET",
"https://query.wikidata.org/sparql?query=" + queryRootDetails + "&format=json", true)
httpRequest.send()
}
function getPicture(filename)
{
let url = decodeURI(filename)
let str = url.replace(/ /g, '_')
let md5 = $.md5(str)
let imageUrl = "https://upload.wikimedia.org/wikipedia/commons/"+ md5[0] + "/" + md5[0] + md5[1] + "/" + str
rootDetails["imageUrl"] = imageUrl
}
function constructQueryRootDetails(entity)
{
const query =
"SELECT ?entityLabel ?entityDescription ?pic ?desc " +
"WHERE { " +
entity + " rdfs:label ?entityLabel . " +
"OPTIONAL { " + entity + " wdt:P18 ?pic. " + " } " +
"SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". " + entity + " schema:description ?desc . } " +
"FILTER (LANG(?entityLabel) = 'en') . }" +
"LIMIT 10 "
return query
}
function constructQueryPropsAndObjects(entity, limit = 10) {
// Queries all properties associated entities of the given entity.
// See: https://stackoverflow.com/questions/25100224/how-to-get-a-list-of-all-wikidata-properties
// Also: https://query.wikidata.org/ example: data of douglas adams
const query =
"SELECT ?entityLabel ?prop ?propLabel ?object ?objectLabel " +
"WHERE { " +
entity + " ?propUrl ?object . " + // get all propertyUrls and objects of entity
"?prop ?ref ?propUrl . " + // get property referred to by url
"?prop rdf:type wikibase:Property . " + // restrict to wikibases properties
"?object rdfs:label ?objectLabel . " + // get label of objects "
entity + " rdfs:label ?entityLabel . " + // get label of our entity
"SERVICE wikibase:label { bd:serviceParam wikibase:language \"en\". } " +
"FILTER (LANG(?objectLabel) = 'en') . " +
"FILTER (LANG(?entityLabel) = 'en') . " +
"FILTER (?propUrl != wdt:P1963) . " + // specifically exclude "properties for this type"-property
"} " +
"LIMIT " + limit
// const query =
// "PREFIX entity: <http://www.wikidata.org/entity/> " +
// "SELECT ?propUrl ?propLabel ?valUrl ?valLabel ?picture " +
// "WHERE { " +
// "hint:Query hint:optimizer 'None' . " +
// "{BIND(entity:Q42 AS ?valUrl) . " +
// "BIND(\"N/A\" AS ?propUrl ) . " +
// "BIND(\"identity\"@en AS ?propLabel ) . " +
// "} " +
// "UNION " +
// "{entity:Q42 ?propUrl ?valUrl . " +
// "?property ?ref ?propUrl . " +
// "?property rdf:type wikibase:Property . " +
// "?property rdfs:label ?propLabel} " +
// "?valUrl rdfs:label ?valLabel " +
// "FILTER (LANG(?valLabel) = 'en') . " +
// "OPTIONAL{ ?valUrl wdt:P18 ?picture .} " +
// "FILTER (lang(?propLabel) = 'en' )} " +
// "ORDER BY ?propUrl ?valUrl " +
// "LIMIT 50"
// console.log(query)
return query
}
function parseResponse(res) {
// console.log("Parsing response..")
const response = JSON.parse(res)
const results = response.results.bindings
const tree = {}
tree.name = results[0].entityLabel.value
tree.children = []
let propNames = new Set()
for (let i = 0; i < results.length; i++)
{
const objUrlParts = results[i].object.value.split("/")
const propLabel = results[i].propLabel.value
if (propNames.has(propLabel))
{ // if the property is already a child, push object to its children
let cont = false
for (let j = 0; j < tree.children.length; j++)
{
if (tree.children[j].name === propLabel)
{
tree.children[j].children.push( { "name": results[i].objectLabel.value,
"children": [],
"parent": tree.children[j],
"prop": propLabel,
"obj": "wd:" + objUrlParts[objUrlParts.length -1],
} )
break
}
}
}
else
{ // if the property is not a child yet, push it to the tree's children
let newProp = { "name": propLabel,
"children": [],
"parent": tree,
"prop": null,
"obj": null
}
newProp.children.push( { "name": results[i].objectLabel.value,
"children": [],
"parent": newProp,
"prop": propLabel,
"obj": "wd:" + objUrlParts[objUrlParts.length -1],
})
tree.children.push( newProp )
propNames.add(propLabel)
}
}
addSubtree(tree)
executeQueue()
}
function addSubtree(subtree) {
if (currentTreeDepth === 0) {
currentTree = subtree
currentTreeDepth++
}
else {
// find leave by name
let node = findEmptyNodeWithName(currentTree, subtree.name)
node.children = node.children.concat(subtree.children)
}
}
function findEmptyNodeWithName(tree, name) {
// breadth first search for node without children and passed name
let queue = tree.children
let max = queue.length, i = 0
while (i < max)
{
let node = queue[i]
if (node.children.length === 0 && node.name === name) { return node }
queue = queue.concat(node.children)
max += node.children.length
i++
}
}
function completeTree() {
let queue = currentTree.children
let max = queue.length, i = 0
// find remaining empty nodes
while (i < max)
{
let node = queue[i]
if (node.children.length === 0) { functionQueue.push([getWikidata, node.obj]) }
queue = queue.concat(node.children)
max += node.children.length
i++
}
executeQueue()
}
function executeQueue() {
if (functionQueue.length > 0)
{
let tuple = functionQueue.shift();
(tuple[0])(tuple[1])
}
else
{
console.log("queryService is done!")
console.log(currentTree)
console.log("number of queries: " + numQueries)
numQueries = 0
callback(currentTree, rootDetails, true)
}
}
return my
}())