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

(-)a/Koha/BiblioUtils/Iterator.pm (-131 lines)
Lines 1-131 Link Here
1
package Koha::BiblioUtils::Iterator;
2
3
# This contains an iterator over biblio records
4
5
# Copyright 2014 Catalyst IT
6
#
7
# This file is part of Koha.
8
#
9
# Koha is free software; you can redistribute it and/or modify it
10
# under the terms of the GNU General Public License as published by
11
# the Free Software Foundation; either version 3 of the License, or
12
# (at your option) any later version.
13
#
14
# Koha is distributed in the hope that it will be useful, but
15
# WITHOUT ANY WARRANTY; without even the implied warranty of
16
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
# GNU General Public License for more details.
18
#
19
# You should have received a copy of the GNU General Public License
20
# along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22
=head1 NAME
23
24
Koha::BiblioUtils::Iterator - iterates over biblios provided by a DBIx::Class::ResultSet
25
26
=head1 DESCRIPTION
27
28
This provides an iterator over a L<DBIx::Class::ResultSet> that contains a
29
biblionumber column.
30
Returns a MARC::Record in scalar context.
31
Returns biblionumber and marc in list context.
32
33
=head1 SYNOPSIS
34
35
  use Koha::BiblioUtils::Iterator;
36
  my $rs = $schema->resultset('biblioitems');
37
  my $iterator = Koha::BiblioUtils::Iterator->new($rs);
38
  while (my $record = $iterator->next()) {
39
      // do something with $record
40
  }
41
42
=head1 METHODS
43
44
=cut
45
46
use C4::Biblio;
47
use Koha::Biblio::Metadata;
48
49
use Carp qw( confess );
50
use MARC::Record;
51
use MARC::File::XML;
52
use Modern::Perl;
53
54
=head2 new
55
56
    my $it = new($sth, option => $value, ...);
57
58
Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
59
options may be specified.
60
61
=head3 Options
62
63
=over 4
64
65
=item items
66
67
Set to true to include item data in the resulting MARC record.
68
69
=back
70
71
=cut
72
73
sub new {
74
    my ( $class, $rs, %options ) = @_;
75
76
    bless {
77
        rs => $rs,
78
        %options,
79
    }, $class;
80
}
81
82
=head2 next()
83
84
In a scalar context, provides the next MARC::Record from the ResultSet, or
85
C<undef> if there are no more.
86
87
In a list context it will provide ($biblionumber, $record).
88
89
=cut
90
91
sub next {
92
    my ($self) = @_;
93
94
    my $marc;
95
    my $row = $self->{rs}->next();
96
    return if !$row;
97
    my $marcxml = C4::Biblio::GetXmlBiblio( $row->get_column('biblionumber') );
98
    if ( $marcxml ) {
99
        $marc = MARC::Record->new_from_xml( $marcxml, 'UTF-8' );
100
    }
101
    else {
102
        confess "No marcxml column returned in the request.";
103
    }
104
105
    my $bibnum;
106
    if ( $self->{items} ) {
107
        $bibnum = $row->get_column('biblionumber');
108
        confess "No biblionumber column returned in the request."
109
          if ( !defined($bibnum) );
110
111
        $marc = Koha::Biblio::Metadata->record(
112
            {
113
                record       => $marc,
114
                embed_items  => 1,
115
                biblionumber => $bibnum,
116
            }
117
        );
118
    }
119
120
    if (wantarray) {
121
        $bibnum //= $row->get_column('biblionumber');
122
        confess "No biblionumber column returned in the request."
123
          if ( !defined($bibnum) );
124
        return ( $bibnum, $marc );
125
    }
126
    else {
127
        return $marc;
128
    }
129
}
130
131
1;
(-)a/t/db_dependent/Koha/BiblioUtils/Iterator.t (-71 lines)
Lines 1-70 Link Here
1
#!/usr/bin/perl
2
#
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 7;
21
22
use_ok('Koha::BiblioUtils');
23
use_ok('Koha::BiblioUtils::Iterator');
24
25
use Koha::Database;
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
28
29
my $schema = Koha::Database->new()->schema();
30
$schema->storage->txn_begin();
31
32
# Delete issues to prevent foreign constraint failures.
33
# blank all biblios, biblioitems, and items.
34
$schema->resultset('Issue')->delete();
35
$schema->resultset('Biblio')->delete();
36
37
my $location = 'My Location';
38
my $builder = t::lib::TestBuilder->new;
39
my $library = $builder->build({
40
    source => 'Branch',
41
});
42
my $itemtype = $builder->build({
43
   source => 'Itemtype',
44
});
45
46
# Create a biblio instance for testing
47
t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
48
my $biblio = $builder->build_sample_biblio();
49
50
# Add an item.
51
$builder->build_sample_item({ biblionumber => $biblio->biblionumber });
52
53
my $rs = $schema->resultset('Biblioitem')->search({});
54
my $iterator = Koha::BiblioUtils::Iterator->new($rs, items => 1 );
55
my $record = $iterator->next();
56
my $expected_tags = [ 100, 245, 942, 952, 999 ];
57
my @result_tags = map { $_->tag() } $record->field('...');
58
my @sorted_tags = sort @result_tags;
59
is_deeply(\@sorted_tags,$expected_tags, "Got the same tags as expected");
60
61
62
my $biblio_2 = $builder->build_sample_biblio();
63
my $biblio_3 = $builder->build_sample_biblio();
64
my $records = Koha::BiblioUtils->get_all_biblios_iterator();
65
is( $records->next->id, $biblio->biblionumber );
66
is( $records->next->id, $biblio_2->biblionumber );
67
is( $records->next->id, $biblio_3->biblionumber );
68
is( $records->next, undef, 'no more record' );
69
70
$schema->storage->txn_rollback();
71
- 

Return to bug 34359