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