From b9d65cd6fb01d1185127b03cb06bce4f9b8dbc3a 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, or use title and author to search for a host record, or return an undef record and a string of host information when get_marc_host is called in list context. --- Koha/Biblio.pm | 40 ++++++++++++++++++++---- t/db_dependent/Koha/Biblio/host_record.t | 21 +++++++++++-- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Koha/Biblio.pm b/Koha/Biblio.pm index 18b12e27259..bd2a4f058fc 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,40 @@ 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 ( $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; + } elsif ( $unlinkedf->subfield('a') and $unlinkedf->subfield('t') ) { + # use title and author to search + my ( $error, $results, $total_hits ) = $engine->simple_search_compat( 'Author='.$unlinkedf->subfield('a').' AND Title='.$unlinkedf->subfield('t'), 0, 1 ); + if ( !$error and $total_hits == 1 ) { + $bibno = $engine->extract_biblionumber( $results->[0] ); + } + } + if( $bibno ) { + my $host = Koha::Biblios->find($bibno) or return; + return wantarray ? ( $host, $unlinkedf->subfield('g') ) : $host; + } else { + # 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 d348f963c35..e5eff287ea2 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,26 @@ 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 + + # 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.20.1