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

Last change on this file since 31189 was 31189, checked in by ak19, 4 years ago

Corrections and cleanups ahead of major commit to do with the new oaiinfo db code.

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