View | Details | Raw Unified | Return to bug 16522
Collapse All | Expand All

(-)a/Koha/Biblio.pm (-6 / +34 lines)
Lines 1205-1211 sub to_api_mapping { Link Here
1205
1205
1206
    $host = $biblio->get_marc_host;
1206
    $host = $biblio->get_marc_host;
1207
    # OR:
1207
    # OR:
1208
    ( $host, $relatedparts ) = $biblio->get_marc_host;
1208
    ( $host, $relatedparts, $hostinfo ) = $biblio->get_marc_host;
1209
1209
1210
    Returns host biblio record from MARC21 773 (undef if no 773 present).
1210
    Returns host biblio record from MARC21 773 (undef if no 773 present).
1211
    It looks at the first 773 field with MARCorgCode or only a control
1211
    It looks at the first 773 field with MARCorgCode or only a control
Lines 1214-1219 sub to_api_mapping { Link Here
1214
    If there are, the sub returns undef.
1214
    If there are, the sub returns undef.
1215
    Called in list context, it also returns 773$g (related parts).
1215
    Called in list context, it also returns 773$g (related parts).
1216
1216
1217
    If there is no $w, we use $0 (host biblionumber) or $9 (host itemnumber)
1218
    to search for the host record. If there is also no $0 and no $9, we search
1219
    using author and title. Failing all of that, we return an undef host and
1220
    form a concatenation of strings with 773$agt for host information,
1221
    returned when called in list context.
1222
1217
=cut
1223
=cut
1218
1224
1219
sub get_marc_host {
1225
sub get_marc_host {
Lines 1237-1254 sub get_marc_host { Link Here
1237
            last;
1243
            last;
1238
        }
1244
        }
1239
    }
1245
    }
1246
1247
    my $engine = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
1248
    my $bibno;
1240
    if ( !$hostfld and $record->subfield('773','t') ) {
1249
    if ( !$hostfld and $record->subfield('773','t') ) {
1241
        # not linked using $w so just return plaintext
1250
        # not linked using $w
1242
        my $unlinkedf = $record->field('773');
1251
        my $unlinkedf = $record->field('773');
1243
        my $host = join( ", ", $unlinkedf->subfield('a'), $unlinkedf->subfield('t'), $unlinkedf->subfield('g') );
1252
        my $host;
1244
        return wantarray ? ( $host, $unlinkedf->subfield('g') ) : $host;
1253
        if ( $unlinkedf->subfield('0') ) {
1254
            # use 773$0 host biblionumber
1255
            $bibno = $unlinkedf->subfield('0');
1256
        } elsif ( $unlinkedf->subfield('9') ) {
1257
            # use 773$9 host itemnumber
1258
            my $linkeditemnumber = $unlinkedf->subfield('9');
1259
            $bibno = Koha::Items->find( $linkeditemnumber )->biblionumber;
1260
        } elsif ( $unlinkedf->subfield('a') and $unlinkedf->subfield('t') ) {
1261
            # use title and author to search
1262
            my ( $error, $results, $total_hits ) = $engine->simple_search_compat( 'Author='.$unlinkedf->subfield('a').' AND Title='.$unlinkedf->subfield('t'), 0, 1 );
1263
            if ( !$error and $total_hits == 1 ) {
1264
                $bibno = $engine->extract_biblionumber( $results->[0] );
1265
            }
1266
        }
1267
        if( $bibno ) {
1268
            my $host = Koha::Biblios->find($bibno) or return;
1269
            return wantarray ? ( $host, $unlinkedf->subfield('g') ) : $host;
1270
        } else {
1271
            # just return plaintext and no host record
1272
            my $hostinfo = join( ", ", $unlinkedf->subfield('a'), $unlinkedf->subfield('t'), $unlinkedf->subfield('g') );
1273
            return wantarray ? ( undef, $unlinkedf->subfield('g'), $hostinfo ) : undef;
1274
        }
1245
    }
1275
    }
1246
    return if !$hostfld;
1276
    return if !$hostfld;
1247
    my $rcn = $hostfld->subfield('w');
1277
    my $rcn = $hostfld->subfield('w');
1248
1278
1249
    # Look for control number with/without orgcode
1279
    # Look for control number with/without orgcode
1250
    my $engine = Koha::SearchEngine::Search->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
1251
    my $bibno;
1252
    for my $try (1..2) {
1280
    for my $try (1..2) {
1253
        my ( $error, $results, $total_hits ) = $engine->simple_search_compat( 'Control-number='.$rcn, 0,1 );
1281
        my ( $error, $results, $total_hits ) = $engine->simple_search_compat( 'Control-number='.$rcn, 0,1 );
1254
        if( !$error and $total_hits == 1 ) {
1282
        if( !$error and $total_hits == 1 ) {
(-)a/t/db_dependent/Koha/Biblio/host_record.t (-4 / +18 lines)
Lines 36-48 $schema->storage->txn_begin; Link Here
36
our $builder = t::lib::TestBuilder->new;
36
our $builder = t::lib::TestBuilder->new;
37
37
38
subtest 'get_marc_host' => sub {
38
subtest 'get_marc_host' => sub {
39
    plan tests => 12;
39
    plan tests => 15;
40
40
41
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
41
    t::lib::Mocks::mock_preference( 'marcflavour', 'MARC21' );
42
    t::lib::Mocks::mock_preference( 'MARCOrgCode', 'xyz' );
42
    t::lib::Mocks::mock_preference( 'MARCOrgCode', 'xyz' );
43
43
44
    my $bib1 = $builder->build_object({ class => 'Koha::Biblios' });
44
    my $bib1 = $builder->build_object({ class => 'Koha::Biblios' });
45
    my $bib2 = $builder->build_object({ class => 'Koha::Biblios' });
45
    my $bib2 = $builder->build_object({ class => 'Koha::Biblios' });
46
    my $item1 = $builder->build_object({ class => 'Koha::Items', value => { biblionumber => $bib2->biblionumber } });
46
    my $marc = MARC::Record->new;
47
    my $marc = MARC::Record->new;
47
    my $results = [];
48
    my $results = [];
48
49
Lines 72-83 subtest 'get_marc_host' => sub { Link Here
72
    $marc->field('773')->update( w => '(xyz) bad data' ); # causes no results
73
    $marc->field('773')->update( w => '(xyz) bad data' ); # causes no results
73
    $host = $bib1->get_marc_host;
74
    $host = $bib1->get_marc_host;
74
    is( $bib1->get_marc_host, undef, 'No results for bad 773' );
75
    is( $bib1->get_marc_host, undef, 'No results for bad 773' );
75
    # Test plaintext when no $w
76
77
    # no $w
76
    $marc->field('773')->update( t => 'title' );
78
    $marc->field('773')->update( t => 'title' );
77
    $marc->field('773')->delete_subfield( code => 'w' );
79
    $marc->field('773')->delete_subfield( code => 'w' );
80
    $marc->field('773')->update( '0' => $bib2->biblionumber );
81
    $host = $bib1->get_marc_host;
82
    is( $host->biblionumber, $bib2->biblionumber, 'Found host biblio using 773$0 biblionumber' );
83
84
    $marc->field('773')->delete_subfield( code => '0' );
85
    $marc->field('773')->update( '9' => $item1->itemnumber );
78
    $host = $bib1->get_marc_host;
86
    $host = $bib1->get_marc_host;
79
    is( $host, "title, relpart", '773$atg returned when no $w' );
87
    is( $host->biblionumber, $bib2->biblionumber, 'Found host item using 773$9 itemnumber' );
88
89
    $marc->field('773')->delete_subfield( code => '9' );
90
    my ( $relatedparts, $info );
91
    ( $host, $relatedparts, $info ) = $bib1->get_marc_host;
92
    is( $host, undef, 'No Koha Biblio object returned with no $w' );
93
    is( $info, "title, relpart", '773$atg returned when no $w' );
80
    $marc->field('773')->delete_subfield( code => 't' ); # restore
94
    $marc->field('773')->delete_subfield( code => 't' ); # restore
95
81
    # Add second 773
96
    # Add second 773
82
    $marc->append_fields( MARC::Field->new( '773', '', '', g => 'relpart2', w => '234' ) );
97
    $marc->append_fields( MARC::Field->new( '773', '', '', g => 'relpart2', w => '234' ) );
83
    $host = $bib1->get_marc_host;
98
    $host = $bib1->get_marc_host;
84
- 

Return to bug 16522