source: main/trunk/greenstone2/perllib/inexport.pm@ 22445

Last change on this file since 22445 was 22445, checked in by davidb, 14 years ago

Incorporation of top-level parallel_import functionality into code base.

  • Property svn:executable set to *
File size: 33.9 KB
Line 
1###########################################################################
2#
3# inexport.pm -- useful class to support import.pl and export.pl
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package inexport;
27
28use strict;
29
30no strict 'refs'; # allow filehandles to be variables and vice versa
31no strict 'subs'; # allow barewords (eg STDERR) as function arguments
32
33use arcinfo;
34use colcfg;
35use dbutil;
36use plugin;
37use plugout;
38use manifest;
39use inexport;
40use dbutil;
41use util;
42use scriptutil;
43use FileHandle;
44use gsprintf 'gsprintf';
45use printusage;
46use parse2;
47
48use File::Basename;
49
50sub new
51{
52 my $class = shift (@_);
53 my ($mode,$argv,$options,$opt_listall_options) = @_;
54
55 my $self = { 'xml' => 0, 'mode' => $mode };
56
57 # general options available to all plugins
58 my $arguments = $options->{'args'};
59 my $intArgLeftinAfterParsing = parse2::parse($argv,$arguments,$self,"allow_extra_options");
60 # Parse returns -1 if something has gone wrong
61 if ($intArgLeftinAfterParsing == -1)
62 {
63 &PrintUsage::print_txt_usage($options, "{import.params}");
64 die "\n";
65 }
66
67 my $language = $self->{'language'};
68 # If $language has been specified, load the appropriate resource bundle
69 # (Otherwise, the default resource bundle will be loaded automatically)
70 if ($language && $language =~ /\S/) {
71 &gsprintf::load_language_specific_resource_bundle($language);
72 }
73
74 if ($self->{'listall'}) {
75 if ($self->{'xml'}) {
76 &PrintUsage::print_xml_usage($opt_listall_options);
77 }
78 else
79 {
80 &PrintUsage::print_txt_usage($opt_listall_options,"{export.params}");
81 }
82 die "\n";
83 }
84
85
86 if ($self->{'xml'}) {
87 &PrintUsage::print_xml_usage($options);
88 print "\n";
89 return;
90 }
91
92 if ($self->{'gli'}) { # the gli wants strings to be in UTF-8
93 &gsprintf::output_strings_in_UTF8;
94 }
95
96 # now check that we had exactly one leftover arg, which should be
97 # the collection name. We don't want to do this earlier, cos
98 # -xml arg doesn't need a collection name
99 # Or if the user specified -h, then we output the usage also
100 if ($intArgLeftinAfterParsing != 1 || (@$argv && $argv->[0] =~ /^\-+h/))
101 {
102 &PrintUsage::print_txt_usage($options, "{import.params}");
103 die "\n";
104 }
105
106 $self->{'close_out'} = 0;
107 my $out = $self->{'out'};
108 if ($out !~ /^(STDERR|STDOUT)$/i) {
109 open (OUT, ">$out") ||
110 (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $out) && die);
111 $out = 'import::OUT';
112 $self->{'close_out'} = 1;
113 }
114 $out->autoflush(1);
115 $self->{'out'} = $out;
116
117 # @ARGV should be only one item, the name of the collection
118 $self->{'collection'} = shift @$argv;
119
120 if ((defined $self->{'jobs'}) && ($self->{'jobs'}>1)) {
121 require ParallelInexport;
122 }
123
124 return bless $self, $class;
125}
126
127sub get_collection
128{
129 my $self = shift @_;
130
131 return $self->{'collection'};
132}
133
134
135sub read_collection_cfg
136{
137 my $self = shift @_;
138 my ($collection,$options) = @_;
139
140 my $collectdir = $self->{'collectdir'};
141 my $site = $self->{'site'};
142 my $out = $self->{'out'};
143
144 if (($collection = &colcfg::use_collection($site, $collection, $collectdir)) eq "") {
145 &PrintUsage::print_txt_usage($options, "{import.params}");
146 die "\n";
147 }
148
149 # add collection's perllib dir into include path in
150 # case we have collection specific modules
151 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
152
153 # check that we can open the faillog
154 my $faillog = $self->{'faillog'};
155 if ($faillog eq "") {
156 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
157 }
158 open (FAILLOG, ">$faillog") ||
159 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
160
161
162 my $faillogname = $faillog;
163 $faillog = 'inexport::FAILLOG';
164 $faillog->autoflush(1);
165 $self->{'faillog'} = $faillog;
166 $self->{'faillogname'} = $faillogname;
167
168 # Read in the collection configuration file.
169 my ($config_filename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
170 my $collectcfg = &colcfg::read_collection_cfg ($config_filename, $gs_mode);
171
172 return ($config_filename,$collectcfg);
173}
174
175sub set_collection_options
176{
177 my $self = shift @_;
178 my ($collectcfg) = @_;
179
180 my $inexport_mode = $self->{'mode'};
181
182 my $verbosity = $self->{'verbosity'};
183 my $debug = $self->{'debug'};
184 my $importdir = $self->{'importdir'};
185 my $archivedir = $self->{'archivedir'};
186 my $out = $self->{'out'};
187
188 # If the infodbtype value wasn't defined in the collect.cfg file, use the default
189 if (!defined($collectcfg->{'infodbtype'}))
190 {
191 $collectcfg->{'infodbtype'} = &dbutil::get_default_infodb_type();
192 }
193
194 if (defined $collectcfg->{'importdir'} && $importdir eq "") {
195 $importdir = $collectcfg->{'importdir'};
196 }
197 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
198 $archivedir = $collectcfg->{'archivedir'};
199 }
200 # fill in the default import and archives directories if none
201 # were supplied, turn all \ into / and remove trailing /
202 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
203 $importdir =~ s/[\\\/]+/\//g;
204 $importdir =~ s/\/$//;
205 if (!-e $importdir) {
206 &gsprintf($out, "{import.no_import_dir}\n\n", $importdir);
207 die "\n";
208 }
209 $self->{'importdir'} = $importdir;
210
211 if ($archivedir eq "") {
212 if ($inexport_mode eq "import") {
213 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
214 }
215 elsif ($inexport_mode eq "export") {
216 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "export");
217 }
218 else {
219 print STDERR "Warning: Unrecognized import/export mode '$inexport_mode'\n";
220 print STDERR " Defaulting to 'archives' for file output\n";
221 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
222 }
223 }
224
225 $archivedir =~ s/[\\\/]+/\//g;
226 $archivedir =~ s/\/$//;
227 $self->{'archivedir'} = $archivedir;
228
229 if ($verbosity !~ /\d+/) {
230 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
231 $verbosity = $collectcfg->{'verbosity'};
232 } else {
233 $verbosity = 2; # the default
234 }
235 }
236 $self->{'verbosity'} = $verbosity;
237
238 if (defined $collectcfg->{'manifest'} && $self->{'manifest'} eq "") {
239 $self->{'manifest'} = $collectcfg->{'manifest'};
240 }
241
242 if (defined $collectcfg->{'gzip'} && !$self->{'gzip'}) {
243 if ($collectcfg->{'gzip'} =~ /^true$/i) {
244 $self->{'gzip'} = 1;
245 }
246 }
247
248 if ($self->{'maxdocs'} !~ /\-?\d+/) {
249 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
250 $self->{'maxdocs'} = $collectcfg->{'maxdocs'};
251 } else {
252 $self->{'maxdocs'} = -1; # the default
253 }
254 }
255
256 if ((defined $self->{'groupsize'}) && ($self->{'groupsize'} == 1)) {
257 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
258 $self->{'groupsize'} = $collectcfg->{'groupsize'};
259 }
260 }
261
262 if (!defined $self->{'OIDtype'}
263 || ($self->{'OIDtype'} !~ /^(hash|incremental|assigned|dirname)$/ )) {
264 if (defined $collectcfg->{'OIDtype'}
265 && $collectcfg->{'OIDtype'} =~ /^(hash|incremental|assigned|dirname)$/) {
266 $self->{'OIDtype'} = $collectcfg->{'OIDtype'};
267 } else {
268 $self->{'OIDtype'} = "hash"; # the default
269 }
270 }
271
272 if ((!defined $self->{'OIDmetadata'}) || ($self->{'OIDmetadata'} eq "")) {
273 if (defined $collectcfg->{'OIDmetadata'}) {
274 $self->{'OIDmetadata'} = $collectcfg->{'OIDmetadata'};
275 } else {
276 $self->{'OIDmetadata'} = "dc.Identifier"; # the default
277 }
278 }
279
280 my $sortmeta = $self->{'sortmeta'};
281 if (defined $collectcfg->{'sortmeta'} && (!defined $sortmeta || $sortmeta eq "")) {
282 $sortmeta = $collectcfg->{'sortmeta'};
283 }
284 # sortmeta cannot be used with group size
285 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
286 if (defined $sortmeta && $self->{'groupsize'} > 1) {
287 &gsprintf($out, "{import.cannot_sort}\n\n");
288 $sortmeta = undef;
289 }
290 $self->{'sortmeta'} = $sortmeta;
291
292 if (defined $collectcfg->{'removeprefix'} && $self->{'removeprefix'} eq "") {
293 $self->{'removeprefix'} = $collectcfg->{'removeprefix'};
294 }
295
296 if (defined $collectcfg->{'removesuffix'} && $self->{'removesuffix'} eq "") {
297 $self->{'removesuffix'} = $collectcfg->{'removesuffix'};
298 }
299 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
300 $self->{'debug'} = 1;
301 }
302 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
303 $self->{'gli'} = 1;
304 }
305 $self->{'gli'} = 0 unless defined $self->{'gli'};
306
307 # check keepold and removeold
308 my $checkdir = ($inexport_mode eq "import") ? "archives" : "export";
309
310 my ($removeold, $keepold, $incremental, $incremental_mode)
311 = &scriptutil::check_removeold_and_keepold($self->{'removeold'}, $self->{'keepold'},
312 $self->{'incremental'}, $checkdir,
313 $collectcfg);
314
315 $self->{'removeold'} = $removeold;
316 $self->{'keepold'} = $keepold;
317 $self->{'incremental'} = $incremental;
318 $self->{'incremental_mode'} = $incremental_mode;
319}
320
321sub process_files
322{
323 my $self = shift @_;
324 my ($config_filename,$collectcfg) = @_;
325
326 my $inexport_mode = $self->{'mode'};
327
328 my $verbosity = $self->{'verbosity'};
329 my $debug = $self->{'debug'};
330
331 my $importdir = $self->{'importdir'};
332 my $archivedir = $self->{'archivedir'};
333
334 my $incremental = $self->{'incremental'};
335 my $incremental_mode = $self->{'incremental_mode'};
336
337 my $removeold = $self->{'removeold'};
338 my $keepold = $self->{'keepold'};
339
340 my $saveas = $self->{'saveas'};
341 my $OIDtype = $self->{'OIDtype'};
342 my $OIDmetadata = $self->{'OIDmetadata'};
343
344 my $out = $self->{'out'};
345 my $faillog = $self->{'faillog'};
346
347 my $maxdocs = $self->{'maxdocs'};
348 my $gzip = $self->{'gzip'};
349 my $groupsize = $self->{'groupsize'};
350 my $sortmeta = $self->{'sortmeta'};
351
352 my $removeprefix = $self->{'removeprefix'};
353 my $removesuffix = $self->{'removesuffix'};
354
355 my $gli = $self->{'gli'};
356
357 my $jobs = $self->{'jobs'};
358 my $epoch = $self->{'epoch'};
359
360 # related to export
361 my $xsltfile = $self->{'xsltfile'};
362 my $group_marc = $self->{'group_marc'};
363 my $mapping_file = $self->{'mapping_file'};
364 my $xslt_mets = $self->{'xslt_mets'};
365 my $xslt_txt = $self->{'xslt_txt'};
366 my $fedora_namespace = $self->{'fedora_namespace'};
367
368 if ($inexport_mode eq "import") {
369 print STDERR "<Import>\n" if $gli;
370 }
371 else {
372 print STDERR "<export>\n" if $gli;
373 }
374
375 my $manifest_lookup = new manifest($collectcfg->{'infodbtype'},$archivedir);
376 if ($self->{'manifest'} ne "") {
377 my $manifest_filename = $self->{'manifest'};
378
379 if (!&util::filename_is_absolute($manifest_filename)) {
380 $manifest_filename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, $manifest_filename);
381 }
382
383 $self->{'manifest'} =~ s/[\\\/]+/\//g;
384 $self->{'manifest'} =~ s/\/$//;
385
386 $manifest_lookup->parse($manifest_filename);
387 }
388
389 my $manifest = $self->{'manifest'};
390
391 # load all the plugins
392 my $plugins = [];
393 if (defined $collectcfg->{'plugin'}) {
394 $plugins = $collectcfg->{'plugin'};
395 }
396
397 #some global options for the plugins
398 my @global_opts = ();
399
400 my $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts, $incremental_mode);
401 if (scalar(@$pluginfo) == 0) {
402 &gsprintf($out, "{import.no_plugins_loaded}\n");
403 die "\n";
404 }
405
406 # remove the old contents of the archives directory (and tmp directory) if needed
407 if ($removeold) {
408 if (-e $archivedir) {
409 &gsprintf($out, "{import.removing_archives}\n");
410 &util::rm_r ($archivedir);
411 }
412 my $tmpdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "tmp");
413 $tmpdir =~ s/[\\\/]+/\//g;
414 $tmpdir =~ s/\/$//;
415 if (-e $tmpdir) {
416 &gsprintf($out, "{import.removing_tmpdir}\n");
417 &util::rm_r ($tmpdir);
418 }
419 }
420
421 # create the archives dir if needed
422 &util::mk_all_dir($archivedir);
423
424 # read the archive information file
425## my $arcinfo_doc_filename = &util::filename_cat ($archivedir, "archives.inf");
426
427 # BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files (won't do anything for other infodbtypes)
428 &util::rename_ldb_or_bdb_file(&util::filename_cat($archivedir, "archiveinf-doc"));
429 &util::rename_ldb_or_bdb_file(&util::filename_cat($archivedir, "archiveinf-src"));
430
431 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-doc", $archivedir);
432 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-src", $archivedir);
433
434 my $archive_info = new arcinfo ($collectcfg->{'infodbtype'});
435 $archive_info->load_info ($arcinfo_doc_filename);
436
437 if ($manifest eq "") {
438 # Load in list of files in import folder from last import (if present)
439 $archive_info->load_prev_import_filelist ($arcinfo_src_filename);
440 }
441
442 ####Use Plugout####
443 my $plugout;
444
445 if ($inexport_mode eq "import") {
446 if (defined $collectcfg->{'plugout'}) {
447 # If a plugout was specified in the collect.cfg file, assume it is sensible
448 # We can't check the name because it could be anything, if it is a custom plugout
449 $plugout = $collectcfg->{'plugout'};
450 }
451 else{
452 if ($saveas !~ /^(GreenstoneXML|GreenstoneMETS)$/) {
453 push @$plugout,"GreenstoneXMLPlugout";
454 }
455 else{
456 push @$plugout,$saveas."Plugout";
457 }
458 }
459 }
460 else {
461 if (defined $collectcfg->{'plugout'} && $collectcfg->{'plugout'} =~ /^(.*METS|DSpace|MARCXML)Plugout/) {
462 $plugout = $collectcfg->{'plugout'};
463 }
464 else{
465 if ($saveas !~ /^(GreenstoneMETS|FedoraMETS|DSpace|MARCXML)$/) {
466 push @$plugout,"GreenstoneMETSPlugout";
467 }
468 else{
469 push @$plugout,$saveas."Plugout";
470 }
471 }
472 }
473
474 my $plugout_name = $plugout->[0];
475
476 push @$plugout,("-output_info",$archive_info) if (defined $archive_info);
477 push @$plugout,("-verbosity",$verbosity) if (defined $verbosity);
478 push @$plugout,("-debug") if ($debug);
479 push @$plugout,("-group_size",$groupsize) if (defined $groupsize);
480 push @$plugout,("-gzip_output") if ($gzip);
481 push @$plugout,("-output_handle",$out) if (defined $out);
482
483 push @$plugout,("-xslt_file",$xsltfile) if (defined $xsltfile && $xsltfile ne "");
484
485 if ($plugout_name =~ m/^MARCXMLPlugout$/) {
486 push @$plugout,("-group") if ($group_marc);
487 push @$plugout,("-mapping_file",$mapping_file) if (defined $mapping_file && $mapping_file ne "");
488 }
489 if ($plugout_name =~ m/^.*METSPlugout$/) {
490 push @$plugout,("-xslt_mets",$xslt_mets) if (defined $xslt_mets && $xslt_mets ne "");
491 push @$plugout,("-xslt_txt",$xslt_txt) if (defined $xslt_txt && $xslt_txt ne "");
492 }
493
494 if ($plugout_name eq "FedoraMETSPlugout") {
495 push @$plugout,("-fedora_namespace",$fedora_namespace) if (defined $fedora_namespace && $fedora_namespace ne "");
496 }
497
498
499 my $processor = &plugout::load_plugout($plugout);
500 $processor->setoutputdir ($archivedir);
501 $processor->set_sortmeta ($sortmeta, $removeprefix, $removesuffix) if defined $sortmeta;
502 $processor->set_OIDtype ($OIDtype, $OIDmetadata);
503
504 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs, $gli);
505
506 if ($removeold) {
507 # occasionally, plugins may want to do something on remove old, eg pharos image indexing
508 &plugin::remove_all($pluginfo, $importdir, $processor, $maxdocs, $gli);
509 }
510 if ($manifest eq "") {
511 # process the import directory
512 my $block_hash = {};
513 my $metadata = {};
514 # gobal blocking pass may set up some metadata
515 &plugin::file_block_read($pluginfo, $importdir, "", $block_hash, $metadata, $gli);
516
517 if ($incremental || $incremental_mode eq "onlyadd") {
518
519 prime_doc_oid_count($archivedir);
520
521 # Can now work out which files were new, already existed, and have
522 # been deleted
523
524 new_vs_old_import_diff($archive_info,$block_hash,$importdir,
525 $archivedir,$verbosity,$incremental_mode);
526
527 my @new_files = sort keys %{$block_hash->{'new_files'}};
528 if (scalar(@new_files>0)) {
529 print STDERR "New files and modified metadata files since last import:\n ";
530 print STDERR join("\n ",@new_files), "\n";
531 }
532
533 if ($incremental) {
534 # only look for deletions if we are truely incremental
535 my @deleted_files = sort keys %{$block_hash->{'deleted_files'}};
536 # Filter out any in gsdl/tmp area
537 my @filtered_deleted_files = ();
538 my $gsdl_tmp_area = &util::filename_cat($ENV{'GSDLHOME'}, "tmp");
539 my $collect_tmp_area = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
540 $gsdl_tmp_area = &util::filename_to_regex($gsdl_tmp_area);
541 $collect_tmp_area = &util::filename_to_regex($collect_tmp_area);
542
543 foreach my $df (@deleted_files) {
544 next if ($df =~ m/^$gsdl_tmp_area/);
545 next if ($df =~ m/^$collect_tmp_area/);
546
547 push(@filtered_deleted_files,$df);
548 }
549
550
551 @deleted_files = @filtered_deleted_files;
552
553 if (scalar(@deleted_files)>0) {
554 print STDERR "Files deleted since last import:\n ";
555 print STDERR join("\n ",@deleted_files), "\n";
556
557
558 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@deleted_files);
559
560 mark_docs_for_deletion($archive_info,$block_hash,\@deleted_files, $archivedir,$verbosity, "delete");
561 }
562
563 my @reindex_files = sort keys %{$block_hash->{'reindex_files'}};
564
565 if (scalar(@reindex_files)>0) {
566 print STDERR "Files to reindex since last import:\n ";
567 print STDERR join("\n ",@reindex_files), "\n";
568 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@reindex_files);
569 mark_docs_for_deletion($archive_info,$block_hash,\@reindex_files, $archivedir,$verbosity, "reindex");
570 }
571
572 }
573
574 # Play it safe, and run through the entire folder, only processing new or edited files
575
576 if ((defined $jobs) && ($jobs > 1))
577 {
578 # if jobs are set to >1, run in parallel using MPI helper
579 # [hs, 1 july 2010]
580 &ParallelInexport::farm_out_processes($jobs, $epoch, $importdir, $block_hash,
581 $self->{'collection'}, $self->{'site'});
582 }
583 else
584 {
585 &plugin::read ($pluginfo, $importdir, "", $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
586 }
587 }
588 else {
589 if ((defined $jobs) && ($jobs > 1))
590 {
591 # if jobs are set to >1, run in parallel using MPI helper
592 # [hs, 1 july 2010]
593 &ParallelInexport::farm_out_processes($jobs, $epoch, $importdir, $block_hash,
594 $self->{'collection'}, $self->{'site'});
595 }
596 else
597 {
598 &plugin::read ($pluginfo, $importdir, "", $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
599 }
600 }
601
602 }
603 else
604 {
605 #
606 # 1. Process delete files first
607 #
608
609 my @deleted_files = keys %{$manifest_lookup->{'delete'}};
610 my @full_deleted_files = ();
611
612 # ensure all filenames are absolute
613 foreach my $df (@deleted_files) {
614 my $full_df =
615 (&util::filename_is_absolute($df))
616 ? $df
617 : &util::filename_cat($importdir,$df);
618
619 push(@full_deleted_files,$full_df);
620 }
621
622 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@full_deleted_files);
623 mark_docs_for_deletion($archive_info,{},
624 \@full_deleted_files,
625 $archivedir, $verbosity, "delete");
626
627
628 #
629 # 2. Now files for reindexing
630 #
631
632 my @reindex_files = keys %{$manifest_lookup->{'reindex'}};
633 my @full_reindex_files = ();
634
635 # ensure all filenames are absolute
636 foreach my $rf (@reindex_files) {
637 my $full_rf =
638 (&util::filename_is_absolute($rf))
639 ? $rf
640 : &util::filename_cat($importdir,$rf);
641
642 push(@full_reindex_files,$full_rf);
643 }
644
645 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@full_reindex_files);
646 mark_docs_for_deletion($archive_info,{},\@full_reindex_files, $archivedir,$verbosity, "reindex");
647
648 # And now ensure the new version of the file processed by appropriate
649 # plugin
650 foreach my $full_rf (@full_reindex_files) {
651 &plugin::read ($pluginfo, "", $full_rf, {}, {}, $processor, $maxdocs, 0, $gli);
652 }
653
654
655 #
656 # 3. Now finally any new files
657 #
658
659 foreach my $file (keys %{$manifest_lookup->{'index'}}) {
660 &plugin::read ($pluginfo, $importdir, $file, {}, {}, $processor, $maxdocs, 0, $gli);
661 }
662
663
664 }
665
666 if ($saveas eq "FedoraMETS") {
667 # create collection "doc obj" for Fedora that contains
668 # collection-level metadata
669
670 my $doc_obj = new doc($config_filename,"nonindexed_doc","none");
671 $doc_obj->set_OID("collection");
672
673 my $col_name = undef;
674 my $col_meta = $collectcfg->{'collectionmeta'};
675
676 if (defined $col_meta) {
677 store_collectionmeta($col_meta,"collectionname",$doc_obj); # in GS3 this is a collection's name
678 store_collectionmeta($col_meta,"collectionextra",$doc_obj); # in GS3 this is a collection's description
679 }
680 $processor->process($doc_obj);
681 }
682
683 &plugin::end($pluginfo, $processor);
684
685 &plugin::deinit($pluginfo, $processor);
686
687 # Store the value of OIDCount (used in doc.pm) so it can be
688 # restored correctly to this value on an incremental build
689 store_doc_oid_count($archivedir);
690
691 # write out the archive information file
692 $processor->close_file_output() if $groupsize > 1;
693 $processor->close_group_output() if $processor->is_group();
694
695 # for backwards compatability with archvies.inf file
696 if ($arcinfo_doc_filename =~ m/(contents)|(\.inf)$/) {
697 $archive_info->save_info($arcinfo_doc_filename);
698 }
699 else {
700 $archive_info->save_revinfo_db($arcinfo_src_filename);
701 }
702
703 return $pluginfo;
704}
705
706
707sub generate_statistics
708{
709 my $self = shift @_;
710 my ($pluginfo) = @_;
711
712 my $inexport_mode = $self->{'mode'};
713
714 my $statsfile = $self->{'statsfile'};
715 my $out = $self->{'out'};
716 my $faillogname = $self->{'faillogname'};
717 my $gli = $self->{'gli'};
718 my $jobs = $self->{'jobs'};
719
720 # write out import stats
721
722 if ((!defined $jobs) || ($jobs == 1))
723 {
724 # only output statistics if there are multiple jobs
725 # [hs, 1 july 2010]
726
727 my $close_stats = 0;
728 if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
729 if (open (STATS, ">$statsfile")) {
730 $statsfile = 'import::STATS';
731 $close_stats = 1;
732 } else {
733 &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
734 &gsprintf($out, "{import.stats_backup}\n");
735 $statsfile = 'STDERR';
736 }
737 }
738
739 &gsprintf($out, "\n");
740 &gsprintf($out, "*********************************************\n");
741 &gsprintf($out, "{$inexport_mode.complete}\n");
742 &gsprintf($out, "*********************************************\n");
743
744 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
745 if ($close_stats) {
746 close STATS;
747 }
748 }
749
750 close OUT if $self->{'close_out'};
751 close FAILLOG;
752}
753
754
755
756
757
758
759
760sub oid_count_file {
761 my ($archivedir) = @_;
762 return &util::filename_cat ($archivedir, "OIDcount");
763}
764
765
766sub prime_doc_oid_count
767{
768 my ($archivedir) = @_;
769 my $oid_count_filename = &oid_count_file($archivedir);
770
771 if (-e $oid_count_filename) {
772 if (open(OIDIN,"<$oid_count_filename")) {
773 my $OIDcount = <OIDIN>;
774 chomp $OIDcount;
775 close(OIDIN);
776
777 $doc::OIDcount = $OIDcount;
778 }
779 else {
780
781 print STDERR "Warning: unable to read document OID count from $oid_count_filename\n";
782 print STDERR "Setting value to 0\n";
783 }
784 }
785
786}
787
788sub store_doc_oid_count
789{
790 # Use the file "OIDcount" in the archives directory to record
791 # what value doc.pm got up to
792
793 my ($archivedir) = @_;
794 my $oid_count_filename = &oid_count_file($archivedir);
795
796
797 if (open(OIDOUT,">$oid_count_filename")) {
798 print OIDOUT $doc::OIDcount, "\n";
799
800 close(OIDOUT);
801 }
802 else {
803 print STDERR "Warning: unable to store document OID count\n";
804 }
805}
806
807
808
809sub new_vs_old_import_diff
810{
811 my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
812
813 # Get the infodbtype value for this collection from the arcinfo object
814 my $infodbtype = $archive_info->{'infodbtype'};
815
816 # in this method, we want to know if metadata files are modified or not.
817 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
818
819 my $archiveinf_timestamp = -M $arcinfo_doc_filename;
820
821 # First convert all files to absolute form
822 # This is to support the situation where the import folder is not
823 # the default
824
825 my $prev_all_files = $archive_info->{'prev_import_filelist'};
826 my $full_prev_all_files = {};
827
828 foreach my $prev_file (keys %$prev_all_files) {
829
830 if (!&util::filename_is_absolute($prev_file)) {
831 my $full_prev_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$prev_file);
832 $full_prev_all_files->{$full_prev_file} = $prev_file;
833 }
834 else {
835 $full_prev_all_files->{$prev_file} = $prev_file;
836 }
837 }
838
839
840 # Figure out which are the new files, existing files and so
841 # by implication the files from the previous import that are not
842 # there any more => mark them for deletion
843 foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
844
845 my $full_curr_file = $curr_file;
846
847 # entry in 'all_files' is moved to either 'existing_files',
848 # 'deleted_files', 'new_files', or 'new_or_modified_metadata_files'
849
850 if (!&util::filename_is_absolute($curr_file)) {
851 # add in import dir to make absolute
852 $full_curr_file = &util::filename_cat($importdir,$curr_file);
853 }
854
855 # figure out if new file or not
856 if (defined $full_prev_all_files->{$full_curr_file}) {
857 # delete it so that only files that need deleting are left
858 delete $full_prev_all_files->{$full_curr_file};
859
860 # had it before. is it a metadata file?
861 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
862
863 # is it modified??
864 if (-M $full_curr_file < $archiveinf_timestamp) {
865 print STDERR "*** Detected a modified metadata file: $full_curr_file\n" if $verbosity > 2;
866 # its newer than last build
867 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
868 }
869 }
870 else {
871 if ($incremental_mode eq "all") {
872
873 # had it before
874 $block_hash->{'existing_files'}->{$full_curr_file} = 1;
875
876 }
877 else {
878 # Warning in "onlyadd" mode, but had it before!
879 print STDERR "Warning: File $full_curr_file previously imported.\n";
880 print STDERR " Treating as new file\n";
881
882 $block_hash->{'new_files'}->{$full_curr_file} = 1;
883
884 }
885 }
886 }
887 else {
888 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
889 # the new file is the special sort of file greenstone uses
890 # to attach metadata to src documents
891 # i.e metadata.xml
892 # (but note, the filename used is not constrained in
893 # Greenstone to always be this)
894
895 print STDERR "***** Detected new metadata file: $full_curr_file\n" if $verbosity > 2;
896 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
897 }
898 else {
899 $block_hash->{'new_files'}->{$full_curr_file} = 1;
900 }
901 }
902
903
904 delete $block_hash->{'all_files'}->{$curr_file};
905 }
906
907
908
909
910 # Deal with complication of new or modified metadata files by forcing
911 # everything from this point down in the file hierarchy to
912 # be freshly imported.
913 #
914 # This may mean files that have not changed are reindexed, but does
915 # guarantee by the end of processing all new metadata is correctly
916 # associated with the relevant document(s).
917
918 foreach my $new_mdf (keys %{$block_hash->{'new_or_modified_metadata_files'}}) {
919 my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
920
921 $situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
922 $situated_dir =~ s/\\/\\\\/g; # need to protect windows slash \ in regular expression
923
924 # Go through existing_files, and mark anything that is contained
925 # within 'situated_dir' to be reindexed (in case some of the metadata
926 # attaches to one of these files)
927
928 my $reindex_files = [];
929
930 foreach my $existing_f (keys %{$block_hash->{'existing_files'}}) {
931
932 if ($existing_f =~ m/^$situated_dir/) {
933 push(@$reindex_files,$existing_f);
934 $block_hash->{'reindex_files'}->{$existing_f} = 1;
935 delete $block_hash->{'existing_files'}->{$existing_f};
936
937 }
938 }
939
940 # metadata file needs to be in new_files list so parsed by MetadataXMLPlug
941 # (or equivalent)
942 $block_hash->{'new_files'}->{$new_mdf} = 1;
943
944 }
945
946 # go through remaining existing files and work out what has changed and needs to be reindexed.
947 my @existing_files = sort keys %{$block_hash->{'existing_files'}};
948
949 my $reindex_files = [];
950
951 foreach my $existing_filename (@existing_files) {
952 if (-M $existing_filename < $archiveinf_timestamp) {
953 # file is newer than last build
954
955 my $existing_file = $existing_filename;
956 #my $collectdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'});
957
958 #my $collectdir_resafe = &util::filename_to_regex($collectdir);
959 #$existing_file =~ s/^$collectdir_resafe(\\|\/)?//;
960
961 print STDERR "**** Reindexing existing file: $existing_file\n";
962
963 push(@$reindex_files,$existing_file);
964 $block_hash->{'reindex_files'}->{$existing_filename} = 1;
965 }
966
967 }
968
969
970 # By this point full_prev_all_files contains the files
971 # mentioned in archiveinf-src.db but are not in the 'import'
972 # folder (or whatever was specified through -importdir ...)
973
974 # This list can contain files that were created in the 'tmp' or
975 # 'cache' areas (such as screen-size and thumbnail images).
976 #
977 # In building the final list of files to delete, we test to see if
978 # it exists on the filesystem and if it does (unusual for a "normal"
979 # file in import, but possible in the case of 'tmp' files),
980 # supress it from going into the final list
981
982 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
983
984 my @deleted_files = values %$full_prev_all_files;
985 map { my $curr_file = $_;
986 my $full_curr_file = $curr_file;
987
988 if (!&util::filename_is_absolute($curr_file)) {
989 # add in import dir to make absolute
990
991 $full_curr_file = &util::filename_cat($collectdir,$curr_file);
992 }
993
994
995 if (!-e $full_curr_file) {
996 $block_hash->{'deleted_files'}->{$curr_file} = 1;
997 }
998 } @deleted_files;
999
1000
1001
1002}
1003
1004
1005# this is used to delete "deleted" docs, and to remove old versions of "changed" docs
1006# $mode is 'delete' or 'reindex'
1007sub mark_docs_for_deletion
1008{
1009 my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode) = @_;
1010
1011 my $mode_text = "deleted from index";
1012 if ($mode eq "reindex") {
1013 $mode_text = "reindexed";
1014 }
1015
1016 # Get the infodbtype value for this collection from the arcinfo object
1017 my $infodbtype = $archive_info->{'infodbtype'};
1018
1019 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
1020 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-src", $archivedir);
1021
1022
1023 # record files marked for deletion in arcinfo
1024 foreach my $file (@$deleted_files) {
1025 # use 'archiveinf-src' info database file to look up all the OIDs
1026 # that this file is used in (note in most cases, it's just one OID)
1027
1028 my $src_rec_string = &dbutil::read_infodb_entry($infodbtype, $arcinfo_src_filename, $file);
1029 my $src_rec = &dbutil::convert_infodb_string_to_hash($src_rec_string);
1030 my $oids = $src_rec->{'oid'};
1031 my $file_record_deleted = 0;
1032
1033 # delete the src record
1034 my $src_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_src_filename, "append");
1035 &dbutil::delete_infodb_entry($infodbtype, $src_infodb_file_handle, $file);
1036 &dbutil::close_infodb_write_handle($infodbtype, $src_infodb_file_handle);
1037
1038
1039 foreach my $oid (@$oids) {
1040
1041 # find the source doc (the primary file that becomes this oid)
1042 my $doc_rec_string = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $oid);
1043 my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);
1044 my $doc_source_file = $doc_rec->{'src-file'}->[0];
1045 if (!&util::filename_is_absolute($doc_source_file)) {
1046 $doc_source_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$doc_source_file);
1047 }
1048
1049 if ($doc_source_file ne $file) {
1050 # its an associated or metadata file
1051
1052 # mark source doc for reimport as one of its assoc files has changed or deleted
1053 $block_hash->{'reindex_files'}->{$doc_source_file} = 1;
1054
1055 }
1056 my $curr_status = $archive_info->get_status_info($oid);
1057 if (defined($curr_status) && (($curr_status ne "D"))) {
1058 if ($verbosity>1) {
1059 print STDERR "$oid ($doc_source_file) marked to be $mode_text on next buildcol.pl\n";
1060 }
1061 # mark oid for deletion (it will be deleted or reimported)
1062 $archive_info->set_status_info($oid,"D");
1063 my $val = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $oid);
1064 $val =~ s/^<index-status>(.*)$/<index-status>D/m;
1065
1066 my $val_rec = &dbutil::convert_infodb_string_to_hash($val);
1067 my $doc_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_doc_filename, "append");
1068
1069 &dbutil::write_infodb_entry($infodbtype, $doc_infodb_file_handle, $oid, $val_rec);
1070 &dbutil::close_infodb_write_handle($infodbtype, $doc_infodb_file_handle);
1071 }
1072 }
1073
1074 }
1075 # now go through and check that we haven't marked any primary files for reindex (because their associated files have changed/deleted) when they have been deleted themselves.
1076 foreach my $file (@$deleted_files) {
1077 if (defined $block_hash->{'reindex_files'}->{$file}) {
1078 delete $block_hash->{'reindex_files'}->{$file};
1079 }
1080 }
1081
1082
1083}
1084
1085
1086
10871;
Note: See TracBrowser for help on using the repository browser.