/[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 50 by dpavlin, Sun Jun 1 13:46:42 2003 UTC trunk2/all2all.pl revision 349 by dpavlin, Sun Jun 13 20:39:44 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,  
 use Text::Iconv;  
 use Config::IniFiles;  
 use Encode;  
   
 $|=1;  
   
 my $config_file = $0;  
 $config_file =~ s/\.pl$/.conf/;  
 die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);  
   
 my $config;  
   
 use index_DBI;  # there is no other, right now ;-)  
 my $index;  
   
 my %opts;  
   
 # usage:  
 #       -d directory name  
 #       -m multiple directories  
 #       -q quiet  
 #       -s run swish  
   
 getopts('d:m:qs', \%opts);  
   
 my $path;       # this is name of database  
   
 Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  
   
 # this is encoding of all files on disk, including import_xml/*.xml file and  
 # filter/*.pm files! It will be used to store strings in perl internally!  
 my $codepage = 'ISO-8859-2';  
   
 my $utf2cp = Text::Iconv->new('UTF-8',$codepage);  
 # this function will convert data from XML files to local encoding  
 sub x {  
         return $utf2cp->convert($_[0]);  
 }  
   
 # decode isis import codepage  
 my $isis2cp;  
   
 # outgoing xml must be in UTF-8  
 my $cp2utf = Text::Iconv->new($codepage,'UTF-8');  
   
 sub isis2xml {  
   
         use xmlify;  
   
         my $row = shift @_;  
         my $add_xml = shift @_;  
   
         my $xml;  
   
         use parse_format;  
   
         my $html = "";          # html formatted display output  
   
         my %field_usage;        # counter for usage of each field  
   
         # sort subrouting using order="" attribute  
         sub by_order {  
                 return 0 if (! $config->{indexer}->{$a}->{order});  
                 return 0 if (! $config->{indexer}->{$b}->{order});  
   
                 return $config->{indexer}->{$a}->{order} <=>  
                         $config->{indexer}->{$b}->{order} ;  
         }  
   
         foreach my $field (sort by_order keys %{$config->{indexer}}) {  
   
                 $field=x($field);  
   
                 $field_usage{$field}++;  
   
                 my $swish_data = "";  
                 my $display_data = "";  
                 my $line_delimiter;  
   
                 my ($swish,$display);  
   
                 foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {  
   
                         my $format = x($x->{content});  
                         my $delimiter = x($x->{delimiter}) || ' ';  
   
                         my $isis_i = 0;         # isis repeatable offset  
4    
5                          my ($s,$d,$i) = (1,1,0);        # swish, display default  webpac.pl - basic script for all WebPac needs
                         $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");  
6    
7                          # what will separate last line from this one?  =cut
                         if ($display_data && $x->{append} && $x->{append} eq "1") {  
                                 $line_delimiter = ' ';  
                         } elsif ($display_data) {  
                                 $line_delimiter = '<br/>';  
                         }  
8    
9                          # init vars so that we go into while...  use strict;
10                          ($swish,$display) = (1,1);  use OpenIsis;
11    use Text::Iconv;
12    use Data::Dumper;
13    
14                          while ($swish || $display) {  my $cp = Text::Iconv->new("852", "iso-8859-2");
                                 ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);  
15    
16                                  # filter="name" ; filter this field through  $|=1;
                                 # filter/[name].pm  
                                 my $filter = $x->{filter};  
                                 if ($filter) {  
                                         require "filter/".$filter.".pm";  
                                 }  
                                 # type="swish" ; field for swish  
                                 if ($s && $swish) {  
                                         if ($filter) {  
                                                 no strict 'refs';  
                                                 $swish_data .= join(" ",&$filter($swish));  
                                         } else {  
                                                 $swish_data .= $swish;  
                                         }  
                                 }  
17    
18                                  # type="display" ; field for display  my $db = OpenIsis::open( shift @ARGV || '/data/hidra/THS-500/THS' );
19                                  if ($d && $display) {  my $maxmfn = OpenIsis::maxRowid( $db ) || 1;
                                         if ($line_delimiter && $display_data) {  
                                                 $display_data .= $line_delimiter;  
                                                 undef $line_delimiter;  
                                         }  
                                         if ($filter) {  
                                                 no strict 'refs';  
                                                 $display_data .= join($delimiter,&$filter($display));  
                                         } else {  
                                                 if ($display_data) {  
                                                         $display_data .= $delimiter.$display;  
                                                 } else {  
                                                         $display_data .= $display;  
                                                 }  
                                         }  
                                 }  
                                                   
                                 # type="index" ; insert into index  
                                 if ($i && $display) {  
                                         my $index_data = $display;  
                                         if ($filter) {  
                                                 no strict 'refs';  
                                                 foreach my $d (&$filter($index_data)) {  
                                                         $index->insert($field, $d, $path);  
                                                 }  
                                         } else {  
                                                 $index->insert($field, $index_data, $path);  
                                         }  
                                 }  
                         }  
                 }  
20    
21    print "rows: $maxmfn\n\n";
22    
23                  if ($display_data) {  my $data;
24    
25                          if ($field eq "headline") {  my @lookup = (
26                                  $xml .= xmlify("headline", $display_data);          { 'key' => 'd:v900', 'val' => 'v250^a' },
27                          } else {          { 'eval' => '"v901^a" eq "Područje"', 'key' => 'pa:v561^4:v562^4:v461^1', 'val' => 'v900' },
28            { 'eval '=> '"v901^a" eq "Mikrotezaurus"', 'key' => 'ma:v561^4:v562^4:v461^1', 'val' => 'v900' },
29                                  # find field name (signular, plural)          { 'eval' => '"v901^a" eq "Deskriptor"', 'key' => 'da:v561^4:v562^4:v461^1', 'val' => 'v900' },
30                                  my $field_name = "";  );
31                                  if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {  
32                                          $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";  sub fill_in {
33                                  } elsif ($config->{indexer}->{$field}->{name_plural}) {          my $tmp = shift || return;
34                                          $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";          my $data = shift || return;
35                                  } elsif ($config->{indexer}->{$field}->{name}) {          my $i = shift || 0;
36                                          $field_name = $config->{indexer}->{$field}->{name}."#-#";  
37                                  } else {          my $found = 0;
38                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";  
39                                  }          sub get_sf {
40                                  if ($field_name) {                  my ($found,$data,$f,$sf,$i) = @_;
41                                          $html .= x($field_name);                  if ($$data->{$f} && $$data->{$f}->[$i]->{$sf}) {
42                                  }                          $$found++;
43                                  $html .= $display_data."###\n";                          return $$data->{$f}->[$i]->{$sf};
44                          }                  } else {
45                            return '';
46                  }                  }
47                  if ($swish_data) {          }
                         # remove extra spaces  
                         $swish_data =~ s/ +/ /g;  
                         $swish_data =~ s/ +$//g;  
48    
49                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));          sub get_nosf {
50                    my ($found,$data,$f,$i) = @_;
51                    if ($$data->{$f} && $$data->{$f}->[$i]) {
52                            $$found++;
53                            return $$data->{$f}->[$i];
54                    } else {
55                            return '';
56                  }                  }
   
   
57          }          }
58    
59          # dump formatted output in <html>          $tmp =~ s/v(\d+)\^(\w)/get_sf(\$found,\$data,$1,$2,$i)/ges;
60          if ($html) {          $tmp =~ s/v(\d+)/get_nosf(\$found,\$data,$1,$i)/ges;
61                  $xml .= xmlify("html",$html);  
62          }          if ($found) {
63                            return $tmp;
         if ($xml) {  
                 $xml .= $add_xml if ($add_xml);  
                 return "<xml>\n$xml</xml>\n";  
64          } else {          } else {
65                  return;                  return;
66          }          }
67  }  }
68    
69  ##########################################################################  print "reading database...\n";
   
 my $cfg = new Config::IniFiles( -file => $config_file );  
