/[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 619 by dpavlin, Fri Dec 31 04:22:49 2004 UTC revision 726 by dpavlin, Thu Apr 13 19:35:19 2006 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  use strict;
4  use IsisDB;  use Biblio::Isis;
5  use Getopt::Std;  use Getopt::Std;
6  use Data::Dumper;  use Data::Dumper;
7  use XML::Simple;  use XML::Simple;
# 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 22  die "FATAL: can't find configuration fil Line 23  die "FATAL: can't find configuration fil
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  );  );
68    
69  my $cache;      # for cacheing  my $cache;      # for cacheing
# Line 111  sub data2xml { Line 113  sub data2xml {
113                  $cache->{tags_by_order} = \@sorted_tags;                  $cache->{tags_by_order} = \@sorted_tags;
114          }          }
115    
116            if (! @sorted_tags) {
117                    print STDERR "WARNING: no tags for this type found in import_xml file!\n";
118            }
119    
120          # lookup key          # lookup key
121          my $lookup_key;          my $lookup_key;
122    
# Line 219  sub data2xml { Line 225  sub data2xml {
225    
226                  my ($swish,$display);                  my ($swish,$display);
227    
228                  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";
229    
230                  # is this field page-by-page?                  # is this field page-by-page?
231                  my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page};                  my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page};
# Line 257  sub data2xml { Line 263  sub data2xml {
263                          # init vars so that we go into while...                          # init vars so that we go into while...
264                          ($swish,$display) = (1,1);                          ($swish,$display) = (1,1);
265    
                         # placeholder for all repeatable entries for index  
   
