From 0b56eadaf46ea4f8b0588e01f6b070a9fcd0b484 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 6 Aug 2021 12:44:23 +0100 Subject: [PATCH] Bug 11175: (QA follow-up) Move get_component_part_query This patch removes Koha::Util::Search in preference to embedding the search query builder in Koha::Biblio as get_analytics_query. Signed-off-by: Martin Renvoize Signed-off-by: Andrew Nugged Bug 11175: (QA follow-up) Rename back to get_marc_components Signed-off-by: Martin Renvoize Signed-off-by: Andrew Nugged Signed-off-by: Marcel de Rooy Signed-off-by: Jonathan Druart --- C4/XSLT.pm | 3 +- Koha/Biblio.pm | 46 ++++++++++++++++++++++- Koha/Util/Search.pm | 73 ------------------------------------ catalogue/detail.pl | 1 - t/Koha/Util/Search.t | 54 -------------------------- t/db_dependent/Koha/Biblio.t | 32 ++++++++++++++-- 6 files changed, 74 insertions(+), 135 deletions(-) delete mode 100644 Koha/Util/Search.pm delete mode 100755 t/Koha/Util/Search.t diff --git a/C4/XSLT.pm b/C4/XSLT.pm index b98a10d4a9..8f216852ac 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -28,7 +28,6 @@ use C4::Koha qw( xml_escape ); use C4::Biblio qw( GetAuthorisedValueDesc GetFrameworkCode GetMarcStructure ); use Koha::AuthorisedValues; use Koha::ItemTypes; -use Koha::Util::Search; use Koha::XSLT::Base; use Koha::Libraries; @@ -299,7 +298,7 @@ sub XSLTParse4Display { $variables->{show_analytics_link} = 0; - my $search_query = Koha::Util::Search::get_component_part_query($biblionumber); + my $search_query = $biblio->get_analytics_query; $variables->{ComponentPartQuery} = $search_query; my @componentPartRecordXML = (''); diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index dac5445bb2..ca4fcce983 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -43,7 +43,6 @@ use Koha::Suggestions; use Koha::Subscriptions; use Koha::SearchEngine; use Koha::SearchEngine::Search; -use Koha::Util::Search; =head1 NAME @@ -493,7 +492,7 @@ sub get_marc_components { return [] if (C4::Context->preference('marcflavour') ne 'MARC21'); - my $searchstr = Koha::Util::Search::get_component_part_query($self->id); + my $searchstr = $self->get_components_query; if (defined($searchstr)) { my $searcher = Koha::SearchEngine::Search->new({index => $Koha::SearchEngine::BIBLIOS_INDEX}); @@ -504,6 +503,49 @@ sub get_marc_components { return $self->{_components} || []; } +=head2 get_components_query + +Returns a query which can be used to search for all component parts of MARC21 biblios + +=cut + +sub get_components_query { + my ($self) = @_; + + my $marc = $self->metadata->record; + + my $searchstr; + if ( C4::Context->preference('UseControlNumber') ) { + my $pf001 = $marc->field('001') || undef; + + if ( defined($pf001) ) { + my $pf003 = $marc->field('003') || undef; + + if ( !defined($pf003) ) { + # search for 773$w='Host001' + $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(); + $searchstr .= ")"; + } + + # limit to monograph and serial component part records + $searchstr .= " AND (bib-level:a OR bib-level:b)"; + } + } + else { + my $cleaned_title = $marc->title; + $cleaned_title =~ tr|/||; + $searchstr = "Host-item:($cleaned_title)"; + } + + return $searchstr; +} + =head3 subscriptions my $subscriptions = $self->subscriptions diff --git a/Koha/Util/Search.pm b/Koha/Util/Search.pm deleted file mode 100644 index 99ff38e77f..0000000000 --- a/Koha/Util/Search.pm +++ /dev/null @@ -1,73 +0,0 @@ -package Koha::Util::Search; - -# Copyright 2020 University of Helsinki -# -# This file is part of Koha. -# -# Koha is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# Koha is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Koha; if not, see . - -use Modern::Perl; - -use C4::Biblio qw( GetMarcBiblio ); - -=head1 NAME - -Koha::Util::Search - functions to build complex search queries - -=head1 FUNCTIONS - -=head2 get_component_part_query - -Returns a query which can be used to search for all component parts of MARC21 biblios - -=cut - -sub get_component_part_query { - my ($biblionumber) = @_; - - my $marc = GetMarcBiblio( { biblionumber => $biblionumber } ); - - my $searchstr; - if ( C4::Context->preference('UseControlNumber') ) { - my $pf001 = $marc->field('001') || undef; - - if ( defined($pf001) ) { - my $pf003 = $marc->field('003') || undef; - - if ( !defined($pf003) ) { - # search for 773$w='Host001' - $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(); - $searchstr .= ")"; - } - - # limit to monograph and serial component part records - $searchstr .= " AND (bib-level:a OR bib-level:b)"; - } - } - else { - my $cleaned_title = $marc->title; - $cleaned_title =~ tr|/||; - $searchstr = "Host-item:($cleaned_title)"; - } - - return $searchstr; -} - -1; diff --git a/catalogue/detail.pl b/catalogue/detail.pl index cd186c36b0..0dc82fce9a 100755 --- a/catalogue/detail.pl +++ b/catalogue/detail.pl @@ -211,7 +211,6 @@ foreach my $subscription (@subscriptions) { push @subs, \%cell; } - # Get acquisition details if ( C4::Context->preference('AcquisitionDetails') ) { my $orders = Koha::Acquisition::Orders->search( diff --git a/t/Koha/Util/Search.t b/t/Koha/Util/Search.t deleted file mode 100755 index 30008db150..0000000000 --- a/t/Koha/Util/Search.t +++ /dev/null @@ -1,54 +0,0 @@ -#!/usr/bin/perl -# -# Copyright 2020 University of Helsinki -# -# This file is part of Koha. -# -# Koha is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# Koha is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Koha; if not, see . - -use Modern::Perl; - -use Test::More tests => 1; - -use t::lib::Mocks; -use t::lib::TestBuilder; - -use C4::Biblio qw( GetMarcBiblio ModBiblioMarc ); -use Koha::Util::Search; -use MARC::Field; - -my $builder = t::lib::TestBuilder->new; - -subtest 'get_component_part_query' => sub { - plan tests => 3; - - my $biblio = $builder->build_sample_biblio(); - my $biblionumber = $biblio->biblionumber; - my $record = GetMarcBiblio({ biblionumber => $biblionumber }); - - t::lib::Mocks::mock_preference( 'UseControlNumber', '0' ); - is(Koha::Util::Search::get_component_part_query($biblionumber), "Host-item:(Some boring read)", "UseControlNumber disabled"); - - t::lib::Mocks::mock_preference( 'UseControlNumber', '1' ); - my $marc_001_field = MARC::Field->new('001', $biblionumber); - $record->append_fields($marc_001_field); - ModBiblioMarc($record, $biblionumber); - - is(Koha::Util::Search::get_component_part_query($biblionumber), "rcn:$biblionumber AND (bib-level:a OR bib-level:b)", "UseControlNumber enabled without MarcOrgCode"); - - my $marc_003_field = MARC::Field->new('003', 'OSt'); - $record->append_fields($marc_003_field); - ModBiblioMarc($record, $biblionumber); - is(Koha::Util::Search::get_component_part_query($biblionumber), "((rcn:$biblionumber AND cni:OSt) OR rcn:OSt $biblionumber) AND (bib-level:a OR bib-level:b)", "UseControlNumber enabled with MarcOrgCode"); -}; diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 2a9e49e136..abe420a6df 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,9 +17,9 @@ use Modern::Perl; -use Test::More tests => 17; +use Test::More tests => 18; -use C4::Biblio qw( AddBiblio ModBiblio ); +use C4::Biblio qw( AddBiblio ModBiblio ModBiblioMarc ); use Koha::Database; use Koha::Caches; use Koha::Acquisition::Orders; @@ -524,7 +524,7 @@ subtest 'get_marc_components() tests' => sub { is_deeply( [@components], - [()], + [[]], '->get_marc_components returns an empty ARRAY' ); @@ -542,6 +542,32 @@ subtest 'get_marc_components() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'get_components_query' => sub { + plan tests => 3; + + my $biblio = $builder->build_sample_biblio(); + my $biblionumber = $biblio->biblionumber; + my $record = $biblio->metadata->record; + + t::lib::Mocks::mock_preference( 'UseControlNumber', '0' ); + is($biblio->get_components_query, "Host-item:(Some boring read)", "UseControlNumber disabled"); + + t::lib::Mocks::mock_preference( 'UseControlNumber', '1' ); + my $marc_001_field = MARC::Field->new('001', $biblionumber); + $record->append_fields($marc_001_field); + C4::Biblio::ModBiblio( $record, $biblio->biblionumber ); + $biblio = Koha::Biblios->find( $biblio->biblionumber); + + is($biblio->get_components_query, "rcn:$biblionumber AND (bib-level:a OR bib-level:b)", "UseControlNumber enabled without MarcOrgCode"); + + my $marc_003_field = MARC::Field->new('003', 'OSt'); + $record->append_fields($marc_003_field); + C4::Biblio::ModBiblio( $record, $biblio->biblionumber ); + $biblio = Koha::Biblios->find( $biblio->biblionumber); + + is($biblio->get_components_query, "((rcn:$biblionumber AND cni:OSt) OR rcn:OSt $biblionumber) AND (bib-level:a OR bib-level:b)", "UseControlNumber enabled with MarcOrgCode"); +}; + subtest 'orders() and active_orders() tests' => sub { plan tests => 5; -- 2.39.5