/[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 5 by dpavlin, Sat Jan 11 06:14:48 2003 UTC revision 50 by dpavlin, Sun Jun 1 13:46:42 2003 UTC
# Line 6  use Getopt::Std; Line 6  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,  use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,
9  require Unicode::Map8;  use Text::Iconv;
10    use Config::IniFiles;
11    use Encode;
12    
13  my $config=XMLin(undef, forcearray => [ 'isis' ], forcecontent => 1);  $|=1;
14    
15  my %opts;  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    my $config;
20    
21  getopts('d:m:q', \%opts);  use index_DBI;  # there is no other, right now ;-)
22    my $index;
23    
24  my $db_dir = $opts{d} || "ps";  # FIX  my %opts;
25    
26  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  # usage:
27    #       -d directory name
28    #       -m multiple directories
29    #       -q quiet
30    #       -s run swish
31    
32    getopts('d:m:qs', \%opts);
33    
34    my $path;       # this is name of database
35    
36    Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
37    
38    # this is encoding of all files on disk, including import_xml/*.xml file and
39    # filter/*.pm files! It will be used to store strings in perl internally!
40    my $codepage = 'ISO-8859-2';
41    
42    my $utf2cp = Text::Iconv->new('UTF-8',$codepage);
43    # this function will convert data from XML files to local encoding
44    sub x {
45            return $utf2cp->convert($_[0]);
46    }
47    
48  #print Dumper($config->{indexer});  # decode isis import codepage
49  #print "-" x 70,"\n";  my $isis2cp;
50    
51  # how to convert isis code page to UTF8?  # outgoing xml must be in UTF-8
52  my $isis_map = Unicode::Map8->new($config->{isis_codepage}) || die;  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
53    
54  sub isis2xml {  sub isis2xml {
55    
56            use xmlify;
57    
58          my $row = shift @_;          my $row = shift @_;
59            my $add_xml = shift @_;
60    
61          my $xml;          my $xml;
62    
63          sub isis_sf {          use parse_format;
64                  my $row = shift @_;  
65                  my $isis_id = shift @_;          my $html = "";          # html formatted display output
66                  my $subfield = shift @_;  
67                  if ($row->{$isis_id}->[0]) {          my %field_usage;        # counter for usage of each field
68                          my $sf = OpenIsis::subfields($row->{$isis_id}->[0]);  
69                          if (! defined $subfield || length($subfield) == 0) {          # sort subrouting using order="" attribute
70                                  # subfield list undef, empty or no defined subfields for this record          sub by_order {
71                                  my $all_sf = $row->{$isis_id}->[0];                  return 0 if (! $config->{indexer}->{$a}->{order});
72                                  $all_sf =~ s/\^./ /g;   nuke definirions                  return 0 if (! $config->{indexer}->{$b}->{order});
73                                  return $all_sf;  
74                          } elsif ($sf->{$subfield}) {                  return $config->{indexer}->{$a}->{order} <=>
75                                  return $sf->{$subfield};                          $config->{indexer}->{$b}->{order} ;
                         }  
                 }  
76          }          }
77    
78          foreach my $field (keys %{$config->{indexer}}) {          foreach my $field (sort by_order keys %{$config->{indexer}}) {
79    
80                    $field=x($field);
81    
82                    $field_usage{$field}++;
83    
84                    my $swish_data = "";
85                  my $display_data = "";                  my $display_data = "";
86                  my $index_data = "";                  my $line_delimiter;
87    
88                    my ($swish,$display);
89    
90                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
91    
92                          my $display_tmp = "";                          my $format = x($x->{content});
93                          my $index_tmp = "";                          my $delimiter = x($x->{delimiter}) || ' ';
94    
95                            my $isis_i = 0;         # isis repeatable offset
96    
97                          my $format = $x->{content};                          my ($s,$d,$i) = (1,1,0);        # swish, display default
98                          my $i = 1;      # index only                          $s = 0 if (lc($x->{type}) eq "display");
99                          my $d = 1;      # display only                          $d = 0 if (lc($x->{type}) eq "swish");
100                          $i = 0 if (lc($x->{type}) eq "display");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
101                          $d = 0 if (lc($x->{type}) eq "index");  
102  #print "## i: $i d: $d ## $format ##";                            # what will separate last line from this one?
103                          # parse format                          if ($display_data && $x->{append} && $x->{append} eq "1") {
104                          my $prefix = "";                                  $line_delimiter = ' ';
105                          if ($format =~ s/^([^\d]+)//) {                          } elsif ($display_data) {
106                                  $prefix = $1;                                  $line_delimiter = '<br/>';
107                          }                          }
108                          while ($format) {  
109                                  if ($format =~ s/^(\d\d\d)(\w?)//) {                          # init vars so that we go into while...
110                                          my $isis_tmp = isis_sf($row,$1,$2);                          ($swish,$display) = (1,1);
111                                          if ($isis_tmp) {  
112  #                                               $display_tmp .= $prefix . "/$1/$2/".$isis_tmp if ($d);                          while ($swish || $display) {
113                                                  $display_tmp .= $prefix . $isis_tmp if ($d);                                  ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);
114                                                  $index_tmp .= $isis_tmp." " if ($i);  
115  #print " $isis_tmp <--\n";                                  # filter="name" ; filter this field through
116                                    # filter/[name].pm
117                                    my $filter = $x->{filter};
118                                    if ($filter) {
119                                            require "filter/".$filter.".pm";
120                                    }
121                                    # type="swish" ; field for swish
122                                    if ($s && $swish) {
123                                            if ($filter) {
124                                                    no strict 'refs';
125                                                    $swish_data .= join(" ",&$filter($swish));
126                                            } else {
127                                                    $swish_data .= $swish;
128                                          }                                          }
129                                          $prefix = "";                                  }
130                                  } elsif ($format =~ s/^([^\d]+)//) {  
131                                          $prefix = $1;                                  # type="display" ; field for display
132                                  } else {                                  if ($d && $display) {
133                                          print STDERR "WARNING: unparsed format '$format'\n";                                          if ($line_delimiter && $display_data) {
134                                          last;                                                  $display_data .= $line_delimiter;
135                                  };                                                  undef $line_delimiter;
136                                            }
137                                            if ($filter) {
138                                                    no strict 'refs';
139                                                    $display_data .= join($delimiter,&$filter($display));
140                                            } else {
141                                                    if ($display_data) {
142                                                            $display_data .= $delimiter.$display;
143                                                    } else {
144                                                            $display_data .= $display;
145                                                    }
146                                            }
147                                    }
148                                                    
149                                    # type="index" ; insert into index
150                                    if ($i && $display) {
151                                            my $index_data = $display;
152                                            if ($filter) {
153                                                    no strict 'refs';
154                                                    foreach my $d (&$filter($index_data)) {
155                                                            $index->insert($field, $d, $path);
156                                                    }
157                                            } else {
158                                                    $index->insert($field, $index_data, $path);
159                                            }
160                                    }
161                          }                          }
162                          # add suffix                  }
163                          $display_tmp .= $prefix if ($display_tmp);  
164    
165  #                       $display_data .= $display_tmp if ($display_tmp ne "");                  if ($display_data) {
 #                       $index_data .= $index_tmp if ($index_tmp ne "");  
                         $display_data .= $display_tmp;  
                         $index_data .= $index_tmp;  
166    
167                            if ($field eq "headline") {
168                                    $xml .= xmlify("headline", $display_data);
169                            } else {
170    
171                                    # find field name (signular, plural)
172                                    my $field_name = "";
173                                    if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {
174                                            $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";
175                                    } elsif ($config->{indexer}->{$field}->{name_plural}) {
176                                            $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";
177                                    } elsif ($config->{indexer}->{$field}->{name}) {
178                                            $field_name = $config->{indexer}->{$field}->{name}."#-#";
179                                    } else {
180                                            print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
181                                    }
182                                    if ($field_name) {
183                                            $html .= x($field_name);
184                                    }
185                                    $html .= $display_data."###\n";
186                            }
187                  }                  }
188  #print "--display:$display_data\n--index:$index_data\n";                  if ($swish_data) {
189                  $xml->{$field}->{display} .= $isis_map->tou($display_data)->utf8 if ($display_data);                          # remove extra spaces
190                  $xml->{$field}->{index} .= unac_string($config->{isis_codepage},$index_data) if ($index_data);                          $swish_data =~ s/ +/ /g;
191                                    $swish_data =~ s/ +$//g;
192    
193                            $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
194                    }
195    
196    
197          }          }
198    
199            # dump formatted output in <html>
200            if ($html) {
201                    $xml .= xmlify("html",$html);
202            }
203            
204          if ($xml) {          if ($xml) {
205                  return XMLout($xml, rootname => 'xml', noattr => 1 );                  $xml .= $add_xml if ($add_xml);
206                    return "<xml>\n$xml</xml>\n";
207          } else {          } else {
208                  return;                  return;
209          }          }
# Line 108  sub isis2xml { Line 211  sub isis2xml {
211    
212  ##########################################################################  ##########################################################################
213    
214  my $last_tell=0;  my $cfg = new Config::IniFiles( -file => $config_file );
215    
216  my @isis_dirs = ( '.' );        # use dirname as database name  # open index
217    $index = new index_DBI(
218                    $cfg->val('global', 'dbi_dbd'),
219                    $cfg->val('global', 'dbi_dsn'),
220                    $cfg->val('global', 'dbi_user'),
221                    $cfg->val('global', 'dbi_passwd') || '',
222            );
223    
224  if ($opts{m}) {  # delete [global] section to leave just databases sections
225          @isis_dirs = split(/,/,$opts{m});  $cfg->DeleteSection('global');
 }  
