/[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 213 by dpavlin, Mon Dec 5 17:47:23 2005 UTC revision 600 by dpavlin, Thu Jul 13 21:31:32 2006 UTC
# Line 4  use strict; Line 4  use strict;
4    
5  use Cwd qw/abs_path/;  use Cwd qw/abs_path/;
6  use File::Temp qw/tempdir/;  use File::Temp qw/tempdir/;
 use Data::Dumper;  
7  use lib './lib';  use lib './lib';
8    
9  use WebPAC::Lookup;  use WebPAC::Common 0.02;
10  use WebPAC::Input::ISIS;  use WebPAC::Lookup 0.03;
11    use WebPAC::Input 0.07;
12  use WebPAC::Store 0.03;  use WebPAC::Store 0.03;
13  use WebPAC::Normalize::XML;  use WebPAC::Normalize 0.11;
14  use WebPAC::Output::TT;  use WebPAC::Output::TT;
15  use WebPAC::Output::Estraier 0.02;  use WebPAC::Validate;
16    use WebPAC::Output::MARC;
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    use File::Slurp;
22    use Data::Dump qw/dump/;
23    use Storable qw/dclone/;
24    
25  my $limit = shift @ARGV;  =head1 NAME
26    
27  my $config = LoadFile('conf/config.yml');  run.pl - start WebPAC indexing
28    
29  print "config = ",Dumper($config);  B<this command will probably go away. Don't get used to it!>
30    
31    Options:
32    
33    =over 4
34    
35    =item --offset 42
36    
37    start loading (all) databases at offset 42
38    
39    =item --limit 100
40    
41    limit loading to 100 records
42    
43    =item --clean
44    
45    remove database and Hyper Estraier index before indexing
46    
47    =item --only=database_name/input_filter
48    
49    reindex just single database (legacy name is --one)
50    
51    C</input_filter> is optional part which can be C<name>
52    or C<type> from input
53    
54    =item --config conf/config.yml
55    
56    path to YAML configuration file
57    
58    =item --stats
59    
60    disable indexing and dump statistics about field and subfield
61    usage for each input
62    
63    =item --validate path/to/validation_file
64    
65    turn on extra validation of imput records, see L<WebPAC::Validation>
66    
67    =item --marc-normalize conf/normalize/mapping.pl
68    
69    This option specifies normalisation file for MARC creation
70    
71    =item --marc-output out/marc/test.marc
72    
73    Optional path to output file
74    
75    =item --marc-lint
76    
77    By default turned on if C<--marc-normalize> is used. You can disable lint
78    messages with C<--no-marc-lint>.
79    
80    =item --marc-dump
81    
82    Force dump or input and marc record for debugging.
83    
84    =back
85    
86    =cut
87    
88    my $offset;
89    my $limit;
90    
91    my $clean = 0;
92    my $config = 'conf/config.yml';
93    my $debug = 0;
94    my $only_filter;
95    my $stats = 0;
96    my $validate_path;
97    my ($marc_normalize, $marc_output);
98    my $marc_lint = 1;
99    my $marc_dump = 0;
100    
101    GetOptions(
102            "limit=i" => \$limit,
103            "offset=i" => \$offset,
104            "clean" => \$clean,
105            "one=s" => \$only_filter,
106            "only=s" => \$only_filter,
107            "config" => \$config,
108            "debug+" => \$debug,
109            "stats" => \$stats,
110            "validate=s" => \$validate_path,
111            "marc-normalize=s" => \$marc_normalize,
112            "marc-output=s" => \$marc_output,
113            "marc-lint!" => \$marc_lint,
114            "marc-dump!" => \$marc_dump,
115    );
116    
117    $config = LoadFile($config);
118    
119    print "config = ",dump($config) if ($debug);
120    
121  die "no databases in config file!\n" unless ($config->{databases});  die "no databases in config file!\n" unless ($config->{databases});
122    
123    my $log = _new WebPAC::Common()->_get_logger();
124    $log->info( "-" x 79 );
125    
126    my $validate;
127    $validate = new WebPAC::Validate(
128            path => $validate_path,
129    ) if ($validate_path);
130    
131    my $use_indexer = $config->{use_indexer} || 'hyperestraier';
132    if ($stats) {
133            $log->debug("option --stats disables update of indexing engine...");
134            $use_indexer = undef;
135    } else {
136            $log->info("using $use_indexer indexing engine...");
137    }
138    
139    # disable indexing when creating marc
140    $use_indexer = undef if ($marc_normalize);
141    
142  my $total_rows = 0;  my $total_rows = 0;
143    my $start_t = time();
144    
145    my @links;
146    my $indexer;
147    
148  while (my ($database, $db_config) = each %{ $config->{databases} }) {  while (my ($database, $db_config) = each %{ $config->{databases} }) {
149    
150          my $type = lc($db_config->{input}->{type});          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
151            next if ($only_database && $database !~ m/$only_database/i);
152    
153          die "I know only how to handle input type isis, not '$type'!\n" unless ($type eq 'isis');          if ($use_indexer) {
154                    my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
155                    $indexer_config->{database} = $database;
156                    $indexer_config->{clean} = $clean;
157                    $indexer_config->{label} = $db_config->{name};
158    
159                    if ($use_indexer eq 'hyperestraier') {
160    
161                            # open Hyper Estraier database
162                            use WebPAC::Output::Estraier '0.10';
163                            $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
164                    
165                    } elsif ($use_indexer eq 'kinosearch') {
166    
167                            # open KinoSearch
168                            use WebPAC::Output::KinoSearch;
169                            $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
170                            $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
171    
172          my $abs_path = abs_path($0);                  } else {
173          $abs_path =~ s#/[^/]*$#/#;                          $log->logdie("unknown use_indexer: $use_indexer");
174                    }
175    
176          my $lookup = new WebPAC::Lookup(                  $log->logide("can't continue without valid indexer") unless ($indexer);
177                  lookup_file => $db_config->{input}->{lookup},          }
         );  
178    
         my $db_path = $config->{webpac}->{db_path} . '/' . $database;  
179    
180            #
181            # now WebPAC::Store
182            #
183            my $abs_path = abs_path($0);
184            $abs_path =~ s#/[^/]*$#/#;
185    
186          my $log = $lookup->_get_logger;          my $db_path = $config->{webpac}->{db_path} . '/' . $database;
         $log->info("working on $database in $db_path");  
187    
188          my $path = './db/';          if ($clean) {
189                    $log->info("creating new database '$database' in $db_path");
190                    rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
191            } else {
192                    $log->info("working on database '$database' in $db_path");
193            }
194    
195          my $db = new WebPAC::Store(          my $db = new WebPAC::Store(
196                  path => $db_path,                  path => $db_path,
197                    database => $database,
198                    debug => $debug,
199          );          );
200    
         my $est_config = $config->{hyperestraier} || $log->logdie("can't find 'hyperestraier' part in confguration");  
         $est_config->{database} = $database;  
   
         $log->info("using HyperEstraier URL $est_config->{masterurl}");  
   
         my $est = new WebPAC::Output::Estraier(  
                 %{ $est_config },  
         );  
201    
202          #          #
203          # now, iterate through input formats          # now, iterate through input formats
# Line 67  while (my ($database, $db_config) = each Line 206  while (my ($database, $db_config) = each
206          my @inputs;          my @inputs;
207          if (ref($db_config->{input}) eq 'ARRAY') {          if (ref($db_config->{input}) eq 'ARRAY') {
208                  @inputs = @{ $db_config->{input} };                  @inputs = @{ $db_config->{input} };
209          } else {          } elsif ($db_config->{input}) {
210                  push @inputs, $db_config->{input};                  push @inputs, $db_config->{input};
211            } else {
212                    $log->info("database $database doesn't have inputs defined");
213          }          }
214    
215            my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
216    
217          foreach my $input (@inputs) {          foreach my $input (@inputs) {
                 $log->info("working on input $input->{path} [$input->{type}]");  
218    
219                  my $isis = new WebPAC::Input::ISIS(                  next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
                         code_page => $config->{webpac}->{webpac_encoding},  
                         limit_mfn => $input->{limit},  
                 );  
220    
221                  my $maxmfn = $isis->open(                  my $type = lc($input->{type});
222                          filename => $input->{path},  
223                          code_page => $input->{encoding},        # database encoding                  die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
224    
225                    my $lookup;
226                    if ($input->{lookup}) {
227                            $lookup = new WebPAC::Lookup(
228                                    lookup_file => $input->{lookup},
229                            );
230                            delete( $input->{lookup} );
231                    }
232    
233                    my $input_module = $config->{webpac}->{inputs}->{$type};
234    
235                    $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
236                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
237                  );                  );
238    
239                  my $n = new WebPAC::Normalize::XML(                  my $input_db = new WebPAC::Input(
240                  #       filter => { 'foo' => sub { shift } },                          module => $input_module,
241                          db => $db,                          encoding => $config->{webpac}->{webpac_encoding},
242                          lookup_regex => $lookup->regex,                          limit => $limit || $input->{limit},
243                          lookup => $lookup,                          offset => $offset,
244                            lookup_coderef => sub {
245                                    my $rec = shift || return;
246                                    $lookup->add( $rec );
247                            },
248                            recode => $input->{recode},
249                            stats => $stats,
250                            modify_records => $input->{modify_records},
251                  );                  );
252                    $log->logdie("can't create input using $input_module") unless ($input);
253    
254                  $n->open(                  my $maxmfn = $input_db->open(
255                          tag => $input->{normalize}->{tag},                          path => $input->{path},
256                          xml_file => $input->{normalize}->{path},                          code_page => $input->{encoding},        # database encoding
257                            %{ $input },
258                  );                  );
259    
260                  for ( 0 ... $isis->size ) {                  my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
261                            @{ $input->{normalize} } : ( $input->{normalize} );
262    
263                    if ($marc_normalize) {
264                            @norm_array = ( {
265                                    path => $marc_normalize,
266                                    output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
267                            } );
268                    }
269    
270                          my $row = $isis->fetch || next;                  foreach my $normalize (@norm_array) {
271    
272                          my $mfn = $row->{'000'}->[0] || die "can't find MFN";                          my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
273    
274                          my $ds = $n->data_structure($row);                          $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
275    
276                          $est->add(                          my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
277                                  id => $input->{name} . "#" . $mfn,  
278                                  ds => $ds,                          $log->info("Using $normalize_path for normalization...");
279                                  type => $config->{hyperestraier}->{type},  
280                          );                          my $marc = new WebPAC::Output::MARC(
281                                    path => $normalize->{output},
282                                    lint => $marc_lint,
283                                    dump => $marc_dump,
284                            ) if ($normalize->{output});
285    
286                            # reset position in database
287                            $input_db->seek(1);
288    
289                            foreach my $pos ( 0 ... $input_db->size ) {
290    
291                                    my $row = $input_db->fetch || next;
292    
293                                    my $mfn = $row->{'000'}->[0];
294    
295                                    if (! $mfn || $mfn !~ m#^\d+$#) {
296                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
297                                            $mfn = $pos;
298                                            push @{ $row->{'000'} }, $pos;
299                                    }
300    
301    
302                                    if ($validate) {
303                                            my @errors = $validate->validate_errors( $row );
304                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
305                                    }
306    
307                                    my $ds_config = dclone($db_config);
308    
309                                    # default values -> database key
310                                    $ds_config->{_} = $database;
311    
312                                    # current mfn
313                                    $ds_config->{_mfn} = $mfn;
314    
315                                    # attach current input
316                                    $ds_config->{input} = $input;
317    
318                                    my $ds = WebPAC::Normalize::data_structure(
319                                            row => $row,
320                                            rules => $rules,
321                                            lookup => $lookup ? $lookup->lookup_hash : undef,
322                                            config => $ds_config,
323                                            marc_encoding => 'utf-8',
324                                    );
325    
326                                    $db->save_ds(
327                                            id => $mfn,
328                                            ds => $ds,
329                                            prefix => $input->{name},
330                                    ) if ($ds && !$stats);
331    
332                                    $indexer->add(
333                                            id => $input->{name} . "/" . $mfn,
334                                            ds => $ds,
335                                            type => $config->{$use_indexer}->{type},
336                                    ) if ($indexer && $ds);
337    
338                                    if ($marc) {
339                                            my $i = 0;
340    
341                                            while (my $fields = WebPAC::Normalize::_get_marc_fields( fetch_next => 1 ) ) {
342                                                    $marc->add(
343                                                            id => $mfn . ( $i ? "/$i" : '' ),
344                                                            fields => $fields,
345                                                            leader => WebPAC::Normalize::marc_leader(),
346                                                            row => $row,
347                                                    );
348                                                    $i++;
349                                            }
350    
351                                            $log->info("Created $i instances of MFN $mfn\n") if ($i > 1);
352                                    }
353    
354                                    $total_rows++;
355                            }
356    
357                            $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
358    
359                            # close MARC file
360                            $marc->finish if ($marc);
361    
                         $total_rows++;  
362                  }                  }
363    
364          };          }
365    
366            eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
367    
368            my $dt = time() - $start_t;
369            $log->info("$total_rows records ", $indexer ? "indexed " : "",
370                    sprintf("in %.2f sec [%.2f rec/sec]",
371                            $dt, ($total_rows / $dt)
372                    )
373            );
374    
375            #
376            # add Hyper Estraier links to other databases
377            #
378            if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
379                    foreach my $link (@{ $db_config->{links} }) {
380                            if ($use_indexer eq 'hyperestraier') {
381                                    $log->info("saving link $database -> $link->{to} [$link->{credit}]");
382                                    push @links, {
383                                            from => $database,
384                                            to => $link->{to},
385                                            credit => $link->{credit},
386                                    };
387                            } else {
388                                    $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
389                            }
390                    }
391            }
392    
         $log->info("$total_rows records indexed");  
393  }  }
394    
395    foreach my $link (@links) {
396            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
397            $indexer->add_link( %{ $link } );
398    }

Legend:
Removed from v.213  
changed lines
  Added in v.600

  ViewVC Help
Powered by ViewVC 1.1.26