From 0406ea7ba594c7eb629426633ad3e0568d25df56 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 9 Jul 2009 17:00:51 +0000 Subject: [PATCH] fetch MARC records directly from Koha database Just create local file out of them, they need to be converted in hash to be really useful inside WebPAC git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1231 07558da8-63fa-0310-ba24-9fe276d99e06 --- lib/WebPAC/Input/Koha.pm | 103 +++++++++++++++++++++++++++++++++++++++ t/2-input-koha.t | 41 ++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 lib/WebPAC/Input/Koha.pm create mode 100755 t/2-input-koha.t diff --git a/lib/WebPAC/Input/Koha.pm b/lib/WebPAC/Input/Koha.pm new file mode 100644 index 0000000..194fd3e --- /dev/null +++ b/lib/WebPAC/Input/Koha.pm @@ -0,0 +1,103 @@ +package WebPAC::Input::Koha; + +use warnings; +use strict; + +use DBI; +use MARC::Fast; +use base qw/WebPAC::Common/; +use Carp qw/confess/; + +=head1 NAME + +WebPAC::Input::Koha - read MARC records from Koha + +=cut + +our $VERSION = '0.01'; + +=head1 FUNCTIONS + +=head2 new + + my $input = new WebPAC::Input::Koha( + dsn => '', + filter => \&code_ref, + } + +=cut + +sub new { + my $class = shift; + my $self = {@_}; + bless($self, $class); + + my $arg = {@_}; + + my $log = $self->_get_logger(); + + $log->info("opening Koha database '$arg->{dsn}'"); + + $self->{_dbh} = DBI->connect( $arg->{dsn}, $arg->{user}, $arg->{passwd}, { RaiseError => 1 } ); + $self->{_sth} = $self->{_dbh}->prepare( $arg->{sql} ); + $self->{_sth}->execute; + + warn "got ", $self->{_sth}->rows, " rows for ", $arg->{sql}; + + open( $self->{_koha_fh}, '>', $arg->{path} ) || warn "not creating $arg->{path}: $!"; + + $self ? return $self : return undef; +} + +=head2 fetch_rec + +Return record with ID C<$mfn> from database + + my $rec = $input->fetch_rec( $mfn ); + +=cut + +sub fetch_rec { + my $self = shift; + + my $mfn = shift; + + my $row = $self->{_sth}->fetchrow_hashref; + + if ( my $fh = $self->{_koha_fh} ) { + my $marc = $row->{marc} || die "no marc?"; + print $fh $marc; + } + + push @{$row->{'000'}}, $mfn; + return $row; +} + +=head2 size + +Return number of records in database + + my $size = $isis->size; + +=cut + +sub size { + my $self = shift; + return $self->{_sth}->rows; +} + + +=head1 AUTHOR + +Dobrica Pavlinusic, C<< >> + +=head1 COPYRIGHT & LICENSE + +Copyright 2009 Dobrica Pavlinusic, All Rights Reserved. + +This program is free software; you can redistribute it and/or modify it +under the same terms as Perl itself. + +=cut + +1; diff --git a/t/2-input-koha.t b/t/2-input-koha.t new file mode 100755 index 0000000..b278f72 --- /dev/null +++ b/t/2-input-koha.t @@ -0,0 +1,41 @@ +#!/usr/bin/perl -w + +use strict; +use lib 'lib'; + +use Test::More tests => 27; + +BEGIN { +use_ok( 'WebPAC::Test' ); +use_ok( 'WebPAC::Input' ); +} + +my $module = 'WebPAC::Input::Koha'; +diag "testing with $module"; + +ok(my $input = new WebPAC::Input( + module => $module, + no_progress_bar => 1, + %LOG +), "new"); + +ok(my $db = $input->open( + path => '/tmp/koha.marc', # required? + dsn => 'dbi:mysql:database=koha', + user => $ENV{KOHA_USER}, + passwd => $ENV{KOHA_PASSWD}, + sql => q{ + select biblioitemnumber as mfn, marc from biblioitems limit 7 + }, +), "open"); +ok(my $size = $input->size, "size"); +cmp_ok( $size, '==', 7, 'size ok' ); + +foreach my $mfn ( 1 ... $size ) { + my $rec = $input->fetch; + ok($rec, "fetch $mfn"); + cmp_ok($rec->{'000'}->[0], '==', $mfn, 'has mfn'); + cmp_ok($input->pos, '==', $mfn, "pos $mfn"); + diag "rec: ", dump($rec), "\n" if $debug; +} + -- 2.20.1