source: gs2-extensions/parallel-building/trunk/src/perllib/plugouts/BasePlugout.pm@ 27377

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

Updating calls to intermediate util functions to the new FileUtils functions

File size: 33.2 KB
Line 
1###########################################################################
2#
3# BasePlugout.pm -- base class for all the plugout modules
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) 2006 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 BasePlugout;
27
28eval {require bytes};
29
30use strict;
31no strict 'subs';
32no strict 'refs';
33
34use dbutil;
35use gsprintf 'gsprintf';
36use printusage;
37use parse2;
38use util;
39use FileUtils;
40
41
42# suppress the annoying "subroutine redefined" warning that various
43# gets cause under perl 5.6
44$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
45
46my $arguments = [
47 { 'name' => "group_size",
48 'desc' => "{BasPlugout.group_size}",
49 'type' => "int",
50 'deft' => "1",
51 'reqd' => "no",
52 'hiddengli' => "no"},
53 { 'name' => "output_info",
54 'desc' => "{BasPlugout.output_info}",
55 'type' => "string",
56 'reqd' => "yes",
57 'hiddengli' => "yes"},
58 { 'name' => "xslt_file",
59 'desc' => "{BasPlugout.xslt_file}",
60 'type' => "string",
61 'reqd' => "no",
62 'deft' => "",
63 'hiddengli' => "no"},
64 { 'name' => "output_handle",
65 'desc' => "{BasPlugout.output_handle}",
66 'type' => "string",
67 'deft' => 'STDERR',
68 'reqd' => "no",
69 'hiddengli' => "yes"},
70 { 'name' => "verbosity",
71 'desc' => "{BasPlugout.verbosity}",
72 'type' => "int",
73 'deft' => "0",
74 'reqd' => "no",
75 'hiddengli' => "no"},
76 { 'name' => "gzip_output",
77 'desc' => "{BasPlugout.gzip_output}",
78 'type' => "flag",
79 'reqd' => "no",
80 'hiddengli' => "no"},
81 { 'name' => "debug",
82 'desc' => "{BasPlugout.debug}",
83 'type' => "flag",
84 'reqd' => "no",
85 'hiddengli' => "yes"}
86];
87
88my $options = { 'name' => "BasePlugout",
89 'desc' => "{BasPlugout.desc}",
90 'abstract' => "yes",
91 'inherits' => "no",
92 'args' => $arguments};
93
94sub new
95{
96 my $class = shift (@_);
97
98 my ($plugoutlist,$args,$hashArgOptLists) = @_;
99 push(@$plugoutlist, $class);
100
101 my $plugout_name = (defined $plugoutlist->[0]) ? $plugoutlist->[0] : $class;
102
103 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
104 push(@{$hashArgOptLists->{"OptList"}},$options);
105
106 my $self = {};
107 $self->{'plugout_type'} = $class;
108 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
109 $self->{"info_only"} = 0;
110
111 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
112 # the args, just return the object.
113 foreach my $strArg (@{$args})
114 {
115 if(defined $strArg && $strArg eq "-gsdlinfo")
116 {
117 $self->{"info_only"} = 1;
118 return bless $self, $class;
119 }
120 }
121
122 delete $self->{"info_only"};
123
124 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
125 {
126 my $classTempClass = bless $self, $class;
127 print STDERR "<BadPlugout d=$plugout_name>\n";
128 &gsprintf(STDERR, "\n{BasPlugout.bad_general_option}\n", $plugout_name);
129 $classTempClass->print_txt_usage(""); # Use default resource bundle
130 die "\n";
131 }
132
133
134 if(defined $self->{'xslt_file'} && $self->{'xslt_file'} ne "")
135 {
136 my $full_file_path = &util::locate_config_file($self->{'xslt_file'});
137 if (!defined $full_file_path) {
138 print STDERR "Can not find $self->{'xslt_file'}, please make sure you have supplied the correct file path\n";
139 die "\n";
140 }
141 $self->{'xslt_file'} = $full_file_path;
142 }
143
144 $self->{'gs_count'} = 0;
145
146 $self->{'keep_import_structure'} = 0;
147
148 return bless $self, $class;
149
150}
151
152sub print_xml_usage
153{
154 my $self = shift(@_);
155 my $header = shift(@_);
156 my $high_level_information_only = shift(@_);
157
158 # XML output is always in UTF-8
159 gsprintf::output_strings_in_UTF8;
160
161 if ($header) {
162 &PrintUsage::print_xml_header("plugout");
163 }
164 $self->print_xml($high_level_information_only);
165}
166
167
168sub print_xml
169{
170 my $self = shift(@_);
171 my $high_level_information_only = shift(@_);
172
173 my $optionlistref = $self->{'option_list'};
174 my @optionlist = @$optionlistref;
175 my $plugoutoptions = shift(@$optionlistref);
176 return if (!defined($plugoutoptions));
177
178 gsprintf(STDERR, "<PlugoutInfo>\n");
179 gsprintf(STDERR, " <Name>$plugoutoptions->{'name'}</Name>\n");
180 my $desc = gsprintf::lookup_string($plugoutoptions->{'desc'});
181 $desc =~ s/</&amp;lt;/g; # doubly escaped
182 $desc =~ s/>/&amp;gt;/g;
183 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
184 gsprintf(STDERR, " <Abstract>$plugoutoptions->{'abstract'}</Abstract>\n");
185 gsprintf(STDERR, " <Inherits>$plugoutoptions->{'inherits'}</Inherits>\n");
186 unless (defined($high_level_information_only)) {
187 gsprintf(STDERR, " <Arguments>\n");
188 if (defined($plugoutoptions->{'args'})) {
189 &PrintUsage::print_options_xml($plugoutoptions->{'args'});
190 }
191 gsprintf(STDERR, " </Arguments>\n");
192
193 # Recurse up the plugout hierarchy
194 $self->print_xml();
195 }
196 gsprintf(STDERR, "</PlugoutInfo>\n");
197}
198
199
200sub print_txt_usage
201{
202 my $self = shift(@_);
203
204 # Print the usage message for a plugout (recursively)
205 my $descoffset = $self->determine_description_offset(0);
206 $self->print_plugout_usage($descoffset, 1);
207}
208
209sub determine_description_offset
210{
211 my $self = shift(@_);
212 my $maxoffset = shift(@_);
213
214 my $optionlistref = $self->{'option_list'};
215 my @optionlist = @$optionlistref;
216 my $plugoutoptions = pop(@$optionlistref);
217 return $maxoffset if (!defined($plugoutoptions));
218
219 # Find the length of the longest option string of this download
220 my $plugoutargs = $plugoutoptions->{'args'};
221 if (defined($plugoutargs)) {
222 my $longest = &PrintUsage::find_longest_option_string($plugoutargs);
223 if ($longest > $maxoffset) {
224 $maxoffset = $longest;
225 }
226 }
227
228 # Recurse up the download hierarchy
229 $maxoffset = $self->determine_description_offset($maxoffset);
230 $self->{'option_list'} = \@optionlist;
231 return $maxoffset;
232}
233
234
235sub print_plugout_usage
236{
237 my $self = shift(@_);
238 my $descoffset = shift(@_);
239 my $isleafclass = shift(@_);
240
241 my $optionlistref = $self->{'option_list'};
242 my @optionlist = @$optionlistref;
243 my $plugoutoptions = shift(@$optionlistref);
244 return if (!defined($plugoutoptions));
245
246 my $plugoutname = $plugoutoptions->{'name'};
247 my $plugoutargs = $plugoutoptions->{'args'};
248 my $plugoutdesc = $plugoutoptions->{'desc'};
249
250 # Produce the usage information using the data structure above
251 if ($isleafclass) {
252 if (defined($plugoutdesc)) {
253 gsprintf(STDERR, "$plugoutdesc\n\n");
254 }
255 gsprintf(STDERR, " {common.usage}: plugout $plugoutname [{common.options}]\n\n");
256 }
257
258 # Display the download options, if there are some
259 if (defined($plugoutargs)) {
260 # Calculate the column offset of the option descriptions
261 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
262
263 if ($isleafclass) {
264 gsprintf(STDERR, " {common.specific_options}:\n");
265 }
266 else {
267 gsprintf(STDERR, " {common.general_options}:\n", $plugoutname);
268 }
269
270 # Display the download options
271 &PrintUsage::print_options_txt($plugoutargs, $optiondescoffset);
272 }
273
274 # Recurse up the download hierarchy
275 $self->print_plugout_usage($descoffset, 0);
276 $self->{'option_list'} = \@optionlist;
277}
278
279
280sub error
281{
282 my ($strFunctionName,$strError) = @_;
283 {
284 print "Error occoured in BasePlugout.pm\n".
285 "In Function: ".$strFunctionName."\n".
286 "Error Message: ".$strError."\n";
287 exit(-1);
288 }
289}
290
291# OIDtype may be "hash" or "incremental" or "dirname" or "assigned"
292sub set_OIDtype {
293 my $self = shift (@_);
294 my ($type, $metadata) = @_;
295
296 if ($type =~ /^(hash|incremental|dirname|assigned)$/) {
297 $self->{'OIDtype'} = $type;
298 } else {
299 $self->{'OIDtype'} = "hash";
300 }
301 if ($type =~ /^assigned$/) {
302 if (defined $metadata) {
303 $self->{'OIDmetadata'} = $metadata;
304 } else {
305 $self->{'OIDmetadata'} = "dc.Identifier";
306 }
307 }
308}
309
310sub set_output_dir
311{
312 my $self = shift @_;
313 my ($output_dir) = @_;
314
315 $self->{'output_dir'} = $output_dir;
316}
317
318sub setoutputdir
319{
320 my $self = shift @_;
321 my ($output_dir) = @_;
322
323 $self->{'output_dir'} = $output_dir;
324}
325
326sub get_output_dir
327{
328 my $self = shift (@_);
329
330 return $self->{'output_dir'};
331}
332
333sub getoutputdir
334{
335 my $self = shift (@_);
336
337 return $self->{'output_dir'};
338}
339
340sub getoutputinfo
341{
342 my $self = shift (@_);
343
344 return $self->{'output_info'};
345}
346
347
348sub get_output_handler
349{
350 my $self = shift (@_);
351
352 my ($output_file_name) = @_;
353 my $fh;
354 &FileUtils::openFileHandle($output_file_name, '>', \$fh) or die "Can not open a file handler for $output_file_name\n";
355 return $fh;
356}
357
358sub release_output_handler
359{
360 my $self = shift (@_);
361 my ($outhandler) = @_;
362
363 close($outhandler);
364
365}
366
367sub output_xml_header {
368 my $self = shift (@_);
369 my ($handle,$docroot,$nondoctype) = @_;
370
371
372 #print $handle '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
373
374 #For Dspace must be UTF in lower case
375 print $handle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
376
377 if (!defined $nondoctype){
378 my $doctype = (defined $docroot) ? $docroot : "Section";
379
380 # Used to be '<!DOCTYPE Archive SYSTEM ...'
381
382 print $handle "<!DOCTYPE $doctype SYSTEM \"http://greenstone.org/dtd/Archive/1.0/Archive.dtd\">\n";
383 }
384
385 print $handle "<$docroot>\n" if defined $docroot;
386}
387
388sub output_xml_footer {
389 my $self = shift (@_);
390 my ($handle,$docroot) = @_;
391 print $handle "</$docroot>\n" if defined $docroot;
392}
393
394
395sub output_general_xml_header
396{
397 my $self = shift (@_);
398 my ($handle,$docroot,$opt_attributes,$opt_dtd, $opt_doctype) = @_;
399
400 print $handle '<?xml version="1.0" encoding="utf-8" standalone="no"?>' . "\n";
401
402 if (defined $opt_dtd) {
403 my $doctype = (defined $opt_doctype) ? $opt_doctype : $docroot;
404 print $handle "<!DOCTYPE $doctype SYSTEM \"$opt_dtd\">\n";
405 }
406
407 if (defined $docroot) {
408 my $full_docroot = $docroot;
409 if (defined $opt_attributes) {
410 $full_docroot .= " $opt_attributes";
411 }
412
413 print $handle "<$full_docroot>\n"
414 }
415}
416
417sub output_general_xml_footer
418{
419 output_xml_footer(@_);
420}
421
422
423sub process {
424 my $self = shift (@_);
425 my ($doc_obj) = @_;
426
427 # for OAI purposes
428 $doc_obj->set_lastmodified();
429 $doc_obj->set_oailastmodified();
430
431 if ($self->{'group_size'} > 1) {
432 $self->group_process ($doc_obj);
433 return;
434 }
435
436 my $OID = $doc_obj->get_OID();
437 $OID = "NULL" unless defined $OID;
438
439 my $top_section = $doc_obj->get_top_section();
440
441 #get document's directory
442 my $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
443
444 my $output_info = $self->{'output_info'};
445 return if (!defined $output_info);
446
447 ##############################
448 # call subclass' saveas method
449 ##############################
450 $self->saveas($doc_obj,$doc_dir);
451 $self->archiveinf_db($doc_obj,$doc_dir);
452
453}
454
455sub store_output_info_reference {
456 my $self = shift (@_);
457 my ($doc_obj) = @_;
458
459 my $output_info = $self->{'output_info'};
460 my $metaname = $self->{'sortmeta'};
461 if (!defined $metaname || $metaname !~ /\S/) {
462 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, "");
463 return;
464 }
465
466 my $metadata = "";
467 my $top_section = $doc_obj->get_top_section();
468
469 my @commameta_list = split(/,/, $metaname);
470 foreach my $cmn (@commameta_list) {
471 my $meta = $doc_obj->get_metadata_element($top_section, $cmn);
472 if ($meta) {
473 # do remove prefix/suffix - this will apply to all values
474 $meta =~ s/^$self->{'removeprefix'}// if defined $self->{'removeprefix'};
475 $meta =~ s/$self->{'removesuffix'}$// if defined $self->{'removesuffix'};
476 $meta = &sorttools::format_metadata_for_sorting($cmn, $meta, $doc_obj);
477 $metadata .= $meta if ($meta);
478 }
479 }
480
481 # store reference in the output_info
482 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, $metadata);
483
484}
485
486sub group_process {
487
488 my $self = shift (@_);
489 my ($doc_obj) = @_;
490
491 my $OID = $doc_obj->get_OID();
492 $OID = "NULL" unless defined $OID;
493
494 my $groupsize = $self->{'group_size'};
495 my $gs_count = $self->{'gs_count'};
496 my $open_new_file = (($gs_count % $groupsize)==0);
497 my $outhandle = $self->{'output_handle'};
498
499 # opening a new file, or document has assoicated files => directory needed
500 if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0)) {
501
502 # The directory the archive file (doc.xml) and all associated files
503 # should end up in
504 my $doc_dir;
505 # If we've determined its time for a new file, open it now
506 if ($open_new_file || !defined($self->{'gs_doc_dir'}))
507 {
508 $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
509 # only if opening new file
510 my $output_dir = $self->get_output_dir();
511 &FileUtils::makeAllDirectories ($output_dir) unless &FileUtils::directoryExists($output_dir);
512 my $doc_file = &FileUtils::filenameConcatenate ($output_dir, $doc_dir, "doc.xml");
513 my $short_doc_file = &FileUtils::filenameConcatenate ($doc_dir, "doc.xml");
514
515 if ($gs_count>0)
516 {
517 return if (!$self->close_file_output());
518 }
519
520 open (GROUPPROCESS, ">$doc_file") or (print $outhandle "BasePlugout::group_process could not write to file $doc_file\n" and return);
521
522
523 $self->{'gs_filename'} = $doc_file;
524 $self->{'short_doc_file'} = $short_doc_file;
525 $self->{'gs_OID'} = $OID;
526 $self->{'gs_doc_dir'} = $doc_dir;
527
528 $self->output_xml_header('BasePlugout::GROUPPROCESS','Archive');
529 }
530 # Otherwise load the same archive document directory used last time
531 else
532 {
533 $doc_dir = $self->{'gs_doc_dir'};
534 }
535
536 # copy all the associated files, add this information as metadata
537 # to the document
538 print $outhandle "Writing associated files to $doc_dir\n";
539 $self->process_assoc_files ($doc_obj, $doc_dir);
540
541 # look up 'gsdlmetafile' metadata and store that information
542 # explicitly in $doc_obj
543 $self->process_metafiles_metadata ($doc_obj);
544 }
545
546 # save this document
547 my $section_text = &docprint::get_section_xml($doc_obj,$doc_obj->get_top_section());
548 print GROUPPROCESS $section_text;
549
550 $self->{'gs_count'}++;
551}
552
553
554sub saveas {
555 my $self = shift (@_);
556
557 die "Basplug::saveas function must be implemented in sub classes\n";
558}
559
560sub get_doc_dir {
561 my $self = shift (@_);
562 my ($OID, $source_filename) = @_;
563
564 my $working_dir = $self->get_output_dir();
565 my $working_info = $self->{'output_info'};
566 return if (!defined $working_info);
567
568 my $doc_info = $working_info->get_info($OID);
569 my $doc_dir = '';
570
571 if (defined $doc_info && scalar(@$doc_info) >= 1)
572 {
573 # This OID already has an archives directory, so use it again
574 $doc_dir = $doc_info->[0];
575 $doc_dir =~ s/\/?((doc(mets)?)|(dublin_core))\.xml(\.gz)?$//;
576 }
577 elsif ($self->{'keep_import_structure'})
578 {
579 $source_filename = &File::Basename::dirname($source_filename);
580 $source_filename =~ s/[\\\/]+/\//g;
581 $source_filename =~ s/\/$//;
582
583 $doc_dir = substr($source_filename, length($ENV{'GSDLIMPORTDIR'}) + 1);
584 }
585
586 # We have to use a new archives directory for this document
587 if ($doc_dir eq "")
588 {
589 $doc_dir = $self->get_new_doc_dir ($working_info, $working_dir, $OID);
590 }
591
592 if (!defined $self->{'group'} || !$self->{'group'}){
593 &FileUtils::makeAllDirectories(&FileUtils::filenameConcatenate($working_dir, $doc_dir));
594 }
595
596 return $doc_dir;
597}
598
599sub get_new_doc_dir{
600 my $self = shift (@_);
601 my($working_info,$working_dir,$OID) = @_;
602
603
604 my $doc_dir = "";
605 my $doc_dir_rest = $OID;
606
607 # remove any \ and / from the OID
608 $doc_dir_rest =~ s/[\\\/]//g;
609
610 # Remove ":" if we are on Windows OS, as otherwise they get confused with the drive letters
611 $doc_dir_rest =~ s/\://g if ($ENV{'GSDLOS'} =~ /^windows$/i);
612
613 my $doc_dir_num = 0;
614
615 do {
616 $doc_dir .= "/" if $doc_dir_num > 0;
617# if ($doc_dir_rest =~ s/^((HASH)?.{1,3})//i) {
618 if ($doc_dir_rest =~ s/^(.{1,3})//) {
619 $doc_dir .= $1;
620 $doc_dir_num++;
621 }
622 } while ($doc_dir_rest ne "" && ((&FileUtils::directoryExists(&FileUtils::filenameConcatenate($working_dir, "$doc_dir.dir"))) || ($working_info->size() >= 1024 && $doc_dir_num < 2)));
623 my $i = 1;
624 my $doc_dir_base = $doc_dir;
625 #rint "!! - ensure path is unique by adding numeric suffix if necessary\n";
626 while (&FileUtils::directoryExists(&FileUtils::filenameConcatenate($working_dir, "$doc_dir.dir"))) {
627 $doc_dir = "$doc_dir_base-$i";
628 $i++;
629 }
630
631 return "$doc_dir.dir";
632}
633
634sub process_assoc_files {
635 my $self = shift (@_);
636 my ($doc_obj, $doc_dir, $handle) = @_;
637
638 my $outhandle = $self->{'output_handle'};
639
640 my $output_dir = $self->get_output_dir();
641 return if (!defined $output_dir);
642
643 &FileUtils::makeAllDirectories($output_dir) unless &FileUtils::directoryExists($output_dir);
644
645 my $working_dir = &FileUtils::filenameConcatenate($output_dir, $doc_dir);
646 &FileUtils::makeAllDirectories($working_dir) unless &FileUtils::directoryExists($working_dir);
647
648 my @assoc_files = ();
649 my $filename;;
650
651 my $source_filename = $doc_obj->get_source_filename();
652
653 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
654
655 if (defined $collect_dir) {
656 my $dirsep_regexp = &util::get_os_dirsep();
657
658 if ($collect_dir !~ /$dirsep_regexp$/) {
659 $collect_dir .= &util::get_dirsep(); # ensure there is a slash at the end
660 }
661
662 # This test is never going to fail on Windows -- is this a problem?
663
664 if ($source_filename !~ /^$dirsep_regexp/) {
665 $source_filename = &FileUtils::filenameConcatenate($collect_dir, $source_filename);
666 }
667 }
668
669
670 # set the assocfile path (even if we have no assoc files - need this for lucene)
671 $doc_obj->set_utf8_metadata_element ($doc_obj->get_top_section(),
672 "assocfilepath",
673 "$doc_dir");
674 foreach my $assoc_file_rec (@{$doc_obj->get_assoc_files()}) {
675 my ($dir, $afile) = $assoc_file_rec->[1] =~ /^(.*?)([^\/\\]+)$/;
676 $dir = "" unless defined $dir;
677
678 my $utf8_real_filename = $assoc_file_rec->[0];
679
680 # for some reasons the image associate file has / before the full path
681 $utf8_real_filename =~ s/^\\(.*)/$1/i;
682
683## my $real_filename = &util::utf8_to_real_filename($utf8_real_filename);
684 my $real_filename = $utf8_real_filename;
685 $real_filename = &util::downgrade_if_dos_filename($real_filename);
686
687 if (&FileUtils::fileExists($real_filename)) {
688
689 $filename = &FileUtils::filenameConcatenate($working_dir, $afile);
690
691 &FileUtils::hardLink($real_filename, $filename, $self->{'verbosity'});
692
693 $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
694 "gsdlassocfile",
695 "$afile:$assoc_file_rec->[2]:$dir");
696 } elsif ($self->{'verbosity'} > 1) {
697 print $outhandle "BasePlugout::process couldn't copy the associated file " .
698 "$real_filename to $afile\n";
699 }
700 }
701}
702
703
704sub process_metafiles_metadata
705{
706 my $self = shift (@_);
707 my ($doc_obj) = @_;
708
709 my $top_section = $doc_obj->get_top_section();
710 my $metafiles = $doc_obj->get_metadata($top_section,"gsdlmetafile");
711
712 foreach my $metafile_pair (@$metafiles) {
713 my ($full_metafile,$metafile) = split(/ : /,$metafile_pair);
714
715 $doc_obj->metadata_file($full_metafile,$metafile);
716 }
717
718 $doc_obj->delete_metadata($top_section,"gsdlmetafile");
719}
720
721sub archiveinf_files_to_field
722{
723 my $self = shift(@_);
724 my ($files,$field,$collect_dir,$oid_files,$reverse_lookups) = @_;
725
726 foreach my $file_rec (@$files) {
727 my $real_filename = (ref $file_rec eq "ARRAY") ? $file_rec->[0] : $file_rec;
728 my $full_file = (ref $file_rec eq "ARRAY") ? $file_rec->[1] : $file_rec;
729 # for some reasons the image associate file has / before the full path
730 $real_filename =~ s/^\\(.*)/$1/i;
731
732 my $raw_filename = &util::downgrade_if_dos_filename($real_filename);
733
734 if (&FileUtils::fileExists($raw_filename)) {
735
736# if (defined $collect_dir) {
737# my $collect_dir_re_safe = $collect_dir;
738# $collect_dir_re_safe =~ s/\\/\\\\/g; # use &util::filename_to_regex()
739# $collect_dir_re_safe =~ s/\./\\./g;##
740
741# $real_filename =~ s/^$collect_dir_re_safe//;
742# }
743
744 if (defined $reverse_lookups) {
745 $reverse_lookups->{$real_filename} = 1;
746 }
747### push(@{$oid_files->{$field}},$full_file);
748 push(@{$oid_files->{$field}},$raw_filename);
749 }
750 else {
751 print STDERR "Warning: archiveinf_files_to_field()\n $real_filename does not appear to be on the file system\n";
752 }
753 }
754}
755
756sub archiveinf_db
757{
758 my $self = shift (@_);
759 my ($doc_obj) = @_;
760
761 my $verbosity = $self->{'verbosity'};
762
763 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
764 if (defined $collect_dir) {
765 my $dirsep_regexp = &util::get_os_dirsep();
766
767 if ($collect_dir !~ /$dirsep_regexp$/) {
768 # ensure there is a slash at the end
769 $collect_dir .= &util::get_dirsep();
770 }
771 }
772
773 my $oid = $doc_obj->get_OID();
774 my $source_filename = $doc_obj->get_unmodified_source_filename();
775 my $working_info = $self->{'output_info'};
776 my $doc_info = $working_info->get_info($oid);
777
778 my ($doc_file,$index_status,$sortmeta) = @$doc_info;
779 # doc_file is the path to the archive doc.xml. Make sure it has unix
780 # slashes, then if the collection is copied to linux, it can be built without reimport
781 $doc_file =~ s/\\/\//g;
782 my $oid_files = { 'doc-file' => $doc_file,
783 'index-status' => $index_status,
784 'src-file' => $source_filename,
785 'sort-meta' => $sortmeta,
786 'assoc-file' => [],
787 'meta-file' => [] };
788
789 my $reverse_lookups = { $source_filename => "1" };
790
791
792 $self->archiveinf_files_to_field($doc_obj->get_source_assoc_files(),"assoc-file",
793 $collect_dir,$oid_files,$reverse_lookups);
794
795
796 $self->archiveinf_files_to_field($doc_obj->get_meta_files(),"meta-file",
797 $collect_dir,$oid_files);
798
799 # Get the infodbtype value for this collection from the arcinfo object
800 my $infodbtype = $self->{'output_info'}->{'infodbtype'};
801 my $output_dir = $self->{'output_dir'};
802
803 my $doc_db = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $output_dir);
804 my $src_db = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-src", $output_dir);
805
806 ##print STDERR "*** To set in db: \n\t$doc_db\n\t$oid\n\t$doc_db_text\n";
807 if (($oid_files->{'index-status'} eq "I") || ($oid_files->{'index-status'} eq "R"))
808 {
809 my $top_section = $doc_obj->get_top_section();
810 my $dc_titles = $doc_obj->get_metadata($top_section, 'dls.Title');
811 my $dc_title = join("; ", @$dc_titles);
812 # if we still don't have a title, use a feature I added into a doc_obj ages
813 # ago to find *any* title (regardless of namespace) [jmt12]
814 if ($dc_title eq '')
815 {
816 $dc_title = $doc_obj->get_metadata_element($top_section, 'Title', 1);
817 }
818 if ($oid_files->{'index-status'} eq "R")
819 {
820 $dc_title .= " (Updated)";
821 }
822 my $rss_filename = &FileUtils::filenameConcatenate($output_dir,"rss-items.rdf");
823 my $rssout_fh;
824
825 if (&FileUtils::openFileHandle($rss_filename, '>>', \$rssout_fh))
826 {
827 print $rssout_fh "<item>\n";
828 print $rssout_fh " <title>" . $dc_title . "</title>\n";
829 print $rssout_fh " <link>_httpdomain__httpcollection_/document/" . $doc_obj->get_OID() . "</link>\n";
830 print $rssout_fh "</item>\n";
831 &FileUtils::closeFileHandle($rss_filename, $rssout_fh);
832 }
833 else
834 {
835 print STDERR "**** Failed to open " . $rss_filename . "\n";
836 print STDERR $! . "\n";
837 }
838 }
839
840 $oid_files->{'doc-file'} = [ $oid_files->{'doc-file'} ];
841 $oid_files->{'index-status'} = [ $oid_files->{'index-status'} ];
842 $oid_files->{'src-file'} = [ $oid_files->{'src-file'} ];
843 $oid_files->{'sort-meta'} = [ $oid_files->{'sort-meta'} ];
844
845 my $infodb_file_handle = &dbutil::open_infodb_write_handle($infodbtype, $doc_db, "append");
846 &dbutil::write_infodb_entry($infodbtype, $infodb_file_handle, $oid, $oid_files);
847 &dbutil::close_infodb_write_handle($infodbtype, $infodb_file_handle);
848
849 foreach my $rl (keys %$reverse_lookups) {
850 $working_info->add_reverseinfo($rl,$oid);
851 }
852
853 # meta files not set in reverese entry, but need to set the metadata flag
854 if (defined $doc_obj->get_meta_files()) {
855 foreach my $meta_file_rec(@{$doc_obj->get_meta_files()}) {
856 my $full_file = (ref $meta_file_rec eq "ARRAY") ? $meta_file_rec->[0] : $meta_file_rec;
857 $working_info->set_meta_file_flag($full_file);
858 }
859 }
860}
861
862# /** @function append_to_rss_file()
863# */
864sub append_to_rss_file
865{
866 my $self = shift(@_);
867 my ($doc_obj, $index_status, $output_dir) = @_;
868
869}
870# /** append_to_rss_file() **/
871
872sub set_sortmeta {
873 my $self = shift (@_);
874 my ($sortmeta, $removeprefix, $removesuffix) = @_;
875
876 $self->{'sortmeta'} = $sortmeta;
877 if (defined ($removeprefix) && $removeprefix ) {
878 $removeprefix =~ s/^\^//; # don't need a leading ^
879 $self->{'removeprefix'} = $removeprefix;
880 }
881 if (defined ($removesuffix) && $removesuffix) {
882 $removesuffix =~ s/\$$//; # don't need a trailing $
883 $self->{'removesuffix'} = $removesuffix;
884 }
885}
886
887sub open_xslt_pipe
888{
889 my $self = shift @_;
890 my ($output_file_name, $xslt_file)=@_;
891
892 return unless defined $xslt_file and $xslt_file ne "" and &FileUtils::fileExists($xslt_file);
893
894 my $java_class_path = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"bin","java","ApplyXSLT.jar");
895
896 my $mapping_file_path = "";
897
898 if ($ENV{'GSDLOS'} eq "windows"){
899 $java_class_path .=";".&FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"bin","java","xalan.jar");
900 # this file:/// bit didn't work for me on windows XP
901 #$xslt_file = "\"file:///".$xslt_file."\"";
902 #$mapping_file_path = "\"file:///";
903 }
904 else{
905 $java_class_path .=":".&FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"bin","java","xalan.jar");
906 }
907
908
909 $java_class_path = "\"".$java_class_path."\"";
910
911 my $cmd = "| java -cp $java_class_path org.nzdl.gsdl.ApplyXSLT -t \"$xslt_file\" ";
912
913 if (defined $self->{'mapping_file'} and $self->{'mapping_file'} ne ""){
914 my $mapping_file_path = "\"".$self->{'mapping_file'}."\"";
915 $cmd .= "-m $mapping_file_path";
916 }
917
918 open(*XMLWRITER, $cmd)
919 or die "can't open pipe to xslt: $!";
920
921
922 $self->{'xslt_writer'} = *XMLWRITER;
923
924 print XMLWRITER "<?DocStart?>\n";
925 print XMLWRITER "$output_file_name\n";
926
927
928 }
929
930
931sub close_xslt_pipe
932{
933 my $self = shift @_;
934
935
936 return unless defined $self->{'xslt_writer'} ;
937
938 my $xsltwriter = $self->{'xslt_writer'};
939
940 print $xsltwriter "<?DocEnd?>\n";
941 close($xsltwriter);
942
943 undef $self->{'xslt_writer'};
944
945}
946
947sub close_file_output
948{
949 my ($self) = @_;
950
951 # make sure that the handle has been opened - it won't be if we failed
952 # to import any documents...
953 if (defined(fileno(GROUPPROCESS))) {
954 $self->output_xml_footer('GROUPPROCESS','Archive');
955 close GROUPPROCESS;
956 }
957
958 my $OID = $self->{'gs_OID'};
959 my $short_doc_file = $self->{'short_doc_file'};
960
961 if ($self->{'gzip'}) {
962 my $doc_file = $self->{'gs_filename'};
963 `gzip $doc_file`;
964 $doc_file .= ".gz";
965 $short_doc_file .= ".gz";
966 if (!&FileUtils::fileExists($doc_file)) {
967 my $outhandle = $self->{'output_handle'};
968 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
969 return 0;
970 }
971 }
972
973 # store reference in output_info
974 my $output_info = $self->{'output_info'};
975 return 0 if (!defined $output_info);
976 $output_info->add_info($OID, $short_doc_file, undef, undef);
977 return 1;
978}
979
980
981#the subclass should implement this method if is_group method could return 1.
982sub close_group_output{
983 my $self = shift (@_);
984}
985
986sub is_group {
987 my $self = shift (@_);
988 return 0;
989}
990
991my $dc_set = { Title => 1,
992 Creator => 1,
993 Subject => 1,
994 Description => 1,
995 Publisher => 1,
996 Contributor => 1,
997 Date => 1,
998 Type => 1,
999 Format => 1,
1000 Identifier => 1,
1001 Source => 1,
1002 Language => 1,
1003 Relation => 1,
1004 Coverage => 1,
1005 Rights => 1};
1006
1007
1008# returns an XML representation of the dublin core metadata
1009# if dc meta is not found, try ex meta
1010# This method is not used by the DSpacePlugout, which has its
1011# own method to save its dc metadata
1012sub get_dc_metadata {
1013 my $self = shift(@_);
1014 my ($doc_obj, $section, $version) = @_;
1015
1016 # build up string of dublin core metadata
1017 $section="" unless defined $section;
1018
1019 my $section_ptr = $doc_obj->_lookup_section($section);
1020 return "" unless defined $section_ptr;
1021
1022
1023 my $explicit_dc = {};
1024 my $explicit_ex_dc = {};
1025 my $explicit_ex = {};
1026
1027 my $all_text="";
1028
1029 # We want high quality dc metadata to go in first, so we store all the
1030 # assigned dc.* values first. Then, for all those dc metadata names in
1031 # the official dc set that are as yet unassigned, we look to see whether
1032 # embedded ex.dc.* metadata has defined some values for them. If not,
1033 # then for the same missing dc metadata names, we look in ex metadata.
1034
1035 foreach my $data (@{$section_ptr->{'metadata'}}){
1036 my $escaped_value = &docprint::escape_text($data->[1]);
1037 if ($data->[0]=~ m/^dc\./) {
1038 $data->[0] =~ tr/[A-Z]/[a-z]/;
1039
1040 $data->[0] =~ m/^dc\.(.*)/;
1041 my $dc_element = $1;
1042
1043 if (!defined $explicit_dc->{$dc_element}) {
1044 $explicit_dc->{$dc_element} = [];
1045 }
1046 push(@{$explicit_dc->{$dc_element}},$escaped_value);
1047
1048 if (defined $version && ($version eq "oai_dc")) {
1049 $all_text .= " <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
1050 }
1051 else {
1052 # qualifier???
1053 $all_text .= ' <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
1054 }
1055
1056 } elsif ($data->[0]=~ m/^ex\.dc\./) { # now look through ex.dc.* to fill in as yet unassigned fields in dc metaset
1057 $data->[0] =~ m/^ex\.dc\.(.*)/;
1058 my $ex_dc_element = $1;
1059 my $lc_ex_dc_element = lc($ex_dc_element);
1060
1061 # only store the ex.dc value for this dc metaname if no dc.* was assigned for it
1062 if (defined $dc_set->{$ex_dc_element}) {
1063 if (!defined $explicit_ex_dc->{$lc_ex_dc_element}) {
1064 $explicit_ex_dc->{$lc_ex_dc_element} = [];
1065 }
1066 push(@{$explicit_ex_dc->{$lc_ex_dc_element}},$escaped_value);
1067 }
1068 }
1069 elsif (($data->[0] =~ m/^ex\./) || ($data->[0] !~ m/\./)) { # look through ex. meta (incl. meta without prefix)
1070 $data->[0] =~ m/^(ex\.)?(.*)/;
1071 my $ex_element = $2;
1072 my $lc_ex_element = lc($ex_element);
1073
1074 if (defined $dc_set->{$ex_element}) {
1075 if (!defined $explicit_ex->{$lc_ex_element}) {
1076 $explicit_ex->{$lc_ex_element} = [];
1077 }
1078 push(@{$explicit_ex->{$lc_ex_element}},$escaped_value);
1079 }
1080 }
1081 }
1082
1083 # go through dc_set and for any element *not* defined in explicit_dc
1084 # that does exist in explicit_ex, add it in as metadata
1085 foreach my $k ( keys %$dc_set ) {
1086 my $lc_k = lc($k);
1087
1088 if (!defined $explicit_dc->{$lc_k}) {
1089 # try to find if ex.dc.* defines this dc.* meta,
1090 # if not, then look for whether there's an ex.* equivalent
1091
1092 if (defined $explicit_ex_dc->{$lc_k}) {
1093 foreach my $v (@{$explicit_ex_dc->{$lc_k}}) {
1094 my $dc_element = $lc_k;
1095 my $escaped_value = $v;
1096
1097 if (defined $version && ($version eq "oai_dc")) {
1098 $all_text .= " <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
1099 }
1100 else {
1101 $all_text .= ' <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
1102 }
1103 }
1104 } elsif (defined $explicit_ex->{$lc_k}) {
1105 foreach my $v (@{$explicit_ex->{$lc_k}}) {
1106 my $dc_element = $lc_k;
1107 my $escaped_value = $v;
1108
1109 if (defined $version && ($version eq "oai_dc")) {
1110 $all_text .= " <dc:$dc_element>$escaped_value</dc:$dc_element>\n";
1111 }
1112 else {
1113 $all_text .= ' <dcvalue element="'. $dc_element.'">'. $escaped_value. "</dcvalue>\n";
1114 }
1115 }
1116 }
1117 }
1118 }
1119
1120 if ($all_text eq "") {
1121 $all_text .= " There is no Dublin Core metatdata in this document\n";
1122 }
1123 $all_text =~ s/[\x00-\x09\x0B\x0C\x0E-\x1F]//g;
1124
1125 return $all_text;
1126}
1127
1128# Build up dublin_core metadata. Priority given to dc.* over ex.*
1129# This method was apparently added by Jeffrey and committed by Shaoqun.
1130# But we don't know why it was added, so not using it anymore.
1131sub new_get_dc_metadata {
1132
1133 my $self = shift(@_);
1134 my ($doc_obj, $section, $version) = @_;
1135
1136 # build up string of dublin core metadata
1137 $section="" unless defined $section;
1138
1139 my $section_ptr=$doc_obj->_lookup_section($section);
1140 return "" unless defined $section_ptr;
1141
1142 my $all_text = "";
1143 foreach my $data (@{$section_ptr->{'metadata'}}){
1144 my $escaped_value = &docprint::escape_text($data->[1]);
1145 my $dc_element = $data->[0];
1146
1147 my @array = split('\.',$dc_element);
1148 my ($type,$name);
1149
1150 if(defined $array[1])
1151 {
1152 $type = $array[0];
1153 $name = $array[1];
1154 }
1155 else
1156 {
1157 $type = "ex";
1158 $name = $array[0];
1159 }
1160
1161 $all_text .= ' <Metadata Type="'. $type.'" Name="'.$name.'">'. $escaped_value. "</Metadata>\n";
1162 }
1163 return $all_text;
1164}
1165
1166
11671;
Note: See TracBrowser for help on using the repository browser.