From daf2e4b3c236d981871bd349ebe19f3746f799eb Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 7 Jun 2022 15:24:23 +0100 Subject: [PATCH] Bug 30921: Replace C4::XSLT::transformMARCXML4XSLT with RecordProcessor MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This patch removes the last remaining uses of transformMARCXML4XSLT and replaced them with the equivilent RecordProcessor usage. Signed-off-by: David Nind Signed-off-by: Joonas Kylmälä Signed-off-by: Tomas Cohen Arazi --- C4/XSLT.pm | 91 ++++++----------------------------- Koha/OAI/Server/Repository.pm | 18 +++++-- t/db_dependent/XSLT.t | 15 +----- 3 files changed, 32 insertions(+), 92 deletions(-) diff --git a/C4/XSLT.pm b/C4/XSLT.pm index 0fb7be0d6c..8c2b4c1bcd 100644 --- a/C4/XSLT.pm +++ b/C4/XSLT.pm @@ -28,22 +28,18 @@ use C4::Koha qw( xml_escape ); use C4::Biblio qw( GetAuthorisedValueDesc GetFrameworkCode GetMarcStructure ); use Koha::AuthorisedValues; use Koha::ItemTypes; +use Koha::RecordProcessor; use Koha::XSLT::Base; use Koha::Libraries; use Koha::Recalls; my $engine; #XSLT Handler object -my %authval_per_framework; - # Cache for tagfield-tagsubfield to decode per framework. - # Should be preferably be placed in Koha-core... our (@ISA, @EXPORT_OK); BEGIN { require Exporter; @ISA = qw(Exporter); @EXPORT_OK = qw( - transformMARCXML4XSLT - getAuthorisedValues4MARCSubfields buildKohaItemsNamespace XSLTParse4Display ); @@ -56,75 +52,6 @@ C4::XSLT - Functions for displaying XSLT-generated content =head1 FUNCTIONS -=head2 transformMARCXML4XSLT - -Replaces codes with authorized values in a MARC::Record object - -=cut - -sub transformMARCXML4XSLT { - my ($biblionumber, $record, $opac) = @_; - my $frameworkcode = GetFrameworkCode($biblionumber) || ''; - my $tagslib = &GetMarcStructure(1, $frameworkcode, { unsafe => 1 }); - my @fields; - # FIXME: wish there was a better way to handle exceptions - eval { - @fields = $record->fields(); - }; - if ($@) { warn "PROBLEM WITH RECORD"; next; } - my $marcflavour = C4::Context->preference('marcflavour'); - my $av = getAuthorisedValues4MARCSubfields($frameworkcode); - 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' ) { - $value = GetAuthorisedValueDesc( $tag, $letter, $value, '', $tagslib, undef, $opac ) - if $av->{ $tag }->{ $letter }; - } - push( @new_subfields, $letter, $value ); - } - $field ->replace_with( MARC::Field->new( - $tag, - $field->indicator(1), - $field->indicator(2), - @new_subfields - ) ); - } - } - } - return $record; -} - -=head2 getAuthorisedValues4MARCSubfields - -Returns a ref of hash of ref of hash for tag -> letter controlled by authorised values -Is only used in this module currently. - -=cut - -sub getAuthorisedValues4MARCSubfields { - my ($frameworkcode) = @_; - unless ( $authval_per_framework{ $frameworkcode } ) { - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare("SELECT DISTINCT tagfield, tagsubfield - 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; - } - $authval_per_framework{ $frameworkcode } = $av; - } - return $authval_per_framework{ $frameworkcode }; -} - =head2 XSLTParse4Display Returns xml for biblionumber and requested XSLT transformation. @@ -245,8 +172,9 @@ sub XSLTParse4Display { my ( $params ) = @_; my $biblionumber = $params->{biblionumber}; - my $orig_record = $params->{record}; + my $record = $params->{record}; my $xslsyspref = $params->{xsl_syspref}; + my $interface = ( $xslsyspref =~ /OPAC/ ) ? 'opac' : 'intranet' ; my $fixamps = $params->{fix_amps}; my $hidden_items = $params->{hidden_items} || []; my $variables = $params->{xslt_variables}; @@ -257,8 +185,19 @@ sub XSLTParse4Display { my $xslfilename = get_xsl_filename( $xslsyspref); + my $frameworkcode = GetFrameworkCode($biblionumber) || ''; + my $record_processor = Koha::RecordProcessor->new( + { + filters => [ 'ExpandCodedFields' ], + options => { + interface => $interface, + frameworkcode => $frameworkcode + } + } + ); + $record_processor->process($record); + # grab the XML, run it through our stylesheet, push it out to the browser - my $record = transformMARCXML4XSLT($biblionumber, $orig_record); my $itemsxml; if ( $xslsyspref eq "OPACXSLTDetailsDisplay" || $xslsyspref eq "XSLTDetailsDisplay" || $xslsyspref eq "XSLTResultsDisplay" ) { $itemsxml = ""; #We don't use XSLT for items display on these pages diff --git a/Koha/OAI/Server/Repository.pm b/Koha/OAI/Server/Repository.pm index f8e19b12c1..886131318b 100644 --- a/Koha/OAI/Server/Repository.pm +++ b/Koha/OAI/Server/Repository.pm @@ -25,6 +25,7 @@ use HTTP::OAI::Repository qw( validate_request ); use base ("HTTP::OAI::Repository"); +use Koha::RecordProcessor; use Koha::OAI::Server::Identify; use Koha::OAI::Server::ListSets; use Koha::OAI::Server::ListMetadataFormats; @@ -35,7 +36,7 @@ use XML::SAX::Writer; use YAML::XS; use CGI qw/:standard -oldstyle_urls/; use C4::Context; -use C4::XSLT qw( transformMARCXML4XSLT ); +use C4::Biblio qw( GetFrameworkCode ); use Koha::XSLT::Base; use Koha::Biblios; @@ -178,8 +179,19 @@ sub get_biblio_marcxml { my $biblio = Koha::Biblios->find($biblionumber); my $record = $biblio->metadata->record({ embed_items => $with_items, opac => 1 }); - $record = transformMARCXML4XSLT( $biblionumber, $record ) - if $expanded_avs; + if ( $expanded_avs ) { + my $frameworkcode = GetFrameworkCode($biblionumber) || ''; + my $record_processor = Koha::RecordProcessor->new( + { + filters => [ 'ExpandCodedFields' ], + options => { + interface => 'opac', + frameworkcode => $frameworkcode + } + } + ); + $record_processor->process($record) if $expanded_avs; + } return $record ? $record->as_xml_record() : undef; } diff --git a/t/db_dependent/XSLT.t b/t/db_dependent/XSLT.t index 8f08abdb80..314996d488 100755 --- a/t/db_dependent/XSLT.t +++ b/t/db_dependent/XSLT.t @@ -18,7 +18,7 @@ use Modern::Perl; use MARC::Record; -use Test::More tests => 4; +use Test::More tests => 3; use Test::Warn; use t::lib::TestBuilder; use t::lib::Mocks; @@ -28,7 +28,7 @@ use Koha::Libraries; use Koha::ItemTypes; BEGIN { - use_ok('C4::XSLT', qw( transformMARCXML4XSLT getAuthorisedValues4MARCSubfields buildKohaItemsNamespace )); + use_ok('C4::XSLT', qw( buildKohaItemsNamespace )); } my $schema = Koha::Database->new->schema; @@ -36,17 +36,6 @@ my $builder = t::lib::TestBuilder->new; $schema->storage->txn_begin; -subtest 'transformMARCXML4XSLT tests' => sub { - plan tests => 1; - my $mock_xslt = Test::MockModule->new("C4::XSLT"); - $mock_xslt->mock( getAuthorisedValues4MARCSubfields => sub { return { 942 => { 'n' => 1 } } } ); - $mock_xslt->mock( GetAuthorisedValueDesc => sub { warn "called"; }); - my $record = MARC::Record->new(); - my $suppress_field = MARC::Field->new( 942, ' ', ' ', n => '1' ); - $record->append_fields($suppress_field); - warning_is { C4::XSLT::transformMARCXML4XSLT( 3,$record ) } undef, "942n auth value not translated"; -}; - subtest 'buildKohaItemsNamespace status tests' => sub { plan tests => 17; -- 2.39.5