Bug 12478: Fix error on indexing a specific record
[koha.git] / Koha / BiblioUtils / Iterator.pm
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 under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 that gives the MARC::Record of each biblio that's
29 returned by a L<DBIx::Class::ResultSet> that provides a C<biblionumber>, and
30 C<marc> or C<marcxml> column from the biblioitems table.
31
32 =head1 SYNOPSIS
33
34   use Koha::BiblioUtils::Iterator;
35   my $rs = $schema->resultset('biblioitems');
36   my $iterator = Koha::BiblioUtils::Iterator->new($rs);
37   while (my $record = $iterator->next()) {
38       // do something with $record
39   }
40
41 =head1 METHODS
42
43 =cut
44
45 use C4::Biblio;    # :( - for EmbedItemsInMarcBiblio
46
47 use Carp;
48 use MARC::Record;
49 use MARC::File::XML;
50 use Modern::Perl;
51
52 =head2 new
53
54     my $it = new($sth, option => $value, ...);
55
56 Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
57 options may be specified.
58
59 =head3 Options
60
61 =over 4
62
63 =item items
64
65 Set to true to include item data in the resulting MARC record.
66
67 =back
68
69 =cut
70
71 sub new {
72     my ( $class, $rs, %options ) = @_;
73
74     bless {
75         rs => $rs,
76         %options,
77     }, $class;
78 }
79
80 =head2 next()
81
82 In a scalar context, provides the next MARC::Record from the ResultSet, or
83 C<undef> if there are no more.
84
85 In a list context it will provide ($biblionumber, $record).
86
87 =cut
88
89 sub next {
90     my ($self) = @_;
91
92     my $marc;
93     my $row = $self->{rs}->next();
94     return if !$row;
95     if ( $row->marc ) {
96         $marc = MARC::Record->new_from_usmarc( $row->marc );
97     }
98     elsif ( $row->marcxml ) {
99         $marc = MARC::Record->new_from_xml( $row->marcxml );
100     }
101     else {
102         confess "No marc or 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         # TODO this should really be in Koha::BiblioUtils or something similar.
112         C4::Biblio::EmbedItemsInMarcBiblio( $marc, $bibnum );
113     }
114
115     if (wantarray) {
116         $bibnum //= $row->get_column('biblionumber');
117         confess "No biblionumber column returned in the request."
118           if ( !defined($bibnum) );
119         return ( $bibnum, $marc );
120     }
121     else {
122         return $marc;
123     }
124 }
125
126 1;