8 use Text::Unaccent 1.02; # 1.01 won't compile on my platform,
16 $config_file =~ s/\.pl$/.conf/;
17 die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
21 use index_DBI; # there is no other, right now ;-)
28 # -m multiple directories
32 getopts('d:m:qs', \%opts);
34 my $path; # this is name of database
36 Text::Iconv->raise_error(1); # Conversion errors raise exceptions
38 # this is encoding of all files on disk, including import_xml/*.xml file and
39 # filter/*.pm files! It will be used to store strings in perl internally!
40 my $codepage = 'ISO-8859-2';
42 my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
43 # this function will convert data from XML files to local encoding
45 return $utf2cp->convert($_[0]);
48 # decode isis/excel or other import codepage
51 # outgoing xml must be in UTF-8
52 my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
54 # mapping between data type and tag which specify
67 my $add_xml = shift @_;
73 my $html = ""; # html formatted display output
75 my %field_usage; # counter for usage of each field
77 # sort subrouting using order="" attribute
79 return 0 if (! $config->{indexer}->{$a}->{order});
80 return 0 if (! $config->{indexer}->{$b}->{order});
82 return $config->{indexer}->{$a}->{order} <=>
83 $config->{indexer}->{$b}->{order} ;
86 foreach my $field (sort by_order keys %{$config->{indexer}}) {
90 $field_usage{$field}++;
93 my $display_data = "";
98 my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";
99 foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
101 my $format = x($x->{content});
102 my $delimiter = x($x->{delimiter}) || ' ';
104 my $repeat_off = 0; # repeatable offset
106 my ($s,$d,$i) = (1,1,0); # swish, display default
107 $s = 0 if (lc($x->{type}) eq "display");
108 $d = 0 if (lc($x->{type}) eq "swish");
109 ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
111 # what will separate last line from this one?
112 if ($display_data && $x->{append} && $x->{append} eq "1") {
113 $line_delimiter = ' ';
114 } elsif ($display_data) {
115 $line_delimiter = '<br/>';
118 # init vars so that we go into while...
119 ($swish,$display) = (1,1);
121 if ($swish || $display) {
122 ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
123 # filter="name" ; filter this field through
125 my $filter = $x->{filter};
127 require "filter/".$filter.".pm";
129 # type="swish" ; field for swish
133 $swish_data .= join(" ",&$filter($swish));
135 $swish_data .= $swish;
139 # type="display" ; field for display
140 if ($d && $display) {
141 if ($line_delimiter && $display_data) {
142 $display_data .= $line_delimiter;
143 undef $line_delimiter;
147 $display_data .= join($delimiter,&$filter($display));
150 $display_data .= $delimiter.$display;
152 $display_data .= $display;
157 # type="index" ; insert into index
158 if ($i && $display) {
159 my $index_data = $display;
162 foreach my $d (&$filter($index_data)) {
163 $index->insert($field, $d, $path);
166 $index->insert($field, $index_data, $path);
175 if ($field eq "headline") {
176 $xml .= xmlify("headline", $display_data);
179 # find field name (signular, plural)
181 if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
182 $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
183 } elsif ($config->{indexer}->{$field}->{name_plural}) {
184 $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
185 } elsif ($config->{indexer}->{$field}->{name}) {
186 $field_name = $config->{indexer}->{$field}->{name}."#-#";
188 print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
191 $html .= x($field_name);
193 $html .= $display_data."###\n";
197 # remove extra spaces
198 $swish_data =~ s/ +/ /g;
199 $swish_data =~ s/ +$//g;
201 $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
207 # dump formatted output in <html>
209 $xml .= xmlify("html",$html);
213 $xml .= $add_xml if ($add_xml);
214 return "<xml>\n$xml</xml>\n";
220 ##########################################################################
222 # read configuration for this script
223 my $cfg = new Config::IniFiles( -file => $config_file );
225 # read global.conf configuration
226 my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
229 $index = new index_DBI(
230 $cfg_global->val('global', 'dbi_dbd'),
231 $cfg_global->val('global', 'dbi_dsn'),
232 $cfg_global->val('global', 'dbi_user'),
233 $cfg_global->val('global', 'dbi_passwd') || '',
236 foreach my $database ($cfg->Sections) {
238 my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
239 my $add_xml = $cfg -> val($database, 'xml'); # optional
241 print STDERR "reading ./import_xml/$type.xml\n";
243 $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type} ], forcecontent => 1);
245 # output current progress indicator
248 #return if (! $opts{q}); # FIXME
251 my $p = int($current * 100 / $total);
253 printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );
259 print STDERR "using: $type...\n";
261 if ($type eq "isis") {
262 my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
264 $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
265 my $db = OpenIsis::open( $isis_db );
267 my $max_rowid = OpenIsis::maxRowid( $db );
269 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
271 my $path = $database;
273 for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
274 my $row = OpenIsis::read( $db, $row_id );
275 if ($row && $row->{mfn}) {
277 progress($row->{mfn}, $max_rowid);
279 my $swishpath = $path."#".int($row->{mfn});
281 if (my $xml = data2xml($type,$row,$add_xml)) {
282 $xml = $cp2utf->convert($xml);
283 use bytes; # as opposed to chars
284 print "Path-Name: $swishpath\n";
285 print "Content-Length: ".(length($xml)+1)."\n";
286 print "Document-Type: XML\n\n$xml\n";
292 } elsif ($type eq "excel") {
293 use Spreadsheet::ParseExcel;
294 use Spreadsheet::ParseExcel::Utility qw(int2col);
296 $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);
297 my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";
299 my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
300 my $start_row = x($config->{start_row}) || die "no start_row in $type.xml";
302 my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
305 foreach my $oWks (@{$oBook->{Worksheet}}) {
306 #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";
307 last if ($oWks->{Name} eq $sheet);
311 my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];
313 print STDERR "using sheet: ",$oWorksheet->{Name},"\n";
314 defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file";
315 my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow};
317 for(my $iR = $oWorksheet->{MinRow} ; defined $end_row && $iR <= $end_row ; $iR++) {
319 for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
320 my $cell = $oWorksheet->{Cells}[$iR][$iC];
322 $row->{int2col($iC)} = $cell->Value;
326 progress($iR, $end_row);
328 # print "row[$iR/$end_row] ";
329 # foreach (keys %{$row}) {
330 # print "$_: ",$row->{$_},"\t";
334 my $swishpath = $database."#".$iR;
338 if (my $xml = data2xml($type,$row,$add_xml)) {
339 $xml = $cp2utf->convert($xml);
340 use bytes; # as opposed to chars
341 print "Path-Name: $swishpath\n";
342 print "Content-Length: ".(length($xml)+1)."\n";
343 print "Document-Type: XML\n\n$xml\n";
349 # call this to commit index
354 ##########################################################################
358 isis2xml.pl - read isis file and dump XML
362 This command will read ISIS data file using OpenIsis perl module and
363 create XML file for usage with I<SWISH-E>
364 indexer. Dispite it's name, this script B<isn't general xml generator>
365 from isis files (isis allready has something like that). Output of this
366 script is tailor-made for SWISH-E.
370 Dobrica Pavlinusic <dpavlin@rot13.org>
374 GNU Public License (GPL) v2 or later
378 SWISH-E web site at http://www.swish-e.org