/[webpac]/trunk2/all2all.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 /trunk2/all2all.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 40 by dpavlin, Sat Mar 15 21:33:36 2003 UTC revision 56 by dpavlin, Wed Jun 25 12:09:27 2003 UTC
# Line 19  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 32  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
35    
36  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
37    
# Line 46  sub x { Line 45  sub x {
45          return $utf2cp->convert($_[0]);          return $utf2cp->convert($_[0]);
46  }  }
47    
48  # decode isis import codepage  # decode isis/excel or other import codepage
49  my $isis2cp;  my $import2cp;
50    
51  # outgoing xml must be in UTF-8  # outgoing xml must be in UTF-8
52  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
53    
54  sub isis2xml {  # 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 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 88  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}}) {
100    
101                          my $format = x($x->{content});                          my $format = x($x->{content});
102                          my $delimiter = x($x->{delimiter}) || ' ';                          my $delimiter = x($x->{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");
# Line 110  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++,$isis2cp);                                  ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
   
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 153  sub isis2xml { Line 160  sub isis2xml {
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                          }                          }
# Line 212  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') || 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";  
239          my $add_xml = $cfg -> val($database, 'xml');    # optional          my $add_xml = $cfg -> val($database, 'xml');    # optional
240    
241          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);  print STDERR "reading ./import_xml/$type.xml\n";
242    
243          $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);          $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type} ], forcecontent => 1);
244    
245          my $db = OpenIsis::open( $isis_db );          # output current progress indicator
246          if (0) {          my $last_p = 0;
247  #       # FIX          sub progress {
248  #       if (! $db ) {                  #return if (! $opts{q});        # FIXME
249                  print STDERR "WARNING: can't open '$isis_db'\n";                  my $current = shift;
250                  next ;                  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          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 $path = $database;
272    
273                    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 $swishpath = $path."#".int($row->{mfn});
280    
281  #       { my $row_id = 4514;                                  if (my $xml = data2xml($type,$row,$add_xml)) {
282  # FIX                                          $xml = $cp2utf->convert($xml);
283          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                                          use bytes;      # as opposed to chars
284                  my $row = OpenIsis::read( $db, $row_id );                                          print "Path-Name: $swishpath\n";
285                  if ($row && $row->{mfn}) {                                          print "Content-Length: ".(length($xml)+1)."\n";
286                          # output current process indicator                                          print "Document-Type: XML\n\n$xml\n";
287                          my $p = int($row->{mfn} * 100 / $max_rowid);                                  }
288                          if ($p != $last_p) {                          }
289                                  printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});                  }
290                                  $last_p = $p;                  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 = $cp2utf->convert(isis2xml($row,$add_xml))) {                          progress($iR, $end_row);
327    
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.40  
changed lines
  Added in v.56

  ViewVC Help
Powered by ViewVC 1.1.26