17 |
|
|
18 |
my $config_file = $0; |
my $config_file = $0; |
19 |
$config_file =~ s/\.pl$/.conf/; |
$config_file =~ s/\.pl$/.conf/; |
20 |
|
$config_file = $ARGV[0] if (-f $ARGV[0]); |
21 |
die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file); |
die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file); |
22 |
|
|
23 |
my $config; |
my $config; |
152 |
} |
} |
153 |
|
|
154 |
|
|
155 |
|
# init variables for different types |
156 |
|
sub init_visible_type($) { |
157 |
|
my $type = shift; |
158 |
|
|
159 |
|
# swish, swish_exact, display, index, index_lookup |
160 |
|
# swish and display defaults |
161 |
|
my ($s,$se,$d,$i,$il) = (1,0,1,0,0); |
162 |
|
if (lc($type) eq "display") { |
163 |
|
$s = 0; |
164 |
|
} elsif (lc($type) eq "swish") { |
165 |
|
$d = 0; |
166 |
|
} elsif (lc($type) eq "index") { |
167 |
|
($s,$se,$d,$i) = (0,1,0,1); |
168 |
|
} elsif (lc($type) eq "swish_exact") { |
169 |
|
($s,$se,$d,$i) = (0,1,0,0); |
170 |
|
} elsif (lc($type) =~ /^lookup/) { |
171 |
|
($s,$se,$d,$i,$il) = (0,1,0,0,1); |
172 |
|
} |
173 |
|
return ($s,$se,$d,$i,$il); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
# convert |
178 |
|
# |
179 |
|
# <tag> |
180 |
|
# <delimiter>, </delimiter> |
181 |
|
# <value>200a</value> |
182 |
|
# </tag> |
183 |
|
# |
184 |
|
# to |
185 |
|
# |
186 |
|
# <tag delimiter=", ">200a</tag> |
187 |
|
# |
188 |
|
# but without loosing spaces in delimiter (becasue |
189 |
|
# new XML::Simple strips spaces in attribute values |
190 |
|
# as defined in XML specification) |
191 |
|
# |
192 |
|
sub unroll_x($) { |
193 |
|
my $x = shift; |
194 |
|
|
195 |
|
if (defined $x->{value}) { |
196 |
|
my ($v,$d) = ($x->{value}->{content}, $x->{delimiter}->{content}); |
197 |
|
delete $x->{value}; |
198 |
|
delete $x->{delimiter}; |
199 |
|
$x->{content} = $v; |
200 |
|
$x->{delimiter} = $d; |
201 |
|
} |
202 |
|
return $x; |
203 |
|
} |
204 |
|
|
205 |
# begin real work: go field by field |
# begin real work: go field by field |
206 |
foreach my $field (@sorted_tags) { |
foreach my $field (@sorted_tags) { |
207 |
|
|
235 |
|
|
236 |
foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { |
237 |
|
|
238 |
|
$x = unroll_x($x); |
239 |
|
|
240 |
my $format = x($x->{content}); |
my $format = x($x->{content}); |
241 |
my $delimiter = x($x->{delimiter}) || ' '; |
my $delimiter = x($x->{delimiter}) || ' '; |
242 |
|
|
243 |
my $repeat_off = 0; # init repeatable offset |
my $repeat_off = 0; # init repeatable offset |
244 |
|
|
245 |
# swish, swish_exact, display, index, index_lookup |
my ($s,$se,$d,$i,$il) = init_visible_type($x->{type}); |
|
# swish and display defaults |
|
|
my ($s,$se,$d,$i,$il) = (1,0,1,0,0); |
|
|
$s = 0 if (lc($x->{type}) eq "display"); |
|
|
$d = 0 if (lc($x->{type}) eq "swish"); |
|
|
($s,$se,$d,$i) = (0,1,0,1) if (lc($x->{type}) eq "index"); |
|
|
($s,$se,$d,$i) = (0,1,0,0) if (lc($x->{type}) eq "swish_exact"); |
|
|
($s,$se,$d,$i,$il) = (0,1,0,0,1) if (lc($x->{type}) =~ /^lookup/); |
|
246 |
|
|
247 |
# what will separate last line from this one? |
# what will separate last line from this one? |
248 |
if ($display_data && $x->{append}) { |
if ($display_data && $x->{append}) { |
249 |
$line_delimiter = ' '; |
$line_delimiter = $delimiter; |
250 |
} elsif ($display_data) { |
} elsif ($display_data) { |
251 |
$line_delimiter = '<br/>'; |
$line_delimiter = '<br/>'; |
252 |
} |
} |
256 |
|
|
257 |
# placeholder for all repeatable entries for index |
# placeholder for all repeatable entries for index |
258 |
|
|
|
sub chk_eval($) { |
|
|
my $data = shift; |
|
|
if ($data =~ s/eval{([^}]+)}//) { |
|
|
if (eval "$1") { |
|
|
return $data; |
|
|
} else { |
|
|
return undef; |
|
|
} |
|
|
} else { |
|
|
return $data; |
|
|
} |
|
|
} |
|
|
|
|
259 |
sub mkformat($$) { |
sub mkformat($$) { |
260 |
my $x = shift || die "mkformat needs tag reference"; |
my $x = shift || die "mkformat needs tag reference"; |
261 |
my $data = shift || return; |
my $data = shift || return; |
262 |
my $format_name = x($x->{format_name}) || return chk_eval($data); |
my $format_name = x($x->{format_name}) || return $data; |
263 |
my $fmt = x($config->{format}->{$format_name}->{content}) || die "<format name=\"$format_name\"> is not defined!"; |
my $fmt = x($config->{format}->{$format_name}->{content}) || die "<format name=\"$format_name\"> is not defined!"; |
264 |
my $format_delimiter = x($x->{format_delimiter}); |
my $format_delimiter = x($x->{format_delimiter}); |
265 |
my @data; |
my @data; |
272 |
if ($fmt) { |
if ($fmt) { |
273 |
my $nr = scalar $fmt =~ s/%s/%s/g; |
my $nr = scalar $fmt =~ s/%s/%s/g; |
274 |
if (($#data+1) == $nr) { |
if (($#data+1) == $nr) { |
275 |
return chk_eval(sprintf($fmt,@data)); |
return sprintf($fmt,@data); |
276 |
} else { |
} else { |
277 |
#print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n"; |
#print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n"; |
278 |
return chk_eval($data); |
return $data; |
279 |
} |
} |
280 |
} else { |
} else { |
281 |
print STDERR "usage of link '$format_name' without defined format (<link> tag)\n"; |
print STDERR "usage of link '$format_name' without defined format (<link> tag)\n"; |
303 |
$display = $new_display; |
$display = $new_display; |
304 |
$cache->{lhash}->{$display} = $new_display; |
$cache->{lhash}->{$display} = $new_display; |
305 |
} else { |
} else { |
306 |
print STDERR "WARNING: lookup for '$display' didn't find anything.\n"; |
# print STDERR "WARNING: lookup for '$display' didn't find anything.\n"; |
307 |
$display = ""; |
$display = ""; |
308 |
$cache->{lhash}->{$display} = $null; |
$cache->{lhash}->{$display} = $null; |
309 |
} |
} |
324 |
} |
} |
325 |
# type="swish" ; field for swish |
# type="swish" ; field for swish |
326 |
if ($swish) { |
if ($swish) { |
327 |
|
my $tmp = $swish; |
328 |
if ($filter && ($s || $se)) { |
if ($filter && ($s || $se)) { |
329 |
no strict 'refs'; |
no strict 'refs'; |
330 |
my $tmp = join(" ",&$filter($swish)) if ($s || $se); |
$tmp = join(" ",&$filter($tmp)) if ($s || $se); |
|
$swish_data .= $tmp if ($s); |
|
|
$swish_exact_data .= "xxbxx $tmp xxexx " if ($se && $tmp ne ""); |
|
|
|
|
|
} else { |
|
|
$swish_data .= $swish if ($s); |
|
|
$swish_exact_data .= "xxbxx $swish xxexx " if ($se && $swish ne ""); |
|
331 |
} |
} |
332 |
|
|
333 |
|
$swish_data .= $tmp if ($s && $tmp); |
334 |
|
$swish_exact_data .= "xxbxx $tmp xxexx " if ($tmp && $tmp ne "" && $se); |
335 |
} |
} |
336 |
|
|
337 |
# type="display" ; field for display |
# type="display" ; field for display |
352 |
} else { |
} else { |
353 |
$display_data .= $ldel if ($display_data); |
$display_data .= $ldel if ($display_data); |
354 |
my $tmp = mkformat($x,$display); |
my $tmp = mkformat($x,$display); |
355 |
$display_data = $tmp if ($tmp); |
$display_data .= $tmp if ($tmp); |
356 |
} |
} |
357 |
} |
} |
358 |
|
|
364 |
no strict 'refs'; |
no strict 'refs'; |
365 |
$idisplay = &$filter($idisplay); |
$idisplay = &$filter($idisplay); |
366 |
} |
} |
367 |
push @index_data, $idisplay if (! $iterate_by_page); |
push @index_data, $idisplay if ($idisplay && !$iterate_by_page); |
368 |
} |
} |
369 |
|
|
370 |
# store fields in lookup |
# store fields in lookup |
373 |
if ($lookup_key) { |
if ($lookup_key) { |
374 |
print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)"; |
print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)"; |
375 |
} else { |
} else { |
376 |
$lookup_key = $display; |
if ($filter) { |
377 |
|
no strict 'refs'; |
378 |
|
$lookup_key = &$filter($display); |
379 |
|
} else { |
380 |
|
$lookup_key = $display; |
381 |
|
} |
382 |
} |
} |
383 |
} elsif (lc($x->{type}) eq "lookup_val") { |
} elsif (lc($x->{type}) eq "lookup_val") { |
384 |
if ($lookup_key) { |
if ($lookup_key) { |
385 |
$lhash{$lookup_key} = $display; |
if ($filter) { |
386 |
|
no strict 'refs'; |
387 |
|
$lhash{$lookup_key} = &$filter($display); |
388 |
|
} else { |
389 |
|
$lhash{$lookup_key} = $display; |
390 |
|
} |
391 |
} else { |
} else { |
392 |
print STDERR "WARNING: no lookup_key defined for '$display'?"; |
print STDERR "WARNING: no lookup_key defined for '$display'?"; |
393 |
} |
} |
463 |
# now try to parse variables from configuration file |
# now try to parse variables from configuration file |
464 |
foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) { |
465 |
|
|
466 |
|
$x = unroll_x($x); |
467 |
|
|
468 |
my $delimiter = x($x->{delimiter}) || ' '; |
my $delimiter = x($x->{delimiter}) || ' '; |
469 |
my $val = $cfg->val($database, x($x->{content})); |
my $val = $cfg->val($database, x($x->{content})); |
470 |
|
|
471 |
my ($s,$d,$i) = (1,1,0); # swish, display default |
# FIXME index_lookup is not supported! |
472 |
$s = 0 if (lc($x->{type}) eq "display"); |
my ($s,$se,$d,$i,$il) = init_visible_type($x->{type}); |
|
$d = 0 if (lc($x->{type}) eq "swish"); |
|
|
# no support for swish exact in config. |
|
|
# IMHO, it's useless |
|
|
($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index"); |
|
473 |
|
|
474 |
if ($val) { |
if ($val) { |
475 |
$display_data .= $delimiter.$val if ($d); |
$display_data .= $delimiter.$val if ($d); |
476 |
$swish_data .= $val if ($s); |
$swish_data .= " ".$val if ($s); |
477 |
$index->insert($field, $val, $path) if ($i); |
$index->insert($field, $val, $path) if ($i); |
478 |
} |
} |
479 |
|
|
631 |
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644; |
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644; |
632 |
tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644; |
tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644; |
633 |
print STDERR "creating lookup file '$lookup_file'\n"; |
print STDERR "creating lookup file '$lookup_file'\n"; |
634 |
|
# delete memory cache for lookup file |
635 |
|
delete $cache->{lhash}; |
636 |
} |
} |
637 |
|
|
638 |
# open existing lookup file |
# open existing lookup file |
649 |
my $type_base = $type; |
my $type_base = $type; |
650 |
$type_base =~ s/_.+$//g; |
$type_base =~ s/_.+$//g; |
651 |
|
|
652 |
$config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type_base}, 'config', 'format' ], forcecontent => 1); |
$config=XMLin("./import_xml/$type.xml", ForceArray => [ $type2tag{$type_base}, 'config', 'format' ], ForceContent => 1 ); |
653 |
|
|
654 |
# output current progress indicator |
# output current progress indicator |
655 |
my $last_p = 0; |
my $last_p = 0; |
769 |
print STDERR "\n"; |
print STDERR "\n"; |
770 |
|
|
771 |
} elsif ($type_base eq "excel") { |
} elsif ($type_base eq "excel") { |
772 |
use Spreadsheet::ParseExcel; |
require Spreadsheet::ParseExcel; |
773 |
use Spreadsheet::ParseExcel::Utility qw(int2col); |
require Spreadsheet::ParseExcel::Utility; |
774 |
|
import Spreadsheet::ParseExcel::Utility qw(int2col); |
775 |
|
|
776 |
$import2cp = Text::Iconv->new($config->{excel_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{excel_codepage},$codepage); |
777 |
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!"; |
825 |
} |
} |
826 |
} elsif ($type_base eq "marc") { |
} elsif ($type_base eq "marc") { |
827 |
|
|
828 |
use MARC; |
require MARC; |
829 |
|
|
830 |
$import2cp = Text::Iconv->new($config->{marc_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{marc_codepage},$codepage); |
831 |
my $marc_file = $cfg -> val($database, 'marc_file') || die "$database doesn't have 'marc_file' defined!"; |
my $marc_file = $cfg -> val($database, 'marc_file') || die "$database doesn't have 'marc_file' defined!"; |
832 |
|
|
833 |
# optional argument is format |
# optional argument is format |
834 |
my $format = x($config->{format}) || 'usmarc'; |
my $format = x($config->{marc_format}) || 'usmarc'; |
835 |
|
|
836 |
print STDERR "Reading MARC file '$marc_file'\n"; |
print STDERR "Reading MARC file '$marc_file'\n"; |
837 |
|
|
838 |
my $marc = new MARC; |
my $marc = new MARC; |
839 |
my $nr = $marc->openmarc({ |
my $nr = $marc->openmarc({ |
840 |
file=>$marc_file, format=>$format |
file=>$marc_file, format=>$format |
841 |
}) || die "Can't open MARC file '$marc_file'"; |
}) || die "Can't open MARC file '$marc_file' with format '$format'"; |
842 |
|
|
843 |
my $i=0; # record nr. |
# read MARC file in memory |
844 |
|
$marc->nextmarc(-1); |
845 |
|
|
846 |
my $rec; |
my $max_rec = $marc->marc_count(); |
847 |
|
|
848 |
while ($marc->nextmarc(1)) { |
for(my $i=1; $i<=$max_rec; $i++) { |
849 |
|
|
850 |
# XXX |
progress($i,$max_rec); |
851 |
fakeprogress($i++); |
|
852 |
|
# store value for marc_sf.pm |
853 |
|
$main::cache->{marc_record} = $i; |
854 |
|
|
855 |
my $swishpath = $database."#".$i; |
my $swishpath = $database."#".$i; |
856 |
|
|
862 |
print "Document-Type: XML\n\n$xml\n"; |
print "Document-Type: XML\n\n$xml\n"; |
863 |
} |
} |
864 |
} |
} |
865 |
|
|
866 |
|
print STDERR "\n"; |
867 |
|
|
868 |
} elsif ($type_base eq "feed") { |
} elsif ($type_base eq "feed") { |
869 |
|
|
870 |
$import2cp = Text::Iconv->new($config->{feed_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{feed_codepage},$codepage); |
919 |
|
|
920 |
all2xml.pl - read various file formats and dump XML for SWISH-E |
all2xml.pl - read various file formats and dump XML for SWISH-E |
921 |
|
|
922 |
|
=head1 SYNOPSYS |
923 |
|
|
924 |
|
$ all2xml.pl [test.conf] |
925 |
|
|
926 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
927 |
|
|
928 |
This command will read ISIS data file using OpenIsis perl module, MARC |
This command will read ISIS data file using OpenIsis perl module, MARC |
931 |
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 |
932 |
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. |
933 |
|
|
934 |
|
If no configuration file is specified, it will use default one called |
935 |
|
C<all2xml.conf>. |
936 |
|
|
937 |
=head1 BUGS |
=head1 BUGS |
938 |
|
|
939 |
Documentation is really lacking. However, in true Open Source spirit, source |
Documentation is really lacking. However, in true Open Source spirit, source |