/[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 188 by dpavlin, Sat Nov 29 19:07:00 2003 UTC revision 678 by dpavlin, Sun Feb 27 23:07:35 2005 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  use strict;
4  use OpenIsis;  use Biblio::Isis;
5  use Getopt::Std;  use Getopt::Std;
6  use Data::Dumper;  use Data::Dumper;
7  use XML::Simple;  use XML::Simple;
 use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,  
8  use Text::Iconv;  use Text::Iconv;
9  use Config::IniFiles;  use Config::IniFiles;
10  use Encode;  use Encode;
11  #use GDBM_File;  #use GDBM_File;
12  use Fcntl;      # for O_RDWR  use Fcntl;      # for O_RDWR
13  use TDB_File;  use TDB_File;
14    use Carp;
15    
16  $|=1;  $|=1;
17    
18  my $config_file = $0;  my $config_file = $0;
19  $config_file =~ s/\.pl$/.conf/;  $config_file =~ s/\.pl$/.conf/;
20    $config_file = $ARGV[0] if ($ARGV[0] && -f $ARGV[0]);
21  die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);  die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
22    
23  my $config;  my $config;
24    
25  #use index_DBI;         # default DBI module for index  #use index_DBI;         # default DBI module for index
26  use index_DBI_cache;    # faster DBI module using memory cache  #use index_DBI_cache;   # faster DBI module using memory cache
27    use index_DBI_filter;   # filter support for indexes
28  my $index;  my $index;
29    
30  my %opts;  my %opts;
# Line 61  my %type2tag = ( Line 63  my %type2tag = (
63          'isis' => 'isis',          'isis' => 'isis',
64          'excel' => 'column',          'excel' => 'column',
65          'marc' => 'marc',          'marc' => 'marc',
66          'feed' => 'feed'          'feed' => 'feed',
67            'dbf' => 'isis',        # special case, re-use isis import_xml
68  );  );
69    
70  my $cache;      # for cacheing  my $cache;      # for cacheing
# Line 72  my %lhash; Line 75  my %lhash;
75  # if you are tight on memory, turn this off  # if you are tight on memory, turn this off
76  my $use_lhash_cache = 1;  my $use_lhash_cache = 1;
77    
78    my $last_field_name;    # cache to prevent repeated fields
79    
80  sub data2xml {  sub data2xml {
81    
82          use xmlify;          use xmlify;
# Line 109  sub data2xml { Line 114  sub data2xml {
114                  $cache->{tags_by_order} = \@sorted_tags;                  $cache->{tags_by_order} = \@sorted_tags;
115          }          }
116    
117            if (! @sorted_tags) {
118                    print STDERR "WARNING: no tags for this type found in import_xml file!\n";
119            }
120    
121          # lookup key          # lookup key
122          my $lookup_key;          my $lookup_key;
123    
# Line 137  sub data2xml { Line 146  sub data2xml {
146                  } else {                  } else {
147                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
148                  }                  }
149    
150                  if ($field_name) {                  if ($field_name) {
151                          return x($field_name);                          $field_name = x($field_name);
152                            if (! $last_field_name) {
153                                    $last_field_name = $field_name;
154                                    return $last_field_name;
155                            } elsif ($field_name ne $last_field_name) {
156                                    $last_field_name = $field_name;
157                                    return $last_field_name;
158                            }
159                  }                  }
160          }          }
161    
162    
163            # init variables for different types
164            sub init_visible_type($) {
165                    my $type = shift;
166    
167                    # swish, swish_exact, display, index, index_lookup
168                    # swish and display defaults
169                    my ($s,$se,$d,$i,$il) = (1,0,1,0,0);
170                    if (lc($type) eq "display") {
171                            $s = 0;
172                    } elsif (lc($type) eq "swish") {
173                            $d = 0;
174                    } elsif (lc($type) eq "index") {
175                            ($s,$se,$d,$i) = (0,1,0,1);
176                    } elsif (lc($type) eq "swish_exact") {
177                            ($s,$se,$d,$i) = (0,1,0,0);
178                    } elsif (lc($type) =~ /^lookup/) {
179                            ($s,$se,$d,$i,$il) = (0,1,0,0,1);
180                    } elsif ($type) {
181                            print STDERR "WARNING: unknown type: $type\n";
182                    }
183                    return ($s,$se,$d,$i,$il);
184            }
185    
186    
187            # convert
188            #
189            # <tag>
190            #  <delimiter>, </delimiter>
191            #  <value>200a</value>
192            # </tag>
193            #
194            # to
195            #
196            # <tag delimiter=", ">200a</tag>
197            #
198            # but without loosing spaces in delimiter (becasue
199            # new XML::Simple strips spaces in attribute values
200            # as defined in XML specification)
201            #
202            sub unroll_x($) {
203                    my $x = shift;
204    
205                    if (defined $x->{value}) {
206                            my ($v,$d) = ($x->{value}->{content}, $x->{delimiter}->{content});
207                            delete $x->{value};
208                            delete $x->{delimiter};
209                            $x->{content} = $v;
210                            $x->{delimiter} = $d;
211                    }
212                    return $x;
213            }
214    
215          # begin real work: go field by field          # begin real work: go field by field
216          foreach my $field (@sorted_tags) {          foreach my $field (@sorted_tags) {
217    
# Line 167  sub data2xml { Line 236  sub data2xml {
236                  my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>';                  my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>';
237                  $cache->{index_delimiter}->{$field} = $config->{indexer}->{$field}->{index_delimiter};                  $cache->{index_delimiter}->{$field} = $config->{indexer}->{$field}->{index_delimiter};
238    
239                    my $format_name = $config->{indexer}->{$field}->{format_name};
240                    my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter};
241                    if ($format_name && $format_delimiter) {
242                            $cache->{format}->{$field}->{format_name} = $format_name;
243                            $cache->{format}->{$field}->{format_delimiter} = $format_delimiter;
244                    }
245    
246                  foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
247    
248                            $x = unroll_x($x);
249    
250                          my $format = x($x->{content});                          my $format = x($x->{content});
251                          my $delimiter = x($x->{delimiter}) || ' ';                          my $delimiter = x($x->{delimiter}) || ' ';
252    
253                          my $repeat_off = 0;     # init repeatable offset                          my $repeat_off = 0;     # init repeatable offset
254    
255                          # swish, swish_exact, display, index, index_lookup                          my ($s,$se,$d,$i,$il) = init_visible_type($x->{type});
                         # swish and display defaults  
                         my ($s,$se,$d,$i,$il) = (1,0,1,0,0);  
                         $s = 0 if (lc($x->{type}) eq "display");  
                         $d = 0 if (lc($x->{type}) eq "swish");  
                         ($s,$se,$d,$i) = (0,1,0,1) if (lc($x->{type}) eq "index");  
                         ($s,$se,$d,$i) = (0,1,0,0) if (lc($x->{type}) eq "swish_exact");  
                         ($s,$se,$d,$i,$il) = (0,1,0,0,1) if (lc($x->{type}) =~ /^lookup/);  
256    
257                          # what will separate last line from this one?                          # what will separate last line from this one?
258                          if ($display_data && $x->{append}) {                          if ($display_data && $x->{append}) {
259                                  $line_delimiter = ' ';                                  $line_delimiter = $delimiter;
260                          } elsif ($display_data) {                          } elsif ($display_data) {
261                                  $line_delimiter = '<br/>';                                  $line_delimiter = '<br/>';
262                          }                          }
# Line 193  sub data2xml { Line 264  sub data2xml {
264                          # init vars so that we go into while...                          # init vars so that we go into while...
265                          ($swish,$display) = (1,1);                          ($swish,$display) = (1,1);
266    
267                          # placeholder for all repeatable entries for index                          sub mkformat($$) {
   
                         sub mkformat {  
268                                  my $x = shift || die "mkformat needs tag reference";                                  my $x = shift || die "mkformat needs tag reference";
269                                  my $data = shift || return;                                  my $data = shift || return;
270                                  my $format_name = x($x->{format_name}) || return $data;                                  my $format_name = x($x->{format_name}) || return $data;
# Line 213  sub data2xml { Line 282  sub data2xml {
282                                          if (($#data+1) == $nr) {                                          if (($#data+1) == $nr) {
283                                                  return sprintf($fmt,@data);                                                  return sprintf($fmt,@data);
284                                          } else {                                          } else {
285                                                  print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n";                                                  #print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n";
286                                                  return $data;                                                  return $data;
287                                          }                                          }
288                                  } else {                                  } else {
# Line 242  sub data2xml { Line 311  sub data2xml {
311                                                                  $display = $new_display;                                                                  $display = $new_display;
312                                                                  $cache->{lhash}->{$display} = $new_display;                                                                  $cache->{lhash}->{$display} = $new_display;
313                                                          } else {                                                          } else {
314                                                                  print STDERR "WARNING: lookup for '$display' didn't find anything.\n";  #                                                               print STDERR "WARNING: lookup for '$display' didn't find anything.\n";
315                                                                  $display = "";                                                                  $display = "";
316                                                                  $cache->{lhash}->{$display} = $null;                                                                  $cache->{lhash}->{$display} = $null;
317                                                          }                                                          }
# Line 263  sub data2xml { Line 332  sub data2xml {
332                                  }                                  }
333                                  # type="swish" ; field for swish                                  # type="swish" ; field for swish
334                                  if ($swish) {                                  if ($swish) {
335                                            my $tmp = $swish;
336                                          if ($filter && ($s || $se)) {                                          if ($filter && ($s || $se)) {
337                                                  no strict 'refs';                                                  no strict 'refs';
338                                                  my $tmp = join(" ",&$filter($swish)) if ($s || $se);                                                  $tmp = join(" ",&$filter($tmp)) if ($s || $se);
                                                 $swish_data .= $tmp if ($s);  
                                                 $swish_exact_data .= $tmp if ($se);  
   
                                         } else {  
                                                 $swish_data .= $swish if ($s);  
                                                 $swish_exact_data .= $swish if ($se);  
339                                          }                                          }
340    
341                                            $swish_data .= $tmp if ($s && $tmp);
342                                            $swish_exact_data .= "xxbxx $tmp xxexx " if ($tmp && $tmp ne "" && $se);
343                                  }                                  }
344    
345                                  # type="display" ; field for display                                  # type="display" ; field for display
346                                  if ($d && $display) {                                  if ($d && $display) {
347                                            my $ldel = $delimiter;
348                                          if ($line_delimiter && $display_data) {                                          if ($line_delimiter && $display_data) {
349                                                  $display_data .= $line_delimiter;                                                  $ldel = $line_delimiter;
350                                          }                                          }
351                                          if ($filter) {                                          if ($filter) {
352                                                  no strict 'refs';                                                  no strict 'refs';
353                                                  if ($display_data) {                                                  my @arr;
354                                                          $display_data .= $delimiter.join($delimiter,mkformat($x,&$filter($display)));                                                  foreach my $tmp (&$filter($display)) {
355                                                  } else {                                                          my $tmp2 = mkformat($x,$tmp);
356                                                          $display_data = join($delimiter,mkformat($x,&$filter($display)));                                                          push @arr,$tmp2 if ($tmp2);
357                                                  }                                                  }
358                                                    $display_data .= $ldel if ($display_data && @arr);
359                                                    $display_data .= join($delimiter,@arr);
360                                          } else {                                          } else {
361                                                  if ($display_data) {                                                  $display_data .= $ldel if ($display_data);
362                                                          $display_data .= $delimiter.mkformat($x,$display);                                                  my $tmp = mkformat($x,$display);
363                                                  } else {                                                  $display_data .= $tmp if ($tmp);
                                                         $display_data = mkformat($x,$display);  
                                                 }  
364                                          }                                          }
365                                  }                                  }
366                                                                                                    
# Line 304  sub data2xml { Line 372  sub data2xml {
372                                                  no strict 'refs';                                                  no strict 'refs';
373                                                  $idisplay = &$filter($idisplay);                                                  $idisplay = &$filter($idisplay);
374                                          }                                          }
375                                          push @index_data, $idisplay if (! $iterate_by_page);                                          push @index_data, $idisplay if ($idisplay && !$iterate_by_page);
376                                  }                                  }
377    
378                                  # store fields in lookup                                  # store fields in lookup
# Line 313  sub data2xml { Line 381  sub data2xml {
381                                                  if ($lookup_key) {                                                  if ($lookup_key) {
382                                                          print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)";                                                          print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)";
383                                                  } else {                                                  } else {
384                                                          $lookup_key = $display;                                                          if ($filter) {
385                                                                    no strict 'refs';
386                                                                    $lookup_key = &$filter($display);
387                                                            } else {
388                                                                    $lookup_key = $display;
389                                                            }
390                                                  }                                                  }
391                                          } elsif (lc($x->{type}) eq "lookup_val") {                                          } elsif (lc($x->{type}) eq "lookup_val") {
392                                                  if ($lookup_key) {                                                  if ($lookup_key) {
393                                                          $lhash{$lookup_key} = $display;                                                          if ($filter) {
394                                                                    no strict 'refs';
395                                                                    $lhash{$lookup_key} = &$filter($display);
396                                                            } else {
397                                                                    $lhash{$lookup_key} = $display;
398                                                            }
399                                                  } else {                                                  } else {
400                                                          print STDERR "WARNING: no lookup_key defined for  '$display'?";                                                          print STDERR "WARNING: no lookup_key defined for  '$display'?";
401                                                  }                                                  }
# Line 362  sub data2xml { Line 440  sub data2xml {
440                                                          @index_data = @{$cache->{index_data}->{$field}->[$page]};                                                          @index_data = @{$cache->{index_data}->{$field}->[$page]};
441                                                  }                                                  }
442                                                  if ($x->{append}) {                                                  if ($x->{append}) {
443                                                          $index_data[$#index_data] .= $idisplay;                                                          if (@index_data) {
444                                                                    $index_data[$#index_data] .= $idisplay;
445                                                            } else {
446                                                                    push @index_data, $idisplay;
447                                                            }
448                                                  } else {                                                  } else {
449                                                          push @index_data, $idisplay;                                                          push @index_data, $idisplay;
450                                                  }                                                  }
# Line 389  sub data2xml { Line 471  sub data2xml {
471                  # now try to parse variables from configuration file                  # now try to parse variables from configuration file
472                  foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) {
473    
474                            $x = unroll_x($x);
475    
476                          my $delimiter = x($x->{delimiter}) || ' ';                          my $delimiter = x($x->{delimiter}) || ' ';
477                          my $val = $cfg->val($database, x($x->{content}));                          my $val = $cfg->val($database, x($x->{content}));
478    
479                          my ($s,$d,$i) = (1,1,0);        # swish, display default                          # FIXME index_lookup is not supported!
480                          $s = 0 if (lc($x->{type}) eq "display");                          my ($s,$se,$d,$i,$il) = init_visible_type($x->{type});
                         $d = 0 if (lc($x->{type}) eq "swish");  
                         # no support for swish exact in config.  
                         # IMHO, it's useless  
                         ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");  
481    
482                          if ($val) {                          if ($val) {
483                                  $display_data .= $delimiter.$val if ($d);                                  $display_data .= $delimiter.$val if ($d);
484                                  $swish_data .= $val if ($s);                                  $swish_data .= " ".$val if ($s);
485                                  $index->insert($field, $val, $path) if ($i);                                  $index->insert($field, $val, $path) if ($i);
486                          }                          }
487    
# Line 430  sub data2xml { Line 510  sub data2xml {
510  #print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n";  #print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n";
511  #print STDERR Dumper($cache->{display_data});  #print STDERR Dumper($cache->{display_data});
512                          for (my $page=0; $page <= $nr_pages; $page++) {                          for (my $page=0; $page <= $nr_pages; $page++) {
513                                    my $display_data;
514                                  my $display_data = $cache->{display_data}->{$field}->[$page];                                  if ($cache->{format}->{$field}) {
515                                            my $tmp = mkformat($cache->{format}->{$field},$cache->{display_data}->{$field}->[$page]);
516                                            $display_data=$tmp if ($tmp);
517                                    } else {
518                                            $display_data = $cache->{display_data}->{$field}->[$page];
519                                    }
520                                  if ($display_data) { # default                                  if ($display_data) { # default
521                                          if ($field eq "headline") {                                          if ($field eq "headline") {
522                                                  $xml .= xmlify("headline", $display_data);                                                  $xml .= xmlify("headline", $display_data);
# Line 449  sub data2xml { Line 534  sub data2xml {
534                                          $swish_data =~ s/ +/ /g;                                          $swish_data =~ s/ +/ /g;
535                                          $swish_data =~ s/ +$//g;                                          $swish_data =~ s/ +$//g;
536    
537                                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));                                          $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
538                                  }                                  }
539    
540                                  my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page];                                  my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page];
# Line 459  sub data2xml { Line 544  sub data2xml {
544    
545                                          # add delimiters before and after word.                                          # add delimiters before and after word.
546                                          # That is required to produce exact match                                          # That is required to produce exact match
547                                          $xml .= xmlify($field."_swish_exact", unac_string($codepage,'xxbxx '.$swish_exact_data.' xxexx'));                                          $xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data));
548                                  }                                  }
549                                                                    
550                                  my $idel = $cache->{index_delimiter}->{$field};                                  my $idel = $cache->{index_delimiter}->{$field};
# Line 492  sub data2xml { Line 577  sub data2xml {
577                                  $swish_data =~ s/ +/ /g;                                  $swish_data =~ s/ +/ /g;
578                                  $swish_data =~ s/ +$//g;                                  $swish_data =~ s/ +$//g;
579    
580                                  $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));                                  $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
581                          }                          }
582    
583                          if ($swish_exact_data) {                          if ($swish_exact_data) {
# Line 501  sub data2xml { Line 586  sub data2xml {
586    
587                                  # add delimiters before and after word.                                  # add delimiters before and after word.
588                                  # That is required to produce exact match                                  # That is required to produce exact match
589                                  $xml .= xmlify($field."_swish_exact", unac_string($codepage,'xxbxx '.$swish_exact_data.' xxexx'));                                  $xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data));
590                          }                          }
591                  }                  }
592          }          }
# Line 538  $index = new index_DBI( Line 623  $index = new index_DBI(
623    
624  my $show_progress = $cfg_global->val('global', 'show_progress');  my $show_progress = $cfg_global->val('global', 'show_progress');
625    
626  my $unac_filter = $cfg_global->val('global', 'unac_filter');  my $my_unac_filter = $cfg_global->val('global', 'my_unac_filter');
627  if ($unac_filter) {  if ($my_unac_filter) {
628          require $unac_filter;          print STDERR "using $my_unac_filter to filter characters for search\n";
629            require $my_unac_filter;
630    } else {
631            print STDERR "### fallback to default my_unac_string!\n";
632            eval q{
633            sub main::my_unac_string($$) {
634                    my ($charset, $string) = (@_);
635                    return $string;
636            }
637            };
638  }  }
639    
640  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
641    
642            # save database name in global variable path for later
643            # (need for index filter creation)
644            $path = $database;
645    
646          my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";          my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
647          my $add_xml = $cfg -> val($database, 'xml');    # optional          my $add_xml = $cfg -> val($database, 'xml');    # optional
648    
# Line 552  foreach my $database ($cfg->Sections) { Line 650  foreach my $database ($cfg->Sections) {
650          my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional          my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional
651          if ($lookup_file) {          if ($lookup_file) {
652                  #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644;                  #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644;
653                    if (! -e $lookup_file) {
654                            open(LOOKUP, "> $lookup_file") || die "can't create $lookup_file': $!";
655                            close(LOOKUP);
656                    }
657                  tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644;                  tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644;
658                  print STDERR "creating lookup file '$lookup_file'\n";                  print STDERR "creating lookup file '$lookup_file'\n";
659                    # delete memory cache for lookup file
660                    delete $cache->{lhash};
661          }          }
662    
663          # open existing lookup file          # open existing lookup file
# Line 564  foreach my $database ($cfg->Sections) { Line 668  foreach my $database ($cfg->Sections) {
668                  print STDERR "opening lookup file '$lookup_file'\n";                  print STDERR "opening lookup file '$lookup_file'\n";
669          }          }
670    
671  print STDERR "reading ./import_xml/$type.xml\n";          my $import_xml_file = "./import_xml/$type.xml";
672    
673            if (! -r $import_xml_file) {
674                    print STDERR "ERROR: file $import_xml_file not readable skipping!\n";
675                    next;
676            }
677    
678            print STDERR "reading $import_xml_file\n";
679    
680          # extract just type basic          # extract just type basic
681          my $type_base = $type;          my $type_base = $type;
682          $type_base =~ s/_.+$//g;          $type_base =~ s/_.+$//g;
683    
684          $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type_base}, 'config', 'format' ], forcecontent => 1);          $config=XMLin($import_xml_file, ForceArray => [ $type2tag{$type_base}, 'config', 'format' ], ForceContent => 1 );
685    
686            # helper for progress bar
687            sub fmt_time {
688                    my $t = shift || 0;
689                    my $out = "";
690    
691                    my ($ss,$mm,$hh) = gmtime($t);
692                    $out .= "${hh}h" if ($hh);
693                    $out .= sprintf("%02d:%02d", $mm,$ss);
694                    $out .= "  " if ($hh == 0);
695                    return $out;
696            }
697    
698          # output current progress indicator          # output current progress indicator
699          my $last_p = 0;          my $last_p = 0;
700            my $start_t = time();
701          sub progress {          sub progress {
702                  return if (! $show_progress);                  return if (! $show_progress);
703                  my $current = shift;                  my $current = shift;
704                  my $total = shift || 1;                  my $total = shift || 1;
705                  my $p = int($current * 100 / $total);                  my $p = int($current * 100 / $total);
706                  if ($p != $last_p) {                  if ($p < $last_p || $current == 1) {
707                          printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );                          $start_t = time();
708                            $last_p = 0;
709                    } elsif ($p != $last_p) {
710                            my $rate = ($current / (time() - $start_t || 1));
711                            my $eta = ($total-$current) / ($rate || 1);
712                            printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$current,"=" x ($p/3)."$p%>", $total, $rate, fmt_time($eta));
713                          $last_p = $p;                          $last_p = $p;
714                  }                  }
715          }          }
716    
717          my $fake_dir = 1;          my $fake_dir = 1;
718            my $fake_pos = 0;
719            my $last_fake_t = time();
720          sub fakeprogress {          sub fakeprogress {
721                  return if (! $show_progress);                  return if (! $show_progress);
722                  my $current = shift @_;                  my $current = shift @_;
723    
724                  my @ind = ('-','\\','|','/','-','\\','|','/', '-');                  my @ind = ('-','\\','|','/','-','\\','|','/');
725    
726                    if ($current < $fake_pos) {
727                            $start_t = time();
728                            $last_fake_t = 0;
729                            $fake_dir = 1;
730                            $fake_pos = 0;
731                    }
732    
733                    if (time()-$last_fake_t >= 1) {
734                            $last_fake_t = time();
735                            $fake_pos += $fake_dir;
736                            $fake_dir = -$fake_dir if ($fake_pos > 38);
737                    }
738    
739                  $last_p += $fake_dir;                  if ($current % 10 == 0) {
740                  $fake_dir = -$fake_dir if ($last_p > 1000 || $last_p < 0);                          my $rate = ($current / (time() - $start_t || 1));
741                  if ($last_p % 10 == 0) {                          printf STDERR ("%5d [%-38s] %0.1f/s\r",$current, " " x $fake_pos .$ind[($current / 10) % 8], $rate);
                         printf STDERR ("%5d / %5s [%-51s]\r",$current,"?"," " x ($last_p/20).$ind[($last_p/20) % $#ind]);  
742                  }                  }
743          }          }
744    
# Line 610  print STDERR "using: $type...\n"; Line 753  print STDERR "using: $type...\n";
753                  my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";                  my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
754    
755                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
756                  my $db = OpenIsis::open( $isis_db );                  my $db = new Biblio::Isis( isisdb => $isis_db );
   
                 # check if .txt database for OpenIsis is zero length,  
                 # if so, erase it and re-open database  
                 sub check_txt_db {  
                         my $isis_db = shift || die "need isis database name";  
                         my $reopen = 0;  
   
                         if (-e $isis_db.".TXT") {  
                                 print STDERR "WARNING: removing $isis_db.TXT OpenIsis database...\n";  
                                 unlink $isis_db.".TXT" || warn "FATAL: unlink error on '$isis_db.TXT': $!";  
                                 $reopen++;  
                         }  
                         if (-e $isis_db.".PTR") {  
                                 print STDERR "WARNING: removing $isis_db.PTR OpenIsis database...\n";  
                                 unlink $isis_db.".PTR" || warn "FATAL: unlink error on '$isis_db.PTR': $!";  
                                 $reopen++;  
                         }  
                         return OpenIsis::open( $isis_db ) if ($reopen);  
                 }  
   
                 # EOF error  
                 if ($db == -1) {  
                         $db = check_txt_db($isis_db);  
                         if ($db == -1) {  
                                 print STDERR "FATAL: OpenIsis can't open zero size file $isis_db\n";  
                                 next;  
                         }  
                 }  
757    
758                  # OpenIsis::ERR_BADF                  if (! $db) {
759                  if ($db == -4) {                          print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n";
                         print STDERR "FATAL: OpenIsis can't find file $isis_db\n";  
                         next;  
                 # OpenIsis::ERR_IO  
                 } elsif ($db == -5) {  
                         print STDERR "FATAL: OpenIsis can't access file $isis_db\n";  
                         next;  
                 } elsif ($db < 0) {  
                         print STDERR "FATAL: OpenIsis unknown error $db with file $isis_db\n";  
760                          next;                          next;
761                  }                  }
762    
763                  my $max_rowid = OpenIsis::maxRowid( $db );                  my $max_rowid = $db->count if ($db);
   
                 # if 0 records, try to rease isis .txt database  
                 if ($max_rowid == 0) {  
                         # force removal of database  
                         $db = check_txt_db($isis_db);  
                         $max_rowid = OpenIsis::maxRowid( $db );  
                 }  
764    
765                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
766    
                 my $path = $database;  
   
767                  for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                  for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
768                          my $row = OpenIsis::read( $db, $row_id );                          my $row = $db->to_hash( $row_id );
769                          if ($row && $row->{mfn}) {                          if ($row) {
770            
771                                    $row->{mfn} = $row_id;
772                                    $row->{record} = $db->{record};
773    
774                                  progress($row->{mfn}, $max_rowid);                                  progress($row->{mfn}, $max_rowid);
775    
776                                  my $swishpath = $path."#".int($row->{mfn});                                  my $swishpath = $path."#".int($row->{mfn});
# Line 683  print STDERR "using: $type...\n"; Line 784  print STDERR "using: $type...\n";
784                                  }                                  }
785                          }                          }
786                  }                  }
                 # for this to work with current version of OpenIsis (0.9.0)  
                 # you might need my patch from  
                 # http://www.rot13.org/~dpavlin/projects/openisis-0.9.0-perl_close.diff  
                 OpenIsis::close($db);  
