source: gs2-extensions/parallel-building/trunk/src/perllib/inexport.pm@ 30292

Last change on this file since 30292 was 30292, checked in by jmt12, 6 years ago

Removing reference to debugging module Devel::Peek

  • Property svn:executable set to *
File size: 47.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 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;
49use Scalar::Util 'blessed';
50
51my $oidtype_list =
52 [ { 'name' => "hash",
53 'desc' => "{import.OIDtype.hash}" },
54 { 'name' => "hash_on_full_filename",
55 'desc' => "{import.OIDtype.hash_on_full_filename}" },
56 { 'name' => "assigned",
57 'desc' => "{import.OIDtype.assigned}" },
58 { 'name' => "incremental",
59 'desc' => "{import.OIDtype.incremental}" },
60 { 'name' => "filename",
61 'desc' => "{import.OIDtype.filename}" },
62 { 'name' => "dirname",
63 'desc' => "{import.OIDtype.dirname}" },
64 { 'name' => "full_filename",
65 'desc' => "{import.OIDtype.full_filename}" } ];
66
67$inexport::directory_arguments =
68[
69 { 'name' => "importdir",
70 'desc' => "{import.importdir}",
71 'type' => "string",
72 'reqd' => "no",
73 'deft' => "import",
74 'hiddengli' => "yes" },
75 { 'name' => "collectdir",
76 'desc' => "{import.collectdir}",
77 'type' => "string",
78 # parsearg left "" as default
79 #'deft' => &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "collect"),
80 'deft' => "",
81 'reqd' => "no",
82 'hiddengli' => "yes" },
83
84];
85$inexport::arguments =
86[
87 # don't set the default to hash - want to allow this to come from
88 # entry in collect.cfg but want to override it here
89 { 'name' => "OIDtype",
90 'desc' => "{import.OIDtype}",
91 'type' => "enum",
92 'list' => $oidtype_list,
93 'deft' => "hash_on_full_filename",
94 'reqd' => "no",
95 'modegli' => "2" },
96 { 'name' => "OIDmetadata",
97 'desc' => "{import.OIDmetadata}",
98 'type' => "string",
99 'deft' => "dc.Identifier",
100 'reqd' => "no",
101 'modegli' => "2" },
102 { 'name' => "site",
103 'desc' => "{import.site}",
104 'type' => "string",
105 'deft' => "",
106 'reqd' => "no",
107 'hiddengli' => "yes" },
108 { 'name' => "manifest",
109 'desc' => "{import.manifest}",
110 'type' => "string",
111 'deft' => "",
112 'reqd' => "no",
113 'hiddengli' => "yes" } ,
114 { 'name' => "incremental",
115 'desc' => "{import.incremental}",
116 'type' => "flag",
117 'hiddengli' => "yes" },
118 { 'name' => "keepold",
119 'desc' => "{import.keepold}",
120 'type' => "flag",
121 'reqd' => "no",
122 'hiddengli' => "yes" },
123 { 'name' => "removeold",
124 'desc' => "{import.removeold}",
125 'type' => "flag",
126 'reqd' => "no",
127 'hiddengli' => "yes" },
128 { 'name' => "language",
129 'desc' => "{scripts.language}",
130 'type' => "string",
131 'reqd' => "no",
132 'hiddengli' => "yes" },
133 { 'name' => "maxdocs",
134 'desc' => "{import.maxdocs}",
135 'type' => "int",
136 'reqd' => "no",
137 'deft' => "-1",
138 'range' => "-1,",
139 'modegli' => "1" },
140 { 'name' => "debug",
141 'desc' => "{import.debug}",
142 'type' => "flag",
143 'reqd' => "no",
144 'hiddengli' => "yes" },
145 { 'name' => "faillog",
146 'desc' => "{import.faillog}",
147 'type' => "string",
148 # parsearg left "" as default
149 #'deft' => &FileUtils::filenameConcatenate("<collectdir>", "colname", "etc", "fail.log"),
150 'deft' => "",
151 'reqd' => "no",
152 'modegli' => "3" },
153 { 'name' => "out",
154 'desc' => "{import.out}",
155 'type' => "string",
156 'deft' => "STDERR",
157 'reqd' => "no",
158 'hiddengli' => "yes" },
159 { 'name' => "statsfile",
160 'desc' => "{import.statsfile}",
161 'type' => "string",
162 'deft' => "STDERR",
163 'reqd' => "no",
164 'hiddengli' => "yes" },
165 { 'name' => "verbosity",
166 'desc' => "{import.verbosity}",
167 'type' => "int",
168 'range' => "0,",
169 'deft' => "2",
170 'reqd' => "no",
171 'modegli' => "3" },
172 { 'name' => "gli",
173 'desc' => "{scripts.gli}",
174 'type' => "flag",
175 'reqd' => "no",
176 'hiddengli' => "yes" },
177 { 'name' => "xml",
178 'desc' => "{scripts.xml}",
179 'type' => "flag",
180 'reqd' => "no",
181 'hiddengli' => "yes" },
182
183];
184
185sub new
186{
187 my $class = shift (@_);
188 my ($mode,$argv,$options,$opt_listall_options) = @_;
189
190 my $self = { 'xml' => 0, 'mode' => $mode };
191
192 print "INFO: This inexport.pm supports version 2 manifest files\n";
193
194 # general options available to all plugins
195 my $arguments = $options->{'args'};
196 my $intArgLeftinAfterParsing = parse2::parse($argv,$arguments,$self,"allow_extra_options");
197 # Parse returns -1 if something has gone wrong
198 if ($intArgLeftinAfterParsing == -1)
199 {
200 &PrintUsage::print_txt_usage($options, "{import.params}",1);
201 print STDERR "Something went wrong during parsing the arguments. Scroll up for details.\n";
202 die "\n";
203 }
204
205 my $language = $self->{'language'};
206 # If $language has been specified, load the appropriate resource bundle
207 # (Otherwise, the default resource bundle will be loaded automatically)
208 if ($language && $language =~ /\S/) {
209 &gsprintf::load_language_specific_resource_bundle($language);
210 }
211
212 if ($self->{'listall'}) {
213 if ($self->{'xml'}) {
214 &PrintUsage::print_xml_usage($opt_listall_options);
215 }
216 else
217 {
218 &PrintUsage::print_txt_usage($opt_listall_options,"{export.params}");
219 }
220 die "\n";
221 }
222
223 if ($self->{'xml'}) {
224 &PrintUsage::print_xml_usage($options);
225 print "\n";
226 return bless $self, $class;
227 }
228
229 if ($self->{'gli'}) { # the gli wants strings to be in UTF-8
230 &gsprintf::output_strings_in_UTF8;
231 }
232
233 # If the user specified -h, then we output the usage
234 if (@$argv && $argv->[0] =~ /^\-+h/) {
235 &PrintUsage::print_txt_usage($options, "{import.params}");
236 die "\n";
237 }
238 # now check that we had exactly one leftover arg, which should be
239 # the collection name. We don't want to do this earlier, cos
240 # -xml arg doesn't need a collection name
241
242 if ($intArgLeftinAfterParsing != 1 )
243 {
244 &PrintUsage::print_txt_usage($options, "{import.params}", 1);
245 print STDERR "There should be one argument left after parsing the script args: the collection name.\n";
246 die "\n";
247 }
248
249 $self->{'close_out'} = 0;
250 my $out = $self->{'out'};
251 if ($out !~ /^(STDERR|STDOUT)$/i) {
252 open (OUT, ">$out") ||
253 (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $out) && die);
254 $out = 'inexport::OUT';
255 $self->{'close_out'} = 1;
256 }
257 $out->autoflush(1);
258 $self->{'out'} = $out;
259
260 my $statsfile = $self->{'statsfile'};
261 if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
262 open (STATSFILE, ">$statsfile") ||
263 (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $statsfile) && die);
264 $statsfile = 'inexport::STATSFILE';
265 $self->{'close_stats'} = 1;
266 }
267 $statsfile->autoflush(1);
268 $self->{'statsfile'} = $statsfile;
269
270 # @ARGV should be only one item, the name of the collection
271 $self->{'collection'} = shift @$argv;
272
273 # Unless otherwise stated all manifests are considered version 1---where
274 # they act more like an advanced process expression---as compared to newer
275 # manifest files that act as an explicit (and exhaustive) list of files to
276 # process [jmt12]
277 $self->{'manifest_version'} = 1;
278
279 return bless $self, $class;
280}
281
282# Simplified version of the contstructor for use with CGI scripts
283sub newCGI
284{
285 my $class = shift (@_);
286 my ($mode,$collect,$gsdl_cgi,$opt_site) = @_;
287
288 my $self = { 'xml' => 0, 'mode' => $mode };
289
290 $self->{'out'} = STDERR;
291
292 if (defined $gsdl_cgi) {
293 $self->{'site'} = $opt_site;
294 my $collect_dir = $gsdl_cgi->get_collection_dir($opt_site);
295 $self->{'collectdir'} = $collect_dir;
296 }
297 else {
298 $self->{'site'} = "";
299 $self->{'collectdir'} = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"collect");
300 }
301 $self->{'faillog'} = "";
302
303 $self->{'collection'} = $collect;
304
305 return bless $self, $class;
306}
307sub get_collection
308{
309 my $self = shift @_;
310
311 return $self->{'collection'};
312}
313
314
315sub read_collection_cfg
316{
317 my $self = shift @_;
318 my ($collection,$options) = @_;
319
320 my $collectdir = $self->{'collectdir'};
321 my $site = $self->{'site'};
322 my $out = $self->{'out'};
323
324 if (($collection = &colcfg::use_collection($site, $collection, $collectdir)) eq "") {
325 #&PrintUsage::print_txt_usage($options, "{import.params}", 1);
326 die "\n";
327 }
328
329 # set gs_version 2/3
330 $self->{'gs_version'} = "2";
331 if ((defined $site) && ($site ne "")) {
332 # gs3
333 $self->{'gs_version'} = "3";
334 }
335
336 # add collection's perllib dir into include path in
337 # case we have collection specific modules
338 &util::augmentINC(&FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, 'perllib'));
339
340 # check that we can open the faillog
341 my $faillog = $self->{'faillog'};
342 if ($faillog eq "") {
343 $faillog = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
344 }
345 open (FAILLOG, ">$faillog") ||
346 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
347
348
349 my $faillogname = $faillog;
350 $faillog = 'inexport::FAILLOG';
351 $faillog->autoflush(1);
352 $self->{'faillog'} = $faillog;
353 $self->{'faillogname'} = $faillogname;
354 $self->{'close_faillog'} = 1;
355
356 # Read in the collection configuration file.
357 my $gs_mode = "gs".$self->{'gs_version'}; #gs2 or gs3
358 my $config_filename = &colcfg::get_collect_cfg_name($out, $gs_mode);
359 my $collectcfg = &colcfg::read_collection_cfg ($config_filename, $gs_mode);
360
361 return ($config_filename,$collectcfg);
362}
363
364sub set_collection_options
365{
366 my $self = shift @_;
367 my ($collectcfg) = @_;
368
369 my $inexport_mode = $self->{'mode'};
370
371 my $importdir = $self->{'importdir'};
372 my $archivedir = $self->{'archivedir'} || $self->{'exportdir'};
373 my $out = $self->{'out'};
374
375 # If the infodbtype value wasn't defined in the collect.cfg file, use the default
376 if (!defined($collectcfg->{'infodbtype'}))
377 {
378 $collectcfg->{'infodbtype'} = &dbutil::get_default_infodb_type();
379 }
380 if ($collectcfg->{'infodbtype'} eq "gdbm-txtgz") {
381 # we can't use the text version for archives dbs.
382 $collectcfg->{'infodbtype'} = "gdbm";
383 }
384
385 if (defined $self->{'default_importdir'} && defined $collectcfg->{'importdir'}) {
386 $importdir = $collectcfg->{'importdir'};
387 }
388
389 if ($inexport_mode eq "import") {
390 if ( defined $self->{'default_archivedir'} && defined $collectcfg->{'archivedir'}) {
391 $archivedir = $collectcfg->{'archivedir'};
392 }
393 }
394 elsif ($inexport_mode eq "export") {
395 if (defined $self->{'default_exportdir'} && defined $collectcfg->{'exportdir'}) {
396 $archivedir = $collectcfg->{'exportdir'};
397 }
398 }
399 # fill in the default import and archives directories if none
400 # were supplied, turn all \ into / and remove trailing /
401 if (!&FileUtils::isFilenameAbsolute($importdir))
402 {
403 $importdir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, $importdir);
404 }
405 else
406 {
407 # Don't do this - it kills protocol prefixes
408 #$importdir =~ s/[\\\/]+/\//g;
409 #$importdir =~ s/\/$//;
410 # Do this instead
411 &FileUtils::sanitizePath($importdir);
412 }
413 if (!&FileUtils::directoryExists($importdir))
414 {
415 &gsprintf($out, "{import.no_import_dir}\n\n", $importdir);
416 die "\n";
417 }
418 $self->{'importdir'} = $importdir;
419
420 if (!&FileUtils::isFilenameAbsolute($archivedir)) {
421 $archivedir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, $archivedir);
422 }
423 else {
424
425 $archivedir = &FileUtils::sanitizePath($archivedir);
426 }
427 $self->{'archivedir'} = $archivedir;
428
429 if (defined $self->{'default_verbosity'}) {
430 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
431 $self->{'verbosity'} = $collectcfg->{'verbosity'};
432 }
433 }
434
435 if (defined $collectcfg->{'manifest'} && $self->{'manifest'} eq "") {
436 $self->{'manifest'} = $collectcfg->{'manifest'};
437 }
438
439 if (defined $collectcfg->{'gzip'} && !$self->{'gzip'}) {
440 if ($collectcfg->{'gzip'} =~ /^true$/i) {
441 $self->{'gzip'} = 1;
442 }
443 }
444
445 if (defined $self->{'default_maxdocs'}) {
446 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
447 $self->{'maxdocs'} = $collectcfg->{'maxdocs'};
448 }
449 }
450
451
452
453 if (defined $self->{'default_OIDtype'} ) {
454 if (defined $collectcfg->{'OIDtype'}
455 && $collectcfg->{'OIDtype'} =~ /^(hash|hash_on_full_filename|incremental|assigned|filename|dirname|full_filename)$/) {
456 $self->{'OIDtype'} = $collectcfg->{'OIDtype'};
457 }
458 }
459
460 if (defined $self->{'default_OIDmetadata'}) {
461 if (defined $collectcfg->{'OIDmetadata'}) {
462 $self->{'OIDmetadata'} = $collectcfg->{'OIDmetadata'};
463 }
464 }
465
466 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
467 $self->{'debug'} = 1;
468 }
469 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
470 $self->{'gli'} = 1;
471 }
472 $self->{'gli'} = 0 unless defined $self->{'gli'};
473
474 # check keepold and removeold
475 my $checkdir = ($inexport_mode eq "import") ? "archives" : "export";
476
477 my ($removeold, $keepold, $incremental, $incremental_mode)
478 = &scriptutil::check_removeold_and_keepold($self->{'removeold'}, $self->{'keepold'},
479 $self->{'incremental'}, $checkdir,
480 $collectcfg);
481
482 $self->{'removeold'} = $removeold;
483 $self->{'keepold'} = $keepold;
484 $self->{'incremental'} = $incremental;
485 $self->{'incremental_mode'} = $incremental_mode;
486
487 # We'll need direct access to this plugin to support v2 manifests
488 $self->{'directoryplugin'} = 0;
489}
490
491sub process_files
492{
493 my $self = shift @_;
494 my ($config_filename,$collectcfg) = @_;
495
496 my $inexport_mode = $self->{'mode'};
497
498 my $verbosity = $self->{'verbosity'};
499 my $debug = $self->{'debug'};
500
501 my $importdir = $self->{'importdir'};
502 my $archivedir = $self->{'archivedir'} || $self->{'exportdir'};
503
504 my $incremental = $self->{'incremental'};
505 my $incremental_mode = $self->{'incremental_mode'};
506
507 my $gs_version = $self->{'gs_version'};
508
509 my $removeold = $self->{'removeold'};
510 my $keepold = $self->{'keepold'};
511
512 my $saveas = $self->{'saveas'};
513 my $saveas_options = $self->{'saveas_options'};
514 my $OIDtype = $self->{'OIDtype'};
515 my $OIDmetadata = $self->{'OIDmetadata'};
516
517 my $out = $self->{'out'};
518 my $faillog = $self->{'faillog'};
519
520 my $maxdocs = $self->{'maxdocs'};
521 my $gzip = $self->{'gzip'};
522 my $groupsize = $self->{'groupsize'};
523 my $sortmeta = $self->{'sortmeta'};
524
525 my $removeprefix = $self->{'removeprefix'};
526 my $removesuffix = $self->{'removesuffix'};
527
528 my $gli = $self->{'gli'};
529
530 # related to export
531 my $xsltfile = $self->{'xsltfile'};
532 my $group_marc = $self->{'group_marc'};
533 my $mapping_file = $self->{'mapping_file'};
534 my $xslt_mets = $self->{'xslt_mets'};
535 my $xslt_txt = $self->{'xslt_txt'};
536 my $fedora_namespace = $self->{'fedora_namespace'};
537 my $metadata_prefix = $self->{'metadata_prefix'};
538
539 if ($inexport_mode eq "import") {
540 print STDERR "<Import>\n" if $gli;
541 }
542 else {
543 print STDERR "<export>\n" if $gli;
544 }
545
546 my $manifest_lookup = new manifest($collectcfg->{'infodbtype'},$archivedir);
547 if ($self->{'manifest'} ne "") {
548 my $manifest_filename = $self->{'manifest'};
549
550 if (!&FileUtils::isFilenameAbsolute($manifest_filename)) {
551 $manifest_filename = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, $manifest_filename);
552 }
553 $self->{'manifest'} = &FileUtils::sanitizePath($self->{'manifest'});
554 #$self->{'manifest'} =~ s/[\\\/]+/\//g;
555 #$self->{'manifest'} =~ s/\/$//;
556
557 $manifest_lookup->parse($manifest_filename);
558
559 # manifests may now include a version number [jmt12]
560 $self->{'manifest_version'} = $manifest_lookup->get_version();
561 }
562
563 my $manifest = $self->{'manifest'};
564
565 # load all the plugins
566 my $plugins = [];
567 if (defined $collectcfg->{'plugin'}) {
568 $plugins = $collectcfg->{'plugin'};
569 }
570
571 my $plugin_incr_mode = $incremental_mode;
572 if ($manifest ne "") {
573 # if we have a manifest file, then we pretend we are fully incremental for plugins
574 $plugin_incr_mode = "all";
575 }
576 #some global options for the plugins
577 my @global_opts = ();
578
579 my $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts, $plugin_incr_mode, $gs_version);
580 if (scalar(@$pluginfo) == 0) {
581 &gsprintf($out, "{import.no_plugins_loaded}\n");
582 die "\n";
583 }
584 # Store a reference to the DirectoryPlugin
585 foreach my $a_plugin (@{$pluginfo})
586 {
587 if (blessed ($a_plugin) eq 'DirectoryPlugin')
588 {
589 $self->{'directoryplugin'} = $a_plugin;
590 }
591 }
592 # No directory plugin - no v2 manifest support
593 if ($self->{'directoryplugin'} == 0)
594 {
595 print STDERR "WARNING: DirectoryPlugin not loaded: metadata.xml files not supported.\n";
596 }
597
598 # remove the old contents of the archives directory (and tmp
599 # directory) if needed
600
601 if ($removeold) {
602 if (&FileUtils::directoryExists($archivedir)) {
603 &gsprintf($out, "{import.removing_archives}\n");
604 &FileUtils::removeFilesRecursive($archivedir);
605 }
606 my $tmpdir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "tmp");
607 $tmpdir =~ s/[\\\/]+/\//g;
608 $tmpdir =~ s/\/$//;
609 if (&FileUtils::directoryExists($tmpdir)) {
610 &gsprintf($out, "{import.removing_tmpdir}\n");
611 &FileUtils::removeFilesRecursive($tmpdir);
612 }
613 }
614
615 # create the archives dir if needed
616 &FileUtils::makeAllDirectories($archivedir);
617
618 # read the archive information file
619
620 # BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files (won't do anything for other infodbtypes)
621 &util::rename_ldb_or_bdb_file(&FileUtils::filenameConcatenate($archivedir, "archiveinf-doc"));
622 &util::rename_ldb_or_bdb_file(&FileUtils::filenameConcatenate($archivedir, "archiveinf-src"));
623
624 # When we make these initial calls to determine the archive information doc
625 # and src databases we pass through a '1' to indicate this is the first
626 # time we are referring to these databases. When using dynamic dbutils
627 # (available in extensions) this indicates to some database types (for
628 # example, persistent servers) that this is a good time to perform any
629 # one time initialization. The argument has no effect on vanilla dbutils
630 # [jmt12]
631 my $perform_firsttime_init = 1;
632 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-doc", $archivedir, $perform_firsttime_init);
633 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-src", $archivedir, $perform_firsttime_init);
634
635 my $archive_info = new arcinfo ($collectcfg->{'infodbtype'});
636 $archive_info->load_info ($arcinfo_doc_filename);
637
638 if ($manifest eq "") {
639 # Load in list of files in import folder from last import (if present)
640 $archive_info->load_prev_import_filelist ($arcinfo_src_filename);
641 }
642
643 ####Use Plugout####
644 my $plugout;
645
646 my $generate_auxiliary_files = 0;
647 if ($inexport_mode eq "import") {
648 $generate_auxiliary_files = 1;
649 }
650 elsif ($self->{'include_auxiliary_database_files'}) {
651 $generate_auxiliary_files = 1;
652 }
653 $self->{'generate_auxiliary_files'} = $generate_auxiliary_files;
654
655 # Option to use user defined plugout
656 if ($inexport_mode eq "import") {
657 if (defined $collectcfg->{'plugout'}) {
658 # If a plugout was specified in the collect.cfg file, assume it is sensible
659 # We can't check the name because it could be anything, if it is a custom plugout
660 print STDERR "Using plugout specified in collect.cfg: ".join(' ', @{$collectcfg->{'plugout'}})."\n";
661 $plugout = $collectcfg->{'plugout'};
662 }
663 else {
664 push @$plugout,$saveas."Plugout";
665 }
666
667 }
668 else {
669 if (defined $collectcfg->{'plugout'} && $collectcfg->{'plugout'} =~ /^(GreenstoneXML|.*METS|DSpace|MARCXML)Plugout/) {
670 $plugout = $collectcfg->{'plugout'};
671 print STDERR "Using plugout specified in collect.cfg: $collectcfg->{'plugout'}\n";
672 }
673 else {
674 push @$plugout,$saveas."Plugout";
675 }
676 }
677
678 my $plugout_name = $plugout->[0];
679
680 if ($inexport_mode eq "export" && defined $saveas_options) {
681 my @user_plugout_options = split(" ", $saveas_options);
682 push @$plugout, @user_plugout_options;
683 }
684 push @$plugout,("-output_info",$archive_info) if (defined $archive_info);
685 push @$plugout,("-verbosity",$verbosity) if (defined $verbosity);
686 push @$plugout,("-debug") if ($debug);
687 push @$plugout,("-gzip_output") if ($gzip);
688 push @$plugout,("-output_handle",$out) if (defined $out);
689
690 push @$plugout,("-xslt_file",$xsltfile) if (defined $xsltfile && $xsltfile ne "");
691 push @$plugout, ("-no_auxiliary_databases") if ($generate_auxiliary_files == 0);
692 if ($inexport_mode eq "import") {
693 if ($plugout_name =~ m/^GreenstoneXMLPlugout$/) {
694 push @$plugout,("-group_size",$groupsize) if (defined $groupsize);
695 }
696 }
697 my $processor = &plugout::load_plugout($plugout);
698 $processor->setoutputdir ($archivedir);
699 $processor->set_sortmeta ($sortmeta, $removeprefix, $removesuffix) if defined $sortmeta;
700 $processor->set_OIDtype ($OIDtype, $OIDmetadata);
701 $processor->begin();
702 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs, $gli);
703
704 if ($removeold) {
705 # occasionally, plugins may want to do something on remove
706 # old, eg pharos image indexing
707 &plugin::remove_all($pluginfo, $importdir, $processor, $maxdocs, $gli);
708 }
709
710 # process the import directory
711 my $block_hash = {};
712 $block_hash->{'new_files'} = {};
713 $block_hash->{'reindex_files'} = {};
714 # all of these are set somewhere else, so it's more readable to define them
715 # here [jmt12]
716 $block_hash->{'all_files'} = {};
717 $block_hash->{'deleted_files'} = {};
718 $block_hash->{'file_blocks'} = {};
719 $block_hash->{'metadata_files'} = {};
720 $block_hash->{'shared_fileroot'} = '';
721 # a new flag so we can tell we had a manifest way down in the plugins
722 # [jmt12]
723 $block_hash->{'manifest'} = 'false';
724 my $metadata = {};
725
726 # global blocking pass may set up some metadata
727 # does this set up metadata?????
728 # - when we have a newer manifest file we don't do this -unless- the
729 # collection configuration indicates this collection contains complex
730 # (inherited) metadata [jmt12]
731 if ($manifest eq '' || (defined $collectcfg->{'complexmeta'} && $collectcfg->{'complexmeta'} eq 'true'))
732 {
733 &plugin::file_block_read($pluginfo, $importdir, "", $block_hash, $metadata, $gli);
734 }
735 else
736 {
737 print STDERR "Skipping global file scan due to manifest and complexmeta configuration\n";
738 }
739
740 if ($manifest ne "") {
741
742 # mark that we are using a manifest - information that might be needed
743 # down in plugins (for instance DirectoryPlugin)
744 $block_hash->{'manifest'} = $self->{'manifest_version'};
745
746 #
747 # 1. Process delete files first
748 #
749 my @deleted_files = keys %{$manifest_lookup->{'delete'}};
750 my @full_deleted_files = ();
751
752 # ensure all filenames are absolute
753 foreach my $df (@deleted_files) {
754 my $full_df =
755 (&FileUtils::isFilenameAbsolute($df))
756 ? $df
757 : &FileUtils::filenameConcatenate($importdir,$df);
758
759 if (-d $full_df) {
760 &add_dir_contents_to_list($full_df, \@full_deleted_files);
761 } else {
762 push(@full_deleted_files,$full_df);
763 }
764 }
765
766 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@full_deleted_files);
767 mark_docs_for_deletion($archive_info,{},
768 \@full_deleted_files,
769 $archivedir, $verbosity, "delete");
770
771
772 #
773 # 2. Now files for reindexing
774 #
775
776 my @reindex_files = keys %{$manifest_lookup->{'reindex'}};
777 my @full_reindex_files = ();
778 # ensure all filenames are absolute
779 foreach my $rf (@reindex_files) {
780 my $full_rf =
781 (&FileUtils::isFilenameAbsolute($rf))
782 ? $rf
783 : &FileUtils::filenameConcatenate($importdir,$rf);
784
785 if (-d $full_rf) {
786 &add_dir_contents_to_list($full_rf, \@full_reindex_files);
787 } else {
788 push(@full_reindex_files,$full_rf);
789 }
790 }
791
792 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@full_reindex_files);
793 mark_docs_for_deletion($archive_info,{},\@full_reindex_files, $archivedir,$verbosity, "reindex");
794
795 # And now to ensure the new version of the file processed by
796 # appropriate plugin, we need to add it to block_hash reindex list
797 foreach my $full_rf (@full_reindex_files) {
798 $block_hash->{'reindex_files'}->{$full_rf} = 1;
799 }
800
801
802 #
803 # 3. Now finally any new files - add to block_hash new_files list
804 #
805
806 my @new_files = keys %{$manifest_lookup->{'index'}};
807 my @full_new_files = ();
808
809 foreach my $nf (@new_files) {
810 # ensure filename is absolute
811 my $full_nf =
812 (&FileUtils::isFilenameAbsolute($nf))
813 ? $nf
814 : &FileUtils::filenameConcatenate($importdir,$nf);
815
816 if (-d $full_nf) {
817 &add_dir_contents_to_list($full_nf, \@full_new_files);
818 } else {
819 push(@full_new_files,$full_nf);
820 }
821 }
822
823 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-src", $archivedir);
824 # need to check this file exists before trying to read it - in the past
825 # it wasn't possible to have a manifest unless keepold was also set so
826 # you were pretty much guaranteed arcinfo existed
827 # [jmt12]
828 # @todo &FileUtils::fileExists($arcinfo_src_filename) [jmt12]
829 if (-e $arcinfo_src_filename)
830 {
831 my $arcinfodb_map = {};
832 &dbutil::read_infodb_file($collectcfg->{'infodbtype'}, $arcinfo_src_filename, $arcinfodb_map);
833 foreach my $f (@full_new_files) {
834 my $rel_f = &util::abspath_to_placeholders($f);
835
836 # check that we haven't seen it already
837 if (defined $arcinfodb_map->{$rel_f}) {
838 # TODO make better warning
839 print STDERR "Warning: $f ($rel_f) already in src archive, \n";
840 } else {
841 $block_hash->{'new_files'}->{$f} = 1;
842 }
843 }
844
845 undef $arcinfodb_map;
846 }
847 # no existing files - so we can just add all the files [jmt12]
848 else
849 {
850 foreach my $f (@full_new_files)
851 {
852 $block_hash->{'new_files'}->{$f} = 1;
853 }
854 }
855
856 # If we are not using complex inherited metadata (and thus have skipped
857 # the global file scan) we need to at least check for a matching
858 # metadata.xml for the files being indexed/reindexed
859 # - unless we are using the newer version of Manifests, which are treated
860 # verbatim, and should have a metadata element for metadata files (so
861 # we can explicitly process metadata files other than metadata.xml)
862 # [jmt12]
863 if ($self->{'manifest_version'} == 1 && (!defined $collectcfg->{'complexmeta'} || $collectcfg->{'complexmeta'} ne 'true'))
864 {
865 my @all_files_to_import = (keys %{$block_hash->{'reindex_files'}}, keys %{$block_hash->{'new_files'}});
866 foreach my $file_to_import (@all_files_to_import)
867 {
868 my $metadata_xml_path = $file_to_import;
869 $metadata_xml_path =~ s/[^\\\/]*$/metadata.xml/;
870 if (&FileUtils::fileExists($metadata_xml_path))
871 {
872 &plugin::file_block_read($pluginfo, '', $metadata_xml_path, $block_hash, $metadata, $gli);
873 }
874 }
875 }
876
877 # new version manifest files explicitly list files to be processed and
878 # only support 'simplemeta' format (ignoring complexmeta if set) in that
879 # each document can be accompanied by a metadata.xml file in the same
880 # directory. The metadata.xml can only apply to the fileset ".*".
881 # [jmt12]
882 if ($self->{'manifest_version'} > 1)
883 {
884 # Process metadata files
885 foreach my $file_to_import (keys %{$block_hash->{'reindex_files'}}, keys %{$block_hash->{'new_files'}})
886 {
887 $self->{'directoryplugin'}->read_for_manifest_v2($pluginfo, $file_to_import, $block_hash, $processor, $gli);
888 }
889 }
890 }
891 else {
892 # if incremental, we read through the import folder to see whats changed.
893
894 if ($incremental || $incremental_mode eq "onlyadd") {
895 prime_doc_oid_count($archivedir);
896
897 # Can now work out which files were new, already existed, and have
898 # been deleted
899
900 new_vs_old_import_diff($archive_info,$block_hash,$importdir,
901 $archivedir,$verbosity,$incremental_mode);
902
903 my @new_files = sort keys %{$block_hash->{'new_files'}};
904 if (scalar(@new_files>0)) {
905 print STDERR "New files and modified metadata files since last import:\n ";
906 print STDERR join("\n ",@new_files), "\n";
907 }
908
909 if ($incremental) {
910 # only look for deletions if we are truely incremental
911 my @deleted_files = sort keys %{$block_hash->{'deleted_files'}};
912 # Filter out any in gsdl/tmp area
913 my @filtered_deleted_files = ();
914 my $gsdl_tmp_area = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "tmp");
915 my $collect_tmp_area = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'}, "tmp");
916 $gsdl_tmp_area = &util::filename_to_regex($gsdl_tmp_area);
917 $collect_tmp_area = &util::filename_to_regex($collect_tmp_area);
918
919 foreach my $df (@deleted_files) {
920 next if ($df =~ m/^$gsdl_tmp_area/);
921 next if ($df =~ m/^$collect_tmp_area/);
922
923 push(@filtered_deleted_files,$df);
924 }
925
926
927 @deleted_files = @filtered_deleted_files;
928
929 if (scalar(@deleted_files)>0) {
930 print STDERR "Files deleted since last import:\n ";
931 print STDERR join("\n ",@deleted_files), "\n";
932
933
934 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@deleted_files);
935
936 mark_docs_for_deletion($archive_info,$block_hash,\@deleted_files, $archivedir,$verbosity, "delete");
937 }
938
939 my @reindex_files = sort keys %{$block_hash->{'reindex_files'}};
940
941 if (scalar(@reindex_files)>0) {
942 print STDERR "Files to reindex since last import:\n ";
943 print STDERR join("\n ",@reindex_files), "\n";
944 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@reindex_files);
945 mark_docs_for_deletion($archive_info,$block_hash,\@reindex_files, $archivedir,$verbosity, "reindex");
946 }
947
948 }
949 }
950 }
951
952 # Check for existence of the file that's to contain earliestDateStamp in archivesdir
953 # Do nothing if the file already exists (file exists on incremental build).
954 # If the file doesn't exist, as happens on full build, create it and write out the current datestamp into it
955 # In buildcol, read the file's contents and set the earliestdateStamp in GS2's build.cfg / GS3's buildconfig.xml
956 # In doc.pm have set_oaiLastModified similar to set_lastmodified, and create the doc fields
957 # oailastmodified and oailastmodifieddate
958 my $earliestDatestampFile = &FileUtils::filenameConcatenate($archivedir, "earliestDatestamp");
959 if ($self->{'generate_auxiliary_files'}) {
960 if (!-f $earliestDatestampFile && -d $archivedir) {
961 my $current_time_in_seconds = time; # in seconds
962
963 if(open(FOUT, ">$earliestDatestampFile")) {
964 # || (&gsprintf(STDERR, "{common.cannot_open}: $!\n", $earliestDatestampFile) && die);
965 print FOUT $current_time_in_seconds;
966 close(FOUT);
967 }
968 else {
969 &gsprintf(STDERR, "{import.cannot_write_earliestdatestamp}\n", $earliestDatestampFile);
970 }
971
972 }
973 }
974
975 if ($self->{'manifest_version'} != 2)
976 {
977 $self->perform_process_files($manifest, $pluginfo, $importdir, '', $block_hash, $metadata, $processor, $maxdocs);
978 }
979
980 if ($saveas eq "FedoraMETS") {
981 # create collection "doc obj" for Fedora that contains
982 # collection-level metadata
983
984 my $doc_obj = new doc($config_filename,"nonindexed_doc","none");
985 $doc_obj->set_OID("collection");
986
987 my $col_name = undef;
988 my $col_meta = $collectcfg->{'collectionmeta'};
989
990 if (defined $col_meta) {
991 store_collectionmeta($col_meta,"collectionname",$doc_obj); # in GS3 this is a collection's name
992 store_collectionmeta($col_meta,"collectionextra",$doc_obj); # in GS3 this is a collection's description
993 }
994 $processor->process($doc_obj);
995 }
996
997 &plugin::end($pluginfo, $processor);
998
999 &plugin::deinit($pluginfo, $processor);
1000
1001 # Store the value of OIDCount (used in doc.pm) so it can be
1002 # restored correctly to this value on an incremental build
1003 # - this OIDcount file should only be generated for numerical oids [jmt12]
1004 if ($self->{'OIDtype'} eq 'incremental')
1005 {
1006 store_doc_oid_count($archivedir);
1007 }
1008
1009 # signal to the processor (plugout) that we have finished processing - if we are group processing, then the final output file needs closing.
1010 $processor->close_group_output() if $processor->is_group();
1011
1012# if ($inexport_mode eq "import") {
1013 if ($self->{'generate_auxiliary_files'}) {
1014 # write out the archive information file
1015 # for backwards compatability with archvies.inf file
1016 if ($arcinfo_doc_filename =~ m/(contents)|(\.inf)$/) {
1017 $archive_info->save_info($arcinfo_doc_filename);
1018 }
1019 else {
1020 $archive_info->save_revinfo_db($arcinfo_src_filename);
1021 }
1022 }
1023 return $pluginfo;
1024}
1025
1026# @function perform_process_files()
1027# while process_files() above prepares the system to import files this is the
1028# function that actually initiates the plugin pipeline to process the files.
1029# This function the therefore be overridden in subclasses of inexport.pm should
1030# they wish to do different or further processing
1031# @author jmt12
1032sub perform_process_files
1033{
1034 my $self = shift(@_);
1035 my ($manifest, $pluginfo, $importdir, $file_to_import, $block_hash, $metadata, $processor, $maxdocs) = @_;
1036 my $gli = $self->{'gli'};
1037 # specific file to process - via manifest version 2+
1038 if ($file_to_import ne '')
1039 {
1040 &plugin::read ($pluginfo, '', $file_to_import, $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
1041 }
1042 # global file scan - if we are using a new version manifest, files would have
1043 # been read above. Older manifests use extra settings in the $block_hash to
1044 # control what is imported, while non-manifest imports use a regular
1045 # $block_hash (so obeying process_exp and block_exp) [jmt12]
1046 elsif ($manifest eq '' || $self->{'manifest_version'} == 1)
1047 {
1048 &plugin::read ($pluginfo, $importdir, '', $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
1049 }
1050 else
1051 {
1052 print STDERR "Skipping perform_process_files() due to manifest presence and version\n";
1053 }
1054}
1055# perform_process_files()
1056
1057# @function generate_statistics()
1058sub generate_statistics
1059{
1060 my $self = shift @_;
1061 my ($pluginfo) = @_;
1062
1063 my $inexport_mode = $self->{'mode'};
1064 my $out = $self->{'out'};
1065 my $faillogname = $self->{'faillogname'};
1066 my $statsfile = $self->{'statsfile'};
1067 my $gli = $self->{'gli'};
1068
1069 &gsprintf($out, "\n");
1070 &gsprintf($out, "*********************************************\n");
1071 &gsprintf($out, "{$inexport_mode.complete}\n");
1072 &gsprintf($out, "*********************************************\n");
1073
1074 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
1075}
1076# generate_statistics()
1077
1078
1079# @function deinit()
1080# Close down any file handles that we opened (and hence are responsible for
1081# closing
1082sub deinit
1083{
1084 my $self = shift(@_);
1085 close OUT if $self->{'close_out'};
1086 close FAILLOG if $self->{'close_faillog'};
1087 close STATSFILE if $self->{'close_statsfile'};
1088}
1089# deinit()
1090
1091
1092sub store_collectionmeta
1093{
1094 my ($collectionmeta,$field,$doc_obj) = @_;
1095
1096 my $section = $doc_obj->get_top_section();
1097
1098 my $field_hash = $collectionmeta->{$field};
1099
1100 foreach my $k (keys %$field_hash)
1101 {
1102 my $val = $field_hash->{$k};
1103
1104 ### print STDERR "*** $k = $field_hash->{$k}\n";
1105
1106 my $md_label = "ex.$field";
1107
1108
1109 if ($k =~ m/^\[l=(.*?)\]$/)
1110 {
1111
1112 my $md_suffix = $1;
1113 $md_label .= "^$md_suffix";
1114 }
1115
1116
1117 $doc_obj->add_utf8_metadata($section,$md_label, $val);
1118
1119 # see collConfigxml.pm: GS2's "collectionextra" is called "description" in GS3,
1120 # while "collectionname" in GS2 is called "name" in GS3.
1121 # Variable $nameMap variable in collConfigxml.pm maps between GS2 and GS3
1122 if (($md_label eq "ex.collectionname^en") || ($md_label eq "ex.collectionname"))
1123 {
1124 $doc_obj->add_utf8_metadata($section,"dc.Title", $val);
1125 }
1126
1127 }
1128}
1129
1130
1131sub oid_count_file {
1132 my ($archivedir) = @_;
1133 return &FileUtils::filenameConcatenate($archivedir, "OIDcount");
1134}
1135
1136
1137sub prime_doc_oid_count
1138{
1139 my ($archivedir) = @_;
1140 my $oid_count_filename = &oid_count_file($archivedir);
1141
1142 if (-e $oid_count_filename) {
1143 if (open(OIDIN,"<$oid_count_filename")) {
1144 my $OIDcount = <OIDIN>;
1145 chomp $OIDcount;
1146 close(OIDIN);
1147
1148 $doc::OIDcount = $OIDcount;
1149 }
1150 else {
1151 &gsprintf(STDERR, "{import.cannot_read_OIDcount}\n", $oid_count_filename);
1152 }
1153 }
1154
1155}
1156
1157sub store_doc_oid_count
1158{
1159 # Use the file "OIDcount" in the archives directory to record
1160 # what value doc.pm got up to
1161
1162 my ($archivedir) = @_;
1163 my $oid_count_filename = &oid_count_file($archivedir);
1164
1165 # @todo $oidout = &FileUtils::openFileDescriptor($oid_count_filename, 'w') [jmt12]
1166 if (open(OIDOUT,">$oid_count_filename")) {
1167 print OIDOUT $doc::OIDcount, "\n";
1168
1169 close(OIDOUT);
1170 }
1171 else {
1172 &gsprintf(STDERR, "{import.cannot_write_OIDcount}\n", $oid_count_filename);
1173 }
1174}
1175
1176
1177
1178sub new_vs_old_import_diff
1179{
1180 my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
1181
1182 # Get the infodbtype value for this collection from the arcinfo object
1183 my $infodbtype = $archive_info->{'infodbtype'};
1184
1185 # in this method, we want to know if metadata files are modified or not.
1186 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
1187
1188 my $archiveinf_timestamp = -M $arcinfo_doc_filename;
1189
1190 # First convert all files to absolute form
1191 # This is to support the situation where the import folder is not
1192 # the default
1193
1194 my $prev_all_files = $archive_info->{'prev_import_filelist'};
1195 my $full_prev_all_files = {};
1196
1197 foreach my $prev_file (keys %$prev_all_files) {
1198
1199 if (!&FileUtils::isFilenameAbsolute($prev_file)) {
1200 my $full_prev_file = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},$prev_file);
1201 $full_prev_all_files->{$full_prev_file} = $prev_file;
1202 }
1203 else {
1204 $full_prev_all_files->{$prev_file} = $prev_file;
1205 }
1206 }
1207
1208
1209 # Figure out which are the new files, existing files and so
1210 # by implication the files from the previous import that are not
1211 # there any more => mark them for deletion
1212 foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
1213
1214 my $full_curr_file = $curr_file;
1215
1216 # entry in 'all_files' is moved to either 'existing_files',
1217 # 'deleted_files', 'new_files', or 'new_or_modified_metadata_files'
1218
1219 if (!&FileUtils::isFilenameAbsolute($curr_file)) {
1220 # add in import dir to make absolute
1221 $full_curr_file = &FileUtils::filenameConcatenate($importdir,$curr_file);
1222 }
1223
1224 # figure out if new file or not
1225 if (defined $full_prev_all_files->{$full_curr_file}) {
1226 # delete it so that only files that need deleting are left
1227 delete $full_prev_all_files->{$full_curr_file};
1228
1229 # had it before. is it a metadata file?
1230 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
1231
1232 # is it modified??
1233 if (-M $full_curr_file < $archiveinf_timestamp) {
1234 print STDERR "*** Detected a *modified metadata* file: $full_curr_file\n" if $verbosity >= 2;
1235 # its newer than last build
1236 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
1237 }
1238 }
1239 else {
1240 if ($incremental_mode eq "all") {
1241
1242 # had it before
1243 $block_hash->{'existing_files'}->{$full_curr_file} = 1;
1244
1245 }
1246 else {
1247 # Warning in "onlyadd" mode, but had it before!
1248 print STDERR "Warning: File $full_curr_file previously imported.\n";
1249 print STDERR " Treating as new file\n";
1250
1251 $block_hash->{'new_files'}->{$full_curr_file} = 1;
1252
1253 }
1254 }
1255 }
1256 else {
1257 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
1258 # the new file is the special sort of file greenstone uses
1259 # to attach metadata to src documents
1260 # i.e metadata.xml
1261 # (but note, the filename used is not constrained in
1262 # Greenstone to always be this)
1263
1264 print STDERR "*** Detected *new* metadata file: $full_curr_file\n" if $verbosity >= 2;
1265 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
1266 }
1267 else {
1268 $block_hash->{'new_files'}->{$full_curr_file} = 1;
1269 }
1270 }
1271
1272
1273 delete $block_hash->{'all_files'}->{$curr_file};
1274 }
1275
1276
1277
1278
1279 # Deal with complication of new or modified metadata files by forcing
1280 # everything from this point down in the file hierarchy to
1281 # be freshly imported.
1282 #
1283 # This may mean files that have not changed are reindexed, but does
1284 # guarantee by the end of processing all new metadata is correctly
1285 # associated with the relevant document(s).
1286
1287 foreach my $new_mdf (keys %{$block_hash->{'new_or_modified_metadata_files'}}) {
1288 my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
1289
1290 $situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
1291 $situated_dir = &util::filename_to_regex($situated_dir); # need to escape windows slash \ and brackets in regular expression
1292
1293 # Go through existing_files, and mark anything that is contained
1294 # within 'situated_dir' to be reindexed (in case some of the metadata
1295 # attaches to one of these files)
1296
1297 my $reindex_files = [];
1298
1299 foreach my $existing_f (keys %{$block_hash->{'existing_files'}}) {
1300
1301 if ($existing_f =~ m/^$situated_dir/) {
1302
1303 print STDERR "**** Existing file $existing_f\nis located within\n$situated_dir\n";
1304
1305 push(@$reindex_files,$existing_f);
1306 $block_hash->{'reindex_files'}->{$existing_f} = 1;
1307 delete $block_hash->{'existing_files'}->{$existing_f};
1308
1309 }
1310 }
1311
1312 # metadata file needs to be in new_files list so parsed by MetadataXMLPlug
1313 # (or equivalent)
1314 $block_hash->{'new_files'}->{$new_mdf} = 1;
1315
1316 }
1317
1318 # go through remaining existing files and work out what has changed and needs to be reindexed.
1319 my @existing_files = sort keys %{$block_hash->{'existing_files'}};
1320
1321 my $reindex_files = [];
1322
1323 foreach my $existing_filename (@existing_files) {
1324 if (-M $existing_filename < $archiveinf_timestamp) {
1325 # file is newer than last build
1326
1327 my $existing_file = $existing_filename;
1328 #my $collectdir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'});
1329
1330 #my $collectdir_resafe = &util::filename_to_regex($collectdir);
1331 #$existing_file =~ s/^$collectdir_resafe(\\|\/)?//;
1332
1333 print STDERR "**** Reindexing existing file: $existing_file\n";
1334
1335 push(@$reindex_files,$existing_file);
1336 $block_hash->{'reindex_files'}->{$existing_filename} = 1;
1337 }
1338
1339 }
1340
1341
1342 # By this point full_prev_all_files contains the files
1343 # mentioned in archiveinf-src.db but are not in the 'import'
1344 # folder (or whatever was specified through -importdir ...)
1345
1346 # This list can contain files that were created in the 'tmp' or
1347 # 'cache' areas (such as screen-size and thumbnail images).
1348 #
1349 # In building the final list of files to delete, we test to see if
1350 # it exists on the filesystem and if it does (unusual for a "normal"
1351 # file in import, but possible in the case of 'tmp' files),
1352 # supress it from going into the final list
1353
1354 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
1355
1356 my @deleted_files = values %$full_prev_all_files;
1357 map { my $curr_file = $_;
1358 my $full_curr_file = $curr_file;
1359
1360 if (!&FileUtils::isFilenameAbsolute($curr_file)) {
1361 # add in import dir to make absolute
1362
1363 $full_curr_file = &FileUtils::filenameConcatenate($collectdir,$curr_file);
1364 }
1365
1366
1367 if (!-e $full_curr_file) {
1368 $block_hash->{'deleted_files'}->{$curr_file} = 1;
1369 }
1370 } @deleted_files;
1371
1372
1373
1374}
1375
1376
1377# this is used to delete "deleted" docs, and to remove old versions of "changed" docs
1378# $mode is 'delete' or 'reindex'
1379sub mark_docs_for_deletion
1380{
1381 my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode) = @_;
1382
1383 my $mode_text = "deleted from index";
1384 if ($mode eq "reindex") {
1385 $mode_text = "reindexed";
1386 }
1387
1388 # Get the infodbtype value for this collection from the arcinfo object
1389 my $infodbtype = $archive_info->{'infodbtype'};
1390
1391 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
1392 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-src", $archivedir);
1393
1394
1395 # record files marked for deletion in arcinfo
1396 foreach my $file (@$deleted_files) {
1397 # use 'archiveinf-src' info database file to look up all the OIDs
1398 # that this file is used in (note in most cases, it's just one OID)
1399
1400 my $relfile = &util::abspath_to_placeholders($file);
1401
1402 my $src_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_src_filename, $relfile);
1403 my $oids = $src_rec->{'oid'};
1404 my $file_record_deleted = 0;
1405
1406 # delete the src record
1407 my $src_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_src_filename, "append");
1408 &dbutil::delete_infodb_entry($infodbtype, $src_infodb_file_handle, $relfile);
1409 &dbutil::close_infodb_write_handle($infodbtype, $src_infodb_file_handle);
1410
1411
1412 foreach my $oid (@$oids) {
1413
1414 # find the source doc (the primary file that becomes this oid)
1415 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $oid);
1416 my $doc_source_file = $doc_rec->{'src-file'}->[0];
1417 $doc_source_file = &util::placeholders_to_abspath($doc_source_file);
1418
1419 if (!&FileUtils::isFilenameAbsolute($doc_source_file)) {
1420 $doc_source_file = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},$doc_source_file);
1421 }
1422
1423 if ($doc_source_file ne $file) {
1424 # its an associated or metadata file
1425
1426 # mark source doc for reimport as one of its assoc files has changed or deleted
1427 $block_hash->{'reindex_files'}->{$doc_source_file} = 1;
1428
1429 }
1430 my $curr_status = $archive_info->get_status_info($oid);
1431 if (defined($curr_status) && (($curr_status ne "D"))) {
1432 if ($verbosity>1) {
1433 print STDERR "$oid ($doc_source_file) marked to be $mode_text on next buildcol.pl\n";
1434 }
1435 # mark oid for deletion (it will be deleted or reimported)
1436 $archive_info->set_status_info($oid,"D");
1437 my $val = &dbutil::read_infodb_rawentry($infodbtype, $arcinfo_doc_filename, $oid);
1438 $val =~ s/^<index-status>(.*)$/<index-status>D/m;
1439
1440 my $val_rec = &dbutil::convert_infodb_string_to_hash($val);
1441 my $doc_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_doc_filename, "append");
1442
1443 &dbutil::write_infodb_entry($infodbtype, $doc_infodb_file_handle, $oid, $val_rec);
1444 &dbutil::close_infodb_write_handle($infodbtype, $doc_infodb_file_handle);
1445 }
1446 }
1447
1448 }
1449
1450 # now go through and check that we haven't marked any primary
1451 # files for reindex (because their associated files have
1452 # changed/deleted) when they have been deleted themselves. only in
1453 # delete mode.
1454
1455 if ($mode eq "delete") {
1456 foreach my $file (@$deleted_files) {
1457 if (defined $block_hash->{'reindex_files'}->{$file}) {
1458 delete $block_hash->{'reindex_files'}->{$file};
1459 }
1460 }
1461 }
1462
1463
1464}
1465
1466sub add_dir_contents_to_list {
1467
1468 my ($dirname, $list) = @_;
1469
1470 # Recur over directory contents.
1471 my (@dir, $subfile);
1472
1473 # find all the files in the directory
1474 if (!opendir (DIR, $dirname)) {
1475 print STDERR "inexport: WARNING - couldn't read directory $dirname\n";
1476 return -1; # error in processing
1477 }
1478 @dir = readdir (DIR);
1479 closedir (DIR);
1480
1481 for (my $i = 0; $i < scalar(@dir); $i++) {
1482 my $subfile = $dir[$i];
1483 next if ($subfile =~ m/^\.\.?$/);
1484 next if ($subfile =~ /^\.svn$/);
1485 my $full_file = &FileUtils::filenameConcatenate($dirname, $subfile);
1486 if (-d $full_file) {
1487 &add_dir_contents_to_list($full_file, $list);
1488 } else {
1489 push (@$list, $full_file);
1490 }
1491 }
1492
1493}
1494
1495
14961;
Note: See TracBrowser for help on using the repository browser.