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

Last change on this file since 29013 was 29013, checked in by ak19, 10 years ago

Following Dr Bainbridge's suggestion on determining a document's title which forms the anchor text for its RSS link.

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