Skip to content

Commit 40fac6c

Browse files
committed
Merge pull request #12 from Wikimedia-Sverige/simplify
Reducing the number of parameters passed to functions
2 parents e8470d1 + 081756b commit 40fac6c

File tree

1 file changed

+59
-70
lines changed

1 file changed

+59
-70
lines changed

DCAT.php

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,19 @@ function makeDataBlob( $config ) {
122122
}
123123
$i18n = makeI18nBlob( $langs, $config );
124124

125-
// hardcoded ids (for now at least)
126-
// https://github.com/lokal-profil/DCAT/issues/2
125+
// hardcoded ids
127126
$ids = array(
128127
'publisher' => '_n42',
129128
'contactPoint' => '_n43',
130-
'liveDataset' => 'liveData',
131-
'dumpDatasetPrefix' => 'dumpData',
132-
'liveDistribLD' => 'liveDataLD',
133-
'liveDistribAPI' => 'liveDataAPI',
134-
'dumpDistribPrefix' => 'dumpDist',
129+
'dataset' => array(
130+
'live' => 'liveData',
131+
'dump' => 'dumpData',
132+
),
133+
'distribution' => array(
134+
'ld' => 'liveDataLD',
135+
'api' => 'liveDataAPI',
136+
'dump' => 'dumpDist',
137+
),
135138
);
136139

