Skip to content

Commit

Permalink
[FINNA-2143] Fix field processing rule match handling for multivalued…
Browse files Browse the repository at this point in the history
… fields.
  • Loading branch information
EreMaijala committed May 14, 2024
1 parent c35092f commit 877cbff
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 17 deletions.
49 changes: 33 additions & 16 deletions src/RecordManager/Base/Solr/SolrUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -2471,6 +2471,9 @@ protected function parseFieldRules(string $source, array $rules): void
}
$this->settings[$source]['fieldProcessingRules'][] = $rule;
}
$this->log->writelnVeryVerbose(
"Field rules for $source: " . var_export($this->settings[$source]['fieldProcessingRules'] ?? [], true)
);
}

/**
Expand All @@ -2487,38 +2490,52 @@ protected function processFieldRules(string $source, array &$data): void
{
foreach ($this->settings[$source]['fieldProcessingRules'] ?? [] as $rule) {
$src = $rule['src'];
$srcValue = $data[$src] ?? null;
if (!($fieldValue = $srcValue ?: ($rule['default'] ?? null))) {
continue;
}
$srcValues = $data[$src] ?? null;
if ($match = $rule['match'] ?? null) {
if (null === $srcValue) {
// If we don't have values, nothing can match:
if (null === $srcValues) {
continue;
}
if (str_starts_with($match, '/') && (str_ends_with($match, '/') || str_ends_with($match, '/i'))) {
if (!preg_match($match, $srcValue)) {
continue;
// Filter source values by the match rule:
$re = str_starts_with($match, '/') && (str_ends_with($match, '/') || str_ends_with($match, '/i'));
$matchingValues = array_filter(
(array)$srcValues,
function ($srcValue) use ($re, $match) {
return
($re && preg_match($match, $srcValue))
|| (!$re && $match === $srcValue);
}
} elseif ($match !== $srcValue) {
);
// Stop if we don't have any matching values:
if (!$matchingValues) {
continue;
}
$srcValues = is_array($srcValues) ? $matchingValues : reset($matchingValues);
}
if (!($newValues = $srcValues ?: ($rule['default'] ?? null))) {
continue;
}
$dst = $rule['dst'];
if (in_array($rule['op'], [self::RULE_COPY, self::RULE_MOVE])) {
if (!isset($data[$dst])) {
$data[$dst] = $fieldValue;
$data[$dst] = $newValues;
} else {
$data[$dst] = [
...(array)$data[$dst],
...(array)$fieldValue,
...(array)$newValues,
];
}
}
if (
in_array($rule['op'], [self::RULE_DELETE, self::RULE_MOVE])
&& isset($data[$src])
) {
unset($data[$src]);
if (in_array($rule['op'], [self::RULE_DELETE, self::RULE_MOVE])) {
// If we have a match rule and multiple values, only remove matching values:
if ($match && is_array($srcValues)) {
$data[$src] = array_diff($data[$src], $srcValues);
if (!$data[$src]) {
unset($data[$src]);
}
} else {
unset($data[$src]);
}
}
}
}
Expand Down
71 changes: 70 additions & 1 deletion tests/RecordManagerTest/Base/Solr/SolrUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,75 @@ public function processSingleRecordProvider(): array
'institution' => null,
],
],
'copy multivalued' => [
[
'copy topic newtopic match="/^tutkimus/"',
],
[
'newtopic' => [
'tutkimusrahoitus',
'tutkimuspolitiikka',
'tutkimustyö',
'tutkimus',
],
'topic' => [
'oppaat',
'ft: kirjoittaminen',
'apurahat',
'tutkimusrahoitus',
'tutkimuspolitiikka',
'opinnäytteet',
'tiedonhaku',
'kielioppaat',
'tutkimustyö',
'tutkimus',
],
],
],
'move multivalued' => [
[
'move topic newtopic match="/^tutkimus/"',
],
[
'newtopic' => [
'tutkimusrahoitus',
'tutkimuspolitiikka',
'tutkimustyö',
'tutkimus',
],
'topic' => [
'oppaat',
'ft: kirjoittaminen',
'apurahat',
'opinnäytteet',
'tiedonhaku',
'kielioppaat',
],
],
],
'delete multivalued' => [
[
'delete topic',
],
[
'topic' => null,
],
],
'delete multivalued matching' => [
[
'delete topic match="/^tutkimus/"',
],
[
'topic' => [
'oppaat',
'ft: kirjoittaminen',
'apurahat',
'opinnäytteet',
'tiedonhaku',
'kielioppaat',
],
],
],
];
}

Expand All @@ -287,7 +356,7 @@ public function testFieldProcessingRules(array $rules, array $expected): void

$record = $this->createMarcRecord(
\RecordManager\Base\Record\Marc::class,
'marc-broken.xml'
'marc1.xml'
);

$date = strtotime('2020-10-20 13:01:00');
Expand Down

0 comments on commit 877cbff

Please sign in to comment.