/[webpac]/trunk/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 /trunk/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 32 by dpavlin, Sun Feb 23 07:53:01 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;
44    my $index_codepage;
45    my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');
46    my $xml_codepage;
47    
48    my $XML_CHARSET = 'UTF8';
49    
50    
51  sub isis2xml {  sub isis2xml {
52    
53            use xmlify;
54    
55          my $row = shift @_;          my $row = shift @_;
56            my $add_xml = shift @_;
57    
58          my $xml;          my $xml;
         $xml->{db_dir} = [ $db_dir ];   # FIX remove?  
59    
60          sub isis_sf {          use parse_format;
61                  my $row = shift @_;  
62                  my $isis_id = shift @_;          my $html = "";          # html formatted display output
63                  my $subfield = shift @_;  
64                  if ($row->{$isis_id}->[0]) {          my %field_usage;        # counter for usage of each field
65                          my $sf = OpenIsis::subfields($row->{$isis_id}->[0]);  
66                          if (! defined $subfield || length($subfield) == 0) {          # sort subrouting using order="" attribute
67                                  # subfield list undef, empty or no defined subfields for this record          sub by_order {
68                                  my $all_sf = $row->{$isis_id}->[0];                  return 0 if (! $config->{indexer}->{$a}->{order});
69                                  $all_sf =~ s/\^./ /g;   nuke definirions                  return 0 if (! $config->{indexer}->{$b}->{order});
70                                  return $all_sf;  
71                          } elsif ($sf->{$subfield}) {                  return $config->{indexer}->{$a}->{order} <=>
72                                  return $sf->{$subfield};                          $config->{indexer}->{$b}->{order} ;
                         }  
                 }  
73          }          }
74    
75          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (sort by_order keys %{$config->{indexer}}) {
76    
77                    $field_usage{$field}++;
78    
                 my $display_data = "";  
79                  my $swish_data = "";                  my $swish_data = "";
80                  my $index_data = "";                  my $display_data = "";
81                    my $line_delimiter = "";
82    
83                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
84    
                         my $display_tmp = "";  
                         my $swish_tmp = "";  
                         my $index_tmp = "";  
   
85                          my $format = $x->{content};                          my $format = $x->{content};
86                          my $s = 1;      # swish only                          my $delimiter = $x->{delimiter} || ' ';
87                          my $d = 1;      # display only  
88                          my $i = 0;      # index only                          # FIX: this is ugly, UGLY, cludge string is returned
89                            # in UTF8 encoding , but as if source charset
90                            # is ISO8859-1 and not some other. This break other
91                            # character encodings, so we convert it first
92                            # back to ISO8859-1
93                            $format = $xml_codepage->convert($format);
94                            $delimiter = $xml_codepage->convert($delimiter) if ($delimiter);
95    
96                            my $isis_i = 0;         # isis repeatable offset
97    
98                            my ($s,$d,$i) = (1,1,0);        # swish, display default
99                          $s = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
100                          $d = 0 if (lc($x->{type}) eq "swish");                          $d = 0 if (lc($x->{type}) eq "swish");
101                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
102  #print STDERR "## s: $s d: $d i: $i ## $format ##\n";    #print STDERR "## s: $s d: $d i: $i ## $format ##\n";  
103                          # parse format  
104                          my $prefix = "";                          $display_data .= $line_delimiter if ($display_data && $display_data !~ /$line_delimiter$/);
105                          if ($format =~ s/^([^\d]+)//) {  
106                                  $prefix = $1;                          my ($swish,$display) = (1,1);
107                          }  
108                          while ($format) {                          while ($swish || $display) {
109                                  if ($format =~ s/^(\d\d\d)(\w?)//) {                                  ($swish,$display) = parse_format($format,$row,$isis_i++);
110                                          my $isis_tmp = isis_sf($row,$1,$2);  #print STDERR "s: $swish\nd: $display\n" if ($swish);
111                                          if ($isis_tmp) {  
112  #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);  #print STDERR "swish: $swish<-- display: $display<--\n";
113                                                  $display_tmp .= $prefix . $isis_tmp if ($d);  
114                                                  $swish_tmp .= $isis_tmp." " if ($s);                                  # filter="name" ; filter this field through
115                                                  $index_tmp .= $prefix . $isis_tmp if ($i);                                  # filter/[name].pm
116  #print STDERR " $isis_tmp <--\n";                                  my $filter = $x->{filter};
117                                    if ($filter) {
118                                            require "filter/".$filter.".pm";
119                                    }
120                                    # type="swish" ; field for swish
121                                    if ($s && $swish) {
122                                            if ($filter) {
123    #print STDERR "using filter '$filter'\n";
124                                                    no strict 'refs';
125                                                    $swish_data .= join(" ",&$filter($swish));
126                                            } else {
127                                                    $swish_data .= $swish;
128                                          }                                          }
129                                          $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;  
130    
131                  }                                  # type="display" ; field for display
132  #print "--display:$display_data\n--swish:$swish_data\n";                                  if ($d && $display) {
133                  #$xml->{$field."_display"} = $isis_map->tou($display_data)->utf8 if ($display_data);                                          if ($filter) {
134                  #$xml->{$field."_swish"} = unac_string($config->{isis_codepage},$swish_data) if ($swish_data);                                                  no strict 'refs';
135                  $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data);                                                  $display_data .= join($delimiter,&$filter($display));
136                  $xml->{$field."_swish"} = [ unac_string($config->{isis_codepage},$swish_data) ] if ($swish_data);                                          } else {
137                                                    if ($display_data) {
138                  # index                                                          $display_data .= $delimiter.$display;
139                  if ($index_data && $index_data ne "") {                                                  } else {
140                          my $sql = "select $field from index_$field where upper($field)=upper(?)";                                                          $display_data .= $display;
141                          my $sth = $dbh->prepare($sql) || die $dbh->errstr();                                                  }
142                          $sth->execute($index_data) || die "SQL: $sql; ".$dbh->errstr();                                          }
143  #print STDERR "--->$index_data<---\n";                                  }
144                          if (! $sth->fetchrow_hashref) {                                                  
145                                  my $sql = "insert into index_$field values (?)";                                  # type="index" ; insert into index
146                                  my $sth = $dbh->prepare($sql) || die $dbh->errstr();                                  if ($i && $display) {
147  #print STDERR "$sql: $index_data<!----\n";                                          my $index_data = $index_codepage->convert($display) || $display;
148                                  $sth->execute($index_data) || die "SQL: $sql; ".$dbh->errstr();                                          if ($filter) {
149                                                    no strict 'refs';
150                                                    foreach my $d (&$filter($index_data)) {
151                                                            $index->insert($field, $d, $db_dir);
152                                                    }
153                                            } else {
154                                                    $index->insert($field, $index_data, $db_dir);
155                                            }
156                                    }
157                            }
158                            if ($x->{append}) {
159                                    $line_delimiter = ' ';
160                            } else {
161                                    $line_delimiter = '<br/>';
162                          }                          }
163                  }                  }
164    
         }  
         if ($xml) {  
                 return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );  
         } else {  
                 return;  
         }  
 }  
