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
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.
24 Koha::BiblioUtils - contains fundamental biblio-related functions
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.
35 use C4
::Biblio
; # EmbedItemsInMarcBiblio
36 use Koha
::MetadataIterator
;
40 use Data
::Dumper
; # TODO remove
42 use base
qw(Koha::MetadataRecord);
44 __PACKAGE__
->mk_accessors(qw( record schema id datatype ));
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.
60 my $biblionumber = shift;
62 my $self = $class->SUPER::new
(
65 'schema' => lc C4
::Context
->preference("marcflavour"),
66 'id' => $biblionumber,
67 'datatype' => 'biblio',
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
87 If true, then the item data will be merged into the record when it's loaded.
91 It will return C<undef> if the biblio doesn't exist.
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();
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.
113 sub get_all_biblios_iterator
{
114 my $database = Koha
::Database
->new();
115 my $schema = $database->schema();
117 $schema->resultset('Biblio')->search( {},
118 { columns
=> [qw
/ biblionumber /] } );
119 my $next_func = sub {
120 # Warn and skip bad records, otherwise we break the loop
122 my $row = $rs->next();
124 my $marc = C4
::Biblio
::GetMarcBiblio
({
125 biblionumber
=> $row->biblionumber,
128 __PACKAGE__
->new($marc, $row->biblionumber);
131 warn "Something went wrong reading record for biblio $row->biblionumber: $@\n";
137 return Koha
::MetadataIterator
->new($next_func);
140 =head2 get_marc_biblio
142 my $marc = Koha::BiblioUtils->get_marc_biblio($bibnum, %options);
144 This non-class function fetches the MARC::Record for the given biblio number.
145 Nothing is returned if the biblionumber couldn't be found (or it somehow has no
154 If set to true, item data is embedded in the record. Default is to not do this.
160 sub get_marc_biblio
{
161 my ($class, $bibnum, %options) = @_;
163 return C4
::Biblio
::GetMarcBiblio
({
164 biblionumber
=> $bibnum,
165 embed_items
=> ($options{item_data
} ?
1 : 0 ) });