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

(-)a/C4/XSLT.pm (-1 / +5 lines)
Lines 281-286 Is only used in this module currently. Link Here
281
sub buildKohaItemsNamespace {
281
sub buildKohaItemsNamespace {
282
    my ( $biblionumber, $hidden_items, $items_rs ) = @_;
282
    my ( $biblionumber, $hidden_items, $items_rs ) = @_;
283
283
284
    my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
284
    $hidden_items ||= [];
285
    $hidden_items ||= [];
285
286
286
    my $query = {};
287
    my $query = {};
Lines 288-294 sub buildKohaItemsNamespace { Link Here
288
        if $hidden_items;
289
        if $hidden_items;
289
290
290
    unless ( $items_rs && ref($items_rs) eq 'Koha::Items' ) {
291
    unless ( $items_rs && ref($items_rs) eq 'Koha::Items' ) {
291
        $query->{'me.biblionumber'} = $biblionumber;
292
        $query->{ '-or' } = [
293
            'me.biblionumber' => $biblionumber,
294
            'me.itemnumber' => { -in => \@host_itemnumbers }
295
        ];
292
        $items_rs = Koha::Items->new;
296
        $items_rs = Koha::Items->new;
293
    }
297
    }
294
298
(-)a/Koha/Biblio/Metadata.pm (-2 / +21 lines)
Lines 20-28 use Modern::Perl; Link Here
20
use MARC::File::XML;
20
use MARC::File::XML;
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::Charset qw( StripNonXmlChars );
24
use C4::Charset qw( StripNonXmlChars );
25
use C4::Items   qw( GetMarcItem );
25
use C4::Items qw( GetMarcItem get_hostitemnumbers_of );
26
26
27
use Koha::Database;
27
use Koha::Database;
28
use Koha::Exceptions::Metadata;
28
use Koha::Exceptions::Metadata;
Lines 227-238 sub _embed_items { Link Here
227
227
228
    if ( $format eq 'marcxml' ) {
228
    if ( $format eq 'marcxml' ) {
229
229
230
<<<<<<< HEAD
230
        my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
231
        my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField("items.itemnumber");
231
        my $biblio = Koha::Biblios->find($biblionumber);
232
        my $biblio = Koha::Biblios->find($biblionumber);
232
233
233
        my $items = $biblio->items;
234
        my $items = $biblio->items;
234
        if (@$itemnumbers) {
235
        if (@$itemnumbers) {
235
            $items = $items->search( { itemnumber => { -in => $itemnumbers } } );
236
            $items = $items->search( { itemnumber => { -in => $itemnumbers } } );
237
=======
238
        # First remove the existing items from the MARC record
239
        my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
240
        foreach my $field ( $record->field($itemtag) ) {
241
            $record->delete_field($field);
242
        }
243
244
        push @$itemnumbers, C4::Items::get_hostitemnumbers_of( $biblionumber ) if $params->{embed_host_items};
245
246
        my $items = Koha::Items->search([
247
           'me.biblionumber' => $biblionumber,
248
           'me.itemnumber' => {
249
              -in => $itemnumbers
250
              }
251
        ]);
252
        if ( @$itemnumbers ) {
253
            $items = $items->search({ itemnumber => { -in => $itemnumbers } });
254
>>>>>>> Bug 27138: Index host items in child records when sending to search engine
236
        }
255
        }
237
        if ($opac) {
256
        if ($opac) {
238
            $items = $items->filter_by_visible_in_opac( { patron => $patron } );
257
            $items = $items->filter_by_visible_in_opac( { patron => $patron } );
(-)a/Koha/SearchEngine/Elasticsearch/Indexer.pm (-2 / +3 lines)
Lines 348-360 sub _get_record { Link Here
348
348
349
    if ( $self->index eq $Koha::SearchEngine::BIBLIOS_INDEX ) {
349
    if ( $self->index eq $Koha::SearchEngine::BIBLIOS_INDEX ) {
350
        my $biblio = Koha::Biblios->find($record_id);
350
        my $biblio = Koha::Biblios->find($record_id);
351
        $record = $biblio->metadata_record( { embed_items => 1 } )
351
        $record = $biblio->metadata->record( { embed_items => 1, embed_host_items => 1 } )
352
            if $biblio;
352
          if $biblio;
353
    } else {
353
    } else {
354
        $record = C4::AuthoritiesMarc::GetAuthority($record_id);
354
        $record = C4::AuthoritiesMarc::GetAuthority($record_id);
355
    }
355
    }
356
356
357
    return $record;
357
    return $record;
358
358
}
359
}
359
360
360
=head2 delete_index($biblionums)
361
=head2 delete_index($biblionums)
(-)a/misc/migration_tools/rebuild_zebra.pl (-2 / +22 lines)
Lines 583-590 sub export_marc_records_from_sth { Link Here
583
            if ( $record_type eq 'biblio' ) {
583
            if ( $record_type eq 'biblio' ) {
584
                my $biblio = Koha::Biblios->find($record_number);
584
                my $biblio = Koha::Biblios->find($record_number);
585
                next unless $biblio;
585
                next unless $biblio;
586
                my $items = $biblio->items;
586
                my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $record_number );
587
                if ( $items->count ) {
587
                my $items = Koha::Items->search({
588
                    -or => [
589
                        biblionumber => $record_number,
590
                        itemnumber => {
591
                            -in => \@host_itemnumbers
592
                        }
593
                    ]
594
                });
595
                if ($items->count){
588
                    my $record = MARC::Record->new;
596
                    my $record = MARC::Record->new;
589
                    $record->encoding('UTF-8');
597
                    $record->encoding('UTF-8');
590
                    my @itemsrecord;
598
                    my @itemsrecord;
Lines 765-777 sub get_raw_marc_record { Link Here
765
    my ( $record_type, $record_number ) = @_;
773
    my ( $record_type, $record_number ) = @_;
766
774
767
    my $marc;
775
    my $marc;
776
<<<<<<< HEAD
768
    if ( $record_type eq 'biblio' ) {
777
    if ( $record_type eq 'biblio' ) {
778
=======
779
    if ($record_type eq 'biblio') {
780
<<<<<<< HEAD
781
>>>>>>> Bug 27138: Index host items in child records when sending to search engine
769
        eval {
782
        eval {
770
            my $biblio = Koha::Biblios->find($record_number);
783
            my $biblio = Koha::Biblios->find($record_number);
771
            $marc = $biblio->metadata_record( { embed_items => 1 } );
784
            $marc = $biblio->metadata_record( { embed_items => 1 } );
772
        };
785
        };
786
<<<<<<< HEAD
773
        if ( $@ || !$marc ) {
787
        if ( $@ || !$marc ) {
774
788
789
=======
790
=======
791
        eval { $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $record_number, embed_items => 1, embed_host_items => 1 }); };
792
>>>>>>> Bug 27138: Index host items in child records when sending to search engine
793
        if ($@ || !$marc) {
794
>>>>>>> Bug 27138: Index host items in child records when sending to search engine
775
            # here we do warn since catching an exception
795
            # here we do warn since catching an exception
776
            # means that the bib was found but failed
796
            # means that the bib was found but failed
777
            # to be parsed
797
            # to be parsed
(-)a/t/db_dependent/Items.t (-1 / +155 lines)
Lines 724-729 subtest 'Koha::Item(s) tests' => sub { Link Here
724
    $schema->storage->txn_rollback;
724
    $schema->storage->txn_rollback;
725
};
725
};
726
726
727
subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
728
    plan tests => 11;
729
730
    $schema->storage->txn_begin();
731
732
    my $builder = t::lib::TestBuilder->new;
733
    my $library1 = $builder->build({
734
        source => 'Branch',
735
    });
736
    my $library2 = $builder->build({
737
        source => 'Branch',
738
    });
739
    my $itemtype = $builder->build({
740
        source => 'Itemtype',
741
    });
742
743
    my $biblio = $builder->build_sample_biblio();
744
    my $item_infos = [
745
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
746
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
747
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
748
        { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
749
        { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
750
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
751
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
752
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
753
    ];
754
    my $number_of_items = scalar @$item_infos;
755
    my $number_of_items_with_homebranch_is_CPL =
756
      grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
757
758
    my @itemnumbers;
759
    for my $item_info (@$item_infos) {
760
        my $itemnumber = $builder->build_sample_item(
761
            {
762
                biblionumber  => $biblio->biblionumber,
763
                homebranch    => $item_info->{homebranch},
764
                holdingbranch => $item_info->{holdingbranch},
765
                itype         => $itemtype->{itemtype}
766
            }
767
        )->itemnumber;
768
769
        push @itemnumbers, $itemnumber;
770
    }
771
772
    my $host_item = $builder->build_sample_item({ homebranch => $library2->{branchcode} });
773
    my $child_field = PrepHostMarcField( $host_item->biblionumber, $host_item->itemnumber, 'MARC21');
774
    my $child_record = $biblio->metadata->record;
775
    $child_record->append_fields($child_field);
776
    ModBiblio( $child_record, $biblio->biblionumber, '' );
777
778
    # Emptied the OpacHiddenItems pref
779
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
780
781
    my ($itemfield) =
782
      C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
783
    my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
784
    warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
785
    { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
786
      'Should carp is no record passed.';
787
788
    C4::Biblio::EmbedItemsInMarcBiblio({
789
        marc_record  => $record,
790
        biblionumber => $biblio->biblionumber });
791
    my @items = $record->field($itemfield);
792
    is( scalar @items, $number_of_items, 'Should return all items' );
793
794
    my $marc_with_items = C4::Biblio::GetMarcBiblio({
795
        biblionumber => $biblio->biblionumber,
796
        embed_items  => 1 });
797
    is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
798
799
    C4::Biblio::EmbedItemsInMarcBiblio({
800
        marc_record  => $record,
801
        biblionumber => $biblio->biblionumber,
802
        embed_host_items => 1
803
    });
804
    @items = $record->field($itemfield);
805
    is( scalar @items, $number_of_items + 1, 'Should return all items plus host items' );
806
807
    $marc_with_items = C4::Biblio::GetMarcBiblio({
808
        biblionumber => $biblio->biblionumber,
809
        embed_items  => 1,
810
        embed_host_items => 1
811
    });
812
    is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
813
814
    C4::Biblio::EmbedItemsInMarcBiblio({
815
        marc_record  => $record,
816
        biblionumber => $biblio->biblionumber,
817
        item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
818
    @items = $record->field($itemfield);
819
    is( scalar @items, 2, 'Should return all items present in the list' );
820
821
    C4::Biblio::EmbedItemsInMarcBiblio({
822
        marc_record  => $record,
823
        biblionumber => $biblio->biblionumber,
824
        opac         => 1 });
825
    @items = $record->field($itemfield);
826
    is( scalar @items, $number_of_items, 'Should return all items for opac' );
827
828
    my $opachiddenitems = "
829
        homebranch: ['$library1->{branchcode}']";
830
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
831
832
    C4::Biblio::EmbedItemsInMarcBiblio({
833
        marc_record  => $record,
834
        biblionumber => $biblio->biblionumber });
835
    @items = $record->field($itemfield);
836
    is( scalar @items,
837
        $number_of_items,
838
        'Even with OpacHiddenItems set, all items should have been embedded' );
839
840
    C4::Biblio::EmbedItemsInMarcBiblio({
841
        marc_record  => $record,
842
        biblionumber => $biblio->biblionumber,
843
        opac         => 1 });
844
    @items = $record->field($itemfield);
845
    is(
846
        scalar @items,
847
        $number_of_items - $number_of_items_with_homebranch_is_CPL,
848
'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
849
    );
850
851
    C4::Biblio::EmbedItemsInMarcBiblio({
852
        marc_record  => $record,
853
        biblionumber => $biblio->biblionumber,
854
        opac         => 1,
855
        embed_host_items => 1
856
    });
857
    @items = $record->field($itemfield);
858
    is(
859
        scalar @items,
860
        $number_of_items - $number_of_items_with_homebranch_is_CPL + 1,
861
'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'
862
    );
863
864
865
    $opachiddenitems = "
866
        homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
867
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
868
    C4::Biblio::EmbedItemsInMarcBiblio({
869
        marc_record  => $record,
870
        biblionumber => $biblio->biblionumber,
871
        opac         => 1 });
872
    @items = $record->field($itemfield);
873
    is(
874
        scalar @items,
875
        0,
876
'For OPAC, If all items are hidden, no item should have been embedded'
877
    );
878
879
    $schema->storage->txn_rollback;
880
};
881
727
subtest 'get_hostitemnumbers_of' => sub {
882
subtest 'get_hostitemnumbers_of' => sub {
728
    plan tests => 3;
883
    plan tests => 3;
729
884
730
- 

Return to bug 27138