From 95dabf4bfd15f6aed149c17546c00083063a01c1 Mon Sep 17 00:00:00 2001 From: Robin Sheat Date: Tue, 16 Sep 2014 17:34:25 +1200 Subject: [PATCH] Bug 12478 - add some base objects that the ES code will depend on Signed-off-by: Nick Clemens Signed-off-by: Jesse Weaver Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Brendan Gallagher --- Koha/Biblio/Iterator.pm | 126 +++++++++++++++++++++++++++++++ Koha/BiblioUtils.pm | 105 ++++++++++++++++++++++++++ Koha/Database.pm | 5 +- Koha/ItemType.pm | 4 + t/Koha/ItemType.pm | 46 +++++++++++ t/db_dependent/Koha/ItemTypes.pm | 65 ++++++++++++++++ 6 files changed, 349 insertions(+), 2 deletions(-) create mode 100644 Koha/Biblio/Iterator.pm create mode 100644 Koha/BiblioUtils.pm create mode 100755 t/Koha/ItemType.pm create mode 100755 t/db_dependent/Koha/ItemTypes.pm diff --git a/Koha/Biblio/Iterator.pm b/Koha/Biblio/Iterator.pm new file mode 100644 index 0000000000..fc150f433c --- /dev/null +++ b/Koha/Biblio/Iterator.pm @@ -0,0 +1,126 @@ +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 that provides a C, and +C or C 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 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; diff --git a/Koha/BiblioUtils.pm b/Koha/BiblioUtils.pm new file mode 100644 index 0000000000..58ae55c5dd --- /dev/null +++ b/Koha/BiblioUtils.pm @@ -0,0 +1,105 @@ +package Koha::BiblioUtils; + +# This contains functions to do with managing 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 - contains some handy biblio-related functions + +=head1 DESCRIPTION + +This contains functions for operations on biblio records. + +Note: really, C4::Biblio 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 Koha::Database; +use Modern::Perl; + +use base qw(Class::Accessor); + +__PACKAGE__->mk_accessors(qw()); + +=head1 FUNCTIONS + +=head2 get_all_biblios_iterator + + my $it = 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. + +The iterator is a Koha::Biblio::Iterator object. + +=cut + +sub get_all_biblios_iterator { + 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); +} + +=head2 get_marc_biblio + + my $marc = 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.) + +Options are: + +=over 4 + +=item item_data + +If set to true, item data is embedded in the record. Default is to not do this. + +=back + +=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}; + + return $marc; +} + +1; diff --git a/Koha/Database.pm b/Koha/Database.pm index a8f4eab9a8..ff289c220d 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -41,6 +41,8 @@ use vars qw($database); __PACKAGE__->mk_accessors(qw( )); +our $schema; # the schema is a singleton + # _new_schema # Internal helper function (not a method!). This creates a new # database connection from the data given in the current context, and @@ -110,8 +112,7 @@ creates one, and connects to the database. This database handle is cached for future use: if you call C<$database-Eschema> twice, you will get the same handle both -times. If you need a second database handle, use C<&new_schema> and -possibly C<&set_schema>. +times. If you need a second database handle, use C<&new_schema>. =cut diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index 48df808dc7..17d62268fe 100644 --- a/Koha/ItemType.pm +++ b/Koha/ItemType.pm @@ -1,5 +1,9 @@ package Koha::ItemType; +# This represents a single itemtype + +# Copyright 2014 Catalyst IT +# # This file is part of Koha. # # Koha is free software; you can redistribute it and/or modify it under the diff --git a/t/Koha/ItemType.pm b/t/Koha/ItemType.pm new file mode 100755 index 0000000000..e47d5e26cb --- /dev/null +++ b/t/Koha/ItemType.pm @@ -0,0 +1,46 @@ +#!/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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Test::More tests => 8; + +BEGIN { + use_ok('Koha::ItemType'); +} + +my $data = { + itemtype => 'CODE', + description => 'description', + rentalcharge => 'rentalcharge', + imageurl => 'imageurl', + summary => 'summary', + checkinmsg => 'checkinmsg', + checkinmsgtype => 'checkinmsgtype', +}; + +my $type = Koha::ItemType->new($data); + +is( $type->code, 'CODE', 'itemtype/code' ); +is( $type->description, 'description', 'description' ); +is( $type->rentalcharge, 'rentalcharge', 'rentalcharge' ); +is( $type->imageurl, 'imageurl', 'imageurl' ); +is( $type->summary, 'summary', 'summary' ); +is( $type->checkinmsg, 'checkinmsg', 'checkinmsg' ); +is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' ); diff --git a/t/db_dependent/Koha/ItemTypes.pm b/t/db_dependent/Koha/ItemTypes.pm new file mode 100755 index 0000000000..dfefd08ce8 --- /dev/null +++ b/t/db_dependent/Koha/ItemTypes.pm @@ -0,0 +1,65 @@ +#!/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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +# XXX This doesn't work because I need to figure out how to do transactions +# in a test-case with DBIx::Class + +use Modern::Perl; + +use Test::More tests => 8; +use Data::Dumper; + +BEGIN { + use_ok('Koha::ItemTypes'); +} + +my $dbh = C4::Context->dbh; + +# Start transaction +$dbh->{AutoCommit} = 0; +$dbh->{RaiseError} = 1; + +my $prep = $dbh->prepare('INSERT INTO itemtypes (itemtype, description, rentalcharge, imageurl, summary, checkinmsg, checkinmsgtype) VALUES (?,?,?,?,?,?,?)'); +$prep->execute('type1', 'description', 'rentalcharge', 'imageurl', 'summary', 'checkinmsg', 'checkinmsgtype'); +$prep->execute('type2', 'description', 'rentalcharge', 'imageurl', 'summary', 'checkinmsg', 'checkinmsgtype'); + +my $itypes = Koha::ItemTypes->new(); + +my @types = $itypes->get_itemtype('type1', 'type2'); + +die Dumper(\@types); +my $type = $types[0]; +ok(defined($type), 'first result'); +is( $type->code, 'type1', 'itemtype/code' ); +is( $type->description, 'description', 'description' ); +is( $type->rentalcharge, 'rentalcharge', 'rentalcharge' ); +is( $type->imageurl, 'imageurl', 'imageurl' ); +is( $type->summary, 'summary', 'summary' ); +is( $type->checkinmsg, 'checkinmsg', 'checkinmsg' ); +is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' ); + +$type = $types[1]; +ok(defined($type), 'second result'); +is( $type->code, 'type2', 'itemtype/code' ); +is( $type->description, 'description', 'description' ); +is( $type->rentalcharge, 'rentalcharge', 'rentalcharge' ); +is( $type->imageurl, 'imageurl', 'imageurl' ); +is( $type->summary, 'summary', 'summary' ); +is( $type->checkinmsg, 'checkinmsg', 'checkinmsg' ); +is( $type->checkinmsgtype, 'checkinmsgtype', 'checkinmsgtype' ); -- 2.39.5