Bug 22440: Move backend statuses api endpoint
[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
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;