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?");
184 $log->confess("record $mfn empty?") unless ($rec);
187 if ($self->{'low_mem'}) {
188 $self->{'db'}->put($mfn, $rec);
190 $self->{'data'}->{$mfn} = $rec;
194 $self->{'lookup'}->add( $rec ) if ($self->{'lookup'} && can($self->{'lookup'}->add));
196 $self->progress_bar($mfn,$maxmfn);
200 $self->{'current_mfn'} = -1;
201 $self->{'last_pcnt'} = 0;
203 $log->debug("max mfn: $maxmfn");
205 # store max mfn and return it.
206 $self->{'max_mfn'} = $maxmfn;
213 Fetch next record from database. It will also displays progress bar.
215 my $rec = $isis->fetch;
217 Record from this function should probably go to C<data_structure> for
225 my $log = $self->_get_logger();
227 $log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'});
229 if ($self->{'current_mfn'} == -1) {
230 $self->{'current_mfn'} = $self->{'start_mfn'};
232 $self->{'current_mfn'}++;
235 my $mfn = $self->{'current_mfn'};
237 if ($mfn > $self->{'max_mfn'}) {
238 $self->{'current_mfn'} = $self->{'max_mfn'};
239 $log->debug("at EOF");
243 $self->progress_bar($mfn,$self->{'max_mfn'});
245 if ($self->{'low_mem'}) {
246 return $self->{'db'}->get($mfn);
248 return $self->{'data'}->{$mfn};
254 Returns current record number (MFN).
258 First record in database has position 1.
264 return $self->{'current_mfn'};
270 Returns number of records in database
274 Result from this function can be used to loop through all records
276 foreach my $mfn ( 1 ... $isis->size ) { ... }
278 because it takes into account C<start_mfn> and C<limit_mfn>.
284 return $self->{'size'};
289 Seek to specified MFN in file.
293 First record in database has position 1.
299 my $pos = shift || return;
301 my $log = $self->_get_logger();
304 $log->warn("seek before first record");
306 } elsif ($pos > $self->{'max_mfn'}) {
307 $log->warn("seek beyond last record");
308 $pos = $self->{'max_mfn'};
311 return $self->{'current_mfn'} = (($pos - 1) || -1);
316 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
318 =head1 COPYRIGHT & LICENSE
320 Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
322 This program is free software; you can redistribute it and/or modify it
323 under the same terms as Perl itself.
327 1; # End of WebPAC::Input::ISIS