Skip to content

Commit

Permalink
[FINNA-2110] QDC: Add option to set preferred dc.type fields when ind…
Browse files Browse the repository at this point in the history
…exing format (#158)
  • Loading branch information
mshroom authored Jun 10, 2024
1 parent 877cbff commit 6801463
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/RecordManager/Base/Record/Qdc.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use RecordManager\Base\Utils\Logger;
use RecordManager\Base\Utils\MetadataUtils;

use function in_array;

/**
* Qdc record class
*
Expand Down Expand Up @@ -73,6 +75,13 @@ class Qdc extends AbstractRecord
*/
protected $recordNs = 'http://www.openarchives.org/OAI/2.0/oai_dc/';

/**
* Type fields which should be excluded when defining format.
*
* @var array
*/
protected $excludedFormatTypes = [];

/**
* Constructor
*
Expand Down Expand Up @@ -328,7 +337,28 @@ public function getSeriesNumbering()
*/
public function getFormat()
{
return $this->doc->type ? trim((string)$this->doc->type) : 'Unknown';
$param = $this->getDriverParam('preferredFormatTypes', '');
$preferredTypes = $param ? explode(',', $param) : [];
$collectedTypes = [];
$first = '';
foreach ($this->doc->type ?? [] as $node) {
if ($value = trim((string)$node)) {
$typeAttr = trim((string)($node->attributes()->type ?? '')) ?: 'no_type';
if (!in_array($typeAttr, $this->excludedFormatTypes) && !($collectedTypes[$typeAttr] ?? '')) {
$collectedTypes[$typeAttr] = $value;
$first = $first ?: $typeAttr;
}
}
}
if ($collectedTypes) {
foreach ($preferredTypes as $pref) {
if ($collectedTypes[$pref] ?? '') {
return $collectedTypes[$pref];
}
}
return $collectedTypes[$first];
}
return 'Unknown';
}

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/RecordManagerTest/Base/Record/QdcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public function testQdc1()
'2021-06-16T06:31:44Z',
'2021',
'Article',
'okm_type',
'okm_type_2',
'other_type',
'Eeva-Liisa Viskari, Suvi Lehtoranta, Riikka Malila. Urine : The'
. ' potential, value chain and its sustainable management. '
. 'Sanitation Value Chain (2021) 5, 1, pages 10-12. '
Expand Down Expand Up @@ -192,4 +195,36 @@ public function testQdc1()

$this->compareArray($expected, $keys, 'getWorkIdentificationData');
}

/**
* Test format
*
* @return void
*/
public function testFormat()
{
$expected = [
'okm' => 'okm_type',
'okm,other' => 'okm_type',
'finna,other' => 'other_type',
'finna' => 'Article',
];
foreach ($expected as $preferredTypes => $format) {
$record = $this->createRecord(
Qdc::class,
'qdc1.xml',
[
'__unit_test_no_source__' => [
'driverParams' => [
"preferredFormatTypes=$preferredTypes",
],
],
],
'Base',
[$this->createMock(\RecordManager\Base\Http\ClientManager::class)]
);
$fields = $record->toSolrArray();
$this->assertEquals($format, $fields['format']);
}
}
}
3 changes: 3 additions & 0 deletions tests/fixtures/Base/record/qdc1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<available>2021-06-16T06:31:44Z</available>
<issued>2021</issued>
<type lang="fi">Article</type>
<type lang="fi" type="okm">okm_type</type>
<type lang="fi" type="okm">okm_type_2</type>
<type lang="fi" type="other">other_type</type>
<identifier type="citation">Eeva-Liisa Viskari, Suvi Lehtoranta, Riikka Malila. Urine : The potential, value chain and its sustainable management. Sanitation Value Chain (2021) 5, 1, pages 10-12. https://doi.org/10.34416/svc.00029</identifier>
<identifier type="issn">2432-5058</identifier>
<identifier type="uri">http://hdl.handle.net/10138/331330</identifier>
Expand Down

0 comments on commit 6801463

Please sign in to comment.