/[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 42 by dpavlin, Sat Mar 15 21:48:48 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    use Encode;
12    
13  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();  
14    
15  $dbh->do("delete from index_author") || die $dbh->errstr();  my $config_file = $0;
16  $dbh->do("delete from index_title") || die $dbh->errstr();  $config_file =~ s/\.pl$/.conf/;
17    die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
18    
19    my $config;
20    
21    use index_DBI;  # there is no other, right now ;-)
22    
23    my $index = new index_DBI();    # open index
24    
25  my %opts;  my %opts;
26    
# Line 27  my %opts; Line 32  my %opts;
32    
33  getopts('d:m:qs', \%opts);  getopts('d:m:qs', \%opts);
34    
35  my $db_dir = $opts{d} || "ps";  # FIX  my $db_dir;
36    
37  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
38    
39  #print Dumper($config->{indexer});  # this is encoding of all files on disk, including import_xml/*.xml file and
40  #print "-" x 70,"\n";  # filter/*.pm files! It will be used to store strings in perl internally!
41    my $codepage = 'ISO-8859-2';
42    
43    my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
44    # this function will convert data from XML files to local encoding
45    sub x {
46            return $utf2cp->convert($_[0]);
47    }
48    
49  # how to convert isis code page to UTF8?  # decode isis import codepage
50  my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;  my $isis2cp;
51    
52    # outgoing xml must be in UTF-8
53    my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
54    
55  sub isis2xml {  sub isis2xml {
56    
57            use xmlify;
58    
59          my $row = shift @_;          my $row = shift @_;
60            my $add_xml = shift @_;
61    
62          my $xml;          my $xml;
         $xml->{db_dir} = [ $db_dir ];   # FIX remove?  
63    
64          sub isis_sf {          use parse_format;
65                  my $row = shift @_;  
66                  my $isis_id = shift @_;          my $html = "";          # html formatted display output
67                  my $subfield = shift @_;  
68                  if ($row->{$isis_id}->[0]) {          my %field_usage;        # counter for usage of each field
69                          my $sf = OpenIsis::subfields($row->{$isis_id}->[0]);  
70                          if (! defined $subfield || length($subfield) == 0) {          # sort subrouting using order="" attribute
71                                  # subfield list undef, empty or no defined subfields for this record          sub by_order {
72                                  my $all_sf = $row->{$isis_id}->[0];                  return 0 if (! $config->{indexer}->{$a}->{order});
73                                  $all_sf =~ s/\^./ /g;   nuke definirions                  return 0 if (! $config->{indexer}->{$b}->{order});
74                                  return $all_sf;  
75                          } elsif ($sf->{$subfield}) {                  return $config->{indexer}->{$a}->{order} <=>
76                                  return $sf->{$subfield};                          $config->{indexer}->{$b}->{order} ;
                         }  
                 }  
77          }          }
78    
79          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (sort by_order keys %{$config->{indexer}}) {
80    
81                    $field=x($field);
82    
83                    $field_usage{$field}++;
84    
                 my $display_data = "";  
85                  my $swish_data = "";                  my $swish_data = "";
86                  my $index_data = "";                  my $display_data = "";
87                    my $line_delimiter;
88    
89                    my ($swish,$display);
90    
91                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
92    
93                          my $display_tmp = "";                          my $format = x($x->{content});
94                          my $swish_tmp = "";                          my $delimiter = x($x->{delimiter}) || ' ';
95                          my $index_tmp = "";  
96                            my $isis_i = 0;         # isis repeatable offset
97                          my $format = $x->{content};  
98                          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  
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";    
103                          # parse format                          # what will separate last line from this one?
104                          my $prefix = "";                          if ($display_data && $x->{append} && $x->{append} eq "1") {
105                          if ($format =~ s/^([^\d]+)//) {                                  $line_delimiter = ' ';
106                                  $prefix = $1;                          } elsif ($display_data) {
107                                    $line_delimiter = '<br/>';
108                          }                          }
109                          while ($format) {  
110                                  if ($format =~ s/^(\d\d\d)(\w?)//) {                          # init vars so that we go into while...
111                                          my $isis_tmp = isis_sf($row,$1,$2);                          ($swish,$display) = (1,1);
112                                          if ($isis_tmp) {  
113  #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);                          while ($swish || $display) {
114                                                  $display_tmp .= $prefix . $isis_tmp if ($d);                                  ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);
115                                                  $swish_tmp .= $isis_tmp." " if ($s);  
116                                                  $index_tmp .= $prefix . $isis_tmp if ($i);                                  # filter="name" ; filter this field through
117  #print STDERR " $isis_tmp <--\n";                                  # filter/[name].pm
118                                    my $filter = $x->{filter};
119                                    if ($filter) {
120                                            require "filter/".$filter.".pm";
121                                    }
122                                    # type="swish" ; field for swish
123                                    if ($s && $swish) {
124                                            if ($filter) {
125                                                    no strict 'refs';
126                                                    $swish_data .= join(" ",&$filter($swish));
127    print STDERR "#### $swish_data\n";
128                                            } else {
129                                                    $swish_data .= $swish;
130                                          }                                          }
131                                          $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;  
132    
133                                    # type="display" ; field for display
134                                    if ($d && $display) {
135                                            if ($line_delimiter && $display_data) {
136                                                    $display_data .= $line_delimiter;
137                                                    undef $line_delimiter;
138                                            }
139                                            if ($filter) {
140                                                    no strict 'refs';
141                                                    $display_data .= join($delimiter,&$filter($display));
142                                            } else {
143                                                    if ($display_data) {
144                                                            $display_data .= $delimiter.$display;
145                                                    } else {
146                                                            $display_data .= $display;
147                                                    }
148                                            }
149                                    }
150                                                    
151                                    # type="index" ; insert into index
152                                    if ($i && $display) {
153                                            my $index_data = $display;
154                                            if ($filter) {
155                                                    no strict 'refs';
156                                                    foreach my $d (&$filter($index_data)) {
157                                                            $index->insert($field, $d, $db_dir);
158                                                    }
159                                            } else {
160                                                    $index->insert($field, $index_data, $db_dir);
161                                            }
162                                    }
163                            }
164                  }                  }
165  #print "--display:$display_data\n--swish:$swish_data\n";  
166                  #$xml->{$field."_display"} = $isis_map->tou($display_data)->utf8 if ($display_data);  
167                  #$xml->{$field."_swish"} = unac_string($config->{isis_codepage},$swish_data) if ($swish_data);                  if ($display_data) {
168                  $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data);  
169                  $xml->{$field."_swish"} = [ unac_string($config->{isis_codepage},$swish_data) ] if ($swish_data);                          if ($field eq "headline") {
170                                    $xml .= xmlify("headline", $display_data);
171                  # index                          } else {
172                  if ($index_data && $index_data ne "") {  
173                          my $sql = "select $field from index_$field where upper($field)=upper(?)";                                  # find field name (signular, plural)
174                          my $sth = $dbh->prepare($sql) || die $dbh->errstr();                                  my $field_name = "";
175                          $sth->execute($index_data) || die "SQL: $sql; ".$dbh->errstr();                                  if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
176  #print STDERR "--->$index_data<---\n";                                          $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
177                          if (! $sth->fetchrow_hashref) {                                  } elsif ($config->{indexer}->{$field}->{name_plural}) {
178                                  my $sql = "insert into index_$field values (?)";                                          $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
179                                  my $sth = $dbh->prepare($sql) || die $dbh->errstr();                                  } elsif ($config->{indexer}->{$field}->{name}) {
180  #print STDERR "$sql: $index_data<!----\n";                                          $field_name = $config->{indexer}->{$field}->{name}."#-#";
181                                  $sth->execute($index_data) || die "SQL: $sql; ".$dbh->errstr();                                  } else {
182                                            print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
183                                    }
184                                    if ($field_name) {
185                                            $html .= x($field_name);
186                                    }
187                                    $html .= $display_data."###\n";
188                          }                          }
189                  }                  }
190                    if ($swish_data) {
191                            # remove extra spaces
192                            $swish_data =~ s/ +/ /g;
193                            $swish_data =~ s/ +$//g;
194    
195                            $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
196                    }
197    
198    
199            }
200    
201            # dump formatted output in <html>
202            if ($html) {
203                    $xml .= xmlify("html",$html);
204          }          }
205            
206          if ($xml) {          if ($xml) {
207                  return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );                  $xml .= $add_xml if ($add_xml);
208                    return "<xml>\n$xml</xml>\n";
209          } else {          } else {
210                  return;                  return;
211          }          }
# Line 145  sub isis2xml { Line 213  sub isis2xml {
213    
214  ##########################################################################  ##########################################################################
215    
216  my $last_tell=0;  my $cfg = new Config::IniFiles( -file => $config_file );
217    
218  my @isis_dirs = ( '.' );        # use dirname as database name  foreach my $database ($cfg->Sections) {
219    
220  if ($opts{m}) {          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
221          @isis_dirs = split(/,/,$opts{m});          my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";
222  }          my $add_xml = $cfg -> val($database, 'xml');    # optional
   
 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";  
         }  
 }  
   
 print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);  
