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

Last change on this file since 23053 was 23053, checked in by kjdon, 14 years ago

reworking of manifest stuff. Now, instead of calling plugin::read on each file in the reindex/new_files lists, we add all the filenames into the standard block_hash. This is the equivalent of what the code for incremental build does, except that instead of inspecting the import folder, we just use what is listed in manifest file. If a directory is listed, then we look through the directory and add each file in there to the appropriate list. Once block_hash is set up, then we tell the plugins we are incremental, and just process the import folder as per usual. Only those docs in new_files and reindex lists in block_hash will be processed. But this gets a full metadata_read done, which we need in case there is metadata specified up a level for the files that are changed/new.

  • Property svn:executable set to *
File size: 36.0 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
[22413]50sub new
51{
52 my $class = shift (@_);
[22421]53 my ($mode,$argv,$options,$opt_listall_options) = @_;
[22413]54
[22421]55 my $self = { 'xml' => 0, 'mode' => $mode };
[22413]56
57 # general options available to all plugins
58 my $arguments = $options->{'args'};
59 my $intArgLeftinAfterParsing = parse2::parse($argv,$arguments,$self,"allow_extra_options");
60 # Parse returns -1 if something has gone wrong
61 if ($intArgLeftinAfterParsing == -1)
62 {
63 &PrintUsage::print_txt_usage($options, "{import.params}");
64 die "\n";
65 }
66
67 my $language = $self->{'language'};
68 # If $language has been specified, load the appropriate resource bundle
69 # (Otherwise, the default resource bundle will be loaded automatically)
70 if ($language && $language =~ /\S/) {
71 &gsprintf::load_language_specific_resource_bundle($language);
72 }
73
[22421]74 if ($self->{'listall'}) {
75 if ($self->{'xml'}) {
76 &PrintUsage::print_xml_usage($opt_listall_options);
77 }
78 else
79 {
80 &PrintUsage::print_txt_usage($opt_listall_options,"{export.params}");
81 }
82 die "\n";
83 }
84
85
[22413]86 if ($self->{'xml'}) {
87 &PrintUsage::print_xml_usage($options);
88 print "\n";
[22460]89 return bless $self, $class;
[22413]90 }
91
92 if ($self->{'gli'}) { # the gli wants strings to be in UTF-8
93 &gsprintf::output_strings_in_UTF8;
94 }
95
96 # now check that we had exactly one leftover arg, which should be
97 # the collection name. We don't want to do this earlier, cos
98 # -xml arg doesn't need a collection name
99 # Or if the user specified -h, then we output the usage also
[22460]100
[22413]101 if ($intArgLeftinAfterParsing != 1 || (@$argv && $argv->[0] =~ /^\-+h/))
102 {
103 &PrintUsage::print_txt_usage($options, "{import.params}");
104 die "\n";
105 }
106
107 $self->{'close_out'} = 0;
108 my $out = $self->{'out'};
109 if ($out !~ /^(STDERR|STDOUT)$/i) {
110 open (OUT, ">$out") ||
111 (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $out) && die);
[23042]112 $out = 'inexport::OUT';
[22413]113 $self->{'close_out'} = 1;
114 }
115 $out->autoflush(1);
116 $self->{'out'} = $out;
117
118 # @ARGV should be only one item, the name of the collection
119 $self->{'collection'} = shift @$argv;
120
[22445]121 if ((defined $self->{'jobs'}) && ($self->{'jobs'}>1)) {
122 require ParallelInexport;
123 }
124
[22413]125 return bless $self, $class;
126}
127
128sub get_collection
129{
130 my $self = shift @_;
131
132 return $self->{'collection'};
133}
134
135
136sub read_collection_cfg
137{
138 my $self = shift @_;
139 my ($collection,$options) = @_;
140
141 my $collectdir = $self->{'collectdir'};
142 my $site = $self->{'site'};
143 my $out = $self->{'out'};
[22567]144
[22413]145 if (($collection = &colcfg::use_collection($site, $collection, $collectdir)) eq "") {
146 &PrintUsage::print_txt_usage($options, "{import.params}");
147 die "\n";
148 }
149
150 # add collection's perllib dir into include path in
151 # case we have collection specific modules
152 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
153
154 # check that we can open the faillog
155 my $faillog = $self->{'faillog'};
156 if ($faillog eq "") {
157 $faillog = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "etc", "fail.log");
158 }
159 open (FAILLOG, ">$faillog") ||
160 (&gsprintf(STDERR, "{import.cannot_open_fail_log}\n", $faillog) && die);
161
162
163 my $faillogname = $faillog;
164 $faillog = 'inexport::FAILLOG';
165 $faillog->autoflush(1);
166 $self->{'faillog'} = $faillog;
167 $self->{'faillogname'} = $faillogname;
168
169 # Read in the collection configuration file.
[22421]170 my ($config_filename, $gs_mode) = &colcfg::get_collect_cfg_name($out);
171 my $collectcfg = &colcfg::read_collection_cfg ($config_filename, $gs_mode);
[22413]172
[22421]173 return ($config_filename,$collectcfg);
[22413]174}
175
176sub set_collection_options
177{
178 my $self = shift @_;
[22421]179 my ($collectcfg) = @_;
[22413]180
[22421]181 my $inexport_mode = $self->{'mode'};
182
[22413]183 my $verbosity = $self->{'verbosity'};
184 my $debug = $self->{'debug'};
185 my $importdir = $self->{'importdir'};
[22466]186 my $archivedir = $self->{'archivedir'} || $self->{'exportdir'} || "";
[22413]187 my $out = $self->{'out'};
188
189 # If the infodbtype value wasn't defined in the collect.cfg file, use the default
190 if (!defined($collectcfg->{'infodbtype'}))
191 {
192 $collectcfg->{'infodbtype'} = &dbutil::get_default_infodb_type();
193 }
194
195 if (defined $collectcfg->{'importdir'} && $importdir eq "") {
196 $importdir = $collectcfg->{'importdir'};
197 }
198 if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
199 $archivedir = $collectcfg->{'archivedir'};
200 }
201 # fill in the default import and archives directories if none
202 # were supplied, turn all \ into / and remove trailing /
203 $importdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "import") if $importdir eq "";
204 $importdir =~ s/[\\\/]+/\//g;
205 $importdir =~ s/\/$//;
206 if (!-e $importdir) {
207 &gsprintf($out, "{import.no_import_dir}\n\n", $importdir);
208 die "\n";
209 }
210 $self->{'importdir'} = $importdir;
211
212 if ($archivedir eq "") {
213 if ($inexport_mode eq "import") {
214 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
215 }
216 elsif ($inexport_mode eq "export") {
217 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "export");
218 }
219 else {
220 print STDERR "Warning: Unrecognized import/export mode '$inexport_mode'\n";
221 print STDERR " Defaulting to 'archives' for file output\n";
222 $archivedir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "archives");
223 }
224 }
225
226 $archivedir =~ s/[\\\/]+/\//g;
227 $archivedir =~ s/\/$//;
228 $self->{'archivedir'} = $archivedir;
229
230 if ($verbosity !~ /\d+/) {
231 if (defined $collectcfg->{'verbosity'} && $collectcfg->{'verbosity'} =~ /\d+/) {
232 $verbosity = $collectcfg->{'verbosity'};
233 } else {
234 $verbosity = 2; # the default
235 }
236 }
[22421]237 $self->{'verbosity'} = $verbosity;
238
[22413]239 if (defined $collectcfg->{'manifest'} && $self->{'manifest'} eq "") {
240 $self->{'manifest'} = $collectcfg->{'manifest'};
241 }
242
243 if (defined $collectcfg->{'gzip'} && !$self->{'gzip'}) {
244 if ($collectcfg->{'gzip'} =~ /^true$/i) {
245 $self->{'gzip'} = 1;
246 }
247 }
248
249 if ($self->{'maxdocs'} !~ /\-?\d+/) {
250 if (defined $collectcfg->{'maxdocs'} && $collectcfg->{'maxdocs'} =~ /\-?\d+/) {
251 $self->{'maxdocs'} = $collectcfg->{'maxdocs'};
252 } else {
253 $self->{'maxdocs'} = -1; # the default
254 }
255 }
[22421]256
257 if ((defined $self->{'groupsize'}) && ($self->{'groupsize'} == 1)) {
[22413]258 if (defined $collectcfg->{'groupsize'} && $collectcfg->{'groupsize'} =~ /\d+/) {
259 $self->{'groupsize'} = $collectcfg->{'groupsize'};
260 }
261 }
262
263 if (!defined $self->{'OIDtype'}
264 || ($self->{'OIDtype'} !~ /^(hash|incremental|assigned|dirname)$/ )) {
265 if (defined $collectcfg->{'OIDtype'}
266 && $collectcfg->{'OIDtype'} =~ /^(hash|incremental|assigned|dirname)$/) {
267 $self->{'OIDtype'} = $collectcfg->{'OIDtype'};
268 } else {
269 $self->{'OIDtype'} = "hash"; # the default
270 }
271 }
272
273 if ((!defined $self->{'OIDmetadata'}) || ($self->{'OIDmetadata'} eq "")) {
274 if (defined $collectcfg->{'OIDmetadata'}) {
275 $self->{'OIDmetadata'} = $collectcfg->{'OIDmetadata'};
276 } else {
277 $self->{'OIDmetadata'} = "dc.Identifier"; # the default
278 }
279 }
280
281 my $sortmeta = $self->{'sortmeta'};
282 if (defined $collectcfg->{'sortmeta'} && (!defined $sortmeta || $sortmeta eq "")) {
283 $sortmeta = $collectcfg->{'sortmeta'};
284 }
285 # sortmeta cannot be used with group size
286 $sortmeta = undef unless defined $sortmeta && $sortmeta =~ /\S/;
287 if (defined $sortmeta && $self->{'groupsize'} > 1) {
288 &gsprintf($out, "{import.cannot_sort}\n\n");
289 $sortmeta = undef;
290 }
291 $self->{'sortmeta'} = $sortmeta;
292
293 if (defined $collectcfg->{'removeprefix'} && $self->{'removeprefix'} eq "") {
294 $self->{'removeprefix'} = $collectcfg->{'removeprefix'};
295 }
296
297 if (defined $collectcfg->{'removesuffix'} && $self->{'removesuffix'} eq "") {
298 $self->{'removesuffix'} = $collectcfg->{'removesuffix'};
299 }
300 if (defined $collectcfg->{'debug'} && $collectcfg->{'debug'} =~ /^true$/i) {
301 $self->{'debug'} = 1;
302 }
303 if (defined $collectcfg->{'gli'} && $collectcfg->{'gli'} =~ /^true$/i) {
304 $self->{'gli'} = 1;
305 }
306 $self->{'gli'} = 0 unless defined $self->{'gli'};
307
308 # check keepold and removeold
[22421]309 my $checkdir = ($inexport_mode eq "import") ? "archives" : "export";
310
[22413]311 my ($removeold, $keepold, $incremental, $incremental_mode)
312 = &scriptutil::check_removeold_and_keepold($self->{'removeold'}, $self->{'keepold'},
[22421]313 $self->{'incremental'}, $checkdir,
[22413]314 $collectcfg);
315
316 $self->{'removeold'} = $removeold;
317 $self->{'keepold'} = $keepold;
318 $self->{'incremental'} = $incremental;
319 $self->{'incremental_mode'} = $incremental_mode;
320}
321
322sub process_files
323{
324 my $self = shift @_;
[22421]325 my ($config_filename,$collectcfg) = @_;
[22413]326
[22421]327 my $inexport_mode = $self->{'mode'};
328
[22413]329 my $verbosity = $self->{'verbosity'};
330 my $debug = $self->{'debug'};
331
332 my $importdir = $self->{'importdir'};
[22460]333 my $archivedir = $self->{'archivedir'} || $self->{'exportdir'};
[22413]334
335 my $incremental = $self->{'incremental'};
336 my $incremental_mode = $self->{'incremental_mode'};
337
338 my $removeold = $self->{'removeold'};
339 my $keepold = $self->{'keepold'};
340
341 my $saveas = $self->{'saveas'};
342 my $OIDtype = $self->{'OIDtype'};
343 my $OIDmetadata = $self->{'OIDmetadata'};
344
345 my $out = $self->{'out'};
346 my $faillog = $self->{'faillog'};
347
348 my $maxdocs = $self->{'maxdocs'};
349 my $gzip = $self->{'gzip'};
350 my $groupsize = $self->{'groupsize'};
351 my $sortmeta = $self->{'sortmeta'};
352
353 my $removeprefix = $self->{'removeprefix'};
354 my $removesuffix = $self->{'removesuffix'};
355
[22421]356 my $gli = $self->{'gli'};
[22413]357
[22445]358 my $jobs = $self->{'jobs'};
359 my $epoch = $self->{'epoch'};
360
[22421]361 # related to export
362 my $xsltfile = $self->{'xsltfile'};
363 my $group_marc = $self->{'group_marc'};
364 my $mapping_file = $self->{'mapping_file'};
365 my $xslt_mets = $self->{'xslt_mets'};
366 my $xslt_txt = $self->{'xslt_txt'};
367 my $fedora_namespace = $self->{'fedora_namespace'};
368
369 if ($inexport_mode eq "import") {
370 print STDERR "<Import>\n" if $gli;
371 }
372 else {
373 print STDERR "<export>\n" if $gli;
374 }
[22413]375
376 my $manifest_lookup = new manifest($collectcfg->{'infodbtype'},$archivedir);
[23053]377 if ($self->{'manifest'} ne "") {
378 print STDERR "parsing manifest\n";
[22413]379 my $manifest_filename = $self->{'manifest'};
380
381 if (!&util::filename_is_absolute($manifest_filename)) {
382 $manifest_filename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, $manifest_filename);
383 }
384
385 $self->{'manifest'} =~ s/[\\\/]+/\//g;
386 $self->{'manifest'} =~ s/\/$//;
387
388 $manifest_lookup->parse($manifest_filename);
389 }
390
391 my $manifest = $self->{'manifest'};
392
393 # load all the plugins
394 my $plugins = [];
395 if (defined $collectcfg->{'plugin'}) {
396 $plugins = $collectcfg->{'plugin'};
397 }
398
[23053]399 my $plugin_incr_mode = $incremental_mode;
400 if ($manifest ne "") {
401 # if we have a manifest file, then we pretend we are fully incremental for plugins
402 $plugin_incr_mode = "all";
403 }
[22413]404 #some global options for the plugins
405 my @global_opts = ();
406
[23053]407 my $pluginfo = &plugin::load_plugins ($plugins, $verbosity, $out, $faillog, \@global_opts, $plugin_incr_mode);
[22413]408 if (scalar(@$pluginfo) == 0) {
409 &gsprintf($out, "{import.no_plugins_loaded}\n");
410 die "\n";
411 }
412
413 # remove the old contents of the archives directory (and tmp directory) if needed
414 if ($removeold) {
415 if (-e $archivedir) {
416 &gsprintf($out, "{import.removing_archives}\n");
417 &util::rm_r ($archivedir);
418 }
419 my $tmpdir = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "tmp");
420 $tmpdir =~ s/[\\\/]+/\//g;
421 $tmpdir =~ s/\/$//;
422 if (-e $tmpdir) {
423 &gsprintf($out, "{import.removing_tmpdir}\n");
424 &util::rm_r ($tmpdir);
425 }
426 }
427
428 # create the archives dir if needed
429 &util::mk_all_dir($archivedir);
430
431 # read the archive information file
432
433 # BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files (won't do anything for other infodbtypes)
434 &util::rename_ldb_or_bdb_file(&util::filename_cat($archivedir, "archiveinf-doc"));
435 &util::rename_ldb_or_bdb_file(&util::filename_cat($archivedir, "archiveinf-src"));
436
437 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-doc", $archivedir);
438 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($collectcfg->{'infodbtype'}, "archiveinf-src", $archivedir);
439
440 my $archive_info = new arcinfo ($collectcfg->{'infodbtype'});
441 $archive_info->load_info ($arcinfo_doc_filename);
442
443 if ($manifest eq "") {
444 # Load in list of files in import folder from last import (if present)
445 $archive_info->load_prev_import_filelist ($arcinfo_src_filename);
446 }
447
448 ####Use Plugout####
[22421]449 my $plugout;
450
451 if ($inexport_mode eq "import") {
452 if (defined $collectcfg->{'plugout'}) {
453 # If a plugout was specified in the collect.cfg file, assume it is sensible
454 # We can't check the name because it could be anything, if it is a custom plugout
455 $plugout = $collectcfg->{'plugout'};
456 }
457 else{
458 if ($saveas !~ /^(GreenstoneXML|GreenstoneMETS)$/) {
459 push @$plugout,"GreenstoneXMLPlugout";
460 }
461 else{
462 push @$plugout,$saveas."Plugout";
463 }
464 }
[22413]465 }
[22421]466 else {
467 if (defined $collectcfg->{'plugout'} && $collectcfg->{'plugout'} =~ /^(.*METS|DSpace|MARCXML)Plugout/) {
468 $plugout = $collectcfg->{'plugout'};
[22413]469 }
470 else{
[22421]471 if ($saveas !~ /^(GreenstoneMETS|FedoraMETS|DSpace|MARCXML)$/) {
472 push @$plugout,"GreenstoneMETSPlugout";
473 }
474 else{
475 push @$plugout,$saveas."Plugout";
476 }
[22413]477 }
478 }
[22421]479
480 my $plugout_name = $plugout->[0];
[22413]481
[22421]482 push @$plugout,("-output_info",$archive_info) if (defined $archive_info);
483 push @$plugout,("-verbosity",$verbosity) if (defined $verbosity);
484 push @$plugout,("-debug") if ($debug);
485 push @$plugout,("-group_size",$groupsize) if (defined $groupsize);
486 push @$plugout,("-gzip_output") if ($gzip);
487 push @$plugout,("-output_handle",$out) if (defined $out);
488
489 push @$plugout,("-xslt_file",$xsltfile) if (defined $xsltfile && $xsltfile ne "");
490
491 if ($plugout_name =~ m/^MARCXMLPlugout$/) {
492 push @$plugout,("-group") if ($group_marc);
493 push @$plugout,("-mapping_file",$mapping_file) if (defined $mapping_file && $mapping_file ne "");
494 }
495 if ($plugout_name =~ m/^.*METSPlugout$/) {
496 push @$plugout,("-xslt_mets",$xslt_mets) if (defined $xslt_mets && $xslt_mets ne "");
497 push @$plugout,("-xslt_txt",$xslt_txt) if (defined $xslt_txt && $xslt_txt ne "");
498 }
499
500 if ($plugout_name eq "FedoraMETSPlugout") {
501 push @$plugout,("-fedora_namespace",$fedora_namespace) if (defined $fedora_namespace && $fedora_namespace ne "");
502 }
503
504
[22413]505 my $processor = &plugout::load_plugout($plugout);
506 $processor->setoutputdir ($archivedir);
507 $processor->set_sortmeta ($sortmeta, $removeprefix, $removesuffix) if defined $sortmeta;
508 $processor->set_OIDtype ($OIDtype, $OIDmetadata);
509
510 &plugin::begin($pluginfo, $importdir, $processor, $maxdocs, $gli);
511
512 if ($removeold) {
513 # occasionally, plugins may want to do something on remove old, eg pharos image indexing
514 &plugin::remove_all($pluginfo, $importdir, $processor, $maxdocs, $gli);
515 }
516
[23053]517 # process the import directory
518 my $block_hash = {};
519 $block_hash->{'new_files'} = {};
520 $block_hash->{'reindex_files'} = {};
521 my $metadata = {};
522
523 # gobal blocking pass may set up some metadata
524 &plugin::file_block_read($pluginfo, $importdir, "", $block_hash, $metadata, $gli);
525
526 if ($manifest ne "") {
527 #
528 # 1. Process delete files first
529 #
530
531 my @deleted_files = keys %{$manifest_lookup->{'delete'}};
532 my @full_deleted_files = ();
533
534 # ensure all filenames are absolute
535 foreach my $df (@deleted_files) {
536 #print STDERR "**delete file $df\n";
537 my $full_df =
538 (&util::filename_is_absolute($df))
539 ? $df
540 : &util::filename_cat($importdir,$df);
541
542 if (-d $full_df) {
543 &add_dir_contents_to_list($full_df, \@full_deleted_files);
544 } else {
545 push(@full_deleted_files,$full_df);
546 }
547 }
548
549 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@full_deleted_files);
550 mark_docs_for_deletion($archive_info,{},
551 \@full_deleted_files,
552 $archivedir, $verbosity, "delete");
553
554
555 #
556 # 2. Now files for reindexing
557 #
558
559 my @reindex_files = keys %{$manifest_lookup->{'reindex'}};
560 my @full_reindex_files = ();
561
562 # ensure all filenames are absolute
563 foreach my $rf (@reindex_files) {
564 my $full_rf =
565 (&util::filename_is_absolute($rf))
566 ? $rf
567 : &util::filename_cat($importdir,$rf);
568
569 if (-d $full_rf) {
570 &add_dir_contents_to_list($full_rf, \@full_reindex_files);
571 } else {
572 push(@full_reindex_files,$full_rf);
573 }
574 }
575
576 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@full_reindex_files);
577 mark_docs_for_deletion($archive_info,{},\@full_reindex_files, $archivedir,$verbosity, "reindex");
578
579 # And now to ensure the new version of the file processed by
580 # appropriate plugin, we need to add it to block_hash reindex list
581 foreach my $full_rf (@full_reindex_files) {
582 #print STDERR "***reindex file $full_rf\n";
583 $block_hash->{'reindex_files'}->{$full_rf} = 1;
584 }
585
586
587 #
588 # 3. Now finally any new files - add to block_hash new_files list
589 #
590
591 my @new_files = keys %{$manifest_lookup->{'index'}};
592 my @full_new_files = ();
593
594 foreach my $nf (@new_files) {
595 #print STDERR "***index file $nf\n";
596 # ensure filename is absolute
597 my $full_nf =
598 (&util::filename_is_absolute($nf))
599 ? $nf
600 : &util::filename_cat($importdir,$nf);
601
602 if (-d $full_nf) {
603 &add_dir_contents_to_list($full_nf, \@full_new_files);
604 } else {
605 push(@full_new_files,$full_nf);
606 }
607 }
608
609 foreach my $f (@full_new_files) {
610 $block_hash->{'new_files'}->{$f} = 1;
611 }
612 }
613 else {
614 # if incremental, we read through the import folder to see whats changed.
615
[22413]616 if ($incremental || $incremental_mode eq "onlyadd") {
617 prime_doc_oid_count($archivedir);
618
619 # Can now work out which files were new, already existed, and have
620 # been deleted
621
622 new_vs_old_import_diff($archive_info,$block_hash,$importdir,
623 $archivedir,$verbosity,$incremental_mode);
624
625 my @new_files = sort keys %{$block_hash->{'new_files'}};
626 if (scalar(@new_files>0)) {
627 print STDERR "New files and modified metadata files since last import:\n ";
628 print STDERR join("\n ",@new_files), "\n";
629 }
630
631 if ($incremental) {
632 # only look for deletions if we are truely incremental
633 my @deleted_files = sort keys %{$block_hash->{'deleted_files'}};
634 # Filter out any in gsdl/tmp area
635 my @filtered_deleted_files = ();
636 my $gsdl_tmp_area = &util::filename_cat($ENV{'GSDLHOME'}, "tmp");
637 my $collect_tmp_area = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
638 $gsdl_tmp_area = &util::filename_to_regex($gsdl_tmp_area);
639 $collect_tmp_area = &util::filename_to_regex($collect_tmp_area);
640
641 foreach my $df (@deleted_files) {
642 next if ($df =~ m/^$gsdl_tmp_area/);
643 next if ($df =~ m/^$collect_tmp_area/);
644
645 push(@filtered_deleted_files,$df);
646 }
647
648
649 @deleted_files = @filtered_deleted_files;
650
651 if (scalar(@deleted_files)>0) {
652 print STDERR "Files deleted since last import:\n ";
653 print STDERR join("\n ",@deleted_files), "\n";
654
655
656 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@deleted_files);
657
658 mark_docs_for_deletion($archive_info,$block_hash,\@deleted_files, $archivedir,$verbosity, "delete");
659 }
660
661 my @reindex_files = sort keys %{$block_hash->{'reindex_files'}};
662
663 if (scalar(@reindex_files)>0) {
664 print STDERR "Files to reindex since last import:\n ";
665 print STDERR join("\n ",@reindex_files), "\n";
666 &plugin::remove_some($pluginfo, $collectcfg->{'infodbtype'}, $archivedir, \@reindex_files);
667 mark_docs_for_deletion($archive_info,$block_hash,\@reindex_files, $archivedir,$verbosity, "reindex");
668 }
669
[23053]670 }
[22413]671 }
672 }
[23053]673
674 # now, whichever mode we are in, we can process the entire import folder
675 if ((defined $jobs) && ($jobs > 1))
676 {
677 # if jobs are set to >1, run in parallel using MPI helper
678 # [hs, 1 july 2010]
679 &ParallelInexport::farm_out_processes($jobs, $epoch, $importdir, $block_hash,
680 $self->{'collection'}, $self->{'site'});
681 }
[22413]682 else
683 {
[23053]684 &plugin::read ($pluginfo, $importdir, "", $block_hash, $metadata, $processor, $maxdocs, 0, $gli);
[22413]685 }
[23053]686
687
[22421]688 if ($saveas eq "FedoraMETS") {
689 # create collection "doc obj" for Fedora that contains
690 # collection-level metadata
691
692 my $doc_obj = new doc($config_filename,"nonindexed_doc","none");
693 $doc_obj->set_OID("collection");
694
695 my $col_name = undef;
696 my $col_meta = $collectcfg->{'collectionmeta'};
697
698 if (defined $col_meta) {
699 store_collectionmeta($col_meta,"collectionname",$doc_obj); # in GS3 this is a collection's name
700 store_collectionmeta($col_meta,"collectionextra",$doc_obj); # in GS3 this is a collection's description
701 }
702 $processor->process($doc_obj);
703 }
704
[22413]705 &plugin::end($pluginfo, $processor);
706
707 &plugin::deinit($pluginfo, $processor);
708
709 # Store the value of OIDCount (used in doc.pm) so it can be
710 # restored correctly to this value on an incremental build
711 store_doc_oid_count($archivedir);
712
713 # write out the archive information file
[22464]714 $processor->close_file_output() if (defined $groupsize) && ($groupsize > 1);
[22413]715 $processor->close_group_output() if $processor->is_group();
716
717 # for backwards compatability with archvies.inf file
718 if ($arcinfo_doc_filename =~ m/(contents)|(\.inf)$/) {
719 $archive_info->save_info($arcinfo_doc_filename);
720 }
721 else {
722 $archive_info->save_revinfo_db($arcinfo_src_filename);
723 }
724
725 return $pluginfo;
726}
727
728
729sub generate_statistics
730{
731 my $self = shift @_;
[22421]732 my ($pluginfo) = @_;
[22413]733
[22421]734 my $inexport_mode = $self->{'mode'};
735
[22445]736 my $statsfile = $self->{'statsfile'};
737 my $out = $self->{'out'};
[22413]738 my $faillogname = $self->{'faillogname'};
[22445]739 my $gli = $self->{'gli'};
740 my $jobs = $self->{'jobs'};
[22413]741
742 # write out import stats
743
[22445]744 if ((!defined $jobs) || ($jobs == 1))
745 {
746 # only output statistics if there are multiple jobs
747 # [hs, 1 july 2010]
[22413]748
[22445]749 my $close_stats = 0;
750 if ($statsfile !~ /^(STDERR|STDOUT)$/i) {
751 if (open (STATS, ">$statsfile")) {
[23042]752 $statsfile = 'inexport::STATS';
[22445]753 $close_stats = 1;
754 } else {
755 &gsprintf($out, "{import.cannot_open_stats_file}", $statsfile);
756 &gsprintf($out, "{import.stats_backup}\n");
757 $statsfile = 'STDERR';
758 }
759 }
760
761 &gsprintf($out, "\n");
762 &gsprintf($out, "*********************************************\n");
763 &gsprintf($out, "{$inexport_mode.complete}\n");
764 &gsprintf($out, "*********************************************\n");
765
766 &plugin::write_stats($pluginfo, $statsfile, $faillogname, $gli);
767 if ($close_stats) {
768 close STATS;
769 }
[22413]770 }
771
772 close OUT if $self->{'close_out'};
773 close FAILLOG;
774}
775
776
[22464]777sub store_collectionmeta
778{
779 my ($collectionmeta,$field,$doc_obj) = @_;
780
781 my $section = $doc_obj->get_top_section();
782
783 my $field_hash = $collectionmeta->{$field};
784
785 foreach my $k (keys %$field_hash)
786 {
787 my $val = $field_hash->{$k};
788
789 ### print STDERR "*** $k = $field_hash->{$k}\n";
790
791 my $md_label = "ex.$field";
792
793
794 if ($k =~ m/^\[l=(.*?)\]$/)
795 {
796
797 my $md_suffix = $1;
798 $md_label .= "^$md_suffix";
799 }
800
801
802 $doc_obj->add_utf8_metadata($section,$md_label, $val);
803
804 # see collConfigxml.pm: GS2's "collectionextra" is called "description" in GS3,
805 # while "collectionname" in GS2 is called "name" in GS3.
806 # Variable $nameMap variable in collConfigxml.pm maps between GS2 and GS3
807 if (($md_label eq "ex.collectionname^en") || ($md_label eq "ex.collectionname"))
808 {
809 $doc_obj->add_utf8_metadata($section,"dc.Title", $val);
810 }
811
812 }
813}
[22413]814
815
[21306]816sub oid_count_file {
817 my ($archivedir) = @_;
818 return &util::filename_cat ($archivedir, "OIDcount");
819}
820
821
[18528]822sub prime_doc_oid_count
823{
824 my ($archivedir) = @_;
[21306]825 my $oid_count_filename = &oid_count_file($archivedir);
[18528]826
827 if (-e $oid_count_filename) {
828 if (open(OIDIN,"<$oid_count_filename")) {
829 my $OIDcount = <OIDIN>;
830 chomp $OIDcount;
831 close(OIDIN);
832
833 $doc::OIDcount = $OIDcount;
834 }
835 else {
836
837 print STDERR "Warning: unable to read document OID count from $oid_count_filename\n";
838 print STDERR "Setting value to 0\n";
839 }
840 }
841
842}
843
844sub store_doc_oid_count
845{
846 # Use the file "OIDcount" in the archives directory to record
847 # what value doc.pm got up to
848
849 my ($archivedir) = @_;
[21306]850 my $oid_count_filename = &oid_count_file($archivedir);
[18528]851
852
853 if (open(OIDOUT,">$oid_count_filename")) {
854 print OIDOUT $doc::OIDcount, "\n";
855
856 close(OIDOUT);
857 }
858 else {
859 print STDERR "Warning: unable to store document OID count\n";
860 }
861}
862
863
864
[18457]865sub new_vs_old_import_diff
866{
[20578]867 my ($archive_info,$block_hash,$importdir,$archivedir,$verbosity,$incremental_mode) = @_;
[18457]868
[21620]869 # Get the infodbtype value for this collection from the arcinfo object
870 my $infodbtype = $archive_info->{'infodbtype'};
871
[20776]872 # in this method, we want to know if metadata files are modified or not.
[21620]873 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
[20776]874
875 my $archiveinf_timestamp = -M $arcinfo_doc_filename;
876
[18457]877 # First convert all files to absolute form
878 # This is to support the situation where the import folder is not
879 # the default
880
881 my $prev_all_files = $archive_info->{'prev_import_filelist'};
882 my $full_prev_all_files = {};
883
884 foreach my $prev_file (keys %$prev_all_files) {
885
886 if (!&util::filename_is_absolute($prev_file)) {
887 my $full_prev_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$prev_file);
888 $full_prev_all_files->{$full_prev_file} = $prev_file;
889 }
890 else {
891 $full_prev_all_files->{$prev_file} = $prev_file;
892 }
893 }
894
[18469]895
[18457]896 # Figure out which are the new files, existing files and so
897 # by implication the files from the previous import that are not
898 # there any more => mark them for deletion
899 foreach my $curr_file (keys %{$block_hash->{'all_files'}}) {
900
901 my $full_curr_file = $curr_file;
902
903 # entry in 'all_files' is moved to either 'existing_files',
[20776]904 # 'deleted_files', 'new_files', or 'new_or_modified_metadata_files'
[18457]905
906 if (!&util::filename_is_absolute($curr_file)) {
907 # add in import dir to make absolute
908 $full_curr_file = &util::filename_cat($importdir,$curr_file);
909 }
910
[19498]911 # figure out if new file or not
[18457]912 if (defined $full_prev_all_files->{$full_curr_file}) {
[20776]913 # delete it so that only files that need deleting are left
914 delete $full_prev_all_files->{$full_curr_file};
915
916 # had it before. is it a metadata file?
917 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
[20578]918
[20776]919 # is it modified??
920 if (-M $full_curr_file < $archiveinf_timestamp) {
921 print STDERR "*** Detected a modified metadata file: $full_curr_file\n" if $verbosity > 2;
922 # its newer than last build
923 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
924 }
[20578]925 }
926 else {
[20776]927 if ($incremental_mode eq "all") {
928
929 # had it before
930 $block_hash->{'existing_files'}->{$full_curr_file} = 1;
931
932 }
933 else {
934 # Warning in "onlyadd" mode, but had it before!
935 print STDERR "Warning: File $full_curr_file previously imported.\n";
936 print STDERR " Treating as new file\n";
937
938 $block_hash->{'new_files'}->{$full_curr_file} = 1;
939
940 }
[20578]941 }
942 }
943 else {
944 if ($block_hash->{'metadata_files'}->{$full_curr_file}) {
945 # the new file is the special sort of file greenstone uses
946 # to attach metadata to src documents
947 # i.e metadata.xml
948 # (but note, the filename used is not constrained in
949 # Greenstone to always be this)
[18457]950
[20776]951 print STDERR "***** Detected new metadata file: $full_curr_file\n" if $verbosity > 2;
952 $block_hash->{'new_or_modified_metadata_files'}->{$full_curr_file} = 1;
[20578]953 }
954 else {
955 $block_hash->{'new_files'}->{$full_curr_file} = 1;
956 }
[18457]957 }
[20578]958
[18457]959
960 delete $block_hash->{'all_files'}->{$curr_file};
961 }
962
[20578]963
[21306]964
965
[20776]966 # Deal with complication of new or modified metadata files by forcing
[20578]967 # everything from this point down in the file hierarchy to
968 # be freshly imported.
969 #
970 # This may mean files that have not changed are reindexed, but does
971 # guarantee by the end of processing all new metadata is correctly
972 # associated with the relevant document(s).
973
[20776]974 foreach my $new_mdf (keys %{$block_hash->{'new_or_modified_metadata_files'}}) {
[20578]975 my ($fileroot,$situated_dir,$ext) = fileparse($new_mdf, "\\.[^\\.]+\$");
976
977 $situated_dir =~ s/[\\\/]+$//; # remove tailing slashes
[20769]978 $situated_dir =~ s/\\/\\\\/g; # need to protect windows slash \ in regular expression
979
[20578]980 # Go through existing_files, and mark anything that is contained
981 # within 'situated_dir' to be reindexed (in case some of the metadata
982 # attaches to one of these files)
983
984 my $reindex_files = [];
985
986 foreach my $existing_f (keys %{$block_hash->{'existing_files'}}) {
[20769]987
[20578]988 if ($existing_f =~ m/^$situated_dir/) {
989 push(@$reindex_files,$existing_f);
990 $block_hash->{'reindex_files'}->{$existing_f} = 1;
[21306]991 delete $block_hash->{'existing_files'}->{$existing_f};
[20578]992
993 }
994 }
995
996 # metadata file needs to be in new_files list so parsed by MetadataXMLPlug
997 # (or equivalent)
998 $block_hash->{'new_files'}->{$new_mdf} = 1;
999
1000 }
1001
[21306]1002 # go through remaining existing files and work out what has changed and needs to be reindexed.
1003 my @existing_files = sort keys %{$block_hash->{'existing_files'}};
1004
1005 my $reindex_files = [];
1006
1007 foreach my $existing_filename (@existing_files) {
1008 if (-M $existing_filename < $archiveinf_timestamp) {
1009 # file is newer than last build
1010
1011 my $existing_file = $existing_filename;
1012 #my $collectdir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'});
1013
1014 #my $collectdir_resafe = &util::filename_to_regex($collectdir);
1015 #$existing_file =~ s/^$collectdir_resafe(\\|\/)?//;
1016
1017 print STDERR "**** Reindexing existing file: $existing_file\n";
1018
1019 push(@$reindex_files,$existing_file);
1020 $block_hash->{'reindex_files'}->{$existing_filename} = 1;
1021 }
1022
1023 }
1024
[20578]1025
[18469]1026 # By this point full_prev_all_files contains the files
1027 # mentioned in archiveinf-src.db but are not in the 'import'
1028 # folder (or whatever was specified through -importdir ...)
1029
1030 # This list can contain files that were created in the 'tmp' or
1031 # 'cache' areas (such as screen-size and thumbnail images).
[18457]1032 #
[18469]1033 # In building the final list of files to delete, we test to see if
[20578]1034 # it exists on the filesystem and if it does (unusual for a "normal"
1035 # file in import, but possible in the case of 'tmp' files),
1036 # supress it from going into the final list
[18469]1037
1038 my $collectdir = $ENV{'GSDLCOLLECTDIR'};
1039
[18457]1040 my @deleted_files = values %$full_prev_all_files;
[18469]1041 map { my $curr_file = $_;
1042 my $full_curr_file = $curr_file;
1043
1044 if (!&util::filename_is_absolute($curr_file)) {
1045 # add in import dir to make absolute
1046
1047 $full_curr_file = &util::filename_cat($collectdir,$curr_file);
1048 }
1049
1050
1051 if (!-e $full_curr_file) {
1052 $block_hash->{'deleted_files'}->{$curr_file} = 1;
1053 }
1054 } @deleted_files;
[20578]1055
1056
1057
[18457]1058}
1059
[19498]1060
[20788]1061# this is used to delete "deleted" docs, and to remove old versions of "changed" docs
[21306]1062# $mode is 'delete' or 'reindex'
1063sub mark_docs_for_deletion
[18457]1064{
[21306]1065 my ($archive_info,$block_hash,$deleted_files,$archivedir,$verbosity,$mode) = @_;
[18457]1066
[21306]1067 my $mode_text = "deleted from index";
1068 if ($mode eq "reindex") {
1069 $mode_text = "reindexed";
1070 }
[18457]1071
[21620]1072 # Get the infodbtype value for this collection from the arcinfo object
1073 my $infodbtype = $archive_info->{'infodbtype'};
[18457]1074
[21620]1075 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archivedir);
1076 my $arcinfo_src_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-src", $archivedir);
1077
[22010]1078
[18457]1079 # record files marked for deletion in arcinfo
[19498]1080 foreach my $file (@$deleted_files) {
[21564]1081 # use 'archiveinf-src' info database file to look up all the OIDs
[19789]1082 # that this file is used in (note in most cases, it's just one OID)
[18457]1083
[21620]1084 my $src_rec_string = &dbutil::read_infodb_entry($infodbtype, $arcinfo_src_filename, $file);
[21554]1085 my $src_rec = &dbutil::convert_infodb_string_to_hash($src_rec_string);
[18457]1086 my $oids = $src_rec->{'oid'};
[20776]1087 my $file_record_deleted = 0;
[20788]1088
1089 # delete the src record
[22010]1090 my $src_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_src_filename, "append");
[21620]1091 &dbutil::delete_infodb_entry($infodbtype, $src_infodb_file_handle, $file);
[22010]1092 &dbutil::close_infodb_write_handle($infodbtype, $src_infodb_file_handle);
1093
1094
[18457]1095 foreach my $oid (@$oids) {
1096
[20788]1097 # find the source doc (the primary file that becomes this oid)
[21620]1098 my $doc_rec_string = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $oid);
[21554]1099 my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);
[20776]1100 my $doc_source_file = $doc_rec->{'src-file'}->[0];
1101 if (!&util::filename_is_absolute($doc_source_file)) {
1102 $doc_source_file = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},$doc_source_file);
1103 }
[18457]1104
[20788]1105 if ($doc_source_file ne $file) {
1106 # its an associated or metadata file
1107
[20776]1108 # mark source doc for reimport as one of its assoc files has changed or deleted
1109 $block_hash->{'reindex_files'}->{$doc_source_file} = 1;
[20788]1110
[18457]1111 }
[20788]1112 my $curr_status = $archive_info->get_status_info($oid);
1113 if (defined($curr_status) && (($curr_status ne "D"))) {
1114 if ($verbosity>1) {
1115 print STDERR "$oid ($doc_source_file) marked to be $mode_text on next buildcol.pl\n";
[19498]1116 }
[20788]1117 # mark oid for deletion (it will be deleted or reimported)
1118 $archive_info->set_status_info($oid,"D");
[21620]1119 my $val = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $oid);
[20788]1120 $val =~ s/^<index-status>(.*)$/<index-status>D/m;
[21557]1121
1122 my $val_rec = &dbutil::convert_infodb_string_to_hash($val);
[22010]1123 my $doc_infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $arcinfo_doc_filename, "append");
1124
[21620]1125 &dbutil::write_infodb_entry($infodbtype, $doc_infodb_file_handle, $oid, $val_rec);
[22010]1126 &dbutil::close_infodb_write_handle($infodbtype, $doc_infodb_file_handle);
[19498]1127 }
[18457]1128 }
[22327]1129
[18457]1130 }
[22567]1131 # now go through and check that we haven't marked any primary files for reindex (because their associated files have changed/deleted) when they have been deleted themselves. only in delete mode.
1132 if ($mode eq "delete") {
1133 foreach my $file (@$deleted_files) {
1134 if (defined $block_hash->{'reindex_files'}->{$file}) {
1135 delete $block_hash->{'reindex_files'}->{$file};
1136 }
[22327]1137 }
1138 }
[21560]1139
[22010]1140
[18457]1141}
1142
[23053]1143sub add_dir_contents_to_list {
[18457]1144
[23053]1145 my ($dirname, $list) = @_;
1146
1147 # Recur over directory contents.
1148 my (@dir, $subfile);
1149
1150 # find all the files in the directory
1151 if (!opendir (DIR, $dirname)) {
1152 print STDERR "inexport: WARNING - couldn't read directory $dirname\n";
1153 return -1; # error in processing
1154 }
1155 @dir = readdir (DIR);
1156 closedir (DIR);
1157
1158 for (my $i = 0; $i < scalar(@dir); $i++) {
1159 my $subfile = $dir[$i];
1160 next if ($subfile =~ m/^\.\.?$/);
1161 my $full_file = &util::filename_cat($dirname, $subfile);
1162 if (-d $full_file) {
1163 &add_dir_contents_to_list($full_file, $list);
1164 } else {
1165 push (@$list, $full_file);
1166 }
1167 }
1168
1169}
[18554]1170
[23053]1171
[18457]11721;
Note: See TracBrowser for help on using the repository browser.