From 6bcf1d7e4dc0cb48915e07b5c0f31a1d13260f23 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Thu, 26 May 2022 16:31:30 +0100 Subject: [PATCH] Bug 30848: Caching for performance --- Koha/Filter/MARC/ExpandAuthorizedValues.pm | 88 ++++++++++++++----- .../Koha/Filter/ExpandAuthorizedValues.t | 1 + 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/Koha/Filter/MARC/ExpandAuthorizedValues.pm b/Koha/Filter/MARC/ExpandAuthorizedValues.pm index 3306839728..c9b638be03 100644 --- a/Koha/Filter/MARC/ExpandAuthorizedValues.pm +++ b/Koha/Filter/MARC/ExpandAuthorizedValues.pm @@ -50,12 +50,14 @@ Filter to replace Koha AV codes in MARC::Records with their Koha AV descriptions use Modern::Perl; use C4::Biblio qw( GetAuthorisedValueDesc GetMarcStructure ); +use Koha::Caches; +use Koha::ClassSources; +use Koha::Libraries; +use Koha::ItemTypes; use base qw(Koha::RecordProcessor::Base); our $NAME = 'ExpandAuthorizedValues'; -my %authval_per_framework; # Cache for tagfield-tagsubfield to decode per framework. - =head2 filter Embed items into the MARC::Record object. @@ -69,14 +71,14 @@ sub filter { return unless defined $record and ref($record) eq 'MARC::Record'; my $params = $self->params; - my $interface = $params->{options}->{interface} // 'opac'; - my $opac = $interface eq 'opac' ? 1 : 0; + my $interface = $params->{options}->{interface} // 'opac'; my $frameworkcode = $params->{options}->{frameworkcode} // q{}; - my $marcstructure = $params->{options}->{marcstructure} - // GetMarcStructure( 1, $frameworkcode, { unsafe => 1 } ); + my $opac = $interface eq 'opac' ? 1 : 0; my $marcflavour = C4::Context->preference('marcflavour'); my $av = _getAuthorisedValues4MARCSubfields($frameworkcode); + + my $desc_field = $opac ? 'lib_opac' : 'lib'; foreach my $tag ( keys %$av ) { foreach my $field ( $record->field($tag) ) { if ( $av->{$tag} ) { @@ -89,10 +91,9 @@ sub filter { if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) || $marcflavour eq 'UNIMARC' ) { - $value = - GetAuthorisedValueDesc( $tag, $letter, $value, '', - $marcstructure, undef, $opac ) - if $av->{$tag}->{$letter}; + my $category = $av->{$tag}->{$letter}->{category}; + $value = $av->{$tag}->{$letter}->{$category}->{$value} + ->{$desc_field} || $value; } push( @new_subfields, $letter, $value ); } @@ -111,23 +112,68 @@ sub filter { sub _getAuthorisedValues4MARCSubfields { my ($frameworkcode) = @_; - unless ( $authval_per_framework{$frameworkcode} ) { - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare( - "SELECT DISTINCT tagfield, tagsubfield + $frameworkcode //= ""; + + my $cache = Koha::Caches->get_instance(); + my $cache_key = "MarcAVStructure-$frameworkcode"; + my $cached = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); + return $cached if $cached; + + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare( + "SELECT DISTINCT tagfield, tagsubfield, authorised_value FROM marc_subfield_structure WHERE authorised_value IS NOT NULL AND authorised_value!='' AND frameworkcode=?" - ); - $sth->execute($frameworkcode); - my $av = {}; - while ( my ( $tag, $letter ) = $sth->fetchrow() ) { - $av->{$tag}->{$letter} = 1; + ); + $sth->execute($frameworkcode); + my $av_structure = {}; + while ( my ( $tag, $letter, $category ) = $sth->fetchrow() ) { + $av_structure->{$tag}->{$letter}->{category} = $category; + if ( $category eq 'branches' ) { + my $libraries = { + map { + $_->branchcode => + { lib_opac => $_->branchname, lib => $_->branchname } + } Koha::Libraries->search()->as_list + }; + $av_structure->{$tag}->{$letter}->{$category} = $libraries; + } + elsif ( $category eq 'itemtypes' ) { + my $itemtypes = { + map { + $_->itemtype => { + lib_opac => $_->translated_description, + lib => $_->translated_description + } + } Koha::ItemTypes->search()->as_list + }; + $av_structure->{$tag}->{$letter}->{$category} = $itemtypes; + } + elsif ( $category eq 'cn_source' ) { + my $cn_sources = { + map { + $_->cn_source => + { lib_opac => $_->description, lib => $_->description } + } Koha::ClassSources->search()->as_list + }; + $av_structure->{$tag}->{$letter}->{$category} = $cn_sources; + } + else { + my $authorised_values = { + map { $_->{authorised_value} => $_ } + @{ Koha::AuthorisedValues->search( { category => $category } ) + ->unblessed + } + }; + $av_structure->{$tag}->{$letter}->{$category} = + $authorised_values; } - $authval_per_framework{$frameworkcode} = $av; } - return $authval_per_framework{$frameworkcode}; + + $cache->set_in_cache( $cache_key, $av_structure ); + return $av_structure; } 1; diff --git a/t/db_dependent/Koha/Filter/ExpandAuthorizedValues.t b/t/db_dependent/Koha/Filter/ExpandAuthorizedValues.t index e2e6281dcc..ebdf9bb873 100644 --- a/t/db_dependent/Koha/Filter/ExpandAuthorizedValues.t +++ b/t/db_dependent/Koha/Filter/ExpandAuthorizedValues.t @@ -62,6 +62,7 @@ subtest 'ExpandAuthorizedValues tests' => sub { my $cache = Koha::Caches->get_instance; $cache->clear_from_cache("MarcStructure-0-"); $cache->clear_from_cache("MarcStructure-1-"); + $cache->clear_from_cache("MarcAVStructure-1-"); $cache->clear_from_cache("default_value_for_mod_marc-"); $cache->clear_from_cache("MarcSubfieldStructure-"); -- 2.20.1