/[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 4 by dpavlin, Sun Dec 1 22:51:29 2002 UTC revision 29 by dpavlin, Sun Feb 23 07:08:54 2003 UTC
# Line 5  use OpenIsis; Line 5  use OpenIsis;
5  use Getopt::Std;  use Getopt::Std;
6  use Data::Dumper;  use Data::Dumper;
7  use XML::Simple;  use XML::Simple;
8  use Text::Unaccent;  use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,
9  require Unicode::Map8;  use Text::Iconv;
10    use Config::IniFiles;
11    
12  my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);  $|=1;
13    
14    my $config_file = $0;
15    $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    
26  getopts('d:m:q', \%opts);  # usage:
27    #       -d directory name
28    #       -m multiple directories
29    #       -q quiet
30    #       -s run swish
31    
32    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;
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]);  
                         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};  
                         }  
                 }  
         }  
66    
67          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (keys %{$config->{indexer}}) {
68    
69                    $field_usage{$field}++;
70    
71                    my $swish_data = "";
72                  my $display_data = "";                  my $display_data = "";
73                  my $index_data = "";                  my $line_delimiter = "";
74    
75                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
76    
                         my $display_tmp = "";  
                         my $index_tmp = "";  
   
77                          my $format = $x->{content};                          my $format = $x->{content};
78                          my $i = 1;      # index only                          my $delimiter = $x->{delimiter} || ' ';
                         my $d = 1;      # display only  
                         $i = 0 if (lc($x->{type}) eq "display");  
                         $d = 0 if (lc($x->{type}) eq "index");  
 #print "## i: $i d: $d ## $format ##";    
                         # 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);  
                                                 $index_tmp .= $isis_tmp." " if ($i);  
 #print " $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);  
79    
80  #                       $display_data .= $display_tmp if ($display_tmp ne "");                          # FIX: this is ugly, UGLY, cludge string is returned
81  #                       $index_data .= $index_tmp if ($index_tmp ne "");                          # in UTF8 encoding , but as if source charset
82                          $display_data .= $display_tmp;                          # is ISO8859-1 and not some other. This break other
83                          $index_data .= $index_tmp;                          # character encodings, so we convert it first
84                            # back to ISO8859-1
85                            $format = $xml_codepage->convert($format);
86                            $delimiter = $xml_codepage->convert($delimiter) if ($delimiter);
87    
88                            my $isis_i = 0;         # isis repeatable offset
89    
90                            my ($s,$d,$i) = (1,1,0);        # swish, display default
91                            $s = 0 if (lc($x->{type}) eq "display");
92                            $d = 0 if (lc($x->{type}) eq "swish");
93                            ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
94    #print STDERR "## s: $s d: $d i: $i ## $format ##\n";  
95    
96                            $display_data .= $line_delimiter if ($display_data && $display_data !~ /$line_delimiter$/);
97    
98                            my ($swish,$display) = (1,1);
99    
100                            while ($swish || $display) {
101                                    ($swish,$display) = parse_format($format,$row,$isis_i++);
102    #print STDERR "s: $swish\nd: $display\n" if ($swish);
103    
104    #print STDERR "swish: $swish<-- display: $display<--\n";
105    
106                                    # filter="name" ; filter this field through
107                                    # filter/[name].pm
108                                    my $filter = $x->{filter};
109                                    if ($filter) {
110                                            require "filter/".$filter.".pm";
111                                    }
112                                    # type="swish" ; field for swish
113                                    if ($s && $swish) {
114                                            if ($filter) {
115    print STDERR "using filter '$filter'\n";
116                                                    no strict 'refs';
117                                                    $swish_data .= join(" ",&$filter($swish));
118                                            } else {
119                                                    $swish_data .= $swish;
120                                            }
121                                    }
122    
123                                    # type="display" ; field for display
124                                    if ($d && $display) {
125                                            if ($filter) {
126                                                    no strict 'refs';
127                                                    $display_data .= join($delimiter,&$filter($display));
128                                            } else {
129                                                    if ($display_data) {
130                                                            $display_data .= $delimiter.$display;
131                                                    } else {
132                                                            $display_data .= $display;
133                                                    }
134                                            }
135                                    }
136                                                    
137                                    # type="index" ; insert into index
138                                    if ($i && $display) {
139                                            my $index_data = $index_codepage->convert($display) || $display;
140                                            if ($filter) {
141                                                    no strict 'refs';
142                                                    foreach my $d (&$filter($index_data)) {
143                                                            $index->insert($field, $d, $db_dir);
144                                                    }
145                                            } else {
146                                                    $index->insert($field, $index_data, $db_dir);
147                                            }
148                                    }
149                            }
150                            if ($x->{append}) {
151                                    $line_delimiter = ' ';
152                            } else {
153                                    $line_delimiter = '<br/>';
154                            }
155                  }                  }
 #print "--display:$display_data\n--index:$index_data\n";  
                 $xml->{$field}->{display} .= $isis_map->tou($display_data)->utf8 if ($display_data);  
                 $xml->{$field}->{index} .= unac_string($config->{isis_codepage},$index_data) if ($index_data);  
           
         }  
         if ($xml) {  
                 return XMLout($xml, rootname => 'xml', noattr => 1 );  
         } else {  
                 return;  
         }  
 }  
