/[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 42 by dpavlin, Sat Mar 15 21:48:48 2003 UTC revision 58 by dpavlin, Fri Jul 4 16:56:40 2003 UTC
# Line 18  die "FATAL: can't find configuration fil Line 18  die "FATAL: can't find configuration fil
18    
19  my $config;  my $config;
20    
21  use index_DBI;  # there is no other, right now ;-)  #use index_DBI;         # default DBI module for index
22    use index_DBI_cache;    # faster DBI module using memory cache
23  my $index = new index_DBI();    # open index  my $index;
24    
25  my %opts;  my %opts;
26    
# Line 32  my %opts; Line 32  my %opts;
32    
33  getopts('d:m:qs', \%opts);  getopts('d:m:qs', \%opts);
34    
35  my $db_dir;  my $path;       # this is name of database
36    
37  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  Text::Iconv->raise_error(0);     # Conversion errors don't raise exceptions
38    
39  # this is encoding of all files on disk, including import_xml/*.xml file and  # this is encoding of all files on disk, including import_xml/*.xml file and
40  # filter/*.pm files! It will be used to store strings in perl internally!  # filter/*.pm files! It will be used to store strings in perl internally!
# Line 46  sub x { Line 46  sub x {
46          return $utf2cp->convert($_[0]);          return $utf2cp->convert($_[0]);
47  }  }
48    
49  # decode isis import codepage  # decode isis/excel or other import codepage
50  my $isis2cp;  my $import2cp;
51    
52  # outgoing xml must be in UTF-8  # outgoing xml must be in UTF-8
53  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
54    
55  sub isis2xml {  # mapping between data type and tag which specify
56    # format in XML file
57    my %type2tag = (
58            'isis' => 'isis',
59            'excel' => 'column'
60    );
61    
62    sub data2xml {
63    
64          use xmlify;          use xmlify;
65    
66            my $type = shift @_;
67          my $row = shift @_;          my $row = shift @_;
68          my $add_xml = shift @_;          my $add_xml = shift @_;
69    
# Line 88  sub isis2xml { Line 96  sub isis2xml {
96    
97                  my ($swish,$display);                  my ($swish,$display);
98    
99                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";
100                    foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
101    
102                          my $format = x($x->{content});                          my $format = x($x->{content});
103                          my $delimiter = x($x->{delimiter}) || ' ';                          my $delimiter = x($x->{delimiter}) || ' ';
104    
105                          my $isis_i = 0;         # isis repeatable offset                          my $repeat_off = 0;             # repeatable offset
106    
107                          my ($s,$d,$i) = (1,1,0);        # swish, display default                          my ($s,$d,$i) = (1,1,0);        # swish, display default
108                          $s = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
# Line 110  sub isis2xml { Line 119  sub isis2xml {
119                          # init vars so that we go into while...                          # init vars so that we go into while...
120                          ($swish,$display) = (1,1);                          ($swish,$display) = (1,1);
121    
122                          while ($swish || $display) {                          if ($swish || $display) {
123                                  ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);                                  ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
   
124                                  # filter="name" ; filter this field through                                  # filter="name" ; filter this field through
125                                  # filter/[name].pm                                  # filter/[name].pm
126                                  my $filter = $x->{filter};                                  my $filter = $x->{filter};
# Line 124  sub isis2xml { Line 132  sub isis2xml {
132                                          if ($filter) {                                          if ($filter) {
133                                                  no strict 'refs';                                                  no strict 'refs';
134                                                  $swish_data .= join(" ",&$filter($swish));                                                  $swish_data .= join(" ",&$filter($swish));
 print STDERR "#### $swish_data\n";  
135                                          } else {                                          } else {
136                                                  $swish_data .= $swish;                                                  $swish_data .= $swish;
137                                          }                                          }
# Line 154  print STDERR "#### $swish_data\n"; Line 161  print STDERR "#### $swish_data\n";
161                                          if ($filter) {                                          if ($filter) {
162                                                  no strict 'refs';                                                  no strict 'refs';
163                                                  foreach my $d (&$filter($index_data)) {                                                  foreach my $d (&$filter($index_data)) {
164                                                          $index->insert($field, $d, $db_dir);                                                          $index->insert($field, $d, $path);
165                                                  }                                                  }
166                                          } else {                                          } else {
167                                                  $index->insert($field, $index_data, $db_dir);                                                  $index->insert($field, $index_data, $path);
168                                          }                                          }
169                                  }                                  }
170                          }                          }
# Line 213  print STDERR "#### $swish_data\n"; Line 220  print STDERR "#### $swish_data\n";
220    
221  ##########################################################################  ##########################################################################
222    
223    # read configuration for this script
224  my $cfg = new Config::IniFiles( -file => $config_file );  my $cfg = new Config::IniFiles( -file => $config_file );
225    
226    # read global.conf configuration
227    my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
228    
229    # open index
230    $index = new index_DBI(
231                    $cfg_global->val('global', 'dbi_dbd'),
232                    $cfg_global->val('global', 'dbi_dsn'),
233                    $cfg_global->val('global', 'dbi_user'),
234                    $cfg_global->val('global', 'dbi_passwd') || '',
235            );
236    
237  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
238    
239          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";          my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
         my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";  
