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

Last change on this file since 23170 was 23170, checked in by kjdon, 14 years ago

if infodbtype is gdbm-txtgz, we need to use gdbm for all archives dbs

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