+++ /dev/null
-package Koha::Biblio::Iterator;
-
-# This contains an iterator over biblio records
-
-# Copyright 2014 Catalyst IT
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
-#
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-
-=head1 NAME
-
-Koha::Biblio::Iterator - iterates over biblios provided by a DBIx::Class::ResultSet
-
-=head1 DESCRIPTION
-
-This provides an iterator that gives the MARC::Record of each biblio that's
-returned by a L<DBIx::Class::ResultSet> that provides a C<biblionumber>, and
-C<marc> or C<marcxml> column from the biblioitems table.
-
-=head1 SYNOPSIS
-
- use Koha::Biblio::Iterator;
- my $rs = $schema->resultset('biblioitems');
- my $iterator = Koha::Biblio::Iterator->new($rs);
- while (my $record = $iterator->next()) {
- // do something with $record
- }
-
-=head1 METHODS
-
-=cut
-
-use C4::Biblio; # :( - for EmbedItemsInMarcBiblio
-
-use Carp;
-use MARC::Record;
-use MARC::File::XML;
-use Modern::Perl;
-
-=head2 new
-
- my $it = new($sth, option => $value, ...);
-
-Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
-options may be specified.
-
-=head3 Options
-
-=over 4
-
-=item items
-
-Set to true to include item data in the resulting MARC record.
-
-=back
-
-=cut
-
-sub new {
- my ( $class, $rs, %options ) = @_;
-
- bless {
- rs => $rs,
- %options,
- }, $class;
-}
-
-=head2 next()
-
-In a scalar context, provides the next MARC::Record from the ResultSet, or
-C<undef> if there are no more.
-
-In a list context it will provide ($biblionumber, $record).
-
-=cut
-
-sub next {
- my ($self) = @_;
-
- my $marc;
- my $row = $self->{rs}->next();
- return if !$row;
- if ( $row->marc ) {
- $marc = MARC::Record->new_from_usmarc( $row->marc );
- }
- elsif ( $row->marcxml ) {
- $marc = MARC::Record->new_from_xml( $row->marcxml );
- }
- else {
- confess "No marc or marcxml column returned in the request.";
- }
-
- my $bibnum;
- if ( $self->{items} ) {
- $bibnum = $row->get_column('biblionumber');
- confess "No biblionumber column returned in the request."
- if ( !defined($bibnum) );
-
- # TODO this should really be in Koha::Biblio or something similar.
- C4::Biblio::EmbedItemsInMarcBiblio( $marc, $bibnum );
- }
-
- if (wantarray) {
- $bibnum //= $row->get_column('biblionumber');
- confess "No biblionumber column returned in the request."
- if ( !defined($bibnum) );
- return ( $bibnum, $marc );
- }
- else {
- return $marc;
- }
-}
-
-1;
-package Koha::BiblioUtils;
+package Koha::BiblioUtilsUtils;
# This contains functions to do with managing biblio records.
=head1 NAME
-Koha::BiblioUtils - contains some handy biblio-related functions
+Koha::BiblioUtils - contains fundamental biblio-related functions
=head1 DESCRIPTION
-This contains functions for operations on biblio records.
+This contains functions for normal operations on biblio records.
-Note: really, C4::Biblio does the main functions, but the Koha namespace is
+Note: really, C4::BiblioUtils does the main functions, but the Koha namespace is
the new thing that should be used.
=cut
-use C4::Biblio; # EmbedItemsInMarcBiblio
-use Koha::Biblio::Iterator;
+use C4::BiblioUtils; # EmbedItemsInMarcBiblio
+use Koha::MetadataIterator;
use Koha::Database;
use Modern::Perl;
-use base qw(Class::Accessor);
+use base qw(Koha::MetadataRecord);
-__PACKAGE__->mk_accessors(qw());
+__PACKAGE__->mk_accessors(qw( record schema idnumber datatype ));
=head1 FUNCTIONS
+=head2 new
+
+ my $biblio = Koha::BiblioUtils->new($marc_record, [$biblionumber]);
+
+Creates an instance of C<Koha::BiblioUtils> based on the marc record. If known,
+the biblionumber can be provided too.
+
+=cut
+
+sub new {
+ my $class = shift;
+ my $record = shift;
+ my $biblionumber = shift;
+
+ my $self = $class->SUPER::new(
+ {
+ 'record' => $record,
+ 'schema' => lc C4::Context->preference("marcflavour"),
+ 'idnumber' => $biblionumber,
+ 'datatype' => 'biblio',
+ }
+ );
+ bless $self, $class;
+ return $self;
+}
+
+=head2 get_from_biblionumber
+
+ my $biblio = Koha::BiblioUtils->get_from_biblionumber($biblionumber, %options);
+
+This will give you an instance of L<Koha::BiblioUtils> that is the biblio that
+you requested.
+
+Options are:
+
+=over 4
+
+=item C<$item_data>
+
+If true, then the item data will be merged into the record when it's loaded.
+
+=back
+
+It will return C<undef> if the biblio doesn't exist.
+
+=cut
+
+sub get_from_biblionumber {
+ my ($class, $bibnum, %options) = @_;
+
+ my $marc = $class->get_marc_biblio($bibnum, %options);
+ return $class->new($marc, $bibnum);
+}
+
=head2 get_all_biblios_iterator
- my $it = get_all_biblios_iterator();
+ my $it = Koha::BiblioUtils->get_all_biblios_iterator();
This will provide an iterator object that will, one by one, provide the
-MARC::Record of each biblio. This will include the item data.
+Koha::BiblioUtils of each biblio. This will include the item data.
-The iterator is a Koha::Biblio::Iterator object.
+The iterator is a Koha::MetadataIterator object.
=cut
my $database = Koha::Database->new();
my $schema = $database->schema();
my $rs =
- $schema->resultset('Biblioitem')->search( { marc => { '!=', undef } },
- { columns => [qw/ biblionumber marc /] } );
- return Koha::Biblio::Iterator->new($rs, items => 1);
+ $schema->resultset('Biblio')->search( {},
+ { columns => [qw/ biblionumber /] } );
+ my $next_func = sub {
+ my $row = $rs->next();
+ return undef if !$row;
+ my $marc = C4::Biblio::GetMarcBiblio( $row->biblionumber, 1 );
+ return __PACKAGE__->new($marc, $row->biblionumber);
+ };
+ return Koha::MetadataIterator->new($next_func);
}
=head2 get_marc_biblio
- my $marc = get_marc_biblio($bibnum, %options);
+ my $marc = Koha::BiblioUtils->get_marc_biblio($bibnum, %options);
-This fetches the MARC::Record for the given biblio number. Nothing is returned
-if the biblionumber couldn't be found (or it somehow has no MARC data.)
+This non-class function fetches the MARC::Record for the given biblio number.
+Nothing is returned if the biblionumber couldn't be found (or it somehow has no
+MARC data.)
Options are:
=cut
sub get_marc_biblio {
- my ($class,$bibnum, %options) = @_;
-
- my $database = Koha::Database->new();
- my $schema = $database->schema();
- my $rs =
- $schema->resultset('Biblioitem')
- ->search( { marc => { '!=', undef }, biblionumber => $bibnum },
- { columns => [qw/ marc /] } );
-
- my $row = $rs->next();
- return unless $row;
- my $marc = MARC::Record->new_from_usmarc($row->marc);
-
- # TODO implement this in this module
- C4::Biblio::EmbedItemsInMarcBiblio($marc, $bibnum) if $options{item_data};
+ my ($class, $bibnum, %options) = @_;
- return $marc;
+ return C4::Biblio::GetMarcBiblio( $bibnum, ($options{item_data} ? 1 : 0 ) );
}
1;
--- /dev/null
+package Koha::BiblioUtils::Iterator;
+
+# This contains an iterator over biblio records
+
+# Copyright 2014 Catalyst IT
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it under the
+# terms of the GNU General Public License as published by the Free Software
+# Foundation; either version 3 of the License, or (at your option) any later
+# version.
+#
+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+=head1 NAME
+
+Koha::BiblioUtils::Iterator - iterates over biblios provided by a DBIx::Class::ResultSet
+
+=head1 DESCRIPTION
+
+This provides an iterator that gives the MARC::Record of each biblio that's
+returned by a L<DBIx::Class::ResultSet> that provides a C<biblionumber>, and
+C<marc> or C<marcxml> column from the biblioitems table.
+
+=head1 SYNOPSIS
+
+ use Koha::BiblioUtils::Iterator;
+ my $rs = $schema->resultset('biblioitems');
+ my $iterator = Koha::BiblioUtils::Iterator->new($rs);
+ while (my $record = $iterator->next()) {
+ // do something with $record
+ }
+
+=head1 METHODS
+
+=cut
+
+use C4::Biblio; # :( - for EmbedItemsInMarcBiblio
+
+use Carp;
+use MARC::Record;
+use MARC::File::XML;
+use Modern::Perl;
+
+=head2 new
+
+ my $it = new($sth, option => $value, ...);
+
+Takes a ResultSet to iterate over, and gives you an iterator on it. Optional
+options may be specified.
+
+=head3 Options
+
+=over 4
+
+=item items
+
+Set to true to include item data in the resulting MARC record.
+
+=back
+
+=cut
+
+sub new {
+ my ( $class, $rs, %options ) = @_;
+
+ bless {
+ rs => $rs,
+ %options,
+ }, $class;
+}
+
+=head2 next()
+
+In a scalar context, provides the next MARC::Record from the ResultSet, or
+C<undef> if there are no more.
+
+In a list context it will provide ($biblionumber, $record).
+
+=cut
+
+sub next {
+ my ($self) = @_;
+
+ my $marc;
+ my $row = $self->{rs}->next();
+ return if !$row;
+ if ( $row->marc ) {
+ $marc = MARC::Record->new_from_usmarc( $row->marc );
+ }
+ elsif ( $row->marcxml ) {
+ $marc = MARC::Record->new_from_xml( $row->marcxml );
+ }
+ else {
+ confess "No marc or marcxml column returned in the request.";
+ }
+
+ my $bibnum;
+ if ( $self->{items} ) {
+ $bibnum = $row->get_column('biblionumber');
+ confess "No biblionumber column returned in the request."
+ if ( !defined($bibnum) );
+
+ # TODO this should really be in Koha::BiblioUtils or something similar.
+ C4::Biblio::EmbedItemsInMarcBiblio( $marc, $bibnum );
+ }
+
+ if (wantarray) {
+ $bibnum //= $row->get_column('biblionumber');
+ confess "No biblionumber column returned in the request."
+ if ( !defined($bibnum) );
+ return ( $bibnum, $marc );
+ }
+ else {
+ return $marc;
+ }
+}
+
+1;
|);
print "Upgrade to $DBversion done (Bug 8007: Add System Preferences useDischarge, the discharge notice and the new table discharges)\n";
- SetVersion ($DBversion);
+ SetVersion($DBversion);
}
$DBversion = "3.19.00.036";
use autodie;
use Getopt::Long;
use Koha::Authority;
-use Koha::Biblio;
+use Koha::BiblioUtils;
use Koha::ElasticSearch::Indexer;
use MARC::Field;
use MARC::Record;
$next = sub {
my $r = shift @biblionumbers;
return () unless defined $r;
- return ($r, Koha::Biblio->get_from_biblionumber($r, item_data => 1 ));
+ return ($r, Koha::BiblioUtils->get_from_biblionumber($r, item_data => 1 ));
};
} else {
- my $records = Koha::Biblio->get_all_biblios_iterator();
+ my $records = Koha::BiblioUtils->get_all_biblios_iterator();
$next = sub {
$records->next();
}
+++ /dev/null
-#!/usr/bin/perl
-#
-# Copyright 2014 Catalyst IT
-#
-# This file is part of Koha.
-#
-# Koha is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
-#
-# Koha is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with Koha; if not, see <http://www.gnu.org/licenses>.
-
-use Modern::Perl;
-
-use C4::Context;
-use C4::Biblio qw( AddBiblio );
-use Koha::Database;
-use Koha::Libraries;
-use Koha::Patrons;
-
-use Test::More tests => 4;
-
-use_ok('Koha::Biblio');
-use_ok('Koha::Biblios');
-
-my $schema = Koha::Database->new()->schema();
-$schema->storage->txn_begin();
-
-my $dbh = C4::Context->dbh;
-$dbh->{RaiseError} = 1;
-
-my @branches = Koha::Libraries->search();
-my $borrower = Koha::Patrons->search()->next();
-
-my $biblio = MARC::Record->new();
-$biblio->append_fields(
- MARC::Field->new( '100', ' ', ' ', a => 'Hall, Kyle' ),
- MARC::Field->new( '245', ' ', ' ', a => "Test Record", b => "Test Record Subtitle", b => "Another Test Record Subtitle" ),
-);
-my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
-
-my $field_mappings = Koha::Database->new()->schema()->resultset('Fieldmapping');
-$field_mappings->delete();
-$field_mappings->create( { field => 'subtitle', fieldcode => '245', subfieldcode => 'b' } );
-
-$biblio = Koha::Biblios->find( $biblionumber );
-my @subtitles = $biblio->subtitles();
-is( $subtitles[0], 'Test Record Subtitle', 'Got first subtitle correctly' );
-is( $subtitles[1], 'Another Test Record Subtitle', 'Got second subtitle correctly' );
-
-$schema->storage->txn_rollback();
-
-1;
--- /dev/null
+#!/usr/bin/perl
+#
+# Copyright 2014 Catalyst IT
+#
+# This file is part of Koha.
+#
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use C4::Context;
+use C4::Biblio qw( AddBiblio );
+use Koha::Database;
+use Koha::Libraries;
+use Koha::Patrons;
+
+use Test::More tests => 4;
+
+use_ok('Koha::Biblio');
+use_ok('Koha::Biblios');
+
+my $schema = Koha::Database->new()->schema();
+$schema->storage->txn_begin();
+
+my $dbh = C4::Context->dbh;
+$dbh->{RaiseError} = 1;
+
+my @branches = Koha::Libraries->search();
+my $borrower = Koha::Patrons->search()->next();
+
+my $biblio = MARC::Record->new();
+$biblio->append_fields(
+ MARC::Field->new( '100', ' ', ' ', a => 'Hall, Kyle' ),
+ MARC::Field->new( '245', ' ', ' ', a => "Test Record", b => "Test Record Subtitle", b => "Another Test Record Subtitle" ),
+);
+my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $biblio, '' );
+
+my $field_mappings = Koha::Database->new()->schema()->resultset('Fieldmapping');
+$field_mappings->delete();
+$field_mappings->create( { field => 'subtitle', fieldcode => '245', subfieldcode => 'b' } );
+
+$biblio = Koha::Biblios->find( $biblionumber );
+my @subtitles = $biblio->subtitles();
+is( $subtitles[0], 'Test Record Subtitle', 'Got first subtitle correctly' );
+is( $subtitles[1], 'Another Test Record Subtitle', 'Got second subtitle correctly' );
+
+$schema->storage->txn_rollback();
+
+1;