forked from Islandora-Collaboration-Group/islandora_json_ld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonld.module
219 lines (205 loc) · 7.53 KB
/
jsonld.module
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
<?php
/**
* Generates a valid JSON-LD object from metadata from the current page
*
* @param $object Islandora Object
*
* @return array|bool|false|string
*/
function jsonld_create_script_tag($object) {
if (!isset($object['MODS']) || !islandora_datastream_access(ISLANDORA_VIEW_OBJECTS, $object['MODS'])) {
return FALSE;
}
/**
*
* Start mapping fields, ultimately we want something along the lines of
* {
* "@context": "http://schema.org",
* "@type": "schema:Thesis",
* "schema:identifier": {
* "@type": "PropertyValue",
* "propertyID": "DOI",
* "value": "https://doi.org/10.6092/unibo/amsdottorato/2545"
* },
* "url": "http://example-repository.edu",
* "schema:name": "Example Thesis Title",
* "schema:inLanguage": "eng",
* "schema:inSupportOf": "Doctor of Philosophy, Information and Computer Sciences",
* "schema:author": {
* "@type": "schema:Person",
* "name": "Last, First",
* "givenName": "First",
* "familyName": "Last",
* "@id": "https://orcid.org/0000-0001-7224-6462"
* },
* "schema:sourceOrganization": {
* "@type": "schema:CollegeOrUniversity",
* "schema:name": "College Name"
* },
* "schema:datePublished": "1991",
* "schema:description": "Article abstract"
* }
*/
// Automatic creation using third party libraries such as easyRDF (included in D8 is not possible as RDF support is not available until CLAW(
$cModel = $object->models[0];
$jModel = getSchemaFromCModel($cModel);
$jsonLd = [];
if ($jModel) {
$jsonLd['@context'] = 'http://schema.org';
$jsonLd['@type'] = $jModel;
// Generate image if this one object has any
if (isset($object['TN'])) {
$resource_url = url("islandora/object/{$object->id}/datastream/TN/view", ['absolute' => TRUE]);
$jsonLd['image'] = $resource_url;
}
$mods = NULL;
if ($object['MODS'] instanceof DOMNode) {
$mods = simplexml_import_dom($object['MODS']);
}
else {
try {
//Load MODS data stream into a simpleXML object for easy traversal
$mods = simplexml_load_string($object['MODS']->content);
// Generate Identifiers
$jsonLd['@id'] = url("islandora/object/{$object->id}", ['absolute' => TRUE]);
$jsond_xp_indentifiers = variable_get('islandora_scholar_xpaths_identifiers', '//mods:mods[1]/mods:identifier[@type]');
$jsond_xp_indentifiers = $mods->xpath($jsond_xp_indentifiers);
$jsonLd_xp_abstract = variable_get('islandora_scholar_xpaths_abstract', '//mods:mods[1]/mods:abstract');
$jsonLd_xp_abstract = $mods->xpath($jsonLd_xp_abstract);
$jsonLd['description'] = (!empty($jsonLd_xp_abstract)) ? (string) end($jsonLd_xp_abstract) : '';
// Set identifier values
foreach ($jsond_xp_indentifiers as $jsond_indentifier) {
$attribute_name = $jsond_indentifier->attributes();
$jsonLd['identifier'][] = [
'@type' => 'PropertyValue',
'propertyID' => strval($attribute_name['type']),
'value' => strval($jsond_indentifier),
];
}
$jsonLd['url'] = $GLOBALS['base_root'] . request_uri();
// Get topics
$jsonLd_xp_topics = variable_get('islandora_scholar_xpaths_topics', '//mods:subject');
$jsonLd_xp_topics = $mods->xpath($jsonLd_xp_topics);
foreach ($jsonLd_xp_topics as $topic) {
if (isset($topic->topic) && !empty(strval($topic->topic))) {
$jsonLd['keywords'][] = strval($topic->topic);
}
}
// Keywords or tags used to describe this content. Multiple entries in a keywords list are typically delimited by commas.
if (!empty($jsonLd['keywords'])) {
$jsonLd['keywords'] = implode(",", $jsonLd['keywords']);
}
// Set language
// TODO get this from the object or a configration when available
$jsonLd['inLanguage'] = "eng";
// Set authors for this particular piece of work
$jsonLd['sourceOrganization'] = [
'@type' => 'CollegeOrUniversity',
'name' => variable_get('site_name'),
];
// Get title and subtitle
$title_results = $mods->xpath(variable_get('islandora_scholar_xpaths_title', '//mods:mods[1]/mods:titleInfo/mods:title'));
$title = (string) reset($title_results);
$subtitle_results = $mods->xpath(variable_get('islandora_scholar_xpaths_title_sub_title', '//mods:mods[1]/mods:titleInfo/mods:subTitle'));
$subtitle = (string) reset($subtitle_results);
if (!empty($title)) {
if (!empty($subtitle)) {
$jsonLd['name'] = "{$title}: {$subtitle}";
}
else {
$jsonLd['name'] = $title;
}
// Set headline
$jsonLd['headline'] = $jsonLd['name'];
}
/**
* Date in which this particular publication was published
*
* @see islandora_google_scholar:islandora_google_scholar_create_meta_tags
**/
$tags = islandora_google_scholar_create_meta_tags($object);
if ($tags) {
$jsonLd['datePublished'] = $tags['citation_publication_date'];
foreach ($tags['citation_author'] as $author) {
$author_parts = explode(', ', $author);
$jsonLd['author'][] = [
'name' => $author,
'givenName' => (isset($author_parts[1]) ? $author_parts[1] : ''),
'familyName' => (isset($author_parts[0]) ? $author_parts[0] : ''),
'@type' => 'Person',
];
}
}
return json_encode($jsonLd, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_NUMERIC_CHECK);
} catch (Exception $e) {
watchdog('jsond', 'Got exception while parsing. Message: !msg Errors: !error', [
'!msg' => $e->getMessage(),
'!error' => libxml_get_errors(),
]);
return [];
}
}
}
return FALSE;
}
/**
* Maps Islandora entities/models
*
* @param $model
*
* @return bool|mixed
*/
function getSchemaFromCModel($model) {
$mapping = [
'ir:thesisCModel' => 'Thesis',
'ir:citationCModel' => 'ScholarlyArticle',
'islandora:sp_basic_image' => 'ImageObject',
'islandora:sp_large_image_cmodel' => 'ImageObject',
'islandora:sp_pdf' => 'DigitalDocument',
'islandora:sp-audioCModel' => 'AudioObject',
'islandora:sp_videoCModel' => 'VideoObject',
'islandora:bookCModel' => 'Book',
'islandora:newspaperCModel' => 'Newspaper',
'islandora:eventCModel' => 'Event',
'islandora:placeCModel' => 'Place',
'islandora:personCModel' => 'Person',
'islandora:organizationCModel' => 'CollegeorUniversity',
'islandora:sp_disk_image' => 'Dataset',
'islandora:sp_web_archive' => 'WebPage',
];
if (isset($mapping[$model])) {
return $mapping[$model];
}
else {
return FALSE;
}
}
/**
* Add JSON-LD scripts to pages
*
* @param $_script
*/
function jsonld_embed_scripts($_script) {
// Get islandora mappings
module_load_include('inc', 'islandora', 'includes/mimetype.utils');
$mimetype = islandora_mime_mapping();
// Generate script to hold the JSON data
$script = [
'#tag' => 'script',
'#attributes' => [
'type' => $mimetype['jsonld'],
],
'#value' => $_script,
];
drupal_add_html_head($script, 'script');
}
/**
* Implements hook_CMODEL_islandora_view_object().
*/
function jsonld_islandora_view_object($object) {
$tags = jsonld_create_script_tag($object);
// Only display JSON-D metadata if the user has a valid data stream and the user has the right permissions
if ($tags) {
jsonld_embed_scripts($tags);
}
}