/[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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 113 by dpavlin, Wed Nov 23 00:14:05 2005 UTC revision 298 by dpavlin, Mon Dec 19 19:55:21 2005 UTC
# Line 3  package WebPAC::Input::ISIS; Line 3  package WebPAC::Input::ISIS;
3  use warnings;  use warnings;
4  use strict;  use strict;
5    
6  use WebPAC::Common;  use blib;
7  use base qw/WebPAC::Input WebPAC::Common/;  use WebPAC::Input;
 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.03
16    
17  =cut  =cut
18    
19  our $VERSION = '0.01';  our $VERSION = '0.03';
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> or
25  module and read all records to memory.  C<OpenIsis> module 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 init
33    
34  This function will read whole database in memory and produce lookups.  Autoconfigure this module to use C<Biblio::Isis> or C<OpenIsis>.
35    
36   $isis->open(  =cut
         filename => '/data/ISIS/ISIS',  
         code_page => '852',  
         limit_mfn => 500,  
         start_mfn => 6000,  
         lookup => $lookup_obj,  
  );  
37    
38  By default, ISIS code page is assumed to be C<852>.  sub init {
39            my $self = shift;
40    
41  If optional parametar C<start_mfn> is set, this will be first MFN to read          eval "use Biblio::Isis;";
42  from database (so you can skip beginning of your database if you need to).          unless ($@) {
43                    $self->{have_biblio_isis} = 1
44            } else {
45                    eval "use OpenIsis;";
46                    $self->{have_openisis} = 1 unless ($@);
47            }
48    }
49    
50    =head2 open_db
51    
52  If optional parametar C<limit_mfn> is set, it will read just 500 records  Returns handle to database
 from database in example above.  
53    
54  Returns size of database, regardless of C<start_mfn> and C<limit_mfn>    my $db = $open_db(
55  parametars, see also C<$isis->size>.          path => '/path/to/LIBRI'
56      }
57    
58  =cut  =cut
59    
60  sub open {  sub open_db {
61          my $self = shift;          my $self = shift;
62    
63          my $arg = {@_};          my $arg = {@_};
64    
65          my $log = $self->_get_logger();          my $log = $self->_get_logger();
66    
67          $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_code_page'} = $code_page;  
         foreach my $v (qw/isis_filename start_mfn limit_mfn/) {  
                 $self->{$v} = $arg->{$v} if ($arg->{$v});  
         }  
   
         # 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");  
68    
69          my ($isis_db,$db_size);          my ($isis_db,$db_size);
70    
71          if ($have_openisis) {          if ($self->{have_openisis}) {
72                  $log->debug("using OpenIsis perl bindings");                  $log->debug("using OpenIsis perl bindings");
73                  $isis_db = OpenIsis::open($arg->{'filename'});                  $isis_db = OpenIsis::open($arg->{path});
74                  $db_size = OpenIsis::maxRowid( $isis_db ) || 1;                  $db_size = OpenIsis::maxRowid( $isis_db ) || 1;
75          } elsif ($have_biblio_isis) {          } elsif ($self->{have_biblio_isis}) {
76                  $log->debug("using Biblio::Isis");                  $log->debug("using Biblio::Isis");
77                  use Biblio::Isis;                  use Biblio::Isis;
78                  $isis_db = new Biblio::Isis(                  $isis_db = new Biblio::Isis(
79                          isisdb => $arg->{'filename'},                          isisdb => $arg->{path},
80                          include_deleted => 1,                          include_deleted => 1,
81                          hash_filter => sub {                          hash_filter => sub {
82                                  my $l = shift || return;                                  my $l = shift || return;
83                                  $l = $cp->convert($l);                                  $l = $self->{iconv}->convert($l) if ($self->{iconv});
84                                  return $l;                                  return $l;
85                          },                          },
86                  );                  ) or $log->logdie("can't find database $arg->{path}");
                 $db_size = $isis_db->count;  
87    
88                  unless ($db_size) {                  $db_size = $isis_db->count;
                         $log->logwarn("no records in database ", $arg->{'filename'}, ", skipping...");  
                         return;  
                 }  
89    
90          } else {          } else {
91                  $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.");
92          }          }
93    
94            return ($isis_db, $db_size);
         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?");  
                 }  
   
                 if (! $rec) {  
                         $log->warn("record $mfn empty? skipping...");  
                         next;  
                 }  
   
                 # 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.  
         $self->{'max_mfn'} = $maxmfn;  
   
         return $db_size;  
 }  
   
 =head2 fetch  
   
 Fetch next record from database. It will also displays progress bar.  
   
  my $rec = $isis->fetch;  
   
 Record from this function should probably go to C<data_structure> for  
 normalisation.  
   
 =cut  
   
 sub fetch {  
         my $self = shift;  
   
         my $log = $self->_get_logger();  
   
         $log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'});  
   
         if ($self->{'current_mfn'} == -1) {  
                 $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'});  
   
         my $rec;  
   
         if ($self->{'low_mem'}) {  
                 $rec = $self->{'db'}->get($mfn);  
         } else {  
                 $rec = $self->{'data'}->{$mfn};  
         }  
   
         $rec ||= 0E0;  
95  }  }
96    
97  =head2 pos  =head2 fetch_rec
98    
99  Returns current record number (MFN).  Return record with ID C<$mfn> from database
100    
101   print $isis->pos;    my $rec = $self->fetch_rec( $db, $mfn );
102    
 First record in database has position 1.  
   
 =cut  
   
 sub pos {  
         my $self = shift;  
         return $self->{'current_mfn'};  
103  }  }
104    
   
 =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>.  
   
