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

Last change on this file since 30517 was 30517, checked in by ak19, 8 years ago

Fixing incremental-rebuild when the database is gdbm. At this point (see buildcolutils.pm), the code needs to deactivate the collection before calling make_infodatabase(), since otherwise there's a lock on the gdbm database which prevents successful incremental-rebuild and activation.

  • Property svn:executable set to *
File size: 47.4 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'} && !$self->{'keepold'})
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 guarenteed 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 }
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 }
936 }
937
938 # Check for existence of the file that's to contain earliestDateStamp in archivesdir
939 # Do nothing if the file already exists (file exists on incremental build).
940 # If the file doesn't exist, as happens on full build, create it and write out the current datestamp into it
941 # In buildcol, read the file's contents and set the earliestdateStamp in GS2's build.cfg / GS3's buildconfig.xml
942 # In doc.pm have set_oaiLastModified similar to set_lastmodified, and create the doc fields
943 # oailastmodified and oailastmodifieddate
944 my $earliestDatestampFile = &FileUtils::filenameConcatenate($archivedir, "earliestDatestamp");
945 if ($self->{'generate_auxiliary_files'}) {
946 if (!-f $earliestDatestampFile && -d $archivedir) {
947 my $current_time_in_seconds = time; # in seconds
948
949 if(open(FOUT, ">$earliestDatestampFile")) {
950 # || (&gsprintf(STDERR, "{common.cannot_open}: $!\n", $earliestDatestampFile) && die);
951 print FOUT $current_time_in_seconds;
952 close(FOUT);
953 }
954 else {
955 &gsprintf(STDERR, "{import.cannot_write_earliestdatestamp}\n", $earliestDatestampFile);
956 }
957
958 }
959 }
960
961 $self->perform_process_files($manifest, $pluginfo, $importdir, '', $block_hash, $metadata, $processor, $maxdocs);
962
963 if ($saveas eq "FedoraMETS") {
964 # create collection "doc obj" for Fedora that contains
965 # collection-level metadata
966
967 my $doc_obj = new doc($config_filename,"nonindexed_doc","none");
968 $doc_obj->set_OID("collection");
969
970 my $col_name = undef;
971 my $col_meta = $collectcfg->{'collectionmeta'};
972
973 if (defined $col_meta) {
974 store_collectionmeta($col_meta,"collectionname",$doc_obj); # in GS3 this is a collection's name
975 store_collectionmeta($col_meta,"collectionextra",$doc_obj); # in GS3 this is a collection's description
976 }
977 $processor->process($doc_obj);
978 }
979
980 &plugin::end($pluginfo, $processor);
981
982 &plugin::deinit($pluginfo, $processor);
983
984 # Store the value of OIDCount (used in doc.pm) so it can be
985 # restored correctly to this value on an incremental build
986 # - this OIDcount file should only be generated for numerical oids [jmt12]
987 if ($self->{'OIDtype'} eq 'incremental')
988 {
989 store_doc_oid_count($archivedir);
990 }
991
992 # signal to the processor (plugout) that we have finished processing - if we are group processing, then the final output file needs closing.
993 $processor->close_group_output() if $processor->is_group();
994
995# if ($inexport_mode eq "import") {
996 if ($self->{'generate_auxiliary_files'}) {
997 # write out the archive information file
998 # for backwards compatability with archvies.inf file
999 if ($arcinfo_doc_filename =~ m/(contents)|(\.inf)$/) {
1000 $archive_info->save_info($arcinfo_doc_filename);
1001 }
1002 else {
1003 $archive_info->save_revinfo_db($arcinfo_src_filename);
1004 }
1005 }
1006 return $pluginfo;
1007}
1008
1009# @function perform_process_files()
1010# while process_files() above prepares the system to import files this is the
1011# function that actually initiates the plugin pipeline to process the files.
1012# This function the therefore be overridden in subclasses of inexport.pm should
1013# they wish to do different or further processing
1014# @author jmt12
1015sub perform_process_files
1016{
1017 my $self = shift(@_);
1018 my ($manifest, $pluginfo, $importdir, $file_to_import, $block_hash, $metadata, $processor, $maxdocs) = @_;
1019 my $gli = $self->{'gli'};
1020 # specific file to process - via manifest version 2+
1021 if ($file_to_import ne '')
1022 {
1023 &plugin::read ($pluginfo, '', $file_to_import, $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
1024 }
1025 # global file scan - if we are using a new version manifest, files would have
1026 # been read above. Older manifests use extra settings in the $block_hash to
1027 # control what is imported, while non-manifest imports use a regular
1028 # $block_hash (so obeying process_exp and block_exp) [jmt12]
1029 elsif ($manifest eq '' || $self->{'manifest_version'} == 1)
1030 {
1031 &plugin::read ($pluginfo, $importdir, '', $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
1032 }
1033 else
1034 {
1035 print STDERR "Skipping perform_process_files() due to manifest presence and version\n";
1036 }
1037}
1038# perform_process_files()
1039
1040# @function generate_statistics()
1041sub generate_statistics
1042{
1043 my $self = shift @_;
1044 my ($pluginfo) = @_;
1045
1046 my $inexport_mode = $self->{'mode'};
1047 my $out = $self->{'out'};
1048 my $faillogname = $self->{'faillogname'};
1049 my $statsfile = $self->{'statsfile'};
1050 my $gli = $self->{'gli'};
1051
1052 &gsprintf($out, "\n");
1053 &gsprintf($out, "*********************************************\n");
1054 &gsprintf($out, "{$inexport_mode.complete}\n");
1055 &gsprintf($out, "*********************************************\n");
1056
1057 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
1058}
1059# generate_statistics()
1060
1061
1062# @function deinit()
1063# Close down any file handles that we opened (and hence are responsible for
1064# closing
1065sub deinit
1066{
1067 my $self = shift(@_);
1068 close OUT if $self->{'close_out'};
1069 close FAILLOG if $self->{'close_faillog'};
1070 close STATSFILE if $self->{'close_statsfile'};
1071}
1072# deinit()
1073
1074
1075sub store_collectionmeta
1076{
1077 my ($collectionmeta,$field,$doc_obj) = @_;
1078
1079 my $section = $doc_obj->get_top_section();
1080
1081 my $field_hash = $collectionmeta->{$field};
1082
1083 foreach my $k (keys %$field_hash)
1084 {
1085 my $val = $field_hash->{$k};
1086
1087 ### print STDERR "*** $k = $field_hash->{$k}\n";
1088
1089 my $md_label = "ex.$field";
1090
1091
1092 if ($k =~ m/^\[l=(.*?)\]$/)
1093 {
1094
1095 my $md_suffix = $1;
1096 $md_label .= "^$md_suffix";
1097 }
1098
1099
1100 $doc_obj->add_utf8_metadata($section,$md_label, $val);
1101
1102 # see collConfigxml.pm: GS2's "collectionextra" is called "description" in GS3,
1103 # while "collectionname" in GS2 is called "name" in GS3.
1104 # Variable $nameMap variable in collConfigxml.pm maps between GS2 and GS3
1105 if (($md_label eq "ex.collectionname^en") || ($md_label eq "ex.collectionname"))
1106 {
1107 $doc_obj->add_utf8_metadata($section,"dc.Title", $val);
1108 }
1109
1110 }
1111}
1112
1113
1114sub oid_count_file {
1115 my ($archivedir) = @_;
1116 return &FileUtils::filenameConcatenate($archivedir, "OIDcount");
1117}
1118
1119
1120sub prime_doc_oid_count
1121{
1122 my ($archivedir) = @_;
1123 my $oid_count_filename = &oid_count_file($archivedir);
1124
1125 if (-e $oid_count_filename) {
1126 if (open(OIDIN,"<$oid_count_filename")) {
1127 my $OIDcount = <OIDIN>;
1128 chomp $OIDcount;
1129 close(OIDIN);
1130
1131 $doc::OIDcount = $OIDcount;
1132 }
1133 else {
1134 &gsprintf(STDERR, "{import.cannot_read_OIDcount}\n", $oid_count_filename);
1135 }
1136 }
1137
1138}
1139
1140sub store_doc_oid_count
1141{
1142 # Use the file "OIDcount" in the archives directory to record
1143 # what value doc.pm got up to
1144
1145 my ($archivedir) = @_;
1146 my $oid_count_filename = &oid_count_file($archivedir);
1147
1148 # @todo $oidout = &FileUtils::openFileDescriptor($oid_count_filename, 'w') [jmt12]
1149 if (open(OIDOUT,">$oid_count_filename")) {
1150 print OIDOUT $doc::OIDcount, "\n";
1151
1152 close(OIDOUT);
1153 }
1154 else {
1155 &gsprintf(STDERR, "{import.cannot_write_OIDcount}\n", $oid_count_filename);
1156 }
1157}
1158
1159
1160
1161sub new_vs_old_import_diff
1162{
1163 my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
1164
1165 # Get the infodbtype value for this collection from the arcinfo object
1166 my $infodbtype = $archive_info->{'infodbtype'};
1167
1168 # in this method, we want to know if metadata files are modified or not.
1169 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
1170
1171 my $archiveinf_timestamp = -M $arcinfo_doc_filename;
1172
1173 # First convert all files to absolute form
1174 # This is to support the situation where the import folder is not
1175 # the default
1176
1177 my $prev_all_files = $archive_info->{'prev_import_filelist'};
1178 my $full_prev_all_files = {};
1179
1180 foreach my $prev_file (keys %$prev_all_files) {
1181
1182 if (!&FileUtils::isFilenameAbsolute($prev_file)) {
1183 my $full_prev_file = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},$prev_file);
1184 $full_prev_all_files->{$full_prev_file} = $prev_file;
1185 }
1186 else {
1187 $full_prev_all_files->{$prev_file} = $prev_file;
1188 }
1189 }
1190
1191
1192 # Figure out which are the new files, existing files and so
1193 # by implication the files from the previous import that are not
1194 # there any more => mark them for deletion
1195 foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
1196
1197 my $full_curr_file = $curr_file;
1198
1199 # entry in 'all_files' is moved to either 'existing_files',
1200 # 'deleted_files', 'new_files', or 'new_or_modified_metadata_files'
1201
1202 if (!&FileUtils::isFilenameAbsolute($curr_file)) {
1203 # add in import dir to make absolute
1204 $full_curr_file = &FileUtils::filenameConcatenate($importdir,$curr_file);
1205 }
1206
1207 # figure out if new file or not
1208 if (defined $full_prev_all_files->{$full_curr_file}) {
1209 # delete it so that only files that need deleting are left
1210 delete $full_prev_all_files->{$full_curr_file};
1211
1212 # had it before. is it a metadata file?
1213 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
1214
1215 # is it modified??
1216 if (-M $full_curr_file < $archiveinf_timestamp) {
1217 print STDERR "*** Detected a *modified metadata* file: $full_curr_file\n" if $verbosity >= 2;
1218 # its newer than last build
1219 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
1220 }
1221 }
1222 else {
1223 if ($incremental_mode eq "all") {
1224
1225 # had it before
1226 $block_hash->{'existing_files'}->{$full_curr_file} = 1;
1227
1228 }
1229 else {
1230 # Warning in "onlyadd" mode, but had it before!
1231 print STDERR "Warning: File $full_curr_file previously imported.\n";
1232 print STDERR " Treating as new file\n";
1233
1234 $block_hash->{'new_files'}->{$full_curr_file} = 1;
1235
1236 }
1237 }
1238 }
1239 else {
1240 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
1241 # the new file is the special sort of file greenstone uses
1242 # to attach metadata to src documents
1243 # i.e metadata.xml
1244 # (but note, the filename used is not constrained in
1245 # Greenstone to always be this)
1246
1247 print STDERR "*** Detected *new* metadata file: $full_curr_file\n" if $verbosity >= 2;
1248 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
1249 }
1250 else {
1251 $block_hash->{'new_files'}->{$full_curr_file} = 1;
1252 }
1253 }
1254
1255
1256 delete $block_hash->{'all_files'}->{$curr_file};
1257 }
1258
1259
1260
1261
1262 # Deal with complication of new or modified metadata files by forcing
1263 # everything from this point down in the file hierarchy to
1264 # be freshly imported.
1265 #
1266 # This may mean files that have not changed are reindexed, but does
1267 # guarantee by the end of processing all new metadata is correctly
1268 # associated with the relevant document(s).
1269
1270 foreach my $new_mdf (keys %{$block_hash->{'new_or_modified_metadata_files'}}) {
1271 my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
1272
1273 $situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
1274 $situated_dir = &util::filename_to_regex($situated_dir); # need to escape windows slash \ and brackets in regular expression
1275
1276 # Go through existing_files, and mark anything that is contained
1277 # within 'situated_dir' to be reindexed (in case some of the metadata
1278 # attaches to one of these files)
1279
1280 my $reindex_files = [];
1281
1282 foreach my $existing_f (keys %{$block_hash->{'existing_files'}}) {
1283
1284 if ($existing_f =~ m/^$situated_dir/) {
1285
1286# print STDERR "**** Existing file $existing_f\nis located within\n$situated_dir\n";
1287
1288 push(@$reindex_files,$existing_f);
1289 $block_hash->{'reindex_files'}->{$existing_f} = 1;
1290 delete $block_hash->{'existing_files'}->{$existing_f};
1291
1292 }
1293 }
1294
1295 # metadata file needs to be in new_files list so parsed by MetadataXMLPlug
1296 # (or equivalent)
1297 $block_hash->{'new_files'}->{$new_mdf} = 1;
1298
1299 }
1300
1301 # go through remaining existing files and work out what has changed and needs to be reindexed.
1302 my @existing_files = sort keys %{$block_hash->{'existing_files'}};
1303
1304 my $reindex_files = [];
1305
1306 foreach my $existing_filename (@existing_files) {
1307 if (-M $existing_filename < $archiveinf_timestamp) {
1308 # file is newer than last build
1309
1310 my $existing_file = $existing_filename;
1311 #my $collectdir = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'});
1312
1313 #my $collectdir_resafe = &util::filename_to_regex($collectdir);
1314 #$existing_file =~ s/^$collectdir_resafe(\\|\/)?//;
1315
1316 print STDERR "**** Reindexing existing file: $existing_file\n";
1317
1318 push(@$reindex_files,$existing_file);
1319 $block_hash->{'reindex_files'}->{$existing_filename} = 1;
1320 }
1321
1322 }
1323
1324
1325 # By this point full_prev_all_files contains the files
1326 # mentioned in archiveinf-src.db but are not in the 'import'
1327 # folder (or whatever was specified through -importdir ...)
1328
1329 # This list can contain files that were created in the 'tmp' or
1330 # 'cache' areas (such as screen-size and thumbnail images).
1331 #
1332 # In building the final list of files to delete, we test to see if
1333 # it exists on the filesystem and if it does (unusual for a "normal"
1334 # file in import, but possible in the case of 'tmp' files),
1335 # supress it from going into the final list
1336
1337 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
1338
1339 my @deleted_files = values %$full_prev_all_files;
1340 map { my $curr_file = $_;
1341 my $full_curr_file = $curr_file;
1342
1343 if (!&FileUtils::isFilenameAbsolute($curr_file)) {
1344 # add in import dir to make absolute
1345
1346 $full_curr_file = &FileUtils::filenameConcatenate($collectdir,$curr_file);
1347 }
1348
1349
1350 if (!-e $full_curr_file) {
1351 $block_hash->{'deleted_files'}->{$curr_file} = 1;
1352 }
1353 } @deleted_files;
1354
1355
1356
1357}
1358
1359
1360# this is used to delete "deleted" docs, and to remove old versions of "changed" docs
1361# $mode is 'delete' or 'reindex'
1362sub mark_docs_for_deletion
1363{
1364 my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode) = @_;
1365
1366 my $mode_text = "deleted from index";
1367 if ($mode eq "reindex") {
1368 $mode_text = "reindexed";
1369 }
1370
1371 # Get the infodbtype value for this collection from the arcinfo object
1372 my $infodbtype = $archive_info->{'infodbtype'};
1373
1374 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
1375 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-src", $archivedir);
1376
1377
1378 # record files marked for deletion in arcinfo
1379 foreach my $file (@$deleted_files) {
1380 # use 'archiveinf-src' info database file to look up all the OIDs
1381 # that this file is used in (note in most cases, it's just one OID)
1382
1383 my $relfile = &util::abspath_to_placeholders($file);
1384
1385 my $src_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_src_filename, $relfile);
1386 my $oids = $src_rec->{'oid'};
1387 my $file_record_deleted = 0;
1388
1389 # delete the src record
1390 my $src_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_src_filename, "append");
1391 &dbutil::delete_infodb_entry($infodbtype, $src_infodb_file_handle, $relfile);
1392 &dbutil::close_infodb_write_handle($infodbtype, $src_infodb_file_handle);
1393
1394
1395 foreach my $oid (@$oids) {
1396
1397 # find the source doc (the primary file that becomes this oid)
1398 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $oid);
1399 my $doc_source_file = $doc_rec->{'src-file'}->[0];
1400 $doc_source_file = &util::placeholders_to_abspath($doc_source_file);
1401
1402 if (!&FileUtils::isFilenameAbsolute($doc_source_file)) {
1403 $doc_source_file = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},$doc_source_file);
1404 }
1405
1406 if ($doc_source_file ne $file) {
1407 # its an associated or metadata file
1408
1409 # mark source doc for reimport as one of its assoc files has changed or deleted
1410 $block_hash->{'reindex_files'}->{$doc_source_file} = 1;
1411
1412 }
1413 my $curr_status = $archive_info->get_status_info($oid);
1414 if (defined($curr_status) && (($curr_status ne "D"))) {
1415 if ($verbosity>1) {
1416 print STDERR "$oid ($doc_source_file) marked to be $mode_text on next buildcol.pl\n";
1417 }
1418 # mark oid for deletion (it will be deleted or reimported)
1419 $archive_info->set_status_info($oid,"D");
1420 my $val = &dbutil::read_infodb_rawentry($infodbtype, $arcinfo_doc_filename, $oid);
1421 $val =~ s/^<index-status>(.*)$/<index-status>D/m;
1422
1423 my $val_rec = &dbutil::convert_infodb_string_to_hash($infodbtype,$val);
1424 my $doc_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_doc_filename, "append");
1425
1426 &dbutil::write_infodb_entry($infodbtype, $doc_infodb_file_handle, $oid, $val_rec);
1427 &dbutil::close_infodb_write_handle($infodbtype, $doc_infodb_file_handle);
1428 }
1429 }
1430
1431 }
1432
1433 # now go through and check that we haven't marked any primary
1434 # files for reindex (because their associated files have
1435 # changed/deleted) when they have been deleted themselves. only in
1436 # delete mode.
1437
1438 if ($mode eq "delete") {
1439 foreach my $file (@$deleted_files) {
1440 if (defined $block_hash->{'reindex_files'}->{$file}) {
1441 delete $block_hash->{'reindex_files'}->{$file};
1442 }
1443 }
1444 }
1445
1446
1447}
1448
1449sub add_dir_contents_to_list {
1450
1451 my ($dirname, $list) = @_;
1452
1453 # Recur over directory contents.
1454 my (@dir, $subfile);
1455
1456 # find all the files in the directory
1457 if (!opendir (DIR, $dirname)) {
1458 print STDERR "inexport: WARNING - couldn't read directory $dirname\n";
1459 return -1; # error in processing
1460 }
1461 @dir = readdir (DIR);
1462 closedir (DIR);
1463
1464 for (my $i = 0; $i < scalar(@dir); $i++) {
1465 my $subfile = $dir[$i];
1466 next if ($subfile =~ m/^\.\.?$/);
1467 next if ($subfile =~ /^\.svn$/);
1468 my $full_file = &FileUtils::filenameConcatenate($dirname, $subfile);
1469 if (-d $full_file) {
1470 &add_dir_contents_to_list($full_file, $list);
1471 } else {
1472 push (@$list, $full_file);
1473 }
1474 }
1475
1476}
1477
1478
14791;
Note: See TracBrowser for help on using the repository browser.