diff --git a/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php b/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php index f5fb36b670842cb7f3d977259c2943941603bdbf..3c158d6bd04234f03516967bdb7472c37d4ad34b 100644 --- a/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php +++ b/module/finc/src/finc/RecordDriver/SolrDefaultFincTrait.php @@ -895,93 +895,6 @@ trait SolrDefaultFincTrait $this->fields['title_new'] : []; } - /** - * After giving a record ids as e.g. ppn of the context check (KXP|BSZ) if a - * foreign record exists. This method can be used to indicate a direct link - * than to form a general look for query. - * - * Please note: 1) This method can be simplified if it is necessary only to - * map record_id to id but you need the solr to check if the item really - * exists in the Solr infrastructure to maybe no displaying a false link later - * on. 2) There's legacy component to rewrite back real Solr record_id in - * return container. Can be useful if there are still any successive methods - * in action. - * - * @param array $array Array of record ids to test. - * @param string $context Context where to search related items. - * - * @return null|mixed If success return at least one finc id otherwise null. - */ - protected function addFincIDToRecord($array, $context = 'kxp_id_str') - { - // record ids - $rids = []; - // return array - $retval = []; - - // check if array contain record_ids and collect it as an array to - // use only one solr request for all - if (isset($array) && is_array($array)) { - foreach ($array as $line) { - if (isset($line['record_id']) && isset($line['source_id'])) { - $rids[] = [ - 'rid' => $line['record_id'], - 'sid' => $line['source_id'] - ]; - } - } - } - - // Solr query remains necessary if it is desired to check if solr - // infrastructure contains really the foreign items - - // build the query: - if (count($rids) == 1) { - // single query: - $value = $context . ':' . $rids[0]['rid'] - . ' AND source_id:' . $rids[0]['sid']; - } elseif (count($rids) > 1) { - // multi query: - foreach ($rids as $rid) { - $parts[] = '('. $context .':' . $rid['rid'] - . ' AND source_id:' . $rid['sid'] . ')'; - } - $value = '(' . implode(' OR ', $parts) . ')'; - } else { - return $array; - } - $query = new \VuFindSearch\Query\Query($value); - $bag = new ParamBag(); - $bag->set('fl', 'id, record_id,' . $context); - $records = $this->searchService - ->search('Solr', $query, 0, count($rids), $bag); - - $records = $records->getRecords(); - if (isset($records) - && !empty($records) - ) { - foreach ($records as $record) { - $retval[$record->getField($context)]['id'] = - $record->getUniqueID(); - $retval[$record->getField($context)]['record_id'] = - $record->getRID(); - } - } - - // write back in array - foreach ($array as &$val) { - if (isset($val['record_id'])) { - if (isset($retval[($val['record_id'])])) { - $val['id'] = $retval[($val['record_id'])]['id']; - $val['record_id'] = $retval[($val['record_id'])]['record_id']; - } - } - } - unset($val); - - return $array; - } - /** * Get the recordtype of the current Record * diff --git a/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php b/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php index aaed7e69ea8d14b22fee441e0c217fe7fb5160aa..ae53d4e3012e8f0c6a13288b51f37492e9ca1100 100644 --- a/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php +++ b/module/finc/src/finc/RecordDriver/SolrMarcFincTrait.php @@ -300,24 +300,14 @@ trait SolrMarcFincTrait $array[$key]['text'] = ($line->getSubfield('t')) ? $line->getSubfield('t')->getData() : ''; // get ppns of bsz - $linkFields = $line->getSubfields('w'); - foreach ($linkFields as $current) { - $text = $current->getData(); - // Extract parenthetical prefixes: - if (preg_match(self::KXP_PATTERN, $text, $matches)) { - //$id = $this->checkIfRecordExists($matches[2]); - //if ($id != null) { - $array[$key]['record_id'] = $matches[2].$matches[3]; - if (null != ($sid = $this->getSourceID())) { - $array[$key]['source_id'] = $sid; - } - //} - //break; - } - } // end foreach + $linkedRecords = $this->getRelatedKxpRecord($line); + + if (!empty($linkedRecords)) { + $array[$key]['id'] = current($linkedRecords['first_results'])->getUniqueId(); + } } // end foreach - return $this->addFincIDToRecord($array); + return $array; } /** @@ -1047,20 +1037,14 @@ trait SolrMarcFincTrait ? $line->getSubfield('t')->getData() : ''; } // get ppns of bsz - $linkFields = $line->getSubfields('w'); - foreach ($linkFields as $current) { - $text = $current->getData(); - // Extract parenthetical prefixes: - if (preg_match(self::KXP_PATTERN, $text, $matches)) { - $array[$key]['record_id'] = $matches[2] . $matches[3]; - if (null != ($sid = $this->getSourceID())) { - $array[$key]['source_id'] = $sid; - } - } - } // end foreach + $linkedRecords = $this->getRelatedKxpRecord($line); + + if (!empty($linkedRecords)) { + $array[$key]['id'] = current($linkedRecords['first_results'])->getUniqueId(); + } } // end foreach - return $this->addFincIDToRecord($array); + return $array; } @@ -1104,27 +1088,13 @@ trait SolrMarcFincTrait $retval[$key]['text'] = ($t = $line->getSubfield('t')) ? $a->getData() . ': ' . $t->getData() : $a->getData(); - $linkFields = $line->getSubfields('w'); - foreach ($linkFields as $current) { - $ids = $current->getData(); - - // Extract parenthetical prefixes: - if (preg_match(self::KXP_PATTERN, $ids, $matches)) { - // use the same key to set the record_id into the - // $retval array like it is used for the other - // content below - $retval[$key]['record_id'] - = $matches[2] . $matches[3]; - if (null != ($sid = $this->getSourceID())) { - $retval[$key]['source_id'] = $sid; - } - } + $linkedRecords = $this->getRelatedKxpRecord($line); + + if (!empty($linkedRecords)) { + $retval[$key]['id'] = current($linkedRecords['first_results'])->getUniqueId(); } } } - // add ids already here to the temporary array - // instead of the end of the function with the return value - $retval = $this->addFincIDToRecord($retval); // rearrange return array to collect same subjects below on unique key $array = []; foreach ($retval as $item) { @@ -1172,25 +1142,17 @@ trait SolrMarcFincTrait $array[$i]['text'] = ($line->getSubfield('t')) ? $line->getSubfield('t')->getData() : ''; // get ppns of bsz - $linkFields = $line->getSubfields('w'); - if (is_array($linkFields) && count($linkFields) > 0) { - foreach ($linkFields as $current) { - $text = $current->getData(); - // Extract parenthetical prefixes: - if (preg_match(self::KXP_PATTERN, $text, $matches)) { - $array[$key]['record_id'] = $matches[2].$matches[3]; - if (null != ($sid = $this->getSourceID())) { - $array[$key]['source_id'] = $sid; - } - } - } // end foreach - } // end if + $linkedRecords = $this->getRelatedKxpRecord($line); + + if (!empty($linkedRecords)) { + $array[$key]['id'] = current($linkedRecords['first_results'])->getUniqueId(); + } $i++; } // end if } // end foreach } } - return $this->addFincIDToRecord($array); + return $array; } /** @@ -1219,20 +1181,14 @@ trait SolrMarcFincTrait ? $line->getSubfield('t')->getData() : ''; } // get ppns of bsz - $linkFields = $line->getSubfields('w'); - foreach ($linkFields as $current) { - $text = $current->getData(); - // Extract parenthetical prefixes: - if (preg_match(self::KXP_PATTERN, $text, $matches)) { - $array[$key]['record_id'] = $matches[2].$matches[3]; - if (null != ($sid = $this->getSourceID())) { - $array[$key]['source_id'] = $sid; - } - } - } // end foreach + $linkedRecords = $this->getRelatedKxpRecord($line); + + if (!empty($linkedRecords)) { + $array[$key]['id'] = current($linkedRecords['first_results'])->getUniqueId(); + } } // end foreach - return $this->addFincIDToRecord($array); + return $array; } /** @@ -1407,7 +1363,7 @@ trait SolrMarcFincTrait { $array = []; $fields = ['770','775','776']; - $subfields = ['a', 'l', 't', 'b', 'd', 'e', 'f', 'h', 'o', '7','z']; + $subfields = ['a', 'l', 't', 'b', 'd', 'e', 'f', 'h', 'o', '7']; $i = 0; foreach ($fields as $field) { @@ -1436,33 +1392,51 @@ trait SolrMarcFincTrait $array[$i] = [ 'text' => implode(', ', $text), 'identifier' => ($line->getSubfield('i')) - ? $line->getSubfield('i')->getData() : '' + ? $line->getSubfield('i')->getData() : '', + 'note' => ($line->getSubfield('n')) + ? $line->getSubfield('n')->getData() : null, ]; // finally we can try to use given PPNs (from the BSZ) to // link the record - if ($linkFields = $line->getSubfields('w')) { + $linkedRecords = $this->getRelatedKxpRecord($line); + + if (!empty($linkedRecords['first_results'])) { + $array[$i]['id'] = current($linkedRecords['first_results'])->getUniqueId(); + } + + if (empty($linkedRecords) && $linkFields = $line->getSubfields('z')) { foreach ($linkFields as $current) { - $text = $current->getData(); - // Extract parenthetical prefixes: - if (preg_match(self::KXP_PATTERN, $text, $matches)) { - $array[$i]['record_id'] - = $matches[2] . $matches[3]; - if (null != ($sid = $this->getSourceID())) { - $array[$i]['source_id'] = $sid; - } - } + $linked[] = $current->getData(); + } + $linkedRecords = $this->searchRelatedRecords('isbn',$linked, 1); + if (!empty($linkedRecords['first_results'])) { + $array[$i]['related_records'] = $linkedRecords; } } // at least we found some identifier and text so increment $i++; } + } elseif ($linkFields = $line->getSubfields('z')) { + $linked = []; + foreach ($linkFields as $current) { + $linked[] = $current->getData(); + } + $linkedRecords = $this->searchRelatedRecords('isbn',$linked, 1); + if (!empty($linkedRecords['first_results'])) { + $array[$i] = [ + 'related_records' => $linkedRecords, + 'identifier' => 'Other Editions', + 'text' => '', + ]; + $i++; + } } } } } - return $this->addFincIDToRecord($array); + return $array; } /** @@ -1555,26 +1529,21 @@ trait SolrMarcFincTrait // finally we can try to use given PPNs (from the BSZ) to // link the record - if ($linkFields = $line->getSubfields('w')) { - foreach ($linkFields as $current) { - $text = $current->getData(); - // Extract parenthetical prefixes: - if (preg_match(self::KXP_PATTERN, $text, $matches)) { - $array[$i]['record_id'] - = $matches[2] . $matches[3]; - if (null != ($sid = $this->getSourceID())) { - $array[$i]['source_id'] = $sid; - } - } - } + // finally we can try to use given PPNs (from the BSZ) to + // link the record + $linkedRecords = $this->getRelatedKxpRecord($line); + + if (!empty($linkedRecords)) { + $array[$i]['id'] = current($linkedRecords['first_results'])->getUniqueId(); } + // at least we found some identifier and text so increment $i++; } } } } - return $this->addFincIDToRecord($array); + return $array; } /** @@ -1639,33 +1608,6 @@ trait SolrMarcFincTrait ['830'] ]; - $idRetrieval = function ($value) { - // use preg_match to get rid of the isil - preg_match(self::KXP_PATTERN, $value, $matches); - if (!empty($matches[2])) { - $query = 'kxp_id_str:' . $matches[2].$matches[3]; - if ($sid = $this->fields['source_id']) { - $query .= ' AND source_id:'.$sid; - } - $result = $this->searchService->search( - 'Solr', - new Query($query) - ); - if (count($result) === 0) { - $this->debug( - 'Could not retrieve id for record with ' . $query - ); - return null; - } - return current($result->getRecords())->getUniqueId(); - } - $this->debug( - 'Pregmatch pattern in getHierarchyParentID failed for ' - . $value - ); - return $value; - }; - // loop through all field lists in their particular order (as in // getHierchyParentTitle) and build the $parentID array foreach ($fieldList as $fieldNumbers) { @@ -1673,9 +1615,11 @@ trait SolrMarcFincTrait $fields = $this->getMarcRecord()->getFields($fieldNumber); foreach ($fields as $field) { if ($field->getSubfield('w')) { - $parentID[] = $idRetrieval( - $field->getSubfield('w')->getData() - ); + // refactored with #18333 + $related = $this->getRelatedKxpRecord($field); + if (!empty( $related['first_results'] ?? [])) { + $parentID[] = current($related['first_results'])->getUniqueId(); + } } elseif ($fieldNumber == '490') { // https://intern.finc.info/issues/8704 if ($field->getIndicator(1) == 0 @@ -2082,34 +2026,52 @@ trait SolrMarcFincTrait } } - /** - * Get related records via search index - * - * @param int $limit - * @param string $backend_id Search engine - * - * @return array - * @access protected - */ - protected function getRelatedRecords($limit, $backend_id = 'Solr') - { - $related = $this->getRelatedItems(true); + public function searchRelatedRecords($field, $values, $limit = 20, + $filters=[], $backend_id = 'Solr' + ) { - if (empty($related)) { - return []; + if (!empty($filters)) { + $fq = ''; + foreach ($filters as $filterField => $value) { + $fq = (empty($fq) ? '' : ' AND ') + . "$filterField:$value"; + } } $query = new Query( - 'isbn' . ':' . implode(' OR ', $related) - . ' AND NOT id:' . $this->getUniqueID() + $field . ':(' . implode(' OR ', $values) + . ') AND NOT id:' . $this->getUniqueID() + . (isset($fq) ? " AND $fq" : '') ); $result = $this->searchService->search($backend_id, $query, 0, $limit); $return['first_results'] = $result->getRecords(); - if ($result->getTotal() > $limit) { + + if (isset($limit) && $result->getTotal() > $limit) { $return['more_query'] = $query->getString(); } return $return; + + } + + public function getRelatedKxpRecord($line, $limit = 1, $linkSubField = 'w') { + + if ($linkFields = $line->getSubfields('w')) { + $linked = []; + foreach ($linkFields as $current) { + $text = $current->getData(); + // Extract parenthetical prefixes: + if (preg_match(self::KXP_PATTERN, $text, $matches)) { + $linked[] + = $matches[2] . $matches[3]; + } + } + $linkedRecords = $this->searchRelatedRecords('kxp_id_str', $linked, $limit, ['source_id' => $this->getSourceID()]); + if (!empty($linkedRecords['first_results'])) { + return $linkedRecords; + } + } + return null; } /** diff --git a/module/finc/src/finc/View/Helper/Root/MultiDataFieldsTrait.php b/module/finc/src/finc/View/Helper/Root/MultiDataFieldsTrait.php index b9536ccab15bf7871c05c89ade86a935039feb04..0f137ca2a6525f0f3072aa4fa5a872cba0576234 100644 --- a/module/finc/src/finc/View/Helper/Root/MultiDataFieldsTrait.php +++ b/module/finc/src/finc/View/Helper/Root/MultiDataFieldsTrait.php @@ -82,6 +82,30 @@ trait MultiDataFieldsTrait return $final; } + public function relatedRecords($data,$options,$driver) { + + $formats = $driver->tryMethod('getFormat'); + $reject = array('Microfilm', 'Microform', 'Microfiche'); + if (empty(array_intersect($reject, $formats))) { + $data = $driver->tryMethod( + 'getRelatedRecords', + [$options['limit'] ?? 3] + ); + } + if (!empty($data['first_results'])) { + return [[ + 'label' => 'Other Editions', + 'values' => $data, + 'options' => [ + 'pos' => $options['pos'], + 'renderType' => 'RecordDriverTemplate', + 'template' => 'data-relatedRecords.phtml', + ], + ]]; + } + return null; + } + /** * return render details for events data set * @return callback diff --git a/module/finc/src/finc/View/Helper/Root/Record.php b/module/finc/src/finc/View/Helper/Root/Record.php index f326277cf94206114db2980f4b43304b7017742e..c12efed09377fb0e62cd7f4866275eee5c228702 100644 --- a/module/finc/src/finc/View/Helper/Root/Record.php +++ b/module/finc/src/finc/View/Helper/Root/Record.php @@ -467,6 +467,7 @@ class Record extends \VuFind\View\Helper\Root\Record $online_keys = [ "Online-Ausg.", "Online-Ausg.:", + "Online-Ausgabe", "Digital. Ausg.", "Online-Ausg. u.d.T.", "Elektronische Reproduktion" @@ -474,14 +475,34 @@ class Record extends \VuFind\View\Helper\Root\Record $onlineEditions = []; $recordLinkHelper = $this->getView()->plugin('recordLink'); foreach ($this->driver->tryMethod('getAdditionals') as $add) { - if (isset($add['identifier']) - && in_array($add['identifier'], $online_keys)) { + if ( + isset($add['identifier']) + && + ( + in_array($add['identifier'], $online_keys) + || + ( + isset($add['note']) + && + in_array($add['note'], $online_keys) + ) + ) + ) { if (isset($add['id'])) { $link = $recordLinkHelper->getTabUrl($add['id'], 'Holdings'); if (!empty($link)) { $add['link'] = $link; $onlineEditions[] = $add; } + } elseif (isset($add['related_records'])) { + foreach ($add['related_records']['first_results'] as $linkedRecord) { + $link = $recordLinkHelper->getTabUrl($linkedRecord); + if (!empty($link)) { + //overwrite link in entry and add to result array + $add['link'] = $link; + $onlineEditions[] = $add; + } + } } } } diff --git a/themes/finc/templates/RecordDriver/DefaultRecord/data-additionals.phtml b/themes/finc/templates/RecordDriver/DefaultRecord/data-additionals.phtml index 7ee1dc4a8e260244adb97a318820e7075cf26164..25db346933b87109240b3531290d510c94295262 100644 --- a/themes/finc/templates/RecordDriver/DefaultRecord/data-additionals.phtml +++ b/themes/finc/templates/RecordDriver/DefaultRecord/data-additionals.phtml @@ -9,6 +9,15 @@ <a href="<?=$url?>"><?=$this->escapeHtml($additional['text'])?></a> <?php endif; ?> <?php unset($url) ?> + <?php elseif (isset($additional['related_records'])): ?> + <?=$this->render( + 'RecordDriver/DefaultRecord/data-relatedRecords', + [ + 'data' => $additional['related_records'], + 'driver' => $this->driver, + 'label' => $additional['text'], + ] + ) ?> <?php else: ?> <?=$this->escapeHtml($additional['text'])?> <?php endif; ?> diff --git a/themes/finc/templates/RecordDriver/DefaultRecord/data-relatedRecords.phtml b/themes/finc/templates/RecordDriver/DefaultRecord/data-relatedRecords.phtml new file mode 100644 index 0000000000000000000000000000000000000000..ae4992a21951a38c840dc8aff2800fda90db014a --- /dev/null +++ b/themes/finc/templates/RecordDriver/DefaultRecord/data-relatedRecords.phtml @@ -0,0 +1,17 @@ +<?php $first_other = TRUE; +foreach ($data['first_results'] as $other): ?> + <?php if ($first_other): ?> + <?php $first_other = FALSE; + if (!isset($label) || empty($label)) { + $label = $other->getTitle(); + } + ?> + <?php else: ?> + <?php $label = $other->getTitle() ?> + <br/> + <?php endif; ?> + <a href="<?= $this->recordLink()->getUrl($other->getUniqueId()) ?>"><?= $this->escapeHtml($label) ?></a> +<?php endforeach; ?> +<?php if (isset($data['more_query'])): ?> + <br/><a href="<?= $this->record($this->driver)->getLink('related', $data['more_query']) ?>"><?= $this->translate('More') ?> ...</a> +<?php endif; ?> \ No newline at end of file