/[webpac]/trunk2/all2all.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 /trunk2/all2all.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 35 - (hide annotations)
Sun Feb 23 15:47:40 2003 UTC (16 years, 8 months ago) by dpavlin
Original Path: trunk/all2xml.pl
File MIME type: text/plain
File size: 8276 byte(s)
last changes; completly broken charsets

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 1
12 dpavlin 10 $|=1;
13 dpavlin 9
14 dpavlin 13 my $config_file = $0;
15     $config_file =~ s/\.pl$/.conf/;
16     die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);
17    
18 dpavlin 10 my $config;
19    
20     use index_DBI; # there is no other, right now ;-)
21    
22     my $index = new index_DBI(); # open index
23    
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 13 my $db_dir;
35 dpavlin 1
36 dpavlin 3 #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);
37 dpavlin 1
38 dpavlin 3 #print Dumper($config->{indexer});
39     #print "-" x 70,"\n";
40 dpavlin 1
41 dpavlin 10 Text::Iconv->raise_error(1); # Conversion errors raise exceptions
42 dpavlin 3
43 dpavlin 13 my $isis_codepage;
44     my $index_codepage;
45 dpavlin 10 my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');
46 dpavlin 13 my $xml_codepage;
47 dpavlin 10
48 dpavlin 29 my $XML_CHARSET = 'UTF8';
49    
50    
51 dpavlin 3 sub isis2xml {
52    
53 dpavlin 10 use xmlify;
54    
55 dpavlin 3 my $row = shift @_;
56 dpavlin 13 my $add_xml = shift @_;
57 dpavlin 3
58     my $xml;
59    
60 dpavlin 10 use parse_format;
61 dpavlin 3
62 dpavlin 13 my $html = ""; # html formatted display output
63 dpavlin 10
64 dpavlin 13 my %field_usage; # counter for usage of each field
65    
66 dpavlin 32 # sort subrouting using order="" attribute
67     sub by_order {
68     return 0 if (! $config->{indexer}->{$a}->{order});
69     return 0 if (! $config->{indexer}->{$b}->{order});
70 dpavlin 29
71 dpavlin 32 return $config->{indexer}->{$a}->{order} <=>
72     $config->{indexer}->{$b}->{order} ;
73     }
74 dpavlin 3
75 dpavlin 32 foreach my $field (sort by_order keys %{$config->{indexer}}) {
76    
77 dpavlin 13 $field_usage{$field}++;
78    
79 dpavlin 10 my $swish_data = "";
80 dpavlin 3 my $display_data = "";
81 dpavlin 35 my $line_delimiter;
82 dpavlin 3
83 dpavlin 34 my ($swish,$display);
84    
85 dpavlin 3 foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
86    
87     my $format = $x->{content};
88 dpavlin 29 my $delimiter = $x->{delimiter} || ' ';
89 dpavlin 3
90 dpavlin 20 # FIX: this is ugly, UGLY, cludge string is returned
91     # in UTF8 encoding , but as if source charset
92     # is ISO8859-1 and not some other. This break other
93     # character encodings, so we convert it first
94     # back to ISO8859-1
95 dpavlin 29 $format = $xml_codepage->convert($format);
96     $delimiter = $xml_codepage->convert($delimiter) if ($delimiter);
97 dpavlin 20
98 dpavlin 29 my $isis_i = 0; # isis repeatable offset
99    
100     my ($s,$d,$i) = (1,1,0); # swish, display default
101     $s = 0 if (lc($x->{type}) eq "display");
102     $d = 0 if (lc($x->{type}) eq "swish");
103     ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
104     #print STDERR "## s: $s d: $d i: $i ## $format ##\n";
105    
106 dpavlin 35 # what will separate last line from this one?
107     if ($display_data && $x->{append} && $x->{append} eq "1") {
108 dpavlin 34 $line_delimiter = ' ';
109 dpavlin 35 } elsif ($display_data) {
110 dpavlin 34 $line_delimiter = '<br/>';
111     }
112 dpavlin 29
113 dpavlin 34 # init vars so that we go into while...
114     ($swish,$display) = (1,1);
115 dpavlin 29
116     while ($swish || $display) {
117     ($swish,$display) = parse_format($format,$row,$isis_i++);
118 dpavlin 10 #print STDERR "s: $swish\nd: $display\n" if ($swish);
119 dpavlin 3
120 dpavlin 10 #print STDERR "swish: $swish<-- display: $display<--\n";
121    
122 dpavlin 29 # filter="name" ; filter this field through
123     # filter/[name].pm
124     my $filter = $x->{filter};
125 dpavlin 20 if ($filter) {
126 dpavlin 29 require "filter/".$filter.".pm";
127 dpavlin 20 }
128 dpavlin 29 # type="swish" ; field for swish
129     if ($s && $swish) {
130     if ($filter) {
131 dpavlin 32 #print STDERR "using filter '$filter'\n";
132 dpavlin 29 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     my $index_data = $index_codepage->convert($display) || $display;
160     if ($filter) {
161     no strict 'refs';
162     foreach my $d (&$filter($index_data)) {
163     $index->insert($field, $d, $db_dir);
164     }
165     } else {
166     $index->insert($field, $index_data, $db_dir);
167 dpavlin 20 }
168     }
169 dpavlin 17 }
170 dpavlin 3 }
171 dpavlin 9
172 dpavlin 13
173 dpavlin 10 #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
174     if ($display_data) {
175 dpavlin 29
176 dpavlin 35 # $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";
177 dpavlin 13 # FIX: this is removed and replaced by html tag.
178     #$xml .= xmlify($field."_display", $display_data);
179    
180     if ($field eq "headline") {
181     $xml .= xmlify("headline", $display_data);
182     } else {
183    
184     # find field name (signular, plural)
185     my $field_name = "";
186     if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
187     $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
188     } elsif ($config->{indexer}->{$field}->{name_plural}) {
189     $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
190 dpavlin 20 } elsif ($config->{indexer}->{$field}->{name}) {
191     $field_name = $config->{indexer}->{$field}->{name}."#-#";
192 dpavlin 13 } else {
193 dpavlin 20 print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
194 dpavlin 13 }
195     if ($field_name) {
196     $html .= $xml_codepage->convert($field_name);
197     }
198     $html .= $display_data."###\n";
199     }
200 dpavlin 10 }
201     if ($swish_data) {
202     my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
203 dpavlin 20 # remove extra spaces
204     $swish_data =~ s/ +/ /g;
205     $swish_data =~ s/ +$//g;
206    
207 dpavlin 10 $swish_data = $i->convert($swish_data);
208     $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
209     #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
210     #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));
211     }
212    
213 dpavlin 9
214 dpavlin 3 }
215 dpavlin 13
216     # dump formatted output in <html>
217     if ($html) {
218     $xml .= xmlify("html",$html);
219     }
220    
221 dpavlin 3 if ($xml) {
222 dpavlin 10 #print STDERR "x: $xml\n";
223 dpavlin 13 $xml .= $add_xml if ($add_xml);
224 dpavlin 10 return "<xml>\n$xml</xml>\n";
225 dpavlin 3 } else {
226     return;
227     }
228     }
229    
230     ##########################################################################
231    
232 dpavlin 13 my $cfg = new Config::IniFiles( -file => $config_file );
233 dpavlin 1
234 dpavlin 13 foreach my $database ($cfg->Sections) {
235 dpavlin 1
236 dpavlin 13 my $isis_db = $cfg -> val($database, 'isis_db');
237     my $type = $cfg -> val($database, 'type');
238     my $add_xml = $cfg -> val($database, 'xml');
239 dpavlin 1
240 dpavlin 13 # read configuration for this type
241     $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
242 dpavlin 29 $isis_codepage = Text::Iconv->new($config->{isis_codepage},$XML_CHARSET);
243 dpavlin 13 $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
244 dpavlin 29 $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),$XML_CHARSET);
245 dpavlin 1
246 dpavlin 3 my $db = OpenIsis::open( $isis_db );
247     if (0) {
248     # # FIX
249     # if (! $db ) {
250     print STDERR "WARNING: can't open '$isis_db'\n";
251     next ;
252     }
253    
254 dpavlin 1 my $max_rowid = OpenIsis::maxRowid( $db );
255    
256 dpavlin 3 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
257 dpavlin 1
258 dpavlin 13 my $path = $database; # was $isis_db
259 dpavlin 10
260 dpavlin 3 my $last_p = 0;
261    
262 dpavlin 10 # { my $row_id = 4514;
263 dpavlin 3 # FIX
264 dpavlin 1 for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
265     my $row = OpenIsis::read( $db, $row_id );
266 dpavlin 3 if ($row && $row->{mfn}) {
267 dpavlin 10 #print STDERR "mfn: ",$row->{mfn},"\n";
268 dpavlin 3 # output current process indicator
269     my $p = int($row->{mfn} * 100 / $max_rowid);
270     if ($p != $last_p) {
271     printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});
272     $last_p = $p;
273     }
274    
275 dpavlin 13 if (my $xml = isis2xml($row,$add_xml)) {
276 dpavlin 10 #print STDERR "--ret-->$xml\n";
277 dpavlin 35 use bytes; # as opposed to chars
278 dpavlin 10 print "Path-Name: $path#".int($row->{mfn})."\n";
279     print "Content-Length: ".(length($xml)+1)."\n";
280     print "Document-Type: XML\n\n$xml\n";
281 dpavlin 3 }
282 dpavlin 1 }
283     }
284 dpavlin 3 print STDERR "\n";
285 dpavlin 1 }
286 dpavlin 3
287 dpavlin 10 # call this to commit index
288     $index->close;
289 dpavlin 3
290     1;
291     __END__
292     ##########################################################################
293    
294     =head1 NAME
295    
296     isis2xml.pl - read isis file and dump XML
297    
298     =head1 DESCRIPTION
299    
300     This command will read ISIS data file using OpenIsis perl module and
301     create XML file for usage with I<SWISH-E>
302     indexer. Dispite it's name, this script B<isn't general xml generator>
303     from isis files (isis allready has something like that). Output of this
304     script is tailor-made for SWISH-E.
305    
306     =head1 AUTHOR
307    
308     Dobrica Pavlinusic <dpavlin@rot13.org>
309    
310     =head1 COPYRIGHT
311    
312     GNU Public License (GPL) v2 or later
313    
314     =head1 SEE ALSO
315    
316     SWISH-E web site at http://www.swish-e.org
317    
318     =cut

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26