240          my $add_xml = $cfg -> val($database, 'xml');    # optional          my $add_xml = $cfg -> val($database, 'xml');    # optional
241    
242          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);  print STDERR "reading ./import_xml/$type.xml\n";
243    
244            # extract just type basic
245            my $type_base = $type;
246            $type_base =~ s/_.+$//g;
247    
248          $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);          $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type_base} ], forcecontent => 1);
249    
250          my $db = OpenIsis::open( $isis_db );          # output current progress indicator
251          if (0) {          my $last_p = 0;
252  #       # FIX          sub progress {
253  #       if (! $db ) {                  #return if (! $opts{q});        # FIXME
254                  print STDERR "WARNING: can't open '$isis_db'\n";                  my $current = shift;
255                  next ;                  my $total = shift || 1;
256                    my $p = int($current * 100 / $total);
257                    if ($p != $last_p) {
258                            printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );
259                            $last_p = $p;
260                    }
261          }          }
262    
263          my $max_rowid = OpenIsis::maxRowid( $db );          # now read database
264    print STDERR "using: $type...\n";
265    
266          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          if ($type_base eq "isis") {
267    
268          my $path = $database;                   # was $isis_db                  my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
269    
270          my $last_p = 0;                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
271                    my $db = OpenIsis::open( $isis_db );
272    
273                    my $max_rowid = OpenIsis::maxRowid( $db );
274    
275                    print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
276    
277                    my $path = $database;
278    
279                    for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
280                            my $row = OpenIsis::read( $db, $row_id );
281                            if ($row && $row->{mfn}) {
282            
283                                    progress($row->{mfn}, $max_rowid);
284    
285  #       { my $row_id = 4514;                                  my $swishpath = $path."#".int($row->{mfn});
286  # FIX  
287          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                                  if (my $xml = data2xml($type_base,$row,$add_xml)) {
288                  my $row = OpenIsis::read( $db, $row_id );                                          $xml = $cp2utf->convert($xml);
289                  if ($row && $row->{mfn}) {                                          use bytes;      # as opposed to chars
290                          # output current process indicator                                          print "Path-Name: $swishpath\n";
291                          my $p = int($row->{mfn} * 100 / $max_rowid);                                          print "Content-Length: ".(length($xml)+1)."\n";
292                          if ($p != $last_p) {                                          print "Document-Type: XML\n\n$xml\n";
293                                  printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});                                  }
                                 $last_p = $p;  
294                          }                          }
295                    }
296                    print STDERR "\n";
297    
298            } elsif ($type_base eq "excel") {
299                    use Spreadsheet::ParseExcel;
300                    use Spreadsheet::ParseExcel::Utility qw(int2col);
301                    
302                    $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);
303                    my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
304    
305                    my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
306                    my $start_row = x($config->{start_row}) - 1 || die "no start_row in $type.xml";
307    
308                    my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
309    
310                    my $sheet_nr = 0;
311                    foreach my $oWks (@{$oBook->{Worksheet}}) {
312                            #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";
313                            last if ($oWks->{Name} eq $sheet);
314                            $sheet_nr++;
315                    }
316    
317                    my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];
318            
319                    print STDERR "using sheet: ",$oWorksheet->{Name},"\n";
320                    defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file";
321                    my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow};
322    
323                    for(my $iR = $start_row ; defined $end_row && $iR <= $end_row ; $iR++) {
324                            my $row;
325                            for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
326                                    my $cell = $oWorksheet->{Cells}[$iR][$iC];
327                                    if ($cell) {
328                                            $row->{int2col($iC)} = $cell->Value;
329                                    }
330                            }
331    
332                            progress($iR, $end_row);
333    
334    #                       print "row[$iR/$end_row] ";
335    #                       foreach (keys %{$row}) {
336    #                               print "$_: ",$row->{$_},"\t";
337    #                       }
338    #                       print "\n";
339    
340                            my $swishpath = $database."#".$iR;
341    
342                            next if (! $row);
343    
344                          if (my $xml = $cp2utf->convert(isis2xml($row,$add_xml))) {                          if (my $xml = data2xml($type,$row,$add_xml)) {
345                                    $xml = $cp2utf->convert($xml);
346                                  use bytes;      # as opposed to chars                                  use bytes;      # as opposed to chars
347                                  print "Path-Name: $path#".int($row->{mfn})."\n";                                  print "Path-Name: $swishpath\n";
348                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
349                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
350                          }                          }
351                  }                  }
352          }          }
         print STDERR "\n";  
353  }  }
354    
355  # call this to commit index  # call this to commit index

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

  ViewVC Help
Powered by ViewVC 1.1.26