--- trunk/all2xml.pl 2003/01/11 19:55:30 9 +++ trunk/all2xml.pl 2003/11/29 15:59:19 182 @@ -6,16 +6,24 @@ use Data::Dumper; use XML::Simple; use Text::Unaccent 1.02; # 1.01 won't compile on my platform, -require Unicode::Map8; -use DBI; - -my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1); -my $dbh = DBI->connect("DBI:Pg:dbname=webpac","","") || die $DBI::errstr; # FIX -# FIX; select relname from pg_class where relname like 'index_%' ; -$dbh->begin_work || die $dbh->errstr(); - -$dbh->do("delete from index_author") || die $dbh->errstr(); -$dbh->do("delete from index_title") || die $dbh->errstr(); +use Text::Iconv; +use Config::IniFiles; +use Encode; +#use GDBM_File; +use Fcntl; # for O_RDWR +use TDB_File; + +$|=1; + +my $config_file = $0; +$config_file =~ s/\.pl$/.conf/; +die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file); + +my $config; + +#use index_DBI; # default DBI module for index +use index_DBI_cache; # faster DBI module using memory cache +my $index; my %opts; @@ -27,117 +35,473 @@ getopts('d:m:qs', \%opts); -my $db_dir = $opts{d} || "ps"; # FIX +my $path; # this is name of database + +Text::Iconv->raise_error(0); # Conversion errors don't raise exceptions + +# this is encoding of all files on disk, including import_xml/*.xml file and +# filter/*.pm files! It will be used to store strings in perl internally! +my $codepage = 'ISO-8859-2'; + +my $utf2cp = Text::Iconv->new('UTF-8',$codepage); +# this function will convert data from XML files to local encoding +sub x { + return $utf2cp->convert($_[0]); +} -#die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts); +# decode isis/excel or other import codepage +my $import2cp; -#print Dumper($config->{indexer}); -#print "-" x 70,"\n"; +# outgoing xml must be in UTF-8 +my $cp2utf = Text::Iconv->new($codepage,'UTF-8'); -# how to convert isis code page to UTF8? -my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die; +# mapping between data type and tag which specify +# format in XML file +my %type2tag = ( + 'isis' => 'isis', + 'excel' => 'column', + 'marc' => 'marc', + 'feed' => 'feed' +); + +my $cache; # for cacheing + +# lookup hash (tied to file) +my %lhash; +# this option will cache all lookup entries in memory. +# if you are tight on memory, turn this off +my $use_lhash_cache = 1; -sub isis2xml { +sub data2xml { + use xmlify; + + my $type = shift @_; my $row = shift @_; + my $add_xml = shift @_; + # needed to read values from configuration file + my $cfg = shift @_; + my $database = shift @_; my $xml; - $xml->{db_dir} = [ $db_dir ]; # FIX remove? - sub isis_sf { - my $row = shift @_; - my $isis_id = shift @_; - my $subfield = shift @_; - if ($row->{$isis_id}->[0]) { - my $sf = OpenIsis::subfields($row->{$isis_id}->[0]); - if (! defined $subfield || length($subfield) == 0) { - # subfield list undef, empty or no defined subfields for this record - my $all_sf = $row->{$isis_id}->[0]; - $all_sf =~ s/\^./ /g; nuke definirions - return $all_sf; - } elsif ($sf->{$subfield}) { - return $sf->{$subfield}; - } + use parse_format; + + my $html = ""; # html formatted display output + + my %field_usage; # counter for usage of each field + + # sort subrouting using order="" attribute + sub by_order { + my $va = $config->{indexer}->{$a}->{order} || + $config->{indexer}->{$a}; + my $vb = $config->{indexer}->{$b}->{order} || + $config->{indexer}->{$b}; + + return $va <=> $vb; + } + + my @sorted_tags; + if ($cache->{tags_by_order}) { + @sorted_tags = @{$cache->{tags_by_order}}; + } else { + @sorted_tags = sort by_order keys %{$config->{indexer}}; + $cache->{tags_by_order} = \@sorted_tags; + } + + # lookup key + my $lookup_key; + + # cache for field in pages + delete $cache->{display_data}; + delete $cache->{swish_data}; + delete $cache->{swish_exact_data}; + delete $cache->{index_data}; + my @page_fields; # names of fields + + + # subs used to produce output + + sub get_field_name($$$) { + my ($config,$field,$field_usage) = @_; + + # find field name (signular, plural) + my $field_name = ""; + if ($config->{indexer}->{$field}->{name_singular} && $field_usage == 1) { + $field_name = $config->{indexer}->{$field}->{name_singular}; + } elsif ($config->{indexer}->{$field}->{name_plural}) { + $field_name = $config->{indexer}->{$field}->{name_plural}; + } elsif ($config->{indexer}->{$field}->{name}) { + $field_name = $config->{indexer}->{$field}->{name}; + } else { + print STDERR "WARNING: field '$field' doesn't have 'name' attribute!"; + } + if ($field_name) { + return x($field_name); } } - foreach my $field (keys %{$config->{indexer}}) { - my $display_data = ""; + # begin real work: go field by field + foreach my $field (@sorted_tags) { + + $field=x($field); + $field_usage{$field}++; + my $swish_data = ""; - my $index_data = ""; + my $swish_exact_data = ""; + my $display_data = ""; + my @index_data; + my $line_delimiter; + + my ($swish,$display); + + my $tag = $type2tag{$type} || die "can't find which tag to use for type $type"; + + # is this field page-by-page? + my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page}; + push @page_fields,$field if ($iterate_by_page); + my %page_max = (); + # default line_delimiter if using + my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '
'; + + foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { + + my $format = x($x->{content}); + my $delimiter = x($x->{delimiter}) || ' '; - foreach my $x (@{$config->{indexer}->{$field}->{isis}}) { + my $repeat_off = 0; # init repeatable offset - my $display_tmp = ""; - my $swish_tmp = ""; - my $index_tmp = ""; - - my $format = $x->{content}; - my $s = 1; # swish only - my $d = 1; # display only - my $i = 0; # index only + # swish, swish_exact, display, index, index_lookup + # 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,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index"); -#print STDERR "## s: $s d: $d i: $i ## $format ##\n"; - # parse format - my $prefix = ""; - if ($format =~ s/^([^\d]+)//) { - $prefix = $1; - } - while ($format) { - if ($format =~ s/^(\d\d\d)(\w?)//) { - my $isis_tmp = isis_sf($row,$1,$2); - if ($isis_tmp) { -# $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d); - $display_tmp .= $prefix . $isis_tmp if ($d); - $swish_tmp .= $isis_tmp." " if ($s); - $index_tmp .= $prefix . $isis_tmp if ($i); -#print STDERR " $isis_tmp <--\n"; - } - $prefix = ""; - } elsif ($format =~ s/^([^\d]+)//) { - $prefix = $1; + ($s,$se,$d,$i) = (0,0,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/); + + # what will separate last line from this one? + if ($display_data && $x->{append}) { + $line_delimiter = ' '; + } elsif ($display_data) { + $line_delimiter = '
'; + } + + # init vars so that we go into while... + ($swish,$display) = (1,1); + + # placeholder for all repeatable entries for index + + sub mkformat { + my $x = shift || die "mkformat needs tag reference"; + my $data = shift || return; + my $format_name = x($x->{format_name}) || return $data; + my $fmt = x($config->{format}->{$format_name}->{content}) || die " is not defined!"; + my $format_delimiter = x($x->{format_delimiter}); + my @data; + if ($format_delimiter) { + @data = split(/$format_delimiter/,$data); + } else { + push @data,$data; + } + + if ($fmt) { + my $nr = scalar $fmt =~ s/%s/%s/g; + if (($#data+1) == $nr) { + return sprintf($fmt,@data); + } else { + print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n"; + return $data; + } } else { - print STDERR "WARNING: unparsed format '$format'\n"; + print STDERR "usage of link '$format_name' without defined format ( tag)\n"; + } + } + + # while because of repeatable fields + while ($swish || $display) { + my $page = $repeat_off; + $page_max{$field} = $page if ($iterate_by_page && $page > ($page_max{$field} || 0)); + ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp); + if ($repeat_off > 1000) { + print STDERR "loop (more than 1000 repeatable fields) deteced in $row, $format\n"; last; - }; + } + + # is this field is lookup? + if ($display && $x->{lookup}) { + my $null = ""; + if ($use_lhash_cache) { + if (!defined($cache->{lhash}->{$display})) { + my $new_display = $lhash{$display}; + if (defined($new_display)) { +#print STDERR "lookup cache store '$display' = '$new_display'\n"; + $display = $new_display; + $cache->{lhash}->{$display} = $new_display; + } else { + print STDERR "WARNING: lookup for '$display' didn't find anything.\n"; + $display = ""; + $cache->{lhash}->{$display} = $null; + } + } else { + $display = $cache->{lhash}->{$display}; + } + } else { + $display = $lhash{$display} || $null; + } + } + + # filter="name" ; filter this field through + # filter/[name].pm + my $filter = $x->{filter}; + if ($filter && !$cache->{filter_loaded}->{$filter}) { + require "filter/".$filter.".pm"; + $cache->{filter_loaded}->{$filter}++; + } + # type="swish" ; field for swish + if ($swish) { + if ($filter && ($s || $se)) { + no strict 'refs'; + my $tmp = join(" ",&$filter($swish)) 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); + } + } + + # type="display" ; field for display + if ($d && $display) { + if ($line_delimiter && $display_data) { + $display_data .= $line_delimiter; + } + if ($filter) { + no strict 'refs'; + if ($display_data) { + $display_data .= $delimiter.join($delimiter,mkformat($x,&$filter($display))); + } else { + $display_data = join($delimiter,mkformat($x,&$filter($display))); + } + } else { + if ($display_data) { + $display_data .= $delimiter.mkformat($x,$display); + } else { + $display_data = mkformat($x,$display); + } + } + } + + # type="index" ; insert into index + my $idisplay; + if ($i && $display) { + $idisplay = $display; + if ($filter) { + no strict 'refs'; + $idisplay = &$filter($idisplay); + } + push @index_data, $idisplay if (! $iterate_by_page); + } + + # store fields in lookup + if ($il && $display) { + if (lc($x->{type}) eq "lookup_key") { + if ($lookup_key) { + print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)"; + } else { + $lookup_key = $display; + } + } elsif (lc($x->{type}) eq "lookup_val") { + if ($lookup_key) { + $lhash{$lookup_key} = $display; + } else { + print STDERR "WARNING: no lookup_key defined for '$display'?"; + } + } + + } + + # store data for page-by-page repeatable fields + if ($iterate_by_page) { + sub iterate_fld($$$$$$) { + my ($cache,$what,$field,$page,$data,$append) = @_; + return if (!$data); + + my $ldel = $page_line_delimiter; + $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; + } + } + + if ($display_data) { + iterate_fld($cache,'display_data',$field,$page,$display_data,$x->{append}); + } + $display_data = ""; + if ($swish_data) { + iterate_fld($cache,'swish_data',$field,$page,$swish_data,$x->{append}); + $swish_data = ""; + } + if ($swish_exact_data) { + iterate_fld($cache,'swish_exact_data',$field,$page,$swish_exact_data,$x->{append}); + $swish_exact_data = ""; + } + + if ($idisplay) { + my $ldel=$page_line_delimiter; + my @index_data; + if ($cache->{index_data}->{$field}->[$page]) { + + @index_data = @{$cache->{index_data}->{$field}->[$page]}; + } + if ($x->{append}) { + $index_data[$#index_data] .= $idisplay; + } else { + push @index_data, $idisplay; + } + $idisplay = ""; + @{$cache->{index_data}->{$field}->[$page]} = @index_data; + } + } + } + + if (! $iterate_by_page) { + # fill data in index + foreach my $d (@index_data) { + $index->insert($field, $d, $path); + } + @index_data = (); + } + } + + # now try to parse variables from configuration file + foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) { + + my $delimiter = x($x->{delimiter}) || ' '; + my $val = $cfg->val($database, x($x->{content})); + + my ($s,$d,$i) = (1,1,0); # swish, display default + $s = 0 if (lc($x->{type}) eq "display"); + $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"); + + if ($val) { + $display_data .= $delimiter.$val if ($d); + $swish_data .= $val if ($s); + $index->insert($field, $val, $path) if ($i); + } + + if ($iterate_by_page) { + # FIXME data from config tag will appear just + # on first page!!! + my $page = 0; + if ($display_data) { + $cache->{display_data}->{$field}->[$page] = $display_data; + $display_data = ""; + } + if ($swish_data) { + $cache->{swish_data}->{$field}->[$page] = $swish_data; + $swish_data = ""; + } + if ($swish_exact_data) { + $cache->{swish_exact_data}->{$field}->[$page] = $swish_exact_data; + $swish_exact_data = ""; + } } - # add suffix - $display_tmp .= $prefix if ($display_tmp); - $index_tmp .= $prefix if ($index_tmp); - -# $display_data .= $display_tmp if ($display_tmp ne ""); -# $swish_data .= $swish_tmp if ($swish_tmp ne ""); - $display_data .= $display_tmp; - $swish_data .= $swish_tmp; - $index_data .= $index_tmp; - - } -#print "--display:$display_data\n--swish:$swish_data\n"; - #$xml->{$field."_display"} = $isis_map->tou($display_data)->utf8 if ($display_data); - #$xml->{$field."_swish"} = unac_string($config->{isis_codepage},$swish_data) if ($swish_data); - $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data); - $xml->{$field."_swish"} = [ unac_string($config->{isis_codepage},$swish_data) ] if ($swish_data); - - # index - if ($index_data && $index_data ne "") { - my $sql = "select $field from index_$field where upper($field)=upper(?)"; - my $sth = $dbh->prepare($sql) || die $dbh->errstr(); - $sth->execute($index_data) || die "SQL: $sql; ".$dbh->errstr(); -#print STDERR "--->$index_data<---\n"; - if (! $sth->fetchrow_hashref) { - my $sql = "insert into index_$field values (?)"; - my $sth = $dbh->prepare($sql) || die $dbh->errstr(); -#print STDERR "$sql: $index_data