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