/[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 50 - (hide annotations)
Sun Jun 1 13:46:42 2003 UTC (16 years, 5 months ago) by dpavlin
Original Path: trunk/all2xml.pl
File MIME type: text/plain
File size: 7470 byte(s)
move database arguments to .conf file

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 40 # decode isis import codepage
49     my $isis2cp;
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 3 sub isis2xml {
55    
56 dpavlin 10 use xmlify;
57    
58 dpavlin 3 my $row = shift @_;
59 dpavlin 13 my $add_xml = shift @_;
60 dpavlin 3
61     my $xml;
62    
63 dpavlin 10 use parse_format;
64 dpavlin 3
65 dpavlin 13 my $html = ""; # html formatted display output
66 dpavlin 10
67 dpavlin 13 my %field_usage; # counter for usage of each field
68    
69 dpavlin 32 # sort subrouting using order="" attribute
70     sub by_order {
71     return 0 if (! $config->{indexer}->{$a}->{order});
72     return 0 if (! $config->{indexer}->{$b}->{order});
73 dpavlin 29
74 dpavlin 32 return $config->{indexer}->{$a}->{order} <=>
75     $config->{indexer}->{$b}->{order} ;
76     }
77 dpavlin 3
78 dpavlin 32 foreach my $field (sort by_order keys %{$config->{indexer}}) {
79    
80 dpavlin 40 $field=x($field);
81    
82 dpavlin 13 $field_usage{$field}++;
83    
84 dpavlin 10 my $swish_data = "";
85 dpavlin 3 my $display_data = "";
86 dpavlin 35 my $line_delimiter;
87 dpavlin 3
88 dpavlin 34 my ($swish,$display);
89    
90 dpavlin 3 foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
91    
92 dpavlin 40 my $format = x($x->{content});
93     my $delimiter = x($x->{delimiter}) || ' ';
94 dpavlin 3
95 dpavlin 29 my $isis_i = 0; # isis repeatable offset
96    
97     my ($s,$d,$i) = (1,1,0); # swish, display default
98     $s = 0 if (lc($x->{type}) eq "display");
99     $d = 0 if (lc($x->{type}) eq "swish");
100     ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
101    
102 dpavlin 35 # what will separate last line from this one?
103     if ($display_data && $x->{append} && $x->{append} eq "1") {
104 dpavlin 34 $line_delimiter = ' ';
105 dpavlin 35 } elsif ($display_data) {
106 dpavlin 34 $line_delimiter = '<br/>';
107     }
108 dpavlin 29
109 dpavlin 34 # init vars so that we go into while...
110     ($swish,$display) = (1,1);
111 dpavlin 29
112     while ($swish || $display) {
113 dpavlin 40 ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);
114 dpavlin 3
115 dpavlin 29 # filter="name" ; filter this field through
116     # filter/[name].pm
117     my $filter = $x->{filter};
118 dpavlin 20 if ($filter) {
119 dpavlin 29 require "filter/".$filter.".pm";
120 dpavlin 20 }
121 dpavlin 29 # type="swish" ; field for swish
122     if ($s && $swish) {
123     if ($filter) {
124     no strict 'refs';
125     $swish_data .= join(" ",&$filter($swish));
126     } else {
127     $swish_data .= $swish;
128     }
129     }
130 dpavlin 17
131 dpavlin 29 # type="display" ; field for display
132     if ($d && $display) {
133 dpavlin 35 if ($line_delimiter && $display_data) {
134     $display_data .= $line_delimiter;
135     undef $line_delimiter;
136     }
137 dpavlin 29 if ($filter) {
138     no strict 'refs';
139     $display_data .= join($delimiter,&$filter($display));
140     } else {
141     if ($display_data) {
142     $display_data .= $delimiter.$display;
143     } else {
144     $display_data .= $display;
145     }
146     }
147 dpavlin 20 }
148 dpavlin 29
149     # type="index" ; insert into index
150     if ($i && $display) {
151 dpavlin 40 my $index_data = $display;
152 dpavlin 29 if ($filter) {
153     no strict 'refs';
154     foreach my $d (&$filter($index_data)) {
155 dpavlin 43 $index->insert($field, $d, $path);
156 dpavlin 29 }
157     } else {
158 dpavlin 43 $index->insert($field, $index_data, $path);
159 dpavlin 20 }
160     }
161 dpavlin 17 }
162 dpavlin 3 }
163 dpavlin 9
164 dpavlin 13
165 dpavlin 10 if ($display_data) {
166 dpavlin 29
167 dpavlin 13 if ($field eq "headline") {
168     $xml .= xmlify("headline", $display_data);
169     } else {
170    
171     # find field name (signular, plural)
172     my $field_name = "";
173     if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
174     $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
175     } elsif ($config->{indexer}->{$field}->{name_plural}) {
176     $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
177 dpavlin 20 } elsif ($config->{indexer}->{$field}->{name}) {
178     $field_name = $config->{indexer}->{$field}->{name}."#-#";
179 dpavlin 13 } else {
180 dpavlin 20 print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
181 dpavlin 13 }
182     if ($field_name) {
183 dpavlin 40 $html .= x($field_name);
184 dpavlin 13 }
185     $html .= $display_data."###\n";
186     }
187 dpavlin 10 }
188     if ($swish_data) {
189 dpavlin 20 # remove extra spaces
190     $swish_data =~ s/ +/ /g;
191     $swish_data =~ s/ +$//g;
192    
193 dpavlin 40 $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
194 dpavlin 10 }
195    
196 dpavlin 9
197 dpavlin 3 }
198 dpavlin 13
199     # dump formatted output in <html>
200     if ($html) {
201     $xml .= xmlify("html",$html);
202     }
203    
204 dpavlin 3 if ($xml) {
205 dpavlin 13 $xml .= $add_xml if ($add_xml);
206 dpavlin 10 return "<xml>\n$xml</xml>\n";
207 dpavlin 3 } else {
208     return;
209     }
210     }
211    
212     ##########################################################################
213    
214 dpavlin 13 my $cfg = new Config::IniFiles( -file => $config_file );
215 dpavlin 1
216 dpavlin 50 # open index
217     $index = new index_DBI(
218     $cfg->val('global', 'dbi_dbd'),
219     $cfg->val('global', 'dbi_dsn'),
220     $cfg->val('global', 'dbi_user'),
221     $cfg->val('global', 'dbi_passwd') || '',
222     );
223    
224     # delete [global] section to leave just databases sections
225     $cfg->DeleteSection('global');
226    
227 dpavlin 13 foreach my $database ($cfg->Sections) {
228 dpavlin 1
229 dpavlin 40 my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
230     my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";
231     my $add_xml = $cfg -> val($database, 'xml'); # optional
232 dpavlin 1
233 dpavlin 13 $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
234 dpavlin 1
235 dpavlin 40 $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
236    
237 dpavlin 3 my $db = OpenIsis::open( $isis_db );
238     if (0) {
239     # # FIX
240     # if (! $db ) {
241     print STDERR "WARNING: can't open '$isis_db'\n";
242     next ;
243     }
244    
245 dpavlin 1 my $max_rowid = OpenIsis::maxRowid( $db );
246    
247 dpavlin 3 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
248 dpavlin 1
249 dpavlin 13 my $path = $database; # was $isis_db
250 dpavlin 10
251 dpavlin 3 my $last_p = 0;
252    
253 dpavlin 10 # { my $row_id = 4514;
254 dpavlin 3 # FIX
255 dpavlin 1 for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
256     my $row = OpenIsis::read( $db, $row_id );
257 dpavlin 3 if ($row && $row->{mfn}) {
258     # output current process indicator
259     my $p = int($row->{mfn} * 100 / $max_rowid);
260     if ($p != $last_p) {
261     printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});
262     $last_p = $p;
263     }
264    
265 dpavlin 43 my $swishpath = $path."#".int($row->{mfn});
266    
267 dpavlin 44 if (my $xml = isis2xml($row,$add_xml)) {
268     $xml = $cp2utf->convert($xml);
269 dpavlin 35 use bytes; # as opposed to chars
270 dpavlin 43 print "Path-Name: $swishpath\n";
271 dpavlin 10 print "Content-Length: ".(length($xml)+1)."\n";
272     print "Document-Type: XML\n\n$xml\n";
273 dpavlin 3 }
274 dpavlin 1 }
275     }
276 dpavlin 3 print STDERR "\n";
277 dpavlin 1 }
278 dpavlin 3
279 dpavlin 10 # call this to commit index
280     $index->close;
281 dpavlin 3
282     1;
283     __END__
284     ##########################################################################
285    
286     =head1 NAME
287    
288     isis2xml.pl - read isis file and dump XML
289    
290     =head1 DESCRIPTION
291    
292     This command will read ISIS data file using OpenIsis perl module and
293     create XML file for usage with I<SWISH-E>
294     indexer. Dispite it's name, this script B<isn't general xml generator>
295     from isis files (isis allready has something like that). Output of this
296     script is tailor-made for SWISH-E.
297    
298     =head1 AUTHOR
299    
300     Dobrica Pavlinusic <dpavlin@rot13.org>
301    
302     =head1 COPYRIGHT
303    
304     GNU Public License (GPL) v2 or later
305    
306     =head1 SEE ALSO
307    
308     SWISH-E web site at http://www.swish-e.org
309    
310     =cut

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26