/[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 628 by dpavlin, Sun Jan 2 22:09:01 2005 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 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            'dbf' => 'isis',        # special case, re-use isis import_xml
68  );  );
69    
70  my $cache;      # for cacheing  my $cache;      # for cacheing
# Line 111  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 257  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    
                         # placeholder for all repeatable entries for index  
   
267                          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;
# Line 634  if ($my_unac_filter) { Line 639  if ($my_unac_filter) {
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 659  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          # helper for progress bar
687          sub fmt_time {          sub fmt_time {
# Line 737  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 826  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                  require MARC::File::USMARC;                  require MARC::File::USMARC;
# Line 837  print STDERR "using: $type...\n"; Line 859  print STDERR "using: $type...\n";
859                  warn "marc_format is no longer used!" if ($config->{marc_format});                  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 = MARC::File::USMARC->in( $marc_file )                  my $marc = MARC::File::USMARC->in( $marc_file );
863                          || die "Can't open MARC file '$marc_file': ".$MARC::File::ERROR;  
864                    if (! $marc) {
865                            print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n";
866                            next;
867                    }
868    
869                  # count records in MARC file                  # count records in MARC file
870                  sub marc_count {                  sub marc_count {
871                          my $filename = shift || die;                          my $filename = shift || die;
872                          my $file = MARC::File::USMARC->in($filename) || die $MARC::File::ERROR;                          my $file = MARC::File::USMARC->in($filename) || return;
873                          my $count = 0;                          my $count = 0;
874                          while ($file->skip()) {                          while ($file->skip()) {
875                                  $count++;                                  $count++;
# Line 853  print STDERR "using: $type...\n"; Line 879  print STDERR "using: $type...\n";
879    
880                  my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";                  my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";
881    
882                  my $i = 0;                  my $i = 1;
883    
884                  while( my $rec = $marc->next() ) {                  while( my $rec = $marc->next() ) {
885    
886                          progress($i++,$count);                          progress($i,$count);
887    
888                          my $swishpath = $database."#".$i;                          my $swishpath = $database."#".$i;
889    
# Line 868  print STDERR "using: $type...\n"; Line 894  print STDERR "using: $type...\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";                  print STDERR "\n";
# Line 912  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 932  all2xml.pl - read various file formats a Line 1026  all2xml.pl - read various file formats a
1026    
1027  =head1 DESCRIPTION  =head1 DESCRIPTION
1028    
1029  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
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.

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

  ViewVC Help
Powered by ViewVC 1.1.26