226    
227  my @isis_dbs;  foreach my $database ($cfg->Sections) {
228    
229  foreach (@isis_dirs) {          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
230          if (-e $config->{isis_data}."/$db_dir/$_/LIBRI") {          my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";
231                  push @isis_dbs,$config->{isis_data}."/$db_dir/$_/LIBRI/LIBRI";          my $add_xml = $cfg -> val($database, 'xml');    # optional
         }  
         if (-e $config->{isis_data}."/$db_dir/$_/PERI") {  
                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/PERI/PERI";  
         }  
         if (-e $config->{isis_data}."/$db_dir/$_/AMS") {  
                 push @isis_dbs,$config->{isis_data}."/$db_dir/$_/AMS/AMS";  
         }  
         if (-e $config->{isis_data}."/$db_dir/$_/ARTI") {  
 #               push @isis_dbs,$config->{isis_data}."/$db_dir/$_/ARTI/ARTI";  
         }  
 }  
232    
233  print STDERR "FATAL: Can't find isis database.\nPerhaps isis_data (".$config->{isis_data}.") has wrong value?\n" if (! @isis_dbs);          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
   
 my $db;  
   
 foreach my $isis_db (@isis_dbs) {  
234    
235            $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
236    
237          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
238          if (0) {          if (0) {
# Line 152  foreach my $isis_db (@isis_dbs) { Line 246  foreach my $isis_db (@isis_dbs) {
246    
247          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";
248    
249            my $path = $database;                   # was $isis_db
250    
251          my $last_p = 0;          my $last_p = 0;
252    
253  #       { my $row_id = 1;  #       { my $row_id = 4514;
254  # FIX  # FIX
255          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {          for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {
256                  my $row = OpenIsis::read( $db, $row_id );                  my $row = OpenIsis::read( $db, $row_id );
257                  if ($row && $row->{mfn}) {                  if ($row && $row->{mfn}) {
   
258                          # output current process indicator                          # output current process indicator
259                          my $p = int($row->{mfn} * 100 / $max_rowid);                          my $p = int($row->{mfn} * 100 / $max_rowid);
260                          if ($p != $last_p) {                          if ($p != $last_p) {
# Line 167  foreach my $isis_db (@isis_dbs) { Line 262  foreach my $isis_db (@isis_dbs) {
262                                  $last_p = $p;                                  $last_p = $p;
263                          }                          }
264    
265                          if (my $xml = isis2xml($row)) {                          my $swishpath = $path."#".int($row->{mfn});
266                                  print "Path-Name: $isis_db#".$row->{mfn}."\n";  
267                            if (my $xml = isis2xml($row,$add_xml)) {
268                                    $xml = $cp2utf->convert($xml);
269                                    use bytes;      # as opposed to chars
270                                    print "Path-Name: $swishpath\n";
271                                  print "Content-Length: ".(length($xml)+1)."\n";                                  print "Content-Length: ".(length($xml)+1)."\n";
272                                  print "Document-Type: XML\n\n$xml\n";                                  print "Document-Type: XML\n\n$xml\n";
273                          }                          }
# Line 177  foreach my $isis_db (@isis_dbs) { Line 276  foreach my $isis_db (@isis_dbs) {
276          print STDERR "\n";          print STDERR "\n";
277  }  }
278    
279    # call this to commit index
280    $index->close;
281    
282  1;  1;
283  __END__  __END__

Legend:
Removed from v.5  
changed lines
  Added in v.50

  ViewVC Help
Powered by ViewVC 1.1.26