/[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 647 by dpavlin, Thu Jan 27 17:55:09 2005 UTC revision 747 by dpavlin, Tue Jun 6 12:34:25 2006 UTC
# Line 11  use Encode; Line 11  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    
# Line 62  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  );  );
68    
69  my $cache;      # for cacheing  my $cache;      # for cacheing
# Line 75  my $use_lhash_cache = 1; Line 76  my $use_lhash_cache = 1;
76    
77  my $last_field_name;    # cache to prevent repeated fields  my $last_field_name;    # cache to prevent repeated fields
78    
79    my $broken_cdata = XMLin('<foo><![CDATA[<bar>]]></foo>') eq '<bar>>';
80    warn "XML::Simple on this system seems broken with <![CDATA[..]]>.\n" if ($broken_cdata);
81    
82  sub data2xml {  sub data2xml {
83    
84          use xmlify;          use xmlify;
# Line 112  sub data2xml { Line 116  sub data2xml {
116                  $cache->{tags_by_order} = \@sorted_tags;                  $cache->{tags_by_order} = \@sorted_tags;
117          }          }
118    
119            if (! @sorted_tags) {
120                    print STDERR "WARNING: no tags for this type found in import_xml file!\n";
121            }
122    
123          # lookup key          # lookup key
124          my $lookup_key;          my $lookup_key;
125    
# Line 121  sub data2xml { Line 129  sub data2xml {
129          delete $cache->{swish_exact_data};          delete $cache->{swish_exact_data};
130          delete $cache->{index_data};          delete $cache->{index_data};
131          delete $cache->{index_delimiter};          delete $cache->{index_delimiter};
132            delete $cache->{distinct};
133          my @page_fields;        # names of fields          my @page_fields;        # names of fields
134    
135    
# Line 201  sub data2xml { Line 210  sub data2xml {
210                          delete $x->{value};                          delete $x->{value};
211                          delete $x->{delimiter};                          delete $x->{delimiter};
212                          $x->{content} = $v;                          $x->{content} = $v;
213                            $d =~ s#>$## if ($d && $broken_cdata);
214                          $x->{delimiter} = $d;                          $x->{delimiter} = $d;
215                  }                  }
216                  return $x;                  return $x;
# Line 220  sub data2xml { Line 230  sub data2xml {
230    
231                  my ($swish,$display);                  my ($swish,$display);
232    
233                  my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";                  my $tag = $cfg->val($database, 'import_xml_tag') || $type2tag{$type} || die "can't find which tag to use for type $type";
234    
235                  # is this field page-by-page?                  # is this field page-by-page?
236                  my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page};                  my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page};
# Line 229  sub data2xml { Line 239  sub data2xml {
239                  # default line_delimiter if using                  # default line_delimiter if using
240                  my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>';                  my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>';
241                  $cache->{index_delimiter}->{$field} = $config->{indexer}->{$field}->{index_delimiter};                  $cache->{index_delimiter}->{$field} = $config->{indexer}->{$field}->{index_delimiter};
242                    my $distinct = $config->{indexer}->{$field}->{distinct};
243                    if ($distinct && !$iterate_by_page) {
244                            warn "WARNING: distinct is currently not supported without iterate_by_page!\n";
245                            $distinct = 0;
246                    }
247    
248                  my $format_name = $config->{indexer}->{$field}->{format_name};                  my $format_name = $config->{indexer}->{$field}->{format_name};
249                  my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter};                  my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter};
# Line 407  sub data2xml { Line 422  sub data2xml {
422                                                  $ldel = " " if ($append);                                                  $ldel = " " if ($append);
423  #print STDERR "line delimiter: ",Dumper($ldel) if ($ldel);  #print STDERR "line delimiter: ",Dumper($ldel) if ($ldel);
424                                                  if (! $cache->{$what}->{$field}->[$page]) {                                                  if (! $cache->{$what}->{$field}->[$page]) {
425                                                          $cache->{$what}->{$field}->[$page] = $data;                                                          push @{$cache->{$what}->{$field}->[$page]}, {
426                                                  } else {                                                                  data => $data,
427                                                          $cache->{$what}->{$field}->[$page] .= $ldel.$data;                                                                  delimiter => $ldel,
428                                                            };
429                                                  }                                                  }
430                                          }                                          }
431    
# Line 476  sub data2xml { Line 492  sub data2xml {
492                          if ($val) {                          if ($val) {
493                                  $display_data .= $delimiter.$val if ($d);                                  $display_data .= $delimiter.$val if ($d);
494                                  $swish_data .= " ".$val if ($s);                                  $swish_data .= " ".$val if ($s);
495                                  $index->insert($field, $val, $path) if ($i);                                  $index->insert($field, $val, $val, $path) if ($i);
496                          }                          }
497    
498                          if ($iterate_by_page) {                          if ($iterate_by_page) {
# Line 484  sub data2xml { Line 500  sub data2xml {
500                                  # on first page!!!                                  # on first page!!!
501                                  my $page = 0;                                  my $page = 0;
502                                  if ($display_data) {                                  if ($display_data) {
503                                          $cache->{display_data}->{$field}->[$page] = $display_data;                                          push @{$cache->{display_data}->{$field}->[$page]}, { data => $display_data };
504                                          $display_data = "";                                          $display_data = "";
505                                  }                                  }
506                                  if ($swish_data) {                                  if ($swish_data) {
507                                          $cache->{swish_data}->{$field}->[$page] = $swish_data;                                          push @{$cache->{swish_data}->{$field}->[$page]}, { data => $swish_data };
508                                          $swish_data = "";                                          $swish_data = "";
509                                  }                                  }
510                                  if ($swish_exact_data) {                                  if ($swish_exact_data) {
511                                          $cache->{swish_exact_data}->{$field}->[$page] = $swish_exact_data;                                          push @{$cache->{swish_exact_data}->{$field}->[$page]}, { data => $swish_exact_data };
512                                          $swish_exact_data = "";                                          $swish_exact_data = "";
513                                  }                                  }
514                          }                          }
# Line 503  sub data2xml { Line 519  sub data2xml {
519                          my $nr_pages = $page_max{$field} || next;                          my $nr_pages = $page_max{$field} || next;
520  #print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n";  #print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n";
521  #print STDERR Dumper($cache->{display_data});  #print STDERR Dumper($cache->{display_data});
522                            my $seen;       # used for distinct
523                          for (my $page=0; $page <= $nr_pages; $page++) {                          for (my $page=0; $page <= $nr_pages; $page++) {
524                                  my $display_data;                                  my $display_data;
525                                  if ($cache->{format}->{$field}) {                                  my $delimiter = '';
526                                          my $tmp = mkformat($cache->{format}->{$field},$cache->{display_data}->{$field}->[$page]);                                  foreach my $element (@{ $cache->{display_data}->{$field}->[$page] }) {
527                                          $display_data=$tmp if ($tmp);                                          my $data = $element->{data};
528                                  } else {                                          die "BUG! no data in element?" unless ($data);
529                                          $display_data = $cache->{display_data}->{$field}->[$page];  
530                                            if ($distinct) {
531                                                    next if ($cache->{distinct}->{$field}->{ $data });
532                                                    $cache->{distinct}->{$field}->{ $data } = 1;
533                                            }
534    
535                                            if ($cache->{format}->{$field}) {
536                                                    my $tmp = mkformat($cache->{format}->{$field},$data);
537                                                    $display_data .= $delimiter . $tmp if ($tmp);
538                                            } else {
539                                                    $display_data .= $delimiter . $data;
540                                            }
541                                            $delimiter = $element->{delimiter} if ($element->{delimiter});
542                                  }                                  }
543    
544                                  if ($display_data) { # default                                  if ($display_data) { # default
545                                          if ($field eq "headline") {                                          if ($field eq "headline") {
546                                                  $xml .= xmlify("headline", $display_data);                                                  $xml .= xmlify("headline", $display_data);
# Line 522  sub data2xml { Line 552  sub data2xml {
552                                          }                                          }
553                                  }                                  }
554                                                                    
555                                  my $swish_data = $cache->{swish_data}->{$field}->[$page];                                  my $swish_data = join(" ",map { $_->{data} } @{ $cache->{swish_data}->{$field}->[$page] });
556                                  if ($swish_data) {                                  if ($swish_data) {
557                                          # remove extra spaces                                          # remove extra spaces
558                                          $swish_data =~ s/ +/ /g;                                          $swish_data =~ s/ +/ /g;
# Line 531  sub data2xml { Line 561  sub data2xml {
561                                          $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));                                          $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
562                                  }                                  }
563    
564                                  my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page];                                  my $swish_exact_data = join(" ", map { $_->{data} } @{ $cache->{swish_exact_data}->{$field}->[$page] });
565                                  if ($swish_exact_data) {                                  if ($swish_exact_data) {
566                                          $swish_exact_data =~ s/ +/ /g;                                          $swish_exact_data =~ s/ +/ /g;
567                                          $swish_exact_data =~ s/ +$//g;                                          $swish_exact_data =~ s/ +$//g;
# Line 633  if ($my_unac_filter) { Line 663  if ($my_unac_filter) {
663    
664  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
665    
666            # save database name in global variable path for later
667            # (need for index filter creation)
668            $path = $database;
669    
670          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";
671          my $add_xml = $cfg -> val($database, 'xml');    # optional          my $add_xml = $cfg -> val($database, 'xml');    # optional
672    
# Line 658  foreach my $database ($cfg->Sections) { Line 692  foreach my $database ($cfg->Sections) {
692                  print STDERR "opening lookup file '$lookup_file'\n";                  print STDERR "opening lookup file '$lookup_file'\n";
693          }          }
694    
695  print STDERR "reading ./import_xml/$type.xml\n";          my $import_xml_type = $cfg->val($database, 'import_xml_file') || $type;
696            my $import_xml_file = "./import_xml/$import_xml_type.xml";
697    
698            if (! -r $import_xml_file) {
699                    print STDERR "ERROR: file $import_xml_file not readable skipping!\n";
700                    next;
701            }
702    
703            print STDERR "reading $import_xml_file\n";
704    
705          # extract just type basic          # extract just type basic
706          my $type_base = $type;          my $type_base = $type;
707          $type_base =~ s/_.+$//g;          $type_base =~ s/_.+$//g;
708    
709          $config=XMLin("./import_xml/$type.xml", ForceArray => [ $type2tag{$type_base}, 'config', 'format' ], ForceContent => 1 );          my $tag = $cfg->val($database, 'import_xml_tag') || $type2tag{$type_base} || die "can't find which tag to use for type $type";
710            $config=XMLin($import_xml_file, ForceArray => [ $tag, 'config', 'format' ], ForceContent => 1 );
711    
712            # check for broken XML::Simple
713            if ( $broken_cdata ) {
714                    map {
715                            $config->{format}->{$_}->{content} =~ s#>$##;
716                    } keys %{ $config->{format} };
717            }
718    
719          # helper for progress bar          # helper for progress bar
720          sub fmt_time {          sub fmt_time {
# Line 726  print STDERR "reading ./import_xml/$type Line 776  print STDERR "reading ./import_xml/$type
776          }          }
777    
778          # now read database          # now read database
 print STDERR "using: $type...\n";  
