/[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 56 - (hide annotations)
Wed Jun 25 12:09:27 2003 UTC (16 years, 3 months ago) by dpavlin
File MIME type: text/plain
File size: 9727 byte(s)
use start_row from excel.xml

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 1
13 dpavlin 10 $|=1;
14 dpavlin 9
15 dpavlin 13 my $config_file = $0;
16     $config_file =~ s/\.pl$/.conf/;
17     die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
18    
19 dpavlin 10 my $config;
20    
21     use index_DBI; # there is no other, right now ;-)
22 dpavlin 50 my $index;
23 dpavlin 10
24 dpavlin 1 my %opts;
25    
26 dpavlin 7 # usage:
27     # -d directory name
28     # -m multiple directories
29     # -q quiet
30     # -s run swish
31 dpavlin 1
32 dpavlin 7 getopts('d:m:qs', \%opts);
33    
34 dpavlin 43 my $path; # this is name of database
35 dpavlin 1
36 dpavlin 40 Text::Iconv->raise_error(1); # Conversion errors raise exceptions
37 dpavlin 1
38 dpavlin 40 # 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';
41 dpavlin 1
42 dpavlin 40 my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
43     # this function will convert data from XML files to local encoding
44     sub x {
45     return $utf2cp->convert($_[0]);
46     }
47 dpavlin 3
48 dpavlin 54 # decode isis/excel or other import codepage
49     my $import2cp;
50 dpavlin 10
51 dpavlin 40 # outgoing xml must be in UTF-8
52     my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
53 dpavlin 29
54 dpavlin 54 # mapping between data type and tag which specify
55     # format in XML file
56     my %type2tag = (
57     'isis' => 'isis',
58     'excel' => 'column'
59     );
60 dpavlin 3
61 dpavlin 54 sub data2xml {
62    
63 dpavlin 10 use xmlify;
64    
65 dpavlin 54 my $type = shift @_;
66 dpavlin 3 my $row = shift @_;
67 dpavlin 13 my $add_xml = shift @_;
68 dpavlin 3
69     my $xml;
70    
71 dpavlin 10 use parse_format;
72 dpavlin 3
73 dpavlin 13 my $html = ""; # html formatted display output
74 dpavlin 10
75 dpavlin 13 my %field_usage; # counter for usage of each field
76    
77 dpavlin 32 # sort subrouting using order="" attribute
78     sub by_order {
79     return 0 if (! $config->{indexer}->{$a}->{order});
80     return 0 if (! $config->{indexer}->{$b}->{order});
81 dpavlin 29
82 dpavlin 32 return $config->{indexer}->{$a}->{order} <=>
83     $config->{indexer}->{$b}->{order} ;
84     }
85 dpavlin 3
86 dpavlin 32 foreach my $field (sort by_order keys %{$config->{indexer}}) {
87    
88 dpavlin 40 $field=x($field);
89    
90 dpavlin 13 $field_usage{$field}++;
91    
92 dpavlin 10 my $swish_data = "";
93 dpavlin 3 my $display_data = "";
94 dpavlin 35 my $line_delimiter;
95 dpavlin 3
96 dpavlin 34 my ($swish,$display);
97    
98 dpavlin 54 my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";
99     foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {
100 dpavlin 3
101 dpavlin 40 my $format = x($x->{content});
102     my $delimiter = x($x->{delimiter}) || ' ';
103 dpavlin 3
104 dpavlin 54 my $repeat_off = 0; # repeatable offset
105 dpavlin 29
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");
110    
111 dpavlin 35 # what will separate last line from this one?
112     if ($display_data && $x->{append} && $x->{append} eq "1") {
113 dpavlin 34 $line_delimiter = ' ';
114 dpavlin 35 } elsif ($display_data) {
115 dpavlin 34 $line_delimiter = '<br/>';
116     }
117 dpavlin 29
118 dpavlin 34 # init vars so that we go into while...
119     ($swish,$display) = (1,1);
120 dpavlin 29
121 dpavlin 54 if ($swish || $display) {
122     ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);
123 dpavlin 29 # filter="name" ; filter this field through
124     # filter/[name].pm
125     my $filter = $x->{filter};
126 dpavlin 20 if ($filter) {
127 dpavlin 29 require "filter/".$filter.".pm";
128 dpavlin 20 }
129 dpavlin 29 # type="swish" ; field for swish
130     if ($s && $swish) {
131     if ($filter) {
132     no strict 'refs';
133     $swish_data .= join(" ",&$filter($swish));
134     } else {
135     $swish_data .= $swish;
136     }
137     }
138 dpavlin 17
139 dpavlin 29 # type="display" ; field for display
140     if ($d && $display) {
141 dpavlin 35 if ($line_delimiter && $display_data) {
142     $display_data .= $line_delimiter;
143     undef $line_delimiter;
144     }
145 dpavlin 29 if ($filter) {
146     no strict 'refs';
147     $display_data .= join($delimiter,&$filter($display));
148     } else {
149     if ($display_data) {
150     $display_data .= $delimiter.$display;
151     } else {
152     $display_data .= $display;
153     }
154     }
155 dpavlin 20 }
156 dpavlin 29
157     # type="index" ; insert into index
158     if ($i && $display) {
159 dpavlin 40 my $index_data = $display;
160 dpavlin 29 if ($filter) {
161     no strict 'refs';
162     foreach my $d (&$filter($index_data)) {
163 dpavlin 43 $index->insert($field, $d, $path);
164 dpavlin 29 }
165     } else {
166 dpavlin 43 $index->insert($field, $index_data, $path);
167 dpavlin 20 }
168     }
169 dpavlin 17 }
170 dpavlin 3 }
171 dpavlin 9
172 dpavlin 13
173 dpavlin 10 if ($display_data) {
174 dpavlin 29
175 dpavlin 13 if ($field eq "headline") {
176     $xml .= xmlify("headline", $display_data);
177     } else {
178    
179     # find field name (signular, plural)
180     my $field_name = "";
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 dpavlin 20 } elsif ($config->{indexer}->{$field}->{name}) {
186     $field_name = $config->{indexer}->{$field}->{name}."#-#";
187 dpavlin 13 } else {
188 dpavlin 20 print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
189 dpavlin 13 }
190     if ($field_name) {
191 dpavlin 40 $html .= x($field_name);
192 dpavlin 13 }
193     $html .= $display_data."###\n";
194     }
195 dpavlin 10 }
196     if ($swish_data) {
197 dpavlin 20 # remove extra spaces
198     $swish_data =~ s/ +/ /g;
199     $swish_data =~ s/ +$//g;
200    
201 dpavlin 40 $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
202 dpavlin 10 }
203    
204 dpavlin 9
205 dpavlin 3 }
206 dpavlin 13
207     # dump formatted output in <html>
208     if ($html) {
209     $xml .= xmlify("html",$html);
210     }
211    
212 dpavlin 3 if ($xml) {
213 dpavlin 13 $xml .= $add_xml if ($add_xml);
214 dpavlin 10 return "<xml>\n$xml</xml>\n";
215 dpavlin 3 } else {
216     return;
217     }
218     }
219    
220     ##########################################################################
221    
222 dpavlin 54 # read configuration for this script
223 dpavlin 13 my $cfg = new Config::IniFiles( -file => $config_file );
224 dpavlin 1
225 dpavlin 54 # read global.conf configuration
226     my $cfg_global = new Config::IniFiles( -file => 'global.conf' );
227    
228 dpavlin 50 # open index
229     $index = new index_DBI(
230 dpavlin 54 $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') || '',
234 dpavlin 50 );
235    
236 dpavlin 13 foreach my $database ($cfg->Sections) {
237 dpavlin 1
238 dpavlin 54 my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";
239 dpavlin 40 my $add_xml = $cfg -> val($database, 'xml'); # optional
240 dpavlin 1
241 dpavlin 54 print STDERR "reading ./import_xml/$type.xml\n";
242 dpavlin 1
243 dpavlin 54 $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type} ], forcecontent => 1);
244 dpavlin 40
245 dpavlin 54 # output current progress indicator
246     my $last_p = 0;
247     sub progress {
248     #return if (! $opts{q}); # FIXME
249     my $current = shift;
250     my $total = shift;
251     my $p = int($current * 100 / $total);
252     if ($p != $last_p) {
253     printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );
254     $last_p = $p;
255     }
256 dpavlin 3 }
257    
258 dpavlin 54 # now read database
259     print STDERR "using: $type...\n";
260 dpavlin 1
261 dpavlin 54 if ($type eq "isis") {
262     my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
263 dpavlin 1
264 dpavlin 54 $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
265     my $db = OpenIsis::open( $isis_db );
266 dpavlin 10
267 dpavlin 54 my $max_rowid = OpenIsis::maxRowid( $db );
268 dpavlin 3
269 dpavlin 54 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
270    
271     my $path = $database;
272    
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}) {
276    
277     progress($row->{mfn}, $max_rowid);
278    
279     my $swishpath = $path."#".int($row->{mfn});
280    
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";
287     }
288 dpavlin 3 }
289 dpavlin 54 }
290     print STDERR "\n";
291 dpavlin 3
292 dpavlin 54 } elsif ($type eq "excel") {
293     use Spreadsheet::ParseExcel;
294     use Spreadsheet::ParseExcel::Utility qw(int2col);
295    
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!";
298 dpavlin 43
299 dpavlin 54 my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";
300 dpavlin 56 my $start_row = x($config->{start_row}) - 1 || die "no start_row in $type.xml";
301 dpavlin 54
302     my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";
303    
304     my $sheet_nr = 0;
305     foreach my $oWks (@{$oBook->{Worksheet}}) {
306     #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";
307     last if ($oWks->{Name} eq $sheet);
308     $sheet_nr++;
309     }
310    
311     my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];
312    
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};
316    
317 dpavlin 56 for(my $iR = $start_row ; defined $end_row && $iR <= $end_row ; $iR++) {
318 dpavlin 54 my $row;
319     for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {
320     my $cell = $oWorksheet->{Cells}[$iR][$iC];
321     if ($cell) {
322     $row->{int2col($iC)} = $cell->Value;
323     }
324     }
325    
326     progress($iR, $end_row);
327    
328     # print "row[$iR/$end_row] ";
329     # foreach (keys %{$row}) {
330     # print "$_: ",$row->{$_},"\t";
331     # }
332     # print "\n";
333    
334     my $swishpath = $database."#".$iR;
335    
336     next if (! $row);
337    
338     if (my $xml = data2xml($type,$row,$add_xml)) {
339 dpavlin 44 $xml = $cp2utf->convert($xml);
340 dpavlin 35 use bytes; # as opposed to chars
341 dpavlin 43 print "Path-Name: $swishpath\n";
342 dpavlin 10 print "Content-Length: ".(length($xml)+1)."\n";
343     print "Document-Type: XML\n\n$xml\n";
344 dpavlin 3 }
345 dpavlin 1 }
346     }
347     }
348 dpavlin 3
349 dpavlin 10 # call this to commit index
350     $index->close;
351 dpavlin 3
352     1;
353     __END__
354     ##########################################################################
355    
356     =head1 NAME
357    
358     isis2xml.pl - read isis file and dump XML
359    
360     =head1 DESCRIPTION
361    
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.
367    
368     =head1 AUTHOR
369    
370     Dobrica Pavlinusic <dpavlin@rot13.org>
371    
372     =head1 COPYRIGHT
373    
374     GNU Public License (GPL) v2 or later
375    
376     =head1 SEE ALSO
377    
378     SWISH-E web site at http://www.swish-e.org
379    
380     =cut

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26