-
Notifications
You must be signed in to change notification settings - Fork 1
/
DCAT.php
704 lines (616 loc) · 21.2 KB
/
DCAT.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
<?php
/**
* DCAT-AP generation for Wikibase
*
* @author Lokal_Profil
* @licence MIT
*
*/
/**
* Validate that config is json and contains all necessary keys
*
* @param array $config json decoded config file
*/
function validateConfig( array $config ) {
// Later tests depend on these existing and being defined
$topBool = array( "api-enabled", "dumps-enabled" );
foreach ( $topBool as $val ) {
if ( !array_key_exists( $val, $config ) ) {
throw new Exception( "$val is missing from the config file" );
} elseif ( !is_bool( $config[$val] ) ) {
throw new Exception( "$val in the config file must be a boolean" );
}
}
// Always required
$top = array(
"directory", "uri", "themes", "keywords", "publisher",
"contactPoint", "ld-info", "catalog-license", "catalog-homepage",
"catalog-i18n", "catalog-issued"
);
$sub = array(
"publisher" => array( "publisherType", "homepage", "name", "email" ),
"contactPoint" => array( "vcardType", "name", "email" ),
"ld-info" => array( "accessURL", "mediatype", "license" )
);
// Dependent on topBool
if ( $config['api-enabled'] ) {
array_push( $top, "api-info" );
$sub["api-info"] = array( "accessURL", "mediatype", "license" );
}
if ( $config['dumps-enabled'] ) {
array_push( $top, "dump-info" );
$sub["dump-info"] = array(
"accessURL", "mediatype", "compression", "license"
);
}
// Test
foreach ( $top as $val ) {
if ( !array_key_exists( $val, $config ) ) {
throw new Exception( "$val is missing from the config file" );
}
}
foreach ( $sub as $key => $subArray ) {
foreach ( $subArray as $val ) {
if ( !array_key_exists( $val, $config[$key] ) ) {
throw new Exception(
$key . "[" . $val . "] is missing from the config file"
);
}
}
}
}
/**
* Load i18n files, local and remote, into an array
*
* @param array $langs array of langcode => filename
* @param array $config json decoded config file
* @return array: An i18n blob
*/
function makeI18nBlob( array $langs, array $config ) {
// load i18n files into i18n array
$i18n = array();
foreach ( $langs as $langCode => $filename ) {
$i18n[$langCode] = json_decode( file_get_contents( $filename ), true );
}
// load catalog i18n info from URL and add to i18n object
$i18nJSON = json_decode( file_get_contents( $config['catalog-i18n'] ), true );
if ( !isset( $i18nJSON ) ) {
throw new Exception(
"Could not read catalog-i18n. Are you sure " .
$config['catalog-i18n'] .
" exists and is valid json?"
);
}
foreach ( array_keys( $i18n ) as $langCode ) {
if ( array_key_exists( "$langCode-title", $i18nJSON ) ) {
$i18n[$langCode]['catalog-title'] = $i18nJSON["$langCode-title"];
}
if ( array_key_exists( "$langCode-description", $i18nJSON ) ) {
$i18n[$langCode]['catalog-description'] = $i18nJSON["$langCode-description"];
}
}
return $i18n;
}
/**
* Construct a data blob as an easy way of passing data around
*
* @param string $config path to config file
* @return array: A data blob
*/
function makeDataBlob( $config ) {
// Open config file and languages
$config = json_decode( file_get_contents( $config ), true );
validateConfig( $config );
// identify existing i18n files and load into array
$langs = array();
foreach ( glob( __DIR__ . '/i18n/*.json' ) as $filename ) {
if ( $filename !== __DIR__ . '/i18n/qqq.json' ) {
$langcode = substr( $filename,
strlen( __DIR__ . '/i18n/' ),
-strlen( '.json' ) );
$langs[$langcode] = $filename;
}
}
$i18n = makeI18nBlob( $langs, $config );
// hardcoded ids
$ids = array(
'publisher' => '_n42',
'contactPoint' => '_n43',
'dataset' => array(
'live' => 'liveData',
'dump' => 'dumpData',
),
'distribution' => array(
'ld' => 'liveDataLD',
'api' => 'liveDataAPI',
'dump' => 'dumpDist',
),
);
// stick loaded data into blob
$data = array(
'config' => $config,
'dumps' => null,
'i18n' => $i18n,
'ids' => $ids,
);
return $data;
}
/**
* Add additional data to a distribution entry when dealing with a dump.
* Complement to writeDistribution()
*
* @param XmlWriter $xml XML stream to write to
* @param array $dump the metadata on the dump being described
* @param string $accessURL the url prefix for the filename
*/
function dumpDistributionExtras( XMLWriter $xml, array $dump, $accessURL ) {
$url = str_replace(
'$1',
$dump['filename'],
$accessURL
);
$xml->startElementNS( 'dcat', 'accessURL', null );
$xml->writeAttributeNS( 'rdf', 'resource', null, $url );
$xml->endElement();
$xml->startElementNS( 'dcat', 'downloadURL', null );
$xml->writeAttributeNS( 'rdf', 'resource', null, $url );
$xml->endElement();
$xml->startElementNS( 'dcterms', 'issued', null );
$xml->writeAttributeNS( 'rdf', 'datatype', null,
'http://www.w3.org/2001/XMLSchema#date' );
$xml->text( $dump['timestamp'] );
$xml->endElement();
$xml->startElementNS( 'dcat', 'byteSize', null );
$xml->writeAttributeNS( 'rdf', 'datatype', null,
'http://www.w3.org/2001/XMLSchema#decimal' );
$xml->text( $dump['byteSize'] );
$xml->endElement();
}
/**
* Add i18n descriptions for a distribution
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
* @param string $prefix the type of distribution, one of 'ld', 'api' or 'dump'
* @param string $format the file format, only used for dumps
* @param string $compression the compression format, only used for dumps
*/
function writeDistributionI18n( XMLWriter $xml, array $data, $prefix,
$format, $compression ) {
foreach ( $data['i18n'] as $langCode => $langData ) {
if ( array_key_exists( "distribution-$prefix-description", $langData ) ) {
$formatDescription = $langData["distribution-$prefix-description"];
if ( $prefix === 'dump' ) {
$formatDescription = str_replace(
'$1',
$format,
$formatDescription
);
$formatDescription = str_replace(
'$2',
$compression,
$formatDescription
);
}
$xml->startElementNS( 'dcterms', 'description', null );
$xml->writeAttributeNS( 'xml', 'lang', null, $langCode );
$xml->text( $formatDescription );
$xml->endElement();
}
}
}
/**
* Construct distribution entry for each format in which a distribution
* is available. The DCAT-specification requires each format to be a
* separate distribution.
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
* @param string $prefix the type of distribution, one of 'ld', 'api' or 'dump'
* @param string|null $dumpDate the date of the dumpfile, null for live data
*/
function writeDistribution( XMLWriter $xml, array $data, $prefix, $dumpDate ) {
$ids = array();
$isDump = $prefix === 'dump';
$allowedMediatypes = $data['config']["$prefix-info"]['mediatype'];
$allowedCompressiontypes = array( '' => '' ); // dummy array for non-dumps
if ( $isDump ) {
$allowedCompressiontypes = $data['config']["$prefix-info"]['compression'];
}
foreach ( $allowedCompressiontypes as $compressionName => $compression ) {
foreach ( $allowedMediatypes as $format => $mediatype ) {
$distributionKey = $format . $compression;
// handle missing (and BETA) dump files
if ( $isDump and !array_key_exists( $distributionKey , $data['dumps'][$dumpDate] ) ) {
continue;
}
$id = $data['config']['uri'] . '#' .
$data['ids']['distribution'][$prefix] .
$dumpDate . $distributionKey;
array_push( $ids, $id );
$xml->startElementNS( 'rdf', 'Description', null );
$xml->writeAttributeNS( 'rdf', 'about', null, $id );
$xml->startElementNS( 'rdf', 'type', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://www.w3.org/ns/dcat#Distribution' );
$xml->endElement();
$xml->startElementNS( 'dcterms', 'license', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
$data['config']["$prefix-info"]['license'] );
$xml->endElement();
if ( !$isDump ) {
$xml->startElementNS( 'dcat', 'accessURL', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
$data['config']["$prefix-info"]['accessURL'] );
$xml->endElement();
} else {
dumpDistributionExtras( $xml,
$data['dumps'][$dumpDate][$distributionKey],
$data['config']['dump-info']['accessURL']
);
}
$xml->writeElementNS( 'dcterms', 'format', null, $mediatype );
// add description in each language
writeDistributionI18n( $xml, $data, $prefix, $format,
$compressionName );
$xml->endElement();
}
}
return $ids;
}
/**
* Add i18n title and description for a dataset
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
* @param string|null $dumpDate the date of the dumpfile, null for live data
* @param string $type 'dump' or 'live'
*/
function writeDatasetI18n( XMLWriter $xml, array $data, $dumpDate, $type ) {
foreach ( $data['i18n'] as $langCode => $langData ) {
if ( array_key_exists( "dataset-$type-title", $langData ) ) {
$xml->startElementNS( 'dcterms', 'title', null );
$xml->writeAttributeNS( 'xml', 'lang', null, $langCode );
if ( $type === 'live' ) {
$xml->text( $langData['dataset-live-title'] );
} else {
$xml->text(
str_replace( '$1', $dumpDate, $langData['dataset-dump-title'] )
);
}
$xml->endElement();
}
if ( array_key_exists( "dataset-$type-description", $langData ) ) {
$xml->startElementNS( 'dcterms', 'description', null );
$xml->writeAttributeNS( 'xml', 'lang', null, $langCode );
$xml->text( $langData["dataset-$type-description"] );
$xml->endElement();
}
}
}
/**
* Construct a dataset entry
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
* @param string|null $dumpDate the date of the dumpfile, null for live data
* @param array $distribution array of the distribution identifiers
*/
function writeDataset( XMLWriter $xml, array $data, $dumpDate, array $distribution ) {
$type = 'dump';
if ( is_null( $dumpDate ) ) {
$type = 'live';
}
$id = $data['config']['uri'] . '#' . $data['ids']['dataset'][$type] . $dumpDate;
$xml->startElementNS( 'rdf', 'Description', null );
$xml->writeAttributeNS( 'rdf', 'about', null, $id );
$xml->startElementNS( 'rdf', 'type', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://www.w3.org/ns/dcat#Dataset' );
$xml->endElement();
$xml->startElementNS( 'adms', 'contactPoint', null );
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['contactPoint'] );
$xml->endElement();
$xml->startElementNS( 'dcterms', 'publisher', null );
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['publisher'] );
$xml->endElement();
if ( is_null( $dumpDate ) ) {
$xml->startElementNS( 'dcterms', 'accrualPeriodicity', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://purl.org/cld/freq/continuous' );
$xml->endElement();
}
// add keywords
foreach ( $data['config']['keywords'] as $key => $keyword ) {
$xml->writeElementNS( 'dcat', 'keyword', null, $keyword );
}
// add themes
foreach ( $data['config']['themes'] as $key => $keyword ) {
$xml->startElementNS( 'dcat', 'theme', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
"http://eurovoc.europa.eu/$keyword" );
$xml->endElement();
}
// add title and description in each language
writeDatasetI18n( $xml, $data, $dumpDate, $type );
// add distributions
foreach ( $distribution as $key => $value ) {
$xml->startElementNS( 'dcat', 'distribution', null );
$xml->writeAttributeNS( 'rdf', 'resource', null, $value );
$xml->endElement();
}
$xml->endElement();
return $id;
}
/**
* Construct the publisher for the catalog and datasets
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
*/
function writePublisher( XMLWriter $xml, array $data ) {
$xml->startElementNS( 'rdf', 'Description', null );
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['publisher'] );
$xml->startElementNS( 'rdf', 'type', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://xmlns.com/foaf/0.1/Agent' );
$xml->endElement();
$xml->writeElementNS( 'foaf', 'name', null,
$data['config']['publisher']['name'] );
$xml->startElementNS( 'dcterms', 'type', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://purl.org/adms/publishertype/' .
$data['config']['publisher']['publisherType'] );
$xml->endElement();
$xml->writeElementNS( 'foaf', 'homepage', null,
$data['config']['publisher']['homepage'] );
$xml->startElementNS( 'vcard', 'hasEmail', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'mailto:' . $data['config']['publisher']['email'] );
$xml->endElement();
$xml->endElement();
}
/**
* Construct a contactPoint for the datasets
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
*/
function writeContactPoint( XMLWriter $xml, array $data ) {
$xml->startElementNS( 'rdf', 'Description', null );
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['contactPoint'] );
$xml->startElementNS( 'rdf', 'type', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://www.w3.org/2006/vcard/ns#' .
$data['config']['contactPoint']['vcardType'] );
$xml->endElement();
$xml->startElementNS( 'vcard', 'hasEmail', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'mailto:' . $data['config']['contactPoint']['email'] );
$xml->endElement();
$xml->writeElementNS( 'vcard', 'fn', null,
$data['config']['contactPoint']['name'] );
$xml->endElement();
}
/**
* Add language and i18n title and description for the catalog entry
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
*/
function writeCatalogI18n( XMLWriter $xml, array $data ) {
foreach ( $data['i18n'] as $langCode => $langData ) {
$xml->startElementNS( 'dcterms', 'language', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
"http://id.loc.gov/vocabulary/iso639-1/$langCode" );
$xml->endElement();
if ( array_key_exists( 'catalog-title', $langData ) ) {
$xml->startElementNS( 'dcterms', 'title', null );
$xml->writeAttributeNS( 'xml', 'lang', null, $langCode );
$xml->text( $langData['catalog-title'] );
$xml->endElement();
}
if ( array_key_exists( 'catalog-description', $langData ) ) {
$xml->startElementNS( 'dcterms', 'description', null );
$xml->writeAttributeNS( 'xml', 'lang', null, $langCode );
$xml->text( $langData['catalog-description'] );
$xml->endElement();
}
}
}
/**
* Construct the catalog entry
*
* @param XmlWriter $xml XML stream to write to
* @param array $data data-blob of i18n and config variables
* @param array $dataset array of the dataset identifiers
*/
function writeCatalog( XMLWriter $xml, array $data, array $dataset ) {
$xml->startElementNS( 'rdf', 'Description', null );
$xml->writeAttributeNS( 'rdf', 'about', null,
$data['config']['uri'] . '#catalog' );
$xml->startElementNS( 'rdf', 'type', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://www.w3.org/ns/dcat#Catalog' );
$xml->endElement();
$xml->startElementNS( 'dcterms', 'license', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
$data['config']['catalog-license'] );
$xml->endElement();
$xml->startElementNS( 'dcat', 'themeTaxonomy', null );
$xml->writeAttributeNS( 'rdf', 'resource', null,
'http://eurovoc.europa.eu/' );
$xml->endElement();
$xml->writeElementNS( 'foaf', 'homepage', null,
$data['config']['catalog-homepage'] );
$xml->startElementNS( 'dcterms', 'modified', null );
$xml->writeAttributeNS( 'rdf', 'datatype', null,
'http://www.w3.org/2001/XMLSchema#date' );
$xml->text( date( 'Y-m-d' ) );
$xml->endElement();
$xml->startElementNS( 'dcterms', 'issued', null );
$xml->writeAttributeNS( 'rdf', 'datatype', null,
'http://www.w3.org/2001/XMLSchema#date' );
$xml->text( $data['config']['catalog-issued'] );
$xml->endElement();
$xml->startElementNS( 'dcterms', 'publisher', null );
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['publisher'] );
$xml->endElement();
// add language, title and description in each language
writeCatalogI18n( $xml, $data );
// add datasets
foreach ( $dataset as $key => $value ) {
$xml->startElementNS( 'dcat', 'dataset', null );
$xml->writeAttributeNS( 'rdf', 'resource', null, $value );
$xml->endElement();
}
$xml->endElement();
}
/**
* Construct the whole DCAT-AP document given an array of dump info
*
* @param array $data data-blob of i18n and config variables
* @return string: xmldata
*/
function outputXml( array $data ) {
// Initializing the XML Object
$xml = new XmlWriter();
$xml->openMemory();
$xml->setIndent( true );
$xml->setIndentString( ' ' );
// set namespaces
$xml->startDocument( '1.0', 'UTF-8' );
$xml->startElementNS( 'rdf', 'RDF', null );
$xml->writeAttributeNS( 'xmlns', 'rdf', null,
'http://www.w3.org/1999/02/22-rdf-syntax-ns#' );
$xml->writeAttributeNS( 'xmlns', 'dcterms', null,
'http://purl.org/dc/terms/' );
$xml->writeAttributeNS( 'xmlns', 'dcat', null,
'http://www.w3.org/ns/dcat#' );
$xml->writeAttributeNS( 'xmlns', 'foaf', null,
'http://xmlns.com/foaf/0.1/' );
$xml->writeAttributeNS( 'xmlns', 'adms', null,
'http://www.w3.org/ns/adms#' );
$xml->writeAttributeNS( 'xmlns', 'vcard', null,
'http://www.w3.org/2006/vcard/ns#' );
// Calls previously declared functions to construct xml
writePublisher( $xml, $data );
writeContactPoint( $xml, $data );
$dataset = array();
// Live dataset and distributions
$liveDistribs = writeDistribution( $xml, $data, 'ld', null );
if ( $data['config']['api-enabled'] ) {
$liveDistribs = array_merge( $liveDistribs,
writeDistribution( $xml, $data, 'api', null )
);
}
array_push( $dataset,
writeDataset( $xml, $data, null, $liveDistribs )
);
// Dump dataset and distributions
if ( $data['config']['dumps-enabled'] ) {
foreach ( $data['dumps'] as $key => $value ) {
$distIds = writeDistribution( $xml, $data, 'dump', $key );
array_push( $dataset,
writeDataset( $xml, $data, $key, $distIds )
);
}
}
writeCatalog( $xml, $data, $dataset );
// Closing last XML node
$xml->endElement();
// Printing the XML
return $xml->outputMemory( true );
}
/**
* Given a dump directory produce array with data needed by outputXml()
*
* @param string $dirname directory name
* @param array $data data-blob of i18n and config variables
* @return array: of dumpdata, or empty array
*/
function scanDump( $dirname, array $data ) {
$testStrings = array();
foreach ( $data['config']['dump-info']['compression'] as $compression ) {
foreach ( $data['config']['dump-info']['mediatype'] as $format => $mediatype ) {
$testStrings["$format$compression"] = '-all.' . $format . '.' . $compression;
}
}
$dumps = array();
// each valid subdirectory has the form YYYYMMDD and refers to a timestamp
foreach ( glob( $dirname . '/[0-9]*', GLOB_ONLYDIR ) as $subdir ) {
// $subdir = testdirNew/20150120
$subDump = array();
foreach ( glob( $subdir . '/*' ) as $filename ) {
// match each file against an expected test string
foreach ( $testStrings as $fileEnding => $testString ) {
if ( substr( $filename, -strlen( $testString ) ) === $testString ) {
$info = stat( $filename );
$filename = substr( $filename, strlen( $dirname . '/' ) );
$subDump[$fileEnding] = array(
'timestamp' => gmdate( 'Y-m-d', $info['mtime'] ),
'byteSize' => $info['size'],
'filename' => $filename
);
}
}
}
// if files found then add to dumps
if ( count( $subDump ) > 0 ) {
$subdir = substr( $subdir, strlen( $dirname . '/' ) );
$dumps[$subdir] = $subDump;
}
}
return $dumps;
}
/**
* Scan dump directory for dump files (if any) and
* create dcatap.rdf in the same directory
*
* @param array $options command line options to override defaults
*/
function run( array $options ) {
// Load config variables and i18n a data blob
if ( !isset( $options['config'] ) ) {
$options['config'] = 'config.json';
}
if ( !is_file( $options['config'] ) ) {
throw new Exception( $options['config'] . " does not seem to exist" );
}
$data = makeDataBlob( $options['config'] );
// Load directories from config/options and test for existence
if ( !isset( $options['dumpDir'] ) ) {
$options['dumpDir'] = $data['config']['directory'];
}
if ( !is_dir( $options['dumpDir'] ) or !is_readable( $options['dumpDir'] ) ) {
throw new Exception(
$options['dumpDir'] . " is not a valid readable directory"
);
}
if ( !isset( $options['outputDir'] ) ) {
$options['outputDir'] = $data['config']['directory'];
}
if ( !is_dir( $options['outputDir'] ) or !is_writable( $options['outputDir'] ) ) {
throw new Exception(
$options['outputDir'] . " is not a valid writable directory"
);
}
// add dump data to data blob
$data['dumps'] = scanDump( $options['dumpDir'], $data );
// create xml string from data blob
$xml = outputXml( $data );
file_put_contents( $options['outputDir'] . "/dcatap.rdf", $xml );
}
// run from command-line with options
// Load options
$longOpts = array(
"config::", // Path to the config.json, default: config.json
"dumpDir::", // Path to the directory containing entity dumps, default: set in config
"outputDir::" // Path where dcat.rdf should be outputted, default: same as dumpDir
);
$options = getopt( '', $longOpts );
try {
run( $options );
} catch ( Exception $e ) {
die( $e->getMessage() );
}