/[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 56 by dpavlin, Wed Jun 25 12:09:27 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    use Encode;
12    
13  my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);  $|=1;
14    
15    my $config_file = $0;
16    $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    my $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 $path;       # this is name of database
35    
36    Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
37    
38  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  # this is encoding of all files on disk, including import_xml/*.xml file and
39    # filter/*.pm files! It will be used to store strings in perl internally!
40    my $codepage = 'ISO-8859-2';
41    
42    my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
43    # this function will convert data from XML files to local encoding
44    sub x {
45            return $utf2cp->convert($_[0]);
46    }
47    
48    # decode isis/excel or other import codepage
49    my $import2cp;
50    
51  #print Dumper($config->{indexer});  # outgoing xml must be in UTF-8
52  #print "-" x 70,"\n";  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
53    
54  # how to convert isis code page to UTF8?  # mapping between data type and tag which specify
55  my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;  # format in XML file
56    my %type2tag = (
57            'isis' => 'isis',
58            'excel' => 'column'
59    );
60    
61  sub isis2xml {  sub data2xml {
62    
63            use xmlify;
64    
65            my $type = shift @_;
66          my $row = shift @_;          my $row = shift @_;
67            my $add_xml = shift @_;
68    
69          my $xml;          my $xml;
         $xml->{db_dir} = [ $db_dir ];   # FIX remove?  
70    
71          sub isis_sf {          use parse_format;
72                  my $row = shift @_;  
73                  my $isis_id = shift @_;          my $html = "";          # html formatted display output
74                  my $subfield = shift @_;  
75                  if ($row->{$isis_id}->[0]) {          my %field_usage;        # counter for usage of each field
76                          my $sf = OpenIsis::subfields($row->{$isis_id}->[0]);  
77                          if (! defined $subfield || length($subfield) == 0) {          # sort subrouting using order="" attribute
78                                  # subfield list undef, empty or no defined subfields for this record          sub by_order {
79                                  my $all_sf = $row->{$isis_id}->[0];                  return 0 if (! $config->{indexer}->{$a}->{order});
80                                  $all_sf =~ s/\^./ /g;   nuke definirions                  return 0 if (! $config->{indexer}->{$b}->{order});
81                                  return $all_sf;  
82                          } elsif ($sf->{$subfield}) {                  return $config->{indexer}->{$a}->{order} <=>
83                                  return $sf->{$subfield};                          $config->{indexer}->{$b}->{order} ;
                         }  
                 }  
84          }          }
85    
86          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (sort by_order keys %{$config->{indexer}}) {
87    
88                    $field=x($field);
89    
90                    $field_usage{$field}++;
91    
92                    my $swish_data = "";
93                  my $display_data = "";                  my $display_data = "";
94                  my $index_data = "";                  my $line_delimiter;
95    
96                    my ($swish,$display);
97    
98                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";
99                    foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
100    
101                          my $display_tmp = "";                          my $format = x($x->{content});
102                          my $index_tmp = "";                          my $delimiter = x($x->{delimiter}) || ' ';
103    
104                          my $format = $x->{content};                          my $repeat_off = 0;             # repeatable offset
105                          my $i = 1;      # index only  
106                          my $d = 1;      # display only                          my ($s,$d,$i) = (1,1,0);        # swish, display default
107                          $i = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
108                          $d = 0 if (lc($x->{type}) eq "index");                          $d = 0 if (lc($x->{type}) eq "swish");
109  #print "## i: $i d: $d ## $format ##";                            ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
110                          # parse format  
111                          my $prefix = "";                          # what will separate last line from this one?
112                          if ($format =~ s/^([^\d]+)//) {                          if ($display_data && $x->{append} && $x->{append} eq "1") {
113                                  $prefix = $1;                                  $line_delimiter = ' ';
114                          }                          } elsif ($display_data) {
115                          while ($format) {                                  $line_delimiter = '<br/>';
116                                  if ($format =~ s/^(\d\d\d)(\w?)//) {                          }
117                                          my $isis_tmp = isis_sf($row,$1,$2);  
118                                          if ($isis_tmp) {                          # init vars so that we go into while...
119  #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);                          ($swish,$display) = (1,1);
120                                                  $display_tmp .= $prefix . $isis_tmp if ($d);  
121                                                  $index_tmp .= $isis_tmp." " if ($i);                          if ($swish || $display) {
122  #print " $isis_tmp <--\n";                                  ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
123                                    # filter="name" ; filter this field through
124                                    # filter/[name].pm
125                                    my $filter = $x->{filter};
126                                    if ($filter) {
127                                            require "filter/".$filter.".pm";
128                                    }
129                                    # type="swish" ; field for swish
130                                    if ($s && $swish) {
131                                            if ($filter) {
132                                                    no strict 'refs';
133                                                    $swish_data .= join(" ",&$filter($swish));
134                                            } else {
135                                                    $swish_data .= $swish;
136                                            }
137                                    }
138    
139                                    # type="display" ; field for display
140                                    if ($d && $display) {
141                                            if ($line_delimiter && $display_data) {
142                                                    $display_data .= $line_delimiter;
143                                                    undef $line_delimiter;
144                                            }
145                                            if ($filter) {
146                                                    no strict 'refs';
147                                                    $display_data .= join($delimiter,&$filter($display));
148                                            } else {
149                                                    if ($display_data) {
150                                                            $display_data .= $delimiter.$display;
151                                                    } else {
152                                                            $display_data .= $display;
153                                                    }
154                                          }                                          }
155                                          $prefix = "";                                  }
156                                  } elsif ($format =~ s/^([^\d]+)//) {                                                  
157                                          $prefix = $1;                                  # type="index" ; insert into index
158                                    if ($i && $display) {
159                                            my $index_data = $display;
160                                            if ($filter) {
161                                                    no strict 'refs';
162                                                    foreach my $d (&$filter($index_data)) {
163                                                            $index->insert($field, $d, $path);
164                                                    }
165                                            } else {
166                                                    $index->insert($field, $index_data, $path);
167                                            }
168                                    }
169                            }
170                    }
171    
172    
173                    if ($display_data) {
174    
175                            if ($field eq "headline") {
176                                    $xml .= xmlify("headline", $display_data);
177                            } else {
178    
179                                    # find field name (signular, plural)
180                                    my $field_name = "";
181                                    if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
182                                            $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
183                                    } elsif ($config->{indexer}->{$field}->{name_plural}) {
184                                            $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
185                                    } elsif ($config->{indexer}->{$field}->{name}) {
186                                            $field_name = $config->{indexer}->{$field}->{name}."#-#";
187                                  } else {                                  } else {
188                                          print STDERR "WARNING: unparsed format '$format'\n";                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
189                                          last;                                  }
190                                  };                                  if ($field_name) {
191                          }                                          $html .= x($field_name);
192                          # add suffix                                  }
193                          $display_tmp .= $prefix if ($display_tmp);                                  $html .= $display_data."###\n";
194                            }
195  #                       $display_data .= $display_tmp if ($display_tmp ne "");                  }
196  #                       $index_data .= $index_tmp if ($index_tmp ne "");                  if ($swish_data) {
197                          $display_data .= $display_tmp;                          # remove extra spaces
198                          $index_data .= $index_tmp;                          $swish_data =~ s/ +/ /g;
199                            $swish_data =~ s/ +$//g;
200                  }  
201  #print "--display:$display_data\n--index:$index_data\n";                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
202                  #$xml->{$field."_display"} = $isis_map->tou($display_data)->utf8 if ($display_data);                  }
203                  #$xml->{$field."_index"} = unac_string($config->{isis_codepage},$index_data) if ($index_data);  
204                  $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data);  
205                  $xml->{$field."_index"} = [ unac_string($config->{isis_codepage},$index_data)." jabuka" ] if ($index_data);          }
206            
207            # dump formatted output in <html>
208            if ($html) {
209                    $xml .= xmlify("html",$html);
210          }          }
211            
212          if ($xml) {          if ($xml) {
213                  return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );                  $xml .= $add_xml if ($add_xml);
214                    return "<xml>\n$xml</xml>\n";
215          } else {          } else {
216                  return;                  return;
217          }          }
# Line 117  sub isis2xml { Line 219  sub isis2xml {
219    
220  ##########################################################################  ##########################################################################
221    
222  my $last_tell=0;  # read configuration for this script
223    my $cfg = new Config::IniFiles( -file => $config_file );
224    
225  my @isis_dirs = ( '.' );        # use dirname as database name  # read global.conf configuration
226    my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
227    
228  if ($opts{m}) {  # open index
229          @isis_dirs = split(/,/,$opts{m});  $index = new index_DBI(
230  }                  $cfg_global->val('global', 'dbi_dbd'),
231                    $cfg_global->val('global', 'dbi_dsn'),
232                    $cfg_global->val('global', 'dbi_user'),
233                    $cfg_global->val('global', 'dbi_passwd') || '',
234            );
235    
236  my @isis_dbs;  foreach my $database ($cfg->Sections) {
237    
238  foreach (@isis_dirs) {          my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
239          if (-e $config->{isis_data}."/$db_dir/$_/LIBRI") {          my $add_xml = $cfg -> val($database, 'xml');    # optional
240                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/LIBRI/LIBRI";  
241          }  print STDERR "reading ./import_xml/$type.xml\n";
242          if (-e $config->{isis_data}."/$db_dir/$_/PERI") {  
243                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/PERI/PERI";          $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type} ], forcecontent => 1);
244          }  
245          if (-e $config->{isis_data}."/$db_dir/$_/AMS") {          # output current progress indicator
246                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";          my $last_p = 0;
247          }          sub progress {
248          if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {                  #return if (! $opts{q});        # FIXME
249  #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";                  my $current = shift;
250                    my $total = shift;
251                    my $p = int($current * 100 / $total);
252                    if ($p != $last_p) {
253                            printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );
254                            $last_p = $p;
255                    }
256          }          }
 }  
