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 |
|
|
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; |
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 |
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 |
|
|
126 |
delete $cache->{swish_exact_data}; |
delete $cache->{swish_exact_data}; |
127 |
delete $cache->{index_data}; |
delete $cache->{index_data}; |
128 |
delete $cache->{index_delimiter}; |
delete $cache->{index_delimiter}; |
129 |
|
delete $cache->{distinct}; |
130 |
my @page_fields; # names of fields |
my @page_fields; # names of fields |
131 |
|
|
132 |
|
|
226 |
|
|
227 |
my ($swish,$display); |
my ($swish,$display); |
228 |
|
|
229 |
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"; |
230 |
|
|
231 |
# is this field page-by-page? |
# is this field page-by-page? |
232 |
my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page}; |
my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page}; |
235 |
# default line_delimiter if using |
# default line_delimiter if using |
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 |
|
my $distinct = $config->{indexer}->{$field}->{distinct}; |
239 |
|
if ($distinct && !$iterate_by_page) { |
240 |
|
warn "WARNING: distinct is currently not supported without iterate_by_page!\n"; |
241 |
|
$distinct = 0; |
242 |
|
} |
243 |
|
|
244 |
my $format_name = $config->{indexer}->{$field}->{format_name}; |
my $format_name = $config->{indexer}->{$field}->{format_name}; |
245 |
my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter}; |
my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter}; |
269 |
# init vars so that we go into while... |
# init vars so that we go into while... |
270 |
($swish,$display) = (1,1); |
($swish,$display) = (1,1); |
271 |
|
|
|
# placeholder for all repeatable entries for index |
|
|
|
|
272 |
sub mkformat($$) { |
sub mkformat($$) { |
273 |
my $x = shift || die "mkformat needs tag reference"; |
my $x = shift || die "mkformat needs tag reference"; |
274 |
my $data = shift || return; |
my $data = shift || return; |
418 |
$ldel = " " if ($append); |
$ldel = " " if ($append); |
419 |
#print STDERR "line delimiter: ",Dumper($ldel) if ($ldel); |
#print STDERR "line delimiter: ",Dumper($ldel) if ($ldel); |
420 |
if (! $cache->{$what}->{$field}->[$page]) { |
if (! $cache->{$what}->{$field}->[$page]) { |
421 |
$cache->{$what}->{$field}->[$page] = $data; |
push @{$cache->{$what}->{$field}->[$page]}, { |
422 |
} else { |
data => $data, |
423 |
$cache->{$what}->{$field}->[$page] .= $ldel.$data; |
delimiter => $ldel, |
424 |
|
}; |
425 |
} |
} |
426 |
} |
} |
427 |
|
|
496 |
# on first page!!! |
# on first page!!! |
497 |
my $page = 0; |
my $page = 0; |
498 |
if ($display_data) { |
if ($display_data) { |
499 |
$cache->{display_data}->{$field}->[$page] = $display_data; |
push @{$cache->{display_data}->{$field}->[$page]}, { data => $display_data }; |
500 |
$display_data = ""; |
$display_data = ""; |
501 |
} |
} |
502 |
if ($swish_data) { |
if ($swish_data) { |
503 |
$cache->{swish_data}->{$field}->[$page] = $swish_data; |
push @{$cache->{swish_data}->{$field}->[$page]}, { data => $swish_data }; |
504 |
$swish_data = ""; |
$swish_data = ""; |
505 |
} |
} |
506 |
if ($swish_exact_data) { |
if ($swish_exact_data) { |
507 |
$cache->{swish_exact_data}->{$field}->[$page] = $swish_exact_data; |
push @{$cache->{swish_exact_data}->{$field}->[$page]}, { data => $swish_exact_data }; |
508 |
$swish_exact_data = ""; |
$swish_exact_data = ""; |
509 |
} |
} |
510 |
} |
} |
515 |
my $nr_pages = $page_max{$field} || next; |
my $nr_pages = $page_max{$field} || next; |
516 |
#print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n"; |
#print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n"; |
517 |
#print STDERR Dumper($cache->{display_data}); |
#print STDERR Dumper($cache->{display_data}); |
518 |
|
my $seen; # used for distinct |
519 |
for (my $page=0; $page <= $nr_pages; $page++) { |
for (my $page=0; $page <= $nr_pages; $page++) { |
520 |
my $display_data; |
my $display_data; |
521 |
if ($cache->{format}->{$field}) { |
my $delimiter = ''; |
522 |
my $tmp = mkformat($cache->{format}->{$field},$cache->{display_data}->{$field}->[$page]); |
foreach my $element (@{ $cache->{display_data}->{$field}->[$page] }) { |
523 |
$display_data=$tmp if ($tmp); |
my $data = $element->{data}; |
524 |
} else { |
die "BUG! no data in element?" unless ($data); |
525 |
$display_data = $cache->{display_data}->{$field}->[$page]; |
|
526 |
|
if ($distinct) { |
527 |
|
next if ($cache->{distinct}->{$field}->{ $data }); |
528 |
|
$cache->{distinct}->{$field}->{ $data } = 1; |
529 |
|
} |
530 |
|
|
531 |
|
if ($cache->{format}->{$field}) { |
532 |
|
my $tmp = mkformat($cache->{format}->{$field},$data); |
533 |
|
$display_data .= $delimiter . $tmp if ($tmp); |
534 |
|
} else { |
535 |
|
$display_data .= $delimiter . $data; |
536 |
|
} |
537 |
|
$delimiter = $element->{delimiter} if ($element->{delimiter}); |
538 |
} |
} |
539 |
|
|
540 |
if ($display_data) { # default |
if ($display_data) { # default |
541 |
if ($field eq "headline") { |
if ($field eq "headline") { |
542 |
$xml .= xmlify("headline", $display_data); |
$xml .= xmlify("headline", $display_data); |
548 |
} |
} |
549 |
} |
} |
550 |
|
|
551 |
my $swish_data = $cache->{swish_data}->{$field}->[$page]; |
my $swish_data = join(" ",map { $_->{data} } @{ $cache->{swish_data}->{$field}->[$page] }); |
552 |
if ($swish_data) { |
if ($swish_data) { |
553 |
# remove extra spaces |
# remove extra spaces |
554 |
$swish_data =~ s/ +/ /g; |
$swish_data =~ s/ +/ /g; |
557 |
$xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data)); |
$xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data)); |
558 |
} |
} |
559 |
|
|
560 |
my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page]; |
my $swish_exact_data = join(" ", map { $_->{data} } @{ $cache->{swish_exact_data}->{$field}->[$page] }); |
561 |
if ($swish_exact_data) { |
if ($swish_exact_data) { |
562 |
$swish_exact_data =~ s/ +/ /g; |
$swish_exact_data =~ s/ +/ /g; |
563 |
$swish_exact_data =~ s/ +$//g; |
$swish_exact_data =~ s/ +$//g; |
659 |
|
|
660 |
foreach my $database ($cfg->Sections) { |
foreach my $database ($cfg->Sections) { |
661 |
|
|
662 |
|
# save database name in global variable path for later |
663 |
|
# (need for index filter creation) |
664 |
|
$path = $database; |
665 |
|
|
666 |
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"; |
667 |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
668 |
|
|
688 |
print STDERR "opening lookup file '$lookup_file'\n"; |
print STDERR "opening lookup file '$lookup_file'\n"; |
689 |
} |
} |
690 |
|
|
691 |
print STDERR "reading ./import_xml/$type.xml\n"; |
my $import_xml_type = $cfg->val($database, 'import_xml_file') || $type; |
692 |
|
my $import_xml_file = "./import_xml/$import_xml_type.xml"; |
693 |
|
|
694 |
|
if (! -r $import_xml_file) { |
695 |
|
print STDERR "ERROR: file $import_xml_file not readable skipping!\n"; |
696 |
|
next; |
697 |
|
} |
698 |
|
|
699 |
|
print STDERR "reading $import_xml_file\n"; |
700 |
|
|
701 |
# extract just type basic |
# extract just type basic |
702 |
my $type_base = $type; |
my $type_base = $type; |
703 |
$type_base =~ s/_.+$//g; |
$type_base =~ s/_.+$//g; |
704 |
|
|
705 |
$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"; |
706 |
|
$config=XMLin($import_xml_file, ForceArray => [ $tag, 'config', 'format' ], ForceContent => 1 ); |
707 |
|
|
708 |
# helper for progress bar |
# helper for progress bar |
709 |
sub fmt_time { |
sub fmt_time { |
765 |
} |
} |
766 |
|
|
767 |
# now read database |
# now read database |
|
print STDERR "using: $type...\n"; |
|
768 |
|
|
769 |
# erase cache for tags by order in this database |
# erase cache for tags by order in this database |
770 |
delete $cache->{tags_by_order}; |
delete $cache->{tags_by_order}; |
776 |
$import2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
777 |
my $db = new Biblio::Isis( isisdb => $isis_db ); |
my $db = new Biblio::Isis( isisdb => $isis_db ); |
778 |
|
|
779 |
my $max_rowid = $db->count || die "can't find maxmfn"; |
if (! $db) { |
780 |
|
print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n"; |
781 |
|
next; |
782 |
|
} |
783 |
|
|
784 |
|
my $max_rowid = $db->count if ($db); |
785 |
|
|
786 |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
787 |
|
|
|
my $path = $database; |
|
|
|
|
788 |
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
789 |
my $row = $db->to_hash( $row_id ); |
my $row = $db->to_hash( $row_id ); |
790 |
if ($row) { |
if ($row) { |
816 |
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!"; |
817 |
|
|
818 |
my $sheet = x($config->{sheet}) || die "no sheet in $type.xml"; |
my $sheet = x($config->{sheet}) || die "no sheet in $type.xml"; |
819 |
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"; |
820 |
|
$start_row--; |
821 |
|
|
822 |
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'"; |
823 |
|
|
867 |
print "Document-Type: XML\n\n$xml\n"; |
print "Document-Type: XML\n\n$xml\n"; |
868 |
} |
} |
869 |
} |
} |
870 |
|
|
871 |
|
print STDERR "\n"; |
872 |
|
|
873 |
} elsif ($type_base eq "marc") { |
} elsif ($type_base eq "marc") { |
874 |
|
|
875 |
require MARC::File::USMARC; |
require MARC::File::USMARC; |
881 |
warn "marc_format is no longer used!" if ($config->{marc_format}); |
warn "marc_format is no longer used!" if ($config->{marc_format}); |
882 |
print STDERR "Reading MARC file '$marc_file'\n"; |
print STDERR "Reading MARC file '$marc_file'\n"; |
883 |
|
|
884 |
my $marc = MARC::File::USMARC->in( $marc_file ) |
my $marc = MARC::File::USMARC->in( $marc_file ); |
885 |
|| die "Can't open MARC file '$marc_file': ".$MARC::File::ERROR; |
|
886 |
|
if (! $marc) { |
887 |
|
print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n"; |
888 |
|
next; |
889 |
|
} |
890 |
|
|
891 |
# count records in MARC file |
# count records in MARC file |
892 |
sub marc_count { |
sub marc_count { |
893 |
my $filename = shift || die; |
my $filename = shift || die; |
894 |
my $file = MARC::File::USMARC->in($filename) || die $MARC::File::ERROR; |
my $file = MARC::File::USMARC->in($filename) || return; |
895 |
my $count = 0; |
my $count = 0; |
896 |
while ($file->skip()) { |
while ($file->skip()) { |
897 |
$count++; |
$count++; |
901 |
|
|
902 |
my $count = marc_count($marc_file) || warn "no records in '$marc_file'?"; |
my $count = marc_count($marc_file) || warn "no records in '$marc_file'?"; |
903 |
|
|
904 |
my $i = 0; |
my $i = 1; |
905 |
|
|
906 |
while( my $rec = $marc->next() ) { |
while( my $rec = $marc->next() ) { |
907 |
|
|
908 |
progress($i++,$count); |
progress($i,$count); |
909 |
|
|
910 |
my $swishpath = $database."#".$i; |
my $swishpath = $database."#".$i; |
911 |
|
|
916 |
print "Content-Length: ".(length($xml)+1)."\n"; |
print "Content-Length: ".(length($xml)+1)."\n"; |
917 |
print "Document-Type: XML\n\n$xml\n"; |
print "Document-Type: XML\n\n$xml\n"; |
918 |
} |
} |
919 |
|
|
920 |
|
$i++; |
921 |
} |
} |
922 |
|
|
923 |
print STDERR "\n"; |
print STDERR "\n"; |
962 |
} |
} |
963 |
# close lookup |
# close lookup |
964 |
untie %lhash if (%lhash); |
untie %lhash if (%lhash); |
965 |
|
|
966 |
|
} elsif ($type_base eq "dbf") { |
967 |
|
|
968 |
|
my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!"; |
969 |
|
my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!"; |
970 |
|
my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!"; |
971 |
|
|
972 |
|
$import2cp = Text::Iconv->new($dbf_codepage,$codepage); |
973 |
|
require XBase; |
974 |
|
my $db = new XBase $dbf_file; |
975 |
|
|
976 |
|
if (! $db) { |
977 |
|
print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n"; |
978 |
|
next; |
979 |
|
} |
980 |
|
|
981 |
|
my $max_rowid = $db->last_record; |
982 |
|
|
983 |
|
print STDERR "Reading database: $dbf_file [$max_rowid rows]\n"; |
984 |
|
|
985 |
|
my %dbf2iso; |
986 |
|
foreach my $m (split(/[\n\r]+/,$dbf_mapping)) { |
987 |
|
my ($col,$fld) = split(/\s+/,$m,2); |
988 |
|
$dbf2iso{$col} = $fld; |
989 |
|
} |
990 |
|
|
991 |
|
#print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n"; |
992 |
|
|
993 |
|
# bad, bad... |
994 |
|
require "to_hash.pm"; |
995 |
|
|
996 |
|
foreach my $row_id (0 .. $max_rowid) { |
997 |
|
my $dbf_row = $db->get_record_as_hash($row_id); |
998 |
|
if ($dbf_row) { |
999 |
|
|
1000 |
|
#print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n"; |
1001 |
|
# apply mapping from config file |
1002 |
|
# all unspecified records will get _ in |
1003 |
|
# front of them - _DELETE will be __DELETE |
1004 |
|
my $rec; |
1005 |
|
map { |
1006 |
|
my $new_fld = $dbf2iso{$_} || '_'.$_; |
1007 |
|
my $data = $dbf_row->{$_}; |
1008 |
|
push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/); |
1009 |
|
} keys %{$dbf_row}; |
1010 |
|
#print STDERR "## rec: ",Dumper($rec),"\n## /rec\n"; |
1011 |
|
my $row = to_hash($row_id+1, $rec); |
1012 |
|
|
1013 |
|
$row->{mfn} = $row_id+1; |
1014 |
|
$row->{record} = $rec; |
1015 |
|
|
1016 |
|
#print STDERR "## row: ",Dumper($row),"\n## /row\n"; |
1017 |
|
progress($row->{mfn}, $max_rowid); |
1018 |
|
|
1019 |
|
my $swishpath = $path."#".int($row->{mfn}); |
1020 |
|
|
1021 |
|
if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) { |
1022 |
|
$xml = $cp2utf->convert($xml); |
1023 |
|
use bytes; # as opposed to chars |
1024 |
|
print "Path-Name: $swishpath\n"; |
1025 |
|
print "Content-Length: ".(length($xml)+1)."\n"; |
1026 |
|
print "Document-Type: XML\n\n$xml\n"; |
1027 |
|
} |
1028 |
|
} |
1029 |
|
} |
1030 |
|
print STDERR "\n"; |
1031 |
} |
} |
1032 |
} |
} |
1033 |
|
|
1048 |
|
|
1049 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
1050 |
|
|
1051 |
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 |
1052 |
records using MARC module and optionally Micro$oft Excel files to |
records using MARC::File module and optionally Micro$oft Excel files to |
1053 |
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, |
1054 |
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 |
1055 |
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. |