3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
use WebPAC::Common; |
require Exporter; |
7 |
use base qw/WebPAC::Input WebPAC::Common/; |
use vars qw/@ISA @EXPORT/; |
8 |
|
@ISA = qw/Exporter/; |
9 |
|
@EXPORT = qw/init open_db fetch_rec/; |
10 |
|
|
11 |
|
use blib; |
12 |
|
use WebPAC::Input; |
13 |
|
#use base qw/WebPAC::Input/; |
14 |
|
|
15 |
=head1 NAME |
=head1 NAME |
16 |
|
|
17 |
WebPAC::Input::ISIS - support for CDS/ISIS source files |
WebPAC::Input::ISIS - support for CDS/ISIS database files |
18 |
|
|
19 |
=head1 VERSION |
=head1 VERSION |
20 |
|
|
21 |
Version 0.01 |
Version 0.03 |
22 |
|
|
23 |
=cut |
=cut |
24 |
|
|
25 |
our $VERSION = '0.01'; |
our $VERSION = '0.03'; |
26 |
|
|
27 |
|
|
|
# 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 ($@); |
|
|
} |
|
|
|
|
28 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
29 |
|
|
30 |
Open CDS/ISIS, WinISIS or IsisMarc database using Biblio::Isis or OpenIsis |
Open CDS/ISIS, WinISIS or IsisMarc database using C<Biblio::Isis> or |
31 |
module and read all records to memory. |
C<OpenIsis> module and read all records to memory. |
32 |
|
|
33 |
my $isis = new WebPAC::Input::ISIS(); |
my $isis = new WebPAC::Input::ISIS(); |
34 |
$isis->open( filename => '/path/to/ISIS/ISIS' ); |
$isis->open( path => '/path/to/ISIS/ISIS' ); |
35 |
|
|
36 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
37 |
|
|
38 |
=head2 open |
=head2 init |
39 |
|
|
40 |
This function will read whole database in memory and produce lookups. |
Autoconfigure this module to use C<Biblio::Isis> or C<OpenIsis>. |
41 |
|
|
42 |
$isis->open( |
=cut |
43 |
filename => '/data/ISIS/ISIS', |
|
44 |
code_page => '852', |
sub init { |
45 |
limit_mfn => 500, |
my $self = shift; |
|
start_mfn => 6000, |
|
|
lookup => $lookup_obj, |
|
|
); |
|
46 |
|
|
47 |
By default, ISIS code page is assumed to be C<852>. |
eval "use Biblio::Isis 0.13;"; |
48 |
|
unless ($@) { |
49 |
|
$self->{have_biblio_isis} = 1 |
50 |
|
} else { |
51 |
|
eval "use OpenIsis;"; |
52 |
|
$self->{have_openisis} = 1 unless ($@); |
53 |
|
} |
54 |
|
} |
55 |
|
|
56 |
If optional parametar C<start_mfn> is set, this will be first MFN to read |
=head2 open_db |
|
from database (so you can skip beginning of your database if you need to). |
|
57 |
|
|
58 |
If optional parametar C<limit_mfn> is set, it will read just 500 records |
Returns handle to database |
|
from database in example above. |
|
59 |
|
|
60 |
Returns number of last record read into memory (size of database, really). |
my $db = $open_db( |
61 |
|
path => '/path/to/LIBRI' |
62 |
|
} |
63 |
|
|
64 |
=cut |
=cut |
65 |
|
|
66 |
sub open { |
sub open_db { |
67 |
my $self = shift; |
my $self = shift; |
68 |
|
|
69 |
my $arg = {@_}; |
my $arg = {@_}; |
70 |
|
|
71 |
my $log = $self->_get_logger(); |
my $log = $self->_get_logger(); |
72 |
|
|
73 |
$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'}.'.*')); |
|
|
|
|
|
# store data in object |
|
|
$self->{'isis_filename'} = $arg->{'filename'}; |
|
|
$self->{'isis_code_page'} = $code_page; |
|
|
|
|
|
#$self->{'isis_code_page'} = $code_page; |
|
|
|
|
|
# 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"); |
|
74 |
|
|
75 |
my ($isis_db,$maxmfn); |
my ($isis_db,$db_size); |
76 |
|
|
77 |
if ($have_openisis) { |
if ($self->{have_openisis}) { |
78 |
$log->debug("using OpenIsis perl bindings"); |
$log->debug("using OpenIsis perl bindings"); |
79 |
$isis_db = OpenIsis::open($arg->{'filename'}); |
$isis_db = OpenIsis::open($arg->{path}); |
80 |
$maxmfn = OpenIsis::maxRowid( $isis_db ) || 1; |
$db_size = OpenIsis::maxRowid( $isis_db ) || 1; |
81 |
} elsif ($have_biblio_isis) { |
} elsif ($self->{have_biblio_isis}) { |
82 |
$log->debug("using Biblio::Isis"); |
$log->debug("using Biblio::Isis"); |
83 |
use Biblio::Isis; |
use Biblio::Isis; |
84 |
$isis_db = new Biblio::Isis( |
$isis_db = new Biblio::Isis( |
85 |
isisdb => $arg->{'filename'}, |
isisdb => $arg->{path}, |
86 |
include_deleted => 1, |
include_deleted => 1, |
87 |
hash_filter => sub { |
hash_filter => sub { |
88 |
my $l = shift || return; |
my $l = shift || return; |
89 |
$l = $cp->convert($l); |
$l = $self->{iconv}->convert($l) if ($self->{iconv}); |
90 |
return $l; |
return $l; |
91 |
}, |
}, |
92 |
); |
) or $log->logdie("can't find database $arg->{path}"); |
|
$maxmfn = $isis_db->count; |
|
93 |
|
|
94 |
unless ($maxmfn) { |
$db_size = $isis_db->count; |
|
$log->logwarn("no records in database ", $arg->{'filename'}, ", skipping..."); |
|
|
return; |
|
|
} |
|
95 |
|
|
96 |
} else { |
} else { |
97 |
$log->logdie("Can't find supported ISIS library for perl. I suggent that you install Bilbio::Isis from CPAN."); |
$log->logdie("Can't find supported ISIS library for perl. I suggent that you install Bilbio::Isis from CPAN."); |
98 |
} |
} |
99 |
|
|
100 |
|
return ($isis_db, $db_size); |
|
my $startmfn = 1; |
|
|
|
|
|
if (my $s = $self->{'start_mfn'}) { |
|
|
$log->info("skipping to MFN $s"); |
|
|
$startmfn = $s; |
|
|
} else { |
|
|
$self->{'start_mfn'} = $startmfn; |
|
|
} |
|
|
|
|
|
$maxmfn = $startmfn + $self->{limit_mfn} if ($self->{limit_mfn}); |
|
|
|
|
|
$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; |
|
|
|
|
|
$log->debug("max mfn: $maxmfn"); |
|
|
|
|
|
# store max mfn and return it. |
|
|
return $self->{'max_mfn'} = $maxmfn; |
|
101 |
} |
} |
102 |
|
|
103 |
=head2 fetch_rec |
=head2 fetch_rec |
104 |
|
|
105 |
Fetch next record from database. It will also displays progress bar. |
Return record with ID C<$mfn> from database |
106 |
|
|
107 |
my $rec = $webpac->fetch_rec; |
my $rec = $self->fetch_rec( $db, $mfn ); |
108 |
|
|
109 |
|
} |
110 |
|
|
111 |
=cut |
=cut |
112 |
|
|
113 |
sub fetch_rec { |
sub fetch_rec { |
114 |
my $self = shift; |
my $self = shift; |
115 |
|
|
116 |
my $log = $self->_get_logger(); |
my ($isis_db, $mfn) = @_; |
117 |
|
|
118 |
$log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'}); |
my $rec; |
119 |
|
|
120 |
if ($self->{'current_mfn'} == -1) { |
if ($self->{have_openisis}) { |
|
$self->{'current_mfn'} = $self->{'start_mfn'}; |
|
|
} else { |
|
|
$self->{'current_mfn'}++; |
|
|
} |
|
|
|
|
|
my $mfn = $self->{'current_mfn'}; |
|
121 |
|
|
122 |
if ($mfn > $self->{'max_mfn'}) { |
# read record using OpenIsis |
123 |
$self->{'current_mfn'} = $self->{'max_mfn'}; |
my $row = OpenIsis::read( $isis_db, $mfn ); |
|
$log->debug("at EOF"); |
|
|
return; |
|
|
} |
|
124 |
|
|
125 |
$self->progress_bar($mfn,$self->{'max_mfn'}); |
# convert record to hash |
126 |
|
foreach my $k (keys %{$row}) { |
127 |
|
if ($k ne "mfn") { |
128 |
|
foreach my $l (@{$row->{$k}}) { |
129 |
|
$l = $self->{iconv}->convert($l) if ($self->{iconv}); |
130 |
|
# has subfields? |
131 |
|
my $val; |
132 |
|
if ($l =~ m/\^/) { |
133 |
|
foreach my $t (split(/\^/,$l)) { |
134 |
|
next if (! $t); |
135 |
|
$val->{substr($t,0,1)} = substr($t,1); |
136 |
|
} |
137 |
|
} else { |
138 |
|
$val = $l; |
139 |
|
} |
140 |
|
push @{$rec->{$k}}, $val; |
141 |
|
} |
142 |
|
} else { |
143 |
|
push @{$rec->{'000'}}, $mfn; |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
if ($self->{'low_mem'}) { |
} elsif ($self->{have_biblio_isis}) { |
148 |
return $self->{'db'}->get($mfn); |
$rec = $isis_db->to_hash($mfn); |
149 |
} else { |
} else { |
150 |
return $self->{'data'}->{$mfn}; |
$self->_get_logger()->logdie("hum? implementation missing?"); |
151 |
} |
} |
|
} |
|
|
|
|
|
=head2 mfn |
|
|
|
|
|
Returns current record number (MFN). |
|
|
|
|
|
print $webpac->mfn; |
|
152 |
|
|
153 |
=cut |
return $rec; |
|
|
|
|
sub mfn { |
|
|
my $self = shift; |
|
|
return $self->{'current_mfn'}; |
|
154 |
} |
} |
155 |
|
|
156 |
=head1 AUTHOR |
=head1 AUTHOR |