257    
258  print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);          # now read database
259    print STDERR "using: $type...\n";
260    
261  my $db;          if ($type eq "isis") {
262                    my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
263    
264  foreach my $isis_db (@isis_dbs) {                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
265                    my $db = OpenIsis::open( $isis_db );
266    
267                    my $max_rowid = OpenIsis::maxRowid( $db );
268    
269          my $db = OpenIsis::open( $isis_db );                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
         if (0) {  
 #       # FIX  
 #       if (! $db ) {  
                 print STDERR "WARNING: can't open '$isis_db'\n";  
                 next ;  
         }  
270    
271          my $max_rowid = OpenIsis::maxRowid( $db );                  my $path = $database;
272    
273          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";                  for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
274                            my $row = OpenIsis::read( $db, $row_id );
275                            if ($row && $row->{mfn}) {
276            
277                                    progress($row->{mfn}, $max_rowid);
278    
279          my $last_p = 0;                                  my $swishpath = $path."#".int($row->{mfn});
280    
281                                    if (my $xml = data2xml($type,$row,$add_xml)) {
282                                            $xml = $cp2utf->convert($xml);
283                                            use bytes;      # as opposed to chars
284                                            print "Path-Name: $swishpath\n";
285                                            print "Content-Length: ".(length($xml)+1)."\n";
286                                            print "Document-Type: XML\n\n$xml\n";
287                                    }
288                            }
289                    }
290                    print STDERR "\n";
291    
292            } elsif ($type eq "excel") {
293                    use Spreadsheet::ParseExcel;
294                    use Spreadsheet::ParseExcel::Utility qw(int2col);
295                    
296                    $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);
297                    my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
298    
299                    my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
300                    my $start_row = x($config->{start_row}) - 1 || die "no start_row in $type.xml";
301    
302                    my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
303    
304                    my $sheet_nr = 0;
305                    foreach my $oWks (@{$oBook->{Worksheet}}) {
306                            #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";
307                            last if ($oWks->{Name} eq $sheet);
308                            $sheet_nr++;
309                    }
310    
311                    my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];
312            
313                    print STDERR "using sheet: ",$oWorksheet->{Name},"\n";
314                    defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file";
315                    my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow};
316    
317                    for(my $iR = $start_row ; defined $end_row && $iR <= $end_row ; $iR++) {
318                            my $row;
319                            for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
320                                    my $cell = $oWorksheet->{Cells}[$iR][$iC];
321                                    if ($cell) {
322                                            $row->{int2col($iC)} = $cell->Value;
323                                    }
324                            }
325    
326                            progress($iR, $end_row);
327    
328  #       { my $row_id = 1;  #                       print "row[$iR/$end_row] ";
329  # FIX  #                       foreach (keys %{$row}) {
330          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {  #                               print "$_: ",$row->{$_},"\t";
331                  my $row = OpenIsis::read( $db, $row_id );  #                       }
332                  if ($row && $row->{mfn}) {  #                       print "\n";
333    
334                          # output current process indicator                          my $swishpath = $database."#".$iR;
335                          my $p = int($row->{mfn} * 100 / $max_rowid);  
336                          if ($p != $last_p) {                          next if (! $row);
337                                  printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});  
338                                  $last_p = $p;                          if (my $xml = data2xml($type,$row,$add_xml)) {
339                          }                                  $xml = $cp2utf->convert($xml);
340                                    use bytes;      # as opposed to chars
341                          if (my $xml = isis2xml($row)) {                                  print "Path-Name: $swishpath\n";
342                                  my $path = $isis_db;                                  print "Content-Length: ".(length($xml)+1)."\n";
343                                  $path =~ s#$config->{isis_data}/*##g;                                  print "Document-Type: XML\n\n$xml\n";
                                 my $out = "Path-Name: $path#".$row->{mfn}."\n";  
                                 $out .= "Content-Length: ".(length($xml)+1)."\n";  
                                 $out .= "Document-Type: XML\n\n$xml\n";  
                                 print $out;  
344                          }                          }
345                  }                  }
346          }          }
         print STDERR "\n";  
347  }  }
348    
349    # call this to commit index
350    $index->close;
351    
352  1;  1;
353  __END__  __END__

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

  ViewVC Help
Powered by ViewVC 1.1.26