223    
224  my $db;          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
   
 foreach my $isis_db (@isis_dbs) {  
225    
226            $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
227    
228          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
229          if (0) {          if (0) {
# Line 189  foreach my $isis_db (@isis_dbs) { Line 237  foreach my $isis_db (@isis_dbs) {
237    
238          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
239    
240            my $path = $database;                   # was $isis_db
241    
242          my $last_p = 0;          my $last_p = 0;
243    
244  #       { my $row_id = 1;  #       { my $row_id = 4514;
245  # FIX  # FIX
246          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
247                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
248                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
   
249                          # output current process indicator                          # output current process indicator
250                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
251                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 204  foreach my $isis_db (@isis_dbs) { Line 253  foreach my $isis_db (@isis_dbs) {
253                                  $last_p = $p;                                  $last_p = $p;
254                          }                          }
255    
256                          if (my $xml = isis2xml($row)) {                          if (my $xml = $cp2utf->convert(isis2xml($row,$add_xml))) {
257                                  my $path = $isis_db;                                  use bytes;      # as opposed to chars
258                                  $path =~ s#$config->{isis_data}/*##g;                                  print "Path-Name: $path#".int($row->{mfn})."\n";
259                                  my $out = "Path-Name: $path#".$row->{mfn}."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
260                                  $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;  
261                          }                          }
262                  }                  }
263          }          }
264          print STDERR "\n";          print STDERR "\n";
265  }  }
266    
267  $dbh->commit || die $dbh->errstr();  # call this to commit index
268    $index->close;
269    
270  1;  1;
271  __END__  __END__

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

  ViewVC Help
Powered by ViewVC 1.1.26