156    
 ##########################################################################  
157    
158  my $last_tell=0;  #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
159                    if ($display_data) {
160    
161  my @isis_dirs = ( '.' );        # use dirname as database name                          # remove last <br>
162                            $display_data =~ s/$line_delimiter$//;
163    
164  if ($opts{m}) {                          $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";
165          @isis_dirs = split(/,/,$opts{m});                          # FIX: this is removed and replaced by html tag.
166  }                          #$xml .= xmlify($field."_display", $display_data);
167    
168                            if ($field eq "headline") {
169                                    $xml .= xmlify("headline", $display_data);
170                            } else {
171    
172                                    # find field name (signular, plural)
173                                    my $field_name = "";
174                                    if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
175                                            $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
176                                    } elsif ($config->{indexer}->{$field}->{name_plural}) {
177                                            $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
178                                    } elsif ($config->{indexer}->{$field}->{name}) {
179                                            $field_name = $config->{indexer}->{$field}->{name}."#-#";
180                                    } else {
181                                            print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
182                                    }
183                                    if ($field_name) {
184                                            $html .= $xml_codepage->convert($field_name);
185    #                                       $html .= "-->".$field_name."<--";
186                                    }
187                                    $html .= $display_data."###\n";
188                            }
189                    }
190                    if ($swish_data) {
191                            my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
192                            # remove extra spaces
193                            $swish_data =~ s/ +/ /g;
194                            $swish_data =~ s/ +$//g;
195    
196                            $swish_data = $i->convert($swish_data);
197                            $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
198                            #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
199                            #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));
200                    }
201    
 my @isis_dbs;  
202    
 foreach (@isis_dirs) {  
         if (-e $config->{isis_data}."/$db_dir/$_/LIBRI") {  
                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/LIBRI/LIBRI";  
203          }          }
204          if (-e $config->{isis_data}."/$db_dir/$_/PERI") {  
205                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/PERI/PERI";          # dump formatted output in <html>
206          }          if ($html) {
207          if (-e $config->{isis_data}."/$db_dir/$_/AMS") {                  $xml .= xmlify("html",$html);
                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";  
208          }          }
209          if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {          
210  #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";          if ($xml) {
211    #print STDERR "x: $xml\n";
212                    $xml .= $add_xml if ($add_xml);
213                    return "<xml>\n$xml</xml>\n";
214            } else {
215                    return;
216          }          }
217  }  }
218    
219  print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);  ##########################################################################
220    
221  my $db;  my $cfg = new Config::IniFiles( -file => $config_file );
222    
223  foreach my $isis_db (@isis_dbs) {  foreach my $database ($cfg->Sections) {
224    
225            my $isis_db = $cfg -> val($database, 'isis_db');
226            my $type = $cfg -> val($database, 'type');
227            my $add_xml = $cfg -> val($database, 'xml');
228    
229            # read configuration for this type
230            $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
231            $isis_codepage = Text::Iconv->new($config->{isis_codepage},$XML_CHARSET);
232            $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
233            $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),$XML_CHARSET);
234    
235          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
236          if (0) {          if (0) {
# Line 152  foreach my $isis_db (@isis_dbs) { Line 244  foreach my $isis_db (@isis_dbs) {
244    
245          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
246    
247            my $path = $database;                   # was $isis_db
248    
249          my $last_p = 0;          my $last_p = 0;
250    
251  #       { my $row_id = 1;  #       { my $row_id = 4514;
252  # FIX  # FIX
253          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
254                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
255                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
256    #print STDERR "mfn: ",$row->{mfn},"\n";
257                          # output current process indicator                          # output current process indicator
258                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
259                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 167  foreach my $isis_db (@isis_dbs) { Line 261  foreach my $isis_db (@isis_dbs) {
261                                  $last_p = $p;                                  $last_p = $p;
262                          }                          }
263    
264                          if (my $xml = isis2xml($row)) {                          if (my $xml = isis2xml($row,$add_xml)) {
265                                  print "Path-Name: $isis_db#".$row->{mfn}."\n";  #print STDERR "--ret-->$xml\n";
266                                    print "Path-Name: $path#".int($row->{mfn})."\n";
267                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
268                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
269                          }                          }
# Line 177  foreach my $isis_db (@isis_dbs) { Line 272  foreach my $isis_db (@isis_dbs) {
272          print STDERR "\n";          print STDERR "\n";
273  }  }
274    
275    # call this to commit index
276    $index->close;
277    
278  1;  1;
279  __END__  __END__

Legend:
Removed from v.4  
changed lines
  Added in v.29

  ViewVC Help
Powered by ViewVC 1.1.26