/[webpac]/trunk2/all2all.pl
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 /trunk2/all2all.pl

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

trunk/all2xml.pl revision 9 by dpavlin, Sat Jan 11 19:55:30 2003 UTC trunk2/all2all.pl revision 407 by dpavlin, Sun Sep 5 17:54:51 2004 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  =head1 NAME
 use OpenIsis;  
 use Getopt::Std;  
 use Data::Dumper;  
 use XML::Simple;  
 use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,  
 require Unicode::Map8;  
 use DBI;  
   
 my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);  
 my $dbh = DBI->connect("DBI:Pg:dbname=webpac","","") || die $DBI::errstr; # FIX  
 # FIX; select relname from pg_class where relname like 'index_%' ;  
 $dbh->begin_work || die $dbh->errstr();  
   
 $dbh->do("delete from index_author") || die $dbh->errstr();  
 $dbh->do("delete from index_title") || die $dbh->errstr();  
   
 my %opts;  
   
 # usage:  
 #       -d directory name  
 #       -m multiple directories  
 #       -q quiet  
 #       -s run swish  
   
 getopts('d:m:qs', \%opts);  
   
 my $db_dir = $opts{d} || "ps";  # FIX  
   
 #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  
   
 #print Dumper($config->{indexer});  
 #print "-" x 70,"\n";  
   
 # how to convert isis code page to UTF8?  
 my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;  
   
 sub isis2xml {  
   
         my $row = shift @_;  
   
         my $xml;  
         $xml->{db_dir} = [ $db_dir ];   # FIX remove?  
   
         sub isis_sf {  
                 my $row = shift @_;  
                 my $isis_id = shift @_;  
                 my $subfield = shift @_;  
                 if ($row->{$isis_id}->[0]) {  
                         my $sf = OpenIsis::subfields($row->{$isis_id}->[0]);  
                         if (! defined $subfield || length($subfield) == 0) {  
                                 # subfield list undef, empty or no defined subfields for this record  
                                 my $all_sf = $row->{$isis_id}->[0];  
                                 $all_sf =~ s/\^./ /g;   nuke definirions  
                                 return $all_sf;  
                         } elsif ($sf->{$subfield}) {  
                                 return $sf->{$subfield};  
                         }  
                 }  
         }  
4    
5          foreach my $field (keys %{$config->{indexer}}) {  all2all.pl - basic script for all WebPAC needs
6    
7                  my $display_data = "";  =cut
                 my $swish_data = "";  
                 my $index_data = "";  
   
                 foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {  
   
                         my $display_tmp = "";  
                         my $swish_tmp = "";  
                         my $index_tmp = "";  
   
                         my $format = $x->{content};  
                         my $s = 1;      # swish only  
                         my $d = 1;      # display only  
                         my $i = 0;      # index only  
                         $s = 0 if (lc($x->{type}) eq "display");  
                         $d = 0 if (lc($x->{type}) eq "swish");  
                         ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");  
 #print STDERR "## s: $s d: $d i: $i ## $format ##\n";    
                         # parse format  
                         my $prefix = "";  
                         if ($format =~ s/^([^\d]+)//) {  
                                 $prefix = $1;  
                         }  
                         while ($format) {  
                                 if ($format =~ s/^(\d\d\d)(\w?)//) {  
                                         my $isis_tmp = isis_sf($row,$1,$2);  
                                         if ($isis_tmp) {  
 #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);  
                                                 $display_tmp .= $prefix . $isis_tmp if ($d);  
                                                 $swish_tmp .= $isis_tmp." " if ($s);  
                                                 $index_tmp .= $prefix . $isis_tmp if ($i);  
 #print STDERR " $isis_tmp <--\n";  
                                         }  
                                         $prefix = "";  
                                 } elsif ($format =~ s/^([^\d]+)//) {  
                                         $prefix = $1;  
                                 } else {  
                                         print STDERR "WARNING: unparsed format '$format'\n";  
                                         last;  
                                 };  
                         }  
                         # add suffix  
                         $display_tmp .= $prefix if ($display_tmp);  
                         $index_tmp .= $prefix if ($index_tmp);  
   
 #                       $display_data .= $display_tmp if ($display_tmp ne "");  
 #                       $swish_data .= $swish_tmp if ($swish_tmp ne "");  
                         $display_data .= $display_tmp;  
                         $swish_data .= $swish_tmp;  
                         $index_data .= $index_tmp;  
   
                 }  
 #print "--display:$display_data\n--swish:$swish_data\n";  
                 #$xml->{$field."_display"} = $isis_map->tou($display_data)->utf8 if ($display_data);  
                 #$xml->{$field."_swish"} = unac_string($config->{isis_codepage},$swish_data) if ($swish_data);  
                 $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data);  
                 $xml->{$field."_swish"} = [ unac_string($config->{isis_codepage},$swish_data) ] if ($swish_data);  
   
                 # index  
                 if ($index_data && $index_data ne "") {  
                         my $sql = "select $field from index_$field where upper($field)=upper(?)";  
                         my $sth = $dbh->prepare($sql) || die $dbh->errstr();  
                         $sth->execute($index_data) || die "SQL: $sql; ".$dbh->errstr();  
 #print STDERR "--->$index_data<---\n";  
                         if (! $sth->fetchrow_hashref) {  
                                 my $sql = "insert into index_$field values (?)";  
                                 my $sth = $dbh->prepare($sql) || die $dbh->errstr();  
 #print STDERR "$sql: $index_data<!----\n";  
                                 $sth->execute($index_data) || die "SQL: $sql; ".$dbh->errstr();  
                         }  
                 }  
8    
9          }  use strict;
10          if ($xml) {  use Data::Dumper;
11                  return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );  use Carp;
         } else {  
                 return;  
         }  
 }  
