1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
use Biblio::Isis; |
use Biblio::Isis 0.23; |
5 |
use Getopt::Std; |
use Getopt::Std; |
6 |
use Data::Dumper; |
use Data::Dumper; |
7 |
use XML::Simple; |
use XML::Simple; |
8 |
use Text::Iconv; |
use Text::Iconv; |
9 |
use Config::IniFiles; |
use Config::IniFiles; |
10 |
use Encode; |
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; |
use Carp; |
15 |
|
|
16 |
$|=1; |
$|=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; |
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 |
|
|
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; |
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}; |
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 |
|
|
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) { |
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 |
} |
} |
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); |
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; |
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; |
673 |
# create new lookup file |
# create new lookup file |
674 |
my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional |
my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional |
675 |
if ($lookup_file) { |
if ($lookup_file) { |
|
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644; |
|
676 |
if (! -e $lookup_file) { |
if (! -e $lookup_file) { |
677 |
open(LOOKUP, "> $lookup_file") || die "can't create $lookup_file': $!"; |
open(LOOKUP, "> $lookup_file") || die "can't create $lookup_file': $!"; |
678 |
close(LOOKUP); |
close(LOOKUP); |
679 |
} |
} |
680 |
tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644; |
tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644; |
681 |
|
#tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644; |
682 |
print STDERR "creating lookup file '$lookup_file'\n"; |
print STDERR "creating lookup file '$lookup_file'\n"; |
683 |
# delete memory cache for lookup file |
# delete memory cache for lookup file |
684 |
delete $cache->{lhash}; |
delete $cache->{lhash}; |
687 |
# open existing lookup file |
# open existing lookup file |
688 |
$lookup_file = $cfg -> val($database, 'lookup_open'); # optional |
$lookup_file = $cfg -> val($database, 'lookup_open'); # optional |
689 |
if ($lookup_file) { |
if ($lookup_file) { |
690 |
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644; |
tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644; |
691 |
tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644; |
#tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644; |
692 |
print STDERR "opening lookup file '$lookup_file'\n"; |
print STDERR "opening lookup file '$lookup_file'\n"; |
693 |
} |
} |
694 |
|
|
709 |
my $tag = $cfg->val($database, 'import_xml_tag') || $type2tag{$type_base} || die "can't find which tag to use for type $type"; |
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 ); |
$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 { |
721 |
my $t = shift || 0; |
my $t = shift || 0; |
785 |
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!"; |
786 |
|
|
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( |
789 |
|
isisdb => $isis_db, |
790 |
|
join_subfields_with => ' ; ', |
791 |
|
); |
792 |
|
|
793 |
if (! $db) { |
if (! $db) { |
794 |
print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n"; |
print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n"; |
830 |
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!"; |
831 |
|
|
832 |
my $sheet = x($config->{sheet}) || die "no sheet in $type.xml"; |
my $sheet = x($config->{sheet}) || die "no sheet in $type.xml"; |
833 |
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"; |
834 |
|
$start_row--; |
835 |
|
|
836 |
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'"; |
837 |
|
|