From 47f3059bbb0c4a94400d605fe8554c10ba225a27 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 13 Aug 2024 17:22:50 +0000 Subject: [PATCH] Bug 31161: Suppress OAI records when all items hidden by OpacHiddenItems This patch set adds an option to Koha::Metadata::Record to suppress a record if all items are hidden. This option is only enabled by OAI and only when OpacHiddenItemsHidesRecord is enabled. This test plan assumes KTD default data To setup: edit/create /etc/koha/sites/kohadev/oai.conf with content below: format: marcxml: metadataPrefix: marcxml metadataNamespace: http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim schema: http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd include_items: 1 Set sysprefs: OAI-PMH: Enable OAI-PMH:ConfFile: /etc/koha/sites/kohadev/oai.conf OAI-PMH:AutoUpdateSets: Enable OAI-PMH:AutoUpdateSetsEmbedItemData: Enabled OpacHiddenItems: itype: [BK] OpacHiddenItemsHidesRecord: Hide Adminsitration->OAI set configuration->New set Choose anything for setSpec and setName On new set: Actrion->define mappings Field: 942 Subfield: c Operator: is equal to Value: MU To test: 1 - Set above, do not apply patch, restart_all 2 - Visit: http://localhost:8080/cgi-bin/koha/oai.pl?verb=ListRecords&metadataPrefix=marcxml 3 - Confirm record 1 is visible wit no items (or anothe record that has BK itemtypes only) 4 - perl misc/migration_tools/build_oai_sets.pl -r -v -i 5 - Confirm 11 records in set created above (or correct number for your site) 6 - Apply patches, restart_all 7 - Repeat 2, note "Results fetched" is 43-50, count is off because of hidden records 8 - Confirm record 1 and many others not shown 9 - Confirm 'Show more' works 10 - Repeat 4, still 11 11 - Find a record with record type music, say 315 12 - Edit all items and change type to BK - Books 13 - Repeat 4 - now 10 records 14 - Set pref OpacHiddenItemsHidesRecord: Don't hide 15 - Repeat 2 and 4 and confirm results as before patch --- Koha/Biblio/Metadata.pm | 6 ++++-- Koha/OAI/Server/ListBase.pm | 6 ++++-- Koha/OAI/Server/Repository.pm | 22 +++++++++++++++++++--- misc/migration_tools/build_oai_sets.pl | 3 +++ 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Koha/Biblio/Metadata.pm b/Koha/Biblio/Metadata.pm index 1b6854f0bf4..effaa91a004 100644 --- a/Koha/Biblio/Metadata.pm +++ b/Koha/Biblio/Metadata.pm @@ -130,7 +130,7 @@ sub record { } if ( $embed_items ) { - $self->_embed_items({ %$params, format => $format, record => $record }); + $record = $self->_embed_items({ %$params, format => $format, record => $record }); } return $record; @@ -220,6 +220,7 @@ sub _embed_items { my $itemnumbers = $params->{itemnumbers} // []; my $patron = $params->{patron}; my $opac = $params->{opac}; + my $suppress = $params->{suppress_if_items_hidden}; if ( $format eq 'marcxml' ) { @@ -235,8 +236,9 @@ sub _embed_items { if ( @$itemnumbers ) { $items = $items->search({ itemnumber => { -in => $itemnumbers } }); } - if ( $opac ) { + if ( $opac && $items->count > 0 ) { $items = $items->filter_by_visible_in_opac({ patron => $patron }); + return if $suppress && $items->count < 1; } my @itemnumbers = $items->get_column('itemnumber'); my @item_fields; diff --git a/Koha/OAI/Server/ListBase.pm b/Koha/OAI/Server/ListBase.pm index 0c4f3a85450..414b7d9e4a2 100644 --- a/Koha/OAI/Server/ListBase.pm +++ b/Koha/OAI/Server/ListBase.pm @@ -157,8 +157,8 @@ sub GetRecords { push @setSpecs, $_->{spec}; } if ( $metadata ) { - my ( $marcxml, $marcxml_error ); - ( $marcxml, $marcxml_error ) = $repository->get_biblio_marcxml( $biblionumber, $format ) if !$deleted; + my ( $marcxml, $marcxml_error, $suppressed ); + ( $marcxml, $marcxml_error, $suppressed ) = $repository->get_biblio_marcxml( $biblionumber, $format ) if !$deleted; my %params; $params{identifier} = $repository->{koha_identifier} . ':' . $biblionumber; $params{metadataPrefix} = $token->{metadata_prefix}; @@ -170,6 +170,8 @@ sub GetRecords { %params ) ); + } elsif ($suppressed) { + next; } elsif ($marcxml_error) { my $record = MARC::Record->new(); my $marcxml = $record->as_xml_record(); diff --git a/Koha/OAI/Server/Repository.pm b/Koha/OAI/Server/Repository.pm index 4ec02811abc..3b2cad4e460 100644 --- a/Koha/OAI/Server/Repository.pm +++ b/Koha/OAI/Server/Repository.pm @@ -176,7 +176,7 @@ sub get_biblio_marcxml { my $with_items = 0; my $expanded_avs = 0; if ( my $conf = $self->{conf} ) { - $with_items = $conf->{format}->{$format}->{include_items }; + $with_items = $conf->{format}->{$format}->{include_items}; $expanded_avs = $conf->{format}->{$format}->{expanded_avs}; } @@ -185,13 +185,29 @@ sub get_biblio_marcxml { # Koha::Biblio::Metadata->record throws an exception on bad encoding, # we don't want OAI harvests to die, so we catch and warn, and try to clean the record - eval { $record = $biblio->metadata->record( { embed_items => $with_items, opac => 1 } ); }; + eval { + $record = $biblio->metadata->record( + { + embed_items => $with_items, opac => 1, + suppress_if_items_hidden => C4::Context->preference('OpacHiddenItemsHidesRecord') + } + ); + }; my $decoding_error; if ($@) { my $exception = $@; $exception->rethrow unless ( $exception->isa('Koha::Exceptions::Metadata::Invalid') ); $decoding_error = "INVALID_METADATA"; - $record = $biblio->metadata->record_strip_nonxml( { embed_items => $with_items, opac => 1 } ); + $record = $biblio->metadata->record_strip_nonxml( + { + embed_items => $with_items, opac => 1, + suppress_if_items_hidden => C4::Context->preference('OpacHiddenItemsHidesRecord') + } + ); + } + if ( $biblio && !$record ){ + # We had a biblio, but didn't get a record, assuming it was suppressed by hidden items (otherwise should have been error) + return( undef, undef, "hidden_by_hidden_items" ); } if ( $record && $expanded_avs ) { my $frameworkcode = GetFrameworkCode($biblionumber) || ''; diff --git a/misc/migration_tools/build_oai_sets.pl b/misc/migration_tools/build_oai_sets.pl index a312d3e0f94..c0528d5ee50 100755 --- a/misc/migration_tools/build_oai_sets.pl +++ b/misc/migration_tools/build_oai_sets.pl @@ -146,8 +146,11 @@ foreach my $res (@$results) { record => $record, embed_items => 1, biblionumber => $biblionumber, + opac => 1, + suppress_if_items_hidden => C4::Context->preference('OpacHiddenItemsHidesRecord'), } ); + next unless $record; } my @biblio_sets = CalcOAISetsBiblio($record, $mappings); -- 2.39.2