/[webpac]/trunk2/all2all.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/all2all.pl

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

trunk/all2xml.pl revision 44 by dpavlin, Sat Mar 22 22:51:48 2003 UTC trunk2/all2all.pl revision 420 by dpavlin, Thu Sep 9 18:25:34 2004 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/usr/bin/perl -w
2    
3  use strict;  =head1 NAME
 use OpenIsis;  
 use Getopt::Std;  
 use Data::Dumper;  
 use XML::Simple;  
 use Text::Unaccent 1.02;        # 1.01 won't compile on my platform,  
 use Text::Iconv;  
 use Config::IniFiles;  
 use Encode;  
   
 $|=1;  
   
 my $config_file = $0;  
 $config_file =~ s/\.pl$/.conf/;  
 die "FATAL: can't find configuration file '$config_file'" if (! -e $config_file);  
   
 my $config;  
   
 use index_DBI;  # there is no other, right now ;-)  
4    
5  my $index = new index_DBI();    # open index  all2all.pl - basic script for all WebPAC needs
6    
7  my %opts;  =cut
8    
9  # usage:  use strict;
10  #       -d directory name  use Data::Dumper;
11  #       -m multiple directories  use Carp;
 #       -q quiet  
 #       -s run swish  
12    
13  getopts('d:m:qs', \%opts);  use lib './lib';
14    use WebPAC;
15    use WebPAC::jsFind;
16    use WebPAC::Index;
17    
18    my $webpac = new WebPAC(
19            code_page => 'ISO-8859-2',
20    #       limit_mfn => 500,
21    #       debug => 1,
22    ) || die;
23    
24    my $log = $webpac->_get_logger();
25    
26    my $index = new WebPAC::jsFind(
27            index_path => './out/index',
28            keys => 10,
29    ) || die;
30    
31  my $path;       # this is name of database  my $thes;
32    
33  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  $|=1;
34    
35  # this is encoding of all files on disk, including import_xml/*.xml file and  my $maxmfn = $webpac->open_isis(
36  # filter/*.pm files! It will be used to store strings in perl internally!          filename => shift @ARGV || '/data/hidra/THS/THS',
37  my $codepage = 'ISO-8859-2';          lookup => [
38            { 'key' => 'd:v900', 'val' => 'v250^a' },
39    #       { 'eval' => '"v901^a" eq "Područje"', 'key' => 'pa:v561^4:v562^4:v461^1', 'val' => 'v900' },
40    #       { 'eval '=> '"v901^a" eq "Mikrotezaurus"', 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
41    #       { 'eval' => '"v901^a" eq "Deskriptor"', 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
42            { 'key' => 'a:v561^4:v562^4:v461^1', 'val' => 'v900' },
43            { 'key' => '900_mfn:v900', 'val' => 'v000' },
44            # tree structure
45            { 'eval' => 'length("v251") == 2', 'key' => 'root:v251', 'val' => 'v900' },
46            { 'eval' => '"v251"', 'key' => 'code:v900', 'val' => 'v561^4:v251' },
47            ],
48    );
49    
50    $log->info("rows: $maxmfn");
51    
52    $webpac->open_import_xml(type => 'isis_hidra_ths');
53    
54    while (my $rec = $webpac->fetch_rec) {
55    
56            my @ds = $webpac->data_structure($rec);
57    
58            if (0 && $log->is_debug) {
59                    $log->debug("rec = ",Dumper($rec));
60                    $log->debug("ds = ",Dumper(\@ds));
61            }
62    
63            next if (! @ds);
64    
65            my $filename = $webpac->{'current_filename'};
66    
67            if ($filename) {
68                    $webpac->output_file(
69                            file => $filename,
70                            template => 'html.tt',
71                            data => \@ds,
72                            headline => $webpac->{'headline'},
73                    );
74            } else {
75                    print $webpac->output(
76                            template => 'text.tt',
77                            data => \@ds,
78                            headline => $webpac->{'headline'},
79                    );
80            }
81    
82  my $utf2cp = Text::Iconv->new('UTF-8',$codepage);          my $headline = $webpac->{'headline'};
 # this function will convert data from XML files to local encoding  
 sub x {  
         return $utf2cp->convert($_[0]);  
 }  
83    
84  # decode isis import codepage          my $f = $filename;
85  my $isis2cp;          $f =~ s!out/!!;
86    
87  # outgoing xml must be in UTF-8          # save into index
88  my $cp2utf = Text::Iconv->new($codepage,'UTF-8');          foreach my $ds (@ds) {
89                    next if (! $ds->{'swish'});
90    
91  sub isis2xml {                  $index->insert(
92                            index_name => $ds->{'tag'},
93                            path => $f,
94                            headline => $headline,
95                            words => join(" ",@{$ds->{'swish'}})
96                    );
97            }
98    
99          use xmlify;          # save into sorted index (thesaurus)
100            foreach my $ds (@ds) {
101                    next if (! $ds->{'index'});
102    
103          my $row = shift @_;                  $thes->{$ds->{'tag'}} ||= new WebPAC::Index;
         my $add_xml = shift @_;  
104    
105          my $xml;                  foreach my $h (@{$ds->{'index'}}) {
106                            $thes->{$ds->{'tag'}}->insert(
107                                    path => $f,
108                                    headline => $h,
109                            );
110                    }
111            }
112    
113          use parse_format;  #       print Dumper(\@ds);
114    
115          my $html = "";          # html formatted display output  }
116    
117          my %field_usage;        # counter for usage of each field  foreach my $t (keys %{$thes}) {
118    
119          # sort subrouting using order="" attribute          my @e = $thes->{$t}->elements;
120          sub by_order {          if (! @e) {
121                  return 0 if (! $config->{indexer}->{$a}->{order});                  $log->logwarn("no elements in sorted index $t?");
122                  return 0 if (! $config->{indexer}->{$b}->{order});                  next;
123            }
124    
125            my $file = "./out/bfilter/$t.txt";
126            $log->info("saving sorted index $t to '$file' [".scalar(@e)." elements]");
127    
128            $webpac->output_file(
129                    file => $file,
130                    template => 'index.tt',
131                    data => \@e,
132                    index_name => $t,
133            );
134    }
135    
136                  return $config->{indexer}->{$a}->{order} <=>  if (0 && $log->is_debug) {
137                          $config->{indexer}->{$b}->{order} ;          $log->debug("lookup hash: ",Dumper($webpac->{'lookup'}));
138            $log->debug("data hash: ",Dumper($webpac->{'data'}));
139            foreach my $t (keys %{$thes}) {
140                    $log->debug("thesaurus $t hash: ",Dumper($thes->{$t}));
141          }          }
142    }
143    
144          foreach my $field (sort by_order keys %{$config->{indexer}}) {  $log->info("lookup hash: ",Dumper($webpac->{'lookup'}));
   
                 $field=x($field);  
   
                 $field_usage{$field}++;  
145    
146                  my $swish_data = "";  #
147                  my $display_data = "";  # tree dump implementation which shouldn't be here :-)
148                  my $line_delimiter;  #
149    
150                  my ($swish,$display);  my $file = 'out/browse.html';
151    open(HTML, "> $file") || $log->logdie("can't open '$file'");
152    
153                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {  print HTML qq{
154    <html>
155    <head>
156     <title>Browse</title>
157     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
158    </head>
159    <body>
160    <ul>
161    };
162    
163                          my $format = x($x->{content});  foreach my $code (sort keys %{$webpac->{'lookup'}}) {
                         my $delimiter = x($x->{delimiter}) || ' ';  
164    
165                          my $isis_i = 0;         # isis repeatable offset          my $l = $webpac->{'lookup'} || $log->logconfess("can't find lookup");
166    
167                          my ($s,$d,$i) = (1,1,0);        # swish, display default          if ($code =~ m/^root:/) {
168                          $s = 0 if (lc($x->{type}) eq "display");                  my $v900 = shift @{ $l->{$code} } || $log->logconfess("can't lookup '$code'");
169                          $d = 0 if (lc($x->{type}) eq "swish");                  $code =~ s/^root:// || die;
                         ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");  
170    
171                          # what will separate last line from this one?                  my $term = shift @{ $l->{"d:${v900}"} } || die;
172                          if ($display_data && $x->{append} && $x->{append} eq "1") {                  my $mfn = shift @{ $l->{"900_mfn:${v900}"} } || die;
                                 $line_delimiter = ' ';  
                         } elsif ($display_data) {  
                                 $line_delimiter = '<br/>';  
                         }  
173    
174                          # init vars so that we go into while...                  $log->info("$code -> $v900 : $term [$mfn]");
                         ($swish,$display) = (1,1);  
175    
176                          while ($swish || $display) {                  print HTML qq{ <li><a href="thes/$mfn.html">$term</a></li>\n} if (-e "out/thes/$mfn.html");
                                 ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);  
177    
178                                  # filter="name" ; filter this field through                  unless ($l->{"a:${code}::"}) {
179                                  # filter/[name].pm                          $log->warn("can't find 'a:${code}::'");
180                                  my $filter = $x->{filter};                          next;
                                 if ($filter) {  
                                         require "filter/".$filter.".pm";  
                                 }  
                                 # type="swish" ; field for swish  
                                 if ($s && $swish) {  
                                         if ($filter) {  
                                                 no strict 'refs';  
                                                 $swish_data .= join(" ",&$filter($swish));  
                                         } else {  
                                                 $swish_data .= $swish;  
                                         }  
                                 }  
   
                                 # type="display" ; field for display  
                                 if ($d && $display) {  
                                         if ($line_delimiter && $display_data) {  
                                                 $display_data .= $line_delimiter;  
                                                 undef $line_delimiter;  
                                         }  
                                         if ($filter) {  
                                                 no strict 'refs';  
                                                 $display_data .= join($delimiter,&$filter($display));  
                                         } else {  
                                                 if ($display_data) {  
                                                         $display_data .= $delimiter.$display;  
                                                 } else {  
                                                         $display_data .= $display;  
                                                 }  
                                         }  
                                 }  
                                                   
                                 # type="index" ; insert into index  
                                 if ($i && $display) {  
                                         my $index_data = $display;  
                                         if ($filter) {  
                                                 no strict 'refs';  
                                                 foreach my $d (&$filter($index_data)) {  
                                                         $index->insert($field, $d, $path);  
                                                 }  
                                         } else {  
                                                 $index->insert($field, $index_data, $path);  
                                         }  
                                 }  
                         }  
181                  }                  }
182    
183                    print HTML qq{ <ul>\n};
184    
185                  if ($display_data) {                  foreach my $l2_v900 (@{ $l->{"a:${code}::"} }) {
186                            
187                          if ($field eq "headline") {                          my $l2_code = shift @{ $l->{"code:${l2_v900}"} } || die;
188                                  $xml .= xmlify("headline", $display_data);                          my $l2_term = shift @{ $l->{"d:$l2_v900"} } || die;
189                          } else {                          my $l2_mfn = shift @{ $l->{"900_mfn:${l2_v900}"} } || die;
190    
191                                  # find field name (signular, plural)                          $log->info("$l2_code -> $l2_v900 : $l2_term [$l2_mfn]");
192                                  my $field_name = "";  
193                                  if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {                          print HTML qq{  <li><a href="thes/$l2_mfn.html">$l2_term</a></li>\n} if (-e "out/thes/$l2_mfn.html");
194                                          $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";                          
195                                  } elsif ($config->{indexer}->{$field}->{name_plural}) {                          unless ($l->{"a:${l2_code}:"}) {
196                                          $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";                                  $log->warn("can't find 'a:${l2_code}:'");
197                                  } elsif ($config->{indexer}->{$field}->{name}) {                                  next;
                                         $field_name = $config->{indexer}->{$field}->{name}."#-#";  
                                 } else {  
                                         print STDERR "WARNING: field '$field' doesn't have 'name' attribute!";  
                                 }  
                                 if ($field_name) {  
                                         $html .= x($field_name);  
                                 }  
                                 $html .= $display_data."###\n";  
198                          }                          }
                 }  
                 if ($swish_data) {  
                         # remove extra spaces  
                         $swish_data =~ s/ +/ /g;  
                         $swish_data =~ s/ +$//g;  
199    
200                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));                          print HTML qq{  <ul>\n};
                 }  
