7 |
use XML::Simple; |
use XML::Simple; |
8 |
use Text::Unaccent 1.02; # 1.01 won't compile on my platform, |
use Text::Unaccent 1.02; # 1.01 won't compile on my platform, |
9 |
use Text::Iconv; |
use Text::Iconv; |
10 |
|
use Config::IniFiles; |
11 |
|
|
12 |
$|=1; |
$|=1; |
13 |
|
|
14 |
my $config; |
my $config_file = $0; |
15 |
|
$config_file =~ s/\.pl$/.conf/; |
16 |
|
die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file); |
17 |
|
|
18 |
$config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1); |
my $config; |
19 |
|
|
20 |
use index_DBI; # there is no other, right now ;-) |
use index_DBI; # there is no other, right now ;-) |
21 |
|
|
31 |
|
|
32 |
getopts('d:m:qs', \%opts); |
getopts('d:m:qs', \%opts); |
33 |
|
|
34 |
my $db_dir = $opts{d} || "ps"; # FIX |
my $db_dir; |
35 |
|
|
36 |
#die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts); |
#die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts); |
37 |
|
|
40 |
|
|
41 |
Text::Iconv->raise_error(1); # Conversion errors raise exceptions |
Text::Iconv->raise_error(1); # Conversion errors raise exceptions |
42 |
|
|
43 |
my $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8'); |
#my $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8'); |
44 |
my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage}); |
#my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage}); |
45 |
|
my $isis_codepage; |
46 |
|
my $index_codepage; |
47 |
my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1'); |
my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1'); |
48 |
|
my $xml_codepage; |
49 |
|
|
50 |
sub isis2xml { |
sub isis2xml { |
51 |
|
|
52 |
use xmlify; |
use xmlify; |
53 |
|
|
54 |
my $row = shift @_; |
my $row = shift @_; |
55 |
|
my $add_xml = shift @_; |
56 |
|
|
57 |
my $xml; |
my $xml; |
|
$xml .= xmlify('db_dir',$db_dir); # FIX remove? |
|
58 |
|
|
59 |
use parse_format; |
use parse_format; |
60 |
|
|
61 |
|
my $html = ""; # html formatted display output |
62 |
|
|
63 |
|
my %field_usage; # counter for usage of each field |
64 |
|
|
65 |
foreach my $field (keys %{$config->{indexer}}) { |
foreach my $field (keys %{$config->{indexer}}) { |
66 |
|
|
67 |
|
$field_usage{$field}++; |
68 |
|
|
69 |
my $swish_data = ""; |
my $swish_data = ""; |
70 |
my $display_data = ""; |
my $display_data = ""; |
|
my $index_data = ""; |
|
71 |
|
|
72 |
foreach my $x (@{$config->{indexer}->{$field}->{isis}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{isis}}) { |
73 |
|
|
78 |
($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index"); |
($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index"); |
79 |
#print STDERR "## s: $s d: $d i: $i ## $format ##\n"; |
#print STDERR "## s: $s d: $d i: $i ## $format ##\n"; |
80 |
|
|
81 |
|
# FIX: this is ugly, UGLY, cludge string is returned |
82 |
|
# in UTF8 encoding , but as if source charset |
83 |
|
# is ISO8859-1 and not some other. This break other |
84 |
|
# character encodings, so we convert it first |
85 |
|
# back to ISO8859-1 |
86 |
$format = $cludge_codepage->convert($format); |
$format = $cludge_codepage->convert($format); |
87 |
|
|
88 |
my ($swish,$display) = parse_format($format,$row); |
my ($swish,$display) = parse_format($format,$row); |
89 |
#print STDERR "s: $swish\nd: $display\n" if ($swish); |
#print STDERR "s: $swish\nd: $display\n" if ($swish); |
90 |
|
|
91 |
#print STDERR "swish: $swish<-- display: $display<--\n"; |
#print STDERR "swish: $swish<-- display: $display<--\n"; |
92 |
# FIX: this is ugly, UGLY, cludge: OpenIsis return |
|
93 |
# UTF8 encoding of strings, but as if source charset |
# filter="name" ; filter this field through |
94 |
# is ISO8859-1 and not some other. This breaks our |
# filter/[name].pm |
95 |
# isis character encoding, so we convert it first |
my $filter; |
96 |
# back to ISO8859-1 (which can actually be different |
if ($x->{filter}) { |
97 |
# encoding in isis) |
$filter = "filter/".$x->{filter}.".pm"; |
98 |
|
require $filter; |
99 |
$swish_data .= $swish if ($s && $swish); |
} |
100 |
$display_data .= $display if ($d && $display); |
# type="swish" ; field for swish |
101 |
$index_data .= $display if ($i && $display); |
if ($s && $swish) { |
102 |
|
if ($filter) { |
103 |
|
$swish_data .= join(" ",&filter($swish)); |
104 |
|
} else { |
105 |
|
$swish_data .= $swish if ($s && $swish); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
# type="display" ; field for display |
110 |
|
if ($d && $display) { |
111 |
|
if ($filter) { |
112 |
|
$display_data .= join(" ",&filter($display)); |
113 |
|
} else { |
114 |
|
$display_data .= $display; |
115 |
|
} |
116 |
|
} |
117 |
|
|
118 |
|
# type="index" ; insert into index |
119 |
|
if ($i && $display) { |
120 |
|
my $index_data = $index_codepage->convert($display) || $display; |
121 |
|
if ($filter) { |
122 |
|
foreach my $d (&filter($index_data)) { |
123 |
|
$index->insert($field, $d, $db_dir); |
124 |
|
} |
125 |
|
} else { |
126 |
|
$index->insert($field, $index_data, $db_dir); |
127 |
|
} |
128 |
|
} |
129 |
} |
} |
130 |
|
|
131 |
|
|
132 |
#print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data); |
#print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data); |
133 |
if ($display_data) { |
if ($display_data) { |
134 |
$display_data = $isis_codepage->convert($display_data)."##" || $display_data; |
$display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !"; |
135 |
$xml .= xmlify($field."_display", $display_data); |
# FIX: this is removed and replaced by html tag. |
136 |
|
#$xml .= xmlify($field."_display", $display_data); |
137 |
|
|
138 |
|
if ($field eq "headline") { |
139 |
|
$xml .= xmlify("headline", $display_data); |
140 |
|
} else { |
141 |
|
|
142 |
|
# find field name (signular, plural) |
143 |
|
my $field_name = ""; |
144 |
|
if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) { |
145 |
|
$field_name = $config->{indexer}->{$field}->{name_singular}."#-#"; |
146 |
|
} elsif ($config->{indexer}->{$field}->{name_plural}) { |
147 |
|
$field_name = $config->{indexer}->{$field}->{name_plural}."#-#"; |
148 |
|
} elsif ($config->{indexer}->{$field}->{name}) { |
149 |
|
$field_name = $config->{indexer}->{$field}->{name}."#-#"; |
150 |
|
} else { |
151 |
|
print STDERR "WARNING: field '$field' doesn't have 'name' attribute!"; |
152 |
|
} |
153 |
|
if ($field_name) { |
154 |
|
$html .= $xml_codepage->convert($field_name); |
155 |
|
} |
156 |
|
$html .= $display_data."###\n"; |
157 |
|
} |
158 |
} |
} |
159 |
if ($swish_data) { |
if ($swish_data) { |
160 |
my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2'); |
my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2'); |
161 |
|
# remove extra spaces |
162 |
|
$swish_data =~ s/ +/ /g; |
163 |
|
$swish_data =~ s/ +$//g; |
164 |
|
|
165 |
$swish_data = $i->convert($swish_data); |
$swish_data = $i->convert($swish_data); |
166 |
$xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data)); |
$xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data)); |
167 |
#$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data; |
#$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data; |
168 |
#$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data)); |
#$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data)); |
169 |
} |
} |
170 |
|
|
|
# index |
|
|
if ($index_data && $index_data ne "") { |
|
|
$index_data = $index_codepage->convert($index_data) || $index_data; |
|
|
$index->insert($field, $index_data, $db_dir); |
|
|
} |
|
171 |
|
|
172 |
} |
} |
173 |
|
|
174 |
|
# dump formatted output in <html> |
175 |
|
if ($html) { |
176 |
|
$xml .= xmlify("html",$html); |
177 |
|
} |
178 |
|
|
179 |
if ($xml) { |
if ($xml) { |
180 |
#print STDERR "x: $xml\n"; |
#print STDERR "x: $xml\n"; |
181 |
|
$xml .= $add_xml if ($add_xml); |
182 |
return "<xml>\n$xml</xml>\n"; |
return "<xml>\n$xml</xml>\n"; |
183 |
} else { |
} else { |
184 |
return; |
return; |
187 |
|
|
188 |
########################################################################## |
########################################################################## |
189 |
|
|
190 |
my $last_tell=0; |
my $cfg = new Config::IniFiles( -file => $config_file ); |
|
|
|
|
my @isis_dirs = ( '.' ); # use dirname as database name |
|
|
|
|
|
if ($opts{m}) { |
|
|
@isis_dirs = split(/,/,$opts{m}); |
|
|
} |
|
|
|
|
|
my @isis_dbs; |
|
|
|
|
|
foreach (@isis_dirs) { |
|
|
if (-e $config->{isis_data}."/$db_dir/$_/LIBRI") { |
|
|
push @isis_dbs,$config->{isis_data}."/$db_dir/$_/LIBRI/LIBRI"; |
|
|
} |
|
|
if (-e $config->{isis_data}."/$db_dir/$_/PERI") { |
|
|
push @isis_dbs,$config->{isis_data}."/$db_dir/$_/PERI/PERI"; |
|
|
} |
|
|
if (-e $config->{isis_data}."/$db_dir/$_/AMS") { |
|
|
push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS"; |
|
|
} |
|
|
if (-e $config->{isis_data}."/$db_dir/$_/ARTI") { |
|
|
# push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI"; |
|
|
} |
|
|
} |
|
|
|
|
|
print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs); |
|
|
|
|
|
my $db; |
|
191 |
|
|
192 |
foreach my $isis_db (@isis_dbs) { |
foreach my $database ($cfg->Sections) { |
193 |
|
|
194 |
|
my $isis_db = $cfg -> val($database, 'isis_db'); |
195 |
|
my $type = $cfg -> val($database, 'type'); |
196 |
|
my $add_xml = $cfg -> val($database, 'xml'); |
197 |
|
|
198 |
|
# read configuration for this type |
199 |
|
$config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1); |
200 |
|
$isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8'); |
201 |
|
$index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage}); |
202 |
|
$xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),'UTF8'); |
203 |
|
|
204 |
my $db = OpenIsis::open( $isis_db ); |
my $db = OpenIsis::open( $isis_db ); |
205 |
if (0) { |
if (0) { |
213 |
|
|
214 |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
215 |
|
|
216 |
my $path = $isis_db; |
my $path = $database; # was $isis_db |
|
$path =~ s#$config->{isis_data}/*##g; |
|
217 |
|
|
218 |
my $last_p = 0; |
my $last_p = 0; |
219 |
|
|
230 |
$last_p = $p; |
$last_p = $p; |
231 |
} |
} |
232 |
|
|
233 |
if (my $xml = isis2xml($row)) { |
if (my $xml = isis2xml($row,$add_xml)) { |
234 |
#print STDERR "--ret-->$xml\n"; |
#print STDERR "--ret-->$xml\n"; |
235 |
print "Path-Name: $path#".int($row->{mfn})."\n"; |
print "Path-Name: $path#".int($row->{mfn})."\n"; |
236 |
print "Content-Length: ".(length($xml)+1)."\n"; |
print "Content-Length: ".(length($xml)+1)."\n"; |