From 6f36a3a6ba9fb890717facf25b77f5e9ae2684f5 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 19 May 2022 21:39:49 +0000 Subject: [PATCH] Bug 30813: Remove TransformMarcToKohaOneField MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit To test: 1 - prove -v t/db_dependent/Biblio/TransformMarcToKoha.t t/db_dependent/Breeding.t 2 - Test Z3950 search in advanced catalog editor Signed-off-by: Martin Renvoize Signed-off-by: Joonas Kylmälä Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- C4/Biblio.pm | 38 +------------------ C4/Breeding.pm | 38 ++++--------------- C4/XISBN.pm | 5 ++- Koha/MetaSearcher.pm | 27 ++++---------- Koha/MetadataRecord.pm | 2 +- t/db_dependent/Biblio/TransformMarcToKoha.t | 16 ++++---- t/db_dependent/Breeding.t | 41 +++++++-------------- 7 files changed, 41 insertions(+), 126 deletions(-) diff --git a/C4/Biblio.pm b/C4/Biblio.pm index 90cc4382fd..ef094ab375 100644 --- a/C4/Biblio.pm +++ b/C4/Biblio.pm @@ -65,7 +65,6 @@ BEGIN { TransformHtmlToMarc TransformHtmlToXml prepare_host_field - TransformMarcToKohaOneField ); # Internal functions @@ -2396,44 +2395,9 @@ sub _disambiguate { } -=head2 TransformMarcToKohaOneField - - $val = TransformMarcToKohaOneField( 'biblio.title', $marc ); - - Note: The authoritative Default framework is used implicitly. - -=cut - -sub TransformMarcToKohaOneField { - my ( $kohafield, $marc ) = @_; - - my ( @rv, $retval ); - my @mss = GetMarcSubfieldStructureFromKohaField($kohafield); - foreach my $fldhash ( @mss ) { - my $tag = $fldhash->{tagfield}; - my $sub = $fldhash->{tagsubfield}; - foreach my $fld ( $marc->field($tag) ) { - if( $sub eq '@' || $fld->is_control_field ) { - push @rv, $fld->data if $fld->data; - } else { - push @rv, grep { $_ } $fld->subfield($sub); - } - } - } - return unless @rv; - $retval = join ' | ', uniq(@rv); - - # Additional polishing for individual kohafields - if( $kohafield =~ /copyrightdate|publicationyear/ ) { - $retval = _adjust_pubyear( $retval ); - } - - return $retval; -} - =head2 _adjust_pubyear - Helper routine for TransformMarcToKohaOneField + Helper routine for TransformMarcToKoha =cut diff --git a/C4/Breeding.pm b/C4/Breeding.pm index 3f442926c4..48ea5acfab 100644 --- a/C4/Breeding.pm +++ b/C4/Breeding.pm @@ -304,16 +304,14 @@ sub _handle_one_result { my $breedingid = AddBiblioToBatch($batch_id, $seq, $marcrecord, 'UTF-8', 0); #Last zero indicates: no update for batch record counts - - #call to TransformMarcToKoha replaced by next call - #we only need six fields from the marc record my $row; - $row = _add_rowdata( - { - biblionumber => $bib, - server => $servhref->{servername}, - breedingid => $breedingid, - }, $marcrecord) if $breedingid; + if( $breedingid ){ + my @kohafields = ('biblio.title','biblio.author','biblioitems.isbn','biblioitems.lccn','biblioitems.editionstatement'); + push @kohafields, C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear'; + $row = TransformMarcToKoha({ record => $marcrecord, kohafields => \@kohafields, limit_table => 'no_items' }); + $row->{isbn}=_isbn_replace($row->{isbn}); + $row = _add_custom_field_rowdata($row, $marcrecord); + } return ( $row, $error ); } @@ -341,28 +339,6 @@ sub _do_xslt_proc { } } -sub _add_rowdata { - my ($row, $record)=@_; - my %fetch= ( - title => 'biblio.title', - author => 'biblio.author', - isbn =>'biblioitems.isbn', - lccn =>'biblioitems.lccn', #LC control number (not call number) - edition =>'biblioitems.editionstatement' - ); - $fetch{date} = C4::Context->preference('marcflavour') eq "MARC21" ? 'biblio.copyrightdate' : 'biblioitems.publicationyear'; - - foreach my $k (keys %fetch) { - $row->{$k} = C4::Biblio::TransformMarcToKohaOneField( $fetch{$k}, $record ); - } - $row->{date}//= $row->{date2}; - $row->{isbn}=_isbn_replace($row->{isbn}); - - $row = _add_custom_field_rowdata($row, $record); - - return $row; -} - sub _add_custom_field_rowdata { my ( $row, $record ) = @_; diff --git a/C4/XISBN.pm b/C4/XISBN.pm index c6e03d3071..466c2da169 100644 --- a/C4/XISBN.pm +++ b/C4/XISBN.pm @@ -58,7 +58,10 @@ sub _get_biblio_from_xisbn { return unless ( !$errors && scalar @$results ); my $record = C4::Search::new_record_from_zebra( 'biblioserver', $results->[0] ); - my $biblionumber = C4::Biblio::TransformMarcToKohaOneField( 'biblio.biblionumber', $record ); + my $biblionumber = C4::Biblio::TransformMarcToKoha({ + kohafields => ['biblio.biblionumber'], + record => $record + })->{biblionumber}; return unless $biblionumber; my $biblio = Koha::Biblios->find( $biblionumber ); diff --git a/Koha/MetaSearcher.pm b/Koha/MetaSearcher.pm index 5a385e82e4..16a343f48d 100644 --- a/Koha/MetaSearcher.pm +++ b/Koha/MetaSearcher.pm @@ -58,26 +58,13 @@ sub new { sub handle_hit { my ( $self, $index, $server, $marcrecord ) = @_; - my $record = Koha::MetadataRecord->new( { schema => 'marc', record => $marcrecord } ); - - my %fetch = ( - title => 'biblio.title', - subtitle => 'biblio.subtitle', - seriestitle => 'biblio.seriestitle', - author => 'biblio.author', - isbn =>'biblioitems.isbn', - issn =>'biblioitems.issn', - lccn =>'biblioitems.lccn', #LC control number (not call number) - edition =>'biblioitems.editionstatement', - date => 'biblio.copyrightdate', #MARC21 - date2 => 'biblioitems.publicationyear', #UNIMARC - ); - - my $metadata = {}; - while ( my ( $key, $kohafield ) = each %fetch ) { - $metadata->{$key} = $record->getKohaField($kohafield); - } - $metadata->{date} //= $metadata->{date2}; + my @kohafields = ('biblio.title','biblio.subtitle','biblio.seriestitle','biblio.author', + 'biblioitems.isbn','biblioitems.issn','biblioitems.lccn','biblioitems.editionstatement', + 'biblio.copyrightdate','biblioitems.publicationyear'); + my $metadata = C4::Biblio::TransformMarcToKoha({ kohafields => \@kohafields, record => $marcrecord}); + $metadata->{edition} = delete $metadata->{editionstatement}; + $metadata->{date} = delete $metadata->{copyrightdate}; + $metadata->{date} //= delete $metadata->{publicationyear}; push @{ $self->{results} }, { server => $server, diff --git a/Koha/MetadataRecord.pm b/Koha/MetadataRecord.pm index 3426ea5d2e..840f63b386 100644 --- a/Koha/MetadataRecord.pm +++ b/Koha/MetadataRecord.pm @@ -118,7 +118,7 @@ sub createMergeHash { sub getKohaField { my ($self, $kohafield) = @_; if ($self->schema =~ m/marc/) { - return C4::Biblio::TransformMarcToKohaOneField($kohafield, $self->record); + return C4::Biblio::TransformMarcToKoha({ kohafields => [$kohafield], record => $self->record}); } } diff --git a/t/db_dependent/Biblio/TransformMarcToKoha.t b/t/db_dependent/Biblio/TransformMarcToKoha.t index 42f0a619b6..ab16f52d30 100755 --- a/t/db_dependent/Biblio/TransformMarcToKoha.t +++ b/t/db_dependent/Biblio/TransformMarcToKoha.t @@ -1,6 +1,6 @@ #!/usr/bin/perl -# Tests for C4::Biblio::TransformMarcToKoha, TransformMarcToKohaOneField +# Tests for C4::Biblio::TransformMarcToKoha # Copyright 2017 Rijksmuseum # @@ -28,7 +28,7 @@ use t::lib::TestBuilder; use Koha::Database; use Koha::Caches; use Koha::MarcSubfieldStructures; -use C4::Biblio qw( TransformMarcToKoha TransformMarcToKohaOneField ); +use C4::Biblio qw( TransformMarcToKoha ); my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -57,10 +57,10 @@ subtest 'Test a few mappings' => sub { is( $result->{field2}, 'b1 | b2', 'Check field2 results' ); is( $result->{field3}, 'note1 | note2', 'Check field3 results' ); - is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ), - $result->{field1}, 'TransformMarcToKohaOneField returns biblio.field1'); - is( C4::Biblio::TransformMarcToKohaOneField( 'field4', $marc ), - undef, 'TransformMarcToKohaOneField returns undef' ); + is_deeply( C4::Biblio::TransformMarcToKoha({ record => $marc, kohafields => ['biblio.field1'] }), + {field1 => 'a1 | a2'}, 'TransformMarcToKoha returns biblio.field1 if kohafields specified'); + is_deeply( C4::Biblio::TransformMarcToKoha({ record => $marc, kohafields => ['field4'] }), + {} , 'TransformMarcToKoha returns empty hashref on unknown kohafields' ); }; @@ -83,8 +83,8 @@ subtest 'Multiple mappings for one kohafield' => sub { $result = C4::Biblio::TransformMarcToKoha({ record => $marc }); is_deeply( $result, { field1 => '3a | 51' }, 'Got 300a and 510a' ); - is( C4::Biblio::TransformMarcToKohaOneField( 'biblio.field1', $marc ), - '3a | 51', 'TransformMarcToKohaOneField returns biblio.field1' ); + is_deeply( C4::Biblio::TransformMarcToKoha({ kohafields => ['biblio.field1'], record => $marc }), + { 'field1' => '3a | 51'}, 'TransformMarcToKoha returns biblio.field1 when kohafields specified' ); }; subtest 'Testing _adjust_pubyear' => sub { diff --git a/t/db_dependent/Breeding.t b/t/db_dependent/Breeding.t index 81cd2e04f1..875e7278f6 100755 --- a/t/db_dependent/Breeding.t +++ b/t/db_dependent/Breeding.t @@ -56,10 +56,10 @@ subtest '_do_xslt_proc' => sub { plan tests => 6; test_do_xslt(); }; -#Group 4: testing _add_rowdata (part of Z3950Search) -subtest '_add_rowdata' => sub { - plan tests => 5; - test_add_rowdata(); +#Group 4: testing _add_custom_field_rowdata (part of Z3950Search) +subtest '_add_custom_field_rowdata' => sub { + plan tests => 3; + test_add_custom_field_rowdata(); }; subtest ImportBreedingAuth => sub { @@ -261,44 +261,29 @@ sub test_do_xslt { 'At least the title is the same :)' ); } -sub test_add_rowdata { - t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch',''); - +sub test_add_custom_field_rowdata { my $row = { biblionumber => 0, server => "testServer", - breedingid => 0 + breedingid => 0, + title => "Just a title" }; my $biblio = MARC::Record->new(); $biblio->append_fields( - MARC::Field->new('245', ' ', ' ', a => 'Just a title'), #title + MARC::Field->new('245', ' ', ' ', a => 'Just a title'), + MARC::Field->new('035', ' ', ' ', a => 'First 035'), + MARC::Field->new('035', ' ', ' ', a => 'Second 035') ); - my $returned_row = C4::Breeding::_add_rowdata($row, $biblio); - - is($returned_row->{title}, "Just a title", "_add_rowdata returns the title of a biblio"); - is($returned_row->{addnumberfields}[0], undef, "_add_rowdata returns undef if it has no additionnal field"); + t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch',"245\$a, 035\$a"); - t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch',"245\$a, 035\$a"); - - $row = { - biblionumber => 0, - server => "testServer", - breedingid => 0 - }; - $biblio = MARC::Record->new(); - $biblio->append_fields( - MARC::Field->new('245', ' ', ' ', a => 'Just a title'), #title - MARC::Field->new('035', ' ', ' ', a => 'First 035'), - MARC::Field->new('035', ' ', ' ', a => 'Second 035') - ); - $returned_row = C4::Breeding::_add_rowdata($row, $biblio); + my $returned_row = C4::Breeding::_add_custom_field_rowdata($row, $biblio); is($returned_row->{title}, "Just a title", "_add_rowdata returns the title of a biblio"); is($returned_row->{addnumberfields}[0], "245\$a", "_add_rowdata returns the field number chosen in the AdditionalFieldsInZ3950ResultSearch preference"); - # Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_row_data + # Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_field_row_data is_deeply(\$returned_row->{"035\$a"}, \["First 035 ", "Second 035 "],"_add_rowdata supports repeatable tags"); } -- 2.39.5