switch back to GDBM_File which is included in recent
[webpac] / all2xml.pl
index 1c5123b..e87fdbf 100755 (executable)
@@ -1,16 +1,17 @@
 #!/usr/bin/perl -w
 
 use strict;
-use Biblio::Isis;
+use Biblio::Isis 0.23;
 use Getopt::Std;
 use Data::Dumper;
 use XML::Simple;
 use Text::Iconv;
 use Config::IniFiles;
 use Encode;
-#use GDBM_File;
+use GDBM_File;
 use Fcntl;     # for O_RDWR
-use TDB_File;
+#use TDB_File;
+use Carp;
 
 $|=1;
 
@@ -62,7 +63,7 @@ my %type2tag = (
        'isis' => 'isis',
        'excel' => 'column',
        'marc' => 'marc',
-       'feed' => 'feed'
+       'feed' => 'feed',
 );
 
 my $cache;     # for cacheing
@@ -75,6 +76,9 @@ my $use_lhash_cache = 1;
 
 my $last_field_name;   # cache to prevent repeated fields
 
+my $broken_cdata = XMLin('<foo><![CDATA[<bar>]]></foo>') eq '<bar>>';
+warn "XML::Simple on this system seems broken with <![CDATA[..]]>.\n" if ($broken_cdata);
+
 sub data2xml {
 
        use xmlify;
@@ -112,6 +116,10 @@ sub data2xml {
                $cache->{tags_by_order} = \@sorted_tags;
        }
 
+       if (! @sorted_tags) {
+               print STDERR "WARNING: no tags for this type found in import_xml file!\n";
+       }
+
        # lookup key
        my $lookup_key;
 
@@ -121,6 +129,7 @@ sub data2xml {
        delete $cache->{swish_exact_data};
        delete $cache->{index_data};
        delete $cache->{index_delimiter};
+       delete $cache->{distinct};
        my @page_fields;        # names of fields
 
 
@@ -201,6 +210,7 @@ sub data2xml {
                        delete $x->{value};
                        delete $x->{delimiter};
                        $x->{content} = $v;
+                       $d =~ s#>$## if ($d && $broken_cdata);
                        $x->{delimiter} = $d;
                }
                return $x;
@@ -220,7 +230,7 @@ sub data2xml {
 
                my ($swish,$display);
 
-               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";
 
                # is this field page-by-page?
                my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page};
@@ -229,6 +239,11 @@ sub data2xml {
                # default line_delimiter if using
                my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>';
                $cache->{index_delimiter}->{$field} = $config->{indexer}->{$field}->{index_delimiter};
+               my $distinct = $config->{indexer}->{$field}->{distinct};
+               if ($distinct && !$iterate_by_page) {
+                       warn "WARNING: distinct is currently not supported without iterate_by_page!\n";
+                       $distinct = 0;
+               }
 
                my $format_name = $config->{indexer}->{$field}->{format_name};
                my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter};
@@ -407,9 +422,10 @@ sub data2xml {
                                                $ldel = " " if ($append);
 #print STDERR "line delimiter: ",Dumper($ldel) if ($ldel);
                                                if (! $cache->{$what}->{$field}->[$page]) {
-                                                       $cache->{$what}->{$field}->[$page] = $data;
-                                               } else {
-                                                       $cache->{$what}->{$field}->[$page] .= $ldel.$data;
+                                                       push @{$cache->{$what}->{$field}->[$page]}, {
+                                                               data => $data,
+                                                               delimiter => $ldel,
+                                                       };
                                                }
                                        }
 
@@ -476,7 +492,7 @@ sub data2xml {
                        if ($val) {
                                $display_data .= $delimiter.$val if ($d);
                                $swish_data .= " ".$val if ($s);
-                               $index->insert($field, $val, $path) if ($i);
+                               $index->insert($field, $val, $val, $path) if ($i);
                        }
 
                        if ($iterate_by_page) {
@@ -484,15 +500,15 @@ sub data2xml {
                                # on first page!!!
                                my $page = 0;
                                if ($display_data) {
-                                       $cache->{display_data}->{$field}->[$page] = $display_data;
+                                       push @{$cache->{display_data}->{$field}->[$page]}, { data => $display_data };
                                        $display_data = "";
                                }
                                if ($swish_data) {
-                                       $cache->{swish_data}->{$field}->[$page] = $swish_data;
+                                       push @{$cache->{swish_data}->{$field}->[$page]}, { data => $swish_data };
                                        $swish_data = "";
                                }
                                if ($swish_exact_data) {
-                                       $cache->{swish_exact_data}->{$field}->[$page] = $swish_exact_data;
+                                       push @{$cache->{swish_exact_data}->{$field}->[$page]}, { data => $swish_exact_data };
                                        $swish_exact_data = "";
                                }
                        }
@@ -503,14 +519,28 @@ sub data2xml {
                        my $nr_pages = $page_max{$field} || next;
 #print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n";
 #print STDERR Dumper($cache->{display_data});
+                       my $seen;       # used for distinct
                        for (my $page=0; $page <= $nr_pages; $page++) {
                                my $display_data;
-                               if ($cache->{format}->{$field}) {
-                                       my $tmp = mkformat($cache->{format}->{$field},$cache->{display_data}->{$field}->[$page]);
-                                       $display_data=$tmp if ($tmp);
-                               } else {
-                                       $display_data = $cache->{display_data}->{$field}->[$page];
+                               my $delimiter = '';
+                               foreach my $element (@{ $cache->{display_data}->{$field}->[$page] }) {
+                                       my $data = $element->{data};
+                                       die "BUG! no data in element?" unless ($data);
+
+                                       if ($distinct) {
+                                               next if ($cache->{distinct}->{$field}->{ $data });
+                                               $cache->{distinct}->{$field}->{ $data } = 1;
+                                       }
+
+                                       if ($cache->{format}->{$field}) {
+                                               my $tmp = mkformat($cache->{format}->{$field},$data);
+                                               $display_data .= $delimiter . $tmp if ($tmp);
+                                       } else {
+                                               $display_data .= $delimiter . $data;
+                                       }
+                                       $delimiter = $element->{delimiter} if ($element->{delimiter});
                                }
+
                                if ($display_data) { # default
                                        if ($field eq "headline") {
                                                $xml .= xmlify("headline", $display_data);
@@ -522,7 +552,7 @@ sub data2xml {
                                        }
                                }
                                
-                               my $swish_data = $cache->{swish_data}->{$field}->[$page];
+                               my $swish_data = join(" ",map { $_->{data} } @{ $cache->{swish_data}->{$field}->[$page] });
                                if ($swish_data) {
                                        # remove extra spaces
                                        $swish_data =~ s/ +/ /g;
@@ -531,7 +561,7 @@ sub data2xml {
                                        $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
                                }
 
-                               my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page];
+                               my $swish_exact_data = join(" ", map { $_->{data} } @{ $cache->{swish_exact_data}->{$field}->[$page] });
                                if ($swish_exact_data) {
                                        $swish_exact_data =~ s/ +/ /g;
                                        $swish_exact_data =~ s/ +$//g;
@@ -643,12 +673,12 @@ foreach my $database ($cfg->Sections) {
        # create new lookup file
        my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional
        if ($lookup_file) {
-               #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644;
                if (! -e $lookup_file) {
                        open(LOOKUP, "> $lookup_file") || die "can't create $lookup_file': $!";
                        close(LOOKUP);
                }
-               tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644;
+               tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644;
+               #tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644;
                print STDERR "creating lookup file '$lookup_file'\n";
                # delete memory cache for lookup file
                delete $cache->{lhash};
@@ -657,18 +687,34 @@ foreach my $database ($cfg->Sections) {
        # open existing lookup file
        $lookup_file = $cfg -> val($database, 'lookup_open'); # optional
        if ($lookup_file) {
-               #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644;
-               tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644;
+               tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644;
+               #tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644;
                print STDERR "opening lookup file '$lookup_file'\n";
        }
 
-print STDERR "reading ./import_xml/$type.xml\n";
+       my $import_xml_type = $cfg->val($database, 'import_xml_file') || $type;
+       my $import_xml_file = "./import_xml/$import_xml_type.xml";
+
+       if (! -r $import_xml_file) {
+               print STDERR "ERROR: file $import_xml_file not readable skipping!\n";
+               next;
+       }
+
+       print STDERR "reading $import_xml_file\n";
 
        # extract just type basic
        my $type_base = $type;
        $type_base =~ s/_.+$//g;
 
-       $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";
+       $config=XMLin($import_xml_file, ForceArray => [ $tag, 'config', 'format' ], ForceContent => 1 );
+
+       # check for broken XML::Simple
+       if ( $broken_cdata ) {
+               map {
+                       $config->{format}->{$_}->{content} =~ s#>$##;
+               } keys %{ $config->{format} };
+       }
 
        # helper for progress bar
        sub fmt_time {
@@ -730,7 +776,6 @@ print STDERR "reading ./import_xml/$type.xml\n";
        }
 
        # now read database
-print STDERR "using: $type...\n";
 
        # erase cache for tags by order in this database
        delete $cache->{tags_by_order};
@@ -740,7 +785,10 @@ print STDERR "using: $type...\n";
                my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
 
                $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
-               my $db = new Biblio::Isis( isisdb => $isis_db );
+               my $db = new Biblio::Isis(
+                       isisdb => $isis_db,
+                       join_subfields_with => ' ; ',
+               );
 
                if (! $db) {
                        print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n";
@@ -782,7 +830,8 @@ print STDERR "using: $type...\n";
                my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
 
                my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
-               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";
+               $start_row--;
 
                my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
 
@@ -927,6 +976,72 @@ print STDERR "using: $type...\n";
                }
                # close lookup
                untie %lhash if (%lhash);
+
+       } elsif ($type_base eq "dbf") {
+
+               my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!";
+               my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!";
+               my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!";
+
+               $import2cp = Text::Iconv->new($dbf_codepage,$codepage);
+               require XBase;
+               my $db = new XBase $dbf_file;
+
+               if (! $db) {
+                       print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n";
+                       next;
+               }
+
+               my $max_rowid = $db->last_record;
+
+               print STDERR "Reading database: $dbf_file [$max_rowid rows]\n";
+
+               my %dbf2iso;
+               foreach my $m (split(/[\n\r]+/,$dbf_mapping)) {
+                       my ($col,$fld) = split(/\s+/,$m,2);
+                       $dbf2iso{$col} = $fld;
+               }
+
+#print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n";
+
+               # bad, bad...
+               require "to_hash.pm";
+
+               foreach my $row_id (0 .. $max_rowid) {
+                       my $dbf_row = $db->get_record_as_hash($row_id);
+                       if ($dbf_row) {
+
+#print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n";
+                               # apply mapping from config file
+                               # all unspecified records will get _ in
+                               # front of them - _DELETE will be __DELETE
+                               my $rec;
+                               map { 
+                                       my $new_fld = $dbf2iso{$_} || '_'.$_;
+                                       my $data = $dbf_row->{$_};
+                                       push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/);
+                               } keys %{$dbf_row};
+#print STDERR "## rec: ",Dumper($rec),"\n## /rec\n";
+                               my $row = to_hash($row_id+1, $rec);
+
+                               $row->{mfn} = $row_id+1;
+                               $row->{record} = $rec;
+
+#print STDERR "## row: ",Dumper($row),"\n## /row\n";
+                               progress($row->{mfn}, $max_rowid);
+
+                               my $swishpath = $path."#".int($row->{mfn});
+
+                               if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
+                                       $xml = $cp2utf->convert($xml);
+                                       use bytes;      # as opposed to chars
+                                       print "Path-Name: $swishpath\n";
+                                       print "Content-Length: ".(length($xml)+1)."\n";
+                                       print "Document-Type: XML\n\n$xml\n";
+                               }
+                       }
+               }
+               print STDERR "\n";
        }
 }