/
graphdb.ts
441 lines (396 loc) · 12.9 KB
/
graphdb.ts
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
441
import fetch, {Headers, Response} from 'node-fetch';
import DatasetExt from 'rdf-ext/lib/Dataset';
import factory from 'rdf-ext';
import {URL} from 'url';
import querystring from 'querystring';
import {Quad, Quad_Object, Quad_Predicate} from 'rdf-js';
import {Writer} from 'n3';
import {
AllowedRegistrationDomainStore,
Registration,
RegistrationStore,
} from './registration.js';
import {DatasetStore, extractIris} from './dataset.js';
import {Rating, RatingStore} from './rate.js';
import http from 'node:http';
export type SparqlResult = {
results: {
bindings: Binding[];
};
};
export type Binding = {
[key: string]: {value: string};
};
/**
* GraphDB client that uses the REST API.
*
* @see https://triplestore.netwerkdigitaalerfgoed.nl/webapi
*/
export class GraphDbClient {
private token?: string;
private username?: string;
private password?: string;
private agent = new http.Agent({keepAlive: false});
constructor(private url: string, private repository: string) {
// Doesn't work with authentication: see https://github.com/Ontotext-AD/graphdb.js/issues/123
// const config = new graphdb.repository.RepositoryClientConfig()
// .setEndpoints([url])
// .setUsername(username)
// .setPass(password);
// this.repository = new graphdb.repository.RDFRepositoryClient(config);
}
public async authenticate(username: string, password: string) {
this.username = username;
this.password = password;
const response = await fetch(this.url + '/rest/login/' + this.username, {
method: 'POST',
headers: {'X-Graphdb-Password': this.password!},
});
if (!response.ok) {
throw Error(
'Could not authenticate username ' +
this.username +
' with GraphDB; got status code ' +
response.status
);
}
this.token = response.headers.get('Authorization')!;
}
public async request(options: {
method: string;
url?: string;
body?: string;
accept?: string;
contentType?: string;
}): Promise<Response> {
const headers = await this.getHeaders();
headers.set('Content-Type', options.contentType ?? 'application/x-trig');
if (options.accept) {
headers.set('Accept', options.accept);
}
const repositoryUrl =
this.url + '/repositories/' + this.repository + options.url;
const response = await fetch(repositoryUrl, {
agent: this.agent,
method: options.method,
headers: headers,
body: options.body,
});
if (
// 409 = `Auth token hash mismatch`, which occurs after GraphDB has restarted.
(response.status === 401 || response.status === 409) &&
this.username !== undefined &&
this.password !== undefined
) {
this.token = undefined;
// Retry original request.
await this.request(options);
}
if (!response.ok) {
console.error(
'HTTP error ' +
response.status +
' for ' +
options.method +
' ' +
repositoryUrl
);
}
return response;
}
public async query(query: string): Promise<SparqlResult> {
const response = await this.request({
method: 'GET',
url: '?' + querystring.stringify({query}),
accept: 'application/sparql-results+json',
});
return (await response.json()) as SparqlResult;
}
public async update(payload: string): Promise<void> {
const response = await this.request({
method: 'POST',
url: '/statements',
body: payload,
contentType: 'application/sparql-update',
});
if (!response.ok) {
console.error(
`${response.status} response for SPARQL update ${payload})`
);
}
}
private async getHeaders(): Promise<Headers> {
if (this.username === undefined || this.password === undefined) {
return new Headers();
}
if (this.token === undefined) {
await this.authenticate(this.username, this.password);
}
return new Headers({Authorization: this.token!});
}
}
export class GraphDbRegistrationStore implements RegistrationStore {
private readonly registrationsGraph =
'https://demo.netwerkdigitaalerfgoed.nl/registry/registrations';
constructor(private client: GraphDbClient) {}
async store(registration: Registration) {
const quads = [
this.registrationQuad(
registration,
factory.namedNode('http://schema.org/datePosted'),
factory.literal(
registration.datePosted.toISOString(),
factory.namedNode('http://www.w3.org/2001/XMLSchema#dateTime')
)
),
this.registrationQuad(
registration,
factory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
factory.namedNode('http://schema.org/EntryPoint')
),
this.registrationQuad(
registration,
factory.namedNode('http://schema.org/encoding'),
factory.namedNode('http://schema.org') // Currently the only vocabulary that we support.
),
...registration.datasets.flatMap(datasetIri => {
const datasetQuads = [
this.registrationQuad(
registration,
factory.namedNode('http://schema.org/about'),
factory.namedNode(datasetIri.toString())
),
factory.quad(
factory.namedNode(datasetIri.toString()),
factory.namedNode(
'http://www.w3.org/1999/02/22-rdf-syntax-ns#type'
),
factory.namedNode('http://schema.org/Dataset'),
factory.namedNode(this.registrationsGraph)
),
factory.quad(
factory.namedNode(datasetIri.toString()),
factory.namedNode('http://schema.org/subjectOf'),
factory.namedNode(registration.url.toString()),
factory.namedNode(this.registrationsGraph)
),
];
if (registration.dateRead !== undefined) {
datasetQuads.push(
factory.quad(
factory.namedNode(datasetIri.toString()),
factory.namedNode('http://schema.org/dateRead'),
factory.literal(
registration.dateRead.toISOString(),
factory.namedNode('http://www.w3.org/2001/XMLSchema#dateTime')
),
factory.namedNode(this.registrationsGraph)
)
);
}
return datasetQuads;
}),
];
if (registration.dateRead !== undefined) {
quads.push(
this.registrationQuad(
registration,
factory.namedNode('http://schema.org/dateRead'),
factory.literal(
registration.dateRead.toISOString(),
factory.namedNode('http://www.w3.org/2001/XMLSchema#dateTime')
)
)
);
}
if (registration.statusCode !== undefined) {
quads.push(
this.registrationQuad(
registration,
factory.namedNode('http://schema.org/status'),
factory.literal(
registration.statusCode.toString(),
factory.namedNode('http://www.w3.org/2001/XMLSchema#integer')
)
)
);
}
if (registration.validUntil !== undefined) {
quads.push(
this.registrationQuad(
registration,
factory.namedNode('http://schema.org/validUntil'),
factory.literal(
registration.validUntil.toISOString(),
factory.namedNode('http://www.w3.org/2001/XMLSchema#dateTime')
)
)
);
}
return new Promise((resolve, reject) => {
getWriter(quads).end(async (error, result) => {
try {
await this.client.request({
method: 'DELETE',
url:
'/statements?' +
querystring.stringify({
subj: '<' + registration.url.toString() + '>',
context: '<' + this.registrationsGraph + '>',
}),
});
await this.client.request({
method: 'POST',
url: '/statements',
body: result,
});
resolve(null);
} catch (e) {
reject(e);
}
});
});
}
async findRegistrationsReadBefore(date: Date): Promise<Registration[]> {
// Use STR(?dateRead) as a workaround for https://github.com/netwerk-digitaal-erfgoed/register/issues/45
const result = await this.client.query(`
PREFIX schema: <http://schema.org/>
SELECT ?s ?datePosted ?validUntil WHERE {
GRAPH <${this.registrationsGraph}> {
?s a schema:EntryPoint ;
schema:datePosted ?datePosted ;
schema:dateRead ?dateRead .
OPTIONAL { ?s schema:validUntil ?validUntil . }
FILTER (STR(?dateRead) < "${date.toISOString()}")
}
} GROUP BY ?s ?datePosted ?validUntil`);
return result.results.bindings.map(
binding =>
new Registration(
new URL(binding.s.value),
new Date(binding.datePosted.value),
binding.validUntil ? new Date(binding.validUntil.value) : undefined
)
);
}
private registrationQuad = (
registration: Registration,
predicate: Quad_Predicate,
object: Quad_Object
) =>
factory.quad(
factory.namedNode(registration.url.toString()),
predicate,
object,
factory.namedNode(this.registrationsGraph)
);
}
export class GraphDbAllowedRegistrationDomainStore
implements AllowedRegistrationDomainStore
{
constructor(
private readonly client: GraphDbClient,
private readonly allowedDomainNamesGraph = 'https://data.netwerkdigitaalerfgoed.nl/registry/allowed_domain_names'
) {}
async contains(...domainNames: Array<string>) {
const result = await this.client.query(`
SELECT * WHERE {
GRAPH <${this.allowedDomainNamesGraph}> {
?s <https://data.netwerkdigitaalerfgoed.nl/allowed_domain_names/def/domain_name> ?domainNames .
VALUES ?domainNames { ${domainNames
.map(domainName => `"${domainName}"`)
.join(' ')} }
}
}`);
return result.results.bindings.length > 0;
}
}
export class GraphDbDatasetStore implements DatasetStore {
constructor(private readonly client: GraphDbClient) {}
public async countDatasets(): Promise<number> {
const result = await this.client.query(`
PREFIX dcat: <http://www.w3.org/ns/dcat#>
SELECT (COUNT(?s) as ?count) WHERE {
?s a dcat:Dataset .
}`);
return parseInt(result.results.bindings[0].count.value);
}
public async countOrganisations(): Promise<number> {
const result = await this.client.query(`
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT (COUNT(DISTINCT(?publisher)) as ?count) WHERE {
?s a dcat:Dataset ;
dct:publisher ?publisher .
}`);
return parseInt(result.results.bindings[0].count.value);
}
/**
* Store the dataset using optimized graph replacement.
*
* @see https://graphdb.ontotext.com/documentation/standard/replace-graph.html
*/
public async store(datasets: DatasetExt[]) {
// Find each Dataset’s IRI.
for (const [iri, dataset] of [...extractIris(datasets)]) {
// Serialize requests: wait for each response before sending next request to prevent GraphDB from running OOM.
await this.storeDataset(dataset, iri);
}
}
private async storeDataset(dataset: DatasetExt, graphIri: URL) {
await new Promise((resolve, reject) => {
getWriter([...dataset.match(null, null, null, null)]).end(
async (error, result) => {
try {
resolve(
await this.client.request({
method: 'PUT',
url:
'/rdf-graphs/service?graph=' +
encodeURIComponent(graphIri.toString()),
body: result,
})
);
} catch (e) {
reject(e);
}
}
);
});
}
}
function getWriter(quads: Quad[]): Writer {
const writer = new Writer({format: 'application/x-trig'});
writer.addQuads(quads);
return writer;
}
export class GraphDbRatingStore implements RatingStore {
private readonly graph =
'https://data.netwerkdigitaalerfgoed.nl/registry/ratings';
constructor(private readonly client: GraphDbClient) {}
async store(datasetUri: URL, rating: Rating): Promise<void> {
await this.client.update(`
PREFIX schema: <http://schema.org/>
WITH <${this.graph}>
DELETE {
?dataset schema:contentRating ?rating .
?rating ?p ?o .
}
WHERE {
BIND(<${datasetUri}> as ?dataset)
?dataset schema:contentRating ?rating .
?rating ?p ?o .
};
WITH <${this.graph}>
INSERT {
<${datasetUri}> schema:contentRating [
schema:bestRating ${rating.bestRating} ;
schema:worstRating ${rating.worstRating} ;
schema:ratingValue ${rating.score} ;
schema:ratingExplanation "${rating.explanation}" ;
]
}
WHERE {}
`);
}
}