1 package WebPAC::Input::ISIS;
7 use base qw/WebPAC::Input WebPAC::Common/;
12 WebPAC::Input::ISIS - support for CDS/ISIS source files
20 our $VERSION = '0.01';
25 my ($have_biblio_isis, $have_openisis) = (0,0);
27 eval "use Biblio::Isis 0.13;";
32 $have_openisis = 1 unless ($@);
37 Open CDS/ISIS, WinISIS or IsisMarc database using Biblio::Isis or OpenIsis
38 module and read all records to memory.
40 my $isis = new WebPAC::Input::ISIS();
41 $isis->open( filename => '/path/to/ISIS/ISIS' );
47 This function will read whole database in memory and produce lookups.
50 filename => '/data/ISIS/ISIS',
54 lookup => $lookup_obj,
57 By default, ISIS code page is assumed to be C<852>.
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).
62 If optional parametar C<limit_mfn> is set, it will read just 500 records
63 from database in example above.
65 Returns size of database, regardless of C<start_mfn> and C<limit_mfn>
66 parametars, see also C<$isis->size>.
74 my $log = $self->_get_logger();
76 $log->logcroak("need filename") if (! $arg->{'filename'});
77 my $code_page = $arg->{'code_page'} || '852';
79 $log->logdie("can't find database ",$arg->{'filename'}) unless (glob($arg->{'filename'}.'.*'));
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});
87 # create Text::Iconv object
88 my $cp = Text::Iconv->new($code_page,$self->{'code_page'});
90 $log->info("reading ISIS database '",$arg->{'filename'},"'");
91 $log->debug("isis code page: $code_page");
93 my ($isis_db,$db_size);
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");
102 $isis_db = new Biblio::Isis(
103 isisdb => $arg->{'filename'},
104 include_deleted => 1,
106 my $l = shift || return;
107 $l = $cp->convert($l);
111 $db_size = $isis_db->count;
114 $log->logwarn("no records in database ", $arg->{'filename'}, ", skipping...");
119 $log->logdie("Can't find supported ISIS library for perl. I suggent that you install Bilbio::Isis from CPAN.");
124 my $maxmfn = $db_size;
126 if (my $s = $self->{'start_mfn'}) {
127 $log->info("skipping to MFN $s");
130 $self->{'start_mfn'} = $startmfn;
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);
139 # store size for later
140 $self->{'size'} = ($maxmfn - $startmfn) ? ($maxmfn - $startmfn + 1) : 0;
142 $log->info("processing ",($maxmfn-$startmfn)." records using ",( $have_openisis ? 'OpenIsis' : 'Biblio::Isis'));
146 for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) {
148 $log->debug("mfn: $mfn\n");
152 if ($have_openisis) {
154 # read record using OpenIsis
155 my $row = OpenIsis::read( $isis_db, $mfn );
156 foreach my $k (keys %{$row}) {
158 foreach my $l (@{$row->{$k}}) {
159 $l = $cp->convert($l);
163 foreach my $t (split(/\^/,$l)) {
165 $val->{substr($t,0,1)} = substr($t,1);
171 push @{$rec->{$k}}, $val;
174 push @{$rec->{'000'}}, $mfn;
178 } elsif ($have_biblio_isis) {
179 $rec = $isis_db->to_hash($mfn);
181 $log->logdie("hum? implementation missing?");
185 $log->warn("record $mfn empty? skipping...");
190 if ($self->{'low_mem'}) {
191 $self->{'db'}->put($mfn, $rec);
193 $self->{'data'}->{$mfn} = $rec;
197 $self->{'lookup'}->add( $rec ) if ($self->{'lookup'} && can($self->{'lookup'}->add));
199 $self->progress_bar($mfn,$maxmfn);
203 $self->{'current_mfn'} = -1;
204 $self->{'last_pcnt'} = 0;
206 $log->debug("max mfn: $maxmfn");
208 # store max mfn and return it.
209 $self->{'max_mfn'} = $maxmfn;
216 Fetch next record from database. It will also displays progress bar.
218 my $rec = $isis->fetch;
220 Record from this function should probably go to C<data_structure> for
228 my $log = $self->_get_logger();
230 $log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'});
232 if ($self->{'current_mfn'} == -1) {
233 $self->{'current_mfn'} = $self->{'start_mfn'};
235 $self->{'current_mfn'}++;
238 my $mfn = $self->{'current_mfn'};
240 if ($mfn > $self->{'max_mfn'}) {
241 $self->{'current_mfn'} = $self->{'max_mfn'};
242 $log->debug("at EOF");
246 $self->progress_bar($mfn,$self->{'max_mfn'});
250 if ($self->{'low_mem'}) {
251 $rec = $self->{'db'}->get($mfn);
253 $rec = $self->{'data'}->{$mfn};
261 Returns current record number (MFN).
265 First record in database has position 1.
271 return $self->{'current_mfn'};
277 Returns number of records in database
281 Result from this function can be used to loop through all records
283 foreach my $mfn ( 1 ... $isis->size ) { ... }
285 because it takes into account C<start_mfn> and C<limit_mfn>.
291 return $self->{'size'};
296 Seek to specified MFN in file.
300 First record in database has position 1.
306 my $pos = shift || return;
308 my $log = $self->_get_logger();
311 $log->warn("seek before first record");
313 } elsif ($pos > $self->{'max_mfn'}) {
314 $log->warn("seek beyond last record");
315 $pos = $self->{'max_mfn'};
318 return $self->{'current_mfn'} = (($pos - 1) || -1);
323 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
325 =head1 COPYRIGHT & LICENSE
327 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
329 This program is free software; you can redistribute it and/or modify it
330 under the same terms as Perl itself.
334 1; # End of WebPAC::Input::ISIS