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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 21 - (hide annotations)
Sun Feb 23 00:00:51 2003 UTC (16 years, 9 months ago) by dpavlin
Original Path: trunk/all2xml.pl
File MIME type: text/plain
File size: 7225 byte(s)
fix

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 = Text::Iconv->new($config->{isis_codepage},'UTF8');
44     #my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
45     my $isis_codepage;
46     my $index_codepage;
47 dpavlin 10 my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');
48 dpavlin 13 my $xml_codepage;
49 dpavlin 10
50 dpavlin 3 sub isis2xml {
51    
52 dpavlin 10 use xmlify;
53    
54 dpavlin 3 my $row = shift @_;
55 dpavlin 13 my $add_xml = shift @_;
56 dpavlin 3
57     my $xml;
58    
59 dpavlin 10 use parse_format;
60 dpavlin 3
61 dpavlin 13 my $html = ""; # html formatted display output
62 dpavlin 10
63 dpavlin 13 my %field_usage; # counter for usage of each field
64    
65 dpavlin 3 foreach my $field (keys %{$config->{indexer}}) {
66    
67 dpavlin 13 $field_usage{$field}++;
68    
69 dpavlin 10 my $swish_data = "";
70 dpavlin 3 my $display_data = "";
71    
72     foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
73    
74     my $format = $x->{content};
75 dpavlin 10 my ($s,$d,$i) = (1,1,0); # swish, display default
76 dpavlin 9 $s = 0 if (lc($x->{type}) eq "display");
77     $d = 0 if (lc($x->{type}) eq "swish");
78     ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
79     #print STDERR "## s: $s d: $d i: $i ## $format ##\n";
80 dpavlin 3
81 dpavlin 20 # FIX: this is ugly, UGLY, cludge string is returned
82     # in UTF8 encoding , but as if source charset
83     # is ISO8859-1 and not some other. This break other
84     # character encodings, so we convert it first
85     # back to ISO8859-1
86 dpavlin 10 $format = $cludge_codepage->convert($format);
87 dpavlin 20
88 dpavlin 10 my ($swish,$display) = parse_format($format,$row);
89     #print STDERR "s: $swish\nd: $display\n" if ($swish);
90 dpavlin 3
91 dpavlin 10 #print STDERR "swish: $swish<-- display: $display<--\n";
92    
93 dpavlin 20 # filter="name" ; filter this field through
94     # filter/[name].pm
95     my $filter;
96     if ($x->{filter}) {
97     $filter = "filter/".$x->{filter}.".pm";
98     require $filter;
99     }
100     # type="swish" ; field for swish
101     if ($s && $swish) {
102     if ($filter) {
103     $swish_data .= join(" ",&filter($swish));
104     } else {
105     $swish_data .= $swish if ($s && $swish);
106     }
107     }
108 dpavlin 17
109 dpavlin 20 # type="display" ; field for display
110     if ($d && $display) {
111     if ($filter) {
112     $display_data .= join(" ",&filter($display));
113     } else {
114 dpavlin 21 $display_data .= $display;
115 dpavlin 20 }
116     }
117    
118     # type="index" ; insert into index
119 dpavlin 17 if ($i && $display) {
120     my $index_data = $index_codepage->convert($display) || $display;
121 dpavlin 20 if ($filter) {
122     foreach my $d (&filter($index_data)) {
123     $index->insert($field, $d, $db_dir);
124     }
125     } else {
126     $index->insert($field, $index_data, $db_dir);
127     }
128 dpavlin 17 }
129 dpavlin 3 }
130 dpavlin 9
131 dpavlin 13
132 dpavlin 10 #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
133     if ($display_data) {
134 dpavlin 13 $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";
135     # FIX: this is removed and replaced by html tag.
136     #$xml .= xmlify($field."_display", $display_data);
137    
138     if ($field eq "headline") {
139     $xml .= xmlify("headline", $display_data);
140     } else {
141    
142     # find field name (signular, plural)
143     my $field_name = "";
144     if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
145     $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
146     } elsif ($config->{indexer}->{$field}->{name_plural}) {
147     $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
148 dpavlin 20 } elsif ($config->{indexer}->{$field}->{name}) {
149     $field_name = $config->{indexer}->{$field}->{name}."#-#";
150 dpavlin 13 } else {
151 dpavlin 20 print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
152 dpavlin 13 }
153     if ($field_name) {
154     $html .= $xml_codepage->convert($field_name);
155     }
156     $html .= $display_data."###\n";
157     }
158 dpavlin 10 }
159     if ($swish_data) {
160     my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
161 dpavlin 20 # remove extra spaces
162     $swish_data =~ s/ +/ /g;
163     $swish_data =~ s/ +$//g;
164    
165 dpavlin 10 $swish_data = $i->convert($swish_data);
166     $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));
167     #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;
168     #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));
169     }
170    
171 dpavlin 9
172 dpavlin 3 }
173 dpavlin 13
174     # dump formatted output in <html>
175     if ($html) {
176     $xml .= xmlify("html",$html);
177     }
178    
179 dpavlin 3 if ($xml) {
180 dpavlin 10 #print STDERR "x: $xml\n";
181 dpavlin 13 $xml .= $add_xml if ($add_xml);
182 dpavlin 10 return "<xml>\n$xml</xml>\n";
183 dpavlin 3 } else {
184     return;
185     }
186     }
187    
188     ##########################################################################
189    
190 dpavlin 13 my $cfg = new Config::IniFiles( -file => $config_file );
191 dpavlin 1
192 dpavlin 13 foreach my $database ($cfg->Sections) {
193 dpavlin 1
194 dpavlin 13 my $isis_db = $cfg -> val($database, 'isis_db');
195     my $type = $cfg -> val($database, 'type');
196     my $add_xml = $cfg -> val($database, 'xml');
197 dpavlin 1
198 dpavlin 13 # read configuration for this type
199     $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
200     $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');
201     $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
202     $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),'UTF8');
203 dpavlin 1
204 dpavlin 3 my $db = OpenIsis::open( $isis_db );
205     if (0) {
206     # # FIX
207     # if (! $db ) {
208     print STDERR "WARNING: can't open '$isis_db'\n";
209     next ;
210     }
211    
212 dpavlin 1 my $max_rowid = OpenIsis::maxRowid( $db );
213    
214 dpavlin 3 print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
215 dpavlin 1
216 dpavlin 13 my $path = $database; # was $isis_db
217 dpavlin 10
218 dpavlin 3 my $last_p = 0;
219    
220 dpavlin 10 # { my $row_id = 4514;
221 dpavlin 3 # FIX
222 dpavlin 1 for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
223     my $row = OpenIsis::read( $db, $row_id );
224 dpavlin 3 if ($row && $row->{mfn}) {
225 dpavlin 10 #print STDERR "mfn: ",$row->{mfn},"\n";
226 dpavlin 3 # output current process indicator
227     my $p = int($row->{mfn} * 100 / $max_rowid);
228     if ($p != $last_p) {
229     printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});
230     $last_p = $p;
231     }
232    
233 dpavlin 13 if (my $xml = isis2xml($row,$add_xml)) {
234 dpavlin 10 #print STDERR "--ret-->$xml\n";
235     print "Path-Name: $path#".int($row->{mfn})."\n";
236     print "Content-Length: ".(length($xml)+1)."\n";
237     print "Document-Type: XML\n\n$xml\n";
238 dpavlin 3 }
239 dpavlin 1 }
240     }
241 dpavlin 3 print STDERR "\n";
242 dpavlin 1 }
243 dpavlin 3
244 dpavlin 10 # call this to commit index
245     $index->close;
246 dpavlin 3
247     1;
248     __END__
249     ##########################################################################
250    
251     =head1 NAME
252    
253     isis2xml.pl - read isis file and dump XML
254    
255     =head1 DESCRIPTION
256    
257     This command will read ISIS data file using OpenIsis perl module and
258     create XML file for usage with I<SWISH-E>
259     indexer. Dispite it's name, this script B<isn't general xml generator>
260     from isis files (isis allready has something like that). Output of this
261     script is tailor-made for SWISH-E.
262    
263     =head1 AUTHOR
264    
265     Dobrica Pavlinusic <dpavlin@rot13.org>
266    
267     =head1 COPYRIGHT
268    
269     GNU Public License (GPL) v2 or later
270    
271     =head1 SEE ALSO
272    
273     SWISH-E web site at http://www.swish-e.org
274    
275     =cut

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26