12    
13  ##########################################################################  use lib './lib';
14    use WebPAC;
15    use WebPAC::jsFind;
16    
17  my $last_tell=0;  my $webpac = new WebPAC(
18            code_page => 'ISO-8859-2',
19    #       limit_mfn => 100,
20    #       debug => 1,
21    ) || die;
22    
23  my @isis_dirs = ( '.' );        # use dirname as database name  my $log = $webpac->_get_logger();
24    
25  if ($opts{m}) {  my $index = new WebPAC::jsFind(
26          @isis_dirs = split(/,/,$opts{m});          index_path => './out/index',
27  }          keys => 80,
28    ) || die;
29    
30  my @isis_dbs;  $|=1;
31    
32  foreach (@isis_dirs) {  my $maxmfn = $webpac->open_isis(
33          if (-e $config->{isis_data}."/$db_dir/$_/LIBRI") {          filename => shift @ARGV || '/data/hidra/THS/THS',
34                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/LIBRI/LIBRI";          lookup => [
35          }          { 'key' => 'd:v900', 'val' => 'v250^a' },
36          if (-e $config->{isis_data}."/$db_dir/$_/PERI") {  #       { 'eval' => '"v901^a" eq "Područje"', 'key' => 'pa:v561^4:v562^4:v461^1', 'val' => 'v900' },
37                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/PERI/PERI";  #       { 'eval '=> '"v901^a" eq "Mikrotezaurus"', 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
38          }  #       { 'eval' => '"v901^a" eq "Deskriptor"', 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
39          if (-e $config->{isis_data}."/$db_dir/$_/AMS") {          { 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
40                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";          { 'key' => '900_mfn:v900', 'val' => 'v000' },
41          }          ],
42          if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {  );
 #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";  
         }  
 }  
43    
44  print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);  $log->info("rows: $maxmfn");
45    
46  my $db;  $webpac->open_import_xml(type => 'isis_hidra_ths');
47    
48  foreach my $isis_db (@isis_dbs) {  while (my $rec = $webpac->fetch_rec) {
49    
50            my @ds = $webpac->data_structure($rec);
51    
52          my $db = OpenIsis::open( $isis_db );          if ($log->is_debug) {
53          if (0) {                  $log->debug("rec = ",Dumper($rec));
54  #       # FIX                  $log->debug("ds = ",Dumper(\@ds));
 #       if (! $db ) {  
                 print STDERR "WARNING: can't open '$isis_db'\n";  
                 next ;  
55          }          }
56    
57          my $max_rowid = OpenIsis::maxRowid( $db );          next if (! @ds);
58    
59          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          my $filename = $webpac->{'current_filename'};
60    
61          my $last_p = 0;          if ($filename) {
62                    open(OUT,"> $filename") || $log->logdie("can't open output '$filename': $!");
63  #       { my $row_id = 1;                  print OUT $webpac->output(
64  # FIX                          template => 'html.tt',
65          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                          data => \@ds,
66                  my $row = OpenIsis::read( $db, $row_id );                          headline => $webpac->{'headline'},
67                  if ($row && $row->{mfn}) {                  );
68                    close(OUT);
69                          # output current process indicator          } else {
70                          my $p = int($row->{mfn} * 100 / $max_rowid);                  print $webpac->output(
71                          if ($p != $last_p) {                          template => 'text.tt',
72                                  printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});                          data => \@ds,
73                                  $last_p = $p;                          headline => $webpac->{'headline'},
74                          }                  );
   
                         if (my $xml = isis2xml($row)) {  
                                 my $path = $isis_db;  
                                 $path =~ s#$config->{isis_data}/*##g;  
                                 my $out = "Path-Name: $path#".$row->{mfn}."\n";  
                                 $out .= "Content-Length: ".(length($xml)+1)."\n";  
                                 $out .= "Document-Type: XML\n\n$xml\n";  
                                 print $out;  
                         }  
                 }  
75          }          }
         print STDERR "\n";  
 }  
   
 $dbh->commit || die $dbh->errstr();  
   
 1;  
 __END__  
 ##########################################################################  
76    
77  =head1 NAME          my $headline = $webpac->{'headline'};
   
 isis2xml.pl - read isis file and dump XML  
78    
79  =head1 DESCRIPTION          my $f = $filename;
80            $f =~ s!out/!!;
81    
82  This command will read ISIS data file using OpenIsis perl module and          # save into index
83  create XML file for usage with I<SWISH-E>          foreach my $ds (@ds) {
84  indexer. Dispite it's name, this script B<isn't general xml generator>                  next if (! $ds->{'swish'});
 from isis files (isis allready has something like that). Output of this  
 script is tailor-made for SWISH-E.  
85    
86  =head1 AUTHOR                  $index->insert(
87                            index_name => $ds->{'tag'},
88  Dobrica Pavlinusic <dpavlin@rot13.org>                          path => $f,
89                            headline => $headline,
90                            words => join(" ",@{$ds->{'swish'}})
91                    );
92            }
93    
94  =head1 COPYRIGHT  #       print Dumper(\@ds);
95    
96  GNU Public License (GPL) v2 or later  }
97    
98  =head1 SEE ALSO  if ($log->is_debug) {
99            $log->debug("lookup hash: ",Dumper($webpac->{'lookup'}));
100            $log->debug("data hash: ",Dumper($webpac->{'data'}));
101    }
102    
103  SWISH-E web site at http://www.swish-e.org  $index->close;
104    
 =cut  

Legend:
Removed from v.9  
changed lines
  Added in v.407

  ViewVC Help
Powered by ViewVC 1.1.26