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

Last change on this file since 22466 was 22466, checked in by ak19, 14 years ago

Set archivedir to "" in the first instance

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