From afa0d3751dab3025f658ee910c396d347c320999 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 19 Oct 2023 13:38:47 +0100 Subject: [PATCH] Bug 35099: Improve consistency This patch inproves the consistency between get_components_query and get_get_volumes_query methods so they both check for MARC21 and check that the record is valid prior to building the query. We also add a check for !$invalid_marc_record in the staff client details view to match the check for the equivilent compenents check. We need both as the OPAC doesn't do an early check for invalid records in the controller. Signed-off-by: Martin Renvoize --- Koha/Biblio.pm | 38 +++++++++++++++++++++++--------------- catalogue/detail.pl | 2 +- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 0326c1c8147..ef8436d275e 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -669,12 +669,20 @@ Returns a query which can be used to search for all component parts of MARC21 bi sub get_components_query { my ($self) = @_; - my $builder = Koha::SearchEngine::QueryBuilder->new( - { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); - my $marc = $self->metadata->record; + # MARC21 Only for now + return if ( C4::Context->preference('marcflavour') ne 'MARC21' ); + + # If the marc cannot be loaded, do not build the query + # the staff interface should already be displaying an error message in this case + # so we don't need to return the error + my $marc; + eval { $marc = $self->metadata->record }; + return unless $marc; + + my $builder = Koha::SearchEngine::QueryBuilder->new( { index => $Koha::SearchEngine::BIBLIOS_INDEX } ); my $component_sort_field = C4::Context->preference('ComponentSortField') // "title"; my $component_sort_order = C4::Context->preference('ComponentSortOrder') // "asc"; - my $sort = $component_sort_field . "_" . $component_sort_order; + my $sort = $component_sort_field . "_" . $component_sort_order; my $searchstr; if ( C4::Context->preference('UseControlNumber') ) { @@ -685,11 +693,12 @@ sub get_components_query { my $pf003 = $marc->field('003') || undef; if ( !defined($pf003) ) { + # search for 773$w='Host001' - $searchstr .= "rcn:\"" . $pf001->data()."\""; - } - else { + $searchstr .= "rcn:\"" . $pf001->data() . "\""; + } else { $searchstr .= "("; + # search for (773$w='Host001' and 003='Host003') or 773$w='(Host003)Host001' $searchstr .= "(rcn:\"" . $pf001->data() . "\" AND cni:\"" . $pf003->data() . "\")"; $searchstr .= " OR rcn:\"" . $pf003->data() . " " . $pf001->data() . "\""; @@ -700,20 +709,19 @@ sub get_components_query { $searchstr .= " AND (bib-level:a OR bib-level:b)"; $searchstr .= ")"; } - } - else { - my $cleaned_title = $marc->subfield('245', "a"); + } else { + my $cleaned_title = $marc->subfield( '245', "a" ); $cleaned_title =~ tr|/||; $cleaned_title = $builder->clean_search_term($cleaned_title); - $searchstr = qq#Host-item:("$cleaned_title")#; + $searchstr = qq#Host-item:("$cleaned_title")#; } - my ($error, $query ,$query_str) = $builder->build_query_compat( undef, [$searchstr], undef, undef, [$sort], 0 ); - if( $error ){ + my ( $error, $query, $query_str ) = $builder->build_query_compat( undef, [$searchstr], undef, undef, [$sort], 0 ); + if ($error) { warn $error; return; } - return ($query, $query_str, $sort); + return ( $query, $query_str, $sort ); } =head3 get_marc_volumes @@ -771,7 +779,7 @@ sub get_volumes_query { # the staff interface should already be displaying an error message in this case # so we don't need to return the error my $marc; - eval { $marc = $self->metadata->record;}; + eval { $marc = $self->metadata->record; }; return unless $marc; # Only build volumes query if we're in a 'Set' record diff --git a/catalogue/detail.pl b/catalogue/detail.pl index 428574e365b..9edaf3f3bbf 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -270,7 +270,7 @@ if ( $showcomp eq 'both' || $showcomp eq 'staff' ) { } # Display volumes link -my $show_volumes = @{ $biblio->get_marc_volumes(1) } ? 1 : 0; +my $show_volumes = ( !$invalid_marc_record && @{ $biblio->get_marc_volumes(1) } ) ? 1 : 0; # XSLT processing of some stuff my $xslt_variables = { -- 2.41.0