/[webpac]/branches/drustvene/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 /branches/drustvene/all2xml.pl

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

revision 21 by dpavlin, Sun Feb 23 00:00:51 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
35    
36  #die "usage: $0 -d [database_dir] -m [database1,database2] " if (! %opts);  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions
37    
38  #print Dumper($config->{indexer});  # this is encoding of all files on disk, including import_xml/*.xml file and
39  #print "-" x 70,"\n";  # 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  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  # decode isis import codepage
49    my $isis2cp;
50    
51  #my $isis_codepage = Text::Iconv->new($config->{isis_codepage},'UTF8');  # outgoing xml must be in UTF-8
52  #my $index_codepage = Text::Iconv->new($config->{isis_codepage},$config->{index_codepage});  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');
 my $isis_codepage;  
 my $index_codepage;  
 my $cludge_codepage = Text::Iconv->new('UTF8','ISO8859-1');  
 my $xml_codepage;  
53    
54  sub isis2xml {  sub isis2xml {
55    
# Line 62  sub isis2xml { Line 66  sub isis2xml {
66    
67          my %field_usage;        # counter for usage of each field          my %field_usage;        # counter for usage of each field
68    
69          foreach my $field (keys %{$config->{indexer}}) {          # sort subrouting using order="" attribute
70            sub by_order {
71                    return 0 if (! $config->{indexer}->{$a}->{order});
72                    return 0 if (! $config->{indexer}->{$b}->{order});
73    
74                    return $config->{indexer}->{$a}->{order} <=>
75                            $config->{indexer}->{$b}->{order} ;
76            }
77    
78            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;
87    
88                    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($x->{delimiter}) || ' ';
94    
95                            my $isis_i = 0;         # isis repeatable offset
96    
97                          my ($s,$d,$i) = (1,1,0);        # swish, display default                          my ($s,$d,$i) = (1,1,0);        # swish, display default
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                          # FIX: this is ugly, UGLY, cludge string is returned                          # what will separate last line from this one?
103                          # in UTF8 encoding , but as if source charset                          if ($display_data && $x->{append} && $x->{append} eq "1") {
104                          # is ISO8859-1 and not some other. This break other                                  $line_delimiter = ' ';
105                          # character encodings, so we convert it first                          } elsif ($display_data) {
106                          # back to ISO8859-1                                  $line_delimiter = '<br/>';
                         $format = $cludge_codepage->convert($format);  
   
                         my ($swish,$display) = parse_format($format,$row);  
 #print STDERR "s: $swish\nd: $display\n" if ($swish);  
   
 #print STDERR "swish: $swish<-- display: $display<--\n";  
   
                         # filter="name" ; filter this field through  
                         # filter/[name].pm  
                         my $filter;  
                         if ($x->{filter}) {  
                                 $filter = "filter/".$x->{filter}.".pm";  
                                 require $filter;  
107                          }                          }
108                          # type="swish" ; field for swish  
109                          if ($s && $swish) {                          # init vars so that we go into while...
110                            ($swish,$display) = (1,1);
111    
112                            while ($swish || $display) {
113                                    ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);
114    
115                                    # filter="name" ; filter this field through
116                                    # filter/[name].pm
117                                    my $filter = $x->{filter};
118                                  if ($filter) {                                  if ($filter) {
119                                          $swish_data .= join(" ",&filter($swish));                                          require "filter/".$filter.".pm";
120                                  } else {                                  }
121                                          $swish_data .= $swish if ($s && $swish);                                  # 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                                  }                                  }
                         }  
130    
131                          # type="display" ; field for display                                  # type="display" ; field for display
132                          if ($d && $display) {                                  if ($d && $display) {
133                                  if ($filter) {                                          if ($line_delimiter && $display_data) {
134                                          $display_data .= join(" ",&filter($display));                                                  $display_data .= $line_delimiter;
135                                  } else {                                                  undef $line_delimiter;
136                                          $display_data .= $display;                                          }
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                          # type="index" ; insert into index                                  if ($i && $display) {
151                          if ($i && $display) {                                          my $index_data = $display;
152                                  my $index_data = $index_codepage->convert($display) || $display;                                          if ($filter) {
153                                  if ($filter) {                                                  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 {
158                                                    $index->insert($field, $index_data, $path);
159                                          }                                          }
                                 } else {  
                                         $index->insert($field, $index_data, $db_dir);  
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) {
                         $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);  
166    
167                          if ($field eq "headline") {                          if ($field eq "headline") {
168                                  $xml .= xmlify("headline", $display_data);                                  $xml .= xmlify("headline", $display_data);
# Line 151  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);
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 177  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 189  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},'UTF8');  
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'),'UTF8');  
236    
237          my $db = OpenIsis::open( $isis_db );          my $db = OpenIsis::open( $isis_db );
238          if (0) {          if (0) {
# Line 222  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 230  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.21  
changed lines
  Added in v.50

  ViewVC Help
Powered by ViewVC 1.1.26