105  =cut  =cut
106    
107  sub size {  sub fetch_rec {
108          my $self = shift;          my $self = shift;
         return $self->{'size'};  
 }  
109    
110  =head2 seek          my ($isis_db, $mfn) = @_;
111    
112  Seek to specified MFN in file.          my $rec;
   
  $isis->seek(42);  
   
 First record in database has position 1.  
113    
114  =cut          if ($self->{have_openisis}) {
115    
116  sub seek {                  # read record using OpenIsis
117          my $self = shift;                  my $row = OpenIsis::read( $isis_db, $mfn );
         my $pos = shift || return;  
118    
119          my $log = $self->_get_logger();                  # convert record to hash
120                    foreach my $k (keys %{$row}) {
121                            if ($k ne "mfn") {
122                                    foreach my $l (@{$row->{$k}}) {
123                                            $l = $self->{iconv}->convert($l) if ($self->{iconv});
124                                            # has subfields?
125                                            my $val;
126                                            if ($l =~ m/\^/) {
127                                                    foreach my $t (split(/\^/,$l)) {
128                                                            next if (! $t);
129                                                            $val->{substr($t,0,1)} = substr($t,1);
130                                                    }
131                                            } else {
132                                                    $val = $l;
133                                            }
134                                            push @{$rec->{$k}}, $val;
135                                    }
136                            } else {
137                                    push @{$rec->{'000'}}, $mfn;
138                            }
139                    }
140    
141          if ($pos < 1) {          } elsif ($self->{have_biblio_isis}) {
142                  $log->warn("seek before first record");                  $rec = $isis_db->to_hash($mfn);
143                  $pos = 1;          } else {
144          } elsif ($pos > $self->{'max_mfn'}) {                  $self->_get_logger()->logdie("hum? implementation missing?");
                 $log->warn("seek beyond last record");  
                 $pos = $self->{'max_mfn'};  
145          }          }
146    
147          return $self->{'current_mfn'} = (($pos - 1) || -1);          return $rec;
148  }  }
149    
150  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.113  
changed lines
  Added in v.298

  ViewVC Help
Powered by ViewVC 1.1.26