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

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

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

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