Bug 24317: Sample patron data not loading for non-English installations
[koha.git] / Koha / BiblioUtils.pm
blobfd6969d8bff914533c5a2ddf1f32b7002631da7a
1 package Koha::BiblioUtils;
3 # This contains functions to do with managing biblio records.
5 # Copyright 2014 Catalyst IT
7 # This file is part of Koha.
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.
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.
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.
22 =head1 NAME
24 Koha::BiblioUtils - contains fundamental biblio-related functions
26 =head1 DESCRIPTION
28 This contains functions for normal operations on biblio records.
30 Note: really, C4::Biblio does the main functions, but the Koha namespace is
31 the new thing that should be used.
33 =cut
35 use C4::Biblio; # EmbedItemsInMarcBiblio
36 use Koha::MetadataIterator;
37 use Koha::Database;
38 use Modern::Perl;
40 use Data::Dumper; # TODO remove
42 use base qw(Koha::MetadataRecord);
44 __PACKAGE__->mk_accessors(qw( record schema id datatype ));
46 =head1 FUNCTIONS
48 =head2 new
50 my $biblio = Koha::BiblioUtils->new($marc_record, [$biblionumber]);
52 Creates an instance of C<Koha::BiblioUtils> based on the marc record. If known,
53 the biblionumber can be provided too.
55 =cut
57 sub new {
58 my $class = shift;
59 my $record = shift;
60 my $biblionumber = shift;
62 my $self = $class->SUPER::new(
64 'record' => $record,
65 'schema' => lc C4::Context->preference("marcflavour"),
66 'id' => $biblionumber,
67 'datatype' => 'biblio',
70 bless $self, $class;
71 return $self;
74 =head2 get_from_biblionumber
76 my $biblio = Koha::BiblioUtils->get_from_biblionumber($biblionumber, %options);
78 This will give you an instance of L<Koha::BiblioUtils> that is the biblio that
79 you requested.
81 Options are:
83 =over 4
85 =item C<$item_data>
87 If true, then the item data will be merged into the record when it's loaded.
89 =back
91 It will return C<undef> if the biblio doesn't exist.
93 =cut
95 sub get_from_biblionumber {
96 my ($class, $bibnum, %options) = @_;
98 my $marc = $class->get_marc_biblio($bibnum, %options);
99 return $class->new($marc, $bibnum);
102 =head2 get_all_biblios_iterator
104 my $it = Koha::BiblioUtils->get_all_biblios_iterator(%options);
106 This will provide an iterator object that will, one by one, provide the
107 Koha::BiblioUtils of each biblio. This will include the item data.
109 The iterator is a Koha::MetadataIterator object.
111 Possible options are:
113 =over 4
115 =item C<slice>
117 slice may be defined as a hash of two values: index and count. index
118 is the slice number to process and count is total number of slices.
119 With this information the iterator returns just the given slice of
120 records instead of all.
122 =back
124 =cut
126 sub get_all_biblios_iterator {
127 my ($self, %options) = @_;
129 my $search_terms = {};
130 my ($slice_modulo, $slice_count);
131 if ($options{slice}) {
132 $slice_count = $options{slice}->{count};
133 $slice_modulo = $options{slice}->{index};
134 $search_terms = \[ 'mod(biblionumber, ?) = ?', $slice_count, $slice_modulo ];
137 my $database = Koha::Database->new();
138 my $schema = $database->schema();
139 my $rs =
140 $schema->resultset('Biblio')->search(
141 $search_terms,
142 { columns => [qw/ biblionumber /] } );
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 $marc = C4::Biblio::GetMarcBiblio({
149 biblionumber => $row->biblionumber,
150 embed_items => 1 });
151 my $next = eval {
152 __PACKAGE__->new($marc, $row->biblionumber);
154 if ($@) {
155 warn "Something went wrong reading record for biblio $row->biblionumber: $@\n";
156 next;
158 return $next;
161 return Koha::MetadataIterator->new($next_func);
164 =head2 get_marc_biblio
166 my $marc = Koha::BiblioUtils->get_marc_biblio($bibnum, %options);
168 This non-class function fetches the MARC::Record for the given biblio number.
169 Nothing is returned if the biblionumber couldn't be found (or it somehow has no
170 MARC data.)
172 Options are:
174 =over 4
176 =item item_data
178 If set to true, item data is embedded in the record. Default is to not do this.
180 =back
182 =cut
184 sub get_marc_biblio {
185 my ($class, $bibnum, %options) = @_;
187 return C4::Biblio::GetMarcBiblio({
188 biblionumber => $bibnum,
189 embed_items => ($options{item_data} ? 1 : 0 ) });