moved clean into WebPAC::Output::Estraier, cleanup
[webpac2] / lib / WebPAC / Input / ISIS.pm
1 package WebPAC::Input::ISIS;
2
3 use warnings;
4 use strict;
5
6 use WebPAC::Input;
7
8 =head1 NAME
9
10 WebPAC::Input::ISIS - support for CDS/ISIS database files
11
12 =head1 VERSION
13
14 Version 0.03
15
16 =cut
17
18 our $VERSION = '0.03';
19
20
21 =head1 SYNOPSIS
22
23 Open CDS/ISIS, WinISIS or IsisMarc database using C<Biblio::Isis> or
24 C<OpenIsis> module and read all records to memory.
25
26  my $isis = new WebPAC::Input::ISIS();
27  $isis->open( path => '/path/to/ISIS/ISIS' );
28
29 =head1 FUNCTIONS
30
31 =head2 init
32
33 Autoconfigure this module to use C<Biblio::Isis> or C<OpenIsis>.
34
35 =cut
36
37 sub init {
38         my $self = shift;
39
40         eval "use Biblio::Isis;";
41         unless ($@) { 
42                 $self->{have_biblio_isis} = 1
43         } else {
44                 eval "use OpenIsis;";
45                 $self->{have_openisis} = 1 unless ($@);
46         }
47 }
48
49 =head2 open_db
50
51 Returns handle to database
52
53   my $db = $open_db(
54         path => '/path/to/LIBRI'
55   }
56
57 =cut
58
59 sub open_db {
60         my $self = shift;
61
62         my $arg = {@_};
63
64         my $log = $self->_get_logger();
65
66         $log->info("opening ISIS database '$arg->{path}'");
67
68         my ($isis_db,$db_size);
69
70         if ($self->{have_openisis}) {
71                 $log->debug("using OpenIsis perl bindings");
72                 $isis_db = OpenIsis::open($arg->{path});
73                 $db_size = OpenIsis::maxRowid( $isis_db ) || 1;
74         } elsif ($self->{have_biblio_isis}) {
75                 $log->debug("using Biblio::Isis");
76                 use Biblio::Isis;
77                 $isis_db = new Biblio::Isis(
78                         isisdb => $arg->{path},
79                         include_deleted => 1,
80                         hash_filter => sub {
81                                 my $l = shift || return;
82                                 $l = $self->{iconv}->convert($l) if ($self->{iconv});
83                                 return $l;
84                         },
85                 ) or $log->logdie("can't find database $arg->{path}");
86
87                 $db_size = $isis_db->count;
88
89         } else {
90                 $log->logdie("Can't find supported ISIS library for perl. I suggent that you install Bilbio::Isis from CPAN.");
91         }
92
93         return ($isis_db, $db_size);
94 }
95
96 =head2 fetch_rec
97
98 Return record with ID C<$mfn> from database
99
100   my $rec = $self->fetch_rec( $db, $mfn );
101
102 }
103
104 =cut
105
106 sub fetch_rec {
107         my $self = shift;
108
109         my ($isis_db, $mfn) = @_;
110
111         my $rec;
112
113         if ($self->{have_openisis}) {
114
115                 # read record using OpenIsis
116                 my $row = OpenIsis::read( $isis_db, $mfn );
117
118                 # convert record to hash
119                 foreach my $k (keys %{$row}) {
120                         if ($k ne "mfn") {
121                                 foreach my $l (@{$row->{$k}}) {
122                                         $l = $self->{iconv}->convert($l) if ($self->{iconv});
123                                         # has subfields?
124                                         my $val;
125                                         if ($l =~ m/\^/) {
126                                                 foreach my $t (split(/\^/,$l)) {
127                                                         next if (! $t);
128                                                         $val->{substr($t,0,1)} = substr($t,1);
129                                                 }
130                                         } else {
131                                                 $val = $l;
132                                         }
133                                         push @{$rec->{$k}}, $val;
134                                 }
135                         } else {
136                                 push @{$rec->{'000'}}, $mfn;
137                         }
138                 }
139
140         } elsif ($self->{have_biblio_isis}) {
141                 $rec = $isis_db->to_hash($mfn);
142         } else {
143                 $self->_get_logger()->logdie("hum? implementation missing?");
144         }
145
146         return $rec;
147 }
148
149 =head1 AUTHOR
150
151 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
152
153 =head1 COPYRIGHT & LICENSE
154
155 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
156
157 This program is free software; you can redistribute it and/or modify it
158 under the same terms as Perl itself.
159
160 =cut
161
162 1; # End of WebPAC::Input::ISIS