12 use Fcntl; # for O_RDWR
19 $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);
25 #use index_DBI; # default DBI module for index
26 #use index_DBI_cache; # faster DBI module using memory cache
27 use index_DBI_filter; # filter support for indexes
34 # -m multiple directories
38 getopts('d:m:qs', \%opts);
40 my $path; # this is name of database
42 Text::Iconv->raise_error(0); # Conversion errors don't raise exceptions
44 # this is encoding of all files on disk, including import_xml/*.xml file and
45 # filter/*.pm files! It will be used to store strings in perl internally!
46 my $codepage = 'ISO-8859-2';
48 my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
49 # this function will convert data from XML files to local encoding
51 return $utf2cp->convert($_[0]);
54 # decode isis/excel or other import codepage
57 # outgoing xml must be in UTF-8
58 my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
60 # mapping between data type and tag which specify
69 my $cache; # for cacheing
71 # lookup hash (tied to file)
73 # this option will cache all lookup entries in memory.
74 # if you are tight on memory, turn this off
75 my $use_lhash_cache = 1;
77 my $last_field_name; # cache to prevent repeated fields
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);
88 my $add_xml = shift @_;
89 # needed to read values from configuration file
91 my $database = shift @_;
97 my $html = ""; # html formatted display output
99 my %field_usage; # counter for usage of each field
101 # sort subrouting using order="" attribute
103 my $va = $config->{indexer}->{$a}->{order} ||
104 $config->{indexer}->{$a};
105 my $vb = $config->{indexer}->{$b}->{order} ||
106 $config->{indexer}->{$b};
112 if ($cache->{tags_by_order}) {
113 @sorted_tags = @{$cache->{tags_by_order}};
115 @sorted_tags = sort by_order keys %{$config->{indexer}};
116 $cache->{tags_by_order} = \@sorted_tags;
119 if (! @sorted_tags) {
120 print STDERR "WARNING: no tags for this type found in import_xml file!\n";
126 # cache for field in pages
127 delete $cache->{display_data};
128 delete $cache->{swish_data};
129 delete $cache->{swish_exact_data};
130 delete $cache->{index_data};
131 delete $cache->{index_delimiter};
132 delete $cache->{distinct};
133 my @page_fields; # names of fields
136 # subs used to produce output
138 sub get_field_name($$$) {
139 my ($config,$field,$field_usage) = @_;
141 # find field name (signular, plural)
143 if ($config->{indexer}->{$field}->{name_singular} && $field_usage == 1) {
144 $field_name = $config->{indexer}->{$field}->{name_singular};
145 } elsif ($config->{indexer}->{$field}->{name_plural}) {
146 $field_name = $config->{indexer}->{$field}->{name_plural};
147 } elsif ($config->{indexer}->{$field}->{name}) {
148 $field_name = $config->{indexer}->{$field}->{name};
150 print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
154 $field_name = x($field_name);
155 if (! $last_field_name) {
156 $last_field_name = $field_name;
157 return $last_field_name;
158 } elsif ($field_name ne $last_field_name) {
159 $last_field_name = $field_name;
160 return $last_field_name;
166 # init variables for different types
167 sub init_visible_type($) {
170 # swish, swish_exact, display, index, index_lookup
171 # swish and display defaults
172 my ($s,$se,$d,$i,$il) = (1,0,1,0,0);
173 if (lc($type) eq "display") {
175 } elsif (lc($type) eq "swish") {
177 } elsif (lc($type) eq "index") {
178 ($s,$se,$d,$i) = (0,1,0,1);
179 } elsif (lc($type) eq "swish_exact") {
180 ($s,$se,$d,$i) = (0,1,0,0);
181 } elsif (lc($type) =~ /^lookup/) {
182 ($s,$se,$d,$i,$il) = (0,1,0,0,1);
184 print STDERR "WARNING: unknown type: $type\n";
186 return ($s,$se,$d,$i,$il);
193 # <delimiter>, </delimiter>
194 # <value>200a</value>
199 # <tag delimiter=", ">200a</tag>
201 # but without loosing spaces in delimiter (becasue
202 # new XML::Simple strips spaces in attribute values
203 # as defined in XML specification)
208 if (defined $x->{value}) {
209 my ($v,$d) = ($x->{value}->{content}, $x->{delimiter}->{content});
211 delete $x->{delimiter};
213 $d =~ s#>$## if ($d && $broken_cdata);
214 $x->{delimiter} = $d;
219 # begin real work: go field by field
220 foreach my $field (@sorted_tags) {
223 $field_usage{$field}++;
226 my $swish_exact_data = "";
227 my $display_data = "";
231 my ($swish,$display);
233 my $tag = $cfg->val($database, 'import_xml_tag') || $type2tag{$type} || die "can't find which tag to use for type $type";
235 # is this field page-by-page?
236 my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page};
237 push @page_fields,$field if ($iterate_by_page);
239 # default line_delimiter if using
240 my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>';
241 $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";
248 my $format_name = $config->{indexer}->{$field}->{format_name};
249 my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter};
250 if ($format_name && $format_delimiter) {
251 $cache->{format}->{$field}->{format_name} = $format_name;
252 $cache->{format}->{$field}->{format_delimiter} = $format_delimiter;
255 foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
259 my $format = x($x->{content});
260 my $delimiter = x($x->{delimiter}) || ' ';
262 my $repeat_off = 0; # init repeatable offset
264 my ($s,$se,$d,$i,$il) = init_visible_type($x->{type});
266 # what will separate last line from this one?
267 if ($display_data && $x->{append}) {
268 $line_delimiter = $delimiter;
269 } elsif ($display_data) {
270 $line_delimiter = '<br/>';
273 # init vars so that we go into while...
274 ($swish,$display) = (1,1);
277 my $x = shift || die "mkformat needs tag reference";
278 my $data = shift || return;
279 my $format_name = x($x->{format_name}) || return $data;
280 my $fmt = x($config->{format}->{$format_name}->{content}) || die "<format name=\"$format_name\"> is not defined!";
281 my $format_delimiter = x($x->{format_delimiter});
283 if ($format_delimiter) {
284 @data = split(/$format_delimiter/,$data);
290 my $nr = scalar $fmt =~ s/%s/%s/g;
291 if (($#data+1) == $nr) {
292 return sprintf($fmt,@data);
294 #print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n";
298 print STDERR "usage of link '$format_name' without defined format (<link> tag)\n";
302 # while because of repeatable fields
303 while ($swish || $display) {
304 my $page = $repeat_off;
305 $page_max{$field} = $page if ($iterate_by_page && $page > ($page_max{$field} || 0));
306 ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
307 if ($repeat_off > 1000) {
308 print STDERR "loop (more than 1000 repeatable fields) deteced in $row, $format\n";
312 # is this field is lookup?
313 if ($display && $x->{lookup}) {
314 my $null = "<!-- null -->";
315 if ($use_lhash_cache) {
316 if (!defined($cache->{lhash}->{$display})) {
317 my $new_display = $lhash{$display};
318 if (defined($new_display)) {
319 #print STDERR "lookup cache store '$display' = '$new_display'\n";
320 $display = $new_display;
321 $cache->{lhash}->{$display} = $new_display;
323 # print STDERR "WARNING: lookup for '$display' didn't find anything.\n";
325 $cache->{lhash}->{$display} = $null;
328 $display = $cache->{lhash}->{$display};
331 $display = $lhash{$display} || $null;
335 # filter="name" ; filter this field through
337 my $filter = $x->{filter};
338 if ($filter && !$cache->{filter_loaded}->{$filter}) {
339 require "filter/".$filter.".pm";
340 $cache->{filter_loaded}->{$filter}++;
342 # type="swish" ; field for swish
345 if ($filter && ($s || $se)) {
347 $tmp = join(" ",&$filter($tmp)) if ($s || $se);
350 $swish_data .= $tmp if ($s && $tmp);
351 $swish_exact_data .= "xxbxx $tmp xxexx " if ($tmp && $tmp ne "" && $se);
354 # type="display" ; field for display
355 if ($d && $display) {
356 my $ldel = $delimiter;
357 if ($line_delimiter && $display_data) {
358 $ldel = $line_delimiter;
363 foreach my $tmp (&$filter($display)) {
364 my $tmp2 = mkformat($x,$tmp);
365 push @arr,$tmp2 if ($tmp2);
367 $display_data .= $ldel if ($display_data && @arr);
368 $display_data .= join($delimiter,@arr);
370 $display_data .= $ldel if ($display_data);
371 my $tmp = mkformat($x,$display);
372 $display_data .= $tmp if ($tmp);
376 # type="index" ; insert into index
378 if ($i && $display) {
379 $idisplay = $display;
382 $idisplay = &$filter($idisplay);
384 push @index_data, $idisplay if ($idisplay && !$iterate_by_page);
387 # store fields in lookup
388 if ($il && $display) {
389 if (lc($x->{type}) eq "lookup_key") {
391 print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)";
395 $lookup_key = &$filter($display);
397 $lookup_key = $display;
400 } elsif (lc($x->{type}) eq "lookup_val") {
404 $lhash{$lookup_key} = &$filter($display);
406 $lhash{$lookup_key} = $display;
409 print STDERR "WARNING: no lookup_key defined for '$display'?";
415 # store data for page-by-page repeatable fields
416 if ($iterate_by_page) {
417 sub iterate_fld($$$$$$) {
418 my ($cache,$what,$field,$page,$data,$append) = @_;
421 my $ldel = $page_line_delimiter;
422 $ldel = " " if ($append);
423 #print STDERR "line delimiter: ",Dumper($ldel) if ($ldel);
424 if (! $cache->{$what}->{$field}->[$page]) {
425 push @{$cache->{$what}->{$field}->[$page]}, {
433 iterate_fld($cache,'display_data',$field,$page,$display_data,$x->{append});
437 iterate_fld($cache,'swish_data',$field,$page,$swish_data,$x->{append});
440 if ($swish_exact_data) {
441 iterate_fld($cache,'swish_exact_data',$field,$page,$swish_exact_data,$x->{append});
442 $swish_exact_data = "";
446 my $ldel=$page_line_delimiter;
448 if ($cache->{index_data}->{$field}->[$page]) {
450 @index_data = @{$cache->{index_data}->{$field}->[$page]};
454 $index_data[$#index_data] .= $idisplay;
456 push @index_data, $idisplay;
459 push @index_data, $idisplay;
462 @{$cache->{index_data}->{$field}->[$page]} = @index_data;
467 if (! $iterate_by_page) {
468 my $idel = $x->{index_delimiter};
470 foreach my $tmp (@index_data) {
472 if ($idel && $tmp =~ m/$idel/) {
473 ($i,$d) = split(/$idel/,$tmp);
475 $index->insert($field, $i, $d, $path);
481 # now try to parse variables from configuration file
482 foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) {
486 my $delimiter = x($x->{delimiter}) || ' ';
487 my $val = $cfg->val($database, x($x->{content}));
489 # FIXME index_lookup is not supported!
490 my ($s,$se,$d,$i,$il) = init_visible_type($x->{type});
493 $display_data .= $delimiter.$val if ($d);
494 $swish_data .= " ".$val if ($s);
495 $index->insert($field, $val, $val, $path) if ($i);
498 if ($iterate_by_page) {
499 # FIXME data from config tag will appear just
503 push @{$cache->{display_data}->{$field}->[$page]}, { data => $display_data };
507 push @{$cache->{swish_data}->{$field}->[$page]}, { data => $swish_data };
510 if ($swish_exact_data) {
511 push @{$cache->{swish_exact_data}->{$field}->[$page]}, { data => $swish_exact_data };
512 $swish_exact_data = "";
517 # save data page-by-page
518 foreach my $field (@page_fields) {
519 my $nr_pages = $page_max{$field} || next;
520 #print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n";
521 #print STDERR Dumper($cache->{display_data});
522 my $seen; # used for distinct
523 for (my $page=0; $page <= $nr_pages; $page++) {
526 foreach my $element (@{ $cache->{display_data}->{$field}->[$page] }) {
527 my $data = $element->{data};
528 die "BUG! no data in element?" unless ($data);
531 next if ($cache->{distinct}->{$field}->{ $data });
532 $cache->{distinct}->{$field}->{ $data } = 1;
535 if ($cache->{format}->{$field}) {
536 my $tmp = mkformat($cache->{format}->{$field},$data);
537 $display_data .= $delimiter . $tmp if ($tmp);
539 $display_data .= $delimiter . $data;
541 $delimiter = $element->{delimiter} if ($element->{delimiter});
544 if ($display_data) { # default
545 if ($field eq "headline") {
546 $xml .= xmlify("headline", $display_data);
549 # fallback to empty field name if needed
550 $html .= get_field_name($config,$field,$field_usage{$field}) || '';
551 $html .= "#-#".$display_data."###\n";
555 my $swish_data = join(" ",map { $_->{data} } @{ $cache->{swish_data}->{$field}->[$page] });
557 # remove extra spaces
558 $swish_data =~ s/ +/ /g;
559 $swish_data =~ s/ +$//g;
561 $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
564 my $swish_exact_data = join(" ", map { $_->{data} } @{ $cache->{swish_exact_data}->{$field}->[$page] });
565 if ($swish_exact_data) {
566 $swish_exact_data =~ s/ +/ /g;
567 $swish_exact_data =~ s/ +$//g;
569 # add delimiters before and after word.
570 # That is required to produce exact match
571 $xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data));
574 my $idel = $cache->{index_delimiter}->{$field};
575 foreach my $tmp (@{$cache->{index_data}->{$field}->[$page]}) {
578 if ($idel && $tmp =~ m/$idel/) {
579 ($i,$d) = split(/$idel/,$tmp);
581 $index->insert($field, $i, $d, $path);
582 #print STDERR "index [$idel] $field: $i --> $d [$path]\n";
588 if (! $iterate_by_page) {
590 if ($field eq "headline") {
591 $xml .= xmlify("headline", $display_data);
594 # fallback to empty field name if needed
595 $html .= get_field_name($config,$field,$field_usage{$field}) || '';
596 $html .= "#-#".$display_data."###\n";
600 # remove extra spaces
601 $swish_data =~ s/ +/ /g;
602 $swish_data =~ s/ +$//g;
604 $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
607 if ($swish_exact_data) {
608 $swish_exact_data =~ s/ +/ /g;
609 $swish_exact_data =~ s/ +$//g;
611 # add delimiters before and after word.
612 # That is required to produce exact match
613 $xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data));
618 # dump formatted output in <html>
620 #$xml .= xmlify("html",$html);
621 $xml .= "<html><![CDATA[ $html ]]></html>";
625 $xml .= $add_xml if ($add_xml);
626 return "<xml>\n$xml</xml>\n";
632 ##########################################################################
634 # read configuration for this script
635 my $cfg = new Config::IniFiles( -file => $config_file );
637 # read global.conf configuration
638 my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
641 $index = new index_DBI(
642 $cfg_global->val('global', 'dbi_dbd'),
643 $cfg_global->val('global', 'dbi_dsn'),
644 $cfg_global->val('global', 'dbi_user'),
645 $cfg_global->val('global', 'dbi_passwd') || '',
648 my $show_progress = $cfg_global->val('global', 'show_progress');
650 my $my_unac_filter = $cfg_global->val('global', 'my_unac_filter');
651 if ($my_unac_filter) {
652 print STDERR "using $my_unac_filter to filter characters for search\n";
653 require $my_unac_filter;
655 print STDERR "### fallback to default my_unac_string!\n";
657 sub main::my_unac_string($$) {
658 my ($charset, $string) = (@_);
664 foreach my $database ($cfg->Sections) {
666 # save database name in global variable path for later
667 # (need for index filter creation)
670 my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
671 my $add_xml = $cfg -> val($database, 'xml'); # optional
673 # create new lookup file
674 my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional
676 #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644;
677 if (! -e $lookup_file) {
678 open(LOOKUP, "> $lookup_file") || die "can't create $lookup_file': $!";
681 tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644;
682 print STDERR "creating lookup file '$lookup_file'\n";
683 # delete memory cache for lookup file
684 delete $cache->{lhash};
687 # open existing lookup file
688 $lookup_file = $cfg -> val($database, 'lookup_open'); # optional
690 #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644;
691 tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644;
692 print STDERR "opening lookup file '$lookup_file'\n";
695 my $import_xml_type = $cfg->val($database, 'import_xml_file') || $type;
696 my $import_xml_file = "./import_xml/$import_xml_type.xml";
698 if (! -r $import_xml_file) {
699 print STDERR "ERROR: file $import_xml_file not readable skipping!\n";
703 print STDERR "reading $import_xml_file\n";
705 # extract just type basic
706 my $type_base = $type;
707 $type_base =~ s/_.+$//g;
709 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 );
712 # check for broken XML::Simple
713 if ( $broken_cdata ) {
715 $config->{format}->{$_}->{content} =~ s#>$##;
716 } keys %{ $config->{format} };
719 # helper for progress bar
724 my ($ss,$mm,$hh) = gmtime($t);
725 $out .= "${hh}h" if ($hh);
726 $out .= sprintf("%02d:%02d", $mm,$ss);
727 $out .= " " if ($hh == 0);
731 # output current progress indicator
733 my $start_t = time();
735 return if (! $show_progress);
737 my $total = shift || 1;
738 my $p = int($current * 100 / $total);
739 if ($p < $last_p || $current == 1) {
742 } elsif ($p != $last_p) {
743 my $rate = ($current / (time() - $start_t || 1));
744 my $eta = ($total-$current) / ($rate || 1);
745 printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$current,"=" x ($p/3)."$p%>", $total, $rate, fmt_time($eta));
752 my $last_fake_t = time();
754 return if (! $show_progress);
755 my $current = shift @_;
757 my @ind = ('-','\\','|','/','-','\\','|','/');
759 if ($current < $fake_pos) {
766 if (time()-$last_fake_t >= 1) {
767 $last_fake_t = time();
768 $fake_pos += $fake_dir;
769 $fake_dir = -$fake_dir if ($fake_pos > 38);
772 if ($current % 10 == 0) {
773 my $rate = ($current / (time() - $start_t || 1));
774 printf STDERR ("%5d [%-38s] %0.1f/s\r",$current, " " x $fake_pos .$ind[($current / 10) % 8], $rate);
780 # erase cache for tags by order in this database
781 delete $cache->{tags_by_order};
783 if ($type_base eq "isis") {
785 my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
787 $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
788 my $db = new Biblio::Isis( isisdb => $isis_db );
791 print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n";
795 my $max_rowid = $db->count if ($db);
797 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
799 for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
800 my $row = $db->to_hash( $row_id );
803 $row->{mfn} = $row_id;
804 $row->{record} = $db->{record};
806 progress($row->{mfn}, $max_rowid);
808 my $swishpath = $path."#".int($row->{mfn});
810 if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
811 $xml = $cp2utf->convert($xml);
812 use bytes; # as opposed to chars
813 print "Path-Name: $swishpath\n";
814 print "Content-Length: ".(length($xml)+1)."\n";
815 print "Document-Type: XML\n\n$xml\n";
821 } elsif ($type_base eq "excel") {
822 require Spreadsheet::ParseExcel;
823 require Spreadsheet::ParseExcel::Utility;
824 import Spreadsheet::ParseExcel::Utility qw(int2col);
826 $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);
827 my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
829 my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
830 my $start_row = x($config->{start_row}) || die "no start_row in $type.xml";
833 my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
836 foreach my $oWks (@{$oBook->{Worksheet}}) {
837 #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";
838 last if ($oWks->{Name} eq $sheet);
842 my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];
844 print STDERR "using sheet: ",$oWorksheet->{Name},"\n";
845 defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file";
846 my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow};
848 for(my $iR = $start_row ; defined $end_row && $iR <= $end_row ; $iR++) {
850 for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
851 my $cell = $oWorksheet->{Cells}[$iR][$iC];
853 # this conversion is a cludge.
854 # Files from Excell could have
855 # characters which don't fit into
856 # destination encoding.
857 $row->{int2col($iC)} = $utf2cp->convert($cell->Value) || $cell->Value;
861 progress($iR, $end_row);
863 # print "row[$iR/$end_row] ";
864 # foreach (keys %{$row}) {
865 # print "$_: ",$row->{$_},"\t";
869 my $swishpath = $database."#".$iR;
873 if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
874 $xml = $cp2utf->convert($xml);
875 use bytes; # as opposed to chars
876 print "Path-Name: $swishpath\n";
877 print "Content-Length: ".(length($xml)+1)."\n";
878 print "Document-Type: XML\n\n$xml\n";
884 } elsif ($type_base eq "marc") {
886 require MARC::File::USMARC;
888 $import2cp = Text::Iconv->new($config->{marc_codepage},$codepage);
889 my $marc_file = $cfg -> val($database, 'marc_file') || die "$database doesn't have 'marc_file' defined!";
891 # optional argument is format
892 warn "marc_format is no longer used!" if ($config->{marc_format});
893 print STDERR "Reading MARC file '$marc_file'\n";
895 my $marc = MARC::File::USMARC->in( $marc_file );
898 print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n";
902 # count records in MARC file
904 my $filename = shift || die;
905 my $file = MARC::File::USMARC->in($filename) || return;
907 while ($file->skip()) {
913 my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";
917 while( my $rec = $marc->next() ) {
921 my $swishpath = $database."#".$i;
923 if (my $xml = data2xml($type_base,$rec,$add_xml,$cfg,$database)) {
924 $xml = $cp2utf->convert($xml);
925 use bytes; # as opposed to chars
926 print "Path-Name: $swishpath\n";
927 print "Content-Length: ".(length($xml)+1)."\n";
928 print "Document-Type: XML\n\n$xml\n";
936 } elsif ($type_base eq "feed") {
938 $import2cp = Text::Iconv->new($config->{feed_codepage},$codepage);
939 my $prog = x($config->{prog}) || die "$database doesn't have 'prog' defined!";
941 print STDERR "Reading feed from program '$prog'\n";
943 open(FEED,"feeds/$prog |") || die "can't start $prog: $!";
945 my $i=1; # record nr.
954 my $swishpath = $database."#".$i++;
956 if (my $xml = data2xml($type_base,$data,$add_xml,$cfg,$database)) {
957 $xml = $cp2utf->convert($xml);
958 use bytes; # as opposed to chars
959 print "Path-Name: $swishpath\n";
960 print "Content-Length: ".(length($xml)+1)."\n";
961 print "Document-Type: XML\n\n$xml\n";
968 $line = $1 if (s/^(\d+):\s*//);
969 $data->{$line++} = $_;
975 untie %lhash if (%lhash);
977 } elsif ($type_base eq "dbf") {
979 my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!";
980 my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!";
981 my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!";
983 $import2cp = Text::Iconv->new($dbf_codepage,$codepage);
985 my $db = new XBase $dbf_file;
988 print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n";
992 my $max_rowid = $db->last_record;
994 print STDERR "Reading database: $dbf_file [$max_rowid rows]\n";
997 foreach my $m (split(/[\n\r]+/,$dbf_mapping)) {
998 my ($col,$fld) = split(/\s+/,$m,2);
999 $dbf2iso{$col} = $fld;
1002 #print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n";
1005 require "to_hash.pm";
1007 foreach my $row_id (0 .. $max_rowid) {
1008 my $dbf_row = $db->get_record_as_hash($row_id);
1011 #print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n";
1012 # apply mapping from config file
1013 # all unspecified records will get _ in
1014 # front of them - _DELETE will be __DELETE
1017 my $new_fld = $dbf2iso{$_} || '_'.$_;
1018 my $data = $dbf_row->{$_};
1019 push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/);
1021 #print STDERR "## rec: ",Dumper($rec),"\n## /rec\n";
1022 my $row = to_hash($row_id+1, $rec);
1024 $row->{mfn} = $row_id+1;
1025 $row->{record} = $rec;
1027 #print STDERR "## row: ",Dumper($row),"\n## /row\n";
1028 progress($row->{mfn}, $max_rowid);
1030 my $swishpath = $path."#".int($row->{mfn});
1032 if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
1033 $xml = $cp2utf->convert($xml);
1034 use bytes; # as opposed to chars
1035 print "Path-Name: $swishpath\n";
1036 print "Content-Length: ".(length($xml)+1)."\n";
1037 print "Document-Type: XML\n\n$xml\n";
1045 # call this to commit index
1050 ##########################################################################
1054 all2xml.pl - read various file formats and dump XML for SWISH-E
1058 $ all2xml.pl [test.conf]
1062 This command will read ISIS data file using Biblio::Isis perl module, MARC
1063 records using MARC::File module and optionally Micro$oft Excel files to
1064 create one XML file for usage with I<SWISH-E> indexer. Dispite it's name,
1065 this script B<isn't general xml generator> from isis files (isis allready
1066 has something like that). Output of this script is tailor-made for SWISH-E.
1068 If no configuration file is specified, it will use default one called
1073 Documentation is really lacking. However, in true Open Source spirit, source
1074 is best documentation. I even made considerable effort to comment parts
1075 which are not intuitively clear, so...
1079 Dobrica Pavlinusic <dpavlin@rot13.org>
1083 GNU Public License (GPL) v2 or later
1087 SWISH-E web site at http://www.swish-e.org