/[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 54 by dpavlin, Mon Jun 23 20:20:32 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 ;-)  
 my $index;  
   
 my %opts;  
   
 # usage:  
 #       -d directory name  
 #       -m multiple directories  
 #       -q quiet  
 #       -s run swish  
   
 getopts('d:m:qs', \%opts);  
4    
5  my $path;       # this is name of database  all2all.pl - basic script for all WebPAC needs
6    
7  Text::Iconv->raise_error(1);     # Conversion errors raise exceptions  =cut
8    
9  # this is encoding of all files on disk, including import_xml/*.xml file and  use strict;
10  # filter/*.pm files! It will be used to store strings in perl internally!  use Data::Dumper;
11  my $codepage = 'ISO-8859-2';  use Carp;
12    
13  my $utf2cp = Text::Iconv->new('UTF-8',$codepage);  use lib './lib';
14  # this function will convert data from XML files to local encoding  use WebPAC;
15  sub x {  use WebPAC::jsFind;
16          return $utf2cp->convert($_[0]);  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  # decode isis/excel or other import codepage  my $thes;
 my $import2cp;  
32    
33  # outgoing xml must be in UTF-8  $|=1;
 my $cp2utf = Text::Iconv->new($codepage,'UTF-8');  
34    
35  # mapping between data type and tag which specify  my $maxmfn = $webpac->open_isis(
36  # format in XML file          filename => shift @ARGV || '/data/hidra/THS/THS',
37  my %type2tag = (          lookup => [
38          'isis' => 'isis',          { 'key' => 'd:v900', 'val' => 'v250^a' },
39          'excel' => 'column'  #       { '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  sub data2xml {  $log->info("rows: $maxmfn");
51    
52          use xmlify;  $webpac->open_import_xml(type => 'isis_hidra_ths');
53    
54          my $type = shift @_;  while (my $rec = $webpac->fetch_rec) {
         my $row = shift @_;  
         my $add_xml = shift @_;  
55    
56          my $xml;          my @ds = $webpac->data_structure($rec);
57    
58          use parse_format;          if (0 && $log->is_debug) {
59                    $log->debug("rec = ",Dumper($rec));
60          my $html = "";          # html formatted display output                  $log->debug("ds = ",Dumper(\@ds));
   
         my %field_usage;        # counter for usage of each field  
   
         # sort subrouting using order="" attribute  
         sub by_order {  
                 return 0 if (! $config->{indexer}->{$a}->{order});  
                 return 0 if (! $config->{indexer}->{$b}->{order});  
   
                 return $config->{indexer}->{$a}->{order} <=>  
                         $config->{indexer}->{$b}->{order} ;  
61          }          }
62    
63          foreach my $field (sort by_order keys %{$config->{indexer}}) {          next if (! @ds);
64    
65                  $field=x($field);          my $filename = $webpac->{'current_filename'};
66    
67                  $field_usage{$field}++;          if ($filename) {
68                    $webpac->output_file(
69                  my $swish_data = "";                          file => $filename,
70                  my $display_data = "";                          template => 'html.tt',
71                  my $line_delimiter;                          data => \@ds,
72                            headline => $webpac->{'headline'},
73                  my ($swish,$display);                  );
74            } else {
75                    print $webpac->output(
76                            template => 'text.tt',
77                            data => \@ds,
78                            headline => $webpac->{'headline'},
79                    );
80            }
81    
82                  my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";          my $headline = $webpac->{'headline'};
                 foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {  
83    
84                          my $format = x($x->{content});          my $f = $filename;
85                          my $delimiter = x($x->{delimiter}) || ' ';          $f =~ s!out/!!;
86    
87                          my $repeat_off = 0;             # repeatable offset          # save into index
88            foreach my $ds (@ds) {
89                    next if (! $ds->{'swish'});
90    
91                          my ($s,$d,$i) = (1,1,0);        # swish, display default                  $index->insert(
92                          $s = 0 if (lc($x->{type}) eq "display");                          index_name => $ds->{'tag'},
93                          $d = 0 if (lc($x->{type}) eq "swish");                          path => $f,
94                          ($s,$d,$i) = (0,0,1) if (lc($x->{type}) eq "index");                          headline => $headline,
95                            words => join(" ",@{$ds->{'swish'}})
96                    );
97            }
98    
99                          # what will separate last line from this one?          # save into sorted index (thesaurus)
100                          if ($display_data && $x->{append} && $x->{append} eq "1") {          foreach my $ds (@ds) {
101                                  $line_delimiter = ' ';                  next if (! $ds->{'index'});
                         } elsif ($display_data) {  
                                 $line_delimiter = '<br/>';  
                         }  
102    
103                          # init vars so that we go into while...                  $thes->{$ds->{'tag'}} ||= new WebPAC::Index;
                         ($swish,$display) = (1,1);  
104    
105                          if ($swish || $display) {                  foreach my $h (@{$ds->{'index'}}) {
106                                  ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);                          $thes->{$ds->{'tag'}}->insert(
107                                  # filter="name" ; filter this field through                                  path => $f,
108                                  # filter/[name].pm                                  headline => $h,
109                                  my $filter = $x->{filter};                          );
                                 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);  
                                         }  
                                 }  
                         }  
110                  }                  }
111            }
112    
113    #       print Dumper(\@ds);
114    
115                  if ($display_data) {  }
   
                         if ($field eq "headline") {  
                                 $xml .= xmlify("headline", $display_data);  
                         } else {  
   
                                 # find field name (signular, plural)  
                                 my $field_name = "";  
                                 if ($config->{indexer}->{$field}->{name_singular} && $field_usage{$field} == 1) {  
                                         $field_name = $config->{indexer}->{$field}->{name_singular}."#-#";  
                                 } 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;  
   
                         $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));  
                 }  