201    
202                            foreach my $l3_v900 (@{ $l->{"a:${l2_code}:"} }) {
203    
204          }                                  my $l3_term = shift @{ $l->{"d:$l3_v900"} } || die;
205                                    my $l3_mfn = shift @{ $l->{"900_mfn:${l3_v900}"} } || die;
206    
207          # dump formatted output in <html>                                  $log->info(" -> $l3_v900 : $l3_term [$l3_mfn]");
         if ($html) {  
                 $xml .= xmlify("html",$html);  
         }  
           
         if ($xml) {  
                 $xml .= $add_xml if ($add_xml);  
                 return "<xml>\n$xml</xml>\n";  
         } else {  
                 return;  
         }  
 }  
   
 ##########################################################################  
   
 my $cfg = new Config::IniFiles( -file => $config_file );  
   
 foreach my $database ($cfg->Sections) {  
   
         my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";  
         my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";  
         my $add_xml = $cfg -> val($database, 'xml');    # optional  
   
         $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);  
   
         $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);  
208    
209          my $db = OpenIsis::open( $isis_db );                                  print HTML qq{   <li><a href="thes/$l3_mfn.html">$l3_term</a></li>\n} if (-e "out/thes/$l3_mfn.html");
         if (0) {  
 #       # FIX  
 #       if (! $db ) {  
                 print STDERR "WARNING: can't open '$isis_db'\n";  
                 next ;  
         }  
   
         my $max_rowid = OpenIsis::maxRowid( $db );  
   
         print STDERR "Reading database: $isis_db [$max_rowid rows]\n";  
   
         my $path = $database;                   # was $isis_db  
   
         my $last_p = 0;  
   
 #       { my $row_id = 4514;  
 # FIX  
         for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {  
                 my $row = OpenIsis::read( $db, $row_id );  
                 if ($row && $row->{mfn}) {  
                         # output current process indicator  
                         my $p = int($row->{mfn} * 100 / $max_rowid);  
                         if ($p != $last_p) {  
                                 printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$row->{mfn},$max_rowid,"=" x ($p/2).">", $p ) if (! $opts{q});  
                                 $last_p = $p;  
210                          }                          }
211    
212                          my $swishpath = $path."#".int($row->{mfn});                          print HTML qq{  </ul>\n};
   
                         if (my $xml = isis2xml($row,$add_xml)) {  
                                 $xml = $cp2utf->convert($xml);  
                                 use bytes;      # as opposed to chars  
                                 print "Path-Name: $swishpath\n";  
                                 print "Content-Length: ".(length($xml)+1)."\n";  
                                 print "Document-Type: XML\n\n$xml\n";  
                         }  
213                  }                  }
214                    print HTML qq{ </ul>\n};
215          }          }
         print STDERR "\n";  
216  }  }
217    
218  # call this to commit index  print HTML qq{
219  $index->close;  </ul>
220    </body>
221  1;  </html>
222  __END__  };
 ##########################################################################  
   
 =head1 NAME  
   
 isis2xml.pl - read isis file and dump XML  
   
 =head1 DESCRIPTION  
   
 This command will read ISIS data file using OpenIsis perl module and  
 create XML file for usage with I<SWISH-E>  
 indexer. Dispite it's name, this script B<isn't general xml generator>  
 from isis files (isis allready has something like that). Output of this  
 script is tailor-made for SWISH-E.  
223    
224  =head1 AUTHOR  $index->close;
   
 Dobrica Pavlinusic <dpavlin@rot13.org>  
   
 =head1 COPYRIGHT  
   
 GNU Public License (GPL) v2 or later  
   
 =head1 SEE ALSO  
   
 SWISH-E web site at http://www.swish-e.org  
225    
 =cut  

Legend:
Removed from v.44  
changed lines
  Added in v.420

  ViewVC Help
Powered by ViewVC 1.1.26