/[webpac]/branches/ffzg/all2xml.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /branches/ffzg/all2xml.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 731 - (hide annotations)
Thu Apr 13 19:47:32 2006 UTC (13 years, 5 months ago) by dpavlin
File MIME type: text/plain
File size: 29857 byte(s)
updated branches to trunk

1 dpavlin 1 #!/usr/bin/perl -w
2    
3     use strict;
4 dpavlin 633 use Biblio::Isis;
5 dpavlin 1 use Getopt::Std;
6     use Data::Dumper;
7     use XML::Simple;
8 dpavlin 10 use Text::Iconv;
9 dpavlin 13 use Config::IniFiles;
10 dpavlin 40 use Encode;
11 dpavlin 177 #use GDBM_File;
12     use Fcntl; # for O_RDWR
13     use TDB_File;
14 dpavlin 684 use Carp;
15 dpavlin 1
16 dpavlin 10 $|=1;
17 dpavlin 9
18 dpavlin 13 my $config_file = $0;
19     $config_file =~ s/\.pl$/.conf/;
20 dpavlin 321 $config_file = $ARGV[0] if ($ARGV[0] && -f $ARGV[0]);
21 dpavlin 13 die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
22    
23 dpavlin 10 my $config;
24    
25 dpavlin 58 #use index_DBI; # default DBI module for index
26 dpavlin 652 #use index_DBI_cache; # faster DBI module using memory cache
27     use index_DBI_filter; # filter support for indexes
28 dpavlin 50 my $index;
29 dpavlin 10
30 dpavlin 1 my %opts;
31    
32 dpavlin 7 # usage:
33     # -d directory name
34     # -m multiple directories
35     # -q quiet
36     # -s run swish
37 dpavlin 1
38 dpavlin 7 getopts('d:m:qs', \%opts);
39    
40 dpavlin 43 my $path; # this is name of database
41 dpavlin 1
42 dpavlin 57 Text::Iconv->raise_error(0); # Conversion errors don't raise exceptions
43 dpavlin 1
44 dpavlin 40 # 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';
47 dpavlin 1
48 dpavlin 40 my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
49     # this function will convert data from XML files to local encoding
50     sub x {
51     return $utf2cp->convert($_[0]);
52     }
53 dpavlin 3
54 dpavlin 54 # decode isis/excel or other import codepage
55     my $import2cp;
56 dpavlin 10
57 dpavlin 40 # outgoing xml must be in UTF-8
58     my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
59 dpavlin 29
60 dpavlin 54 # mapping between data type and tag which specify
61     # format in XML file
62     my %type2tag = (
63     'isis' => 'isis',
64 dpavlin 62 'excel' => 'column',
65     'marc' => 'marc',
66 dpavlin 684 'feed' => 'feed',
67 dpavlin 54 );
68 dpavlin 3
69 dpavlin 170 my $cache; # for cacheing
70    
71 dpavlin 177 # lookup hash (tied to file)
72     my %lhash;
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;
76    
77 dpavlin 195 my $last_field_name; # cache to prevent repeated fields
78    
79 dpavlin 54 sub data2xml {
80    
81 dpavlin 10 use xmlify;
82    
83 dpavlin 54 my $type = shift @_;
84 dpavlin 3 my $row = shift @_;
85 dpavlin 13 my $add_xml = shift @_;
86 dpavlin 59 # needed to read values from configuration file
87     my $cfg = shift @_;
88     my $database = shift @_;
89 dpavlin 3
90     my $xml;
91    
92 dpavlin 10 use parse_format;
93 dpavlin 3
94 dpavlin 13 my $html = ""; # html formatted display output
95 dpavlin 10
96 dpavlin 13 my %field_usage; # counter for usage of each field
97    
98 dpavlin 32 # sort subrouting using order="" attribute
99     sub by_order {
100 dpavlin 98 my $va = $config->{indexer}->{$a}->{order} ||
101     $config->{indexer}->{$a};
102     my $vb = $config->{indexer}->{$b}->{order} ||
103     $config->{indexer}->{$b};
104 dpavlin 29
105 dpavlin 98 return $va <=> $vb;
106 dpavlin 32 }
107 dpavlin 3
108 dpavlin 170 my @sorted_tags;
109 dpavlin 180 if ($cache->{tags_by_order}) {
110     @sorted_tags = @{$cache->{tags_by_order}};
111 dpavlin 170 } else {
112     @sorted_tags = sort by_order keys %{$config->{indexer}};
113 dpavlin 180 $cache->{tags_by_order} = \@sorted_tags;
114 dpavlin 170 }
115 dpavlin 32
116 dpavlin 684 if (! @sorted_tags) {
117     print STDERR "WARNING: no tags for this type found in import_xml file!\n";
118     }
119    
120 dpavlin 177 # lookup key
121     my $lookup_key;
122    
123 dpavlin 178 # cache for field in pages
124 dpavlin 180 delete $cache->{display_data};
125     delete $cache->{swish_data};
126     delete $cache->{swish_exact_data};
127 dpavlin 182 delete $cache->{index_data};
128 dpavlin 188 delete $cache->{index_delimiter};
129 dpavlin 731 delete $cache->{distinct};
130 dpavlin 178 my @page_fields; # names of fields
131    
132    
133     # subs used to produce output
134    
135     sub get_field_name($$$) {
136     my ($config,$field,$field_usage) = @_;
137    
138     # find field name (signular, plural)
139     my $field_name = "";
140     if ($config->{indexer}->{$field}->{name_singular} && $field_usage == 1) {
141     $field_name = $config->{indexer}->{$field}->{name_singular};
142     } elsif ($config->{indexer}->{$field}->{name_plural}) {
143     $field_name = $config->{indexer}->{$field}->{name_plural};
144     } elsif ($config->{indexer}->{$field}->{name}) {
145     $field_name = $config->{indexer}->{$field}->{name};
146     } else {
147     print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
148     }
149 dpavlin 601
150 dpavlin 178 if ($field_name) {
151 dpavlin 601 $field_name = x($field_name);
152 dpavlin 195 if (! $last_field_name) {
153 dpavlin 601 $last_field_name = $field_name;
154 dpavlin 195 return $last_field_name;
155     } elsif ($field_name ne $last_field_name) {
156 dpavlin 601 $last_field_name = $field_name;
157 dpavlin 195 return $last_field_name;
158     }
159 dpavlin 178 }
160     }
161    
162 dpavlin 279
163     # init variables for different types
164 dpavlin 274 sub init_visible_type($) {
165     my $type = shift;
166 dpavlin 178
167 dpavlin 274 # swish, swish_exact, display, index, index_lookup
168     # swish and display defaults
169     my ($s,$se,$d,$i,$il) = (1,0,1,0,0);
170     if (lc($type) eq "display") {
171     $s = 0;
172     } elsif (lc($type) eq "swish") {
173     $d = 0;
174     } elsif (lc($type) eq "index") {
175     ($s,$se,$d,$i) = (0,1,0,1);
176     } elsif (lc($type) eq "swish_exact") {
177     ($s,$se,$d,$i) = (0,1,0,0);
178     } elsif (lc($type) =~ /^lookup/) {
179     ($s,$se,$d,$i,$il) = (0,1,0,0,1);
180 dpavlin 652 } elsif ($type) {
181     print STDERR "WARNING: unknown type: $type\n";
182 dpavlin 274 }
183     return ($s,$se,$d,$i,$il);
184     }
185    
186 dpavlin 279
187     # convert
188     #
189     # <tag>
190     # <delimiter>, </delimiter>
191     # <value>200a</value>
192     # </tag>
193     #
194     # to
195     #
196     # <tag delimiter=", ">200a</tag>
197     #
198     # but without loosing spaces in delimiter (becasue
199     # new XML::Simple strips spaces in attribute values
200     # as defined in XML specification)
201     #
202     sub unroll_x($) {
203     my $x = shift;
204    
205     if (defined $x->{value}) {
206     my ($v,$d) = ($x->{value}->{content}, $x->{delimiter}->{content});
207     delete $x->{value};
208     delete $x->{delimiter};
209     $x->{content} = $v;
210     $x->{delimiter} = $d;
211     }
212     return $x;
213     }
214    
215 dpavlin 178 # begin real work: go field by field
216 dpavlin 170 foreach my $field (@sorted_tags) {
217    
218 dpavlin 40 $field=x($field);
219 dpavlin 13 $field_usage{$field}++;
220    
221 dpavlin 10 my $swish_data = "";
222 dpavlin 163 my $swish_exact_data = "";
223 dpavlin 3 my $display_data = "";
224 dpavlin 182 my @index_data;
225 dpavlin 35 my $line_delimiter;
226 dpavlin 3
227 dpavlin 34 my ($swish,$display);
228    
229 dpavlin 684 my $tag = $cfg->val($database, 'import_xml_tag') || $type2tag{$type} || die "can't find which tag to use for type $type";
230 dpavlin 178
231     # is this field page-by-page?
232     my $iterate_by_page = $config->{indexer}->{$field}->{iterate_by_page};
233     push @page_fields,$field if ($iterate_by_page);
234     my %page_max = ();
235     # default line_delimiter if using
236     my $page_line_delimiter = $config->{indexer}->{$field}->{page_line_delimiter} || '<br/>';
237 dpavlin 188 $cache->{index_delimiter}->{$field} = $config->{indexer}->{$field}->{index_delimiter};
238 dpavlin 731 my $distinct = $config->{indexer}->{$field}->{distinct};
239     if ($distinct && !$iterate_by_page) {
240     warn "WARNING: distinct is currently not supported without iterate_by_page!\n";
241     $distinct = 0;
242     }
243 dpavlin 178
244 dpavlin 195 my $format_name = $config->{indexer}->{$field}->{format_name};
245     my $format_delimiter = $config->{indexer}->{$field}->{format_delimiter};
246     if ($format_name && $format_delimiter) {
247     $cache->{format}->{$field}->{format_name} = $format_name;
248     $cache->{format}->{$field}->{format_delimiter} = $format_delimiter;
249     }
250    
251 dpavlin 54 foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
252 dpavlin 3
253 dpavlin 279 $x = unroll_x($x);
254    
255 dpavlin 40 my $format = x($x->{content});
256     my $delimiter = x($x->{delimiter}) || ' ';
257 dpavlin 3
258 dpavlin 178 my $repeat_off = 0; # init repeatable offset
259 dpavlin 29
260 dpavlin 274 my ($s,$se,$d,$i,$il) = init_visible_type($x->{type});
261 dpavlin 29
262 dpavlin 35 # what will separate last line from this one?
263 dpavlin 182 if ($display_data && $x->{append}) {
264 dpavlin 291 $line_delimiter = $delimiter;
265 dpavlin 35 } elsif ($display_data) {
266 dpavlin 34 $line_delimiter = '<br/>';
267     }
268 dpavlin 29
269 dpavlin 34 # init vars so that we go into while...
270     ($swish,$display) = (1,1);
271 dpavlin 29
272 dpavlin 195 sub mkformat($$) {
273 dpavlin 153 my $x = shift || die "mkformat needs tag reference";
274     my $data = shift || return;
275 dpavlin 263 my $format_name = x($x->{format_name}) || return $data;
276 dpavlin 153 my $fmt = x($config->{format}->{$format_name}->{content}) || die "<format name=\"$format_name\"> is not defined!";
277     my $format_delimiter = x($x->{format_delimiter});
278     my @data;
279     if ($format_delimiter) {
280     @data = split(/$format_delimiter/,$data);
281     } else {
282     push @data,$data;
283     }
284    
285     if ($fmt) {
286     my $nr = scalar $fmt =~ s/%s/%s/g;
287     if (($#data+1) == $nr) {
288 dpavlin 263 return sprintf($fmt,@data);
289 dpavlin 153 } else {
290 dpavlin 207 #print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n";
291 dpavlin 263 return $data;
292 dpavlin 153 }
293     } else {
294     print STDERR "usage of link '$format_name' without defined format (<link> tag)\n";
295     }
296     }
297    
298 dpavlin 90 # while because of repeatable fields
299     while ($swish || $display) {
300 dpavlin 178 my $page = $repeat_off;
301     $page_max{$field} = $page if ($iterate_by_page && $page > ($page_max{$field} || 0));
302 dpavlin 54 ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
303 dpavlin 90 if ($repeat_off > 1000) {
304     print STDERR "loop (more than 1000 repeatable fields) deteced in $row, $format\n";
305     last;
306     }
307 dpavlin 177
308     # is this field is lookup?
309     if ($display && $x->{lookup}) {
310 dpavlin 178 my $null = "<!-- null -->";
311 dpavlin 177 if ($use_lhash_cache) {
312     if (!defined($cache->{lhash}->{$display})) {
313     my $new_display = $lhash{$display};
314 dpavlin 178 if (defined($new_display)) {
315 dpavlin 177 #print STDERR "lookup cache store '$display' = '$new_display'\n";
316     $display = $new_display;
317     $cache->{lhash}->{$display} = $new_display;
318     } else {
319 dpavlin 255 # print STDERR "WARNING: lookup for '$display' didn't find anything.\n";
320 dpavlin 177 $display = "";
321 dpavlin 178 $cache->{lhash}->{$display} = $null;
322 dpavlin 177 }
323     } else {
324     $display = $cache->{lhash}->{$display};
325     }
326     } else {
327 dpavlin 178 $display = $lhash{$display} || $null;
328 dpavlin 177 }
329     }
330    
331 dpavlin 29 # filter="name" ; filter this field through
332     # filter/[name].pm
333     my $filter = $x->{filter};
334 dpavlin 170 if ($filter && !$cache->{filter_loaded}->{$filter}) {
335 dpavlin 29 require "filter/".$filter.".pm";
336 dpavlin 170 $cache->{filter_loaded}->{$filter}++;
337 dpavlin 20 }
338 dpavlin 29 # type="swish" ; field for swish
339 dpavlin 163 if ($swish) {
340 dpavlin 259 my $tmp = $swish;
341 dpavlin 163 if ($filter && ($s || $se)) {
342 dpavlin 29 no strict 'refs';
343 dpavlin 259 $tmp = join(" ",&$filter($tmp)) if ($s || $se);
344 dpavlin 29 }
345 dpavlin 234
346 dpavlin 255 $swish_data .= $tmp if ($s && $tmp);
347 dpavlin 256 $swish_exact_data .= "xxbxx $tmp xxexx " if ($tmp && $tmp ne "" && $se);
348 dpavlin 29 }
349 dpavlin 17
350 dpavlin 29 # type="display" ; field for display
351     if ($d && $display) {
352 dpavlin 207 my $ldel = $delimiter;
353 dpavlin 35 if ($line_delimiter && $display_data) {
354 dpavlin 207 $ldel = $line_delimiter;
355 dpavlin 35 }
356 dpavlin 29 if ($filter) {
357     no strict 'refs';
358 dpavlin 207 my @arr;
359     foreach my $tmp (&$filter($display)) {
360     my $tmp2 = mkformat($x,$tmp);
361     push @arr,$tmp2 if ($tmp2);
362 dpavlin 138 }
363 dpavlin 207 $display_data .= $ldel if ($display_data && @arr);
364     $display_data .= join($delimiter,@arr);
365 dpavlin 29 } else {
366 dpavlin 207 $display_data .= $ldel if ($display_data);
367     my $tmp = mkformat($x,$display);
368 dpavlin 224 $display_data .= $tmp if ($tmp);
369 dpavlin 29 }
370 dpavlin 20 }
371 dpavlin 29
372     # type="index" ; insert into index
373 dpavlin 182 my $idisplay;
374 dpavlin 29 if ($i && $display) {
375 dpavlin 182 $idisplay = $display;
376 dpavlin 177 if ($filter) {
377     no strict 'refs';
378 dpavlin 182 $idisplay = &$filter($idisplay);
379 dpavlin 177 }
380 dpavlin 255 push @index_data, $idisplay if ($idisplay && !$iterate_by_page);
381 dpavlin 97 }
382    
383 dpavlin 177 # store fields in lookup
384     if ($il && $display) {
385     if (lc($x->{type}) eq "lookup_key") {
386     if ($lookup_key) {
387     print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)";
388     } else {
389 dpavlin 233 if ($filter) {
390     no strict 'refs';
391     $lookup_key = &$filter($display);
392     } else {
393     $lookup_key = $display;
394     }
395 dpavlin 177 }
396     } elsif (lc($x->{type}) eq "lookup_val") {
397     if ($lookup_key) {
398 dpavlin 233 if ($filter) {
399     no strict 'refs';
400     $lhash{$lookup_key} = &$filter($display);
401     } else {
402     $lhash{$lookup_key} = $display;
403     }
404 dpavlin 177 } else {
405     print STDERR "WARNING: no lookup_key defined for '$display'?";
406     }
407 dpavlin 20 }
408 dpavlin 178
409 dpavlin 20 }
410 dpavlin 178
411     # store data for page-by-page repeatable fields
412     if ($iterate_by_page) {
413     sub iterate_fld($$$$$$) {
414     my ($cache,$what,$field,$page,$data,$append) = @_;
415     return if (!$data);
416 dpavlin 182
417     my $ldel = $page_line_delimiter;
418     $ldel = " " if ($append);
419     #print STDERR "line delimiter: ",Dumper($ldel) if ($ldel);
420 dpavlin 178 if (! $cache->{$what}->{$field}->[$page]) {
421 dpavlin 731 push @{$cache->{$what}->{$field}->[$page]}, {
422     data => $data,
423     delimiter => $ldel,
424     };
425 dpavlin 178 }
426     }
427    
428     if ($display_data) {
429     iterate_fld($cache,'display_data',$field,$page,$display_data,$x->{append});
430     }
431     $display_data = "";
432     if ($swish_data) {
433     iterate_fld($cache,'swish_data',$field,$page,$swish_data,$x->{append});
434     $swish_data = "";
435     }
436     if ($swish_exact_data) {
437     iterate_fld($cache,'swish_exact_data',$field,$page,$swish_exact_data,$x->{append});
438     $swish_exact_data = "";
439     }
440 dpavlin 182
441     if ($idisplay) {
442     my $ldel=$page_line_delimiter;
443     my @index_data;
444     if ($cache->{index_data}->{$field}->[$page]) {
445    
446     @index_data = @{$cache->{index_data}->{$field}->[$page]};
447     }
448     if ($x->{append}) {
449 dpavlin 195 if (@index_data) {
450     $index_data[$#index_data] .= $idisplay;
451     } else {
452     push @index_data, $idisplay;
453     }
454 dpavlin 182 } else {
455     push @index_data, $idisplay;
456     }
457     $idisplay = "";
458     @{$cache->{index_data}->{$field}->[$page]} = @index_data;
459     }
460 dpavlin 178 }
461 dpavlin 17 }
462 dpavlin 177
463 dpavlin 182 if (! $iterate_by_page) {
464 dpavlin 188 my $idel = $x->{index_delimiter};
465 dpavlin 182 # fill data in index
466 dpavlin 188 foreach my $tmp (@index_data) {
467     my $i = $d = $tmp;
468     if ($idel && $tmp =~ m/$idel/) {
469     ($i,$d) = split(/$idel/,$tmp);
470     }
471     $index->insert($field, $i, $d, $path);
472 dpavlin 182 }
473     @index_data = ();
474 dpavlin 177 }
475 dpavlin 3 }
476 dpavlin 9
477 dpavlin 59 # now try to parse variables from configuration file
478     foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) {
479 dpavlin 13
480 dpavlin 279 $x = unroll_x($x);
481    
482 dpavlin 62 my $delimiter = x($x->{delimiter}) || ' ';
483 dpavlin 59 my $val = $cfg->val($database, x($x->{content}));
484    
485 dpavlin 274 # FIXME index_lookup is not supported!
486     my ($s,$se,$d,$i,$il) = init_visible_type($x->{type});
487 dpavlin 59
488     if ($val) {
489 dpavlin 62 $display_data .= $delimiter.$val if ($d);
490 dpavlin 274 $swish_data .= " ".$val if ($s);
491 dpavlin 59 $index->insert($field, $val, $path) if ($i);
492     }
493    
494 dpavlin 178 if ($iterate_by_page) {
495     # FIXME data from config tag will appear just
496     # on first page!!!
497     my $page = 0;
498     if ($display_data) {
499 dpavlin 731 push @{$cache->{display_data}->{$field}->[$page]}, { data => $display_data };
500 dpavlin 178 $display_data = "";
501     }
502     if ($swish_data) {
503 dpavlin 731 push @{$cache->{swish_data}->{$field}->[$page]}, { data => $swish_data };
504 dpavlin 178 $swish_data = "";
505     }
506     if ($swish_exact_data) {
507 dpavlin 731 push @{$cache->{swish_exact_data}->{$field}->[$page]}, { data => $swish_exact_data };
508 dpavlin 178 $swish_exact_data = "";
509     }
510     }
511 dpavlin 59 }
512    
513 dpavlin 178 # save data page-by-page
514     foreach my $field (@page_fields) {
515     my $nr_pages = $page_max{$field} || next;
516     #print STDERR "field '$field' iterate over ",($nr_pages || 0)," pages...\n";
517     #print STDERR Dumper($cache->{display_data});
518 dpavlin 731 my $seen; # used for distinct
519 dpavlin 178 for (my $page=0; $page <= $nr_pages; $page++) {
520 dpavlin 195 my $display_data;
521 dpavlin 731 my $delimiter = '';
522     foreach my $element (@{ $cache->{display_data}->{$field}->[$page] }) {
523     my $data = $element->{data};
524     die "BUG! no data in element?" unless ($data);
525    
526     if ($distinct) {
527     next if ($cache->{distinct}->{$field}->{ $data });
528     $cache->{distinct}->{$field}->{ $data } = 1;
529     }
530    
531     if ($cache->{format}->{$field}) {
532     my $tmp = mkformat($cache->{format}->{$field},$data);
533     $display_data .= $delimiter . $tmp if ($tmp);
534     } else {
535     $display_data .= $delimiter . $data;
536     }
537     $delimiter = $element->{delimiter} if ($element->{delimiter});
538 dpavlin 195 }
539 dpavlin 731
540 dpavlin 178 if ($display_data) { # default
541     if ($field eq "headline") {
542     $xml .= xmlify("headline", $display_data);
543     } else {
544 dpavlin 29
545 dpavlin 178 # fallback to empty field name if needed
546     $html .= get_field_name($config,$field,$field_usage{$field}) || '';
547     $html .= "#-#".$display_data."###\n";
548     }
549     }
550    
551 dpavlin 731 my $swish_data = join(" ",map { $_->{data} } @{ $cache->{swish_data}->{$field}->[$page] });
552 dpavlin 178 if ($swish_data) {
553     # remove extra spaces
554     $swish_data =~ s/ +/ /g;
555     $swish_data =~ s/ +$//g;
556 dpavlin 13
557 dpavlin 321 $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
558 dpavlin 13 }
559 dpavlin 178
560 dpavlin 731 my $swish_exact_data = join(" ", map { $_->{data} } @{ $cache->{swish_exact_data}->{$field}->[$page] });
561 dpavlin 178 if ($swish_exact_data) {
562     $swish_exact_data =~ s/ +/ /g;
563     $swish_exact_data =~ s/ +$//g;
564    
565     # add delimiters before and after word.
566     # That is required to produce exact match
567 dpavlin 321 $xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data));
568 dpavlin 13 }
569 dpavlin 182
570 dpavlin 188 my $idel = $cache->{index_delimiter}->{$field};
571     foreach my $tmp (@{$cache->{index_data}->{$field}->[$page]}) {
572     my $i = $tmp;
573     my $d = $tmp;
574     if ($idel && $tmp =~ m/$idel/) {
575     ($i,$d) = split(/$idel/,$tmp);
576     }
577     $index->insert($field, $i, $d, $path);
578     #print STDERR "index [$idel] $field: $i --> $d [$path]\n";
579 dpavlin 182 }
580 dpavlin 13 }
581 dpavlin 178
582 dpavlin 10 }
583 dpavlin 178
584     if (! $iterate_by_page) {
585     if ($display_data) {
586     if ($field eq "headline") {
587     $xml .= xmlify("headline", $display_data);
588     } else {
589 dpavlin 20
590 dpavlin 178 # fallback to empty field name if needed
591     $html .= get_field_name($config,$field,$field_usage{$field}) || '';
592     $html .= "#-#".$display_data."###\n";
593     }
594     }
595     if ($swish_data) {
596     # remove extra spaces
597     $swish_data =~ s/ +/ /g;
598     $swish_data =~ s/ +$//g;
599 dpavlin 10
600 dpavlin 321 $xml .= xmlify($field."_swish", my_unac_string($codepage,$swish_data));
601 dpavlin 178 }
602 dpavlin 9
603 dpavlin 178 if ($swish_exact_data) {
604     $swish_exact_data =~ s/ +/ /g;
605     $swish_exact_data =~ s/ +$//g;
606    
607     # add delimiters before and after word.
608     # That is required to produce exact match
609 dpavlin 321 $xml .= xmlify($field."_swish_exact", my_unac_string($codepage,$swish_exact_data));
610 dpavlin 178 }
611 dpavlin 163 }
612 dpavlin 3 }
613 dpavlin 13
614     # dump formatted output in <html>
615     if ($html) {
616 dpavlin 81 #$xml .= xmlify("html",$html);
617     $xml .= "<html><![CDATA[ $html ]]></html>";
618 dpavlin 13 }
619    
620 dpavlin 3 if ($xml) {
621 dpavlin 13 $xml .= $add_xml if ($add_xml);
622 dpavlin 10 return "<xml>\n$xml</xml>\n";
623 dpavlin 3 } else {
624     return;
625     }
626     }
627    
628     ##########################################################################
629    
630 dpavlin 54 # read configuration for this script
631 dpavlin 13 my $cfg = new Config::IniFiles( -file => $config_file );
632 dpavlin 1
633 dpavlin 54 # read global.conf configuration
634     my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
635    
636 dpavlin 50 # open index
637     $index = new index_DBI(
638 dpavlin 54 $cfg_global->val('global', 'dbi_dbd'),
639     $cfg_global->val('global', 'dbi_dsn'),
640     $cfg_global->val('global', 'dbi_user'),
641     $cfg_global->val('global', 'dbi_passwd') || '',
642 dpavlin 50 );
643    
644 dpavlin 97 my $show_progress = $cfg_global->val('global', 'show_progress');
645    
646 dpavlin 321 my $my_unac_filter = $cfg_global->val('global', 'my_unac_filter');
647     if ($my_unac_filter) {
648     print STDERR "using $my_unac_filter to filter characters for search\n";
649     require $my_unac_filter;
650     } else {
651     print STDERR "### fallback to default my_unac_string!\n";
652     eval q{
653     sub main::my_unac_string($$) {
654     my ($charset, $string) = (@_);
655     return $string;
656     }
657     };
658 dpavlin 164 }
659    
660 dpavlin 13 foreach my $database ($cfg->Sections) {
661 dpavlin 1
662 dpavlin 674 # save database name in global variable path for later
663     # (need for index filter creation)
664     $path = $database;
665    
666 dpavlin 54 my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
667 dpavlin 40 my $add_xml = $cfg -> val($database, 'xml'); # optional
668 dpavlin 1
669 dpavlin 177 # create new lookup file
670     my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional
671     if ($lookup_file) {
672     #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644;
673 dpavlin 458 if (! -e $lookup_file) {
674     open(LOOKUP, "> $lookup_file") || die "can't create $lookup_file': $!";
675     close(LOOKUP);
676     }
677 dpavlin 177 tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644;
678     print STDERR "creating lookup file '$lookup_file'\n";
679 dpavlin 231 # delete memory cache for lookup file
680     delete $cache->{lhash};
681 dpavlin 177 }
682    
683     # open existing lookup file
684     $lookup_file = $cfg -> val($database, 'lookup_open'); # optional
685     if ($lookup_file) {
686     #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644;
687     tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644;
688     print STDERR "opening lookup file '$lookup_file'\n";
689     }
690    
691 dpavlin 684 my $import_xml_type = $cfg->val($database, 'import_xml_file') || $type;
692     my $import_xml_file = "./import_xml/$import_xml_type.xml";
693 dpavlin 1
694 dpavlin 684 if (! -r $import_xml_file) {
695     print STDERR "ERROR: file $import_xml_file not readable skipping!\n";
696     next;
697     }
698    
699     print STDERR "reading $import_xml_file\n";
700    
701 dpavlin 58 # extract just type basic
702     my $type_base = $type;
703     $type_base =~ s/_.+$//g;
704 dpavlin 40
705 dpavlin 684 my $tag = $cfg->val($database, 'import_xml_tag') || $type2tag{$type_base} || die "can't find which tag to use for type $type";
706     $config=XMLin($import_xml_file, ForceArray => [ $tag, 'config', 'format' ], ForceContent => 1 );
707 dpavlin 58
708 dpavlin 633 # helper for progress bar
709     sub fmt_time {
710     my $t = shift || 0;
711     my $out = "";
712    
713     my ($ss,$mm,$hh) = gmtime($t);
714     $out .= "${hh}h" if ($hh);
715     $out .= sprintf("%02d:%02d", $mm,$ss);
716     $out .= " " if ($hh == 0);
717     return $out;
718     }
719    
720 dpavlin 54 # output current progress indicator
721     my $last_p = 0;
722 dpavlin 633 my $start_t = time();
723 dpavlin 54 sub progress {
724 dpavlin 101 return if (! $show_progress);
725 dpavlin 54 my $current = shift;
726 dpavlin 57 my $total = shift || 1;
727 dpavlin 54 my $p = int($current * 100 / $total);
728 dpavlin 633 if ($p < $last_p || $current == 1) {
729     $start_t = time();
730     $last_p = 0;
731     } elsif ($p != $last_p) {
732     my $rate = ($current / (time() - $start_t || 1));
733     my $eta = ($total-$current) / ($rate || 1);
734     printf STDERR ("%5d [%-38s] %-5d %0.1f/s %s\r",$current,"=" x ($p/3)."$p%>", $total, $rate, fmt_time($eta));
735 dpavlin 54 $last_p = $p;
736     }
737 dpavlin 3 }
738    
739 dpavlin 67 my $fake_dir = 1;
740 dpavlin 633 my $fake_pos = 0;
741     my $last_fake_t = time();
742 dpavlin 67 sub fakeprogress {
743 dpavlin 104 return if (! $show_progress);
744 dpavlin 67 my $current = shift @_;
745    
746 dpavlin 633 my @ind = ('-','\\','|','/','-','\\','|','/');
747 dpavlin 67
748 dpavlin 633 if ($current < $fake_pos) {
749     $start_t = time();
750     $last_fake_t = 0;
751     $fake_dir = 1;
752     $fake_pos = 0;
753 dpavlin 67 }
754 dpavlin 633
755     if (time()-$last_fake_t >= 1) {
756     $last_fake_t = time();
757     $fake_pos += $fake_dir;
758     $fake_dir = -$fake_dir if ($fake_pos > 38);
759     }
760    
761     if ($current % 10 == 0) {
762     my $rate = ($current / (time() - $start_t || 1));
763     printf STDERR ("%5d [%-38s] %0.1f/s\r",$current, " " x $fake_pos .$ind[($current / 10) % 8], $rate);
764     }
765 dpavlin 67 }
766    
767 dpavlin 54 # now read database
768 dpavlin 1
769 dpavlin 180 # erase cache for tags by order in this database
770     delete $cache->{tags_by_order};
771    
772 dpavlin 58 if ($type_base eq "isis") {
773    
774 dpavlin 54 my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
775 dpavlin 1
776 dpavlin 54 $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
777 dpavlin 665 my $db = new Biblio::Isis( isisdb => $isis_db ) || next;
778 dpavlin 10
779 dpavlin 674 if (! $db) {
780 dpavlin 652 print STDERR "FATAL: can't read ISIS database: $isis_db, skipping...\n";
781     next;
782     }
783    
784 dpavlin 674 my $max_rowid = $db->count if ($db);
785    
786 dpavlin 54 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
787    
788     for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
789 dpavlin 633 my $row = $db->to_hash( $row_id );
790     if ($row) {
791    
792     $row->{mfn} = $row_id;
793     $row->{record} = $db->{record};
794    
795 dpavlin 54 progress($row->{mfn}, $max_rowid);
796    
797     my $swishpath = $path."#".int($row->{mfn});
798    
799 dpavlin 59 if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
800 dpavlin 54 $xml = $cp2utf->convert($xml);
801     use bytes; # as opposed to chars
802     print "Path-Name: $swishpath\n";
803     print "Content-Length: ".(length($xml)+1)."\n";
804     print "Document-Type: XML\n\n$xml\n";
805     }
806 dpavlin 3 }
807 dpavlin 54 }
808     print STDERR "\n";
809 dpavlin 3
810 dpavlin 58 } elsif ($type_base eq "excel") {
811 dpavlin 255 require Spreadsheet::ParseExcel;
812     require Spreadsheet::ParseExcel::Utility;
813     import Spreadsheet::ParseExcel::Utility qw(int2col);
814 dpavlin 54
815     $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);
816     my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
817 dpavlin 43
818 dpavlin 54 my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
819 dpavlin 731 my $start_row = x($config->{start_row}) || die "no start_row in $type.xml";
820     $start_row--;
821 dpavlin 54
822     my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
823    
824     my $sheet_nr = 0;
825     foreach my $oWks (@{$oBook->{Worksheet}}) {
826     #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";
827     last if ($oWks->{Name} eq $sheet);
828     $sheet_nr++;
829     }
830    
831     my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];
832    
833     print STDERR "using sheet: ",$oWorksheet->{Name},"\n";
834     defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file";
835     my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow};
836    
837 dpavlin 56 for(my $iR = $start_row ; defined $end_row && $iR <= $end_row ; $iR++) {
838 dpavlin 54 my $row;
839     for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
840     my $cell = $oWorksheet->{Cells}[$iR][$iC];
841     if ($cell) {
842 dpavlin 489 # this conversion is a cludge.
843     # Files from Excell could have
844     # characters which don't fit into
845     # destination encoding.
846     $row->{int2col($iC)} = $utf2cp->convert($cell->Value) || $cell->Value;
847 dpavlin 54 }
848     }
849    
850     progress($iR, $end_row);
851    
852     # print "row[$iR/$end_row] ";
853     # foreach (keys %{$row}) {
854     # print "$_: ",$row->{$_},"\t";
855     # }
856     # print "\n";
857    
858     my $swishpath = $database."#".$iR;
859    
860     next if (! $row);
861    
862 dpavlin 59 if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
863 dpavlin 44 $xml = $cp2utf->convert($xml);
864 dpavlin 35 use bytes; # as opposed to chars
865 dpavlin 43 print "Path-Name: $swishpath\n";
866 dpavlin 10 print "Content-Length: ".(length($xml)+1)."\n";
867     print "Document-Type: XML\n\n$xml\n";
868 dpavlin 3 }
869 dpavlin 1 }
870 dpavlin 674
871     print STDERR "\n";
872    
873 dpavlin 62 } elsif ($type_base eq "marc") {
874 dpavlin 67
875 dpavlin 633 require MARC::File::USMARC;
876 dpavlin 62
877     $import2cp = Text::Iconv->new($config->{marc_codepage},$codepage);
878     my $marc_file = $cfg -> val($database, 'marc_file') || die "$database doesn't have 'marc_file' defined!";
879    
880     # optional argument is format
881 dpavlin 633 warn "marc_format is no longer used!" if ($config->{marc_format});
882 dpavlin 62 print STDERR "Reading MARC file '$marc_file'\n";
883    
884 dpavlin 652 my $marc = MARC::File::USMARC->in( $marc_file );
885 dpavlin 62
886 dpavlin 652 if (! $marc) {
887     print STDERR "FATAL: can't read MARC file: $marc_file, skipping...\n";
888     next;
889     }
890    
891 dpavlin 633 # count records in MARC file
892     sub marc_count {
893     my $filename = shift || die;
894 dpavlin 652 my $file = MARC::File::USMARC->in($filename) || return;
895 dpavlin 633 my $count = 0;
896     while ($file->skip()) {
897     $count++;
898     }
899     return $count;
900     }
901 dpavlin 62
902 dpavlin 633 my $count = marc_count($marc_file) || warn "no records in '$marc_file'?";
903 dpavlin 62
904 dpavlin 652 my $i = 1;
905 dpavlin 62
906 dpavlin 633 while( my $rec = $marc->next() ) {
907 dpavlin 62
908 dpavlin 652 progress($i,$count);
909 dpavlin 215
910 dpavlin 62 my $swishpath = $database."#".$i;
911    
912 dpavlin 633 if (my $xml = data2xml($type_base,$rec,$add_xml,$cfg,$database)) {
913 dpavlin 62 $xml = $cp2utf->convert($xml);
914     use bytes; # as opposed to chars
915     print "Path-Name: $swishpath\n";
916     print "Content-Length: ".(length($xml)+1)."\n";
917     print "Document-Type: XML\n\n$xml\n";
918     }
919 dpavlin 652
920     $i++;
921 dpavlin 62 }
922 dpavlin 215
923     print STDERR "\n";
924    
925 dpavlin 67 } elsif ($type_base eq "feed") {
926    
927     $import2cp = Text::Iconv->new($config->{feed_codepage},$codepage);
928     my $prog = x($config->{prog}) || die "$database doesn't have 'prog' defined!";
929    
930     print STDERR "Reading feed from program '$prog'\n";
931    
932     open(FEED,"feeds/$prog |") || die "can't start $prog: $!";
933    
934 dpavlin 74 my $i=1; # record nr.
935 dpavlin 67
936     my $data;
937     my $line=1;
938    
939     while (<FEED>) {
940     chomp;
941    
942     if (/^$/) {
943 dpavlin 74 my $swishpath = $database."#".$i++;
944 dpavlin 67
945     if (my $xml = data2xml($type_base,$data,$add_xml,$cfg,$database)) {
946     $xml = $cp2utf->convert($xml);
947     use bytes; # as opposed to chars
948     print "Path-Name: $swishpath\n";
949     print "Content-Length: ".(length($xml)+1)."\n";
950     print "Document-Type: XML\n\n$xml\n";
951     }
952     $line = 1;
953     $data = {};
954     next;
955     }
956    
957 dpavlin 74 $line = $1 if (s/^(\d+):\s*//);
958 dpavlin 67 $data->{$line++} = $_;
959    
960 dpavlin 74 fakeprogress($i);
961 dpavlin 67
962     }
963 dpavlin 177 # close lookup
964     untie %lhash if (%lhash);
965 dpavlin 684
966     } elsif ($type_base eq "dbf") {
967    
968     my $dbf_file = $cfg -> val($database, 'dbf_file') || die "$database doesn't have 'dbf_file' defined!";
969     my $dbf_codepage = $cfg -> val($database, 'dbf_codepage') || die "$database doesn't have 'dbf_codepage' defined!";
970     my $dbf_mapping = $cfg -> val($database, 'dbf_mapping') || die "$database doesn't have 'dbf_mapping' defined!";
971    
972     $import2cp = Text::Iconv->new($dbf_codepage,$codepage);
973     require XBase;
974     my $db = new XBase $dbf_file;
975    
976     if (! $db) {
977     print STDERR "ERROR: can't read DBF database: $dbf_file, skipping...\n";
978     next;
979     }
980    
981     my $max_rowid = $db->last_record;
982    
983     print STDERR "Reading database: $dbf_file [$max_rowid rows]\n";
984    
985     my %dbf2iso;
986     foreach my $m (split(/[\n\r]+/,$dbf_mapping)) {
987     my ($col,$fld) = split(/\s+/,$m,2);
988     $dbf2iso{$col} = $fld;
989     }
990    
991     #print STDERR "## dbf2iso: ",Dumper(\%dbf2iso),"\n## /dbf2iso\n";
992    
993     # bad, bad...
994     require "to_hash.pm";
995    
996     foreach my $row_id (0 .. $max_rowid) {
997     my $dbf_row = $db->get_record_as_hash($row_id);
998     if ($dbf_row) {
999    
1000     #print STDERR "## dbf_row: ",Dumper($dbf_row),"\n## /dbf_row\n";
1001     # apply mapping from config file
1002     # all unspecified records will get _ in
1003     # front of them - _DELETE will be __DELETE
1004     my $rec;
1005     map {
1006     my $new_fld = $dbf2iso{$_} || '_'.$_;
1007     my $data = $dbf_row->{$_};
1008     push @{ $rec->{$new_fld} }, $data if ($data && $data !~ /^(?:\s+|\$a\.|)$/);
1009     } keys %{$dbf_row};
1010     #print STDERR "## rec: ",Dumper($rec),"\n## /rec\n";
1011     my $row = to_hash($row_id+1, $rec);
1012    
1013     $row->{mfn} = $row_id+1;
1014     $row->{record} = $rec;
1015    
1016     #print STDERR "## row: ",Dumper($row),"\n## /row\n";
1017     progress($row->{mfn}, $max_rowid);
1018    
1019     my $swishpath = $path."#".int($row->{mfn});
1020    
1021     if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
1022     $xml = $cp2utf->convert($xml);
1023     use bytes; # as opposed to chars
1024     print "Path-Name: $swishpath\n";
1025     print "Content-Length: ".(length($xml)+1)."\n";
1026     print "Document-Type: XML\n\n$xml\n";
1027     }
1028     }
1029     }
1030     print STDERR "\n";
1031 dpavlin 1 }
1032     }
1033 dpavlin 3
1034 dpavlin 10 # call this to commit index
1035     $index->close;
1036 dpavlin 3
1037     1;
1038     __END__
1039     ##########################################################################
1040    
1041     =head1 NAME
1042    
1043 dpavlin 81 all2xml.pl - read various file formats and dump XML for SWISH-E
1044 dpavlin 3
1045 dpavlin 303 =head1 SYNOPSYS
1046    
1047     $ all2xml.pl [test.conf]
1048    
1049 dpavlin 3 =head1 DESCRIPTION
1050    
1051 dpavlin 652 This command will read ISIS data file using Biblio::Isis perl module, MARC
1052     records using MARC::File module and optionally Micro$oft Excel files to
1053 dpavlin 81 create one XML file for usage with I<SWISH-E> indexer. Dispite it's name,
1054     this script B<isn't general xml generator> from isis files (isis allready
1055     has something like that). Output of this script is tailor-made for SWISH-E.
1056 dpavlin 3
1057 dpavlin 303 If no configuration file is specified, it will use default one called
1058     C<all2xml.conf>.
1059    
1060 dpavlin 81 =head1 BUGS
1061    
1062     Documentation is really lacking. However, in true Open Source spirit, source
1063     is best documentation. I even made considerable effort to comment parts
1064     which are not intuitively clear, so...
1065    
1066 dpavlin 3 =head1 AUTHOR
1067    
1068     Dobrica Pavlinusic <dpavlin@rot13.org>
1069    
1070     =head1 COPYRIGHT
1071    
1072     GNU Public License (GPL) v2 or later
1073    
1074     =head1 SEE ALSO
1075    
1076     SWISH-E web site at http://www.swish-e.org
1077    
1078     =cut

Properties

Name Value
cvs2svn:cvs-rev 1.64
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26