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

Last change on this file since 27394 was 27394, checked in by jmt12, 11 years ago

Replaced hardcoded -e (which should have been a -d anyway) with FileUtils::directoryExists() call and replaced hardcoded pregs to remove extra and trailing slashes in path with call to FileUtils::sanitizePath() (which does the same thing, but in a way that is aware of protocol prefixes)

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