116    
117    foreach my $t (keys %{$thes}) {
118    
119          }          my @e = $thes->{$t}->elements;
120            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          # dump formatted output in <html>  if (0 && $log->is_debug) {
137          if ($html) {          $log->debug("lookup hash: ",Dumper($webpac->{'lookup'}));
138                  $xml .= xmlify("html",$html);          $log->debug("data hash: ",Dumper($webpac->{'data'}));
139          }          foreach my $t (keys %{$thes}) {
140                            $log->debug("thesaurus $t hash: ",Dumper($thes->{$t}));
         if ($xml) {  
                 $xml .= $add_xml if ($add_xml);  
                 return "<xml>\n$xml</xml>\n";  
         } else {  
                 return;  
141          }          }
142  }  }
143    
144  ##########################################################################  $log->info("lookup hash: ",Dumper($webpac->{'lookup'}));
145    
146  # read configuration for this script  #
147  my $cfg = new Config::IniFiles( -file => $config_file );  # tree dump implementation which shouldn't be here :-)
148    #
149    
150  # read global.conf configuration  my $file = 'out/browse.html';
151  my $cfg_global = new Config::IniFiles( -file => 'global.conf' );  open(HTML, "> $file") || $log->logdie("can't open '$file'");
152    
153  # open index  print HTML qq{
154  $index = new index_DBI(  <html>
155                  $cfg_global->val('global', 'dbi_dbd'),  <head>
156                  $cfg_global->val('global', 'dbi_dsn'),   <title>Browse</title>
157                  $cfg_global->val('global', 'dbi_user'),   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
158                  $cfg_global->val('global', 'dbi_passwd') || '',  </head>
159          );  <body>
160    <ul>
161    };
162    
163  foreach my $database ($cfg->Sections) {  foreach my $code (sort keys %{$webpac->{'lookup'}}) {
164    
165          my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";          my $l = $webpac->{'lookup'} || $log->logconfess("can't find lookup");
         my $add_xml = $cfg -> val($database, 'xml');    # optional  
166    
167  print STDERR "reading ./import_xml/$type.xml\n";          if ($code =~ m/^root:/) {
168                    my $v900 = shift @{ $l->{$code} } || $log->logconfess("can't lookup '$code'");
169                    $code =~ s/^root:// || die;
170    
171          $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type} ], forcecontent => 1);                  my $term = shift @{ $l->{"d:${v900}"} } || die;
172                    my $mfn = shift @{ $l->{"900_mfn:${v900}"} } || die;
173    
174          # output current progress indicator                  $log->info("$code -> $v900 : $term [$mfn]");
         my $last_p = 0;  
         sub progress {  
                 #return if (! $opts{q});        # FIXME  
                 my $current = shift;  
                 my $total = shift;  
                 my $p = int($current * 100 / $total);  
                 if ($p != $last_p) {  
                         printf STDERR ("%5d / %5d [%-51s] %-2d %% \r",$current,$total,"=" x ($p/2).">", $p );  
                         $last_p = $p;  
                 }  
         }  
175    
176          # now read database                  print HTML qq{ <li><a href="thes/$mfn.html">$term</a></li>\n} if (-e "out/thes/$mfn.html");
 print STDERR "using: $type...\n";  
177    
178          if ($type eq "isis") {                  unless ($l->{"a:${code}::"}) {
179                  my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";                          $log->warn("can't find 'a:${code}::'");
180                            next;
181                    }
182    
183                  $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);                  print HTML qq{ <ul>\n};
                 my $db = OpenIsis::open( $isis_db );  
