From 3e1bf969703021281a2ee6c4b7e1380fde478450 Mon Sep 17 00:00:00 2001 From: Marcel de Rooy Date: Sat, 23 Oct 2021 12:23:09 +0000 Subject: [PATCH] Bug 29314: Move OpenURL code from XSLT to Koha::Biblio Content-Type: text/plain; charset=utf-8 We need to pass the xslt variable OpenURLResolverURL in a few opac code locations: opac detail, results (via C4), shelves and tags. The logic with itemtypes is moved to Koha::Biblio. Added a few trivial tests. Test plan: Inspect web page and look for OpenURL when enabled (opac detail and results). Run t/db_dependent/Koha/Biblio.t Signed-off-by: Marcel de Rooy --- C4/Search.pm | 9 +++++---- C4/XSLT.pm | 20 +------------------- Koha/Biblio.pm | 13 ++++++++++--- opac/opac-detail.pl | 1 + opac/opac-shelves.pl | 3 ++- opac/opac-tags.pl | 3 ++- t/db_dependent/Koha/Biblio.t | 17 +++++++++++++++-- 7 files changed, 36 insertions(+), 30 deletions(-) diff --git a/C4/Search.pm b/C4/Search.pm index 441cbc1c6b..28bec4e682 100644 --- a/C4/Search.pm +++ b/C4/Search.pm @@ -1958,6 +1958,9 @@ sub searchResults { push @available_items_loop, $available_items->{$key} } + my $biblio_object = Koha::Biblios->find( $oldbiblio->{biblionumber} ); + $oldbiblio->{biblio_object} = $biblio_object; + # XSLT processing of some stuff # we fetched the sysprefs already before the loop through all retrieved record! if (!$scan) { @@ -1968,6 +1971,7 @@ sub searchResults { $record_processor->process($marcrecord); + my $url = $is_opac && C4::Context->preference('OPACShowOpenURL') ? $biblio_object->get_openurl : undef; $oldbiblio->{XSLTResultsRecord} = XSLTParse4Display( { biblionumber => $oldbiblio->{biblionumber}, @@ -1979,14 +1983,11 @@ sub searchResults { ), fix_amps => 1, hidden_items => \@hiddenitems, - xslt_variables => $xslt_variables + xslt_variables => { %$xslt_variables, OpenURLResolverURL => $url }, } ); } - my $biblio_object = Koha::Biblios->find( $oldbiblio->{biblionumber} ); - $oldbiblio->{biblio_object} = $biblio_object; - my $can_place_holds = 1; # if biblio level itypes are used and itemtype is notforloan, it can't be reserved either if (!C4::Context->preference("item-level_itypes")) { diff --git a/C4/XSLT.pm b/C4/XSLT.pm index 125f887cb0..e761415ef0 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -248,7 +248,7 @@ sub XSLTParse4Display { my $xslsyspref = $params->{xsl_syspref}; my $fixamps = $params->{fix_amps}; my $hidden_items = $params->{hidden_items} || []; - my $variables = $params->{xslt_variables}; + my $variables = $params->{xslt_variables} // {}; my $items_rs = $params->{items_rs}; my $xslfilename = get_xsl_filename( $xslsyspref); @@ -263,24 +263,6 @@ sub XSLTParse4Display { } my $xmlrecord = $record->as_xml(C4::Context->preference('marcflavour')); - $variables ||= {}; - my $biblio; - if (C4::Context->preference('OPACShowOpenURL')) { - my @biblio_itemtypes; - $biblio //= Koha::Biblios->find($biblionumber); - if (C4::Context->preference('item-level_itypes')) { - @biblio_itemtypes = $biblio->items->get_column("itype"); - } else { - push @biblio_itemtypes, $biblio->itemtype; - } - my @itypes = split( /\s/, C4::Context->preference('OPACOpenURLItemTypes') ); - my %original = (); - map { $original{$_} = 1 } @biblio_itemtypes; - if ( grep { $original{$_} } @itypes ) { - $variables->{OpenURLResolverURL} = $biblio->get_openurl; - } - } - my $varxml = "\n"; while (my ($key, $value) = each %$variables) { $varxml .= "$value\n"; diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index f325e5c0f4..f21c86f3bc 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -22,6 +22,7 @@ use Modern::Perl; use List::MoreUtils qw( any ); use URI; use URI::Escape qw( uri_escape_utf8 ); +use Array::Utils qw( intersect ); use C4::Koha qw( GetNormalizedISBN ); use C4::XSLT qw( transformMARCXML4XSLT ); @@ -766,10 +767,17 @@ Returns url for OpenURL resolver set in OpenURLResolverURL system preference sub get_openurl { my ( $self ) = @_; + my $OpenURLResolverURL = C4::Context->preference('OpenURLResolverURL') or return; - my $OpenURLResolverURL = C4::Context->preference('OpenURLResolverURL'); + my @biblio_itemtypes; + if( C4::Context->preference('item-level_itypes') ) { + @biblio_itemtypes = $self->items->get_column("itype"); + } else { + push @biblio_itemtypes, $self->itemtype; + } + my @openurl_itypes = split( /\s/, C4::Context->preference('OPACOpenURLItemTypes') ); - if ($OpenURLResolverURL) { + if( intersect(@biblio_itemtypes, @openurl_itypes) ) { my $uri = URI->new($OpenURLResolverURL); if (not defined $uri->query) { @@ -779,7 +787,6 @@ sub get_openurl { } $OpenURLResolverURL .= $self->get_coins; } - return $OpenURLResolverURL; } diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl index aed49fde16..78fb8a6c38 100755 --- a/opac/opac-detail.pl +++ b/opac/opac-detail.pl @@ -667,6 +667,7 @@ for my $plugin_variables ( @plugin_responses ) { } $variables->{anonymous_session} = $borrowernumber ? 0 : 1; $variables->{show_analytics_link} = $show_analytics; +$variables->{OpenURLResolverURL} = C4::Context->preference('OPACShowOpenURL') ? $biblio->get_openurl : undef; $template->param( XSLTBloc => XSLTParse4Display({ biblionumber => $biblionumber, diff --git a/opac/opac-shelves.pl b/opac/opac-shelves.pl index 611baecb02..3d80081f6a 100755 --- a/opac/opac-shelves.pl +++ b/opac/opac-shelves.pl @@ -366,7 +366,8 @@ if ( $op eq 'view' ) { $this_item->{'ITEM_RESULTS'} = $items; my $variables = { - anonymous_session => ($loggedinuser) ? 0 : 1 + anonymous_session => ($loggedinuser) ? 0 : 1, + OpenURLResolverURL => C4::Context->preference('OPACShowOpenURL') ? $biblio->get_openurl : undef, }; $this_item->{XSLTBloc} = XSLTParse4Display( { diff --git a/opac/opac-tags.pl b/opac/opac-tags.pl index 63169e812a..b6d556376f 100755 --- a/opac/opac-tags.pl +++ b/opac/opac-tags.pl @@ -286,7 +286,8 @@ if ($loggedinuser) { $tag->{artreqpossible} = ( $art_req_itypes->{ $tag->{itemtype} // q{} } || $art_req_itypes->{ '*' } ) ? 1 : q{}; my $variables = { - anonymous_session => ($loggedinuser) ? 0 : 1 + anonymous_session => ($loggedinuser) ? 0 : 1, + OpenURLResolverURL => C4::Context->preference('OPACShowOpenURL') ? $biblio->get_openurl : undef, }; $tag->{XSLTBloc} = XSLTParse4Display( { diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index 8b8f963588..0bfc3ec7c0 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -145,8 +145,7 @@ subtest 'items() tests' => sub { }; subtest 'get_coins and get_openurl' => sub { - - plan tests => 4; + plan tests => 7; $schema->storage->txn_begin; @@ -171,6 +170,8 @@ subtest 'get_coins and get_openurl' => sub { 'GetCOinsBiblio returned right metadata if biblio does not have a title' ); + my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + t::lib::Mocks::mock_preference( 'OPACOpenURLItemTypes', $item->itype ); t::lib::Mocks::mock_preference("OpenURLResolverURL", "https://koha.example.com/"); is( $biblio->get_openurl, @@ -185,6 +186,18 @@ subtest 'get_coins and get_openurl' => sub { 'Koha::Biblio->get_openurl returned right URL' ); + # Now change itemtypes in the pref, preventing coins call + my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } ); + my $itemtype2 = $builder->build_object( { class => 'Koha::ItemTypes' } ); + t::lib::Mocks::mock_preference( 'OPACOpenURLItemTypes', $itemtype1->itemtype. ' '. $itemtype2->itemtype ); + is( $biblio->get_openurl, C4::Context->preference("OpenURLResolverURL"), "No suffix expected now" ); + + # And clear the pref + t::lib::Mocks::mock_preference("OpenURLResolverURL", undef); + is( $biblio->get_openurl, undef, "Undef when pref undefined" ); + t::lib::Mocks::mock_preference("OpenURLResolverURL", q{}); + is( $biblio->get_openurl, undef, "Undef when pref empty" ); + $schema->storage->txn_rollback; }; -- 2.20.1