/[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 7 by dpavlin, Sat Jan 11 16:44:03 2003 UTC revision 21 by dpavlin, Sun Feb 23 00:00:51 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 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    
# Line 20  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                    $field_usage{$field}++;
68    
69                    my $swish_data = "";
70                  my $display_data = "";                  my $display_data = "";
                 my $index_data = "";  
71    
72                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
73    
                         my $display_tmp = "";  
                         my $index_tmp = "";  
   
74                          my $format = $x->{content};                          my $format = $x->{content};
75                          my $i = 1;      # index only                          my ($s,$d,$i) = (1,1,0);        # swish, display default
76                          my $d = 1;      # display only                          $s = 0 if (lc($x->{type}) eq "display");
77                          $i = 0 if (lc($x->{type}) eq "display");                          $d = 0 if (lc($x->{type}) eq "swish");
78                          $d = 0 if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
79  #print "## i: $i d: $d ## $format ##";    #print STDERR "## s: $s d: $d i: $i ## $format ##\n";  
80                          # parse format  
81                          my $prefix = "";                          # FIX: this is ugly, UGLY, cludge string is returned
82                          if ($format =~ s/^([^\d]+)//) {                          # in UTF8 encoding , but as if source charset
83                                  $prefix = $1;                          # is ISO8859-1 and not some other. This break other
84                          }                          # character encodings, so we convert it first
85                          while ($format) {                          # back to ISO8859-1
86                                  if ($format =~ s/^(\d\d\d)(\w?)//) {                          $format = $cludge_codepage->convert($format);
87                                          my $isis_tmp = isis_sf($row,$1,$2);  
88                                          if ($isis_tmp) {                          my ($swish,$display) = parse_format($format,$row);
89  #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);  #print STDERR "s: $swish\nd: $display\n" if ($swish);
90                                                  $display_tmp .= $prefix . $isis_tmp if ($d);  
91                                                  $index_tmp .= $isis_tmp." " if ($i);  #print STDERR "swish: $swish<-- display: $display<--\n";
92  #print " $isis_tmp <--\n";  
93                                          }                          # filter="name" ; filter this field through
94                                          $prefix = "";                          # filter/[name].pm
95                                  } elsif ($format =~ s/^([^\d]+)//) {                          my $filter;
96                                          $prefix = $1;                          if ($x->{filter}) {
97                                    $filter = "filter/".$x->{filter}.".pm";
98                                    require $filter;
99                            }
100                            # type="swish" ; field for swish
101                            if ($s && $swish) {
102                                    if ($filter) {
103                                            $swish_data .= join(" ",&filter($swish));
104                                  } else {                                  } else {
105                                          print STDERR "WARNING: unparsed format '$format'\n";                                          $swish_data .= $swish if ($s && $swish);
106                                          last;                                  }
107                                  };                          }
                         }  
                         # add suffix  
                         $display_tmp .= $prefix if ($display_tmp);  
   
 #                       $display_data .= $display_tmp if ($display_tmp ne "");  
 #                       $index_data .= $index_tmp if ($index_tmp ne "");  
                         $display_data .= $display_tmp;  
                         $index_data .= $index_tmp;  
108    
109                            # type="display" ; field for display
110                            if ($d && $display) {
111                                    if ($filter) {
112                                            $display_data .= join(" ",&filter($display));
113                                    } else {
114                                            $display_data .= $display;
115                                    }
116                            }
117                                            
118                            # type="index" ; insert into index
119                            if ($i && $display) {
120                                    my $index_data = $index_codepage->convert($display) || $display;
121                                    if ($filter) {
122                                            foreach my $d (&filter($index_data)) {
123                                                    $index->insert($field, $d, $db_dir);
124                                            }
125                                    } else {
126                                            $index->insert($field, $index_data, $db_dir);
127                                    }
128                            }
129                  }                  }
 #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);  
                 $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data);  
                 $xml->{$field."_index"} = [ unac_string($config->{isis_codepage},$index_data)." jabuka" ] if ($index_data);  
           
         }  
         if ($xml) {  
                 return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );  
         } else {  
                 return;  
         }  
 }  
