/[webpac2]/trunk/run.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 /trunk/run.pl

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

revision 210 by dpavlin, Mon Dec 5 17:47:04 2005 UTC revision 416 by dpavlin, Sun Feb 26 23:21:50 2006 UTC
# Line 7  use File::Temp qw/tempdir/; Line 7  use File::Temp qw/tempdir/;
7  use Data::Dumper;  use Data::Dumper;
8  use lib './lib';  use lib './lib';
9    
10    use WebPAC::Common 0.02;
11  use WebPAC::Lookup;  use WebPAC::Lookup;
12  use WebPAC::Input::ISIS;  use WebPAC::Input 0.03;
13  use WebPAC::Store 0.03;  use WebPAC::Store 0.03;
14  use WebPAC::Normalize::XML;  use WebPAC::Normalize::XML;
15  use WebPAC::Output::TT;  use WebPAC::Output::TT;
16  use WebPAC::Output::Estraier 0.02;  use WebPAC::Output::Estraier '0.10';
17  use YAML qw/LoadFile/;  use YAML qw/LoadFile/;
18  use LWP::Simple;  use Getopt::Long;
19    use File::Path;
20    use Time::HiRes qw/time/;
21    
22  my $limit = shift @ARGV;  =head1 NAME
23    
24  my $config = LoadFile('conf/config.yml');  run.pl - start WebPAC indexing
25    
26  print "config = ",Dumper($config);  B<this command will probably go away. Don't get used to it!>
27    
28  die "no databases in config file!\n" unless ($config->{databases});  Options:
29    
30  while (my ($database, $db_config) = each %{ $config->{databases} }) {  =over 4
31    
32          my $type = lc($db_config->{input}->{type});  =item --offset 42
33    
34          die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');  start loading (all) databases at offset 42
35    
36          my $abs_path = abs_path($0);  =item --limit 100
         $abs_path =~ s#/[^/]*$#/#;  
37    
38          my $lookup = new WebPAC::Lookup(  limit loading to 100 records
                 lookup_file => $db_config->{input}->{lookup},  
         );  
39    
40          my $db_path = $config->{webpac}->{db_path} . '/' . $database;  =item --clean
41    
42    remove database and Hyper Estraier index before indexing
43    
44          my $log = $lookup->_get_logger;  =item --one=database_name
         $log->info("working on $database in $db_path");  
45    
46          my $isis = new WebPAC::Input::ISIS(  reindex just single database
                 code_page => $config->{webpac}->{webpac_encoding},  
                 limit_mfn => $db_config->{input}->{limit},  
         );  
47    
48          my $maxmfn = $isis->open(  =item --config conf/config.yml
                 filename => $db_config->{input}->{path},  
                 code_page => $db_config->{input}->{encoding},   # database encoding  
         );  
49    
50          my $path = './db/';  path to YAML configuration file
51    
52          my $db = new WebPAC::Store(  =back
                 path => $db_path,  
         );  
53    
54          my $n = new WebPAC::Normalize::XML(  =cut
         #       filter => { 'foo' => sub { shift } },  
                 db => $db,  
                 lookup_regex => $lookup->regex,  
                 lookup => $lookup,  
         );  
55    
56          $n->open(  my $offset;
57                  tag => $db_config->{normalize}->{tag},  my $limit;
                 xml_file => $db_config->{normalize}->{path},  
         );  
58    
59          my $out = new WebPAC::Output::TT(  my $clean = 0;
60                  include_path => $config->{webpac}->{template_path},  my $config = 'conf/config.yml';
61                  filters => { foo => sub { shift } },  my $debug = 0;
62          );  my $one_db_name;
63    
64          my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");  GetOptions(
65          $est_config->{database} = $database;          "limit=i" => \$limit,
66            "offset=i" => \$offset,
67            "clean" => \$clean,
68            "one=s" => \$one_db_name,
69            "config" => \$config,
70            "debug" => \$debug,
71    );
72    
73          $log->info("using HyperEstraier URL $est_config->{url}");  $config = LoadFile($config);
74    
75          my $est = new WebPAC::Output::Estraier(  print "config = ",Dumper($config) if ($debug);
                 %{ $est_config },  
         );  
