From 9ac7e378ec8933ed22552d43d48d0e7da7972950 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 16 Jun 2021 11:57:31 +0000 Subject: [PATCH] Bug 27138: Add preference EmbedHostItemsInSearchEngine --- C4/Search.pm | 2 +- C4/XSLT.pm | 3 +- Koha/Biblio/Metadata.pm | 36 ++++++++----------- installer/data/mysql/mandatory/sysprefs.sql | 1 + .../modules/admin/preferences/searching.pref | 7 ++++ misc/migration_tools/rebuild_zebra.pl | 18 ++-------- t/db_dependent/Items.t | 19 ++++++++-- 7 files changed, 45 insertions(+), 41 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index a8c89b4998a..debc709148e 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1799,7 +1799,7 @@ sub searchResults { my $marcflavor = C4::Context->preference("marcflavour"); # adding linked items that belong to host records - if ( C4::Context->preference('EasyAnalyticalRecords') ) { + if ( C4::Context->preference('EasyAnalyticalRecords') && !C4::Context->preference('EmbedHostItemsInSearchEngine') ) { my $analyticsfield = '773'; if ( $marcflavor eq 'MARC21' ) { $analyticsfield = '773'; diff --git a/C4/XSLT.pm b/C4/XSLT.pm index cd7cd520dd6..80c8af4fe96 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -281,7 +281,8 @@ Is only used in this module currently. sub buildKohaItemsNamespace { my ( $biblionumber, $hidden_items, $items_rs ) = @_; - my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber ); + my @host_itemnumbers = (); + @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber ) if C4::Context->preference('EmbedHostItemsInSearchEngine'); $hidden_items ||= []; my $query = {}; diff --git a/Koha/Biblio/Metadata.pm b/Koha/Biblio/Metadata.pm index 9d92f402151..a1a22be7df3 100644 --- a/Koha/Biblio/Metadata.pm +++ b/Koha/Biblio/Metadata.pm @@ -20,9 +20,9 @@ use Modern::Perl; use MARC::File::XML; use Scalar::Util qw( blessed ); -use C4::Biblio qw( GetMarcFromKohaField ); +use C4::Biblio qw( GetMarcFromKohaField ); use C4::Charset qw( StripNonXmlChars ); -use C4::Items qw( GetMarcItem get_hostitemnumbers_of ); +use C4::Items qw( GetMarcItem get_hostitemnumbers_of ); use Koha::Database; use Koha::Exceptions::Metadata; @@ -227,31 +227,23 @@ sub _embed_items { if ( $format eq 'marcxml' ) { -<<<<<<< HEAD - my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber"); - my $biblio = Koha::Biblios->find($biblionumber); - - my $items = $biblio->items; - if (@$itemnumbers) { - $items = $items->search( { itemnumber => { -in => $itemnumbers } } ); -======= # First remove the existing items from the MARC record - my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" ); + my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber"); foreach my $field ( $record->field($itemtag) ) { $record->delete_field($field); } - push @$itemnumbers, C4::Items::get_hostitemnumbers_of( $biblionumber ) if $params->{embed_host_items}; - - my $items = Koha::Items->search([ - 'me.biblionumber' => $biblionumber, - 'me.itemnumber' => { - -in => $itemnumbers - } - ]); - if ( @$itemnumbers ) { - $items = $items->search({ itemnumber => { -in => $itemnumbers } }); ->>>>>>> Bug 27138: Index host items in child records when sending to search engine + push @$itemnumbers, C4::Items::get_hostitemnumbers_of($biblionumber) + if $params->{embed_host_items} && C4::Context->preference('EmbedHostItemsInSearchEngine'); + + my $items = Koha::Items->search( + [ + 'me.biblionumber' => $biblionumber, + 'me.itemnumber' => { -in => $itemnumbers } + ] + ); + if (@$itemnumbers) { + $items = $items->search( { itemnumber => { -in => $itemnumbers } } ); } if ($opac) { $items = $items->filter_by_visible_in_opac( { patron => $patron } ); diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 76954d20229..90107f28bac 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -250,6 +250,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('EmailPatronWhenHoldIsPlaced', '0', NULL, 'Email patron when a hold has been placed for them', 'YesNo'), ('EmailPurchaseSuggestions','0','0|EmailAddressForSuggestions|BranchEmailAddress|KohaAdminEmailAddress','Choose email address that new purchase suggestions will be sent to: ','Choice'), ('EmailSMSSendDriverFromAddress', '', '', 'Email SMS send driver from address override', 'Free'), +('EmbedHostItemsInSearchEngine','0','','Add host items to records in the search engine','YesNo'), ('EnableAdvancedCatalogingEditor','0','','Enable the Rancor advanced cataloging editor','YesNo'), ('EnableBorrowerFiles','0',NULL,'If enabled, allows librarians to upload and attach arbitrary files to a borrower record.','YesNo'), ('EnableExpiredPasswordReset', '0', NULL, 'Enable ability for patrons with expired password to reset their password directly', 'YesNo'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref index 621c5bf6b3c..f313b69a1cf 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref @@ -131,6 +131,13 @@ Searching: - "
ccode: [REF,ARC]

- would mark all REF and ARC collections (Reference and Archives) items as unavailable in Z3950 results.

" - "You can define multiple keys and values, items meeting ANY of the criteria will be marked unavailable:

" - "
itype: [REF]
holdingbranch: [REPAIR]

- would mark all reference items AND items at the REPAIR branch as unavailable." + - + - pref: EmbedHostItemsInSearchEngine + default: 0 + choices: + 1: Embed + 0: Don't embed + - 'host items in records in the search engine. Requires EasyAnalyticalRecords' Search form: - - pref: LoadSearchHistoryToTheFirstLoggedUser diff --git a/misc/migration_tools/rebuild_zebra.pl b/misc/migration_tools/rebuild_zebra.pl index 6d706fa41f3..cd2ea47f080 100755 --- a/misc/migration_tools/rebuild_zebra.pl +++ b/misc/migration_tools/rebuild_zebra.pl @@ -583,7 +583,8 @@ sub export_marc_records_from_sth { if ( $record_type eq 'biblio' ) { my $biblio = Koha::Biblios->find($record_number); next unless $biblio; - my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $record_number ); + my @host_itemnumbers = (); + @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $record_number ) if C4::Context->preference('EmbedHostItemsInSearchEngine'); my $items = Koha::Items->search({ -or => [ biblionumber => $record_number, @@ -773,25 +774,12 @@ sub get_raw_marc_record { my ( $record_type, $record_number ) = @_; my $marc; -<<<<<<< HEAD if ( $record_type eq 'biblio' ) { -======= - if ($record_type eq 'biblio') { -<<<<<<< HEAD ->>>>>>> Bug 27138: Index host items in child records when sending to search engine eval { my $biblio = Koha::Biblios->find($record_number); - $marc = $biblio->metadata_record( { embed_items => 1 } ); + $marc = $biblio->metadata->record({ embed_items => 1, embed_host_items => 1 }); }; -<<<<<<< HEAD - if ( $@ || !$marc ) { - -======= -======= - eval { $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $record_number, embed_items => 1, embed_host_items => 1 }); }; ->>>>>>> Bug 27138: Index host items in child records when sending to search engine if ($@ || !$marc) { ->>>>>>> Bug 27138: Index host items in child records when sending to search engine # here we do warn since catching an exception # means that the bib was found but failed # to be parsed diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index c78b758d349..666902f1676 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -725,7 +725,7 @@ subtest 'Koha::Item(s) tests' => sub { }; subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { - plan tests => 11; + plan tests => 12; $schema->storage->txn_begin(); @@ -769,6 +769,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { push @itemnumbers, $itemnumber; } + t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', '1' ); my $host_item = $builder->build_sample_item({ homebranch => $library2->{branchcode} }); my $child_field = PrepHostMarcField( $host_item->biblionumber, $host_item->itemnumber, 'MARC21'); my $child_record = $biblio->metadata->record; @@ -804,12 +805,26 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { @items = $record->field($itemfield); is( scalar @items, $number_of_items + 1, 'Should return all items plus host items' ); + t::lib::Mocks::mock_preference('EmbedHostItemsInSearchEngine', '1'); $marc_with_items = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber, embed_items => 1, embed_host_items => 1 }); - is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches'); + is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with embed host items matches if embedding items in search engine'); + $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber }); + C4::Biblio::EmbedItemsInMarcBiblio({ + marc_record => $record, + biblionumber => $biblio->biblionumber, + embed_host_items => 0 + }); + t::lib::Mocks::mock_preference('EmbedHostItemsInSearchEngine', '0'); + $marc_with_items = C4::Biblio::GetMarcBiblio({ + biblionumber => $biblio->biblionumber, + embed_items => 1, + embed_host_items => 1 + }); + is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with embed host items does not include host item if not embedding items in search engine'); C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, -- 2.39.5