787                  print STDERR "\n";                  print STDERR "\n";
788    
789          } elsif ($type_base eq "excel") {          } elsif ($type_base eq "excel") {
790                  use Spreadsheet::ParseExcel;                  require Spreadsheet::ParseExcel;
791                  use Spreadsheet::ParseExcel::Utility qw(int2col);                  require Spreadsheet::ParseExcel::Utility;
792                    import Spreadsheet::ParseExcel::Utility qw(int2col);
793                                    
794                  $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);                  $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);
795                  my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";                  my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
# Line 719  print STDERR "using: $type...\n"; Line 817  print STDERR "using: $type...\n";
817                          for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {                          for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
818                                  my $cell = $oWorksheet->{Cells}[$iR][$iC];                                  my $cell = $oWorksheet->{Cells}[$iR][$iC];
819                                  if ($cell) {                                  if ($cell) {
820                                          $row->{int2col($iC)} = $cell->Value;                                          # this conversion is a cludge.
821                                            # Files from Excell could have
822                                            # characters which don't fit into
823                                            # destination encoding.
824                                            $row->{int2col($iC)} = $utf2cp->convert($cell->Value) || $cell->Value;
825                                  }                                  }
826                          }                          }
827    
# Line 743  print STDERR "using: $type...\n"; Line 845  print STDERR "using: $type...\n";
845                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
846                          }                          }
847                  }                  }
848    
849                    print STDERR "\n";
850    
851          } elsif ($type_base eq "marc") {          } elsif ($type_base eq "marc") {
852    
853                  use MARC;                  require MARC::File::USMARC;
854                                    
855                  $import2cp = Text::Iconv->new($config->{marc_codepage},$codepage);                  $import2cp = Text::Iconv->new($config->{marc_codepage},$codepage);
856                  my $marc_file = $cfg -> val($database, 'marc_file') || die "$database doesn't have 'marc_file' defined!";                  my $marc_file = $cfg -> val($database, 'marc_file') || die "$database doesn't have 'marc_file' defined!";
857    
858                  # optional argument is format                  # optional argument is format
859                  my $format = x($config->{format}) || 'usmarc';                  warn "marc_format is no longer used!" if ($config->{marc_format});
   
860                  print STDERR "Reading MARC file '$marc_file'\n";                  print STDERR "Reading MARC file '$marc_file'\n";
861    
862                  my $marc = new MARC;                  my $marc = MARC::File::USMARC->in( $marc_file );
                 my $nr = $marc->openmarc({  
                                 file=>$marc_file, format=>$format  
                         }) || die "Can't open MARC file '$marc_file'";  
863    
864                  my $i=0;        # record nr.                  if (! $marc) {
865                            print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n";
866                            next;
867                    }
868    
869                  my $rec;                  # count records in MARC file
870                    sub marc_count {
871                            my $filename = shift || die;
872                            my $file = MARC::File::USMARC->in($filename) || return;
873                            my $count = 0;
874                            while ($file->skip()) {
875                                    $count++;
876                            }
877                            return $count;
878                    }
879    
880                  while ($marc->nextmarc(1)) {                  my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";
881    
882                          # XXX                  my $i = 1;
883                          fakeprogress($i++);  
884                    while( my $rec = $marc->next() ) {
885    
886                            progress($i,$count);
887    
888                          my $swishpath = $database."#".$i;                          my $swishpath = $database."#".$i;
889    
890                          if (my $xml = data2xml($type_base,$marc,$add_xml,$cfg,$database)) {                          if (my $xml = data2xml($type_base,$rec,$add_xml,$cfg,$database)) {
891                                  $xml = $cp2utf->convert($xml);                                  $xml = $cp2utf->convert($xml);
892                                  use bytes;      # as opposed to chars                                  use bytes;      # as opposed to chars
893                                  print "Path-Name: $swishpath\n";                                  print "Path-Name: $swishpath\n";
894                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
895                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
896                          }                          }
897    
898                            $i++;
899                  }                  }
900    
901                    print STDERR "\n";
902    
903          } elsif ($type_base eq "feed") {          } elsif ($type_base eq "feed") {
904    
905                  $import2cp = Text::Iconv->new($config->{feed_codepage},$codepage);                  $import2cp = Text::Iconv->new($config->{feed_codepage},$codepage);
# Line 819  print STDERR "using: $type...\n"; Line 940  print STDERR "using: $type...\n";
940                  }                  }
941                  # close lookup                  # close lookup
942                  untie %lhash if (%lhash);                  untie %lhash if (%lhash);
943    
944            } elsif ($type_base eq "dbf") {
945    
946                    my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!";
947                    my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!";
948                    my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!";
949    
950                    $import2cp = Text::Iconv->new($dbf_codepage,$codepage);
951                    require XBase;
952                    my $db = new XBase $dbf_file;
953    
954                    if (! $db) {
955                            print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n";
956                            next;
957                    }
958    
959                    my $max_rowid = $db->last_record;
960    
961                    print STDERR "Reading database: $dbf_file [$max_rowid rows]\n";
962    
963                    my %dbf2iso;
964                    foreach my $m (split(/[\n\r]+/,$dbf_mapping)) {
965                            my ($col,$fld) = split(/\s+/,$m,2);
966                            $dbf2iso{$col} = $fld;
967                    }
968    
969    #print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n";
970    
971                    # bad, bad...
972                    require "to_hash.pm";
973    
974                    foreach my $row_id (0 .. $max_rowid) {
975                            my $dbf_row = $db->get_record_as_hash($row_id);
976                            if ($dbf_row) {
977    
978    #print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n";
979                                    # apply mapping from config file
980                                    # all unspecified records will get _ in
981                                    # front of them - _DELETE will be __DELETE
982                                    my $rec;
983                                    map {
984                                            my $new_fld = $dbf2iso{$_} || '_'.$_;
985                                            my $data = $dbf_row->{$_};
986                                            push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/);
987                                    } keys %{$dbf_row};
988    #print STDERR "## rec: ",Dumper($rec),"\n## /rec\n";
989                                    my $row = to_hash($row_id+1, $rec);
990    
991                                    $row->{mfn} = $row_id+1;
992                                    $row->{record} = $rec;
993    
994    #print STDERR "## row: ",Dumper($row),"\n## /row\n";
995                                    progress($row->{mfn}, $max_rowid);
996    
997                                    my $swishpath = $path."#".int($row->{mfn});
998    
999                                    if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
1000                                            $xml = $cp2utf->convert($xml);
1001                                            use bytes;      # as opposed to chars
1002                                            print "Path-Name: $swishpath\n";
1003                                            print "Content-Length: ".(length($xml)+1)."\n";
1004                                            print "Document-Type: XML\n\n$xml\n";
1005                                    }
1006                            }
1007                    }
1008                    print STDERR "\n";
1009          }          }
1010  }  }
1011    
# Line 833  __END__ Line 1020  __END__
1020    
1021  all2xml.pl - read various file formats and dump XML for SWISH-E  all2xml.pl - read various file formats and dump XML for SWISH-E
1022    
1023    =head1 SYNOPSYS
1024    
1025     $ all2xml.pl [test.conf]
1026    
1027  =head1 DESCRIPTION  =head1 DESCRIPTION
1028    
1029  This command will read ISIS data file using OpenIsis perl module, MARC  This command will read ISIS data file using Biblio::Isis perl module, MARC
1030  records using MARC module and optionally Micro$oft Excel files to  records using MARC::File module and optionally Micro$oft Excel files to
1031  create one XML file for usage with I<SWISH-E> indexer. Dispite it's name,  create one XML file for usage with I<SWISH-E> indexer. Dispite it's name,
1032  this script B<isn't general xml generator> from isis files (isis allready  this script B<isn't general xml generator> from isis files (isis allready
1033  has something like that). Output of this script is tailor-made for SWISH-E.  has something like that). Output of this script is tailor-made for SWISH-E.
1034    
1035    If no configuration file is specified, it will use default one called
1036    C<all2xml.conf>.
1037    
1038  =head1 BUGS  =head1 BUGS
1039    
1040  Documentation is really lacking. However, in true Open Source spirit, source  Documentation is really lacking. However, in true Open Source spirit, source

Legend:
Removed from v.188  
changed lines
  Added in v.678

  ViewVC Help
Powered by ViewVC 1.1.26