/[webpac]/branches/tehnika/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/tehnika/all2xml.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 776 - (hide annotations)
Sun Aug 3 06:33:56 2008 UTC (11 years, 2 months ago) by dpavlin
File MIME type: text/plain
File size: 30252 byte(s)
merged branches to trunk

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26