1 |
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
2 |
|
|
3 |
use strict; |
use strict; |
4 |
use OpenIsis; |
use Biblio::Isis; |
5 |
use Getopt::Std; |
use Getopt::Std; |
6 |
use Data::Dumper; |
use Data::Dumper; |
7 |
use XML::Simple; |
use XML::Simple; |
|
use Text::Unaccent 1.02; # 1.01 won't compile on my platform, |
|
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; |
15 |
|
|
16 |
$|=1; |
$|=1; |
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 ($ARGV[0] && -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; |
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 |
|
|
145 |
} else { |
} else { |
146 |
print STDERR "WARNING: field '$field' doesn't have 'name' attribute!"; |
print STDERR "WARNING: field '$field' doesn't have 'name' attribute!"; |
147 |
} |
} |
148 |
|
|
149 |
if ($field_name) { |
if ($field_name) { |
150 |
|
$field_name = x($field_name); |
151 |
if (! $last_field_name) { |
if (! $last_field_name) { |
152 |
$last_field_name = x($field_name); |
$last_field_name = $field_name; |
153 |
return $last_field_name; |
return $last_field_name; |
154 |
} elsif ($field_name ne $last_field_name) { |
} elsif ($field_name ne $last_field_name) { |
155 |
$last_field_name = x($field_name); |
$last_field_name = $field_name; |
156 |
return $last_field_name; |
return $last_field_name; |
157 |
} |
} |
158 |
} |
} |
159 |
} |
} |
160 |
|
|
161 |
|
|
162 |
|
# init variables for different types |
163 |
|
sub init_visible_type($) { |
164 |
|
my $type = shift; |
165 |
|
|
166 |
|
# swish, swish_exact, display, index, index_lookup |
167 |
|
# swish and display defaults |
168 |
|
my ($s,$se,$d,$i,$il) = (1,0,1,0,0); |
169 |
|
if (lc($type) eq "display") { |
170 |
|
$s = 0; |
171 |
|
} elsif (lc($type) eq "swish") { |
172 |
|
$d = 0; |
173 |
|
} elsif (lc($type) eq "index") { |
174 |
|
($s,$se,$d,$i) = (0,1,0,1); |
175 |
|
} elsif (lc($type) eq "swish_exact") { |
176 |
|
($s,$se,$d,$i) = (0,1,0,0); |
177 |
|
} elsif (lc($type) =~ /^lookup/) { |
178 |
|
($s,$se,$d,$i,$il) = (0,1,0,0,1); |
179 |
|
} elsif ($type) { |
180 |
|
print STDERR "WARNING: unknown type: $type\n"; |
181 |
|
} |
182 |
|
return ($s,$se,$d,$i,$il); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
# convert |
187 |
|
# |
188 |
|
# <tag> |
189 |
|
# <delimiter>, </delimiter> |
190 |
|
# <value>200a</value> |
191 |
|
# </tag> |
192 |
|
# |
193 |
|
# to |
194 |
|
# |
195 |
|
# <tag delimiter=", ">200a</tag> |
196 |
|
# |
197 |
|
# but without loosing spaces in delimiter (becasue |
198 |
|
# new XML::Simple strips spaces in attribute values |
199 |
|
# as defined in XML specification) |
200 |
|
# |
201 |
|
sub unroll_x($) { |
202 |
|
my $x = shift; |
203 |
|
|
204 |
|
if (defined $x->{value}) { |
205 |
|
my ($v,$d) = ($x->{value}->{content}, $x->{delimiter}->{content}); |
206 |
|
delete $x->{value}; |
207 |
|
delete $x->{delimiter}; |
208 |
|
$x->{content} = $v; |
209 |
|
$x->{delimiter} = $d; |
210 |
|
} |
211 |
|
return $x; |
212 |
|
} |
213 |
|
|
214 |
# begin real work: go field by field |
# begin real work: go field by field |
215 |
foreach my $field (@sorted_tags) { |
foreach my $field (@sorted_tags) { |
216 |
|
|
225 |
|
|
226 |
my ($swish,$display); |
my ($swish,$display); |
227 |
|
|
228 |
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"; |
229 |
|
|
230 |
# is this field page-by-page? |
# is this field page-by-page? |
231 |
my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page}; |
my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page}; |
244 |
|
|
245 |
foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { |
246 |
|
|
247 |
|
$x = unroll_x($x); |
248 |
|
|
249 |
my $format = x($x->{content}); |
my $format = x($x->{content}); |
250 |
my $delimiter = x($x->{delimiter}) || ' '; |
my $delimiter = x($x->{delimiter}) || ' '; |
251 |
|
|
252 |
my $repeat_off = 0; # init repeatable offset |
my $repeat_off = 0; # init repeatable offset |
253 |
|
|
254 |
# 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/); |
|
255 |
|
|
256 |
# what will separate last line from this one? |
# what will separate last line from this one? |
257 |
if ($display_data && $x->{append}) { |
if ($display_data && $x->{append}) { |
258 |
$line_delimiter = ' '; |
$line_delimiter = $delimiter; |
259 |
} elsif ($display_data) { |
} elsif ($display_data) { |
260 |
$line_delimiter = '<br/>'; |
$line_delimiter = '<br/>'; |
261 |
} |
} |
263 |
# init vars so that we go into while... |
# init vars so that we go into while... |
264 |
($swish,$display) = (1,1); |
($swish,$display) = (1,1); |
265 |
|
|
|
# placeholder for all repeatable entries for index |
|
|
|
|
266 |
sub mkformat($$) { |
sub mkformat($$) { |
267 |
my $x = shift || die "mkformat needs tag reference"; |
my $x = shift || die "mkformat needs tag reference"; |
268 |
my $data = shift || return; |
my $data = shift || return; |
281 |
if (($#data+1) == $nr) { |
if (($#data+1) == $nr) { |
282 |
return sprintf($fmt,@data); |
return sprintf($fmt,@data); |
283 |
} else { |
} else { |
284 |
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"; |
285 |
return $data; |
return $data; |
286 |
} |
} |
287 |
} else { |
} else { |
310 |
$display = $new_display; |
$display = $new_display; |
311 |
$cache->{lhash}->{$display} = $new_display; |
$cache->{lhash}->{$display} = $new_display; |
312 |
} else { |
} else { |
313 |
print STDERR "WARNING: lookup for '$display' didn't find anything.\n"; |
# print STDERR "WARNING: lookup for '$display' didn't find anything.\n"; |
314 |
$display = ""; |
$display = ""; |
315 |
$cache->{lhash}->{$display} = $null; |
$cache->{lhash}->{$display} = $null; |
316 |
} |
} |
331 |
} |
} |
332 |
# type="swish" ; field for swish |
# type="swish" ; field for swish |
333 |
if ($swish) { |
if ($swish) { |
334 |
|
my $tmp = $swish; |
335 |
if ($filter && ($s || $se)) { |
if ($filter && ($s || $se)) { |
336 |
no strict 'refs'; |
no strict 'refs'; |
337 |
my $tmp = join(" ",&$filter($swish)) if ($s || $se); |
$tmp = join(" ",&$filter($tmp)) if ($s || $se); |
|
$swish_data .= $tmp if ($s); |
|
|
$swish_exact_data .= $tmp if ($se); |
|
|
|
|
|
} else { |
|
|
$swish_data .= $swish if ($s); |
|
|
$swish_exact_data .= $swish if ($se); |
|
338 |
} |
} |
339 |
|
|
340 |
|
$swish_data .= $tmp if ($s && $tmp); |
341 |
|
$swish_exact_data .= "xxbxx $tmp xxexx " if ($tmp && $tmp ne "" && $se); |
342 |
} |
} |
343 |
|
|
344 |
# type="display" ; field for display |
# type="display" ; field for display |
345 |
if ($d && $display) { |
if ($d && $display) { |
346 |
|
my $ldel = $delimiter; |
347 |
if ($line_delimiter && $display_data) { |
if ($line_delimiter && $display_data) { |
348 |
$display_data .= $line_delimiter; |
$ldel = $line_delimiter; |
349 |
} |
} |
350 |
if ($filter) { |
if ($filter) { |
351 |
no strict 'refs'; |
no strict 'refs'; |
352 |
if ($display_data) { |
my @arr; |
353 |
$display_data .= $delimiter.join($delimiter,mkformat($x,&$filter($display))); |
foreach my $tmp (&$filter($display)) { |
354 |
} else { |
my $tmp2 = mkformat($x,$tmp); |
355 |
$display_data = join($delimiter,mkformat($x,&$filter($display))); |
push @arr,$tmp2 if ($tmp2); |
356 |
} |
} |
357 |
|
$display_data .= $ldel if ($display_data && @arr); |
358 |
|
$display_data .= join($delimiter,@arr); |
359 |
} else { |
} else { |
360 |
if ($display_data) { |
$display_data .= $ldel if ($display_data); |
361 |
$display_data .= $delimiter.mkformat($x,$display); |
my $tmp = mkformat($x,$display); |
362 |
} else { |
$display_data .= $tmp if ($tmp); |
|
$display_data = mkformat($x,$display); |
|
|
} |
|
363 |
} |
} |
364 |
} |
} |
365 |
|
|
371 |
no strict 'refs'; |
no strict 'refs'; |
372 |
$idisplay = &$filter($idisplay); |
$idisplay = &$filter($idisplay); |
373 |
} |
} |
374 |
push @index_data, $idisplay if (! $iterate_by_page); |
push @index_data, $idisplay if ($idisplay && !$iterate_by_page); |
375 |
} |
} |
376 |
|
|
377 |
# store fields in lookup |
# store fields in lookup |
380 |
if ($lookup_key) { |
if ($lookup_key) { |
381 |
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!)"; |
382 |
} else { |
} else { |
383 |
$lookup_key = $display; |
if ($filter) { |
384 |
|
no strict 'refs'; |
385 |
|
$lookup_key = &$filter($display); |
386 |
|
} else { |
387 |
|
$lookup_key = $display; |
388 |
|
} |
389 |
} |
} |
390 |
} elsif (lc($x->{type}) eq "lookup_val") { |
} elsif (lc($x->{type}) eq "lookup_val") { |
391 |
if ($lookup_key) { |
if ($lookup_key) { |
392 |
$lhash{$lookup_key} = $display; |
if ($filter) { |
393 |
|
no strict 'refs'; |
394 |
|
$lhash{$lookup_key} = &$filter($display); |
395 |
|
} else { |
396 |
|
$lhash{$lookup_key} = $display; |
397 |
|
} |
398 |
} else { |
} else { |
399 |
print STDERR "WARNING: no lookup_key defined for '$display'?"; |
print STDERR "WARNING: no lookup_key defined for '$display'?"; |
400 |
} |
} |
470 |
# now try to parse variables from configuration file |
# now try to parse variables from configuration file |
471 |
foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) { |
472 |
|
|
473 |
|
$x = unroll_x($x); |
474 |
|
|
475 |
my $delimiter = x($x->{delimiter}) || ' '; |
my $delimiter = x($x->{delimiter}) || ' '; |
476 |
my $val = $cfg->val($database, x($x->{content})); |
my $val = $cfg->val($database, x($x->{content})); |
477 |
|
|
478 |
my ($s,$d,$i) = (1,1,0); # swish, display default |
# FIXME index_lookup is not supported! |
479 |
$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"); |
|
480 |
|
|
481 |
if ($val) { |
if ($val) { |
482 |
$display_data .= $delimiter.$val if ($d); |
$display_data .= $delimiter.$val if ($d); |
483 |
$swish_data .= $val if ($s); |
$swish_data .= " ".$val if ($s); |
484 |
$index->insert($field, $val, $path) if ($i); |
$index->insert($field, $val, $path) if ($i); |
485 |
} |
} |
486 |
|
|
511 |
for (my $page=0; $page <= $nr_pages; $page++) { |
for (my $page=0; $page <= $nr_pages; $page++) { |
512 |
my $display_data; |
my $display_data; |
513 |
if ($cache->{format}->{$field}) { |
if ($cache->{format}->{$field}) { |
514 |
$display_data=mkformat($cache->{format}->{$field},$cache->{display_data}->{$field}->[$page]); |
my $tmp = mkformat($cache->{format}->{$field},$cache->{display_data}->{$field}->[$page]); |
515 |
|
$display_data=$tmp if ($tmp); |
516 |
} else { |
} else { |
517 |
$display_data = $cache->{display_data}->{$field}->[$page]; |
$display_data = $cache->{display_data}->{$field}->[$page]; |
518 |
} |
} |
533 |
$swish_data =~ s/ +/ /g; |
$swish_data =~ s/ +/ /g; |
534 |
$swish_data =~ s/ +$//g; |
$swish_data =~ s/ +$//g; |
535 |
|
|
536 |
$xml .= xmlify($field."_swish", unac_string($codepage,$swish_data)); |
$xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data)); |
537 |
} |
} |
538 |
|
|
539 |
my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page]; |
my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page]; |
543 |
|
|
544 |
# add delimiters before and after word. |
# add delimiters before and after word. |
545 |
# That is required to produce exact match |
# That is required to produce exact match |
546 |
$xml .= xmlify($field."_swish_exact", unac_string($codepage,'xxbxx '.$swish_exact_data.' xxexx')); |
$xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data)); |
547 |
} |
} |
548 |
|
|
549 |
my $idel = $cache->{index_delimiter}->{$field}; |
my $idel = $cache->{index_delimiter}->{$field}; |
576 |
$swish_data =~ s/ +/ /g; |
$swish_data =~ s/ +/ /g; |
577 |
$swish_data =~ s/ +$//g; |
$swish_data =~ s/ +$//g; |
578 |
|
|
579 |
$xml .= xmlify($field."_swish", unac_string($codepage,$swish_data)); |
$xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data)); |
580 |
} |
} |
581 |
|
|
582 |
if ($swish_exact_data) { |
if ($swish_exact_data) { |
585 |
|
|
586 |
# add delimiters before and after word. |
# add delimiters before and after word. |
587 |
# That is required to produce exact match |
# That is required to produce exact match |
588 |
$xml .= xmlify($field."_swish_exact", unac_string($codepage,'xxbxx '.$swish_exact_data.' xxexx')); |
$xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data)); |
589 |
} |
} |
590 |
} |
} |
591 |
} |
} |
622 |
|
|
623 |
my $show_progress = $cfg_global->val('global', 'show_progress'); |
my $show_progress = $cfg_global->val('global', 'show_progress'); |
624 |
|
|
625 |
my $unac_filter = $cfg_global->val('global', 'unac_filter'); |
my $my_unac_filter = $cfg_global->val('global', 'my_unac_filter'); |
626 |
if ($unac_filter) { |
if ($my_unac_filter) { |
627 |
require $unac_filter; |
print STDERR "using $my_unac_filter to filter characters for search\n"; |
628 |
|
require $my_unac_filter; |
629 |
|
} else { |
630 |
|
print STDERR "### fallback to default my_unac_string!\n"; |
631 |
|
eval q{ |
632 |
|
sub main::my_unac_string($$) { |
633 |
|
my ($charset, $string) = (@_); |
634 |
|
return $string; |
635 |
|
} |
636 |
|
}; |
637 |
} |
} |
638 |
|
|
639 |
foreach my $database ($cfg->Sections) { |
foreach my $database ($cfg->Sections) { |
640 |
|
|
641 |
|
# save database name in global variable path for later |
642 |
|
# (need for index filter creation) |
643 |
|
$path = $database; |
644 |
|
|
645 |
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"; |
646 |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
647 |
|
|
649 |
my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional |
my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional |
650 |
if ($lookup_file) { |
if ($lookup_file) { |
651 |
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644; |
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644; |
652 |
|
if (! -e $lookup_file) { |
653 |
|
open(LOOKUP, "> $lookup_file") || die "can't create $lookup_file': $!"; |
654 |
|
close(LOOKUP); |
655 |
|
} |
656 |
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; |
657 |
print STDERR "creating lookup file '$lookup_file'\n"; |
print STDERR "creating lookup file '$lookup_file'\n"; |
658 |
|
# delete memory cache for lookup file |
659 |
|
delete $cache->{lhash}; |
660 |
} |
} |
661 |
|
|
662 |
# open existing lookup file |
# open existing lookup file |
667 |
print STDERR "opening lookup file '$lookup_file'\n"; |
print STDERR "opening lookup file '$lookup_file'\n"; |
668 |
} |
} |
669 |
|
|
670 |
print STDERR "reading ./import_xml/$type.xml\n"; |
my $import_xml_type = $cfg->val($database, 'import_xml_file') || $type; |
671 |
|
my $import_xml_file = "./import_xml/$import_xml_type.xml"; |
672 |
|
|
673 |
|
if (! -r $import_xml_file) { |
674 |
|
print STDERR "ERROR: file $import_xml_file not readable skipping!\n"; |
675 |
|
next; |
676 |
|
} |
677 |
|
|
678 |
|
print STDERR "reading $import_xml_file\n"; |
679 |
|
|
680 |
# extract just type basic |
# extract just type basic |
681 |
my $type_base = $type; |
my $type_base = $type; |
682 |
$type_base =~ s/_.+$//g; |
$type_base =~ s/_.+$//g; |
683 |
|
|
684 |
$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"; |
685 |
|
$config=XMLin($import_xml_file, ForceArray => [ $tag, 'config', 'format' ], ForceContent => 1 ); |
686 |
|
|
687 |
|
# helper for progress bar |
688 |
|
sub fmt_time { |
689 |
|
my $t = shift || 0; |
690 |
|
my $out = ""; |
691 |
|
|
692 |
|
my ($ss,$mm,$hh) = gmtime($t); |
693 |
|
$out .= "${hh}h" if ($hh); |
694 |
|
$out .= sprintf("%02d:%02d", $mm,$ss); |
695 |
|
$out .= " " if ($hh == 0); |
696 |
|
return $out; |
697 |
|
} |
698 |
|
|
699 |
# output current progress indicator |
# output current progress indicator |
700 |
my $last_p = 0; |
my $last_p = 0; |
701 |
|
my $start_t = time(); |
702 |
sub progress { |
sub progress { |
703 |
return if (! $show_progress); |
return if (! $show_progress); |
704 |
my $current = shift; |
my $current = shift; |
705 |
my $total = shift || 1; |
my $total = shift || 1; |
706 |
my $p = int($current * 100 / $total); |
my $p = int($current * 100 / $total); |
707 |
if ($p != $last_p) { |
if ($p < $last_p || $current == 1) { |
708 |
printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p ); |
$start_t = time(); |
709 |
|
$last_p = 0; |
710 |
|
} elsif ($p != $last_p) { |
711 |
|
my $rate = ($current / (time() - $start_t || 1)); |
712 |
|
my $eta = ($total-$current) / ($rate || 1); |
713 |
|
printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$current,"=" x ($p/3)."$p%>", $total, $rate, fmt_time($eta)); |
714 |
$last_p = $p; |
$last_p = $p; |
715 |
} |
} |
716 |
} |
} |
717 |
|
|
718 |
my $fake_dir = 1; |
my $fake_dir = 1; |
719 |
|
my $fake_pos = 0; |
720 |
|
my $last_fake_t = time(); |
721 |
sub fakeprogress { |
sub fakeprogress { |
722 |
return if (! $show_progress); |
return if (! $show_progress); |
723 |
my $current = shift @_; |
my $current = shift @_; |
724 |
|
|
725 |
my @ind = ('-','\\','|','/','-','\\','|','/', '-'); |
my @ind = ('-','\\','|','/','-','\\','|','/'); |
726 |
|
|
727 |
|
if ($current < $fake_pos) { |
728 |
|
$start_t = time(); |
729 |
|
$last_fake_t = 0; |
730 |
|
$fake_dir = 1; |
731 |
|
$fake_pos = 0; |
732 |
|
} |
733 |
|
|
734 |
|
if (time()-$last_fake_t >= 1) { |
735 |
|
$last_fake_t = time(); |
736 |
|
$fake_pos += $fake_dir; |
737 |
|
$fake_dir = -$fake_dir if ($fake_pos > 38); |
738 |
|
} |
739 |
|
|
740 |
$last_p += $fake_dir; |
if ($current % 10 == 0) { |
741 |
$fake_dir = -$fake_dir if ($last_p > 1000 || $last_p < 0); |
my $rate = ($current / (time() - $start_t || 1)); |
742 |
if ($last_p % 10 == 0) { |
printf STDERR ("%5d [%-38s] %0.1f/s\r",$current, " " x $fake_pos .$ind[($current / 10) % 8], $rate); |
|
printf STDERR ("%5d / %5s [%-51s]\r",$current,"?"," " x ($last_p/20).$ind[($last_p/20) % $#ind]); |
|
743 |
} |
} |
744 |
} |
} |
745 |
|
|
746 |
# now read database |
# now read database |
|
print STDERR "using: $type...\n"; |
|
747 |
|
|
748 |
# erase cache for tags by order in this database |
# erase cache for tags by order in this database |
749 |
delete $cache->{tags_by_order}; |
delete $cache->{tags_by_order}; |
753 |
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!"; |
754 |
|
|
755 |
$import2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
756 |
my $db = OpenIsis::open( $isis_db ); |
my $db = new Biblio::Isis( isisdb => $isis_db ); |
757 |
|
|
758 |
# check if .txt database for OpenIsis is zero length, |
if (! $db) { |
759 |
# if so, erase it and re-open database |
print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n"; |
|
sub check_txt_db { |
|
|
my $isis_db = shift || die "need isis database name"; |
|
|
my $reopen = 0; |
|
|
|
|
|
if (-e $isis_db.".TXT") { |
|
|
print STDERR "WARNING: removing $isis_db.TXT OpenIsis database...\n"; |
|
|
unlink $isis_db.".TXT" || warn "FATAL: unlink error on '$isis_db.TXT': $!"; |
|
|
$reopen++; |
|
|
} |
|
|
if (-e $isis_db.".PTR") { |
|
|
print STDERR "WARNING: removing $isis_db.PTR OpenIsis database...\n"; |
|
|
unlink $isis_db.".PTR" || warn "FATAL: unlink error on '$isis_db.PTR': $!"; |
|
|
$reopen++; |
|
|
} |
|
|
return OpenIsis::open( $isis_db ) if ($reopen); |
|
|
} |
|
|
|
|
|
# EOF error |
|
|
if ($db == -1) { |
|
|
$db = check_txt_db($isis_db); |
|
|
if ($db == -1) { |
|
|
print STDERR "FATAL: OpenIsis can't open zero size file $isis_db\n"; |
|
|
next; |
|
|
} |
|
|
} |
|
|
|
|
|
# OpenIsis::ERR_BADF |
|
|
if ($db == -4) { |
|
|
print STDERR "FATAL: OpenIsis can't find file $isis_db\n"; |
|
|
next; |
|
|
# OpenIsis::ERR_IO |
|
|
} elsif ($db == -5) { |
|
|
print STDERR "FATAL: OpenIsis can't access file $isis_db\n"; |
|
|
next; |
|
|
} elsif ($db < 0) { |
|
|
print STDERR "FATAL: OpenIsis unknown error $db with file $isis_db\n"; |
|
760 |
next; |
next; |
761 |
} |
} |
762 |
|
|
763 |
my $max_rowid = OpenIsis::maxRowid( $db ); |
my $max_rowid = $db->count if ($db); |
|
|
|
|
# if 0 records, try to rease isis .txt database |
|
|
if ($max_rowid == 0) { |
|
|
# force removal of database |
|
|
$db = check_txt_db($isis_db); |
|
|
$max_rowid = OpenIsis::maxRowid( $db ); |
|
|
} |
|
764 |
|
|
765 |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
766 |
|
|
|
my $path = $database; |
|
|
|
|
767 |
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
768 |
my $row = OpenIsis::read( $db, $row_id ); |
my $row = $db->to_hash( $row_id ); |
769 |
if ($row && $row->{mfn}) { |
if ($row) { |
770 |
|
|
771 |
|
$row->{mfn} = $row_id; |
772 |
|
$row->{record} = $db->{record}; |
773 |
|
|
774 |
progress($row->{mfn}, $max_rowid); |
progress($row->{mfn}, $max_rowid); |
775 |
|
|
776 |
my $swishpath = $path."#".int($row->{mfn}); |
my $swishpath = $path."#".int($row->{mfn}); |
784 |
} |
} |
785 |
} |
} |
786 |
} |
} |
|
# for this to work with current version of OpenIsis (0.9.0) |
|
|
# you might need my patch from |
|
|
# https://www.rot13.org/~dpavlin/projects/openisis-0.9.0-perl_close.diff |
|
|
OpenIsis::close($db); |
|
787 |
print STDERR "\n"; |
print STDERR "\n"; |
788 |
|
|
789 |
} elsif ($type_base eq "excel") { |
} elsif ($type_base eq "excel") { |
790 |
use Spreadsheet::ParseExcel; |
require Spreadsheet::ParseExcel; |
791 |
use Spreadsheet::ParseExcel::Utility qw(int2col); |
require Spreadsheet::ParseExcel::Utility; |
792 |
|
import Spreadsheet::ParseExcel::Utility qw(int2col); |
793 |
|
|
794 |
$import2cp = Text::Iconv->new($config->{excel_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{excel_codepage},$codepage); |
795 |
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!"; |
796 |
|
|
797 |
my $sheet = x($config->{sheet}) || die "no sheet in $type.xml"; |
my $sheet = x($config->{sheet}) || die "no sheet in $type.xml"; |
798 |
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"; |
799 |
|
$start_row--; |
800 |
|
|
801 |
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'"; |
802 |
|
|
818 |
for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) { |
for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) { |
819 |
my $cell = $oWorksheet->{Cells}[$iR][$iC]; |
my $cell = $oWorksheet->{Cells}[$iR][$iC]; |
820 |
if ($cell) { |
if ($cell) { |
821 |
$row->{int2col($iC)} = $cell->Value; |
# this conversion is a cludge. |
822 |
|
# Files from Excell could have |
823 |
|
# characters which don't fit into |
824 |
|
# destination encoding. |
825 |
|
$row->{int2col($iC)} = $utf2cp->convert($cell->Value) || $cell->Value; |
826 |
} |
} |
827 |
} |
} |
828 |
|
|
846 |
print "Document-Type: XML\n\n$xml\n"; |
print "Document-Type: XML\n\n$xml\n"; |
847 |
} |
} |
848 |
} |
} |
849 |
|
|
850 |
|
print STDERR "\n"; |
851 |
|
|
852 |
} elsif ($type_base eq "marc") { |
} elsif ($type_base eq "marc") { |
853 |
|
|
854 |
use MARC; |
require MARC::File::USMARC; |
855 |
|
|
856 |
$import2cp = Text::Iconv->new($config->{marc_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{marc_codepage},$codepage); |
857 |
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!"; |
858 |
|
|
859 |
# optional argument is format |
# optional argument is format |
860 |
my $format = x($config->{format}) || 'usmarc'; |
warn "marc_format is no longer used!" if ($config->{marc_format}); |
|
|
|
861 |
print STDERR "Reading MARC file '$marc_file'\n"; |
print STDERR "Reading MARC file '$marc_file'\n"; |
862 |
|
|
863 |
my $marc = new MARC; |
my $marc = MARC::File::USMARC->in( $marc_file ); |
864 |
my $nr = $marc->openmarc({ |
|
865 |
file=>$marc_file, format=>$format |
if (! $marc) { |
866 |
}) || die "Can't open MARC file '$marc_file'"; |
print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n"; |
867 |
|
next; |
868 |
|
} |
869 |
|
|
870 |
|
# count records in MARC file |
871 |
|
sub marc_count { |
872 |
|
my $filename = shift || die; |
873 |
|
my $file = MARC::File::USMARC->in($filename) || return; |
874 |
|
my $count = 0; |
875 |
|
while ($file->skip()) { |
876 |
|
$count++; |
877 |
|
} |
878 |
|
return $count; |
879 |
|
} |
880 |
|
|
881 |
my $i=0; # record nr. |
my $count = marc_count($marc_file) || warn "no records in '$marc_file'?"; |
882 |
|
|
883 |
my $rec; |
my $i = 1; |
884 |
|
|
885 |
while ($marc->nextmarc(1)) { |
while( my $rec = $marc->next() ) { |
886 |
|
|
887 |
# XXX |
progress($i,$count); |
|
fakeprogress($i++); |
|
888 |
|
|
889 |
my $swishpath = $database."#".$i; |
my $swishpath = $database."#".$i; |
890 |
|
|
891 |
if (my $xml = data2xml($type_base,$marc,$add_xml,$cfg,$database)) { |
if (my $xml = data2xml($type_base,$rec,$add_xml,$cfg,$database)) { |
892 |
$xml = $cp2utf->convert($xml); |
$xml = $cp2utf->convert($xml); |
893 |
use bytes; # as opposed to chars |
use bytes; # as opposed to chars |
894 |
print "Path-Name: $swishpath\n"; |
print "Path-Name: $swishpath\n"; |
895 |
print "Content-Length: ".(length($xml)+1)."\n"; |
print "Content-Length: ".(length($xml)+1)."\n"; |
896 |
print "Document-Type: XML\n\n$xml\n"; |
print "Document-Type: XML\n\n$xml\n"; |
897 |
} |
} |
898 |
|
|
899 |
|
$i++; |
900 |
} |
} |
901 |
|
|
902 |
|
print STDERR "\n"; |
903 |
|
|
904 |
} elsif ($type_base eq "feed") { |
} elsif ($type_base eq "feed") { |
905 |
|
|
906 |
$import2cp = Text::Iconv->new($config->{feed_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{feed_codepage},$codepage); |
941 |
} |
} |
942 |
# close lookup |
# close lookup |
943 |
untie %lhash if (%lhash); |
untie %lhash if (%lhash); |
944 |
|
|
945 |
|
} elsif ($type_base eq "dbf") { |
946 |
|
|
947 |
|
my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!"; |
948 |
|
my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!"; |
949 |
|
my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!"; |
950 |
|
|
951 |
|
$import2cp = Text::Iconv->new($dbf_codepage,$codepage); |
952 |
|
require XBase; |
953 |
|
my $db = new XBase $dbf_file; |
954 |
|
|
955 |
|
if (! $db) { |
956 |
|
print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n"; |
957 |
|
next; |
958 |
|
} |
959 |
|
|
960 |
|
my $max_rowid = $db->last_record; |
961 |
|
|
962 |
|
print STDERR "Reading database: $dbf_file [$max_rowid rows]\n"; |
963 |
|
|
964 |
|
my %dbf2iso; |
965 |
|
foreach my $m (split(/[\n\r]+/,$dbf_mapping)) { |
966 |
|
my ($col,$fld) = split(/\s+/,$m,2); |
967 |
|
$dbf2iso{$col} = $fld; |
968 |
|
} |
969 |
|
|
970 |
|
#print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n"; |
971 |
|
|
972 |
|
# bad, bad... |
973 |
|
require "to_hash.pm"; |
974 |
|
|
975 |
|
foreach my $row_id (0 .. $max_rowid) { |
976 |
|
my $dbf_row = $db->get_record_as_hash($row_id); |
977 |
|
if ($dbf_row) { |
978 |
|
|
979 |
|
#print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n"; |
980 |
|
# apply mapping from config file |
981 |
|
# all unspecified records will get _ in |
982 |
|
# front of them - _DELETE will be __DELETE |
983 |
|
my $rec; |
984 |
|
map { |
985 |
|
my $new_fld = $dbf2iso{$_} || '_'.$_; |
986 |
|
my $data = $dbf_row->{$_}; |
987 |
|
push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/); |
988 |
|
} keys %{$dbf_row}; |
989 |
|
#print STDERR "## rec: ",Dumper($rec),"\n## /rec\n"; |
990 |
|
my $row = to_hash($row_id+1, $rec); |
991 |
|
|
992 |
|
$row->{mfn} = $row_id+1; |
993 |
|
$row->{record} = $rec; |
994 |
|
|
995 |
|
#print STDERR "## row: ",Dumper($row),"\n## /row\n"; |
996 |
|
progress($row->{mfn}, $max_rowid); |
997 |
|
|
998 |
|
my $swishpath = $path."#".int($row->{mfn}); |
999 |
|
|
1000 |
|
if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) { |
1001 |
|
$xml = $cp2utf->convert($xml); |
1002 |
|
use bytes; # as opposed to chars |
1003 |
|
print "Path-Name: $swishpath\n"; |
1004 |
|
print "Content-Length: ".(length($xml)+1)."\n"; |
1005 |
|
print "Document-Type: XML\n\n$xml\n"; |
1006 |
|
} |
1007 |
|
} |
1008 |
|
} |
1009 |
|
print STDERR "\n"; |
1010 |
} |
} |
1011 |
} |
} |
1012 |
|
|
1021 |
|
|
1022 |
all2xml.pl - read various file formats and dump XML for SWISH-E |
all2xml.pl - read various file formats and dump XML for SWISH-E |
1023 |
|
|
1024 |
|
=head1 SYNOPSYS |
1025 |
|
|
1026 |
|
$ all2xml.pl [test.conf] |
1027 |
|
|
1028 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
1029 |
|
|
1030 |
This command will read ISIS data file using OpenIsis perl module, MARC |
This command will read ISIS data file using Biblio::Isis perl module, MARC |
1031 |
records using MARC module and optionally Micro$oft Excel files to |
records using MARC::File module and optionally Micro$oft Excel files to |
1032 |
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, |
1033 |
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 |
1034 |
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. |
1035 |
|
|
1036 |
|
If no configuration file is specified, it will use default one called |
1037 |
|
C<all2xml.conf>. |
1038 |
|
|
1039 |
=head1 BUGS |
=head1 BUGS |
1040 |
|
|
1041 |
Documentation is really lacking. However, in true Open Source spirit, source |
Documentation is really lacking. However, in true Open Source spirit, source |