Skip to content

Commit 8ada091

Browse files
committed
Store collection descriptions in MARC 520 summary fields
Instead of only carrying the collection description in the local 996f subfield, also write it to a standard MARC 520 summary field (a subfield), so the description is available for the regular summary-based display and indexing. Assisted-by: OpenCode / big-pickle (opencode) Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent 8ac3412 commit 8ada091

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

‎module/VuFindConsole/src/VuFindConsole/Command/Util/AlmaCollectionsCommand.php‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,9 @@ protected function configure()
124124
'',
125125
'Each collection is written to a file collection-<mms_id>.xml together with the',
126126
'records of all member titles (GET /bibs/collections/{pid}/bibs). The records are',
127-
'augmented with a local MARC 996 field containing the VuFind hierarchy fields and',
128-
'the collection description; see the mappings in import/marc.properties.',
127+
'augmented with a MARC 520 summary field containing the collection description and',
128+
'a local MARC 996 field containing the VuFind hierarchy fields; see the mappings in',
129+
'import/marc.properties.',
129130
'',
130131
'To display the member records on the collection page, enable the Collections',
131132
'module (collections = true in the [Collections] section of config.ini) and add',
@@ -153,8 +154,9 @@ protected function configure()
153154
InputOption::VALUE_REQUIRED,
154155
'Write the MARCXML record of each collection to the given directory instead of '
155156
. 'displaying the overview (default: local/harvest/Collections). The records are '
156-
. 'augmented with a local MARC 996 field containing the VuFind hierarchy fields '
157-
. 'and the collection description; see the mappings in import/marc.properties.'
157+
. 'augmented with a MARC 520 summary field containing the collection description '
158+
. 'and a local MARC 996 field containing the VuFind hierarchy fields; see the '
159+
. 'mappings in import/marc.properties.'
158160
);
159161
}
160162

@@ -443,6 +445,24 @@ protected function extractRecord(string $body): ?DOMNode
443445
return $nodes->item(0);
444446
}
445447

448+
/**
449+
* Add a MARC 520 summary field with the collection description to a record.
450+
*
451+
* @param DOMNode $record MARCXML record element
452+
* @param string $description Description of the collection
453+
*
454+
* @return void
455+
*/
456+
protected function addSummaryField(DOMNode $record, string $description): void
457+
{
458+
$datafield = $record->ownerDocument->createElement('datafield');
459+
$datafield->setAttribute('tag', '520');
460+
$datafield->setAttribute('ind1', ' ');
461+
$datafield->setAttribute('ind2', ' ');
462+
$this->addSubfield($datafield, 'a', $description);
463+
$record->appendChild($datafield);
464+
}
465+
446466
/**
447467
* Add a MARC 996 data field with the VuFind hierarchy fields to a record.
448468
*
@@ -461,6 +481,10 @@ protected function addHierarchyField(
461481
string $description,
462482
array $ancestors
463483
): void {
484+
if ('' !== $description) {
485+
$this->addSummaryField($record, $description);
486+
}
487+
464488
$isTop = empty($ancestors);
465489
$topId = $isTop ? $mmsId : $ancestors[0];
466490
$parentId = $isTop ? null : end($ancestors);

‎module/VuFindConsole/tests/unit-tests/src/VuFindTest/Command/Util/AlmaCollectionsCommandTest.php‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ public function testDownloadAllCollections(): void
126126
);
127127
$this->assertArrayNotHasKey('c', $parent);
128128

129+
// The collection description is also stored in a MARC 520 summary field.
130+
$this->assertSame(
131+
['a' => 'Test collection'],
132+
$this->getSummaryField(file_get_contents($parentFile))
133+
);
134+
129135
// Sub-collection: parent and top IDs point to the parent collection.
130136
$child = $this->getHierarchyField(file_get_contents($childFile));
131137
$this->assertSame(
@@ -134,6 +140,10 @@ public function testDownloadAllCollections(): void
134140
'f' => 'Child collection'],
135141
$child
136142
);
143+
$this->assertSame(
144+
['a' => 'Child collection'],
145+
$this->getSummaryField(file_get_contents($childFile))
146+
);
137147
}
138148

139149
/**
@@ -165,7 +175,8 @@ public function testDownloadSingleCollection(): void
165175
}
166176

167177
/**
168-
* Test that a collection without a description gets no 996f subfield.
178+
* Test that a collection without a description gets no 996f subfield
179+
* and no 520 field.
169180
*
170181
* @return void
171182
*/
@@ -185,10 +196,10 @@ public function testDownloadWithoutDescription(): void
185196
$commandTester = new CommandTester($this->getCommand($httpService));
186197
$commandTester->execute(['--output' => $this->outputDir]);
187198
$this->assertSame(0, $commandTester->getStatusCode());
188-
$field = $this->getHierarchyField(
189-
file_get_contents($this->outputDir . '/collection-9919814834502561.xml')
190-
);
199+
$record = file_get_contents($this->outputDir . '/collection-9919814834502561.xml');
200+
$field = $this->getHierarchyField($record);
191201
$this->assertArrayNotHasKey('f', $field);
202+
$this->assertSame([], $this->getSummaryField($record));
192203
}
193204

194205
/**
@@ -273,6 +284,30 @@ protected function getCommand(HttpService $httpService): AlmaCollectionsCommand
273284
);
274285
}
275286

287+
/**
288+
* Extract the summary values of the MARC 520 field from a record.
289+
*
290+
* @param string $xml MARCXML record
291+
*
292+
* @return array
293+
*/
294+
protected function getSummaryField(string $xml): array
295+
{
296+
$dom = new DOMDocument();
297+
$this->assertTrue($dom->loadXML($xml));
298+
$xpath = new DOMXPath($dom);
299+
$nodes = $xpath->query('//datafield[@tag="520"]');
300+
$this->assertNotFalse($nodes);
301+
if (0 === $nodes->length) {
302+
return [];
303+
}
304+
$result = [];
305+
foreach ($nodes->item(0)->getElementsByTagName('subfield') as $subfield) {
306+
$result[$subfield->getAttribute('code')] = $subfield->textContent;
307+
}
308+
return $result;
309+
}
310+
276311
/**
277312
* Extract the hierarchy values of the MARC 996 field from a record.
278313
*

0 commit comments

Comments
 (0)