Skip to content

Commit e149202

Browse files
committed
Break out i18n part of distribution and reorded compression in config
* Inverts the key, value order of compression in config * Breaks out i18n for distributions * Introduces extra variables to improve code legibility * Updates README
1 parent ee4f2c1 commit e149202

File tree

3 files changed

+63
-41
lines changed

3 files changed

+63
-41
lines changed

DCAT.php

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ function validateConfig( array $config ) {
4242
}
4343
if ( $config['dumps-enabled'] ) {
4444
array_push( $top, "dump-info" );
45-
$sub["dump-info"] = array( "accessURL", "mediatype", "compression", "license" );
45+
$sub["dump-info"] = array(
46+
"accessURL", "mediatype", "compression", "license"
47+
);
4648
}
4749

4850
// Test
@@ -149,12 +151,12 @@ function makeDataBlob( $config ) {
149151
* @param XmlWriter $xml XML stream to write to
150152
* @param array $data data-blob of i18n and config variables
151153
* @param string|null $dumpDate the date of the dumpfile, null for live data
152-
* @param string $format the fileformat
154+
* @param string $dumpKey the key for the corresponding dump file
153155
*/
154-
function dumpDistributionExtras( XMLWriter $xml, array $data, $dumpDate, $format ) {
156+
function dumpDistributionExtras( XMLWriter $xml, array $data, $dumpDate, $dumpKey ) {
155157
$url = str_replace(
156158
'$1',
157-
$dumpDate . '/' . $data['dumps'][$dumpDate][$format]['filename'],
159+
$dumpDate . '/' . $data['dumps'][$dumpDate][$dumpKey]['filename'],
158160
$data['config']['dump-info']['accessURL']
159161
);
160162

@@ -169,16 +171,52 @@ function dumpDistributionExtras( XMLWriter $xml, array $data, $dumpDate, $format
169171
$xml->startElementNS( 'dcterms', 'issued', null );
170172
$xml->writeAttributeNS( 'rdf', 'datatype', null,
171173
'http://www.w3.org/2001/XMLSchema#date' );
172-
$xml->text( $data['dumps'][$dumpDate][$format]['timestamp'] );
174+
$xml->text( $data['dumps'][$dumpDate][$dumpKey]['timestamp'] );
173175
$xml->endElement();
174176

175177
$xml->startElementNS( 'dcat', 'byteSize', null );
176178
$xml->writeAttributeNS( 'rdf', 'datatype', null,
177179
'http://www.w3.org/2001/XMLSchema#decimal' );
178-
$xml->text( $data['dumps'][$dumpDate][$format]['byteSize'] );
180+
$xml->text( $data['dumps'][$dumpDate][$dumpKey]['byteSize'] );
179181
$xml->endElement();
180182
}
181183

184+
/**
185+
* Add i18n descriptions for a distribution
186+
*
187+
* @param XmlWriter $xml XML stream to write to
188+
* @param array $data data-blob of i18n and config variables
189+
* @param bool $isDump whether this is a dump distribution
190+
* @param string $prefix the type of distribution, one of ld, api or dump
191+
* @param string $format the file format, if dump
192+
* @param string $compression the compression format, if dump
193+
*/
194+
function writeDistributionI18n( XMLWriter $xml, array $data, $isDump,
195+
$prefix, $format, $compression ) {
196+
197+
foreach ( $data['i18n'] as $langCode => $langData ) {
198+
if ( array_key_exists( "distribution-$prefix-description", $langData ) ) {
199+
$formatDescription = $langData["distribution-$prefix-description"];
200+
if ( $isDump ) {
201+
$formatDescription = str_replace(
202+
'$1',
203+
$format,
204+
$formatDescription
205+
);
206+
$formatDescription = str_replace(
207+
'$2',
208+
$compression,
209+
$formatDescription
210+
);
211+
}
212+
$xml->startElementNS( 'dcterms', 'description', null );
213+
$xml->writeAttributeNS( 'xml', 'lang', null, $langCode );
214+
$xml->text( $formatDescription );
215+
$xml->endElement();
216+
}
217+
}
218+
}
219+
182220
/**
183221
* Construct distribution entry for each format in which a distribution
184222
* is available. The DCAT-specification requires each format to be a
@@ -193,20 +231,23 @@ function dumpDistributionExtras( XMLWriter $xml, array $data, $dumpDate, $format
193231
function writeDistribution( XMLWriter $xml, array $data, $distribId, $prefix, $dumpDate ) {
194232
$ids = array();
195233

234+
$isDump = !is_null( $dumpDate );
196235
$allowedMediatypes = $data['config']["$prefix-info"]['mediatype'];
197236
$allowedCompressiontypes = array( '' => '' ); // dummy array for non-dumps
198-
if ( !is_null( $dumpDate ) ){
237+
if ( $isDump ){
199238
$allowedCompressiontypes = $data['config']["$prefix-info"]['compression'];
200239
}
201240

202-
foreach ( $allowedCompressiontypes as $compression => $compressiontype ) {
241+
foreach ( $allowedCompressiontypes as $compressionName => $compression ) {
203242
foreach ( $allowedMediatypes as $format => $mediatype ) {
243+
$distributionKey = $format . $compression;
244+
204245
// handle missing (and BETA) dump files
205-
if ( !is_null( $dumpDate ) and !array_key_exists( $format . $compression , $data['dumps'][$dumpDate] ) ) {
246+
if ( $isDump and !array_key_exists( $distributionKey , $data['dumps'][$dumpDate] ) ) {
206247
continue;
207248
}
208249

209-
$id = $data['config']['uri'] . '#' . $distribId . $dumpDate . $format . $compression;
250+
$id = $data['config']['uri'] . '#' . $distribId . $dumpDate . $distributionKey;
210251
array_push( $ids, $id );
211252

212253
$xml->startElementNS( 'rdf', 'Description', null );
@@ -222,39 +263,20 @@ function writeDistribution( XMLWriter $xml, array $data, $distribId, $prefix, $d
222263
$data['config']["$prefix-info"]['license'] );
223264
$xml->endElement();
224265

225-
if ( is_null( $dumpDate ) ) {
266+
if ( !$isDump ) {
226267
$xml->startElementNS( 'dcat', 'accessURL', null );
227268
$xml->writeAttributeNS( 'rdf', 'resource', null,
228269
$data['config']["$prefix-info"]['accessURL'] );
229270
$xml->endElement();
230271
} else {
231-
dumpDistributionExtras( $xml, $data, $dumpDate, $format . $compression );
272+
dumpDistributionExtras( $xml, $data, $dumpDate, $distributionKey );
232273
}
233274

234275
$xml->writeElementNS( 'dcterms', 'format', null, $mediatype );
235276

236277
// add description in each language
237-
foreach ( $data['i18n'] as $langCode => $langData ) {
238-
if ( array_key_exists( "distribution-$prefix-description", $langData ) ) {
239-
$formatDescription = $langData["distribution-$prefix-description"];
240-
if ( !is_null( $dumpDate ) ) {
241-
$formatDescription = str_replace(
242-
'$1',
243-
$format,
244-
$formatDescription
245-
);
246-
$formatDescription = str_replace(
247-
'$2',
248-
$compressiontype,
249-
$formatDescription
250-
);
251-
}
252-
$xml->startElementNS( 'dcterms', 'description', null );
253-
$xml->writeAttributeNS( 'xml', 'lang', null, $langCode );
254-
$xml->text( $formatDescription );
255-
$xml->endElement();
256-
}
257-
}
278+
writeDistributionI18n( $xml, $data, $isDump, $prefix,
279+
$format, $compressionName );
258280

259281
$xml->endElement();
260282
}
@@ -599,7 +621,7 @@ function outputXml( array $data ) {
599621
*/
600622
function scanDump( $dirname, array $data ) {
601623
$testStrings = array();
602-
foreach ( $data['config']['dump-info']['compression'] as $compression => $compressiontype ) {
624+
foreach ( $data['config']['dump-info']['compression'] as $compression ) {
603625
foreach ( $data['config']['dump-info']['mediatype'] as $format => $mediatype ) {
604626
$testStrings["$format$compression"] = '-all.' . $format . '.' . $compression;
605627
}
@@ -613,16 +635,14 @@ function scanDump( $dirname, array $data ) {
613635
$subDump = array();
614636
foreach ( glob( $subdir . '/*' ) as $filename ) {
615637
// match each file against an expected testString
616-
#@todo change into one loop
617638
foreach ( $testStrings as $fileEnding => $testString ) {
618639
if ( substr( $filename, -strlen( $testString ) ) === $testString ) {
619640
$info = stat( $filename );
620641
$filename = substr( $filename, strlen( $subdir . '/' ) );
621642
$subDump[$fileEnding] = array(
622643
'timestamp' => gmdate( 'Y-m-d', $info['mtime'] ),
623644
'byteSize' => $info['size'],
624-
'filename' => $filename,
625-
'compression' => $compression
645+
'filename' => $filename
626646
);
627647
}
628648
}

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ Below follows a key by key explanation of the config file.
102102
* `accessURL`: URL to the directory where the *.json.gz* files
103103
reside (`$1` is replaced on the fly by the actual filename),
104104
e.g. *http://example.org/dumps/$1*
105-
* `mediatype`: (`object`) List of media types. In practice this is
106-
always `{"json": "application/json"}` ... for now
105+
* `mediatype`: (`object`) List of media types. e.g.
106+
`{"json": "application/json"}`
107+
* `compression`: (`object`) List of compression formats, in the
108+
format *name:file-ending* e.g. `{"gzip": "gz"}`
107109
* `license`: See ld-info:license above

config.example.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
"ttl": "text/turtle"
4747
},
4848
"compression": {
49-
"gz": "gzip",
50-
"bz": "bzip"
49+
"gzip": "gz",
50+
"bzip": "bz"
5151
},
5252
"license": "http://creativecommons.org/publicdomain/zero/1.0/"
5353
}

0 commit comments

Comments
 (0)