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

Legend:
Removed from v.21  
changed lines
  Added in v.615

  ViewVC Help
Powered by ViewVC 1.1.26