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

(-)a/C4/XSLT.pm (-1 / +5 lines)
Lines 265-270 Is only used in this module currently. Link Here
265
sub buildKohaItemsNamespace {
265
sub buildKohaItemsNamespace {
266
    my ($biblionumber, $hidden_items, $items_rs) = @_;
266
    my ($biblionumber, $hidden_items, $items_rs) = @_;
267
267
268
    my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
268
    $hidden_items ||= [];
269
    $hidden_items ||= [];
269
270
270
    my $query = {};
271
    my $query = {};
Lines 272-278 sub buildKohaItemsNamespace { Link Here
272
      if $hidden_items;
273
      if $hidden_items;
273
274
274
    unless ( $items_rs && ref($items_rs) eq 'Koha::Items' ) {
275
    unless ( $items_rs && ref($items_rs) eq 'Koha::Items' ) {
275
        $query->{'me.biblionumber'} = $biblionumber;
276
        $query->{ '-or' } = [
277
            'me.biblionumber' => $biblionumber,
278
            'me.itemnumber' => { -in => \@host_itemnumbers }
279
        ];
276
        $items_rs = Koha::Items->new;
280
        $items_rs = Koha::Items->new;
277
    }
281
    }
278
282
(-)a/Koha/Biblio/Metadata.pm (-3 / +8 lines)
Lines 21-27 use MARC::File::XML; Link Here
21
use Scalar::Util qw( blessed );
21
use Scalar::Util qw( blessed );
22
22
23
use C4::Biblio qw( GetMarcFromKohaField );
23
use C4::Biblio qw( GetMarcFromKohaField );
24
use C4::Items qw( GetMarcItem );
24
use C4::Items qw( GetMarcItem get_hostitemnumbers_of );
25
use Koha::Database;
25
use Koha::Database;
26
use Koha::Exceptions::Metadata;
26
use Koha::Exceptions::Metadata;
27
27
Lines 157-165 sub _embed_items { Link Here
157
            $record->delete_field($field);
157
            $record->delete_field($field);
158
        }
158
        }
159
159
160
        my $biblio = Koha::Biblios->find($biblionumber);
160
        push @$itemnumbers, C4::Items::get_hostitemnumbers_of( $biblionumber ) if $params->{embed_host_items};
161
161
162
        my $items = $biblio->items;
162
        my $items = Koha::Items->search([
163
           'me.biblionumber' => $biblionumber,
164
           'me.itemnumber' => {
165
              -in => $itemnumbers
166
              }
167
        ]);
163
        if ( @$itemnumbers ) {
168
        if ( @$itemnumbers ) {
164
            $items = $items->search({ itemnumber => { -in => $itemnumbers } });
169
            $items = $items->search({ itemnumber => { -in => $itemnumbers } });
165
        }
170
        }
(-)a/Koha/SearchEngine/Elasticsearch/Indexer.pm (-1 / +2 lines)
Lines 341-353 sub _get_record { Link Here
341
341
342
    if ( $self->index eq $Koha::SearchEngine::BIBLIOS_INDEX ) {
342
    if ( $self->index eq $Koha::SearchEngine::BIBLIOS_INDEX ) {
343
        my $biblio = Koha::Biblios->find($record_id);
343
        my $biblio = Koha::Biblios->find($record_id);
344
        $record = $biblio->metadata->record( { embed_items => 1 } )
344
        $record = $biblio->metadata->record( { embed_items => 1, embed_host_items => 1 } )
345
          if $biblio;
345
          if $biblio;
346
    } else {
346
    } else {
347
        $record = C4::AuthoritiesMarc::GetAuthority($record_id);
347
        $record = C4::AuthoritiesMarc::GetAuthority($record_id);
348
    }
348
    }
349
349
350
    return $record;
350
    return $record;
351
351
}
352
}
352
353
353
=head2 delete_index($biblionums)
354
=head2 delete_index($biblionums)
(-)a/misc/migration_tools/rebuild_zebra.pl (-1 / +13 lines)
Lines 511-517 sub export_marc_records_from_sth { Link Here
511
            if ($record_type eq 'biblio'){
511
            if ($record_type eq 'biblio'){
512
                my $biblio = Koha::Biblios->find($record_number);
512
                my $biblio = Koha::Biblios->find($record_number);
513
                next unless $biblio;
513
                next unless $biblio;
514
                my $items = $biblio->items;
514
                my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $record_number );
515
                my $items = Koha::Items->search({
516
                    -or => [
517
                        biblionumber => $record_number,
518
                        itemnumber => {
519
                            -in => \@host_itemnumbers
520
                        }
521
                    ]
522
                });
515
                if ($items->count){
523
                if ($items->count){
516
                    my $record = MARC::Record->new;
524
                    my $record = MARC::Record->new;
517
                    $record->encoding('UTF-8');
525
                    $record->encoding('UTF-8');
Lines 680-689 sub get_raw_marc_record { Link Here
680
688
681
    my $marc;
689
    my $marc;
682
    if ($record_type eq 'biblio') {
690
    if ($record_type eq 'biblio') {
691
<<<<<<< HEAD
683
        eval {
692
        eval {
684
            my $biblio = Koha::Biblios->find($record_number);
693
            my $biblio = Koha::Biblios->find($record_number);
685
            $marc = $biblio->metadata->record({ embed_items => 1 });
694
            $marc = $biblio->metadata->record({ embed_items => 1 });
686
        };
695
        };
696
=======
697
        eval { $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $record_number, embed_items => 1, embed_host_items => 1 }); };
698
>>>>>>> Bug 27138: Index host items in child records when sending to search engine
687
        if ($@ || !$marc) {
699
        if ($@ || !$marc) {
688
            # here we do warn since catching an exception
700
            # here we do warn since catching an exception
689
            # means that the bib was found but failed
701
            # means that the bib was found but failed
(-)a/t/db_dependent/Items.t (-1 / +155 lines)
Lines 585-590 subtest 'Koha::Item(s) tests' => sub { Link Here
585
    $schema->storage->txn_rollback;
585
    $schema->storage->txn_rollback;
586
};
586
};
587
587
588
subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
589
    plan tests => 11;
590
591
    $schema->storage->txn_begin();
592
593
    my $builder = t::lib::TestBuilder->new;
594
    my $library1 = $builder->build({
595
        source => 'Branch',
596
    });
597
    my $library2 = $builder->build({
598
        source => 'Branch',
599
    });
600
    my $itemtype = $builder->build({
601
        source => 'Itemtype',
602
    });
603
604
    my $biblio = $builder->build_sample_biblio();
605
    my $item_infos = [
606
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
607
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
608
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
609
        { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
610
        { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
611
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
612
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
613
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
614
    ];
615
    my $number_of_items = scalar @$item_infos;
616
    my $number_of_items_with_homebranch_is_CPL =
617
      grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
618
619
    my @itemnumbers;
620
    for my $item_info (@$item_infos) {
621
        my $itemnumber = $builder->build_sample_item(
622
            {
623
                biblionumber  => $biblio->biblionumber,
624
                homebranch    => $item_info->{homebranch},
625
                holdingbranch => $item_info->{holdingbranch},
626
                itype         => $itemtype->{itemtype}
627
            }
628
        )->itemnumber;
629
630
        push @itemnumbers, $itemnumber;
631
    }
632
633
    my $host_item = $builder->build_sample_item({ homebranch => $library2->{branchcode} });
634
    my $child_field = PrepHostMarcField( $host_item->biblionumber, $host_item->itemnumber, 'MARC21');
635
    my $child_record = $biblio->metadata->record;
636
    $child_record->append_fields($child_field);
637
    ModBiblio( $child_record, $biblio->biblionumber, '' );
638
639
    # Emptied the OpacHiddenItems pref
640
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
641
642
    my ($itemfield) =
643
      C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
644
    my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
645
    warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
646
    { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
647
      'Should carp is no record passed.';
648
649
    C4::Biblio::EmbedItemsInMarcBiblio({
650
        marc_record  => $record,
651
        biblionumber => $biblio->biblionumber });
652
    my @items = $record->field($itemfield);
653
    is( scalar @items, $number_of_items, 'Should return all items' );
654
655
    my $marc_with_items = C4::Biblio::GetMarcBiblio({
656
        biblionumber => $biblio->biblionumber,
657
        embed_items  => 1 });
658
    is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
659
660
    C4::Biblio::EmbedItemsInMarcBiblio({
661
        marc_record  => $record,
662
        biblionumber => $biblio->biblionumber,
663
        embed_host_items => 1
664
    });
665
    @items = $record->field($itemfield);
666
    is( scalar @items, $number_of_items + 1, 'Should return all items plus host items' );
667
668
    $marc_with_items = C4::Biblio::GetMarcBiblio({
669
        biblionumber => $biblio->biblionumber,
670
        embed_items  => 1,
671
        embed_host_items => 1
672
    });
673
    is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
674
675
    C4::Biblio::EmbedItemsInMarcBiblio({
676
        marc_record  => $record,
677
        biblionumber => $biblio->biblionumber,
678
        item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
679
    @items = $record->field($itemfield);
680
    is( scalar @items, 2, 'Should return all items present in the list' );
681
682
    C4::Biblio::EmbedItemsInMarcBiblio({
683
        marc_record  => $record,
684
        biblionumber => $biblio->biblionumber,
685
        opac         => 1 });
686
    @items = $record->field($itemfield);
687
    is( scalar @items, $number_of_items, 'Should return all items for opac' );
688
689
    my $opachiddenitems = "
690
        homebranch: ['$library1->{branchcode}']";
691
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
692
693
    C4::Biblio::EmbedItemsInMarcBiblio({
694
        marc_record  => $record,
695
        biblionumber => $biblio->biblionumber });
696
    @items = $record->field($itemfield);
697
    is( scalar @items,
698
        $number_of_items,
699
        'Even with OpacHiddenItems set, all items should have been embedded' );
700
701
    C4::Biblio::EmbedItemsInMarcBiblio({
702
        marc_record  => $record,
703
        biblionumber => $biblio->biblionumber,
704
        opac         => 1 });
705
    @items = $record->field($itemfield);
706
    is(
707
        scalar @items,
708
        $number_of_items - $number_of_items_with_homebranch_is_CPL,
709
'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
710
    );
711
712
    C4::Biblio::EmbedItemsInMarcBiblio({
713
        marc_record  => $record,
714
        biblionumber => $biblio->biblionumber,
715
        opac         => 1,
716
        embed_host_items => 1
717
    });
718
    @items = $record->field($itemfield);
719
    is(
720
        scalar @items,
721
        $number_of_items - $number_of_items_with_homebranch_is_CPL + 1,
722
'For OPAC, the pref OpacHiddenItems should have been take into account and host items embedded. Only items with homebranch ne CPL should have been embedded'
723
    );
724
725
726
    $opachiddenitems = "
727
        homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
728
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
729
    C4::Biblio::EmbedItemsInMarcBiblio({
730
        marc_record  => $record,
731
        biblionumber => $biblio->biblionumber,
732
        opac         => 1 });
733
    @items = $record->field($itemfield);
734
    is(
735
        scalar @items,
736
        0,
737
'For OPAC, If all items are hidden, no item should have been embedded'
738
    );
739
740
    $schema->storage->txn_rollback;
741
};
742
588
subtest 'get_hostitemnumbers_of' => sub {
743
subtest 'get_hostitemnumbers_of' => sub {
589
    plan tests => 3;
744
    plan tests => 3;
590
745
591
- 

Return to bug 27138