-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
ActorRdfMetadataExtractHydraControls.ts
168 lines (150 loc) 路 6.5 KB
/
ActorRdfMetadataExtractHydraControls.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
import type {
IActionRdfMetadataExtract,
IActorRdfMetadataExtractOutput,
IActorRdfMetadataExtractArgs,
} from '@comunica/bus-rdf-metadata-extract';
import { ActorRdfMetadataExtract } from '@comunica/bus-rdf-metadata-extract';
import type { IActorTest } from '@comunica/core';
import type * as RDF from '@rdfjs/types';
import type { UriTemplate } from 'uritemplate';
import { parse as parseUriTemplate } from 'uritemplate';
/**
* An RDF Metadata Extract Actor that extracts all Hydra controls from the metadata stream.
*/
export class ActorRdfMetadataExtractHydraControls extends ActorRdfMetadataExtract {
public static readonly HYDRA: string = 'http://www.w3.org/ns/hydra/core#';
public static readonly LINK_TYPES: string[] = [ 'first', 'next', 'previous', 'last' ];
protected readonly parsedUriTemplateCache: Record<string, UriTemplate> = {};
public constructor(args: IActorRdfMetadataExtractArgs) {
super(args);
}
public async test(_action: IActionRdfMetadataExtract): Promise<IActorTest> {
return true;
}
/**
* Collect all Hydra page links from the given Hydra properties object.
* @param pageUrl The page URL in which the Hydra properties are defined.
* @param hydraProperties The collected Hydra properties.
* @return The Hydra links
*/
public getLinks(pageUrl: string, hydraProperties: Record<string, Record<string, string[]>>):
Record<string, any> {
return Object.fromEntries(ActorRdfMetadataExtractHydraControls.LINK_TYPES.map((link) => {
// First check the correct hydra:next, then the deprecated hydra:nextPage
const links = hydraProperties[link] || hydraProperties[`${link}Page`];
const linkTargets = links && links[pageUrl];
return [ link, linkTargets && linkTargets.length > 0 ? [ linkTargets[0] ] : [] ];
}));
}
/**
* Parse a URI template, or retrieve it from a cache.
* @param {string} template A URI template string.
* @return {} A parsed URI template object.
*/
public parseUriTemplateCached(template: string): UriTemplate {
const cachedUriTemplate: UriTemplate = this.parsedUriTemplateCache[template];
if (cachedUriTemplate) {
return cachedUriTemplate;
}
return this.parsedUriTemplateCache[template] = parseUriTemplate(template);
}
/**
* Collect all search forms from the given Hydra properties object.
* @param hydraProperties The collected Hydra properties.
* @return The search forms.
*/
public getSearchForms(hydraProperties: Record<string, Record<string, string[]>>): ISearchForms {
const searchFormData: Record<string, string[]> = hydraProperties.search;
const searchForms: ISearchForm[] = [];
if (searchFormData) {
for (const dataset in searchFormData) {
for (const searchFormId of searchFormData[dataset]) {
const searchTemplates = (hydraProperties.template || {})[searchFormId] || [];
// Parse the template
if (searchTemplates.length !== 1) {
throw new Error(`Expected 1 hydra:template for ${searchFormId}`);
}
const template: string = searchTemplates[0];
const searchTemplate: UriTemplate = this.parseUriTemplateCached(template);
// Parse the template mappings
const mappings: Record<string, string> = Object
.fromEntries(((hydraProperties.mapping || {})[searchFormId] || [])
.map((mapping) => {
const variable = ((hydraProperties.variable || {})[mapping] || [])[0];
const property = ((hydraProperties.property || {})[mapping] || [])[0];
if (!variable) {
throw new Error(`Expected a hydra:variable for ${mapping}`);
}
if (!property) {
throw new Error(`Expected a hydra:property for ${mapping}`);
}
return [ property, variable ];
}));
// Gets the URL of the Triple Pattern Fragment with the given triple pattern
const getUri = (entries: Record<string, string>): string => searchTemplate
.expand(Object.fromEntries(Object.keys(entries).map(key => [ mappings[key], entries[key] ])));
searchForms.push({ dataset, template, mappings, getUri });
}
}
}
return { values: searchForms };
}
/**
* Collect all hydra properties from a given metadata stream
* in a nice convenient nested hash (property / subject / objects).
* @param {RDF.Stream} metadata
* @return The collected Hydra properties.
*/
public getHydraProperties(metadata: RDF.Stream): Promise<Record<string, Record<string, string[]>>> {
return new Promise((resolve, reject) => {
metadata.on('error', reject);
// Collect all hydra properties in a nice convenient nested hash (property / subject / objects).
const hydraProperties: Record<string, Record<string, string[]>> = {};
metadata.on('data', (quad) => {
if (quad.predicate.value.startsWith(ActorRdfMetadataExtractHydraControls.HYDRA)) {
const property = quad.predicate.value.slice(ActorRdfMetadataExtractHydraControls.HYDRA.length);
const subjectProperties = hydraProperties[property] || (hydraProperties[property] = {});
const objects = subjectProperties[quad.subject.value] || (subjectProperties[quad.subject.value] = []);
objects.push(quad.object.value);
}
});
metadata.on('end', () => resolve(hydraProperties));
});
}
public async run(action: IActionRdfMetadataExtract): Promise<IActorRdfMetadataExtractOutput> {
const metadata: IActorRdfMetadataExtractOutput['metadata'] = {};
const hydraProperties = await this.getHydraProperties(action.metadata);
Object.assign(metadata, this.getLinks(action.url, hydraProperties));
metadata.searchForms = this.getSearchForms(hydraProperties);
return { metadata };
}
}
export interface ISearchForm {
/**
* The dataset in which the search form is defined.
*/
dataset: string;
/**
* The URI template containing Hydra variables.
*/
template: string;
/**
* The mappings.
* With as keys the Hydra properties,
* and as values the Hydra variables
*/
mappings: Record<string, string>;
/**
* Instantiate a uri based on the given Hydra variable values.
* @param entries Entries with as keys Hydra properties,
* and as values Hydra variable values.
* @return {string} The instantiated URI
*/
getUri: (entries: Record<string, string>) => string;
}
export interface ISearchForms {
/**
* All available search forms.
*/
values: ISearchForm[];
}