18 |
|
|
19 |
my $config; |
my $config; |
20 |
|
|
21 |
use index_DBI; # there is no other, right now ;-) |
#use index_DBI; # default DBI module for index |
22 |
|
use index_DBI_cache; # faster DBI module using memory cache |
23 |
my $index = new index_DBI(); # open index |
my $index; |
24 |
|
|
25 |
my %opts; |
my %opts; |
26 |
|
|
34 |
|
|
35 |
my $path; # this is name of database |
my $path; # this is name of database |
36 |
|
|
37 |
Text::Iconv->raise_error(1); # Conversion errors raise exceptions |
Text::Iconv->raise_error(0); # Conversion errors don't raise exceptions |
38 |
|
|
39 |
# this is encoding of all files on disk, including import_xml/*.xml file and |
# this is encoding of all files on disk, including import_xml/*.xml file and |
40 |
# filter/*.pm files! It will be used to store strings in perl internally! |
# filter/*.pm files! It will be used to store strings in perl internally! |
46 |
return $utf2cp->convert($_[0]); |
return $utf2cp->convert($_[0]); |
47 |
} |
} |
48 |
|
|
49 |
# decode isis import codepage |
# decode isis/excel or other import codepage |
50 |
my $isis2cp; |
my $import2cp; |
51 |
|
|
52 |
# outgoing xml must be in UTF-8 |
# outgoing xml must be in UTF-8 |
53 |
my $cp2utf = Text::Iconv->new($codepage,'UTF-8'); |
my $cp2utf = Text::Iconv->new($codepage,'UTF-8'); |
54 |
|
|
55 |
sub isis2xml { |
# mapping between data type and tag which specify |
56 |
|
# format in XML file |
57 |
|
my %type2tag = ( |
58 |
|
'isis' => 'isis', |
59 |
|
'excel' => 'column' |
60 |
|
); |
61 |
|
|
62 |
|
sub data2xml { |
63 |
|
|
64 |
use xmlify; |
use xmlify; |
65 |
|
|
66 |
|
my $type = shift @_; |
67 |
my $row = shift @_; |
my $row = shift @_; |
68 |
my $add_xml = shift @_; |
my $add_xml = shift @_; |
69 |
|
|
96 |
|
|
97 |
my ($swish,$display); |
my ($swish,$display); |
98 |
|
|
99 |
foreach my $x (@{$config->{indexer}->{$field}->{isis}}) { |
my $tag = $type2tag{$type} || die "can't find which tag to use for type $type"; |
100 |
|
foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) { |
101 |
|
|
102 |
my $format = x($x->{content}); |
my $format = x($x->{content}); |
103 |
my $delimiter = x($x->{delimiter}) || ' '; |
my $delimiter = x($x->{delimiter}) || ' '; |
104 |
|
|
105 |
my $isis_i = 0; # isis repeatable offset |
my $repeat_off = 0; # repeatable offset |
106 |
|
|
107 |
my ($s,$d,$i) = (1,1,0); # swish, display default |
my ($s,$d,$i) = (1,1,0); # swish, display default |
108 |
$s = 0 if (lc($x->{type}) eq "display"); |
$s = 0 if (lc($x->{type}) eq "display"); |
119 |
# init vars so that we go into while... |
# init vars so that we go into while... |
120 |
($swish,$display) = (1,1); |
($swish,$display) = (1,1); |
121 |
|
|
122 |
while ($swish || $display) { |
if ($swish || $display) { |
123 |
($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp); |
($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp); |
|
|
|
124 |
# filter="name" ; filter this field through |
# filter="name" ; filter this field through |
125 |
# filter/[name].pm |
# filter/[name].pm |
126 |
my $filter = $x->{filter}; |
my $filter = $x->{filter}; |
220 |
|
|
221 |
########################################################################## |
########################################################################## |
222 |
|
|
223 |
|
# read configuration for this script |
224 |
my $cfg = new Config::IniFiles( -file => $config_file ); |
my $cfg = new Config::IniFiles( -file => $config_file ); |
225 |
|
|
226 |
|
# read global.conf configuration |
227 |
|
my $cfg_global = new Config::IniFiles( -file => 'global.conf' ); |
228 |
|
|
229 |
|
# open index |
230 |
|
$index = new index_DBI( |
231 |
|
$cfg_global->val('global', 'dbi_dbd'), |
232 |
|
$cfg_global->val('global', 'dbi_dsn'), |
233 |
|
$cfg_global->val('global', 'dbi_user'), |
234 |
|
$cfg_global->val('global', 'dbi_passwd') || '', |
235 |
|
); |
236 |
|
|
237 |
foreach my $database ($cfg->Sections) { |
foreach my $database ($cfg->Sections) { |
238 |
|
|
239 |
my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!"; |
my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined"; |
|
my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined"; |
|
240 |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
my $add_xml = $cfg -> val($database, 'xml'); # optional |
241 |
|
|
242 |
$config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1); |
print STDERR "reading ./import_xml/$type.xml\n"; |
243 |
|
|
244 |
|
# extract just type basic |
245 |
|
my $type_base = $type; |
246 |
|
$type_base =~ s/_.+$//g; |
247 |
|
|
248 |
$isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
$config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type_base} ], forcecontent => 1); |
249 |
|
|
250 |
my $db = OpenIsis::open( $isis_db ); |
# output current progress indicator |
251 |
if (0) { |
my $last_p = 0; |
252 |
# # FIX |
sub progress { |
253 |
# if (! $db ) { |
#return if (! $opts{q}); # FIXME |
254 |
print STDERR "WARNING: can't open '$isis_db'\n"; |
my $current = shift; |
255 |
next ; |
my $total = shift || 1; |
256 |
|
my $p = int($current * 100 / $total); |
257 |
|
if ($p != $last_p) { |
258 |
|
printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p ); |
259 |
|
$last_p = $p; |
260 |
|
} |
261 |
} |
} |
262 |
|
|
263 |
my $max_rowid = OpenIsis::maxRowid( $db ); |
# now read database |
264 |
|
print STDERR "using: $type...\n"; |
265 |
|
|
266 |
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
if ($type_base eq "isis") { |
267 |
|
|
268 |
my $path = $database; # was $isis_db |
my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!"; |
269 |
|
|
270 |
my $last_p = 0; |
$import2cp = Text::Iconv->new($config->{isis_codepage},$codepage); |
271 |
|
my $db = OpenIsis::open( $isis_db ); |
272 |
|
|
273 |
|
my $max_rowid = OpenIsis::maxRowid( $db ); |
274 |
|
|
275 |
|
print STDERR "Reading database: $isis_db [$max_rowid rows]\n"; |
276 |
|
|
277 |
|
my $path = $database; |
278 |
|
|
279 |
|
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
280 |
|
my $row = OpenIsis::read( $db, $row_id ); |
281 |
|
if ($row && $row->{mfn}) { |
282 |
|
|
283 |
|
progress($row->{mfn}, $max_rowid); |
284 |
|
|
285 |
|
my $swishpath = $path."#".int($row->{mfn}); |
286 |
|
|
287 |
# { my $row_id = 4514; |
if (my $xml = data2xml($type_base,$row,$add_xml)) { |
288 |
# FIX |
$xml = $cp2utf->convert($xml); |
289 |
for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) { |
use bytes; # as opposed to chars |
290 |
my $row = OpenIsis::read( $db, $row_id ); |
print "Path-Name: $swishpath\n"; |
291 |
if ($row && $row->{mfn}) { |
print "Content-Length: ".(length($xml)+1)."\n"; |
292 |
# output current process indicator |
print "Document-Type: XML\n\n$xml\n"; |
293 |
my $p = int($row->{mfn} * 100 / $max_rowid); |
} |
|
if ($p != $last_p) { |
|
|
printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q}); |
|
|
$last_p = $p; |
|
294 |
} |
} |
295 |
|
} |
296 |
|
print STDERR "\n"; |
297 |
|
|
298 |
|
} elsif ($type_base eq "excel") { |
299 |
|
use Spreadsheet::ParseExcel; |
300 |
|
use Spreadsheet::ParseExcel::Utility qw(int2col); |
301 |
|
|
302 |
|
$import2cp = Text::Iconv->new($config->{excel_codepage},$codepage); |
303 |
|
my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!"; |
304 |
|
|
305 |
|
my $sheet = x($config->{sheet}) || die "no sheet in $type.xml"; |
306 |
|
my $start_row = x($config->{start_row}) - 1 || die "no start_row in $type.xml"; |
307 |
|
|
308 |
|
my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'"; |
309 |
|
|
310 |
|
my $sheet_nr = 0; |
311 |
|
foreach my $oWks (@{$oBook->{Worksheet}}) { |
312 |
|
#print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n"; |
313 |
|
last if ($oWks->{Name} eq $sheet); |
314 |
|
$sheet_nr++; |
315 |
|
} |
316 |
|
|
317 |
|
my $oWorksheet = $oBook->{Worksheet}[$sheet_nr]; |
318 |
|
|
319 |
|
print STDERR "using sheet: ",$oWorksheet->{Name},"\n"; |
320 |
|
defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file"; |
321 |
|
my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow}; |
322 |
|
|
323 |
|
for(my $iR = $start_row ; defined $end_row && $iR <= $end_row ; $iR++) { |
324 |
|
my $row; |
325 |
|
for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) { |
326 |
|
my $cell = $oWorksheet->{Cells}[$iR][$iC]; |
327 |
|
if ($cell) { |
328 |
|
$row->{int2col($iC)} = $cell->Value; |
329 |
|
} |
330 |
|
} |
331 |
|
|
332 |
|
progress($iR, $end_row); |
333 |
|
|
334 |
|
# print "row[$iR/$end_row] "; |
335 |
|
# foreach (keys %{$row}) { |
336 |
|
# print "$_: ",$row->{$_},"\t"; |
337 |
|
# } |
338 |
|
# print "\n"; |
339 |
|
|
340 |
|
my $swishpath = $database."#".$iR; |
341 |
|
|
342 |
my $swishpath = $path."#".int($row->{mfn}); |
next if (! $row); |
343 |
|
|
344 |
if (my $xml = isis2xml($row,$add_xml)) { |
if (my $xml = data2xml($type,$row,$add_xml)) { |
345 |
$xml = $cp2utf->convert($xml); |
$xml = $cp2utf->convert($xml); |
346 |
use bytes; # as opposed to chars |
use bytes; # as opposed to chars |
347 |
print "Path-Name: $swishpath\n"; |
print "Path-Name: $swishpath\n"; |
350 |
} |
} |
351 |
} |
} |
352 |
} |
} |
|
print STDERR "\n"; |
|
353 |
} |
} |
354 |
|
|
355 |
# call this to commit index |
# call this to commit index |