/[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 43 by dpavlin, Sat Mar 22 22:43:05 2003 UTC trunk2/all2all.pl revision 419 by dpavlin, Thu Sep 9 18:09:21 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 ;-)  
   
 my $index = new index_DBI();    # open index  
   
 my %opts;  
   
 # usage:  
 #       -d directory name  
 #       -m multiple directories  
 #       -q quiet  
 #       -s run swish  
   
 getopts('d:m:qs', \%opts);  
   
 my $path;       # this is name of database  
   
 Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  
   
 # this is encoding of all files on disk, including import_xml/*.xml file and  
 # filter/*.pm files! It will be used to store strings in perl internally!  
 my $codepage = 'ISO-8859-2';  
   
 my $utf2cp = Text::Iconv->new('UTF-8',$codepage);  
 # this function will convert data from XML files to local encoding  
 sub x {  
         return $utf2cp->convert($_[0]);  
 }  
   
 # decode isis import codepage  
 my $isis2cp;  
   
 # outgoing xml must be in UTF-8  
 my $cp2utf = Text::Iconv->new($codepage,'UTF-8');  
   
 sub isis2xml {  
   
         use xmlify;  
4    
5          my $row = shift @_;  all2all.pl - basic script for all WebPAC needs
         my $add_xml = shift @_;  
6    
7          my $xml;  =cut
8    
9          use parse_format;  use strict;
10    use Data::Dumper;
11    use Carp;
12    
13          my $html = "";          # html formatted display output  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 %field_usage;        # counter for usage of each field  my $thes;
32    
33          # sort subrouting using order="" attribute  $|=1;
         sub by_order {  
                 return 0 if (! $config->{indexer}->{$a}->{order});  
                 return 0 if (! $config->{indexer}->{$b}->{order});  
34    
35                  return $config->{indexer}->{$a}->{order} <=>  my $maxmfn = $webpac->open_isis(
36                          $config->{indexer}->{$b}->{order} ;          filename => shift @ARGV || '/data/hidra/THS/THS',
37            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          foreach my $field (sort by_order keys %{$config->{indexer}}) {          my $headline = $webpac->{'headline'};
83    
84                  $field=x($field);          my $f = $filename;
85            $f =~ s!out/!!;
86    
87                  $field_usage{$field}++;          # save into index
88            foreach my $ds (@ds) {
89                    next if (! $ds->{'swish'});
90    
91                  my $swish_data = "";                  $index->insert(
92                  my $display_data = "";                          index_name => $ds->{'tag'},
93                  my $line_delimiter;                          path => $f,
94                            headline => $headline,
95                  my ($swish,$display);                          words => join(" ",@{$ds->{'swish'}})
96                    );
97            }
98    
99                  foreach my $x (@{$config->{indexer}->{$field}->{isis}}) {          # save into sorted index (thesaurus)
100            foreach my $ds (@ds) {
101                    next if (! $ds->{'index'});
102    
103                          my $format = x($x->{content});                  $thes->{$ds->{'tag'}} ||= new WebPAC::Index;
                         my $delimiter = x($x->{delimiter}) || ' ';  
104    
105                          my $isis_i = 0;         # isis repeatable offset                  foreach my $h (@{$ds->{'index'}}) {
106                            $thes->{$ds->{'tag'}}->insert(
107                                    path => $f,
108                                    headline => $h,
109                            );
110                    }
111            }
112    
113                          my ($s,$d,$i) = (1,1,0);        # swish, display default  #       print Dumper(\@ds);
                         $s = 0 if (lc($x->{type}) eq "display");  
                         $d = 0 if (lc($x->{type}) eq "swish");  
                         ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");  
114    
115                          # what will separate last line from this one?  }
                         if ($display_data && $x->{append} && $x->{append} eq "1") {  
                                 $line_delimiter = ' ';  
                         } elsif ($display_data) {  
                                 $line_delimiter = '<br/>';  
                         }  
116    
117                          # init vars so that we go into while...  foreach my $t (keys %{$thes}) {
                         ($swish,$display) = (1,1);  
118    
119                          while ($swish || $display) {          my @e = $thes->{$t}->elements;
120                                  ($swish,$display) = parse_format($format,$row,$isis_i++,$isis2cp);          if (! @e) {
121                    $log->logwarn("no elements in sorted index $t?");
122                    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                                  # filter="name" ; filter this field through  if (0 && $log->is_debug) {
137                                  # filter/[name].pm          $log->debug("lookup hash: ",Dumper($webpac->{'lookup'}));
138                                  my $filter = $x->{filter};          $log->debug("data hash: ",Dumper($webpac->{'data'}));
139                                  if ($filter) {          foreach my $t (keys %{$thes}) {
140                                          require "filter/".$filter.".pm";                  $log->debug("thesaurus $t hash: ",Dumper($thes->{$t}));
141                                  }          }
142                                  # 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);  
                                         }  
                                 }  
                         }  
                 }  
143    
144    #
145    # tree dump implementation which shouldn't be here :-)
146    #
147    
148                  if ($display_data) {  my $file = 'out/browse.html';
149    open(HTML, "> $file") || $log->logdie("can't open '$file'");
150    
151                          if ($field eq "headline") {  print HTML qq{
152                                  $xml .= xmlify("headline", $display_data);  <html>
153                          } else {  <head>
154     <title>Browse</title>
155                                  # find field name (signular, plural)   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
156                                  my $field_name = "";  </head>
157                                  if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {  <body>
158                                          $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";  <ul>
159                                  } elsif ($config->{indexer}->{$field}->{name_plural}) {  };
                                         $field_name = $config->{indexer}->{$field}->{name_plural}."#-#";  
                                 } elsif ($config->{indexer}->{$field}->{name}) {  
                                         $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";  
                         }  
                 }  
                 if ($swish_data) {  
                         # remove extra spaces  
                         $swish_data =~ s/ +/ /g;  
                         $swish_data =~ s/ +$//g;  
160    
161                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));  foreach my $code (sort keys %{$webpac->{'lookup'}}) {
                 }  
162    
163            my $l = $webpac->{'lookup'} || $log->logconfess("can't find lookup");
164    
165          }          if ($code =~ m/^root:/) {
166                    my $v900 = shift @{ $l->{$code} } || $log->logconfess("can't lookup '$code'");
167                    $code =~ s/^root:// || die;
168    
169          # dump formatted output in <html>                  my $term = shift @{ $l->{"d:${v900}"} } || die;
170          if ($html) {                  my $mfn = shift @{ $l->{"900_mfn:${v900}"} } || die;
                 $xml .= xmlify("html",$html);  
         }  
           
         if ($xml) {  
                 $xml .= $add_xml if ($add_xml);  
                 return "<xml>\n$xml</xml>\n";  
         } else {  
                 return;  
         }  
 }  
171    
172  ##########################################################################                  $log->info("$code -> $v900 : $term [$mfn]");
173    
174  my $cfg = new Config::IniFiles( -file => $config_file );                  print HTML qq{ <li><a href="thes/$mfn.html">$term</a></li>\n} if (-e "out/thes/$mfn.html");
175    
176  foreach my $database ($cfg->Sections) {                  $log->logconfess("can't find 'a:${code}::'") unless ($l->{"a:${code}::"});
177    
178          my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";                  print HTML qq{ <ul>\n};
         my $type = $cfg -> val($database, 'type') || die "$database doesn't have 'type' defined";  
         my $add_xml = $cfg -> val($database, 'xml');    # optional  
179    
180          $config=XMLin("./import_xml/$type.xml", forcearray => [ 'isis' ], forcecontent => 1);                  foreach my $l2_v900 (@{ $l->{"a:${code}::"} }) {
181                            
182                            my $l2_code = shift @{ $l->{"code:${l2_v900}"} } || die;
183                            my $l2_term = shift @{ $l->{"d:$l2_v900"} } || die;
184                            my $l2_mfn = shift @{ $l->{"900_mfn:${l2_v900}"} } || die;
185    
186          $isis2cp = Text::Iconv->new($config->{isis_codepage},$codepage);                          $log->info("$l2_code -> $l2_v900 : $l2_term [$l2_mfn]");
187    
188          my $db = OpenIsis::open( $isis_db );                          print HTML qq{  <li><a href="thes/$l2_mfn.html">$l2_term</a></li>\n} if (-e "out/thes/$l2_mfn.html");
         if (0) {  
 #       # FIX  
 #       if (! $db ) {  
                 print STDERR "WARNING: can't open '$isis_db'\n";  
                 next ;  
         }  
189    
190          my $max_rowid = OpenIsis::maxRowid( $db );                          print HTML qq{  <ul>\n};
191    
192          print STDERR "Reading database: $isis_db [$max_rowid rows]\n";                          foreach my $l3_v900 (@{ $l->{"a:${l2_code}:"} }) {
193    
194          my $path = $database;                   # was $isis_db                                  my $l3_term = shift @{ $l->{"d:$l3_v900"} } || die;
195                                    my $l3_mfn = shift @{ $l->{"900_mfn:${l3_v900}"} } || die;
196    
197          my $last_p = 0;                                  $log->info(" -> $l3_v900 : $l3_term [$l3_mfn]");
198    
199  #       { my $row_id = 4514;                                  print HTML qq{   <li><a href="thes/$l3_mfn.html">$l3_term</a></li>\n} if (-e "out/thes/$l3_mfn.html");
 # 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;  
200                          }                          }
201    
202                          my $swishpath = $path."#".int($row->{mfn});                          print HTML qq{  </ul>\n};
   
                         if (my $xml = $cp2utf->convert(isis2xml($row,$add_xml."<path>$swishpath</path>"))) {  
                                 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";  
                         }  
203                  }                  }
204                    print HTML qq{ </ul>\n};
205          }          }
         print STDERR "\n";  
206  }  }
207    
208  # call this to commit index  print HTML qq{
209  $index->close;  </ul>
210    </body>
211  1;  </html>
212  __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.  
213    
214  =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  
215    
 =cut  

Legend:
Removed from v.43  
changed lines
  Added in v.419

  ViewVC Help
Powered by ViewVC 1.1.26