1 |
package WebPAC::Input::ISIS; |
2 |
|
3 |
use warnings; |
4 |
use strict; |
5 |
|
6 |
use WebPAC::Common; |
7 |
use base qw/WebPAC::Input WebPAC::Common/; |
8 |
use Text::Iconv; |
9 |
|
10 |
=head1 NAME |
11 |
|
12 |
WebPAC::Input::ISIS - support for CDS/ISIS source files |
13 |
|
14 |
=head1 VERSION |
15 |
|
16 |
Version 0.01 |
17 |
|
18 |
=cut |
19 |
|
20 |
our $VERSION = '0.01'; |
21 |
|
22 |
|
23 |
# auto-configure |
24 |
|
25 |
my ($have_biblio_isis, $have_openisis) = (0,0); |
26 |
|
27 |
eval "use Biblio::Isis 0.13;"; |
28 |
unless ($@) { |
29 |
$have_biblio_isis = 1 |
30 |
} else { |
31 |
eval "use OpenIsis;"; |
32 |
$have_openisis = 1 unless ($@); |
33 |
} |
34 |
|
35 |
=head1 SYNOPSIS |
36 |
|
37 |
Open CDS/ISIS, WinISIS or IsisMarc database using Biblio::Isis or OpenIsis |
38 |
module and read all records to memory. |
39 |
|
40 |
my $isis = new WebPAC::Input::ISIS(); |
41 |
$isis->open( filename => '/path/to/ISIS/ISIS' ); |
42 |
|
43 |
=head1 FUNCTIONS |
44 |
|
45 |
=head2 open |
46 |
|
47 |
This function will read whole database in memory and produce lookups. |
48 |
|
49 |
$isis->open( |
50 |
filename => '/data/ISIS/ISIS', |
51 |
code_page => '852', |
52 |
limit_mfn => 500, |
53 |
start_mfn => 6000, |
54 |
lookup => $lookup_obj, |
55 |
); |
56 |
|
57 |
By default, ISIS code page is assumed to be C<852>. |
58 |
|
59 |
If optional parametar C<start_mfn> is set, this will be first MFN to read |
60 |
from database (so you can skip beginning of your database if you need to). |
61 |
|
62 |
If optional parametar C<limit_mfn> is set, it will read just 500 records |
63 |
from database in example above. |
64 |
|
65 |
Returns size of database, regardless of C<start_mfn> and C<limit_mfn> |
66 |
parametars, see also C<$isis->size>. |
67 |
|
68 |
=cut |
69 |
|
70 |
sub open { |
71 |
my $self = shift; |
72 |
my $arg = {@_}; |
73 |
|
74 |
my $log = $self->_get_logger(); |
75 |
|
76 |
$log->logcroak("need filename") if (! $arg->{'filename'}); |
77 |
my $code_page = $arg->{'code_page'} || '852'; |
78 |
|
79 |
$log->logdie("can't find database ",$arg->{'filename'}) unless (glob($arg->{'filename'}.'.*')); |
80 |
|
81 |
# store data in object |
82 |
$self->{'isis_code_page'} = $code_page; |
83 |
foreach my $v (qw/isis_filename start_mfn limit_mfn/) { |
84 |
$self->{$v} = $arg->{$v} if ($arg->{$v}); |
85 |
} |
86 |
|
87 |
# create Text::Iconv object |
88 |
my $cp = Text::Iconv->new($code_page,$self->{'code_page'}); |
89 |
|
90 |
$log->info("reading ISIS database '",$arg->{'filename'},"'"); |
91 |
$log->debug("isis code page: $code_page"); |
92 |
|
93 |
my ($isis_db,$db_size); |
94 |
|
95 |
if ($have_openisis) { |
96 |
$log->debug("using OpenIsis perl bindings"); |
97 |
$isis_db = OpenIsis::open($arg->{'filename'}); |
98 |
$db_size = OpenIsis::maxRowid( $isis_db ) || 1; |
99 |
} elsif ($have_biblio_isis) { |
100 |
$log->debug("using Biblio::Isis"); |
101 |
use Biblio::Isis; |
102 |
$isis_db = new Biblio::Isis( |
103 |
isisdb => $arg->{'filename'}, |
104 |
include_deleted => 1, |
105 |
hash_filter => sub { |
106 |
my $l = shift || return; |
107 |
$l = $cp->convert($l); |
108 |
return $l; |
109 |
}, |
110 |
); |
111 |
$db_size = $isis_db->count; |
112 |
|
113 |
unless ($db_size) { |
114 |
$log->logwarn("no records in database ", $arg->{'filename'}, ", skipping..."); |
115 |
return; |
116 |
} |
117 |
|
118 |
} else { |
119 |
$log->logdie("Can't find supported ISIS library for perl. I suggent that you install Bilbio::Isis from CPAN."); |
120 |
} |
121 |
|
122 |
|
123 |
my $startmfn = 1; |
124 |
my $maxmfn = $db_size; |
125 |
|
126 |
if (my $s = $self->{'start_mfn'}) { |
127 |
$log->info("skipping to MFN $s"); |
128 |
$startmfn = $s; |
129 |
} else { |
130 |
$self->{'start_mfn'} = $startmfn; |
131 |
} |
132 |
|
133 |
if ($self->{limit_mfn}) { |
134 |
$log->info("limiting to ",$self->{limit_mfn}," records"); |
135 |
$maxmfn = $startmfn + $self->{limit_mfn} - 1; |
136 |
$maxmfn = $db_size if ($maxmfn > $db_size); |
137 |
} |
138 |
|
139 |
# store size for later |
140 |
$self->{'size'} = ($maxmfn - $startmfn) ? ($maxmfn - $startmfn + 1) : 0; |
141 |
|
142 |
$log->info("processing ",($maxmfn-$startmfn)." records using ",( $have_openisis ? 'OpenIsis' : 'Biblio::Isis')); |
143 |
|
144 |
|
145 |
# read database |
146 |
for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) { |
147 |
|
148 |
$log->debug("mfn: $mfn\n"); |
149 |
|
150 |
my $rec; |
151 |
|
152 |
if ($have_openisis) { |
153 |
|
154 |
# read record using OpenIsis |
155 |
my $row = OpenIsis::read( $isis_db, $mfn ); |
156 |
foreach my $k (keys %{$row}) { |
157 |
if ($k ne "mfn") { |
158 |
foreach my $l (@{$row->{$k}}) { |
159 |
$l = $cp->convert($l); |
160 |
# has subfields? |
161 |
my $val; |
162 |
if ($l =~ m/\^/) { |
163 |
foreach my $t (split(/\^/,$l)) { |
164 |
next if (! $t); |
165 |
$val->{substr($t,0,1)} = substr($t,1); |
166 |
} |
167 |
} else { |
168 |
$val = $l; |
169 |
} |
170 |
|
171 |
push @{$rec->{$k}}, $val; |
172 |
} |
173 |
} else { |
174 |
push @{$rec->{'000'}}, $mfn; |
175 |
} |
176 |
} |
177 |
|
178 |
} elsif ($have_biblio_isis) { |
179 |
$rec = $isis_db->to_hash($mfn); |
180 |
} else { |
181 |
$log->logdie("hum? implementation missing?"); |
182 |
} |
183 |
|
184 |
$log->confess("record $mfn empty?") unless ($rec); |
185 |
|
186 |
# store |
187 |
if ($self->{'low_mem'}) { |
188 |
$self->{'db'}->put($mfn, $rec); |
189 |
} else { |
190 |
$self->{'data'}->{$mfn} = $rec; |
191 |
} |
192 |
|
193 |
# create lookup |
194 |
$self->{'lookup'}->add( $rec ) if ($self->{'lookup'} && can($self->{'lookup'}->add)); |
195 |
|
196 |
$self->progress_bar($mfn,$maxmfn); |
197 |
|
198 |
} |
199 |
|
200 |
$self->{'current_mfn'} = -1; |
201 |
$self->{'last_pcnt'} = 0; |
202 |
|
203 |
$log->debug("max mfn: $maxmfn"); |
204 |
|
205 |
# store max mfn and return it. |
206 |
$self->{'max_mfn'} = $maxmfn; |
207 |
|
208 |
return $db_size; |
209 |
} |
210 |
|
211 |
=head2 fetch |
212 |
|
213 |
Fetch next record from database. It will also displays progress bar. |
214 |
|
215 |
my $rec = $isis->fetch; |
216 |
|
217 |
Record from this function should probably go to C<data_structure> for |
218 |
normalisation. |
219 |
|
220 |
=cut |
221 |
|
222 |
sub fetch { |
223 |
my $self = shift; |
224 |
|
225 |
my $log = $self->_get_logger(); |
226 |
|
227 |
$log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'}); |
228 |
|
229 |
if ($self->{'current_mfn'} == -1) { |
230 |
$self->{'current_mfn'} = $self->{'start_mfn'}; |
231 |
} else { |
232 |
$self->{'current_mfn'}++; |
233 |
} |
234 |
|
235 |
my $mfn = $self->{'current_mfn'}; |
236 |
|
237 |
if ($mfn > $self->{'max_mfn'}) { |
238 |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
239 |
$log->debug("at EOF"); |
240 |
return; |
241 |
} |
242 |
|
243 |
$self->progress_bar($mfn,$self->{'max_mfn'}); |
244 |
|
245 |
if ($self->{'low_mem'}) { |
246 |
return $self->{'db'}->get($mfn); |
247 |
} else { |
248 |
return $self->{'data'}->{$mfn}; |
249 |
} |
250 |
} |
251 |
|
252 |
=head2 pos |
253 |
|
254 |
Returns current record number (MFN). |
255 |
|
256 |
print $isis->pos; |
257 |
|
258 |
First record in database has position 1. |
259 |
|
260 |
=cut |
261 |
|
262 |
sub pos { |
263 |
my $self = shift; |
264 |
return $self->{'current_mfn'}; |
265 |
} |
266 |
|
267 |
|
268 |
=head2 size |
269 |
|
270 |
Returns number of records in database |
271 |
|
272 |
print $isis->size; |
273 |
|
274 |
Result from this function can be used to loop through all records |
275 |
|
276 |
foreach my $mfn ( 1 ... $isis->size ) { ... } |
277 |
|
278 |
because it takes into account C<start_mfn> and C<limit_mfn>. |
279 |
|
280 |
=cut |
281 |
|
282 |
sub size { |
283 |
my $self = shift; |
284 |
return $self->{'size'}; |
285 |
} |
286 |
|
287 |
=head2 seek |
288 |
|
289 |
Seek to specified MFN in file. |
290 |
|
291 |
$isis->seek(42); |
292 |
|
293 |
First record in database has position 1. |
294 |
|
295 |
=cut |
296 |
|
297 |
sub seek { |
298 |
my $self = shift; |
299 |
my $pos = shift || return; |
300 |
|
301 |
my $log = $self->_get_logger(); |
302 |
|
303 |
if ($pos < 1) { |
304 |
$log->warn("seek before first record"); |
305 |
$pos = 1; |
306 |
} elsif ($pos > $self->{'max_mfn'}) { |
307 |
$log->warn("seek beyond last record"); |
308 |
$pos = $self->{'max_mfn'}; |
309 |
} |
310 |
|
311 |
return $self->{'current_mfn'} = (($pos - 1) || -1); |
312 |
} |
313 |
|
314 |
=head1 AUTHOR |
315 |
|
316 |
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >> |
317 |
|
318 |
=head1 COPYRIGHT & LICENSE |
319 |
|
320 |
Copyright 2005 Dobrica Pavlinusic, All Rights Reserved. |
321 |
|
322 |
This program is free software; you can redistribute it and/or modify it |
323 |
under the same terms as Perl itself. |
324 |
|
325 |
=cut |
326 |
|
327 |
1; # End of WebPAC::Input::ISIS |