fetch MARC records directly from Koha database
[webpac2] / lib / WebPAC / Input / Koha.pm
1 package WebPAC::Input::Koha;
2
3 use warnings;
4 use strict;
5
6 use DBI;
7 use MARC::Fast;
8 use base qw/WebPAC::Common/;
9 use Carp qw/confess/;
10
11 =head1 NAME
12
13 WebPAC::Input::Koha - read MARC records from Koha
14
15 =cut
16
17 our $VERSION = '0.01';
18
19 =head1 FUNCTIONS
20
21 =head2 new
22
23   my $input = new WebPAC::Input::Koha(
24         dsn => '',
25         filter => \&code_ref,
26   }
27
28 =cut
29
30 sub new {
31         my $class = shift;
32         my $self = {@_};
33         bless($self, $class);
34
35         my $arg = {@_};
36
37         my $log = $self->_get_logger();
38
39         $log->info("opening Koha database '$arg->{dsn}'");
40
41         $self->{_dbh} = DBI->connect( $arg->{dsn}, $arg->{user}, $arg->{passwd}, { RaiseError => 1 } );
42         $self->{_sth} = $self->{_dbh}->prepare( $arg->{sql} );
43         $self->{_sth}->execute;
44
45         warn "got ", $self->{_sth}->rows, " rows for ", $arg->{sql};
46
47         open( $self->{_koha_fh}, '>', $arg->{path} ) || warn "not creating $arg->{path}: $!";
48
49         $self ? return $self : return undef;
50 }
51
52 =head2 fetch_rec
53
54 Return record with ID C<$mfn> from database
55
56   my $rec = $input->fetch_rec( $mfn );
57
58 =cut
59
60 sub fetch_rec {
61         my $self = shift;
62
63         my $mfn = shift;
64
65         my $row = $self->{_sth}->fetchrow_hashref;
66
67         if ( my $fh = $self->{_koha_fh} ) {
68                 my $marc = $row->{marc} || die "no marc?";
69                 print $fh $marc;
70         }
71
72         push @{$row->{'000'}}, $mfn;
73         return $row;
74 }
75
76 =head2 size
77
78 Return number of records in database
79
80   my $size = $isis->size;
81
82 =cut
83
84 sub size {
85         my $self = shift;
86         return $self->{_sth}->rows;
87 }
88
89
90 =head1 AUTHOR
91
92 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
93
94 =head1 COPYRIGHT & LICENSE
95
96 Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
97
98 This program is free software; you can redistribute it and/or modify it
99 under the same terms as Perl itself.
100
101 =cut
102
103 1;