70    
71  # open index  for (my $mfn = 1; $mfn <= $maxmfn; $mfn++) {
 $index = new index_DBI(  
                 $cfg->val('global', 'dbi_dbd'),  
                 $cfg->val('global', 'dbi_dsn'),  
                 $cfg->val('global', 'dbi_user'),  
                 $cfg->val('global', 'dbi_passwd') || '',  
         );  
72    
73  # delete [global] section to leave just databases sections          my $row = OpenIsis::read( $db, $mfn );
74  $cfg->DeleteSection('global');          foreach my $k (keys %{$row}) {
75                    if ($k ne "mfn") {
76  foreach my $database ($cfg->Sections) {                          foreach my $l (@{$row->{$k}}) {
77                                    $l = $cp->convert($l);
78          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";                                  # has subfields?
79          my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";                                  my $val;
80          my $add_xml = $cfg -> val($database, 'xml');    # optional                                  if ($l =~ m/\^/) {
81                                            foreach my $t (split(/\^/,$l)) {
82          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);                                                  next if (! $t);
83                                                    $val->{substr($t,0,1)} = substr($t,1);
84                                            }
85                                    } else {
86                                            $val = $l;
87                                    }
88    
89          $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);                                  push @{$data->{$mfn}->{$k}}, $val;
90                            }
91                    }
92    
         my $db = OpenIsis::open( $isis_db );  
         if (0) {  
 #       # FIX  
 #       if (! $db ) {  
                 print STDERR "WARNING: can't open '$isis_db'\n";  
                 next ;  
93          }          }
94    
         my $max_rowid = OpenIsis::maxRowid( $db );  
   
         print STDERR "Reading database: $isis_db [$max_rowid rows]\n";  
   
         my $path = $database;                   # was $isis_db  
   
         my $last_p = 0;  
