/[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 35 by dpavlin, Sun Feb 23 15:47:40 2003 UTC revision 57 by dpavlin, Fri Jul 4 15:05:23 2003 UTC
# Line 8  use XML::Simple; Line 8  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  use Text::Iconv;  use Text::Iconv;
10  use Config::IniFiles;  use Config::IniFiles;
11    use Encode;
12    
13  $|=1;  $|=1;
14    
# Line 18  die "FATAL: can't find configuration fil Line 19  die "FATAL: can't find configuration fil
19  my $config;  my $config;
20    
21  use index_DBI;  # there is no other, right now ;-)  use index_DBI;  # there is no other, right now ;-)
22    my $index;
 my $index = new index_DBI();    # open index  
23    
24  my %opts;  my %opts;
25    
# Line 31  my %opts; Line 31  my %opts;
31    
32  getopts('d:m:qs', \%opts);  getopts('d:m:qs', \%opts);
33    
34  my $db_dir;  my $path;       # this is name of database
   
 #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  
35    
36  #print Dumper($config->{indexer});  Text::Iconv->raise_error(0);     # Conversion errors don't raise exceptions
 #print "-" x 70,"\n";  
37    
38  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  # 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  my $isis_codepage;  # decode isis/excel or other import codepage
49  my $index_codepage;  my $import2cp;
 my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');  
 my $xml_codepage;  
50    
51  my $XML_CHARSET = 'UTF8';  # outgoing xml must be in UTF-8
52    my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
53    
54    # mapping between data type and tag which specify
55    # format in XML file
56    my %type2tag = (
57            'isis' => 'isis',
58            'excel' => 'column'
59    );
60    
61  sub isis2xml {  sub data2xml {
62    
63          use xmlify;          use xmlify;
64    
65            my $type = shift @_;
66          my $row = shift @_;          my $row = shift @_;
67          my $add_xml = shift @_;          my $add_xml = shift @_;
68    
# Line 74  sub isis2xml { Line 85  sub isis2xml {
85    
86          foreach my $field (sort by_order keys %{$config->{indexer}}) {          foreach my $field (sort by_order keys %{$config->{indexer}}) {
87    
88                    $field=x($field);
89    
90                  $field_usage{$field}++;                  $field_usage{$field}++;
91    
92                  my $swish_data = "";                  my $swish_data = "";
# Line 82  sub isis2xml { Line 95  sub isis2xml {
95    
96                  my ($swish,$display);                  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}}) {
                         my $format = $x->{content};  
                         my $delimiter = $x->{delimiter} || ' ';  
100    
101                          # FIX: this is ugly, UGLY, cludge string is returned                          my $format = x($x->{content});
102                          # in UTF8 encoding , but as if source charset                          my $delimiter = x($x->{delimiter}) || ' ';
                         # is ISO8859-1 and not some other. This break other  
                         # character encodings, so we convert it first  
                         # back to ISO8859-1  
                         $format = $xml_codepage->convert($format);  
                         $delimiter = $xml_codepage->convert($delimiter) if ($delimiter);  
103    
104                          my $isis_i = 0;         # isis repeatable offset                          my $repeat_off = 0;             # repeatable offset
105    
106                          my ($s,$d,$i) = (1,1,0);        # swish, display default                          my ($s,$d,$i) = (1,1,0);        # swish, display default
107                          $s = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
108                          $d = 0 if (lc($x->{type}) eq "swish");                          $d = 0 if (lc($x->{type}) eq "swish");
109                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
 #print STDERR "## s: $s d: $d i: $i ## $format ##\n";    
110    
111                          # what will separate last line from this one?                          # what will separate last line from this one?
112                          if ($display_data && $x->{append} && $x->{append} eq "1") {                          if ($display_data && $x->{append} && $x->{append} eq "1") {
# Line 113  sub isis2xml { Line 118  sub isis2xml {
118                          # init vars so that we go into while...                          # init vars so that we go into while...
119                          ($swish,$display) = (1,1);                          ($swish,$display) = (1,1);
120    
121                          while ($swish || $display) {                          if ($swish || $display) {
122                                  ($swish,$display) = parse_format($format,$row,$isis_i++);                                  ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
 #print STDERR "s: $swish\nd: $display\n" if ($swish);  
   
 #print STDERR "swish: $swish<-- display: $display<--\n";  
   
123                                  # filter="name" ; filter this field through                                  # filter="name" ; filter this field through
124                                  # filter/[name].pm                                  # filter/[name].pm
125                                  my $filter = $x->{filter};                                  my $filter = $x->{filter};
# Line 128  sub isis2xml { Line 129  sub isis2xml {
129                                  # type="swish" ; field for swish                                  # type="swish" ; field for swish
130                                  if ($s && $swish) {                                  if ($s && $swish) {
131                                          if ($filter) {                                          if ($filter) {
 #print STDERR "using filter '$filter'\n";  
132                                                  no strict 'refs';                                                  no strict 'refs';
133                                                  $swish_data .= join(" ",&$filter($swish));                                                  $swish_data .= join(" ",&$filter($swish));
134                                          } else {                                          } else {
# Line 156  sub isis2xml { Line 156  sub isis2xml {
156                                                                                                    
157                                  # type="index" ; insert into index                                  # type="index" ; insert into index
158                                  if ($i && $display) {                                  if ($i && $display) {
159                                          my $index_data = $index_codepage->convert($display) || $display;                                          my $index_data = $display;
160                                          if ($filter) {                                          if ($filter) {
161                                                  no strict 'refs';                                                  no strict 'refs';
162                                                  foreach my $d (&$filter($index_data)) {                                                  foreach my $d (&$filter($index_data)) {
163                                                          $index->insert($field, $d, $db_dir);                                                          $index->insert($field, $d, $path);
164                                                  }                                                  }
165                                          } else {                                          } else {
166                                                  $index->insert($field, $index_data, $db_dir);                                                  $index->insert($field, $index_data, $path);
167                                          }                                          }
168                                  }                                  }
169                          }                          }
170                  }                  }
171    
172    
 #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);  
173                  if ($display_data) {                  if ($display_data) {
174    
 #                       $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";  
                         # FIX: this is removed and replaced by html tag.  
                         #$xml .= xmlify($field."_display", $display_data);  
   
175                          if ($field eq "headline") {                          if ($field eq "headline") {
176                                  $xml .= xmlify("headline", $display_data);                                  $xml .= xmlify("headline", $display_data);
177                          } else {                          } else {
# Line 193  sub isis2xml { Line 188  sub isis2xml {
188                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
189                                  }                                  }
190                                  if ($field_name) {                                  if ($field_name) {
191                                          $html .= $xml_codepage->convert($field_name);                                          $html .= x($field_name);
192                                  }                                  }
193                                  $html .= $display_data."###\n";                                  $html .= $display_data."###\n";
194                          }                          }
195                  }                  }
196                  if ($swish_data) {                  if ($swish_data) {
                         my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');  
197                          # remove extra spaces                          # remove extra spaces
198                          $swish_data =~ s/ +/ /g;                          $swish_data =~ s/ +/ /g;
199                          $swish_data =~ s/ +$//g;                          $swish_data =~ s/ +$//g;
200    
201                          $swish_data = $i->convert($swish_data);                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
                         $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));  
                         #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;  
                         #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));  
202                  }                  }
203    
204    
# Line 219  sub isis2xml { Line 210  sub isis2xml {
210          }          }
211                    
212          if ($xml) {          if ($xml) {
 #print STDERR "x: $xml\n";  
213                  $xml .= $add_xml if ($add_xml);                  $xml .= $add_xml if ($add_xml);
214                  return "<xml>\n$xml</xml>\n";                  return "<xml>\n$xml</xml>\n";
215          } else {          } else {
# Line 229  sub isis2xml { Line 219  sub isis2xml {
219    
220  ##########################################################################  ##########################################################################
221    
222    # read configuration for this script
223  my $cfg = new Config::IniFiles( -file => $config_file );  my $cfg = new Config::IniFiles( -file => $config_file );
224    
225    # read global.conf configuration
226    my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
227    
228    # open index
229    $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  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
237    
238          my $isis_db = $cfg -> val($database, 'isis_db');          my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
239          my $type = $cfg -> val($database, 'type');          my $add_xml = $cfg -> val($database, 'xml');    # optional
240          my $add_xml = $cfg -> val($database, 'xml');  
241    print STDERR "reading ./import_xml/$type.xml\n";
242          # read configuration for this type  
243          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);          $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type} ], forcecontent => 1);
244          $isis_codepage = Text::Iconv->new($config->{isis_codepage},$XML_CHARSET);  
245          $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});          # output current progress indicator
246          $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),$XML_CHARSET);          my $last_p = 0;
247            sub progress {
248          my $db = OpenIsis::open( $isis_db );                  #return if (! $opts{q});        # FIXME
249          if (0) {                  my $current = shift;
250  #       # FIX                  my $total = shift || 1;
251  #       if (! $db ) {                  my $p = int($current * 100 / $total);
252                  print STDERR "WARNING: can't open '$isis_db'\n";                  if ($p != $last_p) {
253                  next ;                          printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );
254                            $last_p = $p;
255                    }
256          }          }
257    
258          my $max_rowid = OpenIsis::maxRowid( $db );          # now read database
259    print STDERR "using: $type...\n";
260    
261          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          if ($type eq "isis") {
262                    my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
263    
264          my $path = $database;                   # was $isis_db                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
265                    my $db = OpenIsis::open( $isis_db );
266    
267          my $last_p = 0;                  my $max_rowid = OpenIsis::maxRowid( $db );
268    
269                    print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
270    
271  #       { my $row_id = 4514;                  my $path = $database;
272  # FIX  
273          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                  for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
274                  my $row = OpenIsis::read( $db, $row_id );                          my $row = OpenIsis::read( $db, $row_id );
275                  if ($row && $row->{mfn}) {                          if ($row && $row->{mfn}) {
276  #print STDERR "mfn: ",$row->{mfn},"\n";          
277                          # output current process indicator                                  progress($row->{mfn}, $max_rowid);
278                          my $p = int($row->{mfn} * 100 / $max_rowid);  
279                          if ($p != $last_p) {                                  my $swishpath = $path."#".int($row->{mfn});
280                                  printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});  
281                                  $last_p = $p;                                  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                          if (my $xml = isis2xml($row,$add_xml)) {                          progress($iR, $end_row);
327  #print STDERR "--ret-->$xml\n";  
328    #                       print "row[$iR/$end_row] ";
329    #                       foreach (keys %{$row}) {
330    #                               print "$_: ",$row->{$_},"\t";
331    #                       }
332    #                       print "\n";
333    
334                            my $swishpath = $database."#".$iR;
335    
336                            next if (! $row);
337    
338                            if (my $xml = data2xml($type,$row,$add_xml)) {
339                                    $xml = $cp2utf->convert($xml);
340                                  use bytes;      # as opposed to chars                                  use bytes;      # as opposed to chars
341                                  print "Path-Name: $path#".int($row->{mfn})."\n";                                  print "Path-Name: $swishpath\n";
342                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
343                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
344                          }                          }
345                  }                  }
346          }          }
         print STDERR "\n";  
347  }  }
348    
349  # call this to commit index  # call this to commit index

Legend:
Removed from v.35  
changed lines
  Added in v.57

  ViewVC Help
Powered by ViewVC 1.1.26