--- branches/lezbib/all2xml.pl 2005/02/28 10:43:38 684
+++ branches/lezbib/all2xml.pl 2008/08/03 06:33:56 776
@@ -1,16 +1,16 @@
#!/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;
@@ -76,6 +76,9 @@
my $last_field_name; # cache to prevent repeated fields
+my $broken_cdata = XMLin(']]>') eq '>';
+warn "XML::Simple on this system seems broken with .\n" if ($broken_cdata);
+
sub data2xml {
use xmlify;
@@ -126,6 +129,7 @@
delete $cache->{swish_exact_data};
delete $cache->{index_data};
delete $cache->{index_delimiter};
+ delete $cache->{distinct};
my @page_fields; # names of fields
@@ -206,6 +210,7 @@
delete $x->{value};
delete $x->{delimiter};
$x->{content} = $v;
+ $d =~ s#>$## if ($d && $broken_cdata);
$x->{delimiter} = $d;
}
return $x;
@@ -234,6 +239,11 @@
# default line_delimiter if using
my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '
';
$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};
@@ -412,9 +422,10 @@
$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,
+ };
}
}
@@ -481,7 +492,7 @@
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) {
@@ -489,15 +500,15 @@
# 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 = "";
}
}
@@ -508,14 +519,28 @@
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);
@@ -527,7 +552,7 @@
}
}
- 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;
@@ -536,7 +561,7 @@
$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;
@@ -648,12 +673,12 @@
# 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};
@@ -662,8 +687,8 @@
# 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";
}
@@ -684,6 +709,13 @@
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 {
my $t = shift || 0;
@@ -753,7 +785,10 @@
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";
@@ -795,7 +830,8 @@
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'";