/[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 113 by dpavlin, Wed Nov 23 00:14:05 2005 UTC revision 560 by dpavlin, Sun Jul 2 14:40: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::Common 0.02;
10  use WebPAC::Lookup;  use WebPAC::Lookup;
11  use WebPAC::Input::ISIS;  use WebPAC::Input 0.03;
12  use WebPAC::DB;  use WebPAC::Store 0.03;
13  use WebPAC::Normalize::XML;  use WebPAC::Normalize;
14  use WebPAC::Output::TT;  use WebPAC::Output::TT;
15  use WebPAC::Output::Estraier;  use WebPAC::Validate;
16    use YAML qw/LoadFile/;
17    use Getopt::Long;
18    use File::Path;
19    use Time::HiRes qw/time/;
20    use File::Slurp;
21    use MARC::Record 2.0;   # need 2.0 for utf-8 encoding see marcpm.sf.net
22    use MARC::Lint;
23    use Data::Dump qw/dump/;
24    
25  my $limit = shift @ARGV;  =head1 NAME
26    
27  my $abs_path = abs_path($0);  run.pl - start WebPAC indexing
 $abs_path =~ s#/[^/]*$#/#;  
28    
29  my $isis_file = '/data/isis_data/ps/LIBRI/LIBRI';  B<this command will probably go away. Don't get used to it!>
30    
31  my $lookup = new WebPAC::Lookup(  Options:
         lookup_file => "$abs_path/conf/lookup/isis.pm",  
 );  
32    
33  my $isis = new WebPAC::Input::ISIS(  =over 4
         code_page => 'ISO-8859-2',      # application encoding  
         limit_mfn => $limit,  
 );  
34    
35  my $maxmfn = $isis->open(  =item --offset 42
         filename => $isis_file,  
         code_page => '852',             # database encoding  
 );  
36    
37  my $path = './db/';  start loading (all) databases at offset 42
38    
39  my $db = new WebPAC::DB(  =item --limit 100
         path => $path,  
 );  
40    
41  my $n = new WebPAC::Normalize::XML(  limit loading to 100 records
 #       filter => { 'foo' => sub { shift } },  
         db => $db,  
         lookup_regex => $lookup->regex,  
         lookup => $lookup,  
 );  
42    
43  $n->open(  =item --clean
         tag => 'isis',  
         xml_file => "$abs_path/conf/normalize/isis_ffzg.xml",  
 );  
44    
45  my $out = new WebPAC::Output::TT(  remove database and Hyper Estraier index before indexing
46          include_path => "$abs_path/conf/output/tt",  
47          filters => { foo => sub { shift } },  =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  my $est = new WebPAC::Output::Estraier(  Force dump or input and marc record for debugging.
83          url => 'http://localhost:1978/node/webpac2',  
84          user => 'admin',  =back
85          passwd => 'admin',  
86          database => 'ps',  =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});
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  for ( 0 ... $isis->size ) {  my @links;
146    my $indexer;
147    
148          my $row = $isis->fetch || next;  my $lint = new MARC::Lint if ($marc_lint);
149    
150          my $mfn = $row->{'000'}->[0] || die "can't find MFN";  while (my ($database, $db_config) = each %{ $config->{databases} }) {
151    
152          my $ds = $n->data_structure($row);          my ($only_database,$only_input) = split(m#/#, $only_filter) if ($only_filter);
153            next if ($only_database && $database !~ m/$only_database/i);
154    
155  #       print STDERR Dumper($row, $ds);          if ($use_indexer) {
156                    my $indexer_config = $config->{$use_indexer} || $log->logdie("can't find '$use_indexer' part in confguration");
157  #       my $html = $out->apply(                  $indexer_config->{database} = $database;
158  #               template => 'html_ffzg.tt',                  $indexer_config->{clean} = $clean;
159  #               data => $ds,                  $indexer_config->{label} = $db_config->{name};
 #       );  
 #  
 #       # create test output  
 #  
 #       my $file = sprintf('out/%02d.html', $mfn );  
 #       open(my $fh, '>', $file) or die "can't open $file: $!";  
 #       print $fh $html;  
 #       close($fh);  
 #  
 #       $html =~ s#\s*[\n\r]+\s*##gs;  
 #  
 #       print STDERR $html;  
   
         $est->add(  
                 id => $mfn,  
                 ds => $ds,  
                 type => 'search',  
         );  
160    
161          $total_rows++;                  if ($use_indexer eq 'hyperestraier') {
162    
163  };                          # open Hyper Estraier database
164                            use WebPAC::Output::Estraier '0.10';
165                            $indexer = new WebPAC::Output::Estraier( %{ $indexer_config } );
166                    
167                    } elsif ($use_indexer eq 'kinosearch') {
168    
169  my $log = $lookup->_get_logger;                          # open KinoSearch
170                            use WebPAC::Output::KinoSearch;
171                            $indexer_config->{clean} = 1 unless (-e $indexer_config->{index_path});
172                            $indexer = new WebPAC::Output::KinoSearch( %{ $indexer_config } );
173    
174                    } else {
175                            $log->logdie("unknown use_indexer: $use_indexer");
176                    }
177    
178                    $log->logide("can't continue without valid indexer") unless ($indexer);
179            }
180    
181    
182            #
183            # now WebPAC::Store
184            #
185            my $abs_path = abs_path($0);
186            $abs_path =~ s#/[^/]*$#/#;
187    
188            my $db_path = $config->{webpac}->{db_path} . '/' . $database;
189    
190            if ($clean) {
191                    $log->info("creating new database '$database' in $db_path");
192                    rmtree( $db_path ) || $log->warn("can't remove $db_path: $!");
193            } else {
194                    $log->info("working on database '$database' in $db_path");
195            }
196    
197            my $db = new WebPAC::Store(
198                    path => $db_path,
199                    database => $database,
200                    debug => $debug,
201            );
202    
203    
204            #
205            # now, iterate through input formats
206            #
207    
208            my @inputs;
209            if (ref($db_config->{input}) eq 'ARRAY') {
210                    @inputs = @{ $db_config->{input} };
211            } elsif ($db_config->{input}) {
212                    push @inputs, $db_config->{input};
213            } else {
214                    $log->info("database $database doesn't have inputs defined");
215            }
216    
217            my @supported_inputs = keys %{ $config->{webpac}->{inputs} };
218    
219            foreach my $input (@inputs) {
220    
221                    next if ($only_input && ($input->{name} !~ m#$only_input#i && $input->{type} !~ m#$only_input#i));
222    
223                    my $type = lc($input->{type});
224    
225                    die "I know only how to handle input types ", join(",", @supported_inputs), " not '$type'!\n" unless (grep(/$type/, @supported_inputs));
226    
227                    my $lookup = new WebPAC::Lookup(
228                            lookup_file => $input->{lookup},
229                    ) if ($input->{lookup});
230    
231                    my $input_module = $config->{webpac}->{inputs}->{$type};
232    
233                    $log->info("working on input '$input->{name}' in $input->{path} [type: $input->{type}] using $input_module",
234                            $input->{lookup} ? "lookup '$input->{lookup}'" : ""
235                    );
236    
237                    my $input_db = new WebPAC::Input(
238                            module => $input_module,
239                            code_page => $config->{webpac}->{webpac_encoding},
240                            limit => $limit || $input->{limit},
241                            offset => $offset,
242                            lookup => $lookup,
243                            recode => $input->{recode},
244                            stats => $stats,
245                    );
246                    $log->logdie("can't create input using $input_module") unless ($input);
247    
248                    my $maxmfn = $input_db->open(
249                            path => $input->{path},
250                            code_page => $input->{encoding},        # database encoding
251                            %{ $input },
252                    );
253    
254                    my @norm_array = ref($input->{normalize}) eq 'ARRAY' ?
255                            @{ $input->{normalize} } : ( $input->{normalize} );
256    
257                    if ($marc_normalize) {
258                            @norm_array = ( {
259                                    path => $marc_normalize,
260                                    output => $marc_output || 'out/marc/' . $database . '-' . $input->{name} . '.marc',
261                            } );
262                    }
263    
264                    foreach my $normalize (@norm_array) {
265    
266                            my $normalize_path = $normalize->{path} || $log->logdie("can't find normalize path in config");
267    
268                            $log->logdie("Found '$normalize_path' as normalization file which isn't supported any more!") unless ( $normalize_path =~ m!\.pl$!i );
269    
270                            my $rules = read_file( $normalize_path ) or die "can't open $normalize_path: $!";
271    
272                            $log->info("Using $normalize_path for normalization...");
273    
274                            my $marc_fh;
275                            if (my $path = $normalize->{output}) {
276                                    open($marc_fh, '>', $path) ||
277                                            $log->logdie("can't open MARC output $path: $!");
278    
279                                    $log->info("Creating MARC export file $path", $marc_lint ? ' (with lint)' : '', "\n");
280                            }
281    
282                            # reset position in database
283                            $input_db->seek(1);
284    
285                            foreach my $pos ( 0 ... $input_db->size ) {
286    
287                                    my $row = $input_db->fetch || next;
288    
289                                    my $mfn = $row->{'000'}->[0];
290    
291                                    if (! $mfn || $mfn !~ m#^\d+$#) {
292                                            $log->warn("record $pos doesn't have valid MFN but '$mfn', using $pos");
293                                            $mfn = $pos;
294                                            push @{ $row->{'000'} }, $pos;
295                                    }
296    
297    
298                                    if ($validate) {
299                                            my @errors = $validate->validate_errors( $row );
300                                            $log->error( "MFN $mfn validation errors:\n", join("\n", @errors) ) if (@errors);
301                                    }
302    
303                                    my $ds = WebPAC::Normalize::data_structure(
304                                            row => $row,
305                                            rules => $rules,
306                                            lookup => $lookup ? $lookup->lookup_hash : undef,
307                                            marc_encoding => 'utf-8',
308                                    );
309    
310                                    $db->save_ds(
311                                            id => $mfn,
312                                            ds => $ds,
313                                            prefix => $input->{name},
314                                    ) if ($ds && !$stats);
315    
316                                    $indexer->add(
317                                            id => $input->{name} . "/" . $mfn,
318                                            ds => $ds,
319                                            type => $config->{$use_indexer}->{type},
320                                    ) if ($indexer && $ds);
321    
322                                    if ($marc_fh) {
323                                            my $marc = new MARC::Record;
324                                            $marc->encoding( 'utf-8' );
325                                            my @marc_fields = WebPAC::Normalize::_get_marc_fields();
326                                            if (! @marc_fields) {
327                                                    $log->warn("MARC record $mfn is empty, skipping");
328                                            } else {
329                                                    $marc->add_fields( @marc_fields );
330    
331                                                    if ($marc_lint) {
332                                                            $lint->check_record( $marc );
333                                                            my $err = join( "\n", $lint->warnings );
334                                                            $log->error("MARC lint detected warning on MFN $mfn\n",
335                                                                    "Original imput row: ",dump($row), "\n",
336                                                                    "Normalized MARC row: ",dump(@marc_fields), "\n",
337                                                                    "MARC lint warnings: ",$err,"\n"
338                                                            ) if ($err);
339                                                    }
340    
341                                                    if ($marc_dump) {
342                                                            $log->info("MARC record on MFN $mfn\n",
343                                                                    "Original imput row: ",dump($row), "\n",
344                                                                    "Normalized MARC row: ",dump(@marc_fields), "\n",
345                                                            );
346                                                    }
347    
348                                                    print $marc_fh $marc->as_usmarc;
349                                            }
350                                    }
351    
352                                    $total_rows++;
353                            }
354    
355                            $log->info("statistics of fields usage:\n", $input_db->stats) if ($stats);
356    
357                            # close MARC file
358                            close($marc_fh) if ($marc_fh);
359    
360                    }
361    
362            }
363    
364            eval { $indexer->finish } if ($indexer && $indexer->can('finish'));
365    
366            my $dt = time() - $start_t;
367            $log->info("$total_rows records ", $indexer ? "indexed " : "",
368                    sprintf("in %.2f sec [%.2f rec/sec]",
369                            $dt, ($total_rows / $dt)
370                    )
371            );
372    
373  $log->info("$total_rows records indexed");          #
374            # add Hyper Estraier links to other databases
375            #
376            if (ref($db_config->{links}) eq 'ARRAY' && $use_indexer) {
377                    foreach my $link (@{ $db_config->{links} }) {
378                            if ($use_indexer eq 'hyperestraier') {
379                                    $log->info("saving link $database -> $link->{to} [$link->{credit}]");
380                                    push @links, {
381                                            from => $database,
382                                            to => $link->{to},
383                                            credit => $link->{credit},
384                                    };
385                            } else {
386                                    $log->warn("NOT IMPLEMENTED WITH $use_indexer: adding link $database -> $link->{to} [$link->{credit}]");
387                            }
388                    }
389            }
390    
391    }
392    
393    foreach my $link (@links) {
394            $log->info("adding link $link->{from} -> $link->{to} [$link->{credit}]");
395            $indexer->add_link( %{ $link } );
396    }

Legend:
Removed from v.113  
changed lines
  Added in v.560

  ViewVC Help
Powered by ViewVC 1.1.26