source: trunk/gsdl/perllib/plugouts/BasPlugout.pm@ 12618

Last change on this file since 12618 was 12618, checked in by kjdon, 18 years ago

now can pass in OIDmetadata to set_OIDtype

  • Property svn:keywords set to Author Date Id Revision
File size: 19.2 KB
Line 
1###########################################################################
2#
3# BasPlugout.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 BasPlugout;
27
28eval {require bytes};
29
30use strict;
31no strict 'subs';
32no strict 'refs';
33
34use gsprintf 'gsprintf';
35use printusage;
36use parse2;
37
38# suppress the annoying "subroutine redefined" warning that various
39# gets cause under perl 5.6
40$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
41
42my $arguments = [
43 { 'name' => "group_size",
44 'desc' => "{BasPlugout.group_size}",
45 'type' => "int",
46 'deft' => "1",
47 'reqd' => "no",
48 'hiddengli' => "no"},
49 { 'name' => "output_info",
50 'desc' => "{BasPlugout.output_info}",
51 'type' => "string",
52 'reqd' => "yes",
53 'hiddengli' => "yes"},
54 { 'name' => "xslt_file",
55 'desc' => "{BasPlugout.xslt_file}",
56 'type' => "string",
57 'reqd' => "no",
58 'hiddengli' => "no"},
59 { 'name' => "output_handle",
60 'desc' => "{BasPlugout.output_handle}",
61 'type' => "string",
62 'deft' => 'STDERR',
63 'reqd' => "no",
64 'hiddengli' => "yes"},
65 { 'name' => "verbosity",
66 'desc' => "{BasPlugout.verbosity}",
67 'type' => "int",
68 'deft' => "0",
69 'reqd' => "no",
70 'hiddengli' => "no"},
71 { 'name' => "gzip_output",
72 'desc' => "{BasPlugout.gzip_output}",
73 'type' => "flag",
74 'reqd' => "no",
75 'hiddengli' => "no"}
76];
77
78my $options = { 'name' => "BasPlugout",
79 'desc' => "{BasPlugout.desc}",
80 'abstract' => "yes",
81 'inherits' => "no",
82 'args' => $arguments};
83
84sub new
85{
86 my $class = shift (@_);
87
88 my ($plugoutlist,$args,$hashArgOptLists) = @_;
89 push(@$plugoutlist, $class);
90
91 my $strPlugoutName = (defined $plugoutlist->[0]) ? $plugoutlist->[0] : $class;
92
93 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
94 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
95
96 my $self = {};
97 $self->{'plugout_type'} = $class;
98 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
99 $self->{"info_only"} = 0;
100
101 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
102 # the args, just return the object.
103 foreach my $strArg (@{$args})
104 {
105 if(defined $strArg && $strArg eq "-gsdlinfo")
106 {
107 $self->{"info_only"} = 1;
108 return bless $self, $class;
109 }
110 }
111
112 delete $self->{"info_only"};
113
114 if(parse2::parse($args,$hashArgOptLists->{"ArgList"},$self) == -1)
115 {
116 my $classTempClass = bless $self, $class;
117 print STDERR "<BadPlugout d=$self->{'plugout_name'}>\n";
118 &gsprintf(STDERR, "\n{BasPlugout.bad_general_option}\n", $self->{'plugout_name'});
119 $classTempClass->print_txt_usage(""); # Use default resource bundle
120 die "\n";
121 }
122
123
124 if(defined $self->{'xslt_file'} && $self->{'xslt_file'} ne "")
125 {
126 ##$self->{'xslt_file'} =~ s/\"//g;##working on Windows???
127 print STDERR "Can not find $self->{'xslt_file'}, please make sure you have supplied the correct file path\n" and die "\n" unless (-e $self->{'xslt_file'});
128 }
129
130 $self->{'gs_count'} = 0;
131
132 $self->{'keep_import_structure'} = 0;
133
134 return bless $self, $class;
135
136}
137
138sub print_xml_usage
139{
140 my $self = shift(@_);
141 my $header = shift(@_);
142
143 # XML output is always in UTF-8
144 gsprintf::output_strings_in_UTF8;
145
146 if ($header) {
147 &PrintUsage::print_xml_header("plugout");
148 }
149 $self->print_xml();
150}
151
152
153sub print_xml
154{
155 my $self = shift(@_);
156 my $optionlistref = $self->{'option_list'};
157 my @optionlist = @$optionlistref;
158 my $plugoutoptions = shift(@$optionlistref);
159 return if (!defined($plugoutoptions));
160
161 gsprintf(STDERR, "<PlugoutInfo>\n");
162 gsprintf(STDERR, " <Name>$plugoutoptions->{'name'}</Name>\n");
163 my $desc = gsprintf::lookup_string($plugoutoptions->{'desc'});
164 $desc =~ s/</&amp;lt;/g; # doubly escaped
165 $desc =~ s/>/&amp;gt;/g;
166 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
167 gsprintf(STDERR, " <Abstract>$plugoutoptions->{'abstract'}</Abstract>\n");
168 gsprintf(STDERR, " <Inherits>$plugoutoptions->{'inherits'}</Inherits>\n");
169 gsprintf(STDERR, " <Arguments>\n");
170
171 if (defined($plugoutoptions->{'args'})) {
172 &PrintUsage::print_options_xml($plugoutoptions->{'args'});
173 }
174
175 gsprintf(STDERR, " </Arguments>\n");
176 # Recurse up the plugout hierarchy
177 $self->print_xml();
178 gsprintf(STDERR, "</PlugoutInfo>\n");
179}
180
181
182sub print_txt_usage
183{
184 my $self = shift(@_);
185
186 # Print the usage message for a plugout (recursively)
187 my $descoffset = $self->determine_description_offset(0);
188 $self->print_plugout_usage($descoffset, 1);
189}
190
191sub determine_description_offset
192{
193 my $self = shift(@_);
194 my $maxoffset = shift(@_);
195
196 my $optionlistref = $self->{'option_list'};
197 my @optionlist = @$optionlistref;
198 my $plugoutoptions = pop(@$optionlistref);
199 return $maxoffset if (!defined($plugoutoptions));
200
201 # Find the length of the longest option string of this download
202 my $plugoutargs = $plugoutoptions->{'args'};
203 if (defined($plugoutargs)) {
204 my $longest = &PrintUsage::find_longest_option_string($plugoutargs);
205 if ($longest > $maxoffset) {
206 $maxoffset = $longest;
207 }
208 }
209
210 # Recurse up the download hierarchy
211 $maxoffset = $self->determine_description_offset($maxoffset);
212 $self->{'option_list'} = \@optionlist;
213 return $maxoffset;
214}
215
216
217sub print_plugout_usage
218{
219 my $self = shift(@_);
220 my $descoffset = shift(@_);
221 my $isleafclass = shift(@_);
222
223 my $optionlistref = $self->{'option_list'};
224 my @optionlist = @$optionlistref;
225 my $plugoutoptions = shift(@$optionlistref);
226 return if (!defined($plugoutoptions));
227
228 my $plugoutname = $plugoutoptions->{'name'};
229 my $plugoutargs = $plugoutoptions->{'args'};
230 my $plugoutdesc = $plugoutoptions->{'desc'};
231
232 # Produce the usage information using the data structure above
233 if ($isleafclass) {
234 if (defined($plugoutdesc)) {
235 gsprintf(STDERR, "$plugoutdesc\n\n");
236 }
237 gsprintf(STDERR, " {common.usage}: plugout $plugoutname [{common.options}]\n\n");
238 }
239
240 # Display the download options, if there are some
241 if (defined($plugoutargs)) {
242 # Calculate the column offset of the option descriptions
243 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
244
245 if ($isleafclass) {
246 gsprintf(STDERR, " {common.specific_options}:\n");
247 }
248 else {
249 gsprintf(STDERR, " {common.general_options}:\n", $plugoutname);
250 }
251
252 # Display the download options
253 &PrintUsage::print_options_txt($plugoutargs, $optiondescoffset);
254 }
255
256 # Recurse up the download hierarchy
257 $self->print_plugout_usage($descoffset, 0);
258 $self->{'option_list'} = \@optionlist;
259}
260
261
262sub error
263{
264 my ($strFunctionName,$strError) = @_;
265 {
266 print "Error occoured in BasPlugout.pm\n".
267 "In Function: ".$strFunctionName."\n".
268 "Error Message: ".$strError."\n";
269 exit(-1);
270 }
271}
272
273# OIDtype may be "hash" or "incremental" or "dirname" or "assigned"
274sub set_OIDtype {
275 my $self = shift (@_);
276 my ($type, $metadata) = @_;
277
278 if ($type =~ /^(hash|incremental|dirname|assigned)$/) {
279 $self->{'OIDtype'} = $type;
280 } else {
281 $self->{'OIDtype'} = "hash";
282 }
283 if ($type =~ /^assigned$/) {
284 if (defined $metadata) {
285 $self->{'OIDmetadata'} = $metadata;
286 } else {
287 $self->{'OIDmetadata'} = "dc.Identifier";
288 }
289 }
290}
291
292sub set_output_dir
293{
294 my $self = shift @_;
295 my ($output_dir) = @_;
296
297 $self->{'output_dir'} = $output_dir;
298}
299
300sub setoutputdir
301{
302 my $self = shift @_;
303 my ($output_dir) = @_;
304
305 $self->{'output_dir'} = $output_dir;
306}
307
308sub get_output_dir
309{
310 my $self = shift (@_);
311
312 return $self->{'output_dir'};
313}
314
315sub getoutputdir
316{
317 my $self = shift (@_);
318
319 return $self->{'output_dir'};
320}
321
322sub getoutputinfo
323{
324 my $self = shift (@_);
325
326 return $self->{'output_info'};
327}
328
329
330sub get_output_handler
331{
332 my $self = shift (@_);
333
334 my ($output_file_name) = @_;
335
336 open(*OUTPUT, ">$output_file_name") or die "Can not open a file handler for $output_file_name\n";
337
338 return *OUTPUT;
339}
340
341sub release_output_handler
342{
343 my $self = shift (@_);
344 my ($outhandler) = @_;
345
346 close($outhandler);
347
348}
349
350sub output_xml_header {
351 my $self = shift (@_);
352 my ($handle,$docroot,$nondoctype) = @_;
353
354 print $handle '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
355
356 if (!defined $nondoctype){
357 print $handle '<!DOCTYPE Archive SYSTEM "http://greenstone.org/dtd/Archive/1.0/Archive.dtd">' . "\n";
358 }
359
360 print $handle "<$docroot>\n" if defined $docroot;
361}
362
363sub output_xml_footer {
364 my $self = shift (@_);
365 my ($handle,$docroot) = @_;
366 print $handle "</$docroot>\n" if defined $docroot;
367}
368
369sub process {
370 my $self = shift (@_);
371 my ($doc_obj) = @_;
372
373 $doc_obj->set_lastmodified();
374
375 if ($self->{'group_size'} > 1) {
376 $self->group_process ($doc_obj);
377 return;
378 }
379
380 my $OID = $doc_obj->get_OID();
381 $OID = "NULL" unless defined $OID;
382
383 my $top_section = $doc_obj->get_top_section();
384
385 #get document's directory
386 my $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
387
388 my $output_info = $self->{'output_info'};
389 return if (!defined $output_info);
390
391 ##############################
392 # call subclass' saveas method
393 ##############################
394 $self->saveas($doc_obj,$doc_dir);
395
396}
397
398sub store_output_info_reference {
399 my $self = shift (@_);
400 my ($doc_obj) = @_;
401
402 my $output_info = $self->{'output_info'};
403 my $metaname = $self->{'sortmeta'};
404 if (!defined $metaname || $metaname !~ /\S/) {
405 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, "");
406 return;
407 }
408
409 my $metadata = "";
410 my $top_section = $doc_obj->get_top_section();
411
412 my @commameta_list = split(/,/, $metaname);
413 foreach my $cmn (@commameta_list) {
414 my $meta = $doc_obj->get_metadata_element($top_section, $cmn);
415 if ($meta) {
416 # do remove prefix/suffix - this will apply to all values
417 $meta =~ s/^$self->{'removeprefix'}// if defined $self->{'removeprefix'};
418 $meta =~ s/$self->{'removesuffix'}$// if defined $self->{'removesuffix'};
419 $meta = &sorttools::format_metadata_for_sorting($cmn, $meta, $doc_obj);
420 $metadata .= $meta if ($meta);
421 }
422 }
423
424 # store reference in the output_info
425 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, $metadata);
426
427}
428
429sub group_process {
430
431 my $self = shift (@_);
432 my ($doc_obj) = @_;
433
434 my $OID = $doc_obj->get_OID();
435 $OID = "NULL" unless defined $OID;
436
437 my $groupsize = $self->{'group_size'};
438 my $gs_count = $self->{'gs_count'};
439 my $open_new_file = (($gs_count % $groupsize)==0);
440 my $outhandle = $self->{'output_handle'};
441
442 # opening a new file, or document has assoicated files => directory needed
443 if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0)) {
444
445 # The directory the archive file (doc.xml) and all associated files
446 # should end up in
447 my $doc_dir;
448 # If we've determined its time for a new file, open it now
449 if ($open_new_file || !defined($self->{'gs_doc_dir'}))
450 {
451 $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
452 # only if opening new file
453 my $output_dir = $self->get_output_dir();
454 &util::mk_all_dir ($output_dir) unless -e $output_dir;
455 my $doc_file = &util::filename_cat ($output_dir, $doc_dir, "doc.xml");
456 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.xml");
457
458 if ($gs_count>0)
459 {
460 return if (!$self->close_file_output());
461 }
462
463 open (GROUPPROCESS, ">$doc_file") or (print $outhandle "BasPlugout::group_process could not write to file $doc_file\n" and return);
464
465
466 $self->{'gs_filename'} = $doc_file;
467 $self->{'short_doc_file'} = $short_doc_file;
468 $self->{'gs_OID'} = $OID;
469 $self->{'gs_doc_dir'} = $doc_dir;
470
471 $self->output_xml_header('BasPlugout::GROUPPROCESS','Archive');
472 }
473 # Otherwise load the same archive document directory used last time
474 else
475 {
476 $doc_dir = $self->{'gs_doc_dir'};
477 }
478
479 # copy all the associated files, add this information as metadata
480 # to the document
481 print STDERR "Writing associated files to $doc_dir\n";
482 $self->process_assoc_files ($doc_obj, $doc_dir);
483 }
484
485 # save this document
486 $doc_obj->output_section('BasPlugout::GROUPPROCESS', $doc_obj->get_top_section());
487
488 $self->{'gs_count'}++;
489}
490
491
492sub saveas {
493 my $self = shift (@_);
494
495 die "Basplug::saveas function must be implemented in sub classes\n";
496}
497
498sub get_doc_dir {
499 my $self = shift (@_);
500 my ($OID, $source_filename) = @_;
501
502 my $working_dir = $self->get_output_dir();
503 my $working_info = $self->{output_info};
504 return if (!defined $working_info);
505
506 my $doc_info = $working_info->get_info($OID);
507 my $doc_dir = '';
508
509 if (defined $doc_info && scalar(@$doc_info) >= 1) {
510 # this OID already has an assigned directory, use the
511 # same one.
512 $doc_dir = $doc_info->[0];
513 $doc_dir =~ s/\/?((doc(mets)?)|(dublin_core))\.xml(\.gz)?$//;
514 } elsif ($self->{'keep_import_structure'}) {
515 $source_filename = &File::Basename::dirname($source_filename);
516 $source_filename =~ s/[\\\/]+/\//g;
517 $source_filename =~ s/\/$//;
518
519 $doc_dir = substr($source_filename, length($ENV{'GSDLIMPORTDIR'}) + 1);
520
521 }
522
523 # have to get a new document directory
524 $doc_dir = $self->get_new_doc_dir($working_info,$working_dir,$OID) unless $doc_dir ne "";
525
526 $doc_dir .= ".dir";
527 if (!defined $self->{'group'} || !$self->{'group'}){
528 &util::mk_all_dir (&util::filename_cat ($working_dir, $doc_dir));
529 }
530 return $doc_dir;
531}
532
533sub get_new_doc_dir{
534 my $self = shift (@_);
535 my($working_info,$working_dir,$OID) = @_;
536
537 my $doc_dir = "";
538 my $doc_dir_rest = $OID;
539 my $doc_dir_num = 0;
540
541 do {
542 $doc_dir .= "/" if $doc_dir_num > 0;
543 if ($doc_dir_rest =~ s/^(.{1,8})//) {
544 $doc_dir .= $1;
545 $doc_dir_num++;
546 }
547 } while ($doc_dir_rest ne "" &&
548 ((-d &util::filename_cat ($working_dir, "$doc_dir.dir")) ||
549 ($working_info->size() >= 1024 && $doc_dir_num < 2)));
550
551
552 return $doc_dir;
553}
554
555sub process_assoc_files {
556 my $self = shift (@_);
557 my ($doc_obj, $doc_dir, $handle) = @_;
558
559 my $outhandle = $self->{'output_handle'};
560
561 my $output_dir = $self->get_output_dir();
562 return if (!defined $output_dir);
563
564 &util::mk_all_dir ($output_dir) unless -e $output_dir;
565
566 my $working_dir = &util::filename_cat($output_dir, $doc_dir);
567 &util::mk_all_dir ($working_dir) unless -e $working_dir;
568
569 my @assoc_files = ();
570 my $filename;;
571
572 my $source_filename = $doc_obj->get_source_filename();
573
574 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
575
576 if (defined $collect_dir) {
577 my $dirsep_regexp = &util::get_os_dirsep();
578
579 if ($collect_dir !~ /$dirsep_regexp$/) {
580 $collect_dir .= &util::get_dirsep(); # ensure there is a slash at the end
581 }
582
583 # This test is never going to fail on Windows -- is this a problem?
584
585 if ($source_filename !~ /^$dirsep_regexp/) {
586 $source_filename = &util::filename_cat($collect_dir, $source_filename);
587 }
588 }
589
590
591 # set the assocfile path (even if we have no assoc files - need this for lucene)
592 $doc_obj->set_utf8_metadata_element ($doc_obj->get_top_section(),
593 "assocfilepath",
594 "$doc_dir");
595 foreach my $assoc_file_rec (@{$doc_obj->get_assoc_files()}) {
596 my ($dir, $afile) = $assoc_file_rec->[1] =~ /^(.*?)([^\/\\]+)$/;
597 $dir = "" unless defined $dir;
598
599
600 my $real_filename = $assoc_file_rec->[0];
601 # for some reasons the image associate file has / before the full path
602 $real_filename =~ s/^\\(.*)/$1/i;
603 if (-e $real_filename) {
604
605 $filename = &util::filename_cat($working_dir, $afile);
606
607 &util::hard_link ($real_filename, $filename);
608
609 $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
610 "gsdlassocfile",
611 "$afile:$assoc_file_rec->[2]:$dir");
612 } elsif ($self->{'verbosity'} > 2) {
613 print $outhandle "BasPlugout::process couldn't copy the associated file " .
614 "$real_filename to $afile\n";
615 }
616 }
617}
618
619sub set_sortmeta {
620 my $self = shift (@_);
621 my ($sortmeta, $removeprefix, $removesuffix) = @_;
622
623 $self->{'sortmeta'} = $sortmeta;
624 if (defined ($removeprefix) && $removeprefix ) {
625 $removeprefix =~ s/^\^//; # don't need a leading ^
626 $self->{'removeprefix'} = $removeprefix;
627 }
628 if (defined ($removesuffix) && $removesuffix) {
629 $removesuffix =~ s/\$$//; # don't need a trailing $
630 $self->{'removesuffix'} = $removesuffix;
631 }
632}
633
634sub open_xslt_pipe
635{
636 my $self = shift @_;
637 my ($output_file_name, $xslt_file)=@_;
638
639 return unless defined $xslt_file and $xslt_file ne "" and -e $xslt_file;
640
641 my $java_class_path = &util::filename_cat ($ENV{'GSDLHOME'},"bin","java");
642 my $cmd = "| java -cp $java_class_path org.nzdl.gsdl.ApplyXSLT $xslt_file ";
643
644 if (defined $self->{'mapping_file'} and $self->{'mapping_file'} ne ""){
645 $cmd .= $self->{'mapping_file'};
646 }
647
648 open(*XMLWRITER, $cmd)
649 or die "can't open pipe to xslt: $!";
650
651
652 $self->{'xslt_writer'} = *XMLWRITER;
653
654 print XMLWRITER "<?DocStart?>\n";
655 print XMLWRITER "$output_file_name\n";
656
657 }
658
659
660sub close_xslt_pipe
661{
662 my $self = shift @_;
663
664
665 return unless defined $self->{'xslt_writer'} ;
666
667 my $xsltwriter = $self->{'xslt_writer'};
668
669 print $xsltwriter "<?DocEnd?>\n";
670 close($xsltwriter);
671}
672
673sub close_file_output
674{
675 my ($self) = @_;
676
677 # make sure that the handle has been opened - it won't be if we failed
678 # to import any documents...
679 if (defined(fileno(GROUPPROCESS))) {
680 $self->output_xml_footer('GROUPPROCESS','Archive');
681 close GROUPPROCESS;
682 }
683
684 my $OID = $self->{'gs_OID'};
685 my $short_doc_file = $self->{'short_doc_file'};
686
687 if ($self->{'gzip'}) {
688 my $doc_file = $self->{'gs_filename'};
689 `gzip $doc_file`;
690 $doc_file .= ".gz";
691 $short_doc_file .= ".gz";
692 if (!-e $doc_file) {
693 my $outhandle = $self->{'output_handle'};
694 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
695 return 0;
696 }
697 }
698
699 # store reference in output_info
700 my $output_info = $self->{'output_info'};
701 return 0 if (!defined $output_info);
702 $output_info->add_info($OID, $short_doc_file, undef, undef);
703 return 1;
704}
705
706#the subclass should implement this method if is_group method could return 1.
707sub close_group_output{
708 my $self = shift (@_);
709}
710
711sub is_group {
712 my $self = shift (@_);
713 return 0;
714}
715
7161;
Note: See TracBrowser for help on using the repository browser.