-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.php
executable file
·441 lines (399 loc) · 13 KB
/
parser.php
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
#! /usr/bin/php
<?php
/**
* try to read the OAI interface output from a give DSpace instance
* and transform the DublinCore stuff into a JSON-LD as defined by the ODIS-Arch project
* https://book.oceaninfohub.org/thematics/docs/README.html
*
* @author Arno Lambert <[email protected]>
* @since 24/02/2021
*
*/
/*
* short arguments that can be given
-v : verbose, give more information on the console
-q : quiet, give no output on the console
-h : show the help
*/
$shortOptions = "vhq";
/*
* long arguments that can be given
*
--url (mandatory) url of the DSpace instance, like 'https://aquadocs.org'
--metadataPrefix (mandatory) metadataPrefix, like 'oai_dc'
--output (mandatory) output file with the complete path, like '/var/www/html/oih.aquadocs.org/aquadocs.json
--verbose give more information on the console
--quiet give no output on the console
--help show the help
*/
$longOptions = array(
"url:",
"metadataPrefix:",
"output:",
"verbose",
"quiet",
"help",
);
$options = getopt($shortOptions, $longOptions);
$helpMessage = <<<EOF
Usage:
/usr/local/bin/OIHDSpaceParser/parser.php /
[-v, --verbose] /
[-h, --help] /
[-q, --quiet] /
--url=https://aquadocs.org /
--metadataPrefix=oai_dc /
--output=/var/www/html/oih.aquadocs.org/aquadocs.json
Arguments:
--url (mandatory) url of the DSpace instance, like 'https://aquadocs.org'
--metadataPrefix (mandatory) metadataPrefix, like 'oai_dc'
--output (mandatory) output file with the complete path, like '/var/www/html/oih.aquadocs.org/aquadocs.json
--verbose, -v give more information on the console
--quiet, -q give no output on the console
--help, -h show this help
EOF;
if (isset($options['h'])
|| isset($options['help'])
) {
print $helpMessage;
exit(0);
}
$verbose = false;
if (isset($options['v'])
|| isset($options['verbose'])
) {
$verbose = true;
}
$quiet = false;
if (isset($options['q'])
|| isset($options['quiet'])
) {
//we cannot have both
$verbose = false;
$quiet = true;
}
if (isset($options['url'])) {
$url = $options['url'];
if (!preg_match('/^https?:\/\/.*\w+\.\w+/', $url)) {
print "\n\n**************ERROR**************\n";
print "we expected a url like 'https://aquadocs.org' or 'https://repository.oceanbestpractices.org' and got '$url'\n";
exit(1);
}
if (!preg_match('/\/$/', $url)) {
$url .= '/';
}
$originalURL = $url;
$repoURL = $url . 'oai/request?verb=ListRecords';
} else {
print "\n\n**************ERROR**************\n we really need a url (https://test.com) to continue\n\n";
print $helpMessage;
exit(1);
}
if (isset($options['metadataPrefix'])) {
$metadataPrefix = $options['metadataPrefix'];
if (!preg_match('/^[\w_\-]+$/', $metadataPrefix)) {
print "\n\n**************ERROR**************\n";
print "we expected a metadataPrefix like 'oai_dc' and got '$metadataPrefix'\n";
exit(1);
}
$repoURL .= '&metadataPrefix=' . $metadataPrefix;
} else {
print "\n\n**************ERROR**************\n we really need a metadataPrefix to continue\n\n";
print $helpMessage;
exit(1);
}
if (isset($options['output'])) {
$output = $options['output'];
$outputPath = '';
try {
$outputPath = dirname($output);
} catch (Exception $e) {
print "\n\n**************ERROR**************\n";
print "the path to the output file ('$outputPath') does not exist\n";
print $e->getMessage() . "\n";
exit(1);
}
if (!is_dir($outputPath)) {
print "\n\n**************ERROR**************\n";
print "the path to the output file ('$outputPath') does not exist\n";
exit(1);
}
if (!is_writable($outputPath)) {
print "\n\n**************ERROR**************\n";
print "the path to the output file ('$outputPath') is not writable\n";
exit(1);
}
if (file_exists($output)
&& !is_writable($output)
) {
print "\n\n**************ERROR**************\n";
print "the output file ('$output') is not writable\n";
exit(1);
}
} else {
print "\n\n**************ERROR**************\n we really need a output file name (--output=/complete/path/filename.ext) to continue\n\n";
print $helpMessage;
exit(1);
}
if ($verbose) {
print "we will use '$repoURL' to get the info\n";
print "the output json will be written to '$output'\n";
}
$nextRepoUrl = $repoURL;
//read the content of the page
$dom = new domDocument();
$lastPage = false;
$outputJSON = array();
$count = 0;
while ($nextRepoUrl != '') {
if ($verbose) {
print "\n*************************\n";
print "$nextRepoUrl \n";
}
/*
* get the complete page in one long string
* replace the dublin core tags with something without prefixes
*/
try {
$page = file_get_contents($nextRepoUrl);
$patterns = array(
'/<oai_dc:dc\s/',
'/<dc:/',
'/<\/oai_dc:dc/',
'/<\/dc:/',
);
$replace = array(
'<oai_dc_dc ',
'<dc_',
'</oai_dc_dc',
'</dc_',
);
$page = preg_replace($patterns, $replace, $page);
//file_put_contents('output.xml', $page);
$oai = new SimpleXMLElement($page);
if ($verbose) {
print $oai->responseDate;
}
foreach ($oai->ListRecords->record as $record) {
$count ++;
if ($verbose) {
print "\n*************************\n";
print $record->header->identifier . ' ' . $record->header->datestamp . "\n";
}
//subtract the handle id from the identifier
$identifier = $record->header->identifier;
$idSuffix = preg_replace(
'/.+?:(\d+\/\d+)/',
"$1",
$identifier
);
$handle = 'https://repository.oceanbestpractices.org/handle/' . $idSuffix;
//to what (sub)sets belongs this document
foreach ($record->header->setSpec as $set) {
if ($verbose) {
print $set . "\n";
}
}
//read all the metadata and put the values in vars ready for output
//we often use the same regex
$pattern = array(
"\n",
"\r",
"\t"
);
$replace = array(
' ',
' ',
' '
);
if (isset($record->header)
&& ($record->header->attributes() !== null)
&& isset($record->header->attributes()->status)
&& $record->header->attributes()->status == 'deleted'
) {
if ($verbose) {
print "$handle has been deleted, ignoring\n";
}
continue;
}
$metaData = $record->metadata->oai_dc_dc;
if (!is_object($metaData)) {
print "no info found for $handle\n";
continue;
}
$name = trim($metaData->dc_title);
$name = str_replace(
$pattern,
$replace,
$name
);
//get all the descriptions
//put them in one long description
$descriptions = array();
$descriptionString = '';
foreach ($metaData->dc_description as $description) {
$description = str_replace(
$pattern,
$replace,
trim($description)
);
$descriptions[] = ' - ' . $description . ' - ';
}
$descriptionString = implode(', ', $descriptions);
//this is the link to the document
$completeURL = $url . $idSuffix;
//get all the creators
$creators = array();
foreach ($metaData->dc_creator as $creator) {
$creator = str_replace(
$pattern,
$replace,
trim($creator)
);
$creators[] = $creator;
}
//get all the keywords
$keywords = array();
$keywordString = '';
foreach ($metaData->dc_subject as $keyword) {
$keyword = str_replace(
$pattern,
$replace,
trim($keyword)
);
$keywords[] = $keyword;
}
$keyword = implode(', ', $keywords);
//special case for contributor
$publisher = '';
if (isset($metaData->dc_publisher)) {
$publisher = $metaData->dc_publisher;
$publisher = str_replace(
$pattern,
$replace,
trim($publisher)
);
}
$JSON = array(
'@context' => array(
'@vocab' => 'https://schema.org/'
),
'@type' => 'CreativeWork',
'@id' => "$identifier",
'name' => "$name",
'description' => "$descriptionString",
'url' => "$completeURL",
'identifier' => array(
'@id' => "https://hdl.handle.net/$idSuffix",
'@type' => 'PropertyValue',
'propertyID' => 'https://hdl.handle.net/',
'value' => "$idSuffix",
'url' => "https://hdl.handle.net/$idSuffix"
)
);
/*
* authors/creators
* ODIS Arch expects
* 'author': {
* '@id': 'https://www.sample-data-repository.org/person/51317',
* '@type': 'Person',
* 'name': 'Dr Uta Passow',
* 'givenName': 'Uta',
* 'familyName': 'Passow',
* 'url': 'https://www.sample-data-repository.org/person/51317'
* },
*/
$authors = array();
if (isset($creators)
&& count($creators)
) {
foreach ($creators as $creator) {
array_push(
$authors,
array(
'@type' => 'Person',
'name' => "$creator"
)
);
}
}
if (count($authors)) {
$JSON['author'] = $authors;
}
$contributors = array();
//in OIH/ODIS Arch, publishers are seen as contributors
if (isset($publisher)
&& $publisher != ''
) {
array_push(
$contributors,
array(
'@type' => 'Organization',
'name' => "$publisher"
)
);
}
if (count($contributors)) {
$JSON['contributor'] = $contributors;
}
if (isset($keywords)
&& count($keywords)
) {
$JSON['keywords'] = $keywords;
}
$outputJSON[] = array(
'@type' => 'ListItem',
'item' => $JSON
);
}
//make the url for the next page if there is any
$nextRepoUrl = '';
if (isset($oai->ListRecords->resumptionToken)
&& $oai->ListRecords->resumptionToken != ''
) {
$nextRepoUrl = str_replace(
'metadataPrefix=oai_dc',
'resumptionToken=' . $oai->ListRecords->resumptionToken,
$repoURL
);
}
} catch (Exception $e) {
print 'there seems to be a problemo....' . $e->getMessage() . "\n";
exit(1);
}
}
if ($verbose) {
print "\n$count documents found and parsed\n";
}
$outputGraph = array(
'@context' => array(
'@vocab' => 'https://schema.org/'
),
'@type' => array(
'ItemList',
'CreativeWork'
),
'name' => 'Resource collection for ' . $originalURL,
'author' => array(
'@type' => 'Person',
'name' => 'Arno Lambert',
'sameAs' => array(
'https://oceanexpert.org/expert/35711',
'https://orcid.org/0000-0002-1859-1588'
)
),
'itemListOrder' => 'https://schema.org/ItemListUnordered',
'numberOfItems' => count($outputJSON),
'itemListElement' => $outputJSON
);
//finally save the file to be accessed
file_put_contents(
$output,
json_encode(
$outputGraph,
JSON_PRETTY_PRINT
)
);
if ($verbose) {
print "\n********--- done ---*******\n";
}