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