/[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

Diff of /trunk/all2xml.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by dpavlin, Sun Nov 24 20:52:11 2002 UTC revision 34 by dpavlin, Sun Feb 23 08:06:07 2003 UTC
# Line 5  use OpenIsis; Line 5  use OpenIsis;
5  use Getopt::Std;  use Getopt::Std;
6  use Data::Dumper;  use Data::Dumper;
7  use XML::Simple;  use XML::Simple;
8    use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,
9    use Text::Iconv;
10    use Config::IniFiles;
11    
12  my $config=XMLin();  $|=1;
13    
14  print Dumper($config);  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    my $config;
19    
20    use index_DBI;  # there is no other, right now ;-)
21    
22    my $index = new index_DBI();    # open index
23    
24  my %opts;  my %opts;
25    
26  getopts('d:m:q', \%opts);  # usage:
27    #       -d directory name
28    #       -m multiple directories
29    #       -q quiet
30    #       -s run swish
31    
32  my $db_dir = $opts{d};  getopts('d:m:qs', \%opts);
33    
34  die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  my $db_dir;
35    
36  #--------------------------------------------------------------------  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);
37    
38  my $last_tell=0;  #print Dumper($config->{indexer});
39    #print "-" x 70,"\n";
40    
41  my @isis_dirs = ( '.' );        # use dirname as database name  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
42    
43  if ($opts{m}) {  my $isis_codepage;
44          @isis_dirs = split(/,/,$opts{m});  my $index_codepage;
45  }  my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');
46    my $xml_codepage;
47    
48    my $XML_CHARSET = 'UTF8';
49    
50    
51    sub isis2xml {
52    
53            use xmlify;
54    
55            my $row = shift @_;
56            my $add_xml = shift @_;
57    
58            my $xml;
59    
60            use parse_format;
61    
62            my $html = "";          # html formatted display output
63    
64  my @isis_dbs;          my %field_usage;        # counter for usage of each field
65    
66  foreach (@isis_dirs) {          # sort subrouting using order="" attribute
67          if (-e "$common::isis_data/$db_dir/$_/LIBRI") {          sub by_order {
68                  push @isis_dbs,"$common::isis_data/$db_dir/$_/LIBRI/LIBRI";                  return 0 if (! $config->{indexer}->{$a}->{order});
69                    return 0 if (! $config->{indexer}->{$b}->{order});
70    
71                    return $config->{indexer}->{$a}->{order} <=>
72                            $config->{indexer}->{$b}->{order} ;
73          }          }
74          if (-e "$common::isis_data/$db_dir/$_/PERI") {  
75                  push @isis_dbs,"$common::isis_data/$db_dir/$_/PERI/PERI";          foreach my $field (sort by_order keys %{$config->{indexer}}) {
76    
77                    $field_usage{$field}++;
78    
79                    my $swish_data = "";
80                    my $display_data = "";
81                    my $line_delimiter = "";
82    
83                    my ($swish,$display);
84    
85                    foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
86    
87                            my $format = $x->{content};
88                            my $delimiter = $x->{delimiter} || ' ';
89    
90                            # 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                            $format = $xml_codepage->convert($format);
96                            $delimiter = $xml_codepage->convert($delimiter) if ($delimiter);
97    
98                            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                            if ($x->{append}) {
107                                    $line_delimiter = ' ';
108                            } else {
109                                    $line_delimiter = '<br/>';
110                            }
111    
112                            $display_data .= $line_delimiter if ($display_data && $display);
113                            # init vars so that we go into while...
114                            ($swish,$display) = (1,1);
115    
116                            while ($swish || $display) {
117                                    ($swish,$display) = parse_format($format,$row,$isis_i++);
118    #print STDERR "s: $swish\nd: $display\n" if ($swish);
119    
120    #print STDERR "swish: $swish<-- display: $display<--\n";
121    
122                                    # filter="name" ; filter this field through
123                                    # filter/[name].pm
124                                    my $filter = $x->{filter};
125                                    if ($filter) {
126                                            require "filter/".$filter.".pm";
127                                    }
128                                    # type="swish" ; field for swish
129                                    if ($s && $swish) {
130                                            if ($filter) {
131    #print STDERR "using filter '$filter'\n";
132                                                    no strict 'refs';
133                                                    $swish_data .= join(" ",&$filter($swish));
134                                            } else {
135                                                    $swish_data .= $swish;
136                                            }
137                                    }
138    
139                                    # type="display" ; field for display
140                                    if ($d && $display) {
141                                            if ($filter) {
142                                                    no strict 'refs';
143                                                    $display_data .= join($delimiter,&$filter($display));
144                                            } else {
145                                                    if ($display_data) {
146                                                            $display_data .= $delimiter.$display;
147                                                    } else {
148                                                            $display_data .= $display;
149                                                    }
150                                            }
151                                    }
152                                                    
153                                    # type="index" ; insert into index
154                                    if ($i && $display) {
155                                            my $index_data = $index_codepage->convert($display) || $display;
156                                            if ($filter) {
157                                                    no strict 'refs';
158                                                    foreach my $d (&$filter($index_data)) {
159                                                            $index->insert($field, $d, $db_dir);
160                                                    }
161                                            } else {
162                                                    $index->insert($field, $index_data, $db_dir);
163                                            }
164                                    }
165                            }
166                    }
167    
168    
169    #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);
170                    if ($display_data) {
171    
172                            # remove last <br>
173                            $display_data =~ s/$line_delimiter$//;
174    
175                            $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";
176                            # FIX: this is removed and replaced by html tag.
177                            #$xml .= xmlify($field."_display", $display_data);
178    
179                            if ($field eq "headline") {
180                                    $xml .= xmlify("headline", $display_data);
181                            } else {
182    
183                                    # find field name (signular, plural)
184                                    my $field_name = "";
185                                    if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
186                                            $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
187                                    } elsif ($config->{indexer}->{$field}->{name_plural}) {
188                                            $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
189                                    } elsif ($config->{indexer}->{$field}->{name}) {
190                                            $field_name = $config->{indexer}->{$field}->{name}."#-#";
191                                    } else {
192                                            print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
193                                    }
194                                    if ($field_name) {
195                                            $html .= $xml_codepage->convert($field_name);
196    #                                       $html .= "-->".$field_name."<--";
197                                    }
198                                    $html .= $display_data."###\n";
199                            }
200                    }
201                    if ($swish_data) {
202                            my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');
203                            # remove extra spaces
204                            $swish_data =~ s/ +/ /g;
205                            $swish_data =~ s/ +$//g;
206    
207                            $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    
214          }          }
215          if (-e "$common::isis_data/$db_dir/$_/AMS") {  
216                  push @isis_dbs,"$common::isis_data/$db_dir/$_/AMS/AMS";          # dump formatted output in <html>
217            if ($html) {
218                    $xml .= xmlify("html",$html);
219          }          }
220          if (-e "$common::isis_data/$db_dir/$_/ARTI") {          
221  #               push @isis_dbs,"$common::isis_data/$db_dir/$_/ARTI/ARTI";          if ($xml) {
222    #print STDERR "x: $xml\n";
223                    $xml .= $add_xml if ($add_xml);
224                    return "<xml>\n$xml</xml>\n";
225            } else {
226                    return;
227          }          }
228  }  }
229    
230  foreach my $isis_db (@isis_dbs) {  ##########################################################################
231    
232          my $db = OpenIsis::open( "$isis_db" ) || warn "can't open '$isis_db'";  my $cfg = new Config::IniFiles( -file => $config_file );
233    
234    foreach my $database ($cfg->Sections) {
235    
236            my $isis_db = $cfg -> val($database, 'isis_db');
237            my $type = $cfg -> val($database, 'type');
238            my $add_xml = $cfg -> val($database, 'xml');
239    
240            # read configuration for this type
241            $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
242            $isis_codepage = Text::Iconv->new($config->{isis_codepage},$XML_CHARSET);
243            $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});
244            $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),$XML_CHARSET);
245    
246            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          my $max_rowid = OpenIsis::maxRowid( $db );          my $max_rowid = OpenIsis::maxRowid( $db );
255    
256          my $last_pcnt = 0;          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
257    
258            my $path = $database;                   # was $isis_db
259    
260            my $last_p = 0;
261    
262    #       { my $row_id = 4514;
263    # FIX
264          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
265                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
266                    if ($row && $row->{mfn}) {
267                  # output current process indicator  #print STDERR "mfn: ",$row->{mfn},"\n";
268                  my $pcnt = int($row->{mfn} * 100 / $max_rowid);                          # output current process indicator
269                  if ($pcnt != $last_pcnt) {                          my $p = int($row->{mfn} * 100 / $max_rowid);
270                          printf STDERR ("%5d / %5d -- %-2d %%\n",$row->{mfn},$max_rowid,$pcnt) if (! $opts{q});                          if ($p != $last_p) {
271                          $last_pcnt = $pcnt;                                  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                            if (my $xml = isis2xml($row,$add_xml)) {
276    #print STDERR "--ret-->$xml\n";
277                                    print "Path-Name: $path#".int($row->{mfn})."\n";
278                                    print "Content-Length: ".(length($xml)+1)."\n";
279                                    print "Document-Type: XML\n\n$xml\n";
280                            }
281                  }                  }
282          }          }
283            print STDERR "\n";
284  }  }
285    
286    # call this to commit index
287    $index->close;
288    
289    1;
290    __END__
291    ##########################################################################
292    
293    =head1 NAME
294    
295    isis2xml.pl - read isis file and dump XML
296    
297    =head1 DESCRIPTION
298    
299    This command will read ISIS data file using OpenIsis perl module and
300    create XML file for usage with I<SWISH-E>
301    indexer. Dispite it's name, this script B<isn't general xml generator>
302    from isis files (isis allready has something like that). Output of this
303    script is tailor-made for SWISH-E.
304    
305    =head1 AUTHOR
306    
307    Dobrica Pavlinusic <dpavlin@rot13.org>
308    
309    =head1 COPYRIGHT
310    
311    GNU Public License (GPL) v2 or later
312    
313    =head1 SEE ALSO
314    
315    SWISH-E web site at http://www.swish-e.org
316    
317    =cut

Legend:
Removed from v.1  
changed lines
  Added in v.34

  ViewVC Help
Powered by ViewVC 1.1.26