137140
// stick loaded data into blob
@@ -149,15 +152,14 @@ function makeDataBlob( $config ) {
149152
* Complement to writeDistribution()
150153
*
151154
* @param XmlWriter $xml XML stream to write to
152-
* @param array $data data-blob of i18n and config variables
153-
* @param string|null $dumpDate the date of the dumpfile, null for live data
154-
* @param string $dumpKey the key for the corresponding dump file
155+
* @param array $dump the metadata on the dump being described
156+
* @param string $accessURL the url prefix for the filename
155157
*/
156-
function dumpDistributionExtras( XMLWriter $xml, array $data, $dumpDate, $dumpKey ) {
158+
function dumpDistributionExtras( XMLWriter $xml, array $dump, $accessURL ) {
157159
$url = str_replace(
158160
'$1',
159-
$dumpDate . '/' . $data['dumps'][$dumpDate][$dumpKey]['filename'],
160-
$data['config']['dump-info']['accessURL']
161+
$dump['filename'],
162+
$accessURL
161163
);
162164

163165
$xml->startElementNS( 'dcat', 'accessURL', null );
@@ -171,13 +173,13 @@ function dumpDistributionExtras( XMLWriter $xml, array $data, $dumpDate, $dumpKe
171173
$xml->startElementNS( 'dcterms', 'issued', null );
172174
$xml->writeAttributeNS( 'rdf', 'datatype', null,
173175
'http://www.w3.org/2001/XMLSchema#date' );
174-
$xml->text( $data['dumps'][$dumpDate][$dumpKey]['timestamp'] );
176+
$xml->text( $dump['timestamp'] );
175177
$xml->endElement();
176178

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

@@ -186,18 +188,17 @@ function dumpDistributionExtras( XMLWriter $xml, array $data, $dumpDate, $dumpKe
186188
*
187189
* @param XmlWriter $xml XML stream to write to
188190
* @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
191+
* @param string $prefix the type of distribution, one of 'ld', 'api' or 'dump'
192+
* @param string $format the file format, only used for dumps
193+
* @param string $compression the compression format, only used for dumps
193194
*/
194-
function writeDistributionI18n( XMLWriter $xml, array $data, $isDump,
195-
$prefix, $format, $compression ) {
195+
function writeDistributionI18n( XMLWriter $xml, array $data, $prefix,
196+
$format, $compression ) {
196197

197198
foreach ( $data['i18n'] as $langCode => $langData ) {
198199
if ( array_key_exists( "distribution-$prefix-description", $langData ) ) {
199200
$formatDescription = $langData["distribution-$prefix-description"];
200-
if ( $isDump ) {
201+
if ( $prefix === 'dump' ) {
201202
$formatDescription = str_replace(
202203
'$1',
203204
$format,
@@ -224,14 +225,13 @@ function writeDistributionI18n( XMLWriter $xml, array $data, $isDump,
224225
*
225226
* @param XmlWriter $xml XML stream to write to
226227
* @param array $data data-blob of i18n and config variables
227-
* @param string $distribId id for the distribution
228-
* @param string $prefix prefix for corresponding entry in config file
228+
* @param string $prefix the type of distribution, one of 'ld', 'api' or 'dump'
229229
* @param string|null $dumpDate the date of the dumpfile, null for live data
230230
*/
231-
function writeDistribution( XMLWriter $xml, array $data, $distribId, $prefix, $dumpDate ) {
231+
function writeDistribution( XMLWriter $xml, array $data, $prefix, $dumpDate ) {
232232
$ids = array();
233233

234-
$isDump = !is_null( $dumpDate );
234+
$isDump = $prefix === 'dump';
235235
$allowedMediatypes = $data['config']["$prefix-info"]['mediatype'];
236236
$allowedCompressiontypes = array( '' => '' ); // dummy array for non-dumps
237237
if ( $isDump ) {
@@ -247,7 +247,9 @@ function writeDistribution( XMLWriter $xml, array $data, $distribId, $prefix, $d
247247
continue;
248248
}
249249

250-
$id = $data['config']['uri'] . '#' . $distribId . $dumpDate . $distributionKey;
250+
$id = $data['config']['uri'] . '#' .
251+
$data['ids']['distribution'][$prefix] .
252+
$dumpDate . $distributionKey;
251253
array_push( $ids, $id );
252254

253255
$xml->startElementNS( 'rdf', 'Description', null );
@@ -269,14 +271,17 @@ function writeDistribution( XMLWriter $xml, array $data, $distribId, $prefix, $d
269271
$data['config']["$prefix-info"]['accessURL'] );
270272
$xml->endElement();
271273
} else {
272-
dumpDistributionExtras( $xml, $data, $dumpDate, $distributionKey );
274+
dumpDistributionExtras( $xml,
275+
$data['dumps'][$dumpDate][$distributionKey],
276+
$data['config']['dump-info']['accessURL']
277+
);
273278
}
274279

275280
$xml->writeElementNS( 'dcterms', 'format', null, $mediatype );
276281

277282
// add description in each language
278-
writeDistributionI18n( $xml, $data, $isDump, $prefix,
279-
$format, $compressionName );
283+
writeDistributionI18n( $xml, $data, $prefix, $format,
284+
$compressionName );
280285

281286
$xml->endElement();
282287
}
@@ -291,7 +296,7 @@ function writeDistribution( XMLWriter $xml, array $data, $distribId, $prefix, $d
291296
* @param XmlWriter $xml XML stream to write to
292297
* @param array $data data-blob of i18n and config variables
293298
* @param string|null $dumpDate the date of the dumpfile, null for live data
294-
* @param string $type dump or live
299+
* @param string $type 'dump' or 'live'
295300
*/
296301
function writeDatasetI18n( XMLWriter $xml, array $data, $dumpDate, $type ) {
297302
foreach ( $data['i18n'] as $langCode => $langData ) {
@@ -322,20 +327,15 @@ function writeDatasetI18n( XMLWriter $xml, array $data, $dumpDate, $type ) {
322327
* @param XmlWriter $xml XML stream to write to
323328
* @param array $data data-blob of i18n and config variables
324329
* @param string|null $dumpDate the date of the dumpfile, null for live data
325-
* @param string $datasetId the id of the dataset
326-
* @param string $publisher the nodeId of the publisher
327-
* @param string $contactPoint the nodeId of the contactPoint
328330
* @param array $distribution array of the distribution identifiers
329331
*/
330-
function writeDataset( XMLWriter $xml, array $data, $dumpDate, $datasetId,
331-
$publisher, $contactPoint, array $distribution ) {
332-
332+
function writeDataset( XMLWriter $xml, array $data, $dumpDate, array $distribution ) {
333333
$type = 'dump';
334334
if ( is_null( $dumpDate ) ) {
335335
$type = 'live';
336336
}
337337

338-
$id = $data['config']['uri'] . '#' . $datasetId . $dumpDate;
338+
$id = $data['config']['uri'] . '#' . $data['ids']['dataset'][$type] . $dumpDate;
339339

340340
$xml->startElementNS( 'rdf', 'Description', null );
341341
$xml->writeAttributeNS( 'rdf', 'about', null, $id );
@@ -346,14 +346,14 @@ function writeDataset( XMLWriter $xml, array $data, $dumpDate, $datasetId,
346346
$xml->endElement();
347347

348348
$xml->startElementNS( 'adms', 'contactPoint', null );
349-
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $contactPoint );
349+
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['contactPoint'] );
350350
$xml->endElement();
351351

352352
$xml->startElementNS( 'dcterms', 'publisher', null );
353-
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $publisher );
353+
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['publisher'] );
354354
$xml->endElement();
355355

356-
if ( $type === 'live' ) {
356+
if ( is_null( $dumpDate ) ) {
357357
$xml->startElementNS( 'dcterms', 'accrualPeriodicity', null );
358358
$xml->writeAttributeNS( 'rdf', 'resource', null,
359359
'http://purl.org/cld/freq/continuous' );
@@ -388,15 +388,14 @@ function writeDataset( XMLWriter $xml, array $data, $dumpDate, $datasetId,
388388
}
389389

390390
/**
391-
* Construct the publisher for the catalog and datasets with a given nodeId
391+
* Construct the publisher for the catalog and datasets
392392
*
393393
* @param XmlWriter $xml XML stream to write to
394394
* @param array $data data-blob of i18n and config variables
395-
* @param string $publisher the nodeId of the publisher
396395
*/
397-
function writePublisher( XMLWriter $xml, array $data, $publisher ) {
396+
function writePublisher( XMLWriter $xml, array $data ) {
398397
$xml->startElementNS( 'rdf', 'Description', null );
399-
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $publisher );
398+
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['publisher'] );
400399

401400
$xml->startElementNS( 'rdf', 'type', null );
402401
$xml->writeAttributeNS( 'rdf', 'resource', null,
@@ -424,15 +423,14 @@ function writePublisher( XMLWriter $xml, array $data, $publisher ) {
424423
}
425424

426425
/**
427-
* Construct a contactPoint for the datasets with a given nodeId
426+
* Construct a contactPoint for the datasets
428427
*
429428
* @param XmlWriter $xml XML stream to write to
430429
* @param array $data data-blob of i18n and config variables
431-
* @param string $contactPoint the nodeId of the contactPoint
432430
*/
433-
function writeContactPoint( XMLWriter $xml, array $data, $contactPoint ) {
431+
function writeContactPoint( XMLWriter $xml, array $data ) {
434432
$xml->startElementNS( 'rdf', 'Description', null );
435-
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $contactPoint );
433+
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['contactPoint'] );
436434

437435
$xml->startElementNS( 'rdf', 'type', null );
438436
$xml->writeAttributeNS( 'rdf', 'resource', null,
@@ -484,10 +482,9 @@ function writeCatalogI18n( XMLWriter $xml, array $data ) {
484482
*
485483
* @param XmlWriter $xml XML stream to write to
486484
* @param array $data data-blob of i18n and config variables
487-
* @param string $publisher the nodeId of the publisher
488485
* @param array $dataset array of the dataset identifiers
489486
*/
490-
function writeCatalog( XMLWriter $xml, array $data, $publisher, array $dataset ) {
487+
function writeCatalog( XMLWriter $xml, array $data, array $dataset ) {
491488
$xml->startElementNS( 'rdf', 'Description', null );
492489
$xml->writeAttributeNS( 'rdf', 'about', null,
493490
$data['config']['uri'] . '#catalog' );
@@ -523,7 +520,7 @@ function writeCatalog( XMLWriter $xml, array $data, $publisher, array $dataset )
523520
$xml->endElement();
524521

525522
$xml->startElementNS( 'dcterms', 'publisher', null );
526-
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $publisher );
523+
$xml->writeAttributeNS( 'rdf', 'nodeID', null, $data['ids']['publisher'] );
527524
$xml->endElement();
528525

529526
// add language, title and description in each language
@@ -569,41 +566,33 @@ function outputXml( array $data ) {
569566
'http://www.w3.org/2006/vcard/ns#' );
570567

571568
// Calls previously declared functions to construct xml
572-
writePublisher( $xml, $data, $data['ids']['publisher'] );
573-
writeContactPoint( $xml, $data, $data['ids']['contactPoint'] );
569+
writePublisher( $xml, $data );
570+
writeContactPoint( $xml, $data );
574571

575572
$dataset = array();
576573

577574
// Live dataset and distributions
578-
$liveDistribs = writeDistribution( $xml, $data,
579-
$data['ids']['liveDistribLD'], 'ld', null );
575+
$liveDistribs = writeDistribution( $xml, $data, 'ld', null );
580576
if ( $data['config']['api-enabled'] ) {
581577
$liveDistribs = array_merge( $liveDistribs,
582-
writeDistribution( $xml, $data,
583-
$data['ids']['liveDistribAPI'], 'api', null )
578+
writeDistribution( $xml, $data, 'api', null )
584579
);
585580
}
586581
array_push( $dataset,
587-
writeDataset( $xml, $data, null, $data['ids']['liveDataset'],
588-
$data['ids']['publisher'], $data['ids']['contactPoint'],
589-
$liveDistribs )
582+
writeDataset( $xml, $data, null, $liveDistribs )
590583
);
591584

592585
// Dump dataset and distributions
593586
if ( $data['config']['dumps-enabled'] ) {
594587
foreach ( $data['dumps'] as $key => $value ) {
595-
$distIds = writeDistribution( $xml, $data,
596-
$data['ids']['dumpDistribPrefix'], 'dump', $key );
588+
$distIds = writeDistribution( $xml, $data, 'dump', $key );
597589
array_push( $dataset,
598-
writeDataset( $xml, $data, $key,
599-
$data['ids']['dumpDatasetPrefix'],
600-
$data['ids']['publisher'],
601-
$data['ids']['contactPoint'], $distIds )
590+
writeDataset( $xml, $data, $key, $distIds )
602591
);
603592
}
604593
}
605594

606-
writeCatalog( $xml, $data, $data['ids']['publisher'], $dataset );
595+
writeCatalog( $xml, $data, $dataset );
607596

608597
// Closing last XML node
609598
$xml->endElement();
@@ -634,11 +623,11 @@ function scanDump( $dirname, array $data ) {
634623
// $subdir = testdirNew/20150120
635624
$subDump = array();
636625
foreach ( glob( $subdir . '/*' ) as $filename ) {
637-
// match each file against an expected testString
626+
// match each file against an expected test string
638627
foreach ( $testStrings as $fileEnding => $testString ) {
639628
if ( substr( $filename, -strlen( $testString ) ) === $testString ) {
640629
$info = stat( $filename );
641-
$filename = substr( $filename, strlen( $subdir . '/' ) );
630+
$filename = substr( $filename, strlen( $dirname . '/' ) );
642631
$subDump[$fileEnding] = array(
643632
'timestamp' => gmdate( 'Y-m-d', $info['mtime'] ),
644633
'byteSize' => $info['size'],

0 commit comments

Comments
 (0)