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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 177 - (hide annotations)
Mon Nov 24 01:19:15 2003 UTC (20 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 19332 byte(s)
support for lookup fields. Implemented using GDBM or TDB (which I recommend
because it's fastest implementation)

1 dpavlin 1 #!/usr/bin/perl -w
2    
3     use strict;
4     use OpenIsis;
5     use Getopt::Std;
6     use Data::Dumper;
7     use XML::Simple;
8 dpavlin 5 use Text::Unaccent 1.02; # 1.01 won't compile on my platform,
9 dpavlin 10 use Text::Iconv;
10 dpavlin 13 use Config::IniFiles;
11 dpavlin 40 use Encode;
12 dpavlin 177 #use GDBM_File;
13     use Fcntl; # for O_RDWR
14     use TDB_File;
15 dpavlin 1
16 dpavlin 10 $|=1;
17 dpavlin 9
18 dpavlin 13 my $config_file = $0;
19     $config_file =~ s/\.pl$/.conf/;
20     die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
21    
22 dpavlin 10 my $config;
23    
24 dpavlin 58 #use index_DBI; # default DBI module for index
25     use index_DBI_cache; # faster DBI module using memory cache
26 dpavlin 50 my $index;
27 dpavlin 10
28 dpavlin 1 my %opts;
29    
30 dpavlin 7 # usage:
31     # -d directory name
32     # -m multiple directories
33     # -q quiet
34     # -s run swish
35 dpavlin 1
36 dpavlin 7 getopts('d:m:qs', \%opts);
37    
38 dpavlin 43 my $path; # this is name of database
39 dpavlin 1
40 dpavlin 57 Text::Iconv->raise_error(0); # Conversion errors don't raise exceptions
41 dpavlin 1
42 dpavlin 40 # this is encoding of all files on disk, including import_xml/*.xml file and
43     # filter/*.pm files! It will be used to store strings in perl internally!
44     my $codepage = 'ISO-8859-2';
45 dpavlin 1
46 dpavlin 40 my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
47     # this function will convert data from XML files to local encoding
48     sub x {
49     return $utf2cp->convert($_[0]);
50     }
51 dpavlin 3
52 dpavlin 54 # decode isis/excel or other import codepage
53     my $import2cp;
54 dpavlin 10
55 dpavlin 40 # outgoing xml must be in UTF-8
56     my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
57 dpavlin 29
58 dpavlin 54 # mapping between data type and tag which specify
59     # format in XML file
60     my %type2tag = (
61     'isis' => 'isis',
62 dpavlin 62 'excel' => 'column',
63     'marc' => 'marc',
64 dpavlin 67 'feed' => 'feed'
65 dpavlin 54 );
66 dpavlin 3
67 dpavlin 170 my $cache; # for cacheing
68    
69 dpavlin 177 # lookup hash (tied to file)
70     my %lhash;
71     # this option will cache all lookup entries in memory.
72     # if you are tight on memory, turn this off
73     my $use_lhash_cache = 1;
74    
75 dpavlin 54 sub data2xml {
76    
77 dpavlin 10 use xmlify;
78    
79 dpavlin 54 my $type = shift @_;
80 dpavlin 3 my $row = shift @_;
81 dpavlin 13 my $add_xml = shift @_;
82 dpavlin 59 # needed to read values from configuration file
83     my $cfg = shift @_;
84     my $database = shift @_;
85 dpavlin 3
86     my $xml;
87    
88 dpavlin 10 use parse_format;
89 dpavlin 3
90 dpavlin 13 my $html = ""; # html formatted display output
91 dpavlin 10
92 dpavlin 13 my %field_usage; # counter for usage of each field
93    
94 dpavlin 32 # sort subrouting using order="" attribute
95     sub by_order {
96 dpavlin 98 my $va = $config->{indexer}->{$a}->{order} ||
97     $config->{indexer}->{$a};
98     my $vb = $config->{indexer}->{$b}->{order} ||
99     $config->{indexer}->{$b};
100 dpavlin 29
101 dpavlin 98 return $va <=> $vb;
102 dpavlin 32 }
103 dpavlin 3
104 dpavlin 170 my @sorted_tags;
105     if ($cache->{tags_by_order}->{$type}) {
106     @sorted_tags = @{$cache->{tags_by_order}->{$type}};
107     } else {
108     @sorted_tags = sort by_order keys %{$config->{indexer}};
109     $cache->{tags_by_order}->{$type} = \@sorted_tags;
110     }
111 dpavlin 32
112 dpavlin 177 # lookup key
113     my $lookup_key;
114    
115 dpavlin 170 foreach my $field (@sorted_tags) {
116    
117 dpavlin 40 $field=x($field);
118 dpavlin 13 $field_usage{$field}++;
119    
120 dpavlin 10 my $swish_data = "";
121 dpavlin 163 my $swish_exact_data = "";
122 dpavlin 3 my $display_data = "";
123 dpavlin 35 my $line_delimiter;
124 dpavlin 3
125 dpavlin 34 my ($swish,$display);
126    
127 dpavlin 54 my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";
128     foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
129 dpavlin 3
130 dpavlin 40 my $format = x($x->{content});
131     my $delimiter = x($x->{delimiter}) || ' ';
132 dpavlin 3
133 dpavlin 54 my $repeat_off = 0; # repeatable offset
134 dpavlin 29
135 dpavlin 177 # swish, swish_exact, display, index, index_lookup
136     # swish and display defaults
137     my ($s,$se,$d,$i,$il) = (1,0,1,0,0);
138 dpavlin 29 $s = 0 if (lc($x->{type}) eq "display");
139     $d = 0 if (lc($x->{type}) eq "swish");
140 dpavlin 163 $se = 1 if (lc($x->{type}) eq "swish_exact");
141 dpavlin 29 ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
142 dpavlin 177 $il = 1 if (lc($x->{type}) =~ /^lookup/);
143 dpavlin 29
144 dpavlin 177
145 dpavlin 35 # what will separate last line from this one?
146     if ($display_data && $x->{append} && $x->{append} eq "1") {
147 dpavlin 34 $line_delimiter = ' ';
148 dpavlin 35 } elsif ($display_data) {
149 dpavlin 34 $line_delimiter = '<br/>';
150     }
151 dpavlin 29
152 dpavlin 34 # init vars so that we go into while...
153     ($swish,$display) = (1,1);
154 dpavlin 29
155 dpavlin 97 # placeholder for all repeatable entries for index
156     my @index_data;
157    
158 dpavlin 153 sub mkformat {
159     my $x = shift || die "mkformat needs tag reference";
160     my $data = shift || return;
161     my $format_name = x($x->{format_name}) || return $data;
162     my $fmt = x($config->{format}->{$format_name}->{content}) || die "<format name=\"$format_name\"> is not defined!";
163     my $format_delimiter = x($x->{format_delimiter});
164     my @data;
165     if ($format_delimiter) {
166     @data = split(/$format_delimiter/,$data);
167     } else {
168     push @data,$data;
169     }
170    
171     if ($fmt) {
172     my $nr = scalar $fmt =~ s/%s/%s/g;
173     if (($#data+1) == $nr) {
174     return sprintf($fmt,@data);
175     } else {
176     print STDERR "mkformat: [$data] can't be split on [$format_delimiter] to $nr fields!\n";
177     return $data;
178     }
179     } else {
180     print STDERR "usage of link '$format_name' without defined format (<link> tag)\n";
181     }
182     }
183    
184 dpavlin 90 # while because of repeatable fields
185     while ($swish || $display) {
186 dpavlin 54 ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
187 dpavlin 90 if ($repeat_off > 1000) {
188     print STDERR "loop (more than 1000 repeatable fields) deteced in $row, $format\n";
189     last;
190     }
191 dpavlin 177
192     # is this field is lookup?
193     if ($display && $x->{lookup}) {
194     if ($use_lhash_cache) {
195     if (!defined($cache->{lhash}->{$display})) {
196     my $new_display = $lhash{$display};
197     if ($new_display) {
198     #print STDERR "lookup cache store '$display' = '$new_display'\n";
199     $display = $new_display;
200     $cache->{lhash}->{$display} = $new_display;
201     } else {
202     print STDERR "WARNING: lookup for '$display' didn't find anything.\n";
203     $display = "";
204     $cache->{lhash}->{$display} = "";
205     }
206     } else {
207     $display = $cache->{lhash}->{$display};
208     }
209     } else {
210     $display = $lhash{$display} || "";
211     }
212     }
213    
214 dpavlin 29 # filter="name" ; filter this field through
215     # filter/[name].pm
216     my $filter = $x->{filter};
217 dpavlin 170 if ($filter && !$cache->{filter_loaded}->{$filter}) {
218 dpavlin 29 require "filter/".$filter.".pm";
219 dpavlin 170 $cache->{filter_loaded}->{$filter}++;
220 dpavlin 20 }
221 dpavlin 29 # type="swish" ; field for swish
222 dpavlin 163 if ($swish) {
223     if ($filter && ($s || $se)) {
224 dpavlin 29 no strict 'refs';
225 dpavlin 163 my $tmp = join(" ",&$filter($swish)) if ($s || $se);
226     $swish_data .= $tmp if ($s);
227     $swish_exact_data .= $tmp if ($se);
228    
229 dpavlin 29 } else {
230 dpavlin 163 $swish_data .= $swish if ($s);
231     $swish_exact_data .= $swish if ($se);
232 dpavlin 29 }
233     }
234 dpavlin 17
235 dpavlin 29 # type="display" ; field for display
236     if ($d && $display) {
237 dpavlin 35 if ($line_delimiter && $display_data) {
238     $display_data .= $line_delimiter;
239     undef $line_delimiter;
240     }
241 dpavlin 29 if ($filter) {
242     no strict 'refs';
243 dpavlin 138 if ($display_data) {
244 dpavlin 153 $display_data .= $delimiter.join($delimiter,mkformat($x,&$filter($display)));
245 dpavlin 138 } else {
246 dpavlin 153 $display_data = join($delimiter,mkformat($x,&$filter($display)));
247 dpavlin 138 }
248 dpavlin 29 } else {
249 dpavlin 138 if ($display_data) {
250 dpavlin 153 $display_data .= $delimiter.mkformat($x,$display);
251 dpavlin 138 } else {
252 dpavlin 153 $display_data = mkformat($x,$display);
253 dpavlin 138 }
254 dpavlin 29 }
255 dpavlin 20 }
256 dpavlin 29
257     # type="index" ; insert into index
258     if ($i && $display) {
259 dpavlin 177 if ($filter) {
260     no strict 'refs';
261     $display = &$filter($display);
262     }
263     if ($x->{append} && @index_data) {
264     $index_data[$#index_data].=$display;
265     } else {
266     push @index_data, $display;
267     }
268 dpavlin 97 }
269    
270 dpavlin 177 # store fields in lookup
271     if ($il && $display) {
272     if (lc($x->{type}) eq "lookup_key") {
273     if ($lookup_key) {
274     print STDERR "WARNING: try to redefine lookup_key (keys shouldn't be repeatable fields!)";
275     } else {
276     $lookup_key = $display;
277     }
278     } elsif (lc($x->{type}) eq "lookup_val") {
279     if ($lookup_key) {
280     $lhash{$lookup_key} = $display;
281     } else {
282     print STDERR "WARNING: no lookup_key defined for '$display'?";
283     }
284 dpavlin 20 }
285     }
286 dpavlin 17 }
287 dpavlin 177
288     # fill data in index
289     foreach my $d (@index_data) {
290     $index->insert($field, $d, $path);
291     }
292 dpavlin 3 }
293 dpavlin 9
294 dpavlin 59 # now try to parse variables from configuration file
295     foreach my $x (@{$config->{indexer}->{$field}->{'config'}}) {
296 dpavlin 13
297 dpavlin 62 my $delimiter = x($x->{delimiter}) || ' ';
298 dpavlin 59 my $val = $cfg->val($database, x($x->{content}));
299    
300     my ($s,$d,$i) = (1,1,0); # swish, display default
301     $s = 0 if (lc($x->{type}) eq "display");
302     $d = 0 if (lc($x->{type}) eq "swish");
303 dpavlin 163 # no support for swish exact in config.
304     # IMHO, it's useless
305 dpavlin 59 ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
306    
307     if ($val) {
308 dpavlin 62 $display_data .= $delimiter.$val if ($d);
309 dpavlin 59 $swish_data .= $val if ($s);
310     $index->insert($field, $val, $path) if ($i);
311     }
312    
313     }
314    
315    
316 dpavlin 10 if ($display_data) {
317 dpavlin 29
318 dpavlin 13 if ($field eq "headline") {
319     $xml .= xmlify("headline", $display_data);
320     } else {
321    
322     # find field name (signular, plural)
323     my $field_name = "";
324     if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
325     $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
326     } elsif ($config->{indexer}->{$field}->{name_plural}) {
327     $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
328 dpavlin 20 } elsif ($config->{indexer}->{$field}->{name}) {
329     $field_name = $config->{indexer}->{$field}->{name}."#-#";
330 dpavlin 13 } else {
331 dpavlin 20 print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
332 dpavlin 13 }
333     if ($field_name) {
334 dpavlin 40 $html .= x($field_name);
335 dpavlin 13 }
336     $html .= $display_data."###\n";
337     }
338 dpavlin 10 }
339     if ($swish_data) {
340 dpavlin 20 # remove extra spaces
341     $swish_data =~ s/ +/ /g;
342     $swish_data =~ s/ +$//g;
343    
344 dpavlin 40 $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
345 dpavlin 10 }
346    
347 dpavlin 163 if ($swish_exact_data) {
348     $swish_exact_data =~ s/ +/ /g;
349     $swish_exact_data =~ s/ +$//g;
350 dpavlin 9
351 dpavlin 163 # add delimiters before and after word.
352     # That is required to produce exact match
353     $xml .= xmlify($field."_swish_exact", unac_string($codepage,'xxbxx '.$swish_exact_data.' xxexx'));
354     }
355    
356    
357 dpavlin 3 }
358 dpavlin 13
359     # dump formatted output in <html>
360     if ($html) {
361 dpavlin 81 #$xml .= xmlify("html",$html);
362     $xml .= "<html><![CDATA[ $html ]]></html>";
363 dpavlin 13 }
364    
365 dpavlin 3 if ($xml) {
366 dpavlin 13 $xml .= $add_xml if ($add_xml);
367 dpavlin 10 return "<xml>\n$xml</xml>\n";
368 dpavlin 3 } else {
369     return;
370     }
371     }
372    
373     ##########################################################################
374    
375 dpavlin 54 # read configuration for this script
376 dpavlin 13 my $cfg = new Config::IniFiles( -file => $config_file );
377 dpavlin 1
378 dpavlin 54 # read global.conf configuration
379     my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
380    
381 dpavlin 50 # open index
382     $index = new index_DBI(
383 dpavlin 54 $cfg_global->val('global', 'dbi_dbd'),
384     $cfg_global->val('global', 'dbi_dsn'),
385     $cfg_global->val('global', 'dbi_user'),
386     $cfg_global->val('global', 'dbi_passwd') || '',
387 dpavlin 50 );
388    
389 dpavlin 97 my $show_progress = $cfg_global->val('global', 'show_progress');
390    
391 dpavlin 164 my $unac_filter = $cfg_global->val('global', 'unac_filter');
392     if ($unac_filter) {
393     require $unac_filter;
394     }
395    
396 dpavlin 13 foreach my $database ($cfg->Sections) {
397 dpavlin 1
398 dpavlin 54 my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
399 dpavlin 40 my $add_xml = $cfg -> val($database, 'xml'); # optional
400 dpavlin 1
401 dpavlin 177 # create new lookup file
402     my $lookup_file = $cfg -> val($database, 'lookup_newfile'); # optional
403     if ($lookup_file) {
404     #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_NEWDB, 0644;
405     tie %lhash, 'TDB_File', $lookup_file, TDB_CLEAR_IF_FIRST, O_RDWR, 0644;
406     print STDERR "creating lookup file '$lookup_file'\n";
407     }
408    
409     # open existing lookup file
410     $lookup_file = $cfg -> val($database, 'lookup_open'); # optional
411     if ($lookup_file) {
412     #tie %lhash, 'GDBM_File', $lookup_file, &GDBM_READER, 0644;
413     tie %lhash, 'TDB_File', $lookup_file, TDB_DEFAULT, O_RDWR, 0644;
414     print STDERR "opening lookup file '$lookup_file'\n";
415     }
416    
417 dpavlin 54 print STDERR "reading ./import_xml/$type.xml\n";
418 dpavlin 1
419 dpavlin 58 # extract just type basic
420     my $type_base = $type;
421     $type_base =~ s/_.+$//g;
422 dpavlin 40
423 dpavlin 153 $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type_base}, 'config', 'format' ], forcecontent => 1);
424 dpavlin 58
425 dpavlin 54 # output current progress indicator
426     my $last_p = 0;
427     sub progress {
428 dpavlin 101 return if (! $show_progress);
429 dpavlin 54 my $current = shift;
430 dpavlin 57 my $total = shift || 1;
431 dpavlin 54 my $p = int($current * 100 / $total);
432     if ($p != $last_p) {
433     printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );
434     $last_p = $p;
435     }
436 dpavlin 3 }
437    
438 dpavlin 67 my $fake_dir = 1;
439     sub fakeprogress {
440 dpavlin 104 return if (! $show_progress);
441 dpavlin 67 my $current = shift @_;
442    
443     my @ind = ('-','\\','|','/','-','\\','|','/', '-');
444    
445     $last_p += $fake_dir;
446     $fake_dir = -$fake_dir if ($last_p > 1000 || $last_p < 0);
447     if ($last_p % 10 == 0) {
448     printf STDERR ("%5d / %5s [%-51s]\r",$current,"?"," " x ($last_p/20).$ind[($last_p/20) % $#ind]);
449     }
450     }
451    
452 dpavlin 54 # now read database
453     print STDERR "using: $type...\n";
454 dpavlin 1
455 dpavlin 58 if ($type_base eq "isis") {
456    
457 dpavlin 54 my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
458 dpavlin 1
459 dpavlin 54 $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
460     my $db = OpenIsis::open( $isis_db );
461 dpavlin 10
462 dpavlin 106 # check if .txt database for OpenIsis is zero length,
463     # if so, erase it and re-open database
464     sub check_txt_db {
465     my $isis_db = shift || die "need isis database name";
466 dpavlin 109 my $reopen = 0;
467    
468 dpavlin 106 if (-e $isis_db.".TXT") {
469 dpavlin 109 print STDERR "WARNING: removing $isis_db.TXT OpenIsis database...\n";
470 dpavlin 108 unlink $isis_db.".TXT" || warn "FATAL: unlink error on '$isis_db.TXT': $!";
471 dpavlin 109 $reopen++;
472 dpavlin 106 }
473 dpavlin 109 if (-e $isis_db.".PTR") {
474     print STDERR "WARNING: removing $isis_db.PTR OpenIsis database...\n";
475     unlink $isis_db.".PTR" || warn "FATAL: unlink error on '$isis_db.PTR': $!";
476     $reopen++;
477     }
478     return OpenIsis::open( $isis_db ) if ($reopen);
479 dpavlin 106 }
480    
481     # EOF error
482     if ($db == -1) {
483     $db = check_txt_db($isis_db);
484 dpavlin 108 if ($db == -1) {
485 dpavlin 106 print STDERR "FATAL: OpenIsis can't open zero size file $isis_db\n";
486     next;
487     }
488     }
489    
490 dpavlin 101 # OpenIsis::ERR_BADF
491     if ($db == -4) {
492     print STDERR "FATAL: OpenIsis can't find file $isis_db\n";
493     next;
494     # OpenIsis::ERR_IO
495     } elsif ($db == -5) {
496     print STDERR "FATAL: OpenIsis can't access file $isis_db\n";
497     next;
498     } elsif ($db < 0) {
499     print STDERR "FATAL: OpenIsis unknown error $db with file $isis_db\n";
500     next;
501     }
502    
503 dpavlin 54 my $max_rowid = OpenIsis::maxRowid( $db );
504 dpavlin 3
505 dpavlin 106 # if 0 records, try to rease isis .txt database
506     if ($max_rowid == 0) {
507     # force removal of database
508     $db = check_txt_db($isis_db);
509     $max_rowid = OpenIsis::maxRowid( $db );
510     }
511    
512 dpavlin 54 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
513    
514     my $path = $database;
515    
516     for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
517     my $row = OpenIsis::read( $db, $row_id );
518     if ($row && $row->{mfn}) {
519    
520     progress($row->{mfn}, $max_rowid);
521    
522     my $swishpath = $path."#".int($row->{mfn});
523    
524 dpavlin 59 if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
525 dpavlin 54 $xml = $cp2utf->convert($xml);
526     use bytes; # as opposed to chars
527     print "Path-Name: $swishpath\n";
528     print "Content-Length: ".(length($xml)+1)."\n";
529     print "Document-Type: XML\n\n$xml\n";
530     }
531 dpavlin 3 }
532 dpavlin 54 }
533 dpavlin 108 # for this to work with current version of OpenIsis (0.9.0)
534     # you might need my patch from
535     # http://www.rot13.org/~dpavlin/projects/openisis-0.9.0-perl_close.diff
536     OpenIsis::close($db);
537 dpavlin 54 print STDERR "\n";
538 dpavlin 3
539 dpavlin 58 } elsif ($type_base eq "excel") {
540 dpavlin 54 use Spreadsheet::ParseExcel;
541     use Spreadsheet::ParseExcel::Utility qw(int2col);
542    
543     $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);
544     my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
545 dpavlin 43
546 dpavlin 54 my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
547 dpavlin 56 my $start_row = x($config->{start_row}) - 1 || die "no start_row in $type.xml";
548 dpavlin 54
549     my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
550    
551     my $sheet_nr = 0;
552     foreach my $oWks (@{$oBook->{Worksheet}}) {
553     #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";
554     last if ($oWks->{Name} eq $sheet);
555     $sheet_nr++;
556     }
557    
558     my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];
559    
560     print STDERR "using sheet: ",$oWorksheet->{Name},"\n";
561     defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file";
562     my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow};
563    
564 dpavlin 56 for(my $iR = $start_row ; defined $end_row && $iR <= $end_row ; $iR++) {
565 dpavlin 54 my $row;
566     for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
567     my $cell = $oWorksheet->{Cells}[$iR][$iC];
568     if ($cell) {
569     $row->{int2col($iC)} = $cell->Value;
570     }
571     }
572    
573     progress($iR, $end_row);
574    
575     # print "row[$iR/$end_row] ";
576     # foreach (keys %{$row}) {
577     # print "$_: ",$row->{$_},"\t";
578     # }
579     # print "\n";
580    
581     my $swishpath = $database."#".$iR;
582    
583     next if (! $row);
584    
585 dpavlin 59 if (my $xml = data2xml($type_base,$row,$add_xml,$cfg,$database)) {
586 dpavlin 44 $xml = $cp2utf->convert($xml);
587 dpavlin 35 use bytes; # as opposed to chars
588 dpavlin 43 print "Path-Name: $swishpath\n";
589 dpavlin 10 print "Content-Length: ".(length($xml)+1)."\n";
590     print "Document-Type: XML\n\n$xml\n";
591 dpavlin 3 }
592 dpavlin 1 }
593 dpavlin 62 } elsif ($type_base eq "marc") {
594 dpavlin 67
595 dpavlin 62 use MARC;
596    
597     $import2cp = Text::Iconv->new($config->{marc_codepage},$codepage);
598     my $marc_file = $cfg -> val($database, 'marc_file') || die "$database doesn't have 'marc_file' defined!";
599    
600     # optional argument is format
601     my $format = x($config->{format}) || 'usmarc';
602    
603     print STDERR "Reading MARC file '$marc_file'\n";
604    
605     my $marc = new MARC;
606     my $nr = $marc->openmarc({
607     file=>$marc_file, format=>$format
608     }) || die "Can't open MARC file '$marc_file'";
609    
610     my $i=0; # record nr.
611    
612     my $rec;
613    
614     while ($marc->nextmarc(1)) {
615    
616     # XXX
617 dpavlin 67 fakeprogress($i++);
618 dpavlin 62
619     my $swishpath = $database."#".$i;
620    
621     if (my $xml = data2xml($type_base,$marc,$add_xml,$cfg,$database)) {
622     $xml = $cp2utf->convert($xml);
623     use bytes; # as opposed to chars
624     print "Path-Name: $swishpath\n";
625     print "Content-Length: ".(length($xml)+1)."\n";
626     print "Document-Type: XML\n\n$xml\n";
627     }
628     }
629 dpavlin 67 } elsif ($type_base eq "feed") {
630    
631     $import2cp = Text::Iconv->new($config->{feed_codepage},$codepage);
632     my $prog = x($config->{prog}) || die "$database doesn't have 'prog' defined!";
633    
634     print STDERR "Reading feed from program '$prog'\n";
635    
636     open(FEED,"feeds/$prog |") || die "can't start $prog: $!";
637    
638 dpavlin 74 my $i=1; # record nr.
639 dpavlin 67
640     my $data;
641     my $line=1;
642    
643     while (<FEED>) {
644     chomp;
645    
646     if (/^$/) {
647 dpavlin 74 my $swishpath = $database."#".$i++;
648 dpavlin 67
649     if (my $xml = data2xml($type_base,$data,$add_xml,$cfg,$database)) {
650     $xml = $cp2utf->convert($xml);
651     use bytes; # as opposed to chars
652     print "Path-Name: $swishpath\n";
653     print "Content-Length: ".(length($xml)+1)."\n";
654     print "Document-Type: XML\n\n$xml\n";
655     }
656     $line = 1;
657     $data = {};
658     next;
659     }
660    
661 dpavlin 74 $line = $1 if (s/^(\d+):\s*//);
662 dpavlin 67 $data->{$line++} = $_;
663    
664 dpavlin 74 fakeprogress($i);
665 dpavlin 67
666     }
667 dpavlin 177 # close lookup
668     untie %lhash if (%lhash);
669 dpavlin 1 }
670     }
671 dpavlin 3
672 dpavlin 10 # call this to commit index
673     $index->close;
674 dpavlin 3
675     1;
676     __END__
677     ##########################################################################
678    
679     =head1 NAME
680    
681 dpavlin 81 all2xml.pl - read various file formats and dump XML for SWISH-E
682 dpavlin 3
683     =head1 DESCRIPTION
684    
685 dpavlin 81 This command will read ISIS data file using OpenIsis perl module, MARC
686     records using MARC module and optionally Micro$oft Excel files to
687     create one XML file for usage with I<SWISH-E> indexer. Dispite it's name,
688     this script B<isn't general xml generator> from isis files (isis allready
689     has something like that). Output of this script is tailor-made for SWISH-E.
690 dpavlin 3
691 dpavlin 81 =head1 BUGS
692    
693     Documentation is really lacking. However, in true Open Source spirit, source
694     is best documentation. I even made considerable effort to comment parts
695     which are not intuitively clear, so...
696    
697 dpavlin 3 =head1 AUTHOR
698    
699     Dobrica Pavlinusic <dpavlin@rot13.org>
700    
701     =head1 COPYRIGHT
702    
703     GNU Public License (GPL) v2 or later
704    
705     =head1 SEE ALSO
706    
707     SWISH-E web site at http://www.swish-e.org
708    
709     =cut

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26