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