184    
185                  my $max_rowid = OpenIsis::maxRowid( $db );                  foreach my $l2_v900 (@{ $l->{"a:${code}::"} }) {
186                            
187                            my $l2_code = shift @{ $l->{"code:${l2_v900}"} } || die;
188                            my $l2_term = shift @{ $l->{"d:$l2_v900"} } || die;
189                            my $l2_mfn = shift @{ $l->{"900_mfn:${l2_v900}"} } || die;
190    
191                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";                          $log->info("$l2_code -> $l2_v900 : $l2_term [$l2_mfn]");
192    
193                  my $path = $database;                          print HTML qq{  <li><a href="thes/$l2_mfn.html">$l2_term</a></li>\n} if (-e "out/thes/$l2_mfn.html");
194                            
195                            unless ($l->{"a:${l2_code}:"}) {
196                                    $log->warn("can't find 'a:${l2_code}:'");
197                                    next;
198                            }
199    
200                  for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {                          print HTML qq{  <ul>\n};
                         my $row = OpenIsis::read( $db, $row_id );  
                         if ($row && $row->{mfn}) {  
           
                                 progress($row->{mfn}, $max_rowid);  
201    
202                                  my $swishpath = $path."#".int($row->{mfn});                          foreach my $l3_v900 (@{ $l->{"a:${l2_code}:"} }) {
203    
204                                  if (my $xml = data2xml($type,$row,$add_xml)) {                                  my $l3_term = shift @{ $l->{"d:$l3_v900"} } || die;
205                                          $xml = $cp2utf->convert($xml);                                  my $l3_mfn = shift @{ $l->{"900_mfn:${l3_v900}"} } || die;
                                         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";  
                                 }  
                         }  
                 }  
                 print STDERR "\n";  
206    
207          } elsif ($type eq "excel") {                                  $log->info(" -> $l3_v900 : $l3_term [$l3_mfn]");
                 use Spreadsheet::ParseExcel;  
                 use Spreadsheet::ParseExcel::Utility qw(int2col);  
                   
                 $import2cp = Text::Iconv->new($config->{excel_codepage},$codepage);  
                 my $excel_file = $cfg -> val($database, 'excel_file') || die "$database doesn't have 'excel_file' defined!";  
   
                 my $sheet = x($config->{sheet}) || die "no sheet in $type.xml";  
                 my $start_row = x($config->{start_row}) || die "no start_row in $type.xml";  
   
                 my $oBook = Spreadsheet::ParseExcel::Workbook->Parse($excel_file) || die "can't open Excel file '$excel_file'";  
   
                 my $sheet_nr = 0;  
                 foreach my $oWks (@{$oBook->{Worksheet}}) {  
                         #print STDERR "-- SHEET $sheet_nr:", $oWks->{Name}, "\n";  
                         last if ($oWks->{Name} eq $sheet);  
                         $sheet_nr++;  
                 }  
208    
209                  my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];                                  print HTML qq{   <li><a href="thes/$l3_mfn.html">$l3_term</a></li>\n} if (-e "out/thes/$l3_mfn.html");
           
                 print STDERR "using sheet: ",$oWorksheet->{Name},"\n";  
                 defined ($oWorksheet) || die "can't find sheet '$sheet' in $excel_file";  
                 my $end_row = x($config->{end_row}) || $oWorksheet->{MaxRow};  
   
                 for(my $iR = $oWorksheet->{MinRow} ; defined $end_row && $iR <= $end_row ; $iR++) {  
                         my $row;  
                         for(my $iC = $oWorksheet->{MinCol} ; defined $oWorksheet->{MaxCol} && $iC <= $oWorksheet->{MaxCol} ; $iC++) {  
                                 my $cell = $oWorksheet->{Cells}[$iR][$iC];  
                                 if ($cell) {  
                                         $row->{int2col($iC)} = $cell->Value;  
                                 }  
210                          }                          }
211    
212                          progress($iR, $end_row);                          print HTML qq{  </ul>\n};
   
 #                       print "row[$iR/$end_row] ";  
 #                       foreach (keys %{$row}) {  
 #                               print "$_: ",$row->{$_},"\t";  
 #                       }  
 #                       print "\n";  
   
                         my $swishpath = $database."#".$iR;  
   
                         next if (! $row);  
   
                         if (my $xml = data2xml($type,$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          }          }
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.54  
changed lines
  Added in v.420

  ViewVC Help
Powered by ViewVC 1.1.26