cb7baed0dbd44341cd24ac4ab40a0e8e0260fcab
[koha.git] / Koha / BiblioUtils.pm
1 package Koha::BiblioUtils;
2
3 # This contains functions to do with managing 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 - contains fundamental biblio-related functions
25
26 =head1 DESCRIPTION
27
28 This contains functions for normal operations on biblio records.
29
30 =cut
31
32 use Koha::Biblios;
33 use Koha::MetadataIterator;
34 use Koha::Database;
35 use Modern::Perl;
36
37
38 use base qw(Koha::MetadataRecord);
39
40 __PACKAGE__->mk_accessors(qw( record schema id datatype ));
41
42 =head1 FUNCTIONS
43
44 =head2 new
45
46     my $biblio = Koha::BiblioUtils->new($marc_record, [$biblionumber]);
47
48 Creates an instance of C<Koha::BiblioUtils> based on the marc record. If known,
49 the biblionumber can be provided too.
50
51 =cut
52
53 sub new {
54     my $class = shift;
55     my $record = shift;
56     my $biblionumber = shift;
57
58     my $self = $class->SUPER::new(
59         {
60             'record'   => $record,
61             'schema'   => lc C4::Context->preference("marcflavour"),
62             'id'       => $biblionumber,
63             'datatype' => 'biblio',
64         }
65     );
66     bless $self, $class;
67     return $self;
68 }
69
70 =head2 get_from_biblionumber
71
72     my $biblio = Koha::BiblioUtils->get_from_biblionumber($biblionumber, %options);
73
74 This will give you an instance of L<Koha::BiblioUtils> that is the biblio that
75 you requested.
76
77 Options are:
78
79 =over 4
80
81 =item C<$item_data>
82
83 If true, then the item data will be merged into the record when it's loaded.
84
85 =back
86
87 It will return C<undef> if the biblio doesn't exist.
88
89 =cut
90
91 sub get_from_biblionumber {
92     my ($class, $bibnum, %options) = @_;
93
94     my $marc = $class->get_marc_biblio($bibnum, %options);
95     return $class->new($marc, $bibnum);
96 }
97
98 =head2 get_all_biblios_iterator
99
100     my $it = Koha::BiblioUtils->get_all_biblios_iterator(%options);
101
102 This will provide an iterator object that will, one by one, provide the
103 Koha::BiblioUtils of each biblio. This will include the item data.
104
105 The iterator is a Koha::MetadataIterator object.
106
107 Possible options are:
108
109 =over 4
110
111 =item C<slice>
112
113 slice may be defined as a hash of two values: index and count. index
114 is the slice number to process and count is total number of slices.
115 With this information the iterator returns just the given slice of
116 records instead of all.
117
118 =back
119
120 =cut
121
122 sub get_all_biblios_iterator {
123     my ($class, %options) = @_;
124
125     my $search_terms = {};
126     my ($slice_modulo, $slice_count);
127     if ($options{slice}) {
128         $slice_count = $options{slice}->{count};
129         $slice_modulo = $options{slice}->{index};
130         $search_terms = \[ 'mod(biblionumber, ?) = ?', $slice_count, $slice_modulo ];
131     }
132
133     my $search_options = { columns => [qw/ biblionumber /] };
134     if ( $options{desc} ){
135         $search_options->{order_by}  = { -desc => 'biblionumber' };
136     }
137
138     my $database = Koha::Database->new();
139     my $schema   = $database->schema();
140     my $rs = Koha::Biblios->search(
141         $search_terms,
142         $search_options );
143     my $next_func = sub {
144         # Warn and skip bad records, otherwise we break the loop
145         while (1) {
146             my $row = $rs->next();
147             return if !$row;
148             my $next = eval {
149                 my $marc = $row->metadata->record({ embed_items => 1 });
150                 $class->new($marc, $row->biblionumber);
151             };
152             if ($@) {
153                 warn sprintf "Something went wrong reading record for biblio %s: %s\n", $row->biblionumber, $@;
154                 next;
155             }
156             return $next;
157         }
158     };
159     return Koha::MetadataIterator->new($next_func);
160 }
161
162 =head2 get_marc_biblio
163
164     my $marc = Koha::BiblioUtils->get_marc_biblio($bibnum, %options);
165
166 This non-class function fetches the MARC::Record for the given biblio number.
167 Nothing is returned if the biblionumber couldn't be found (or it somehow has no
168 MARC data.)
169
170 Options are:
171
172 =over 4
173
174 =item item_data
175
176 If set to true, item data is embedded in the record. Default is to not do this.
177
178 =back
179
180 =cut
181
182 sub get_marc_biblio {
183     my ($class, $bibnum, %options) = @_;
184
185     my $record = Koha::Biblios->find($bibnum)
186       ->metadata->record( { $options{item_data} ? ( embed_items => 1 ) : () } );
187     return $record;
188 }
189
190 1;