9 |
use Text::Iconv; |
use Text::Iconv; |
10 |
use Config::IniFiles; |
use Config::IniFiles; |
11 |
use Encode; |
use Encode; |
12 |
|
#use GDBM_File; |
13 |
|
use Fcntl; # for O_RDWR |
14 |
|
use TDB_File; |
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 (-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; |
65 |
'feed' => 'feed' |
'feed' => 'feed' |
66 |
); |
); |
67 |
|
|
68 |
|
my $cache; # for cacheing |
69 |
|
|
70 |
|
# lookup hash (tied to file) |
71 |
|
my %lhash; |
72 |
|
# this option will cache all lookup entries in memory. |
73 |
|
# if you are tight on memory, turn this off |
74 |
|
my $use_lhash_cache = 1; |
75 |
|
|
76 |
|
my $last_field_name; # cache to prevent repeated fields |
77 |
|
|
78 |
sub data2xml { |
sub data2xml { |
79 |
|
|
80 |
use xmlify; |
use xmlify; |
96 |
|
|
97 |
# sort subrouting using order="" attribute |
# sort subrouting using order="" attribute |
98 |
sub by_order { |
sub by_order { |
99 |
return 0 if (! $config->{indexer}->{$a}->{order}); |
my $va = $config->{indexer}->{$a}->{order} || |
100 |
return 0 if (! $config->{indexer}->{$b}->{order}); |
$config->{indexer}->{$a}; |
101 |
|
my $vb = $config->{indexer}->{$b}->{order} || |
102 |
|
$config->{indexer}->{$b}; |
103 |
|
|
104 |
|
return $va <=> $vb; |
105 |
|
} |
106 |
|
|
107 |
return $config->{indexer}->{$a}->{order} <=> |
my @sorted_tags; |
108 |
$config->{indexer}->{$b}->{order} ; |
if ($cache->{tags_by_order}) { |
109 |
|
@sorted_tags = @{$cache->{tags_by_order}}; |
110 |
|
} else { |
111 |
|
@sorted_tags = sort by_order keys %{$config->{indexer}}; |
112 |
|
$cache->{tags_by_order} = \@sorted_tags; |
113 |
} |
} |
114 |
|
|
115 |
foreach my $field (sort by_order keys %{$config->{indexer}}) { |
# lookup key |
116 |
|
my $lookup_key; |
117 |
|
|
118 |
$field=x($field); |
# cache for field in pages |
119 |
|
delete $cache->{display_data}; |
120 |
|
delete $cache->{swish_data}; |
121 |
|
delete $cache->{swish_exact_data}; |
122 |
|
delete $cache->{index_data}; |
123 |
|
delete $cache->{index_delimiter}; |
124 |
|
my @page_fields; # names of fields |
125 |
|
|
126 |
|
|
127 |
|
# subs used to produce output |
128 |
|
|
129 |
|
sub get_field_name($$$) { |
130 |
|
my ($config,$field,$field_usage) = @_; |
131 |
|
|
132 |
|
# find field name (signular, plural) |
133 |
|
my $field_name = ""; |
134 |
|
if ($config->{indexer}->{$field}->{name_singular} && $field_usage == 1) { |
135 |
|
$field_name = $config->{indexer}->{$field}->{name_singular}; |
136 |
|
} elsif ($config->{indexer}->{$field}->{name_plural}) { |
137 |
|
$field_name = $config->{indexer}->{$field}->{name_plural}; |
138 |
|
} elsif ($config->{indexer}->{$field}->{name}) { |
139 |
|
$field_name = $config->{indexer}->{$field}->{name}; |
140 |
|
} else { |
141 |
|
print STDERR "WARNING: field '$field' doesn't have 'name' attribute!"; |
142 |
|
} |
143 |
|
if ($field_name) { |
144 |
|
if (! $last_field_name) { |
145 |
|
$last_field_name = x($field_name); |
146 |
|
return $last_field_name; |
147 |
|
} elsif ($field_name ne $last_field_name) { |
148 |
|
$last_field_name = x($field_name); |
149 |
|
return $last_field_name; |
150 |
|
} |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
# init variables for different types |
156 |
|
sub init_visible_type($) { |
157 |
|
my $type = shift; |
158 |
|
|
159 |
|
# swish, swish_exact, display, index, index_lookup |
160 |
|
# swish and display defaults |
161 |
|
my ($s,$se,$d,$i,$il) = (1,0,1,0,0); |
162 |
|
if (lc($type) eq "display") { |
163 |
|
$s = 0; |
164 |
|
} elsif (lc($type) eq "swish") { |
165 |
|
$d = 0; |
166 |
|
} elsif (lc($type) eq "index") { |
167 |
|
($s,$se,$d,$i) = (0,1,0,1); |
168 |
|
} elsif (lc($type) eq "swish_exact") { |
169 |
|
($s,$se,$d,$i) = (0,1,0,0); |
170 |
|
} elsif (lc($type) =~ /^lookup/) { |
171 |
|
($s,$se,$d,$i,$il) = (0,1,0,0,1); |
172 |
|
} |
173 |
|
return ($s,$se,$d,$i,$il); |
174 |
|
} |
175 |
|
|
176 |
|
|
177 |
|
# convert |
178 |
|
# |
179 |
|
# <tag> |
180 |
|
# <delimiter>, </delimiter> |
181 |
|
# <value>200a</value> |
182 |
|
# </tag> |
183 |
|
# |
184 |
|
# to |
185 |
|
# |
186 |
|
# <tag delimiter=", ">200a</tag> |
187 |
|
# |
188 |
|
# but without loosing spaces in delimiter (becasue |
189 |
|
# new XML::Simple strips spaces in attribute values |
190 |
|
# as defined in XML specification) |
191 |
|
# |
192 |
|
sub unroll_x($) { |
193 |
|
my $x = shift; |
194 |
|
|
195 |
|
if (defined $x->{value}) { |
196 |
|
my ($v,$d) = ($x->{value}->{content}, $x->{delimiter}->{content}); |
197 |
|
delete $x->{value}; |
198 |
|
delete $x->{delimiter}; |
199 |
|
$x->{content} = $v; |
200 |
|
$x->{delimiter} = $d; |
201 |
|
} |
202 |
|
return $x; |
203 |
|
} |
204 |
|
|
205 |
|
# begin real work: go field by field |
206 |
|
foreach my $field (@sorted_tags) { |
207 |
|
|
208 |
|
$field=x($field); |
209 |
$field_usage{$field}++; |
$field_usage{$field}++; |
210 |
|
|
211 |
my $swish_data = ""; |
my $swish_data = ""; |
212 |
|
my $swish_exact_data = ""; |
213 |
my $display_data = ""; |
my $display_data = ""; |
214 |
|
my @index_data; |
215 |
my $line_delimiter; |
my $line_delimiter; |
216 |
|
|
217 |
my ($swish,$display); |
my ($swish,$display); |
218 |
|
|
219 |
my $tag = $type2tag{$type} || die "can't find which tag to use for type $type"; |
my $tag = $type2tag{$type} || die "can't find which tag to use for type $type"; |
220 |
|
|
221 |
|
# is this field page-by-page? |
222 |
|
my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page}; |
223 |
|
push @page_fields,$field if ($iterate_by_page); |
224 |
|
my %page_max = (); |
225 |
|
# default line_delimiter if using |
226 |
|
my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>'; |
227 |
|
$cache->{index_delimiter}->{$field} = $config->{indexer}->{$field}->{index_delimiter}; |
228 |
|
|
229 |
|
my $format_name = $config->{indexer}->{$field}->{format_name}; |
230 |
|
my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter}; |
231 |
|
if ($format_name && $format_delimiter) { |
232 |
|
$cache->{format}->{$field}->{format_name} = $format_name; |
233 |
|
$cache->{format}->{$field}->{format_delimiter} = $format_delimiter; |
234 |
|
} |
235 |
|
|
236 |
foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { |
237 |
|
|
238 |
|
$x = unroll_x($x); |
239 |
|
|
240 |
my $format = x($x->{content}); |
my $format = x($x->{content}); |
241 |
my $delimiter = x($x->{delimiter}) || ' '; |
my $delimiter = x($x->{delimiter}) || ' '; |
242 |
|
|
243 |
my $repeat_off = 0; # repeatable offset |
my $repeat_off = 0; # init repeatable offset |
244 |
|
|
245 |
my ($s,$d,$i) = (1,1,0); # swish, display default |
my ($s,$se,$d,$i,$il) = init_visible_type($x->{type}); |
|
$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"); |
|
246 |
|
|
247 |
# what will separate last line from this one? |
# what will separate last line from this one? |
248 |
if ($display_data && $x->{append} && $x->{append} eq "1") { |
if ($display_data && $x->{append}) { |
249 |
$line_delimiter = ' '; |
$line_delimiter = $delimiter; |
250 |
} elsif ($display_data) { |
} elsif ($display_data) { |
251 |
$line_delimiter = '<br/>'; |
$line_delimiter = '<br/>'; |
252 |
} |
} |
254 |
# init vars so that we go into while... |
# init vars so that we go into while... |
255 |
($swish,$display) = (1,1); |
($swish,$display) = (1,1); |
256 |
|
|
257 |
if ($swish || $display) { |
# placeholder for all repeatable entries for index |
258 |
|
|
259 |
|
sub mkformat($$) { |
260 |
|
my $x = shift || die "mkformat needs tag reference"; |
261 |
|
my $data = shift || return; |
262 |
|
my $format_name = x($x->{format_name}) || return $data; |
263 |
|
my $fmt = x($config->{format}->{$format_name}->{content}) || die "<format name=\"$format_name\"> is not defined!"; |
264 |
|
my $format_delimiter = x($x->{format_delimiter}); |
265 |
|
my @data; |
266 |
|
if ($format_delimiter) { |
267 |
|
@data = split(/$format_delimiter/,$data); |
268 |
|
} else { |
269 |
|
push @data,$data; |
270 |
|
} |
271 |
|
|
272 |
|
if ($fmt) { |
273 |
|
my $nr = scalar $fmt =~ s/%s/%s/g; |
274 |
|
if (($#data+1) == $nr) { |
275 |
|
return sprintf($fmt,@data); |
276 |
|
} else { |
277 |
|
#print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n"; |
278 |
|
return $data; |
279 |
|
} |
280 |
|
} else { |
281 |
|
print STDERR "usage of link '$format_name' without defined format (<link> tag)\n"; |
282 |
|
} |
283 |
|
} |
284 |
|
|
285 |
|
# while because of repeatable fields |
286 |
|
while ($swish || $display) { |
287 |
|
my $page = $repeat_off; |
288 |
|
$page_max{$field} = $page if ($iterate_by_page && $page > ($page_max{$field} || 0)); |
289 |
($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp); |
($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp); |
290 |
|
if ($repeat_off > 1000) { |
291 |
|
print STDERR "loop (more than 1000 repeatable fields) deteced in $row, $format\n"; |
292 |
|
last; |
293 |
|
} |
294 |
|
|
295 |
|
# is this field is lookup? |
296 |
|
if ($display && $x->{lookup}) { |
297 |
|
my $null = "<!-- null -->"; |
298 |
|
if ($use_lhash_cache) { |
299 |
|
if (!defined($cache->{lhash}->{$display})) { |
300 |
|
my $new_display = $lhash{$display}; |
301 |
|
if (defined($new_display)) { |
302 |
|
#print STDERR "lookup cache store '$display' = '$new_display'\n"; |
303 |
|
$display = $new_display; |
304 |
|
$cache->{lhash}->{$display} = $new_display; |
305 |
|
} else { |
306 |
|
# print STDERR "WARNING: lookup for '$display' didn't find anything.\n"; |
307 |
|
$display = ""; |
308 |
|
$cache->{lhash}->{$display} = $null; |
309 |
|
} |
310 |
|
} else { |
311 |
|
$display = $cache->{lhash}->{$display}; |
312 |
|
} |
313 |
|
} else { |
314 |
|
$display = $lhash{$display} || $null; |
315 |
|
} |
316 |
|
} |
317 |
|
|
318 |
# filter="name" ; filter this field through |
# filter="name" ; filter this field through |
319 |
# filter/[name].pm |
# filter/[name].pm |
320 |
my $filter = $x->{filter}; |
my $filter = $x->{filter}; |
321 |
if ($filter) { |
if ($filter && !$cache->{filter_loaded}->{$filter}) { |
322 |
require "filter/".$filter.".pm"; |
require "filter/".$filter.".pm"; |
323 |
|
$cache->{filter_loaded}->{$filter}++; |
324 |
} |
} |
325 |
# type="swish" ; field for swish |
# type="swish" ; field for swish |
326 |
if ($s && $swish) { |
if ($swish) { |
327 |
if ($filter) { |
my $tmp = $swish; |
328 |
|
if ($filter && ($s || $se)) { |
329 |
no strict 'refs'; |
no strict 'refs'; |
330 |
$swish_data .= join(" ",&$filter($swish)); |
$tmp = join(" ",&$filter($tmp)) if ($s || $se); |
|
} else { |
|
|
$swish_data .= $swish; |
|
331 |
} |
} |
332 |
|
|
333 |
|
$swish_data .= $tmp if ($s && $tmp); |
334 |
|
$swish_exact_data .= "xxbxx $tmp xxexx " if ($tmp && $tmp ne "" && $se); |
335 |
} |
} |
336 |
|
|
337 |
# type="display" ; field for display |
# type="display" ; field for display |
338 |
if ($d && $display) { |
if ($d && $display) { |
339 |
|
my $ldel = $delimiter; |
340 |
if ($line_delimiter && $display_data) { |
if ($line_delimiter && $display_data) { |
341 |
$display_data .= $line_delimiter; |
$ldel = $line_delimiter; |
|
undef $line_delimiter; |
|
342 |
} |
} |
343 |
if ($filter) { |
if ($filter) { |
344 |
no strict 'refs'; |
no strict 'refs'; |
345 |
$display_data .= join($delimiter,&$filter($display)); |
my @arr; |
346 |
} else { |
foreach my $tmp (&$filter($display)) { |
347 |
if ($display_data) { |
my $tmp2 = mkformat($x,$tmp); |
348 |
$display_data .= $delimiter.$display; |
push @arr,$tmp2 if ($tmp2); |
|
} else { |
|
|
$display_data .= $display; |
|
349 |
} |
} |
350 |
|
$display_data .= $ldel if ($display_data && @arr); |
351 |
|
$display_data .= join($delimiter,@arr); |
352 |
|
} else { |
353 |
|
$display_data .= $ldel if ($display_data); |
354 |
|
my $tmp = mkformat($x,$display); |
355 |
|
$display_data .= $tmp if ($tmp); |
356 |
} |
} |
357 |
} |
} |
358 |
|
|
359 |
# type="index" ; insert into index |
# type="index" ; insert into index |
360 |
|
my $idisplay; |
361 |
if ($i && $display) { |
if ($i && $display) { |
362 |
my $index_data = $display; |
$idisplay = $display; |
363 |
if ($filter) { |
if ($filter) { |
364 |
no strict 'refs'; |
no strict 'refs'; |
365 |
foreach my $d (&$filter($index_data)) { |
$idisplay = &$filter($idisplay); |
366 |
$index->insert($field, $d, $path); |
} |
367 |
|
push @index_data, $idisplay if ($idisplay && !$iterate_by_page); |
368 |
|
} |
369 |
|
|
370 |
|
# store fields in lookup |
371 |
|
if ($il && $display) { |
372 |
|
if (lc($x->{type}) eq "lookup_key") { |
373 |
|
if ($lookup_key) { |
374 |
|
print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)"; |
375 |
|
} else { |
376 |
|
if ($filter) { |
377 |
|
no strict 'refs'; |
378 |
|
$lookup_key = &$filter($display); |
379 |
|
} else { |
380 |
|
$lookup_key = $display; |
381 |
|
} |
382 |
|
} |
383 |
|
} elsif (lc($x->{type}) eq "lookup_val") { |
384 |
|
if ($lookup_key) { |
385 |
|
if ($filter) { |
386 |
|
no strict 'refs'; |
387 |
|
$lhash{$lookup_key} = &$filter($display); |
388 |
|
} else { |
389 |
|
$lhash{$lookup_key} = $display; |
390 |
|
} |
391 |
|
} else { |
392 |
|
print STDERR "WARNING: no lookup_key defined for '$display'?"; |
393 |
} |
} |
394 |
} else { |
} |
395 |
$index->insert($field, $index_data, $path); |
|
396 |
|
} |
397 |
|
|
398 |
|
# store data for page-by-page repeatable fields |
399 |
|
if ($iterate_by_page) { |
400 |
|
sub iterate_fld($$$$$$) { |
401 |
|
my ($cache,$what,$field,$page,$data,$append) = @_; |
402 |
|
return if (!$data); |
403 |
|
|
404 |
|
my $ldel = $page_line_delimiter; |
405 |
|
$ldel = " " if ($append); |
406 |
|
#print STDERR "line delimiter: ",Dumper($ldel) if ($ldel); |
407 |
|
if (! $cache->{$what}->{$field}->[$page]) { |
408 |
|
$cache->{$what}->{$field}->[$page] = $data; |
409 |
|
} else { |
410 |
|
$cache->{$what}->{$field}->[$page] .= $ldel.$data; |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
|
if ($display_data) { |
415 |
|
iterate_fld($cache,'display_data',$field,$page,$display_data,$x->{append}); |
416 |
|
} |
417 |
|
$display_data = ""; |
418 |
|
if ($swish_data) { |
419 |
|
iterate_fld($cache,'swish_data',$field,$page,$swish_data,$x->{append}); |
420 |
|
$swish_data = ""; |
421 |
|
} |
422 |
|
if ($swish_exact_data) { |
423 |
|
iterate_fld($cache,'swish_exact_data',$field,$page,$swish_exact_data,$x->{append}); |
424 |
|
$swish_exact_data = ""; |
425 |
|
} |
426 |
|
|
427 |
|
if ($idisplay) { |
428 |
|
my $ldel=$page_line_delimiter; |
429 |
|
my @index_data; |
430 |
|
if ($cache->{index_data}->{$field}->[$page]) { |
431 |
|
|
432 |
|
@index_data = @{$cache->{index_data}->{$field}->[$page]}; |
433 |
|
} |
434 |
|
if ($x->{append}) { |
435 |
|
if (@index_data) { |
436 |
|
$index_data[$#index_data] .= $idisplay; |
437 |
|
} else { |
438 |
|
push @index_data, $idisplay; |
439 |
|
} |
440 |
|
} else { |
441 |
|
push @index_data, $idisplay; |
442 |
|
} |
443 |
|
$idisplay = ""; |
444 |
|
@{$cache->{index_data}->{$field}->[$page]} = @index_data; |
445 |
} |
} |
446 |
} |
} |
447 |
} |
} |
448 |
|
|
449 |
|
if (! $iterate_by_page) { |
450 |
|
my $idel = $x->{index_delimiter}; |
451 |
|
# fill data in index |
452 |
|
foreach my $tmp (@index_data) { |
453 |
|
my $i = $d = $tmp; |
454 |
|
if ($idel && $tmp =~ m/$idel/) { |
455 |
|
($i,$d) = split(/$idel/,$tmp); |
456 |
|
} |
457 |
|
$index->insert($field, $i, $d, $path); |
458 |
|
} |
459 |
|
@index_data = (); |
460 |
|
} |
461 |
} |
} |
462 |
|
|
463 |
# now try to parse variables from configuration file |
# now try to parse variables from configuration file |
464 |
foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) { |
foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) { |
465 |
|
|
466 |
|
$x = unroll_x($x); |
467 |
|
|
468 |
my $delimiter = x($x->{delimiter}) || ' '; |
my $delimiter = x($x->{delimiter}) || ' '; |
469 |
my $val = $cfg->val($database, x($x->{content})); |
my $val = $cfg->val($database, x($x->{content})); |
470 |
|
|
471 |
my ($s,$d,$i) = (1,1,0); # swish, display default |
# FIXME index_lookup is not supported! |
472 |
$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"); |
|
|
($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index"); |
|
473 |
|
|
474 |
if ($val) { |
if ($val) { |
475 |
$display_data .= $delimiter.$val if ($d); |
$display_data .= $delimiter.$val if ($d); |
476 |
$swish_data .= $val if ($s); |
$swish_data .= " ".$val if ($s); |
477 |
$index->insert($field, $val, $path) if ($i); |
$index->insert($field, $val, $path) if ($i); |
478 |
} |
} |
479 |
|
|
480 |
|
if ($iterate_by_page) { |
481 |
|
# FIXME data from config tag will appear just |
482 |
|
# on first page!!! |
483 |
|
my $page = 0; |
484 |
|
if ($display_data) { |
485 |
|
$cache->{display_data}->{$field}->[$page] = $display_data; |
486 |
|
$display_data = ""; |
487 |
|
} |
488 |
|
if ($swish_data) { |
489 |
|
$cache->{swish_data}->{$field}->[$page] = $swish_data; |
490 |
|
$swish_data = ""; |
491 |
|
} |
492 |
|
if ($swish_exact_data) { |
493 |
|
$cache->{swish_exact_data}->{$field}->[$page] = $swish_exact_data; |
494 |
|
$swish_exact_data = ""; |
495 |
|
} |
496 |
|
} |
497 |
} |
} |
498 |
|
|
499 |
|
# save data page-by-page |
500 |
|
foreach my $field (@page_fields) { |
501 |
|
my $nr_pages = $page_max{$field} || next; |
502 |
|
#print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n"; |
503 |
|
#print STDERR Dumper($cache->{display_data}); |
504 |
|
for (my $page=0; $page <= $nr_pages; $page++) { |
505 |
|
my $display_data; |
506 |
|
if ($cache->{format}->{$field}) { |
507 |
|
my $tmp = mkformat($cache->{format}->{$field},$cache->{display_data}->{$field}->[$page]); |
508 |
|
$display_data=$tmp if ($tmp); |
509 |
|
} else { |
510 |
|
$display_data = $cache->{display_data}->{$field}->[$page]; |
511 |
|
} |
512 |
|
if ($display_data) { # default |
513 |
|
if ($field eq "headline") { |
514 |
|
$xml .= xmlify("headline", $display_data); |
515 |
|
} else { |
516 |
|
|
517 |
if ($display_data) { |
# fallback to empty field name if needed |
518 |
|
$html .= get_field_name($config,$field,$field_usage{$field}) || ''; |
519 |
|
$html .= "#-#".$display_data."###\n"; |
520 |
|
} |
521 |
|
} |
522 |
|
|
523 |
|
my $swish_data = $cache->{swish_data}->{$field}->[$page]; |
524 |
|
if ($swish_data) { |
525 |
|
# remove extra spaces |
526 |
|
$swish_data =~ s/ +/ /g; |
527 |
|
$swish_data =~ s/ +$//g; |
528 |
|
|
529 |
if ($field eq "headline") { |
$xml .= xmlify($field."_swish", unac_string($codepage,$swish_data)); |
|
$xml .= xmlify("headline", $display_data); |
|
|
} else { |
|
|
|
|
|
# find field name (signular, plural) |
|
|
my $field_name = ""; |
|
|
if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 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!"; |
|
530 |
} |
} |
531 |
if ($field_name) { |
|
532 |
$html .= x($field_name); |
my $swish_exact_data = $cache->{swish_exact_data}->{$field}->[$page]; |
533 |
|
if ($swish_exact_data) { |
534 |
|
$swish_exact_data =~ s/ +/ /g; |
535 |
|
$swish_exact_data =~ s/ +$//g; |
536 |
|
|
537 |
|
# add delimiters before and after word. |
538 |
|
# That is required to produce exact match |
539 |
|
$xml .= xmlify($field."_swish_exact", unac_string($codepage,$swish_exact_data)); |
540 |
|
} |
541 |
|
|
542 |
|
my $idel = $cache->{index_delimiter}->{$field}; |
543 |
|
foreach my $tmp (@{$cache->{index_data}->{$field}->[$page]}) { |
544 |
|
my $i = $tmp; |
545 |
|
my $d = $tmp; |
546 |
|
if ($idel && $tmp =~ m/$idel/) { |
547 |
|
($i,$d) = split(/$idel/,$tmp); |
548 |
|
} |
549 |
|
$index->insert($field, $i, $d, $path); |
550 |
|
#print STDERR "index [$idel] $field: $i --> $d [$path]\n"; |
551 |
} |
} |
|
$html .= $display_data."###\n"; |
|
552 |
} |
} |
553 |
|
|
554 |
} |
} |
555 |
if ($swish_data) { |
|
556 |
# remove extra spaces |
if (! $iterate_by_page) { |
557 |
$swish_data =~ s/ +/ /g; |
if ($display_data) { |
558 |
$swish_data =~ s/ +$//g; |
if ($field eq "headline") { |
559 |
|
$xml .= xmlify("headline", $display_data); |
560 |
|
} else { |
561 |
|
|
562 |
$xml .= xmlify($field."_swish", unac_string($codepage,$swish_data)); |
# fallback to empty field name if needed |
563 |
} |
$html .= get_field_name($config,$field,$field_usage{$field}) || ''; |
564 |
|
$html .= "#-#".$display_data."###\n"; |
565 |
|
} |
566 |
|
} |
567 |
|
if ($swish_data) { |
568 |
|
# remove extra spaces |
569 |
|
$swish_data =~ s/ +/ /g; |
570 |
|
$swish_data =~ s/ +$//g; |
571 |
|
|
572 |
|
$xml .= xmlify($field."_swish", unac_string($codepage,$swish_data)); |
573 |
|
} |
574 |
|
|
575 |
|
if ($swish_exact_data) { |
576 |
|
$swish_exact_data =~ s/ +/ /g; |
577 |
|
$swish_exact_data =~ s/ +$//g; |
578 |
|
|
579 |
|
# add delimiters before and after word. |
580 |
|
# That is required to produce exact match |
581 |
|
$xml .= xmlify($field."_swish_exact", unac_string($codepage,$swish_exact_data)); |
582 |
|
} |
583 |
|
} |
584 |
} |
} |
585 |
|
|
586 |
# dump formatted output in <html> |
# dump formatted output in <html> |
613 |
$cfg_global->val('global', 'dbi_passwd') || '', |
$cfg_global->val('global', 'dbi_passwd') || '', |
614 |
); |
); |
615 |
|
|
616 |
|
my $show_progress = $cfg_global->val('global', 'show_progress'); |
617 |
|
|
618 |
|
my $unac_filter = $cfg_global->val('global', 'unac_filter'); |
619 |
|
if ($unac_filter) { |
620 |
|
require $unac_filter; |
621 |
|
} |
622 |
|
|
623 |
foreach my $database ($cfg->Sections) { |
foreach my $database ($cfg->Sections) { |
624 |
|
|
625 |
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"; |
626 |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
627 |
|
|
628 |
|
# create new lookup file |
629 |
|
my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional |
630 |
|
if ($lookup_file) { |
631 |
|
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644; |
632 |
|
tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644; |
633 |
|
print STDERR "creating lookup file '$lookup_file'\n"; |
634 |
|
# delete memory cache for lookup file |
635 |
|
delete $cache->{lhash}; |
636 |
|
} |
637 |
|
|
638 |
|
# open existing lookup file |
639 |
|
$lookup_file = $cfg -> val($database, 'lookup_open'); # optional |
640 |
|
if ($lookup_file) { |
641 |
|
#tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644; |
642 |
|
tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644; |
643 |
|
print STDERR "opening lookup file '$lookup_file'\n"; |
644 |
|
} |
645 |
|
|
646 |
print STDERR "reading ./import_xml/$type.xml\n"; |
print STDERR "reading ./import_xml/$type.xml\n"; |
647 |
|
|
648 |
# extract just type basic |
# extract just type basic |
649 |
my $type_base = $type; |
my $type_base = $type; |
650 |
$type_base =~ s/_.+$//g; |
$type_base =~ s/_.+$//g; |
651 |
|
|
652 |
$config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type_base}, 'config' ], forcecontent => 1); |
$config=XMLin("./import_xml/$type.xml", ForceArray => [ $type2tag{$type_base}, 'config', 'format' ], ForceContent => 1 ); |
653 |
|
|
654 |
# output current progress indicator |
# output current progress indicator |
655 |
my $last_p = 0; |
my $last_p = 0; |
656 |
sub progress { |
sub progress { |
657 |
#return if (! $opts{q}); # FIXME |
return if (! $show_progress); |
658 |
my $current = shift; |
my $current = shift; |
659 |
my $total = shift || 1; |
my $total = shift || 1; |
660 |
my $p = int($current * 100 / $total); |
my $p = int($current * 100 / $total); |
666 |
|
|
667 |
my $fake_dir = 1; |
my $fake_dir = 1; |
668 |
sub fakeprogress { |
sub fakeprogress { |
669 |
|
return if (! $show_progress); |
670 |
my $current = shift @_; |
my $current = shift @_; |
671 |
|
|
672 |
my @ind = ('-','\\','|','/','-','\\','|','/', '-'); |
my @ind = ('-','\\','|','/','-','\\','|','/', '-'); |
681 |
# now read database |
# now read database |
682 |
print STDERR "using: $type...\n"; |
print STDERR "using: $type...\n"; |
683 |
|
|
684 |
|
# erase cache for tags by order in this database |
685 |
|
delete $cache->{tags_by_order}; |
686 |
|
|
687 |
if ($type_base eq "isis") { |
if ($type_base eq "isis") { |
688 |
|
|
689 |
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!"; |
691 |
$import2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
692 |
my $db = OpenIsis::open( $isis_db ); |
my $db = OpenIsis::open( $isis_db ); |
693 |
|
|
694 |
|
# check if .txt database for OpenIsis is zero length, |
695 |
|
# if so, erase it and re-open database |
696 |
|
sub check_txt_db { |
697 |
|
my $isis_db = shift || die "need isis database name"; |
698 |
|
my $reopen = 0; |
699 |
|
|
700 |
|
if (-e $isis_db.".TXT") { |
701 |
|
print STDERR "WARNING: removing $isis_db.TXT OpenIsis database...\n"; |
702 |
|
unlink $isis_db.".TXT" || warn "FATAL: unlink error on '$isis_db.TXT': $!"; |
703 |
|
$reopen++; |
704 |
|
} |
705 |
|
if (-e $isis_db.".PTR") { |
706 |
|
print STDERR "WARNING: removing $isis_db.PTR OpenIsis database...\n"; |
707 |
|
unlink $isis_db.".PTR" || warn "FATAL: unlink error on '$isis_db.PTR': $!"; |
708 |
|
$reopen++; |
709 |
|
} |
710 |
|
return OpenIsis::open( $isis_db ) if ($reopen); |
711 |
|
} |
712 |
|
|
713 |
|
# EOF error |
714 |
|
if ($db == -1) { |
715 |
|
$db = check_txt_db($isis_db); |
716 |
|
if ($db == -1) { |
717 |
|
print STDERR "FATAL: OpenIsis can't open zero size file $isis_db\n"; |
718 |
|
next; |
719 |
|
} |
720 |
|
} |
721 |
|
|
722 |
|
# OpenIsis::ERR_BADF |
723 |
|
if ($db == -4) { |
724 |
|
print STDERR "FATAL: OpenIsis can't find file $isis_db\n"; |
725 |
|
next; |
726 |
|
# OpenIsis::ERR_IO |
727 |
|
} elsif ($db == -5) { |
728 |
|
print STDERR "FATAL: OpenIsis can't access file $isis_db\n"; |
729 |
|
next; |
730 |
|
} elsif ($db < 0) { |
731 |
|
print STDERR "FATAL: OpenIsis unknown error $db with file $isis_db\n"; |
732 |
|
next; |
733 |
|
} |
734 |
|
|
735 |
my $max_rowid = OpenIsis::maxRowid( $db ); |
my $max_rowid = OpenIsis::maxRowid( $db ); |
736 |
|
|
737 |
|
# if 0 records, try to rease isis .txt database |
738 |
|
if ($max_rowid == 0) { |
739 |
|
# force removal of database |
740 |
|
$db = check_txt_db($isis_db); |
741 |
|
$max_rowid = OpenIsis::maxRowid( $db ); |
742 |
|
} |
743 |
|
|
744 |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
745 |
|
|
746 |
my $path = $database; |
my $path = $database; |
762 |
} |
} |
763 |
} |
} |
764 |
} |
} |
765 |
|
# for this to work with current version of OpenIsis (0.9.0) |
766 |
|
# you might need my patch from |
767 |
|
# http://www.rot13.org/~dpavlin/projects/openisis-0.9.0-perl_close.diff |
768 |
|
OpenIsis::close($db); |
769 |
print STDERR "\n"; |
print STDERR "\n"; |
770 |
|
|
771 |
} elsif ($type_base eq "excel") { |
} elsif ($type_base eq "excel") { |
772 |
use Spreadsheet::ParseExcel; |
require Spreadsheet::ParseExcel; |
773 |
use Spreadsheet::ParseExcel::Utility qw(int2col); |
require Spreadsheet::ParseExcel::Utility; |
774 |
|
import Spreadsheet::ParseExcel::Utility qw(int2col); |
775 |
|
|
776 |
$import2cp = Text::Iconv->new($config->{excel_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{excel_codepage},$codepage); |
777 |
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!"; |
825 |
} |
} |
826 |
} elsif ($type_base eq "marc") { |
} elsif ($type_base eq "marc") { |
827 |
|
|
828 |
use MARC; |
require MARC; |
829 |
|
|
830 |
$import2cp = Text::Iconv->new($config->{marc_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{marc_codepage},$codepage); |
831 |
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!"; |
832 |
|
|
833 |
# optional argument is format |
# optional argument is format |
834 |
my $format = x($config->{format}) || 'usmarc'; |
my $format = x($config->{marc_format}) || 'usmarc'; |
835 |
|
|
836 |
print STDERR "Reading MARC file '$marc_file'\n"; |
print STDERR "Reading MARC file '$marc_file'\n"; |
837 |
|
|
838 |
my $marc = new MARC; |
my $marc = new MARC; |
839 |
my $nr = $marc->openmarc({ |
my $nr = $marc->openmarc({ |
840 |
file=>$marc_file, format=>$format |
file=>$marc_file, format=>$format |
841 |
}) || die "Can't open MARC file '$marc_file'"; |
}) || die "Can't open MARC file '$marc_file' with format '$format'"; |
842 |
|
|
843 |
my $i=0; # record nr. |
# read MARC file in memory |
844 |
|
$marc->nextmarc(-1); |
845 |
|
|
846 |
my $rec; |
my $max_rec = $marc->marc_count(); |
847 |
|
|
848 |
while ($marc->nextmarc(1)) { |
for(my $i=1; $i<=$max_rec; $i++) { |
849 |
|
|
850 |
# XXX |
progress($i,$max_rec); |
851 |
fakeprogress($i++); |
|
852 |
|
# store value for marc_sf.pm |
853 |
|
$main::cache->{marc_record} = $i; |
854 |
|
|
855 |
my $swishpath = $database."#".$i; |
my $swishpath = $database."#".$i; |
856 |
|
|
862 |
print "Document-Type: XML\n\n$xml\n"; |
print "Document-Type: XML\n\n$xml\n"; |
863 |
} |
} |
864 |
} |
} |
865 |
|
|
866 |
|
print STDERR "\n"; |
867 |
|
|
868 |
} elsif ($type_base eq "feed") { |
} elsif ($type_base eq "feed") { |
869 |
|
|
870 |
$import2cp = Text::Iconv->new($config->{feed_codepage},$codepage); |
$import2cp = Text::Iconv->new($config->{feed_codepage},$codepage); |
903 |
fakeprogress($i); |
fakeprogress($i); |
904 |
|
|
905 |
} |
} |
906 |
|
# close lookup |
907 |
|
untie %lhash if (%lhash); |
908 |
} |
} |
909 |
} |
} |
910 |
|
|
919 |
|
|
920 |
all2xml.pl - read various file formats and dump XML for SWISH-E |
all2xml.pl - read various file formats and dump XML for SWISH-E |
921 |
|
|
922 |
|
=head1 SYNOPSYS |
923 |
|
|
924 |
|
$ all2xml.pl [test.conf] |
925 |
|
|
926 |
=head1 DESCRIPTION |
=head1 DESCRIPTION |
927 |
|
|
928 |
This command will read ISIS data file using OpenIsis perl module, MARC |
This command will read ISIS data file using OpenIsis perl module, MARC |
931 |
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 |
932 |
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. |
933 |
|
|
934 |
|
If no configuration file is specified, it will use default one called |
935 |
|
C<all2xml.conf>. |
936 |
|
|
937 |
=head1 BUGS |
=head1 BUGS |
938 |
|
|
939 |
Documentation is really lacking. However, in true Open Source spirit, source |
Documentation is really lacking. However, in true Open Source spirit, source |