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

revision 9 by dpavlin, Sat Jan 11 19:55:30 2003 UTC revision 17 by dpavlin, Sat Feb 22 21:33:04 2003 UTC
# Line 6  use Getopt::Std; Line 6  use Getopt::Std;
6  use Data::Dumper;  use Data::Dumper;
7  use XML::Simple;  use XML::Simple;
8  use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,  use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,
9  require Unicode::Map8;  use Text::Iconv;
10  use DBI;  use Config::IniFiles;
11    
12  my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);  $|=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();  
13    
14  $dbh->do("delete from index_author") || die $dbh->errstr();  my $config_file = $0;
15  $dbh->do("delete from index_title") || die $dbh->errstr();  $config_file =~ s/\.pl$/.conf/;
16    die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
17    
18    my $config;
19    
20    use index_DBI;  # there is no other, right now ;-)
21    
22    my $index = new index_DBI();    # open index
23    
24  my %opts;  my %opts;
25    
# Line 27  my %opts; Line 31  my %opts;
31    
32  getopts('d:m:qs', \%opts);  getopts('d:m:qs', \%opts);
33    
34  my $db_dir = $opts{d} || "ps";  # FIX  my $db_dir;
35    
36  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);
37    
38  #print Dumper($config->{indexer});  #print Dumper($config->{indexer});
39  #print "-" x 70,"\n";  #print "-" x 70,"\n";
40    
41  # how to convert isis code page to UTF8?  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
42  my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;  
43    #my $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');
44    #my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
45    my $isis_codepage;
46    my $index_codepage;
47    my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');
48    my $xml_codepage;
49    
50  sub isis2xml {  sub isis2xml {
51    
52            use xmlify;
53    
54          my $row = shift @_;          my $row = shift @_;
55            my $add_xml = shift @_;
56    
57          my $xml;          my $xml;
         $xml->{db_dir} = [ $db_dir ];   # FIX remove?  
58    
59          sub isis_sf {          use parse_format;
60                  my $row = shift @_;  
61                  my $isis_id = shift @_;          my $html = "";          # html formatted display output
62                  my $subfield = shift @_;  
63                  if ($row->{$isis_id}->[0]) {          my %field_usage;        # counter for usage of each field
                         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};  
                         }  
                 }  
         }  
64    
65          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (keys %{$config->{indexer}}) {
66    
67                  my $display_data = "";                  $field_usage{$field}++;
68    
69                  my $swish_data = "";                  my $swish_data = "";
70                  my $index_data = "";                  my $display_data = "";
71    
72                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
73    
                         my $display_tmp = "";  
                         my $swish_tmp = "";  
                         my $index_tmp = "";  
   
74                          my $format = $x->{content};                          my $format = $x->{content};
75                          my $s = 1;      # swish only                          my ($s,$d,$i) = (1,1,0);        # swish, display default
                         my $d = 1;      # display only  
                         my $i = 0;      # index only  
76                          $s = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
77                          $d = 0 if (lc($x->{type}) eq "swish");                          $d = 0 if (lc($x->{type}) eq "swish");
78                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
79  #print STDERR "## s: $s d: $d i: $i ## $format ##\n";    #print STDERR "## s: $s d: $d i: $i ## $format ##\n";  
80                          # parse format  
81                          my $prefix = "";                          $format = $cludge_codepage->convert($format);
82                          if ($format =~ s/^([^\d]+)//) {                          my ($swish,$display) = parse_format($format,$row);
83                                  $prefix = $1;  #print STDERR "s: $swish\nd: $display\n" if ($swish);
84    
85    #print STDERR "swish: $swish<-- display: $display<--\n";
86                            # FIX: this is ugly, UGLY, cludge: OpenIsis return
87                            # UTF8 encoding of strings, but as if source charset
88                            # is ISO8859-1 and not some other. This breaks our
89                            # isis character encoding, so we convert it first
90                            # back to ISO8859-1 (which can actually be different
91                            # encoding in isis)
92    
93                            $swish_data .= $swish if ($s && $swish);
94                            $display_data .= $display if ($d && $display);
95    
96                            # insert into index
97                            if ($i && $display) {
98                                    my $index_data = $index_codepage->convert($display) || $display;
99                                    $index->insert($field, $index_data, $db_dir);
100                          }                          }
101                          while ($format) {                  }
102                                  if ($format =~ s/^(\d\d\d)(\w?)//) {  
103                                          my $isis_tmp = isis_sf($row,$1,$2);  
104                                          if ($isis_tmp) {  #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
105  #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);                  if ($display_data) {
106                                                  $display_tmp .= $prefix . $isis_tmp if ($d);                          $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";
107                                                  $swish_tmp .= $isis_tmp." " if ($s);                          # FIX: this is removed and replaced by html tag.
108                                                  $index_tmp .= $prefix . $isis_tmp if ($i);                          #$xml .= xmlify($field."_display", $display_data);
109  #print STDERR " $isis_tmp <--\n";  
110                                          }                          if ($field eq "headline") {
111                                          $prefix = "";                                  $xml .= xmlify("headline", $display_data);
112                                  } elsif ($format =~ s/^([^\d]+)//) {                          } else {
113                                          $prefix = $1;  
114                                    # find field name (signular, plural)
115                                    my $field_name = "";
116                                    if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
117                                            $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
118                                    } elsif ($config->{indexer}->{$field}->{name_plural}) {
119                                            $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
120                                  } else {                                  } else {
121                                          print STDERR "WARNING: unparsed format '$format'\n";                                          $field_name = $config->{indexer}->{$field}->{name}."#-#";
122                                          last;                                  }
123                                  };                                  if ($field_name) {
124                                            $html .= $xml_codepage->convert($field_name);
125                                    }
126                                    $html .= $display_data."###\n";
127                          }                          }
                         # 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;  
   
