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

Diff of /trunk2/all2xml.pl

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

revision 34 by dpavlin, Sun Feb 23 08:06:07 2003 UTC revision 50 by dpavlin, Sun Jun 1 13:46:42 2003 UTC
# Line 8  use XML::Simple; Line 8  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  use Text::Iconv;  use Text::Iconv;
10  use Config::IniFiles;  use Config::IniFiles;
11    use Encode;
12    
13  $|=1;  $|=1;
14    
# Line 18  die "FATAL: can't find configuration fil Line 19  die "FATAL: can't find configuration fil
19  my $config;  my $config;
20    
21  use index_DBI;  # there is no other, right now ;-)  use index_DBI;  # there is no other, right now ;-)
22    my $index;
 my $index = new index_DBI();    # open index  
23    
24  my %opts;  my %opts;
25    
# Line 31  my %opts; Line 31  my %opts;
31    
32  getopts('d:m:qs', \%opts);  getopts('d:m:qs', \%opts);
33    
34  my $db_dir;  my $path;       # this is name of database
   
 #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  
   
 #print Dumper($config->{indexer});  
 #print "-" x 70,"\n";  
35    
36  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
37    
38  my $isis_codepage;  # this is encoding of all files on disk, including import_xml/*.xml file and
39  my $index_codepage;  # filter/*.pm files! It will be used to store strings in perl internally!
40  my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');  my $codepage = 'ISO-8859-2';
41  my $xml_codepage;  
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  my $XML_CHARSET = 'UTF8';  # decode isis import codepage
49    my $isis2cp;
50    
51    # outgoing xml must be in UTF-8
52    my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
53    
54  sub isis2xml {  sub isis2xml {
55    
# Line 74  sub isis2xml { Line 77  sub isis2xml {
77    
78          foreach my $field (sort by_order keys %{$config->{indexer}}) {          foreach my $field (sort by_order keys %{$config->{indexer}}) {
79    
80                    $field=x($field);
81    
82                  $field_usage{$field}++;                  $field_usage{$field}++;
83    
84                  my $swish_data = "";                  my $swish_data = "";
85                  my $display_data = "";                  my $display_data = "";
86                  my $line_delimiter = "";                  my $line_delimiter;
87    
88                  my ($swish,$display);                  my ($swish,$display);
89    
90                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {
91    
92                          my $format = $x->{content};                          my $format = x($x->{content});
93                          my $delimiter = $x->{delimiter} || ' ';                          my $delimiter = x($x->{delimiter}) || ' ';
   
                         # FIX: this is ugly, UGLY, cludge string is returned  
                         # in UTF8 encoding , but as if source charset  
                         # is ISO8859-1 and not some other. This break other  
                         # character encodings, so we convert it first  
                         # back to ISO8859-1  
                         $format = $xml_codepage->convert($format);  
                         $delimiter = $xml_codepage->convert($delimiter) if ($delimiter);  
94    
95                          my $isis_i = 0;         # isis repeatable offset                          my $isis_i = 0;         # isis repeatable offset
96    
# Line 101  sub isis2xml { Line 98  sub isis2xml {
98                          $s = 0 if (lc($x->{type}) eq "display");                          $s = 0 if (lc($x->{type}) eq "display");
99                          $d = 0 if (lc($x->{type}) eq "swish");                          $d = 0 if (lc($x->{type}) eq "swish");
100                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");
 #print STDERR "## s: $s d: $d i: $i ## $format ##\n";    
101    
102                          if ($x->{append}) {                          # what will separate last line from this one?
103                            if ($display_data && $x->{append} && $x->{append} eq "1") {
104                                  $line_delimiter = ' ';                                  $line_delimiter = ' ';
105                          } else {                          } elsif ($display_data) {
106                                  $line_delimiter = '<br/>';                                  $line_delimiter = '<br/>';
107                          }                          }
108    
                         $display_data .= $line_delimiter if ($display_data && $display);  
109                          # init vars so that we go into while...                          # init vars so that we go into while...
110                          ($swish,$display) = (1,1);                          ($swish,$display) = (1,1);
111    
112                          while ($swish || $display) {                          while ($swish || $display) {
113                                  ($swish,$display) = parse_format($format,$row,$isis_i++);                                  ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);
 #print STDERR "s: $swish\nd: $display\n" if ($swish);  
   
 #print STDERR "swish: $swish<-- display: $display<--\n";  
114    
115                                  # filter="name" ; filter this field through                                  # filter="name" ; filter this field through
116                                  # filter/[name].pm                                  # filter/[name].pm
# Line 128  sub isis2xml { Line 121  sub isis2xml {
121                                  # type="swish" ; field for swish                                  # type="swish" ; field for swish
122                                  if ($s && $swish) {                                  if ($s && $swish) {
123                                          if ($filter) {                                          if ($filter) {
 #print STDERR "using filter '$filter'\n";  
124                                                  no strict 'refs';                                                  no strict 'refs';
125                                                  $swish_data .= join(" ",&$filter($swish));                                                  $swish_data .= join(" ",&$filter($swish));
126                                          } else {                                          } else {
# Line 138  sub isis2xml { Line 130  sub isis2xml {
130    
131                                  # type="display" ; field for display                                  # type="display" ; field for display
132                                  if ($d && $display) {                                  if ($d && $display) {
133                                            if ($line_delimiter && $display_data) {
134                                                    $display_data .= $line_delimiter;
135                                                    undef $line_delimiter;
136                                            }
137                                          if ($filter) {                                          if ($filter) {
138                                                  no strict 'refs';                                                  no strict 'refs';
139                                                  $display_data .= join($delimiter,&$filter($display));                                                  $display_data .= join($delimiter,&$filter($display));
# Line 152  sub isis2xml { Line 148  sub isis2xml {
148                                                                                                    
149                                  # type="index" ; insert into index                                  # type="index" ; insert into index
150                                  if ($i && $display) {                                  if ($i && $display) {
151                                          my $index_data = $index_codepage->convert($display) || $display;                                          my $index_data = $display;
152                                          if ($filter) {                                          if ($filter) {
153                                                  no strict 'refs';                                                  no strict 'refs';
154                                                  foreach my $d (&$filter($index_data)) {                                                  foreach my $d (&$filter($index_data)) {
155                                                          $index->insert($field, $d, $db_dir);                                                          $index->insert($field, $d, $path);
156                                                  }                                                  }
157                                          } else {                                          } else {
158                                                  $index->insert($field, $index_data, $db_dir);                                                  $index->insert($field, $index_data, $path);
159                                          }                                          }
160                                  }                                  }
161                          }                          }
162                  }                  }
163    
164    
 #print STDERR "s_d: $swish_data\nd_d: $display_data\n" if ($swish_data);  
165                  if ($display_data) {                  if ($display_data) {
166    
                         # remove last <br>  
                         $display_data =~ s/$line_delimiter$//;  
   
                         $display_data = $isis_codepage->convert($display_data) || die "Can't convert '$display_data' !";  
                         # FIX: this is removed and replaced by html tag.  
                         #$xml .= xmlify($field."_display", $display_data);  
   
167                          if ($field eq "headline") {                          if ($field eq "headline") {
168                                  $xml .= xmlify("headline", $display_data);                                  $xml .= xmlify("headline", $display_data);
169                          } else {                          } else {
# Line 192  sub isis2xml { Line 180  sub isis2xml {
180                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";                                          print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";
181                                  }                                  }
182                                  if ($field_name) {                                  if ($field_name) {
183                                          $html .= $xml_codepage->convert($field_name);                                          $html .= x($field_name);
 #                                       $html .= "-->".$field_name."<--";  
184                                  }                                  }
185                                  $html .= $display_data."###\n";                                  $html .= $display_data."###\n";
186                          }                          }
187                  }                  }
188                  if ($swish_data) {                  if ($swish_data) {
                         my $i = Text::Iconv->new($config->{isis_codepage},'ISO8859-2');  
189                          # remove extra spaces                          # remove extra spaces
190                          $swish_data =~ s/ +/ /g;                          $swish_data =~ s/ +/ /g;
191                          $swish_data =~ s/ +$//g;                          $swish_data =~ s/ +$//g;
192    
193                          $swish_data = $i->convert($swish_data);                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));
                         $xml .= xmlify($field."_swish",unac_string('ISO8859-2',$swish_data));  
                         #$swish_data = $isis_codepage->convert($swish_data)."##" || $swish_data;  
                         #$xml .= xmlify($field."_swish",unac_string($config->{isis_codepage},$swish_data));  
194                  }                  }
195    
196    
# Line 219  sub isis2xml { Line 202  sub isis2xml {
202          }          }
203                    
204          if ($xml) {          if ($xml) {
 #print STDERR "x: $xml\n";  
205                  $xml .= $add_xml if ($add_xml);                  $xml .= $add_xml if ($add_xml);
206                  return "<xml>\n$xml</xml>\n";                  return "<xml>\n$xml</xml>\n";
207          } else {          } else {
# Line 231  sub isis2xml { Line 213  sub isis2xml {
213    
214  my $cfg = new Config::IniFiles( -file => $config_file );  my $cfg = new Config::IniFiles( -file => $config_file );
215    
216    # 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    # delete [global] section to leave just databases sections
225    $cfg->DeleteSection('global');
226    
227  foreach my $database ($cfg->Sections) {  foreach my $database ($cfg->Sections) {
228    
229          my $isis_db = $cfg -> val($database, 'isis_db');          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";
230          my $type = $cfg -> val($database, 'type');          my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";
231          my $add_xml = $cfg -> val($database, 'xml');          my $add_xml = $cfg -> val($database, 'xml');    # optional
232    
         # read configuration for this type  
233          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);
234          $isis_codepage = Text::Iconv->new($config->{isis_codepage},$XML_CHARSET);  
235          $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});          $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);
         $xml_codepage = Text::Iconv->new($cfg->val($database,'xml_codepage'),$XML_CHARSET);  
236    
237          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
238          if (0) {          if (0) {
# Line 264  foreach my $database ($cfg->Sections) { Line 255  foreach my $database ($cfg->Sections) {
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}) {
 #print STDERR "mfn: ",$row->{mfn},"\n";  
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 272  foreach my $database ($cfg->Sections) { Line 262  foreach my $database ($cfg->Sections) {
262                                  $last_p = $p;                                  $last_p = $p;
263                          }                          }
264    
265                            my $swishpath = $path."#".int($row->{mfn});
266    
267                          if (my $xml = isis2xml($row,$add_xml)) {                          if (my $xml = isis2xml($row,$add_xml)) {
268  #print STDERR "--ret-->$xml\n";                                  $xml = $cp2utf->convert($xml);
269                                  print "Path-Name: $path#".int($row->{mfn})."\n";                                  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                          }                          }

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

  ViewVC Help
Powered by ViewVC 1.1.26