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