/[webpac2]/trunk/lib/WebPAC/Input/ISIS.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Input/ISIS.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (hide annotations)
Sun Jul 17 22:28:11 2005 UTC (18 years, 10 months ago) by dpavlin
File size: 6703 byte(s)
fixed ISIS size

1 dpavlin 6 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 dpavlin 9 use Text::Iconv;
9 dpavlin 6
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 dpavlin 21 Returns size of database, regardless of C<start_mfn> and C<limit_mfn>
66     parametars, see also C<$isis->size>.
67 dpavlin 6
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 dpavlin 21 foreach my $v (qw/isis_filename start_mfn limit_mfn/) {
84     $self->{$v} = $arg->{$v} if ($arg->{$v});
85     }
86 dpavlin 6
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 dpavlin 21 my ($isis_db,$db_size);
94 dpavlin 6
95     if ($have_openisis) {
96     $log->debug("using OpenIsis perl bindings");
97     $isis_db = OpenIsis::open($arg->{'filename'});
98 dpavlin 21 $db_size = OpenIsis::maxRowid( $isis_db ) || 1;
99 dpavlin 6 } 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 dpavlin 21 $db_size = $isis_db->count;
112 dpavlin 6
113 dpavlin 21 unless ($db_size) {
114 dpavlin 6 $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 dpavlin 21 my $maxmfn = $db_size;
125 dpavlin 6
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 dpavlin 21 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 dpavlin 6
139 dpavlin 21 # store size for later
140     $self->{'size'} = ($maxmfn - $startmfn) ? ($maxmfn - $startmfn + 1) : 0;
141    
142 dpavlin 6 $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 dpavlin 21 $self->{'max_mfn'} = $maxmfn;
207    
208     return $db_size;
209 dpavlin 6 }
210    
211 dpavlin 10 =head2 fetch
212 dpavlin 6
213     Fetch next record from database. It will also displays progress bar.
214    
215 dpavlin 10 my $rec = $isis->fetch;
216 dpavlin 6
217 dpavlin 10 Record from this function should probably go to C<data_structure> for
218     normalisation.
219 dpavlin 8
220 dpavlin 6 =cut
221    
222 dpavlin 10 sub fetch {
223 dpavlin 6 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 dpavlin 10 =head2 pos
253 dpavlin 8
254 dpavlin 10 Returns current record number (MFN).
255 dpavlin 8
256 dpavlin 10 print $isis->pos;
257 dpavlin 8
258 dpavlin 10 First record in database has position 1.
259    
260 dpavlin 8 =cut
261    
262 dpavlin 10 sub pos {
263 dpavlin 8 my $self = shift;
264 dpavlin 10 return $self->{'current_mfn'};
265     }
266 dpavlin 8
267 dpavlin 10
268     =head2 size
269    
270     Returns number of records in database
271    
272     print $isis->size;
273    
274 dpavlin 21 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 dpavlin 10 =cut
281    
282     sub size {
283     my $self = shift;
284 dpavlin 21 return $self->{'size'};
285 dpavlin 8 }
286    
287 dpavlin 10 =head2 seek
288 dpavlin 6
289 dpavlin 10 Seek to specified MFN in file.
290 dpavlin 6
291 dpavlin 10 $isis->seek(42);
292 dpavlin 6
293 dpavlin 10 First record in database has position 1.
294    
295 dpavlin 6 =cut
296    
297 dpavlin 10 sub seek {
298 dpavlin 6 my $self = shift;
299 dpavlin 10 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 dpavlin 6 }
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

  ViewVC Help
Powered by ViewVC 1.1.26