/[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 8 by dpavlin, Sat Jul 16 16:48:35 2005 UTC revision 774 by dpavlin, Fri Nov 3 20:56:21 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;
7  use base qw/WebPAC::Input WebPAC::Common/;  use Biblio::Isis 0.23;
8    use base qw/WebPAC::Common/;
9    
10  =head1 NAME  =head1 NAME
11    
12  WebPAC::Input::ISIS - support for CDS/ISIS source files  WebPAC::Input::ISIS - support for CDS/ISIS database files
13    
14  =head1 VERSION  =head1 VERSION
15    
16  Version 0.01  Version 0.09
17    
18  =cut  =cut
19    
20  our $VERSION = '0.01';  our $VERSION = '0.09';
21    
22    
 # 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 ($@);  
 }  
   
23  =head1 SYNOPSIS  =head1 SYNOPSIS
24    
25  Open CDS/ISIS, WinISIS or IsisMarc database using Biblio::Isis or OpenIsis  Open CDS/ISIS, WinISIS or IsisMarc database using C<Biblio::Isis>
26  module and read all records to memory.  and read all records to memory.
27    
28   my $isis = new WebPAC::Input::ISIS();   my $isis = new WebPAC::Input::ISIS(
29   $isis->open( filename => '/path/to/ISIS/ISIS' );          path => '/path/to/ISIS/ISIS',
30     );
31    
32  =head1 FUNCTIONS  =head1 FUNCTIONS
33    
34  =head2 open  =head2 new
35    
36  This function will read whole database in memory and produce lookups.  Returns new low-level input API object
37    
38   $isis->open(    my $isis = new WebPAC::Input::ISIS(
39          filename => '/data/ISIS/ISIS',          path => '/path/to/LIBRI'
40          code_page => '852',          filter => sub {
41          limit_mfn => 500,                  my ($l,$field_nr) = @_;
42          start_mfn => 6000,                  # do something with $l which is line of input file
43          lookup => $lookup_obj,                  return $l;
44   );          },
45      }
46    
47    Options:
48    
49  By default, ISIS code page is assumed to be C<852>.  =over 4
50    
51  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).  
52    
53  If optional parametar C<limit_mfn> is set, it will read just 500 records  path to CDS/ISIS database
 from database in example above.  
54    
55  Returns number of last record read into memory (size of database, really).  =back
56    
57  =cut  =cut
58    
59  sub open {  sub new {
60          my $self = shift;          my $class = shift;
61            my $self = {@_};
62            bless($self, $class);
63    
64          my $arg = {@_};          my $arg = {@_};
65    
66          my $log = $self->_get_logger();          my $log = $self->_get_logger();
67    
68          $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");  
   
         my ($isis_db,$maxmfn);  
   
         if ($have_openisis) {  
                 $log->debug("using OpenIsis perl bindings");  
                 $isis_db = OpenIsis::open($arg->{'filename'});  
                 $maxmfn = 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;  
                         },  
                 );  
                 $maxmfn = $isis_db->count;  
   
                 unless ($maxmfn) {  
                         $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;  
   
         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);  
   
         }  
69    
70          $self->{'current_mfn'} = -1;          $log->debug("using Biblio::Isis");
71          $self->{'last_pcnt'} = 0;          my $isis_db = new Biblio::Isis(
72                    isisdb => $arg->{path},
73                    include_deleted => 1,
74                    hash_filter => $arg->{filter} ? sub { return $arg->{filter}->(@_); } : undef,
75            ) or $log->logdie("can't find database $arg->{path}");
76    
77          $log->debug("max mfn: $maxmfn");          $self->{_isis_db} = $isis_db;
78    
79          # store max mfn and return it.          $self ? return $self : return undef;
         return $self->{'max_mfn'} = $maxmfn;  
80  }  }
81    
82  =head2 fetch_rec  =head2 fetch_rec
83    
84  Fetch next record from database. It will also displays progress bar.  Return record with ID C<$mfn> from database
85    
86   my $rec = $webpac->fetch_rec;    my $rec = $isis->fetch_rec( $mfn, $filter_coderef);
   
 You should rearly have the need to call this function directly. Instead use  
 C<fetch_data_structure> which returns normalised data.  
87    
88  =cut  =cut
89    
90  sub fetch_rec {  sub fetch_rec {
91          my $self = shift;          my $self = shift;
92    
93          my $log = $self->_get_logger();          my ($mfn, $filter_coderef) = @_;
94    
95          $log->logconfess("it seems that you didn't load database!") unless ($self->{'current_mfn'});          my $rec = $self->{_isis_db}->to_hash({
96                    mfn => $mfn,
97                    include_subfields => 1,
98                    hash_filter => $filter_coderef,
99    #               hash_filter => sub {
100    #                       my ($l,$f) = @_;
101    #                       warn "## in hash_filter ($l,$f)\n";
102    #                       my $o = $filter_coderef->($l,$f) if ($filter_coderef);
103    #                       warn "## out hash_filter = $o\n";
104    #                       return $o;
105    #               },
106            });
107    
108          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};  
         }  
109  }  }
110    
111  =head2 fetch_data_structure  =head2 dump_ascii
112    
113  Fetch data structure of next record from database.  Return dump of record ID C<$mfn> from database
114    
115   my @ds = $webpac->fetch_data_structure;    my $rec = $isis->dump_ascii( $mfn );
116    
117  =cut  =cut
118    
119  sub fetch_data_structure {  sub dump_ascii {
120          my $self = shift;          my $self = shift;
121    
122          return $self->data_structure(          my $mfn = shift;
123                  $self->fetch_rec(@_)  
124          );          return $self->{_isis_db}->to_ascii( $mfn );
125  }  }
126    
127  =head2 mfn  =head2 size
128    
129  Returns current record number (MFN).  Return number of records in database
130    
131   print $webpac->mfn;    my $size = $isis->size;
132    
133  =cut  =cut
134    
135  sub mfn {  sub size {
136          my $self = shift;          my $self = shift;
137          return $self->{'current_mfn'};          return $self->{_isis_db}->count;
138  }  }
139    
140  =head1 AUTHOR  =head1 AUTHOR

Legend:
Removed from v.8  
changed lines
  Added in v.774

  ViewVC Help
Powered by ViewVC 1.1.26