/[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 684 - (hide annotations)
Mon Feb 28 10:43:38 2005 UTC (14 years, 8 months ago) by dpavlin
File MIME type: text/plain
File size: 29093 byte(s)
updated branches to head

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26