/[webpac]/branches/drustvene/all2xml.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 /branches/drustvene/all2xml.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 10 by dpavlin, Thu Jan 16 17:35:54 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;
 use DBI;  
10    
 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();  
11    
12  $dbh->do("delete from index_author") || die $dbh->errstr();  $|=1;
13  $dbh->do("delete from index_title") || die $dbh->errstr();  
14    my $config;
15    
16    $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);
17    
18    use index_DBI;  # there is no other, right now ;-)
19    
20    my $index = new index_DBI();    # open index
21    
22  my %opts;  my %opts;
23    
# Line 34  my $db_dir = $opts{d} || "ps"; # FIX Line 36  my $db_dir = $opts{d} || "ps"; # FIX
36  #print Dumper($config->{indexer});  #print Dumper($config->{indexer});
37  #print "-" x 70,"\n";  #print "-" x 70,"\n";
38    
39  # how to convert isis code page to UTF8?  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
40  my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;  
41    my $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');
42    my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
43    my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');
44    
45  sub isis2xml {  sub isis2xml {
46    
47            use xmlify;
48    
49          my $row = shift @_;          my $row = shift @_;
50    
51          my $xml;          my $xml;
52          $xml->{db_dir} = [ $db_dir ];   # FIX remove?          $xml .= xmlify('db_dir',$db_dir);       # FIX remove?
53    
54            use parse_format;
55    
         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};  
                         }  
                 }  
         }  
56    
57          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (keys %{$config->{indexer}}) {
58    
                 my $display_data = "";  
59                  my $swish_data = "";                  my $swish_data = "";
60                    my $display_data = "";
61                  my $index_data = "";                  my $index_data = "";
62    
63                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
64    
                         my $display_tmp = "";  
                         my $swish_tmp = "";  
                         my $index_tmp = "";  
   
65                          my $format = $x->{content};                          my $format = $x->{content};
66                          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  
67                          $s = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
68                          $d = 0 if (lc($x->{type}) eq "swish");                          $d = 0 if (lc($x->{type}) eq "swish");
69                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
70  #print STDERR "## s: $s d: $d i: $i ## $format ##\n";    #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;  
71    
72                            $format = $cludge_codepage->convert($format);
73                            my ($swish,$display) = parse_format($format,$row);
74    #print STDERR "s: $swish\nd: $display\n" if ($swish);
75    
76    #print STDERR "swish: $swish<-- display: $display<--\n";
77                            # FIX: this is ugly, UGLY, cludge: OpenIsis return
78                            # UTF8 encoding of strings, but as if source charset
79                            # is ISO8859-1 and not some other. This breaks our
80                            # isis character encoding, so we convert it first
81                            # back to ISO8859-1 (which can actually be different
82                            # encoding in isis)
83    
84                            $swish_data .= $swish if ($s && $swish);
85                            $display_data .= $display if ($d && $display);
86                            $index_data .= $display if ($i && $display);
87                    }
88    
89    #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
90                    if ($display_data) {
91                            $display_data = $isis_codepage->convert($display_data)."##" || $display_data;
92                            $xml .= xmlify($field."_display", $display_data);
93                    }
94                    if ($swish_data) {
95                            my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
96                            $swish_data = $i->convert($swish_data);
97                            $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
98                            #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
99                            #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));
100                  }                  }
 #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);  
101    
102                  # index                  # index
103                  if ($index_data && $index_data ne "") {                  if ($index_data && $index_data ne "") {
104                          my $sql = "select $field from index_$field where upper($field)=upper(?)";                          $index_data = $index_codepage->convert($index_data) || $index_data;
105                          my $sth = $dbh->prepare($sql) || die $dbh->errstr();                          $index->insert($field, $index_data, $db_dir);
                         $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();  
                         }  
106                  }                  }
107    
108          }          }
109          if ($xml) {          if ($xml) {
110                  return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );  #print STDERR "x: $xml\n";
111                    return "<xml>\n$xml</xml>\n";
112          } else {          } else {
113                  return;                  return;
114          }          }
# Line 189  foreach my $isis_db (@isis_dbs) { Line 160  foreach my $isis_db (@isis_dbs) {
160    
161          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
162    
163            my $path = $isis_db;
164            $path =~ s#$config->{isis_data}/*##g;
165    
166          my $last_p = 0;          my $last_p = 0;
167    
168  #       { my $row_id = 1;  #       { my $row_id = 4514;
169  # FIX  # FIX
170          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
171                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
172                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
173    #print STDERR "mfn: ",$row->{mfn},"\n";
174                          # output current process indicator                          # output current process indicator
175                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
176                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 205  foreach my $isis_db (@isis_dbs) { Line 179  foreach my $isis_db (@isis_dbs) {
179                          }                          }
180    
181                          if (my $xml = isis2xml($row)) {                          if (my $xml = isis2xml($row)) {
182                                  my $path = $isis_db;  #print STDERR "--ret-->$xml\n";
183                                  $path =~ s#$config->{isis_data}/*##g;                                  print "Path-Name: $path#".int($row->{mfn})."\n";
184                                  my $out = "Path-Name: $path#".$row->{mfn}."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
185                                  $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;  
186                          }                          }
187                  }                  }
188          }          }
189          print STDERR "\n";          print STDERR "\n";
190  }  }
191    
192  $dbh->commit || die $dbh->errstr();  # call this to commit index
193    $index->close;
194    
195  1;  1;
196  __END__  __END__

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

  ViewVC Help
Powered by ViewVC 1.1.26