From 0a3959d3eef881e05ad4b2444ffc4c88bbe8be1e Mon Sep 17 00:00:00 2001 From: Aleisha Amohia Date: Mon, 17 Oct 2022 22:38:44 +0000 Subject: [PATCH] Bug 16522: (follow-up) If no 773$w, try to return host record Use host biblionumber or host itemnumber to return a host record if EasyAnalyticalRecords is enabled, or return an undef record and a string of host information when get_marc_host is called in list context. Confirm tests pass: - t/db_dependent/Koha/Biblio/host_record.t Signed-off-by: Heather Hernandez Signed-off-by: Katrin Fischer Signed-off-by: Tomas Cohen Arazi (cherry picked from commit 5e226c1a63f916e0224d9fcbaa6e5beaf8f5b44c) Signed-off-by: Jacob O'Mara --- Koha/Biblio.pm | 35 ++++++++++++++++++++---- t/db_dependent/Koha/Biblio/host_record.t | 22 +++++++++++++-- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 4b0c14aec9..06d0e86c7f 100644 --- a/Koha/Biblio.pm +++ b/Koha/Biblio.pm @@ -1205,7 +1205,7 @@ sub to_api_mapping { $host = $biblio->get_marc_host; # OR: - ( $host, $relatedparts ) = $biblio->get_marc_host; + ( $host, $relatedparts, $hostinfo ) = $biblio->get_marc_host; Returns host biblio record from MARC21 773 (undef if no 773 present). It looks at the first 773 field with MARCorgCode or only a control @@ -1214,6 +1214,12 @@ sub to_api_mapping { If there are, the sub returns undef. Called in list context, it also returns 773$g (related parts). + If there is no $w, we use $0 (host biblionumber) or $9 (host itemnumber) + to search for the host record. If there is also no $0 and no $9, we search + using author and title. Failing all of that, we return an undef host and + form a concatenation of strings with 773$agt for host information, + returned when called in list context. + =cut sub get_marc_host { @@ -1237,18 +1243,35 @@ sub get_marc_host { last; } } + + my $engine = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); + my $bibno; if ( !$hostfld and $record->subfield('773','t') ) { - # not linked using $w so just return plaintext + # not linked using $w my $unlinkedf = $record->field('773'); - my $host = join( ", ", $unlinkedf->subfield('a'), $unlinkedf->subfield('t'), $unlinkedf->subfield('g') ); - return wantarray ? ( $host, $unlinkedf->subfield('g') ) : $host; + my $host; + if ( C4::Context->preference("EasyAnalyticalRecords") ) { + if ( $unlinkedf->subfield('0') ) { + # use 773$0 host biblionumber + $bibno = $unlinkedf->subfield('0'); + } elsif ( $unlinkedf->subfield('9') ) { + # use 773$9 host itemnumber + my $linkeditemnumber = $unlinkedf->subfield('9'); + $bibno = Koha::Items->find( $linkeditemnumber )->biblionumber; + } + } + if ( $bibno ) { + my $host = Koha::Biblios->find($bibno) or return; + return wantarray ? ( $host, $unlinkedf->subfield('g') ) : $host; + } + # just return plaintext and no host record + my $hostinfo = join( ", ", $unlinkedf->subfield('a'), $unlinkedf->subfield('t'), $unlinkedf->subfield('g') ); + return wantarray ? ( undef, $unlinkedf->subfield('g'), $hostinfo ) : undef; } return if !$hostfld; my $rcn = $hostfld->subfield('w'); # Look for control number with/without orgcode - my $engine = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); - my $bibno; for my $try (1..2) { my ( $error, $results, $total_hits ) = $engine->simple_search_compat( 'Control-number='.$rcn, 0,1 ); if( !$error and $total_hits == 1 ) { diff --git a/t/db_dependent/Koha/Biblio/host_record.t b/t/db_dependent/Koha/Biblio/host_record.t index d348f963c3..4094a102cb 100755 --- a/t/db_dependent/Koha/Biblio/host_record.t +++ b/t/db_dependent/Koha/Biblio/host_record.t @@ -36,13 +36,14 @@ $schema->storage->txn_begin; our $builder = t::lib::TestBuilder->new; subtest 'get_marc_host' => sub { - plan tests => 12; + plan tests => 15; t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' ); t::lib::Mocks::mock_preference( 'MARCOrgCode', 'xyz' ); my $bib1 = $builder->build_object({ class => 'Koha::Biblios' }); my $bib2 = $builder->build_object({ class => 'Koha::Biblios' }); + my $item1 = $builder->build_object({ class => 'Koha::Items', value => { biblionumber => $bib2->biblionumber } }); my $marc = MARC::Record->new; my $results = []; @@ -72,12 +73,27 @@ subtest 'get_marc_host' => sub { $marc->field('773')->update( w => '(xyz) bad data' ); # causes no results $host = $bib1->get_marc_host; is( $bib1->get_marc_host, undef, 'No results for bad 773' ); - # Test plaintext when no $w + + t::lib::Mocks::mock_preference( 'EasyAnalyticalRecords', 1 ); + # no $w $marc->field('773')->update( t => 'title' ); $marc->field('773')->delete_subfield( code => 'w' ); + $marc->field('773')->update( '0' => $bib2->biblionumber ); + $host = $bib1->get_marc_host; + is( $host->biblionumber, $bib2->biblionumber, 'Found host biblio using 773$0 biblionumber' ); + + $marc->field('773')->delete_subfield( code => '0' ); + $marc->field('773')->update( '9' => $item1->itemnumber ); $host = $bib1->get_marc_host; - is( $host, "title, relpart", '773$atg returned when no $w' ); + is( $host->biblionumber, $bib2->biblionumber, 'Found host item using 773$9 itemnumber' ); + + $marc->field('773')->delete_subfield( code => '9' ); + my ( $relatedparts, $info ); + ( $host, $relatedparts, $info ) = $bib1->get_marc_host; + is( $host, undef, 'No Koha Biblio object returned with no $w' ); + is( $info, "title, relpart", '773$atg returned when no $w' ); $marc->field('773')->delete_subfield( code => 't' ); # restore + # Add second 773 $marc->append_fields( MARC::Field->new( '773', '', '', g => 'relpart2', w => '234' ) ); $host = $bib1->get_marc_host; -- 2.39.5