From 6cdb0be6fde01ba89dcd9386ae9d85acae7f0b80 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Fri, 27 May 2022 14:14:38 +0100 Subject: [PATCH] Bug 30848: Move caching level --- C4/Biblio.pm | 67 +++++++++-- Koha/Filter/MARC/ExpandAuthorizedValues.pm | 131 +++++++-------------- 2 files changed, 100 insertions(+), 98 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 459a94510a..b032860543 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -98,6 +98,7 @@ use C4::Charset qw( SetUTF8Flag StripNonXmlChars ); +use C4::Languages; use C4::Linker; use C4::OAI::Sets; use C4::Items qw( GetHiddenItemnumbers GetMarcItem ); @@ -1464,25 +1465,56 @@ descriptions rather than normal ones when they exist. sub GetAuthorisedValueDesc { my ( $tag, $subfield, $value, $framework, $tagslib, $category, $opac ) = @_; + my $cache = Koha::Caches->get_instance(); + my $cache_key; if ( !$category ) { return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'}; #---- branch if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) { - my $branch = Koha::Libraries->find($value); - return $branch? $branch->branchname: q{}; + $cache_key = "LibraryNames"; + my $libraries = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); + if ( !$libraries ) { + $libraries = { + map { $_->branchcode => $_->branchname } + Koha::Libraries->search( {}, + { columns => [ 'branchcode', 'branchname' ] } ) + ->as_list + }; + $cache->set_in_cache($cache_key, $libraries); + } + return $libraries->{$value}; } #---- itemtypes if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) { - my $itemtype = Koha::ItemTypes->find( $value ); - return $itemtype ? $itemtype->translated_description : q||; + my $lang = C4::Languages::getlanguage; + $lang //= 'en'; + $cache_key = $lang . 'ItemTypeDescriptions'; + my $itypes = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); + if ( !$itypes ) { + $itypes = + { map { $_->itemtype => $_->translated_description } + Koha::ItemTypes->search()->as_list }; + $cache->set_in_cache( $cache_key, $itypes ); + } + return $itypes->{$value}; } if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "cn_source" ) { - my $source = GetClassSource($value); - return $source ? $source->{description} : q||; + $cache_key = "ClassSources"; + my $cn_sources = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); + if ( !$cn_sources ) { + $cn_sources = { + map { $_->cn_source => $_->description } + Koha::ClassSources->search( {}, + { columns => [ 'cn_source', 'description' ] } ) + ->as_list + }; + $cache->set_in_cache($cache_key, $cn_sources); + } + return $cn_sources->{$value}; } #---- "true" authorized value @@ -1491,10 +1523,25 @@ sub GetAuthorisedValueDesc { my $dbh = C4::Context->dbh; if ( $category ne "" ) { - my $sth = $dbh->prepare( "SELECT lib, lib_opac FROM authorised_values WHERE category = ? AND authorised_value = ?" ); - $sth->execute( $category, $value ); - my $data = $sth->fetchrow_hashref; - return ( $opac && $data->{'lib_opac'} ) ? $data->{'lib_opac'} : $data->{'lib'}; + $cache_key = "AVDescriptions"; + my $av_descriptions = $cache->get_from_cache( $cache_key, { unsafe => 1 } ); + if ( !$av_descriptions ) { + $av_descriptions = { + map { + $_->authorised_value => + { lib => $_->lib, lib_opac => $_->lib_opac } + } Koha::AuthorisedValues->search( + { category => $category }, + { + columns => [ 'authorised_value', 'lib_opac', 'lib' ] + } + )->as_list + }; + $cache->set_in_cache($cache_key, $av_descriptions); + } + return ( $opac && $av_descriptions->{$value}->{'lib_opac'} ) + ? $av_descriptions->{$value}->{'lib_opac'} + : $av_descriptions->{$value}->{'lib'}; } else { return $value; # if nothing is found return the original value } diff --git a/Koha/Filter/MARC/ExpandAuthorizedValues.pm b/Koha/Filter/MARC/ExpandAuthorizedValues.pm index d49370031d..6c7b2a6837 100644 --- a/Koha/Filter/MARC/ExpandAuthorizedValues.pm +++ b/Koha/Filter/MARC/ExpandAuthorizedValues.pm @@ -49,6 +49,8 @@ Filter to replace Koha AV codes in MARC::Records with their Koha AV descriptions use Modern::Perl; +use C4::Biblio qw(GetAuthorisedValueDesc); + use Koha::Caches; use Koha::ClassSources; use Koha::Libraries; @@ -75,113 +77,66 @@ sub filter { my $opac = $interface eq 'opac' ? 1 : 0; my $marcflavour = C4::Context->preference('marcflavour'); - my $av = _getAuthorisedValues4MARCSubfields($frameworkcode); + my $coded_fields = _getCodedFields($frameworkcode); my $desc_field = $opac ? 'lib_opac' : 'lib'; - foreach my $tag ( keys %$av ) { - foreach my $field ( $record->field($tag) ) { - if ( $av->{$tag} ) { - my @new_subfields = (); - for my $subfield ( $field->subfields() ) { - my ( $letter, $value ) = @$subfield; - - # Replace the field value with the authorised value - # *except* for MARC21 field 942$n (suppression in opac) - if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) - || $marcflavour eq 'UNIMARC' ) - { - my $category = $av->{$tag}->{$letter}->{category}; - $value = $av->{$tag}->{$letter}->{$category}->{$value} - ->{$desc_field} || $value; - } - push( @new_subfields, $letter, $value ); + for my $tag ( keys %$coded_fields ) { + for my $field ( $record->field($tag) ) { + my @new_subfields = (); + for my $subfield ( $field->subfields() ) { + my ( $letter, $value ) = @$subfield; + + # Replace the field value with the authorised value + # *except* for MARC21 field 942$n (suppression in opac) + if ( !( $tag eq '942' && $subfield->[0] eq 'n' ) + || $marcflavour eq 'UNIMARC' ) + { + $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $coded_fields, undef, $opac ) if $coded_fields->{$tag}->{$letter}; } - $field->replace_with( - MARC::Field->new( - $tag, $field->indicator(1), - $field->indicator(2), @new_subfields - ) - ); + push( @new_subfields, $letter, $value ); } + $field->replace_with( + MARC::Field->new( + $tag, $field->indicator(1), + $field->indicator(2), @new_subfields + ) + ); } } return $record; } -sub _getAuthorisedValues4MARCSubfields { +sub _getCodedFields { my ($frameworkcode) = @_; $frameworkcode //= ""; my $cache = Koha::Caches->get_instance(); - my $cache_key = "MarcAVStructure-$frameworkcode"; + my $cache_key = "MarcCodedFields-$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_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} => - { lib => $_->lib, lib_opac => $_->lib_opac } - } @{ Koha::AuthorisedValues->search( - { category => $category }, - { - columns => [ 'authorised_value', 'lib_opac', 'lib' ] - } - )->unblessed + my $coded_fields = { + map { + $_->tagfield => { + $_->tagsubfield => { + 'authorised_value' => $_->authorised_value } - }; - $av_structure->{$tag}->{$letter}->{$category} = $authorised_values; - } - } + } + } Koha::MarcSubfieldStructures->search( + { + frameworkcode => $frameworkcode, + authorised_value => { '>' => '' } + }, + { + columns => [ 'tagfield', 'tagsubfield', 'authorised_value' ], + order_by => [ 'tagfield', 'tagsubfield' ] + } + )->as_list + }; - $cache->set_in_cache( $cache_key, $av_structure ); - return $av_structure; + $cache->set_in_cache( $cache_key, $coded_fields ); + return $coded_fields; } 1; -- 2.20.1