130    
 ##########################################################################  
131    
132  my $last_tell=0;  #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
133                    if ($display_data) {
134  my @isis_dirs = ( '.' );        # use dirname as database name                          $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";
135                            # FIX: this is removed and replaced by html tag.
136  if ($opts{m}) {                          #$xml .= xmlify($field."_display", $display_data);
137          @isis_dirs = split(/,/,$opts{m});  
138  }                          if ($field eq "headline") {
139                                    $xml .= xmlify("headline", $display_data);
140                            } else {
141    
142                                    # find field name (signular, plural)
143                                    my $field_name = "";
144                                    if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
145                                            $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
146                                    } elsif ($config->{indexer}->{$field}->{name_plural}) {
147                                            $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
148                                    } elsif ($config->{indexer}->{$field}->{name}) {
149                                            $field_name = $config->{indexer}->{$field}->{name}."#-#";
150                                    } else {
151                                            print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
152                                    }
153                                    if ($field_name) {
154                                            $html .= $xml_codepage->convert($field_name);
155                                    }
156                                    $html .= $display_data."###\n";
157                            }
158                    }
159                    if ($swish_data) {
160                            my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
161                            # remove extra spaces
162                            $swish_data =~ s/ +/ /g;
163                            $swish_data =~ s/ +$//g;
164    
165                            $swish_data = $i->convert($swish_data);
166                            $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
167                            #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
168                            #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));
169                    }
170    
 my @isis_dbs;  
171    
 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";  
172          }          }
173          if (-e $config->{isis_data}."/$db_dir/$_/AMS") {  
174                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";          # dump formatted output in <html>
175            if ($html) {
176                    $xml .= xmlify("html",$html);
177          }          }
178          if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {          
179  #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";          if ($xml) {
180    #print STDERR "x: $xml\n";
181                    $xml .= $add_xml if ($add_xml);
182                    return "<xml>\n$xml</xml>\n";
183            } else {
184                    return;
185          }          }
186  }  }
187    
188  print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);  ##########################################################################
189    
190  my $db;  my $cfg = new Config::IniFiles( -file => $config_file );
191    
192  foreach my $isis_db (@isis_dbs) {  foreach my $database ($cfg->Sections) {
193    
194            my $isis_db = $cfg -> val($database, 'isis_db');
195            my $type = $cfg -> val($database, 'type');
196            my $add_xml = $cfg -> val($database, 'xml');
197    
198            # read configuration for this type
199            $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
200            $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');
201            $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
202            $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),'UTF8');
203    
204          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
205          if (0) {          if (0) {
# Line 161  foreach my $isis_db (@isis_dbs) { Line 213  foreach my $isis_db (@isis_dbs) {
213    
214          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
215    
216            my $path = $database;                   # was $isis_db
217    
218          my $last_p = 0;          my $last_p = 0;
219    
220  #       { my $row_id = 1;  #       { my $row_id = 4514;
221  # FIX  # FIX
222          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
223                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
224                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
225    #print STDERR "mfn: ",$row->{mfn},"\n";
226                          # output current process indicator                          # output current process indicator
227                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
228                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 176  foreach my $isis_db (@isis_dbs) { Line 230  foreach my $isis_db (@isis_dbs) {
230                                  $last_p = $p;                                  $last_p = $p;
231                          }                          }
232    
233                          if (my $xml = isis2xml($row)) {                          if (my $xml = isis2xml($row,$add_xml)) {
234                                  my $path = $isis_db;  #print STDERR "--ret-->$xml\n";
235                                  $path =~ s#$config->{isis_data}/*##g;                                  print "Path-Name: $path#".int($row->{mfn})."\n";
236                                  my $out = "Path-Name: $path#".$row->{mfn}."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
237                                  $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;  
238                          }                          }
239                  }                  }
240          }          }
241          print STDERR "\n";          print STDERR "\n";
242  }  }
243    
244    # call this to commit index
245    $index->close;
246    
247  1;  1;
248  __END__  __END__

Legend:
Removed from v.7  
changed lines
  Added in v.21

  ViewVC Help
Powered by ViewVC 1.1.26