165    
166  ##########################################################################  #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
167                    if ($display_data) {
 my $last_tell=0;  
168    
169  my @isis_dirs = ( '.' );        # use dirname as database name                          # remove last <br>
170                            $display_data =~ s/$line_delimiter$//;
171    
172  if ($opts{m}) {                          $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";
173          @isis_dirs = split(/,/,$opts{m});                          # FIX: this is removed and replaced by html tag.
174  }                          #$xml .= xmlify($field."_display", $display_data);
175    
176                            if ($field eq "headline") {
177                                    $xml .= xmlify("headline", $display_data);
178                            } else {
179    
180                                    # find field name (signular, plural)
181                                    my $field_name = "";
182                                    if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
183                                            $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
184                                    } elsif ($config->{indexer}->{$field}->{name_plural}) {
185                                            $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
186                                    } elsif ($config->{indexer}->{$field}->{name}) {
187                                            $field_name = $config->{indexer}->{$field}->{name}."#-#";
188                                    } else {
189                                            print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
190                                    }
191                                    if ($field_name) {
192                                            $html .= $xml_codepage->convert($field_name);
193    #                                       $html .= "-->".$field_name."<--";
194                                    }
195                                    $html .= $display_data."###\n";
196                            }
197                    }
198                    if ($swish_data) {
199                            my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
200                            # remove extra spaces
201                            $swish_data =~ s/ +/ /g;
202                            $swish_data =~ s/ +$//g;
203    
204                            $swish_data = $i->convert($swish_data);
205                            $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
206                            #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
207                            #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));
208                    }
209    
 my @isis_dbs;  
210    
 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";  
211          }          }
212          if (-e $config->{isis_data}."/$db_dir/$_/AMS") {  
213                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";          # dump formatted output in <html>
214            if ($html) {
215                    $xml .= xmlify("html",$html);
216          }          }
217          if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {          
218  #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";          if ($xml) {
219    #print STDERR "x: $xml\n";
220                    $xml .= $add_xml if ($add_xml);
221                    return "<xml>\n$xml</xml>\n";
222            } else {
223                    return;
224          }          }
225  }  }
226    
227  print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);  ##########################################################################
228    
229  my $db;  my $cfg = new Config::IniFiles( -file => $config_file );
230    
231  foreach my $isis_db (@isis_dbs) {  foreach my $database ($cfg->Sections) {
232    
233            my $isis_db = $cfg -> val($database, 'isis_db');
234            my $type = $cfg -> val($database, 'type');
235            my $add_xml = $cfg -> val($database, 'xml');
236    
237            # read configuration for this type
238            $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
239            $isis_codepage = Text::Iconv->new($config->{isis_codepage},$XML_CHARSET);
240            $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
241            $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),$XML_CHARSET);
242    
243          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
244          if (0) {          if (0) {
# Line 189  foreach my $isis_db (@isis_dbs) { Line 252  foreach my $isis_db (@isis_dbs) {
252    
253          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
254    
255            my $path = $database;                   # was $isis_db
256    
257          my $last_p = 0;          my $last_p = 0;
258    
259  #       { my $row_id = 1;  #       { my $row_id = 4514;
260  # FIX  # FIX
261          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
262                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
263                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
264    #print STDERR "mfn: ",$row->{mfn},"\n";
265                          # output current process indicator                          # output current process indicator
266                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
267                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 204  foreach my $isis_db (@isis_dbs) { Line 269  foreach my $isis_db (@isis_dbs) {
269                                  $last_p = $p;                                  $last_p = $p;
270                          }                          }
271    
272                          if (my $xml = isis2xml($row)) {                          if (my $xml = isis2xml($row,$add_xml)) {
273                                  my $path = $isis_db;  #print STDERR "--ret-->$xml\n";
274                                  $path =~ s#$config->{isis_data}/*##g;                                  print "Path-Name: $path#".int($row->{mfn})."\n";
275                                  my $out = "Path-Name: $path#".$row->{mfn}."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
276                                  $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;  
277                          }                          }
278                  }                  }
279          }          }
280          print STDERR "\n";          print STDERR "\n";
281  }  }
282    
283  $dbh->commit || die $dbh->errstr();  # call this to commit index
284    $index->close;
285    
286  1;  1;
287  __END__  __END__

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

  ViewVC Help
Powered by ViewVC 1.1.26