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

Last change on this file since 30286 was 30286, checked in by jmt12, 9 years ago

Adding a customized version of inexport.pm allowing us to handle metadata.xml files associated with files being processed in parallel (and hence using a version 2 manifest)

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