95    
96  #       { my $row_id = 4514;          foreach my $i (@lookup) {
97  # FIX                  my $d = $data->{$mfn};
98          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                  if ($i->{'eval'}) {
99                  my $row = OpenIsis::read( $db, $row_id );                          my $eval = fill_in($i->{'eval'},$d);
100                  if ($row && $row->{mfn}) {                          my $key = fill_in($i->{'key'}, $d);
101                          # output current process indicator                          my $val = fill_in($i->{'val'}, $d);
102                          my $p = int($row->{mfn} * 100 / $max_rowid);                          if ($key && $val && eval $eval) {
103                          if ($p != $last_p) {                                  push @{$data->{lookup}->{$key}}, $val;
                                 printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});  
                                 $last_p = $p;  
104                          }                          }
105                    } else {
106                          my $swishpath = $path."#".int($row->{mfn});                          my $key = fill_in($i->{'key'}, $d);
107                            my $val = fill_in($i->{'val'}, $d);
108                          if (my $xml = isis2xml($row,$add_xml)) {                          if ($key && $val) {
109                                  $xml = $cp2utf->convert($xml);                                  push @{$data->{lookup}->{$key}}, $val;
                                 use bytes;      # as opposed to chars  
                                 print "Path-Name: $swishpath\n";  
                                 print "Content-Length: ".(length($xml)+1)."\n";  
                                 print "Document-Type: XML\n\n$xml\n";  
110                          }                          }
111                  }                  }
112          }          }
         print STDERR "\n";  
 }  
113    
114  # call this to commit index  }
115  $index->close;  print "\n";
   
 1;  
 __END__  
 ##########################################################################  
   
 =head1 NAME  
   
 isis2xml.pl - read isis file and dump XML  
   
 =head1 DESCRIPTION  
   
 This command will read ISIS data file using OpenIsis perl module and  
 create XML file for usage with I<SWISH-E>  
 indexer. Dispite it's name, this script B<isn't general xml generator>  
 from isis files (isis allready has something like that). Output of this  
 script is tailor-made for SWISH-E.  
   
 =head1 AUTHOR  
   
 Dobrica Pavlinusic <dpavlin@rot13.org>  
   
 =head1 COPYRIGHT  
   
 GNU Public License (GPL) v2 or later  
   
 =head1 SEE ALSO  
116    
117  SWISH-E web site at http://www.swish-e.org  for (my $mfn = 1; $mfn <= $maxmfn; $mfn++) {
118            my $d = $data->{$mfn};
119            print $d->{'250'}->[0]->{'a'},"\n";
120            my $t = fill_in('v553^1;;v553^a', $d);
121            print " Uži pojam: $t\n" if ($t);
122            $t = fill_in('[a:v251];;[d:[a:v251]]', $d);
123            print " Uži pojam: $t\n" if ($t);
124    }
125    
126  =cut  print Dumper($data);

Legend:
Removed from v.50  
changed lines
  Added in v.349

  ViewVC Help
Powered by ViewVC 1.1.26