5 |
use Getopt::Std; |
use Getopt::Std; |
6 |
use Data::Dumper; |
use Data::Dumper; |
7 |
use XML::Simple; |
use XML::Simple; |
8 |
use Text::Unaccent; |
use Text::Unaccent 1.02; # 1.01 won't compile on my platform, |
9 |
require Unicode::Map8; |
use Text::Iconv; |
10 |
|
use Config::IniFiles; |
11 |
|
use Encode; |
12 |
|
|
13 |
my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1); |
$|=1; |
14 |
|
|
15 |
my %opts; |
my $config_file = $0; |
16 |
|
$config_file =~ s/\.pl$/.conf/; |
17 |
|
die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file); |
18 |
|
|
19 |
|
my $config; |
20 |
|
|
21 |
|
use index_DBI; # there is no other, right now ;-) |
22 |
|
|
23 |
getopts('d:m:q', \%opts); |
my $index = new index_DBI(); # open index |
24 |
|
|
25 |
my $db_dir = $opts{d} || "ps"; # FIX |
my %opts; |
26 |
|
|
27 |
#die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts); |
# usage: |
28 |
|
# -d directory name |
29 |
|
# -m multiple directories |
30 |
|
# -q quiet |
31 |
|
# -s run swish |
32 |
|
|
33 |
|
getopts('d:m:qs', \%opts); |
34 |
|
|
35 |
|
my $db_dir; |
36 |
|
|
37 |
|
Text::Iconv->raise_error(1); # Conversion errors raise exceptions |
38 |
|
|
39 |
|
# this is encoding of all files on disk, including import_xml/*.xml file and |
40 |
|
# filter/*.pm files! It will be used to store strings in perl internally! |
41 |
|
my $codepage = 'ISO-8859-2'; |
42 |
|
|
43 |
|
my $utf2cp = Text::Iconv->new('UTF-8',$codepage); |
44 |
|
# this function will convert data from XML files to local encoding |
45 |
|
sub x { |
46 |
|
return $utf2cp->convert($_[0]); |
47 |
|
} |
48 |
|
|
49 |
#print Dumper($config->{indexer}); |
# decode isis import codepage |
50 |
#print "-" x 70,"\n"; |
my $isis2cp; |
51 |
|
|
52 |
# how to convert isis code page to UTF8? |
# outgoing xml must be in UTF-8 |
53 |
my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die; |
my $cp2utf = Text::Iconv->new($codepage,'UTF-8'); |
54 |
|
|
55 |
sub isis2xml { |
sub isis2xml { |
56 |
|
|
57 |
|
use xmlify; |
58 |
|
|
59 |
my $row = shift @_; |
my $row = shift @_; |
60 |
|
my $add_xml = shift @_; |
61 |
|
|
62 |
my $xml; |
my $xml; |
63 |
|
|
64 |
sub isis_sf { |
use parse_format; |
65 |
my $row = shift @_; |
|
66 |
my $isis_id = shift @_; |
my $html = ""; # html formatted display output |
67 |
my $subfield = shift @_; |
|
68 |
if ($row->{$isis_id}->[0]) { |
my %field_usage; # counter for usage of each field |
69 |
my $sf = OpenIsis::subfields($row->{$isis_id}->[0]); |
|
70 |
if (! defined $subfield || length($subfield) == 0) { |
# sort subrouting using order="" attribute |
71 |
# subfield list undef, empty or no defined subfields for this record |
sub by_order { |
72 |
return $row->{$isis_id}->[0]; |
return 0 if (! $config->{indexer}->{$a}->{order}); |
73 |
} elsif ($sf->{$subfield}) { |
return 0 if (! $config->{indexer}->{$b}->{order}); |
74 |
return $sf->{$subfield}; |
|
75 |
} |
return $config->{indexer}->{$a}->{order} <=> |
76 |
} |
$config->{indexer}->{$b}->{order} ; |
77 |
} |
} |
78 |
|
|
79 |
foreach my $field (keys %{$config->{indexer}}) { |
foreach my $field (sort by_order keys %{$config->{indexer}}) { |
80 |
|
|
81 |
|
$field=x($field); |
82 |
|
|
83 |
|
$field_usage{$field}++; |
84 |
|
|
85 |
|
my $swish_data = ""; |
86 |
my $display_data = ""; |
my $display_data = ""; |
87 |
my $index_data = ""; |
my $line_delimiter; |
88 |
|
|
89 |
|
my ($swish,$display); |
90 |
|
|
91 |
foreach my $x (@{$config->{indexer}->{$field}->{isis}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{isis}}) { |
92 |
|
|
93 |
my $display_tmp = ""; |
my $format = x($x->{content}); |
94 |
my $index_tmp = ""; |
my $delimiter = x($x->{delimiter}) || ' '; |
95 |
|
|
96 |
my $format = $x->{content}; |
my $isis_i = 0; # isis repeatable offset |
97 |
my $i = 1; # index only |
|
98 |
my $d = 1; # display only |
my ($s,$d,$i) = (1,1,0); # swish, display default |
99 |
$i = 0 if (lc($x->{type}) eq "display"); |
$s = 0 if (lc($x->{type}) eq "display"); |
100 |
$d = 0 if (lc($x->{type}) eq "index"); |
$d = 0 if (lc($x->{type}) eq "swish"); |
101 |
#print "## i: $i d: $d ## $format ##"; |
($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index"); |
102 |
# parse format |
|
103 |
my $prefix = ""; |
# what will separate last line from this one? |
104 |
if ($format =~ s/^([^\d]+)//) { |
if ($display_data && $x->{append} && $x->{append} eq "1") { |
105 |
$prefix = $1; |
$line_delimiter = ' '; |
106 |
|
} elsif ($display_data) { |
107 |
|
$line_delimiter = '<br/>'; |
108 |
} |
} |
109 |
while ($format) { |
|
110 |
if ($format =~ s/^(\d\d\d)(\w?)//) { |
# init vars so that we go into while... |
111 |
my $isis_tmp = isis_sf($row,$1,$2); |
($swish,$display) = (1,1); |
112 |
if ($isis_tmp) { |
|
113 |
# $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d); |
while ($swish || $display) { |
114 |
$display_tmp .= $prefix . $isis_tmp if ($d); |
($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp); |
115 |
$index_tmp .= $isis_tmp." " if ($i); |
|
116 |
#print " $isis_tmp <--\n"; |
# filter="name" ; filter this field through |
117 |
|
# filter/[name].pm |
118 |
|
my $filter = $x->{filter}; |
119 |
|
if ($filter) { |
120 |
|
require "filter/".$filter.".pm"; |
121 |
|
} |
122 |
|
# type="swish" ; field for swish |
123 |
|
if ($s && $swish) { |
124 |
|
if ($filter) { |
125 |
|
no strict 'refs'; |
126 |
|
$swish_data .= join(" ",&$filter($swish)); |
127 |
|
} else { |
128 |
|
$swish_data .= $swish; |
129 |
} |
} |
130 |
$prefix = ""; |
} |
131 |
} elsif ($format =~ s/^([^\d]+)//) { |
|
132 |
$prefix = $1; |
# type="display" ; field for display |
133 |
} else { |
if ($d && $display) { |
134 |
print STDERR "WARNING: unparsed format '$format'\n"; |
if ($line_delimiter && $display_data) { |
135 |
last; |
$display_data .= $line_delimiter; |
136 |
}; |
undef $line_delimiter; |
137 |
|
} |
138 |
|
if ($filter) { |
139 |
|
no strict 'refs'; |
140 |
|
$display_data .= join($delimiter,&$filter($display)); |
141 |
|
} else { |
142 |
|
if ($display_data) { |
143 |
|
$display_data .= $delimiter.$display; |
144 |
|
} else { |
145 |
|
$display_data .= $display; |
146 |
|
} |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
# type="index" ; insert into index |
151 |
|
if ($i && $display) { |
152 |
|
my $index_data = $display; |
153 |
|
if ($filter) { |
154 |
|
no strict 'refs'; |
155 |
|
foreach my $d (&$filter($index_data)) { |
156 |
|
$index->insert($field, $d, $db_dir); |
157 |
|
} |
158 |
|
} else { |
159 |
|
$index->insert($field, $index_data, $db_dir); |
160 |
|
} |
161 |
|
} |
162 |
} |
} |
163 |
# add suffix |
} |
164 |
$display_tmp .= $prefix if ($display_tmp); |
|
165 |
|
|
166 |
# $display_data .= $display_tmp if ($display_tmp ne ""); |
if ($display_data) { |
|
# $index_data .= $index_tmp if ($index_tmp ne ""); |
|
|
$display_data .= $display_tmp; |
|
|
$index_data .= $index_tmp; |
|
167 |
|
|
168 |
|
if ($field eq "headline") { |
169 |
|
$xml .= xmlify("headline", $display_data); |
170 |
|
} else { |
171 |
|
|
172 |
|
# find field name (signular, plural) |
173 |
|
my $field_name = ""; |
174 |
|
if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) { |
175 |
|
$field_name = $config->{indexer}->{$field}->{name_singular}."#-#"; |
176 |
|
} elsif ($config->{indexer}->{$field}->{name_plural}) { |
177 |
|
$field_name = $config->{indexer}->{$field}->{name_plural}."#-#"; |
178 |
|
} elsif ($config->{indexer}->{$field}->{name}) { |
179 |
|
$field_name = $config->{indexer}->{$field}->{name}."#-#"; |
180 |
|
} else { |
181 |
|
print STDERR "WARNING: field '$field' doesn't have 'name' attribute!"; |
182 |
|
} |
183 |
|
if ($field_name) { |
184 |
|
$html .= x($field_name); |
185 |
|
} |
186 |
|
$html .= $display_data."###\n"; |
187 |
|
} |
188 |
} |
} |
189 |
#print "--display:$display_data\n--index:$index_data\n"; |
if ($swish_data) { |
190 |
$xml->{$field}->{display} .= $isis_map->tou($display_data)->utf8 if ($display_data); |
# remove extra spaces |
191 |
$xml->{$field}->{index} .= unac_string($config->{isis_codepage},$index_data) if ($index_data); |
$swish_data =~ s/ +/ /g; |
192 |
|
$swish_data =~ s/ +$//g; |
193 |
|
|
194 |
|
$xml .= xmlify($field."_swish", unac_string($codepage,$swish_data)); |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
} |
} |
199 |
|
|
200 |
|
# dump formatted output in <html> |
201 |
|
if ($html) { |
202 |
|
$xml .= xmlify("html",$html); |
203 |
|
} |
204 |
|
|
205 |
if ($xml) { |
if ($xml) { |
206 |
return XMLout($xml, rootname => 'xml', noattr => 1 ); |
$xml .= $add_xml if ($add_xml); |
207 |
|
return "<xml>\n$xml</xml>\n"; |
208 |
} else { |
} else { |
209 |
return; |
return; |
210 |
} |
} |
212 |
|
|
213 |
########################################################################## |
########################################################################## |
214 |
|
|
215 |
my $last_tell=0; |
my $cfg = new Config::IniFiles( -file => $config_file ); |
216 |
|
|
217 |
my @isis_dirs = ( '.' ); # use dirname as database name |
foreach my $database ($cfg->Sections) { |
218 |
|
|
219 |
if ($opts{m}) { |
my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!"; |
220 |
@isis_dirs = split(/,/,$opts{m}); |
my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined"; |
221 |
} |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
|
|
|
|
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); |
|
222 |
|
|
223 |
my $db; |
$config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1); |
|
|
|
|
foreach my $isis_db (@isis_dbs) { |
|
224 |
|
|
225 |
|
$isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
226 |
|
|
227 |
my $db = OpenIsis::open( $isis_db ); |
my $db = OpenIsis::open( $isis_db ); |
228 |
if (0) { |
if (0) { |
236 |
|
|
237 |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
238 |
|
|
239 |
|
my $path = $database; # was $isis_db |
240 |
|
|
241 |
my $last_p = 0; |
my $last_p = 0; |
242 |
|
|
243 |
# { my $row_id = 1; |
# { my $row_id = 4514; |
244 |
# FIX |
# FIX |
245 |
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
246 |
my $row = OpenIsis::read( $db, $row_id ); |
my $row = OpenIsis::read( $db, $row_id ); |
247 |
if ($row && $row->{mfn}) { |
if ($row && $row->{mfn}) { |
|
|
|
248 |
# output current process indicator |
# output current process indicator |
249 |
my $p = int($row->{mfn} * 100 / $max_rowid); |
my $p = int($row->{mfn} * 100 / $max_rowid); |
250 |
if ($p != $last_p) { |
if ($p != $last_p) { |
252 |
$last_p = $p; |
$last_p = $p; |
253 |
} |
} |
254 |
|
|
255 |
if (my $xml = isis2xml($row)) { |
if (my $xml = $cp2utf->convert(isis2xml($row,$add_xml))) { |
256 |
print "Path-Name: $isis_db#".$row->{mfn}."\n"; |
use bytes; # as opposed to chars |
257 |
|
print "Path-Name: $path#".int($row->{mfn})."\n"; |
258 |
print "Content-Length: ".(length($xml)+1)."\n"; |
print "Content-Length: ".(length($xml)+1)."\n"; |
259 |
print "Document-Type: XML\n\n$xml\n"; |
print "Document-Type: XML\n\n$xml\n"; |
260 |
} |
} |
263 |
print STDERR "\n"; |
print STDERR "\n"; |
264 |
} |
} |
265 |
|
|
266 |
|
# call this to commit index |
267 |
|
$index->close; |
268 |
|
|
269 |
1; |
1; |
270 |
__END__ |
__END__ |