3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
use WebPAC::Common; |
use WebPAC::Input; |
7 |
use base qw/WebPAC::Input WebPAC::Common/; |
use Biblio::Isis 0.23; |
|
use Text::Iconv; |
|
8 |
|
|
9 |
=head1 NAME |
=head1 NAME |
10 |
|
|
11 |
WebPAC::Input::ISIS - support for CDS/ISIS source files |
WebPAC::Input::ISIS - support for CDS/ISIS database files |
12 |
|
|
13 |
=head1 VERSION |
=head1 VERSION |
14 |
|
|
15 |
Version 0.01 |
Version 0.07 |
16 |
|
|
17 |
=cut |
=cut |
18 |
|
|
19 |
our $VERSION = '0.01'; |
our $VERSION = '0.07'; |
20 |
|
|
21 |
|
|
|
# auto-configure |
|
|
|
|
|
my ($have_biblio_isis, $have_openisis) = (0,0); |
|
|
|
|
|
eval "use Biblio::Isis 0.13;"; |
|
|
unless ($@) { |
|
|
$have_biblio_isis = 1 |
|
|
} else { |
|
|
eval "use OpenIsis;"; |
|
|
$have_openisis = 1 unless ($@); |
|
|
} |
|
|
|
|
22 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
23 |
|
|
24 |
Open CDS/ISIS, WinISIS or IsisMarc database using Biblio::Isis or OpenIsis |
Open CDS/ISIS, WinISIS or IsisMarc database using C<Biblio::Isis> |
25 |
module and read all records to memory. |
and read all records to memory. |
26 |
|
|
27 |
my $isis = new WebPAC::Input::ISIS(); |
my $isis = new WebPAC::Input::ISIS(); |
28 |
$isis->open( filename => '/path/to/ISIS/ISIS' ); |
$isis->open( path => '/path/to/ISIS/ISIS' ); |
29 |
|
|
30 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
31 |
|
|
32 |
=head2 open |
=head2 open_db |
33 |
|
|
34 |
|
Returns handle to database and size in records |
35 |
|
|
36 |
This function will read whole database in memory and produce lookups. |
my ($db,$size) = $isis->open_db( |
37 |
|
path => '/path/to/LIBRI' |
38 |
|
filter => sub { |
39 |
|
my ($l,$field_nr) = @_; |
40 |
|
# do something with $l which is line of input file |
41 |
|
return $l; |
42 |
|
}, |
43 |
|
} |
44 |
|
|
45 |
$isis->open( |
Options: |
|
filename => '/data/ISIS/ISIS', |
|
|
code_page => '852', |
|
|
limit_mfn => 500, |
|
|
start_mfn => 6000, |
|
|
lookup => $lookup_obj, |
|
|
); |
|
46 |
|
|
47 |
By default, ISIS code page is assumed to be C<852>. |
=over 4 |
48 |
|
|
49 |
If optional parametar C<start_mfn> is set, this will be first MFN to read |
=item path |
|
from database (so you can skip beginning of your database if you need to). |
|
50 |
|
|
51 |
If optional parametar C<limit_mfn> is set, it will read just 500 records |
path to CDS/ISIS database |
|
from database in example above. |
|
52 |
|
|
53 |
Returns size of database, regardless of C<start_mfn> and C<limit_mfn> |
=back |
|
parametars, see also C<$isis->size>. |
|
54 |
|
|
55 |
=cut |
=cut |
56 |
|
|
57 |
sub open { |
sub open_db { |
58 |
my $self = shift; |
my $self = shift; |
59 |
|
|
60 |
my $arg = {@_}; |
my $arg = {@_}; |
61 |
|
|
62 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
63 |
|
|
64 |
$log->logcroak("need filename") if (! $arg->{'filename'}); |
$log->info("opening ISIS database '$arg->{path}'"); |
|
my $code_page = $arg->{'code_page'} || '852'; |
|
|
|
|
|
$log->logdie("can't find database ",$arg->{'filename'}) unless (glob($arg->{'filename'}.'.*')); |
|
65 |
|
|
66 |
# store data in object |
$log->debug("using Biblio::Isis"); |
67 |
$self->{'isis_code_page'} = $code_page; |
my $isis_db = new Biblio::Isis( |
68 |
foreach my $v (qw/isis_filename start_mfn limit_mfn/) { |
isisdb => $arg->{path}, |
69 |
$self->{$v} = $arg->{$v} if ($arg->{$v}); |
include_deleted => 1, |
70 |
} |
hash_filter => $arg->{filter} ? sub { return $arg->{filter}->(@_); } : undef, |
71 |
|
) or $log->logdie("can't find database $arg->{path}"); |
|
# create Text::Iconv object |
|
|
my $cp = Text::Iconv->new($code_page,$self->{'code_page'}); |
|
|
|
|
|
$log->info("reading ISIS database '",$arg->{'filename'},"'"); |
|
|
$log->debug("isis code page: $code_page"); |
|
|
|
|
|
my ($isis_db,$db_size); |
|
|
|
|
|
if ($have_openisis) { |
|
|
$log->debug("using OpenIsis perl bindings"); |
|
|
$isis_db = OpenIsis::open($arg->{'filename'}); |
|
|
$db_size = OpenIsis::maxRowid( $isis_db ) || 1; |
|
|
} elsif ($have_biblio_isis) { |
|
|
$log->debug("using Biblio::Isis"); |
|
|
use Biblio::Isis; |
|
|
$isis_db = new Biblio::Isis( |
|
|
isisdb => $arg->{'filename'}, |
|
|
include_deleted => 1, |
|
|
hash_filter => sub { |
|
|
my $l = shift || return; |
|
|
$l = $cp->convert($l); |
|
|
return $l; |
|
|
}, |
|
|
); |
|
|
$db_size = $isis_db->count; |
|
|
|
|
|
unless ($db_size) { |
|
|
$log->logwarn("no records in database ", $arg->{'filename'}, ", skipping..."); |
|
|
return; |
|
|
} |
|
|
|
|
|
} else { |
|
|
$log->logdie("Can't find supported ISIS library for perl. I suggent that you install Bilbio::Isis from CPAN."); |
|
|
} |
|
|
|
|
|
|
|
|
my $startmfn = 1; |
|
|
my $maxmfn = $db_size; |
|
|
|
|
|
if (my $s = $self->{'start_mfn'}) { |
|
|
$log->info("skipping to MFN $s"); |
|
|
$startmfn = $s; |
|
|
} else { |
|
|
$self->{'start_mfn'} = $startmfn; |
|
|
} |
|
|
|
|
|
if ($self->{limit_mfn}) { |
|
|
$log->info("limiting to ",$self->{limit_mfn}," records"); |
|
|
$maxmfn = $startmfn + $self->{limit_mfn} - 1; |
|
|
$maxmfn = $db_size if ($maxmfn > $db_size); |
|
|
} |
|
|
|
|
|
# store size for later |
|
|
$self->{'size'} = ($maxmfn - $startmfn) ? ($maxmfn - $startmfn + 1) : 0; |
|
|
|
|
|
$log->info("processing ",($maxmfn-$startmfn)." records using ",( $have_openisis ? 'OpenIsis' : 'Biblio::Isis')); |
|
|
|
|
|
|
|
|
# read database |
|
|
for (my $mfn = $startmfn; $mfn <= $maxmfn; $mfn++) { |
|
|
|
|
|
$log->debug("mfn: $mfn\n"); |
|
|
|
|
|
my $rec; |
|
|
|
|
|
if ($have_openisis) { |
|
|
|
|
|
# read record using OpenIsis |
|
|
my $row = OpenIsis::read( $isis_db, $mfn ); |
|
|
foreach my $k (keys %{$row}) { |
|
|
if ($k ne "mfn") { |
|
|
foreach my $l (@{$row->{$k}}) { |
|
|
$l = $cp->convert($l); |
|
|
# has subfields? |
|
|
my $val; |
|
|
if ($l =~ m/\^/) { |
|
|
foreach my $t (split(/\^/,$l)) { |
|
|
next if (! $t); |
|
|
$val->{substr($t,0,1)} = substr($t,1); |
|
|
} |
|
|
} else { |
|
|
$val = $l; |
|
|
} |
|
|
|
|
|
push @{$rec->{$k}}, $val; |
|
|
} |
|
|
} else { |
|
|
push @{$rec->{'000'}}, $mfn; |
|
|
} |
|
|
} |
|
|
|
|
|
} elsif ($have_biblio_isis) { |
|
|
$rec = $isis_db->to_hash($mfn); |
|
|
} else { |
|
|
$log->logdie("hum? implementation missing?"); |
|
|
} |
|
|
|
|
|
$log->confess("record $mfn empty?") unless ($rec); |
|
|
|
|
|
# store |
|
|
if ($self->{'low_mem'}) { |
|
|
$self->{'db'}->put($mfn, $rec); |
|
|
} else { |
|
|
$self->{'data'}->{$mfn} = $rec; |
|
|
} |
|
|
|
|
|
# create lookup |
|
|
$self->{'lookup'}->add( $rec ) if ($self->{'lookup'} && can($self->{'lookup'}->add)); |
|
|
|
|
|
$self->progress_bar($mfn,$maxmfn); |
|
|
|
|
|
} |
|
|
|
|
|
$self->{'current_mfn'} = -1; |
|
|
$self->{'last_pcnt'} = 0; |
|
72 |
|
|
73 |
$log->debug("max mfn: $maxmfn"); |
my $size = $isis_db->count; |
74 |
|
|
75 |
# store max mfn and return it. |
$self->{_isis_db} = $isis_db; |
|
$self->{'max_mfn'} = $maxmfn; |
|
76 |
|
|
77 |
return $db_size; |
return ($isis_db, $size); |
78 |
} |
} |
79 |
|
|
80 |
=head2 fetch |
=head2 fetch_rec |
81 |
|
|
82 |
Fetch next record from database. It will also displays progress bar. |
Return record with ID C<$mfn> from database |
83 |
|
|
84 |
my $rec = $isis->fetch; |
my $rec = $self->fetch_rec( $mfn, $filter_coderef); |
|
|
|
|
Record from this function should probably go to C<data_structure> for |
|
|
normalisation. |
|
85 |
|
|
86 |
=cut |
=cut |
87 |
|
|
88 |
sub fetch { |
sub fetch_rec { |
89 |
my $self = shift; |
my $self = shift; |
90 |
|
|
91 |
my $log = $self->_get_logger(); |
my ($mfn, $filter_coderef) = @_; |
92 |
|
|
93 |
$log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'}); |
my $rec = $self->{_isis_db}->to_hash({ |
94 |
|
mfn => $mfn, |
95 |
|
include_subfields => 1, |
96 |
|
hash_filter => $filter_coderef, |
97 |
|
# hash_filter => sub { |
98 |
|
# my ($l,$f) = @_; |
99 |
|
# warn "## in hash_filter ($l,$f)\n"; |
100 |
|
# my $o = $filter_coderef->($l,$f) if ($filter_coderef); |
101 |
|
# warn "## out hash_filter = $o\n"; |
102 |
|
# return $o; |
103 |
|
# }, |
104 |
|
}); |
105 |
|
|
106 |
if ($self->{'current_mfn'} == -1) { |
return $rec; |
|
$self->{'current_mfn'} = $self->{'start_mfn'}; |
|
|
} else { |
|
|
$self->{'current_mfn'}++; |
|
|
} |
|
|
|
|
|
my $mfn = $self->{'current_mfn'}; |
|
|
|
|
|
if ($mfn > $self->{'max_mfn'}) { |
|
|
$self->{'current_mfn'} = $self->{'max_mfn'}; |
|
|
$log->debug("at EOF"); |
|
|
return; |
|
|
} |
|
|
|
|
|
$self->progress_bar($mfn,$self->{'max_mfn'}); |
|
|
|
|
|
if ($self->{'low_mem'}) { |
|
|
return $self->{'db'}->get($mfn); |
|
|
} else { |
|
|
return $self->{'data'}->{$mfn}; |
|
|
} |
|
107 |
} |
} |
108 |
|
|
109 |
=head2 pos |
=head2 dump_rec |
110 |
|
|
111 |
Returns current record number (MFN). |
Return dump of record ID C<$mfn> from database |
112 |
|
|
113 |
print $isis->pos; |
my $rec = $self->dump_rec( $db, $mfn ); |
|
|
|
|
First record in database has position 1. |
|
114 |
|
|
115 |
=cut |
=cut |
116 |
|
|
117 |
sub pos { |
sub dump_rec { |
118 |
my $self = shift; |
my $self = shift; |
|
return $self->{'current_mfn'}; |
|
|
} |
|
|
|
|
|
|
|
|
=head2 size |
|
|
|
|
|
Returns number of records in database |
|
|
|
|
|
print $isis->size; |
|
|
|
|
|
Result from this function can be used to loop through all records |
|
|
|
|
|
foreach my $mfn ( 1 ... $isis->size ) { ... } |
|
|
|
|
|
because it takes into account C<start_mfn> and C<limit_mfn>. |
|
|
|
|
|
=cut |
|
|
|
|
|
sub size { |
|
|
my $self = shift; |
|
|
return $self->{'size'}; |
|
|
} |
|
|
|
|
|
=head2 seek |
|
|
|
|
|
Seek to specified MFN in file. |
|
|
|
|
|
$isis->seek(42); |
|
|
|
|
|
First record in database has position 1. |
|
|
|
|
|
=cut |
|
|
|
|
|
sub seek { |
|
|
my $self = shift; |
|
|
my $pos = shift || return; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
119 |
|
|
120 |
if ($pos < 1) { |
my $mfn = shift; |
|
$log->warn("seek before first record"); |
|
|
$pos = 1; |
|
|
} elsif ($pos > $self->{'max_mfn'}) { |
|
|
$log->warn("seek beyond last record"); |
|
|
$pos = $self->{'max_mfn'}; |
|
|
} |
|
121 |
|
|
122 |
return $self->{'current_mfn'} = (($pos - 1) || -1); |
return $self->{_isis_db}->to_ascii( $mfn ); |
123 |
} |
} |
124 |
|
|
125 |
=head1 AUTHOR |
=head1 AUTHOR |