/[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 42 - (hide annotations)
Sat Mar 15 21:48:48 2003 UTC (16 years, 8 months ago) by dpavlin
Original Path: trunk/all2xml.pl
File MIME type: text/plain
File size: 7188 byte(s)
filter fix && optimisation

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26