779    
780          # erase cache for tags by order in this database          # erase cache for tags by order in this database
781          delete $cache->{tags_by_order};          delete $cache->{tags_by_order};
# Line 738  print STDERR "using: $type...\n"; Line 787  print STDERR "using: $type...\n";
787                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
788                  my $db = new Biblio::Isis( isisdb => $isis_db );                  my $db = new Biblio::Isis( isisdb => $isis_db );
789    
790                  my $max_rowid = $db->count;                  if (! $db) {
791                            print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n";
                 if (! $max_rowid) {  
                         print STDERR "FATAL: can't read database: $isis_db, skipping...\n";  
792                          next;                          next;
793                  }                  }
794    
795                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";                  my $max_rowid = $db->count if ($db);
796    
797                  $path = $database;                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
798    
799                  for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                  for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
800                          my $row = $db->to_hash( $row_id );                          my $row = $db->to_hash( $row_id );
# Line 780  print STDERR "using: $type...\n"; Line 827  print STDERR "using: $type...\n";
827                  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!";
828    
829                  my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";                  my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
830                  my $start_row = x($config->{start_row}) - 1 || die "no start_row in $type.xml";                  my $start_row = x($config->{start_row}) || die "no start_row in $type.xml";
831                    $start_row--;
832    
833                  my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";                  my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
834    
# Line 830  print STDERR "using: $type...\n"; Line 878  print STDERR "using: $type...\n";
878                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
879                          }                          }
880                  }                  }
881    
882                    print STDERR "\n";
883    
884          } elsif ($type_base eq "marc") {          } elsif ($type_base eq "marc") {
885    
886                  require MARC::File::USMARC;                  require MARC::File::USMARC;
# Line 841  print STDERR "using: $type...\n"; Line 892  print STDERR "using: $type...\n";
892                  warn "marc_format is no longer used!" if ($config->{marc_format});                  warn "marc_format is no longer used!" if ($config->{marc_format});
893                  print STDERR "Reading MARC file '$marc_file'\n";                  print STDERR "Reading MARC file '$marc_file'\n";
894    
895                  my $marc = MARC::File::USMARC->in( $marc_file )                  my $marc = MARC::File::USMARC->in( $marc_file );
896                          || die "Can't open MARC file '$marc_file': ".$MARC::File::ERROR;  
897                    if (! $marc) {
898                            print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n";
899                            next;
900                    }
901    
902                  # count records in MARC file                  # count records in MARC file
903                  sub marc_count {                  sub marc_count {
904                          my $filename = shift || die;                          my $filename = shift || die;
905                          my $file = MARC::File::USMARC->in($filename) || die $MARC::File::ERROR;                          my $file = MARC::File::USMARC->in($filename) || return;
906                          my $count = 0;                          my $count = 0;
907                          while ($file->skip()) {                          while ($file->skip()) {
908                                  $count++;                                  $count++;
# Line 857  print STDERR "using: $type...\n"; Line 912  print STDERR "using: $type...\n";
912    
913                  my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";                  my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";
914    
915                  my $i = 0;                  my $i = 1;
916    
917                  while( my $rec = $marc->next() ) {                  while( my $rec = $marc->next() ) {
918    
919                          progress($i++,$count);                          progress($i,$count);
920    
921                          my $swishpath = $database."#".$i;                          my $swishpath = $database."#".$i;
922    
# Line 872  print STDERR "using: $type...\n"; Line 927  print STDERR "using: $type...\n";
927                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
928                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
929                          }                          }
930    
931                            $i++;
932                  }                  }
933    
934                  print STDERR "\n";                  print STDERR "\n";
# Line 916  print STDERR "using: $type...\n"; Line 973  print STDERR "using: $type...\n";
973                  }                  }
974                  # close lookup                  # close lookup
975                  untie %lhash if (%lhash);                  untie %lhash if (%lhash);
976    
977            } elsif ($type_base eq "dbf") {
978    
979                    my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!";
980                    my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!";
981                    my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!";
982    
983                    $import2cp = Text::Iconv->new($dbf_codepage,$codepage);
984                    require XBase;
985                    my $db = new XBase $dbf_file;
986    
987                    if (! $db) {
988                            print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n";
989                            next;
990                    }
991    
992                    my $max_rowid = $db->last_record;
993    
994                    print STDERR "Reading database: $dbf_file [$max_rowid rows]\n";
995    
996                    my %dbf2iso;
997                    foreach my $m (split(/[\n\r]+/,$dbf_mapping)) {
998                            my ($col,$fld) = split(/\s+/,$m,2);
999                            $dbf2iso{$col} = $fld;
1000                    }
1001    
1002    #print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n";
1003    
1004                    # bad, bad...
1005                    require "to_hash.pm";
1006    
1007                    foreach my $row_id (0 .. $max_rowid) {
1008                            my $dbf_row = $db->get_record_as_hash($row_id);
1009                            if ($dbf_row) {
1010    
1011    #print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n";
1012                                    # apply mapping from config file
1013                                    # all unspecified records will get _ in
1014                                    # front of them - _DELETE will be __DELETE
1015                                    my $rec;
1016                                    map {
1017                                            my $new_fld = $dbf2iso{$_} || '_'.$_;
1018                                            my $data = $dbf_row->{$_};
1019                                            push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/);
1020                                    } keys %{$dbf_row};
1021    #print STDERR "## rec: ",Dumper($rec),"\n## /rec\n";
1022                                    my $row = to_hash($row_id+1, $rec);
1023    
1024                                    $row->{mfn} = $row_id+1;
1025                                    $row->{record} = $rec;
1026    
1027    #print STDERR "## row: ",Dumper($row),"\n## /row\n";
1028                                    progress($row->{mfn}, $max_rowid);
1029    
1030                                    my $swishpath = $path."#".int($row->{mfn});
1031    
1032                                    if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
1033                                            $xml = $cp2utf->convert($xml);
1034                                            use bytes;      # as opposed to chars
1035                                            print "Path-Name: $swishpath\n";
1036                                            print "Content-Length: ".(length($xml)+1)."\n";
1037                                            print "Document-Type: XML\n\n$xml\n";
1038                                    }
1039                            }
1040                    }
1041                    print STDERR "\n";
1042          }          }
1043  }  }
1044    
# Line 936  all2xml.pl - read various file formats a Line 1059  all2xml.pl - read various file formats a
1059    
1060  =head1 DESCRIPTION  =head1 DESCRIPTION
1061    
1062  This command will read ISIS data file using IsisDB perl module, MARC  This command will read ISIS data file using Biblio::Isis perl module, MARC
1063  records using MARC module and optionally Micro$oft Excel files to  records using MARC::File module and optionally Micro$oft Excel files to
1064  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,
1065  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
1066  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.

Legend:
Removed from v.647  
changed lines
  Added in v.747

  ViewVC Help
Powered by ViewVC 1.1.26