76    
77          my $total_rows = 0;  die "no databases in config file!\n" unless ($config->{databases});
78    
79    my $total_rows = 0;
80    my $start_t = time();
81    
82    while (my ($database, $db_config) = each %{ $config->{databases} }) {
83    
84          for ( 0 ... $isis->size ) {          next if ($one_db_name && $database !~ m/$one_db_name/i);
85    
86                  my $row = $isis->fetch || next;          my $log = _new WebPAC::Common()->_get_logger();
87    
88                  my $mfn = $row->{'000'}->[0] || die "can't find MFN";          #
89            # open Hyper Estraier database
90            #
91    
92                  my $ds = $n->data_structure($row);          my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");
93            $est_config->{database} = $database;
94            $est_config->{clean} = $clean;
95            $est_config->{label} = $db_config->{name};
96    
97          #       print STDERR Dumper($row, $ds);          my $est = new WebPAC::Output::Estraier( %{ $est_config } );
98    
         #       my $html = $out->apply(  
         #               template => 'html_ffzg.tt',  
         #               data => $ds,  
         #       );  
99          #          #
100          #       # create test output          # now WebPAC::Store
101          #          #
102          #       my $file = sprintf('out/%02d.html', $mfn );          my $abs_path = abs_path($0);
103          #       open(my $fh, '>', $file) or die "can't open $file: $!";          $abs_path =~ s#/[^/]*$#/#;
104          #       print $fh $html;  
105          #       close($fh);          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
106    
107            if ($clean) {
108                    $log->info("creating new database $database in $db_path");
109                    rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
110            } else {
111                    $log->debug("working on $database in $db_path");
112            }
113    
114            my $db = new WebPAC::Store(
115                    path => $db_path,
116                    database => $database,
117                    debug => $debug,
118            );
119    
120    
121          #          #
122          #       $html =~ s#\s*[\n\r]+\s*##gs;          # now, iterate through input formats
123          #          #
         #       print STDERR $html;  
124    
125                  $est->add(          my @inputs;
126                          id => $mfn,          if (ref($db_config->{input}) eq 'ARRAY') {
127                          ds => $ds,                  @inputs = @{ $db_config->{input} };
128                          type => $config->{hyperestraier}->{type},          } elsif ($db_config->{input}) {
129                    push @inputs, $db_config->{input};
130            } else {
131                    $log->info("database $database doesn't have inputs defined");
132            }
133    
134            my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
135    
136            foreach my $input (@inputs) {
137    
138                    my $type = lc($input->{type});
139    
140                    die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
141    
142                    my $lookup = new WebPAC::Lookup(
143                            lookup_file => $input->{lookup},
144                    );
145    
146                    my $input_module = $config->{webpac}->{inputs}->{$type};
147    
148                    $log->info("working on input '$input->{path}' [$input->{type}] using $input_module lookup '$input->{lookup}'");
149    
150                    my $input_db = new WebPAC::Input(
151                            module => $input_module,
152                            code_page => $config->{webpac}->{webpac_encoding},
153                            limit => $limit || $input->{limit},
154                            offset => $offset,
155                            lookup => $lookup,
156                            recode => $input->{recode},
157                    );
158                    $log->logdie("can't create input using $input_module") unless ($input);
159    
160                    my $maxmfn = $input_db->open(
161                            path => $input->{path},
162                            code_page => $input->{encoding},        # database encoding
163                    );
164    
165                    my $n = new WebPAC::Normalize::XML(
166                    #       filter => { 'foo' => sub { shift } },
167                            db => $db,
168                            lookup_regex => $lookup->regex,
169                            lookup => $lookup,
170                            prefix => $input->{name},
171                  );                  );
172    
173                  $total_rows++;                  my $normalize_path = $input->{normalize}->{path};
174    
175                    if ($normalize_path =~ m/\.xml$/i) {
176                            $n->open(
177                                    tag => $input->{normalize}->{tag},
178                                    xml_file => $input->{normalize}->{path},
179                            );
180                    } elsif ($normalize_path =~ m/\.(?:yml|yaml)$/i) {
181                            $n->open_yaml(
182                                    path => $normalize_path,
183                                    tag => $input->{normalize}->{tag},
184                            );
185                    }
186    
187                    foreach my $pos ( 0 ... $input_db->size ) {
188    
189                            my $row = $input_db->fetch || next;
190    
191                            my $mfn = $row->{'000'}->[0];
192    
193                            if (! $mfn || $mfn !~ m#^\d+$#) {
194                                    $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
195                                    $mfn = $pos;
196                                    push @{ $row->{'000'} }, $pos;
197                            }
198    
199                            my $ds = $n->data_structure($row);
200    
201                            $est->add(
202                                    id => $input->{name} . "/" . $mfn,
203                                    ds => $ds,
204                                    type => $config->{hyperestraier}->{type},
205                            );
206    
207                            $total_rows++;
208                    }
209    
210          };          };
211    
212          $log->info("$total_rows records indexed");          my $dt = time() - $start_t;
213            $log->info("$total_rows records indexed in " .
214                    sprintf("%.2f sec [%.2f rec/sec]",
215                            $dt, ($total_rows / $dt)
216                    )
217            );
218    
219            #
220            # add Hyper Estraier links to other databases
221            #
222            if (ref($db_config->{links}) eq 'ARRAY') {
223                    foreach my $link (@{ $db_config->{links} }) {
224                            $log->info("adding link $database -> $link->{to} [$link->{credit}]");
225                            $est->add_link(
226                                    from => $database,
227                                    to => $link->{to},
228                                    credit => $link->{credit},
229                            );
230                    }
231            }
232    
233  }  }
234    

Legend:
Removed from v.210  
changed lines
  Added in v.416

  ViewVC Help
Powered by ViewVC 1.1.26