128                  }                  }
129  #print "--display:$display_data\n--swish:$swish_data\n";                  if ($swish_data) {
130                  #$xml->{$field."_display"} = $isis_map->tou($display_data)->utf8 if ($display_data);                          my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
131                  #$xml->{$field."_swish"} = unac_string($config->{isis_codepage},$swish_data) if ($swish_data);                          $swish_data = $i->convert($swish_data);
132                  $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data);                          $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
133                  $xml->{$field."_swish"} = [ unac_string($config->{isis_codepage},$swish_data) ] if ($swish_data);                          #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
134                            #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$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();  
                         }  
135                  }                  }
136    
137    
138          }          }
139    
140            # dump formatted output in <html>
141            if ($html) {
142                    $xml .= xmlify("html",$html);
143            }
144            
145          if ($xml) {          if ($xml) {
146                  return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );  #print STDERR "x: $xml\n";
147                    $xml .= $add_xml if ($add_xml);
148                    return "<xml>\n$xml</xml>\n";
149          } else {          } else {
150                  return;                  return;
151          }          }
# Line 145  sub isis2xml { Line 153  sub isis2xml {
153    
154  ##########################################################################  ##########################################################################
155    
156  my $last_tell=0;  my $cfg = new Config::IniFiles( -file => $config_file );
   
 my @isis_dirs = ( '.' );        # use dirname as database name  
   
 if ($opts{m}) {  
         @isis_dirs = split(/,/,$opts{m});  
 }  
   
 my @isis_dbs;  
   
 foreach (@isis_dirs) {  
         if (-e $config->{isis_data}."/$db_dir/$_/LIBRI") {  
                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/LIBRI/LIBRI";  
         }  
         if (-e $config->{isis_data}."/$db_dir/$_/PERI") {  
                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/PERI/PERI";  
         }  
         if (-e $config->{isis_data}."/$db_dir/$_/AMS") {  
                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";  
         }  
         if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {  
 #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";  
         }  
 }  
157    
158  print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);  foreach my $database ($cfg->Sections) {
   
 my $db;  
   
 foreach my $isis_db (@isis_dbs) {  
159    
160            my $isis_db = $cfg -> val($database, 'isis_db');
161            my $type = $cfg -> val($database, 'type');
162            my $add_xml = $cfg -> val($database, 'xml');
163    
164            # read configuration for this type
165            $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
166            $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');
167            $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
168            $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),'UTF8');
169    
170          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
171          if (0) {          if (0) {
# Line 189  foreach my $isis_db (@isis_dbs) { Line 179  foreach my $isis_db (@isis_dbs) {
179    
180          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
181    
182            my $path = $database;                   # was $isis_db
183    
184          my $last_p = 0;          my $last_p = 0;
185    
186  #       { my $row_id = 1;  #       { my $row_id = 4514;
187  # FIX  # FIX
188          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
189                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
190                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
191    #print STDERR "mfn: ",$row->{mfn},"\n";
192                          # output current process indicator                          # output current process indicator
193                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
194                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 204  foreach my $isis_db (@isis_dbs) { Line 196  foreach my $isis_db (@isis_dbs) {
196                                  $last_p = $p;                                  $last_p = $p;
197                          }                          }
198    
199                          if (my $xml = isis2xml($row)) {                          if (my $xml = isis2xml($row,$add_xml)) {
200                                  my $path = $isis_db;  #print STDERR "--ret-->$xml\n";
201                                  $path =~ s#$config->{isis_data}/*##g;                                  print "Path-Name: $path#".int($row->{mfn})."\n";
202                                  my $out = "Path-Name: $path#".$row->{mfn}."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
203                                  $out .= "Content-Length: ".(length($xml)+1)."\n";                                  print "Document-Type: XML\n\n$xml\n";
                                 $out .= "Document-Type: XML\n\n$xml\n";  
                                 print $out;  
204                          }                          }
205                  }                  }
206          }          }
207          print STDERR "\n";          print STDERR "\n";
208  }  }
209    
210  $dbh->commit || die $dbh->errstr();  # call this to commit index
211    $index->close;
212    
213  1;  1;
214  __END__  __END__

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

  ViewVC Help
Powered by ViewVC 1.1.26