source: main/trunk/greenstone2/perllib/plugouts/BasePlugout.pm@ 21414

Last change on this file since 21414 was 21414, checked in by davidb, 14 years ago

Separation of different database back-ends into individual files

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