266                          sub mkformat($$) {                          sub mkformat($$) {
267                                  my $x = shift || die "mkformat needs tag reference";                                  my $x = shift || die "mkformat needs tag reference";
268                                  my $data = shift || return;                                  my $data = shift || return;
# Line 634  if ($my_unac_filter) { Line 638  if ($my_unac_filter) {
638    
639  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
640    
641            # save database name in global variable path for later
642            # (need for index filter creation)
643            $path = $database;
644    
645          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";
646          my $add_xml = $cfg -> val($database, 'xml');    # optional          my $add_xml = $cfg -> val($database, 'xml');    # optional
647    
# Line 659  foreach my $database ($cfg->Sections) { Line 667  foreach my $database ($cfg->Sections) {
667                  print STDERR "opening lookup file '$lookup_file'\n";                  print STDERR "opening lookup file '$lookup_file'\n";
668          }          }
669    
670  print STDERR "reading ./import_xml/$type.xml\n";          my $import_xml_type = $cfg->val($database, 'import_xml_file') || $type;
671            my $import_xml_file = "./import_xml/$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 );          my $tag = $cfg->val($database, 'import_xml_tag') || $type2tag{$type_base} || die "can't find which tag to use for type $type";
685            $config=XMLin($import_xml_file, ForceArray => [ $tag, 'config', 'format' ], ForceContent => 1 );
686    
687          # helper for progress bar          # helper for progress bar
688          sub fmt_time {          sub fmt_time {
# Line 684  print STDERR "reading ./import_xml/$type Line 701  print STDERR "reading ./import_xml/$type
701          my $start_t = time();          my $start_t = time();
702          sub progress {          sub progress {
703                  return if (! $show_progress);                  return if (! $show_progress);
704                  my $current = shift || 1;                  my $current = shift;
705                  my $total = shift || 1;                  my $total = shift || 1;
706                  my $p = int($current * 100 / $total);                  my $p = int($current * 100 / $total);
707                  if ($p != $last_p) {                  if ($p < $last_p || $current == 1) {
708                            $start_t = time();
709                            $last_p = 0;
710                    } elsif ($p != $last_p) {
711                          my $rate = ($current / (time() - $start_t || 1));                          my $rate = ($current / (time() - $start_t || 1));
712                          my $eta = ($total-$current) / ($rate || 1);                          my $eta = ($total-$current) / ($rate || 1);
713                          printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$current,"=" x ($p/3)."$p%>", $total, $rate, fmt_time($eta));                          printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$current,"=" x ($p/3)."$p%>", $total, $rate, fmt_time($eta));
# Line 696  print STDERR "reading ./import_xml/$type Line 716  print STDERR "reading ./import_xml/$type
716          }          }
717    
718          my $fake_dir = 1;          my $fake_dir = 1;
719            my $fake_pos = 0;
720            my $last_fake_t = time();
721          sub fakeprogress {          sub fakeprogress {
722                  return if (! $show_progress);                  return if (! $show_progress);
723                  my $current = shift @_;                  my $current = shift @_;
724    
725                  my @ind = ('-','\\','|','/','-','\\','|','/', '-');                  my @ind = ('-','\\','|','/','-','\\','|','/');
726    
727                  $last_p += $fake_dir;                  if ($current < $fake_pos) {
728                  $fake_dir = -$fake_dir if ($last_p > 1000 || $last_p < 0);                          $start_t = time();
729                  if ($last_p % 10 == 0) {                          $last_fake_t = 0;
730                          printf STDERR ("%5d / %5s [%-51s]\r",$current,"?"," " x ($last_p/20).$ind[($last_p/20) % $#ind]);                          $fake_dir = 1;
731                            $fake_pos = 0;
732                    }
733    
734                    if (time()-$last_fake_t >= 1) {
735                            $last_fake_t = time();
736                            $fake_pos += $fake_dir;
737                            $fake_dir = -$fake_dir if ($fake_pos > 38);
738                    }
739    
740                    if ($current % 10 == 0) {
741                            my $rate = ($current / (time() - $start_t || 1));
742                            printf STDERR ("%5d [%-38s] %0.1f/s\r",$current, " " x $fake_pos .$ind[($current / 10) % 8], $rate);
743                  }                  }
744          }          }
745    
746          # now read database          # now read database
 print STDERR "using: $type...\n";  
747    
748          # erase cache for tags by order in this database          # erase cache for tags by order in this database
749          delete $cache->{tags_by_order};          delete $cache->{tags_by_order};
# Line 720  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 = new IsisDB( isisdb => $isis_db );                  my $db = new Biblio::Isis( isisdb => $isis_db );
757    
758                  my $max_rowid = $db->{'maxmfn'} || die "can't find maxmfn";                  if (! $db) {
759                            print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n";
760                            next;
761                    }
762    
763                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";                  my $max_rowid = $db->count if ($db);
764    
765                  my $path = $database;                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
766    
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 = $db->to_hash( $row_id );                          my $row = $db->to_hash( $row_id );
# Line 759  print STDERR "using: $type...\n"; Line 795  print STDERR "using: $type...\n";
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!";
796    
797                  my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";                  my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
798                  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";
799                    $start_row--;
800    
801                  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'";
802    
# Line 809  print STDERR "using: $type...\n"; Line 846  print STDERR "using: $type...\n";
846                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
847                          }                          }
848                  }                  }
849    
850                    print STDERR "\n";
851    
852          } elsif ($type_base eq "marc") {          } elsif ($type_base eq "marc") {
853    
854                  require MARC;                  require MARC::File::USMARC;
855                                    
856                  $import2cp = Text::Iconv->new($config->{marc_codepage},$codepage);                  $import2cp = Text::Iconv->new($config->{marc_codepage},$codepage);
857                  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!";
858    
859                  # optional argument is format                  # optional argument is format
860                  my $format = x($config->{marc_format}) || 'usmarc';                  warn "marc_format is no longer used!" if ($config->{marc_format});
   
861                  print STDERR "Reading MARC file '$marc_file'\n";                  print STDERR "Reading MARC file '$marc_file'\n";
862    
863                  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' with format '$format'";  
864    
865                  # read MARC file in memory                  if (! $marc) {
866                  $marc->nextmarc(-1);                          print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n";
867                            next;
868                    }
869    
870                    # count records in MARC file
871                    sub marc_count {
872                            my $filename = shift || die;
873                            my $file = MARC::File::USMARC->in($filename) || return;
874                            my $count = 0;
875                            while ($file->skip()) {
876                                    $count++;
877                            }
878                            return $count;
879                    }
880    
881                  my $max_rec = $marc->marc_count();                  my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";
882    
883                  for(my $i=1; $i<=$max_rec; $i++) {                  my $i = 1;
884    
885                          progress($i,$max_rec);                  while( my $rec = $marc->next() ) {
886    
887                          # store value for marc_sf.pm                          progress($i,$count);
                         $main::cache->{marc_record} = $i;  
888    
889                          my $swishpath = $database."#".$i;                          my $swishpath = $database."#".$i;
890    
891                          if (my $xml = data2xml($type_base,$marc,$add_xml,$cfg,$database)) {                          if (my $xml = data2xml($type_base,$rec,$add_xml,$cfg,$database)) {
892                                  $xml = $cp2utf->convert($xml);                                  $xml = $cp2utf->convert($xml);
893                                  use bytes;      # as opposed to chars                                  use bytes;      # as opposed to chars
894                                  print "Path-Name: $swishpath\n";                                  print "Path-Name: $swishpath\n";
895                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
896                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
897                          }                          }
898    
899                            $i++;
900                  }                  }
901    
902                  print STDERR "\n";                  print STDERR "\n";
# Line 891  print STDERR "using: $type...\n"; Line 941  print STDERR "using: $type...\n";
941                  }                  }
942                  # close lookup                  # close lookup
943                  untie %lhash if (%lhash);                  untie %lhash if (%lhash);
944    
945            } elsif ($type_base eq "dbf") {
946    
947                    my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!";
948                    my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!";
949                    my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!";
950    
951                    $import2cp = Text::Iconv->new($dbf_codepage,$codepage);
952                    require XBase;
953                    my $db = new XBase $dbf_file;
954    
955                    if (! $db) {
956                            print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n";
957                            next;
958                    }
959    
960                    my $max_rowid = $db->last_record;
961    
962                    print STDERR "Reading database: $dbf_file [$max_rowid rows]\n";
963    
964                    my %dbf2iso;
965                    foreach my $m (split(/[\n\r]+/,$dbf_mapping)) {
966                            my ($col,$fld) = split(/\s+/,$m,2);
967                            $dbf2iso{$col} = $fld;
968                    }
969    
970    #print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n";
971    
972                    # bad, bad...
973                    require "to_hash.pm";
974    
975                    foreach my $row_id (0 .. $max_rowid) {
976                            my $dbf_row = $db->get_record_as_hash($row_id);
977                            if ($dbf_row) {
978    
979    #print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n";
980                                    # apply mapping from config file
981                                    # all unspecified records will get _ in
982                                    # front of them - _DELETE will be __DELETE
983                                    my $rec;
984                                    map {
985                                            my $new_fld = $dbf2iso{$_} || '_'.$_;
986                                            my $data = $dbf_row->{$_};
987                                            push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/);
988                                    } keys %{$dbf_row};
989    #print STDERR "## rec: ",Dumper($rec),"\n## /rec\n";
990                                    my $row = to_hash($row_id+1, $rec);
991    
992                                    $row->{mfn} = $row_id+1;
993                                    $row->{record} = $rec;
994    
995    #print STDERR "## row: ",Dumper($row),"\n## /row\n";
996                                    progress($row->{mfn}, $max_rowid);
997    
998                                    my $swishpath = $path."#".int($row->{mfn});
999    
1000                                    if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
1001                                            $xml = $cp2utf->convert($xml);
1002                                            use bytes;      # as opposed to chars
1003                                            print "Path-Name: $swishpath\n";
1004                                            print "Content-Length: ".(length($xml)+1)."\n";
1005                                            print "Document-Type: XML\n\n$xml\n";
1006                                    }
1007                            }
1008                    }
1009                    print STDERR "\n";
1010          }          }
1011  }  }
1012    
# Line 911  all2xml.pl - read various file formats a Line 1027  all2xml.pl - read various file formats a
1027    
1028  =head1 DESCRIPTION  =head1 DESCRIPTION
1029    
1030  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
1031  records using MARC module and optionally Micro$oft Excel files to  records using MARC::File module and optionally Micro$oft Excel files to
1032  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,
1033  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
1034  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.619  
changed lines
  Added in v.726

  ViewVC Help
Powered by ViewVC 1.1.26