major modifications to produce first (non-working) version of Web CGI
[webpac] / all2xml.pl
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 use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,
9 require Unicode::Map8;
10
11 my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);
12
13 my %opts;
14
15 # usage:
16 #       -d directory name
17 #       -m multiple directories
18 #       -q quiet
19 #       -s run swish
20
21 getopts('d:m:qs', \%opts);
22
23 my $db_dir = $opts{d} || "ps";  # FIX
24
25 #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);
26
27 #print Dumper($config->{indexer});
28 #print "-" x 70,"\n";
29
30 # how to convert isis code page to UTF8?
31 my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;
32
33 sub isis2xml {
34
35         my $row = shift @_;
36
37         my $xml;
38         $xml->{db_dir} = [ $db_dir ];   # FIX remove?
39
40         sub isis_sf {
41                 my $row = shift @_;
42                 my $isis_id = shift @_;
43                 my $subfield = shift @_;
44                 if ($row->{$isis_id}->[0]) {
45                         my $sf = OpenIsis::subfields($row->{$isis_id}->[0]);
46                         if (! defined $subfield || length($subfield) == 0) {
47                                 # subfield list undef, empty or no defined subfields for this record
48                                 my $all_sf = $row->{$isis_id}->[0];
49                                 $all_sf =~ s/\^./ /g;   nuke definirions
50                                 return $all_sf;
51                         } elsif ($sf->{$subfield}) {
52                                 return $sf->{$subfield};
53                         }
54                 }
55         }
56
57         foreach my $field (keys %{$config->{indexer}}) {
58
59                 my $display_data = "";
60                 my $index_data = "";
61
62                 foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
63
64                         my $display_tmp = "";
65                         my $index_tmp = "";
66
67                         my $format = $x->{content};
68                         my $i = 1;      # index only
69                         my $d = 1;      # display only
70                         $i = 0 if (lc($x->{type}) eq "display");
71                         $d = 0 if (lc($x->{type}) eq "index");
72 #print "## i: $i d: $d ## $format ##";  
73                         # parse format
74                         my $prefix = "";
75                         if ($format =~ s/^([^\d]+)//) {
76                                 $prefix = $1;
77                         }
78                         while ($format) {
79                                 if ($format =~ s/^(\d\d\d)(\w?)//) {
80                                         my $isis_tmp = isis_sf($row,$1,$2);
81                                         if ($isis_tmp) {
82 #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);
83                                                 $display_tmp .= $prefix . $isis_tmp if ($d);
84                                                 $index_tmp .= $isis_tmp." " if ($i);
85 #print " $isis_tmp <--\n";
86                                         }
87                                         $prefix = "";
88                                 } elsif ($format =~ s/^([^\d]+)//) {
89                                         $prefix = $1;
90                                 } else {
91                                         print STDERR "WARNING: unparsed format '$format'\n";
92                                         last;
93                                 };
94                         }
95                         # add suffix
96                         $display_tmp .= $prefix if ($display_tmp);
97
98 #                       $display_data .= $display_tmp if ($display_tmp ne "");
99 #                       $index_data .= $index_tmp if ($index_tmp ne "");
100                         $display_data .= $display_tmp;
101                         $index_data .= $index_tmp;
102
103                 }
104 #print "--display:$display_data\n--index:$index_data\n";
105                 #$xml->{$field."_display"} = $isis_map->tou($display_data)->utf8 if ($display_data);
106                 #$xml->{$field."_index"} = unac_string($config->{isis_codepage},$index_data) if ($index_data);
107                 $xml->{$field."_display" } = [ $isis_map->tou($display_data)->utf8 ] if ($display_data);
108                 $xml->{$field."_index"} = [ unac_string($config->{isis_codepage},$index_data)." jabuka" ] if ($index_data);
109         
110         }
111         if ($xml) {
112                 return XMLout($xml, rootname => 'xml', keeproot => 0, noattr => 0 );
113         } else {
114                 return;
115         }
116 }
117
118 ##########################################################################
119
120 my $last_tell=0;
121
122 my @isis_dirs = ( '.' );        # use dirname as database name
123
124 if ($opts{m}) {
125         @isis_dirs = split(/,/,$opts{m});
126 }
127
128 my @isis_dbs;
129
130 foreach (@isis_dirs) {
131         if (-e $config->{isis_data}."/$db_dir/$_/LIBRI") {
132                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/LIBRI/LIBRI";
133         }
134         if (-e $config->{isis_data}."/$db_dir/$_/PERI") {
135                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/PERI/PERI";
136         }
137         if (-e $config->{isis_data}."/$db_dir/$_/AMS") {
138                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";
139         }
140         if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {
141 #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";
142         }
143 }
144
145 print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);
146
147 my $db;
148
149 foreach my $isis_db (@isis_dbs) {
150
151
152         my $db = OpenIsis::open( $isis_db );
153         if (0) {
154 #       # FIX
155 #       if (! $db ) {
156                 print STDERR "WARNING: can't open '$isis_db'\n";
157                 next ;
158         }
159
160         my $max_rowid = OpenIsis::maxRowid( $db );
161
162         print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
163
164         my $last_p = 0;
165
166 #       { my $row_id = 1;
167 # FIX
168         for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
169                 my $row = OpenIsis::read( $db, $row_id );
170                 if ($row && $row->{mfn}) {
171
172                         # output current process indicator
173                         my $p = int($row->{mfn} * 100 / $max_rowid);
174                         if ($p != $last_p) {
175                                 printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});
176                                 $last_p = $p;
177                         }
178
179                         if (my $xml = isis2xml($row)) {
180                                 my $path = $isis_db;
181                                 $path =~ s#$config->{isis_data}/*##g;
182                                 my $out = "Path-Name: $path#".$row->{mfn}."\n";
183                                 $out .= "Content-Length: ".(length($xml)+1)."\n";
184                                 $out .= "Document-Type: XML\n\n$xml\n";
185                                 print $out;
186                         }
187                 }
188         }
189         print STDERR "\n";
190 }
191
192
193 1;
194 __END__
195 ##########################################################################
196
197 =head1 NAME
198
199 isis2xml.pl - read isis file and dump XML
200
201 =head1 DESCRIPTION
202
203 This command will read ISIS data file using OpenIsis perl module and
204 create XML file for usage with I<SWISH-E>
205 indexer. Dispite it's name, this script B<isn't general xml generator>
206 from isis files (isis allready has something like that). Output of this
207 script is tailor-made for SWISH-E.
208
209 =head1 AUTHOR
210
211 Dobrica Pavlinusic <dpavlin@rot13.org>
212
213 =head1 COPYRIGHT
214
215 GNU Public License (GPL) v2 or later
216
217 =head1 SEE ALSO
218
219 SWISH-E web site at http://www.swish-e.org
220
221 =cut