/[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 56 by dpavlin, Wed Jun 25 12:09:27 2003 UTC trunk2/all2all.pl revision 410 by dpavlin, Sun Sep 5 21:40:57 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 => 100,
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 => 80,
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            ],
45  );  );
46    
47  sub data2xml {  $log->info("rows: $maxmfn");
   
         use xmlify;  
48    
49          my $type = shift @_;  $webpac->open_import_xml(type => 'isis_hidra_ths');
         my $row = shift @_;  
         my $add_xml = shift @_;  
50    
51          my $xml;  while (my $rec = $webpac->fetch_rec) {
52    
53          use parse_format;          my @ds = $webpac->data_structure($rec);
54    
55          my $html = "";          # html formatted display output          if ($log->is_debug) {
56                    $log->debug("rec = ",Dumper($rec));
57          my %field_usage;        # counter for usage of each field                  $log->debug("ds = ",Dumper(\@ds));
   
         # 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} ;  
58          }          }
59    
60          foreach my $field (sort by_order keys %{$config->{indexer}}) {          next if (! @ds);
61    
62                  $field=x($field);          my $filename = $webpac->{'current_filename'};
63    
64                  $field_usage{$field}++;          if ($filename) {
65                    open(OUT,"> $filename") || $log->logdie("can't open output '$filename': $!");
66                  my $swish_data = "";                  print OUT $webpac->output(
67                  my $display_data = "";                          template => 'html.tt',
68                  my $line_delimiter;                          data => \@ds,
69                            headline => $webpac->{'headline'},
70                  my ($swish,$display);                  );
71                    close(OUT);
72                  my $tag = $type2tag{$type} || die "can't find which tag to use for type $type";          } else {
73                  foreach my $x (@{$config->{indexer}->{$field}->{$tag}}) {                  print $webpac->output(
74                            template => 'text.tt',
75                          my $format = x($x->{content});                          data => \@ds,
76                          my $delimiter = x($x->{delimiter}) || ' ';                          headline => $webpac->{'headline'},
77                    );
78                          my $repeat_off = 0;             # repeatable offset          }
   
                         my ($s,$d,$i) = (1,1,0);        # swish, display default  
                         $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");  
   
                         # 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/>';  
                         }  
   
                         # init vars so that we go into while...  
                         ($swish,$display) = (1,1);  
   
                         if ($swish || $display) {  
                                 ($swish,$display) = parse_format($type, $format,$row,$repeat_off++,$import2cp);  
                                 # filter="name" ; filter this field through  
                                 # filter/[name].pm  
                                 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);  
                                         }  
                                 }  
                         }  
                 }  
   
   
                 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;  
79    
80                          $xml .= xmlify($field."_swish", unac_string($codepage,$swish_data));          my $headline = $webpac->{'headline'};
                 }  
81    
82            my $f = $filename;
83            $f =~ s!out/!!;
84    
85          }          # save into index
86            foreach my $ds (@ds) {
87                    next if (! $ds->{'swish'});
88    
89          # dump formatted output in <html>                  $index->insert(
90          if ($html) {                          index_name => $ds->{'tag'},
91                  $xml .= xmlify("html",$html);                          path => $f,
92          }                          headline => $headline,
93                                    words => join(" ",@{$ds->{'swish'}})
94          if ($xml) {                  );
                 $xml .= $add_xml if ($add_xml);  
                 return "<xml>\n$xml</xml>\n";  
         } else {  
                 return;  
95          }          }
 }  
   
 ##########################################################################  
96    
97  # read configuration for this script          # save into sorted index (thesaurus)
98  my $cfg = new Config::IniFiles( -file => $config_file );          foreach my $ds (@ds) {
99                    next if (! $ds->{'index'});
100    
101  # read global.conf configuration                  $thes->{$ds->{'tag'}} ||= new WebPAC::Index;
 my $cfg_global = new Config::IniFiles( -file => 'global.conf' );  
102    
103  # open index                  $thes->{$ds->{'tag'}}->insert(
104  $index = new index_DBI(                          path => $f,
105                  $cfg_global->val('global', 'dbi_dbd'),                          headline => $headline,
106                  $cfg_global->val('global', 'dbi_dsn'),                  );
                 $cfg_global->val('global', 'dbi_user'),  
                 $cfg_global->val('global', 'dbi_passwd') || '',  
         );  
   
 foreach my $database ($cfg->Sections) {  
   
         my $type = lc($cfg -> val($database, 'type')) || die "$database doesn't have 'type' defined";  
         my $add_xml = $cfg -> val($database, 'xml');    # optional  
   
 print STDERR "reading ./import_xml/$type.xml\n";  
   
         $config=XMLin("./import_xml/$type.xml", forcearray => [ $type2tag{$type} ], forcecontent => 1);  
   
         # output current progress indicator  
         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;  
                 }  
107          }          }
108    
109          # now read database  #       print Dumper(\@ds);
 print STDERR "using: $type...\n";  
110    
111          if ($type eq "isis") {  }
                 my $isis_db = $cfg -> val($database, 'isis_db') || die "$database doesn't have 'isis_db' defined!";  
   
                 $import2cp = Text::Iconv->new($config->{isis_codepage},$codepage);  
                 my $db = OpenIsis::open( $isis_db );  
112    
113                  my $max_rowid = OpenIsis::maxRowid( $db );  if ($log->is_debug) {
114            $log->debug("lookup hash: ",Dumper($webpac->{'lookup'}));
115                  print STDERR "Reading database: $isis_db [$max_rowid rows]\n";          $log->debug("data hash: ",Dumper($webpac->{'data'}));
116            foreach my $t (keys %{$thes}) {
117                  my $path = $database;                  $log->debug("thesaurus $t hash: ",Dumper($thes->{$t}));
   
                 for (my $row_id = 1; $row_id <= $max_rowid; $row_id++ ) {  
                         my $row = OpenIsis::read( $db, $row_id );  
                         if ($row && $row->{mfn}) {  
           
                                 progress($row->{mfn}, $max_rowid);  
   
                                 my $swishpath = $path."#".int($row->{mfn});  
   
                                 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";  
                                 }  
                         }  
                 }  
                 print STDERR "\n";  
   
         } elsif ($type eq "excel") {  
                 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}) - 1 || 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++;  
                 }  
   
                 my $oWorksheet = $oBook->{Worksheet}[$sheet_nr];  
           
                 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 = $start_row ; 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;  
                                 }  
                         }  
   
                         progress($iR, $end_row);  
   
 #                       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";  
                         }  
                 }  
118          }          }
119  }  }
120    
 # call this to commit index  
121  $index->close;  $index->close;
122    
 1;  
 __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.  
   
 =head1 AUTHOR  
   
 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  
   
 =cut  

Legend:
Removed from v.56  
changed lines
  Added in v.410

  ViewVC Help
Powered by ViewVC 1.1.26