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

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

changed parse2::parse so that it returns -1 on error, 0 on success, or if allow_extra_options is specified, then on success returns the number of args left over.

  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 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($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) = @_;
277
278 if ($type =~ /^(hash|incremental|dirname|assigned)$/) {
279 $self->{'OIDtype'} = $type;
280 } else {
281 $self->{'OIDtype'} = "hash";
282 }
283}
284
285sub set_output_dir
286{
287 my $self = shift @_;
288 my ($output_dir) = @_;
289
290 $self->{'output_dir'} = $output_dir;
291}
292
293sub setoutputdir
294{
295 my $self = shift @_;
296 my ($output_dir) = @_;
297
298 $self->{'output_dir'} = $output_dir;
299}
300
301sub get_output_dir
302{
303 my $self = shift (@_);
304
305 return $self->{'output_dir'};
306}
307
308sub getoutputdir
309{
310 my $self = shift (@_);
311
312 return $self->{'output_dir'};
313}
314
315sub getoutputinfo
316{
317 my $self = shift (@_);
318
319 return $self->{'output_info'};
320}
321
322
323sub get_output_handler
324{
325 my $self = shift (@_);
326
327 my ($output_file_name) = @_;
328
329 open(*OUTPUT, ">$output_file_name") or die "Can not open a file handler for $output_file_name\n";
330
331 return *OUTPUT;
332}
333
334sub release_output_handler
335{
336 my $self = shift (@_);
337 my ($outhandler) = @_;
338
339 close($outhandler);
340
341}
342
343sub output_xml_header {
344 my $self = shift (@_);
345 my ($handle,$docroot,$nondoctype) = @_;
346
347 print $handle '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
348
349 if (!defined $nondoctype){
350 print $handle '<!DOCTYPE Archive SYSTEM "http://greenstone.org/dtd/Archive/1.0/Archive.dtd">' . "\n";
351 }
352
353 print $handle "<$docroot>\n" if defined $docroot;
354}
355
356sub output_xml_footer {
357 my $self = shift (@_);
358 my ($handle,$docroot) = @_;
359 print $handle "</$docroot>\n" if defined $docroot;
360}
361
362sub process {
363 my $self = shift (@_);
364 my ($doc_obj) = @_;
365
366 $doc_obj->set_lastmodified();
367
368 if ($self->{'group_size'} > 1) {
369 $self->group_process ($doc_obj);
370 return;
371 }
372
373 my $OID = $doc_obj->get_OID();
374 $OID = "NULL" unless defined $OID;
375
376 my $top_section = $doc_obj->get_top_section();
377
378 #get document's directory
379 my $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
380
381 my $output_info = $self->{'output_info'};
382 return if (!defined $output_info);
383
384 ##############################
385 # call subclass' saveas method
386 ##############################
387 $self->saveas($doc_obj,$doc_dir);
388
389}
390
391sub store_output_info_reference {
392 my $self = shift (@_);
393 my ($doc_obj) = @_;
394
395 my $output_info = $self->{'output_info'};
396 my $metaname = $self->{'sortmeta'};
397 if (!defined $metaname || $metaname !~ /\S/) {
398 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, "");
399 return;
400 }
401
402 my $metadata = "";
403 my $top_section = $doc_obj->get_top_section();
404
405 my @commameta_list = split(/,/, $metaname);
406 foreach my $cmn (@commameta_list) {
407 my $meta = $doc_obj->get_metadata_element($top_section, $cmn);
408 if ($meta) {
409 # do remove prefix/suffix - this will apply to all values
410 $meta =~ s/^$self->{'removeprefix'}// if defined $self->{'removeprefix'};
411 $meta =~ s/$self->{'removesuffix'}$// if defined $self->{'removesuffix'};
412 $meta = &sorttools::format_metadata_for_sorting($cmn, $meta, $doc_obj);
413 $metadata .= $meta if ($meta);
414 }
415 }
416
417 # store reference in the output_info
418 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, $metadata);
419
420}
421
422sub group_process {
423
424 my $self = shift (@_);
425 my ($doc_obj) = @_;
426
427 my $OID = $doc_obj->get_OID();
428 $OID = "NULL" unless defined $OID;
429
430 my $groupsize = $self->{'group_size'};
431 my $gs_count = $self->{'gs_count'};
432 my $open_new_file = (($gs_count % $groupsize)==0);
433 my $outhandle = $self->{'output_handle'};
434
435 # opening a new file, or document has assoicated files => directory needed
436 if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0)) {
437
438 # The directory the archive file (doc.xml) and all associated files
439 # should end up in
440 my $doc_dir;
441 # If we've determined its time for a new file, open it now
442 if ($open_new_file || !defined($self->{'gs_doc_dir'}))
443 {
444 $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
445 # only if opening new file
446 my $output_dir = $self->get_output_dir();
447 &util::mk_all_dir ($output_dir) unless -e $output_dir;
448 my $doc_file = &util::filename_cat ($output_dir, $doc_dir, "doc.xml");
449 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.xml");
450
451 if ($gs_count>0)
452 {
453 return if (!$self->close_file_output());
454 }
455
456 open (GROUPPROCESS, ">$doc_file") or (print $outhandle "BasPlugout::group_process could not write to file $doc_file\n" and return);
457
458
459 $self->{'gs_filename'} = $doc_file;
460 $self->{'short_doc_file'} = $short_doc_file;
461 $self->{'gs_OID'} = $OID;
462 $self->{'gs_doc_dir'} = $doc_dir;
463
464 $self->output_xml_header('BasPlugout::GROUPPROCESS','Archive');
465 }
466 # Otherwise load the same archive document directory used last time
467 else
468 {
469 $doc_dir = $self->{'gs_doc_dir'};
470 }
471
472 # copy all the associated files, add this information as metadata
473 # to the document
474 print STDERR "Writing associated files to $doc_dir\n";
475 $self->process_assoc_files ($doc_obj, $doc_dir);
476 }
477
478 # save this document
479 $doc_obj->output_section('BasPlugout::GROUPPROCESS', $doc_obj->get_top_section());
480
481 $self->{'gs_count'}++;
482}
483
484
485sub saveas {
486 my $self = shift (@_);
487
488 die "Basplug::saveas function must be implemented in sub classes\n";
489}
490
491sub get_doc_dir {
492 my $self = shift (@_);
493 my ($OID, $source_filename) = @_;
494
495 my $working_dir = $self->get_output_dir();
496 my $working_info = $self->{output_info};
497 return if (!defined $working_info);
498
499 my $doc_info = $working_info->get_info($OID);
500 my $doc_dir = '';
501
502 if (defined $doc_info && scalar(@$doc_info) >= 1) {
503 # this OID already has an assigned directory, use the
504 # same one.
505 $doc_dir = $doc_info->[0];
506 $doc_dir =~ s/\/?((doc(mets)?)|(dublin_core))\.xml(\.gz)?$//;
507 } elsif ($self->{'keep_import_structure'}) {
508 $source_filename = &File::Basename::dirname($source_filename);
509 $source_filename =~ s/[\\\/]+/\//g;
510 $source_filename =~ s/\/$//;
511
512 $doc_dir = substr($source_filename, length($ENV{'GSDLIMPORTDIR'}) + 1);
513
514 }
515
516 # have to get a new document directory
517 $doc_dir = $self->get_new_doc_dir($working_info,$working_dir,$OID) unless $doc_dir ne "";
518
519 $doc_dir .= ".dir";
520 &util::mk_all_dir (&util::filename_cat ($working_dir, $doc_dir));
521 return $doc_dir;
522}
523
524sub get_new_doc_dir{
525 my $self = shift (@_);
526 my($working_info,$working_dir,$OID) = @_;
527
528 my $doc_dir = "";
529 my $doc_dir_rest = $OID;
530 my $doc_dir_num = 0;
531
532 do {
533 $doc_dir .= "/" if $doc_dir_num > 0;
534 if ($doc_dir_rest =~ s/^(.{1,8})//) {
535 $doc_dir .= $1;
536 $doc_dir_num++;
537 }
538 } while ($doc_dir_rest ne "" &&
539 ((-d &util::filename_cat ($working_dir, "$doc_dir.dir")) ||
540 ($working_info->size() >= 1024 && $doc_dir_num < 2)));
541
542
543 return $doc_dir;
544}
545
546sub process_assoc_files {
547 my $self = shift (@_);
548 my ($doc_obj, $doc_dir, $handle) = @_;
549
550 my $outhandle = $self->{'output_handle'};
551
552 my $output_dir = $self->get_output_dir();
553 return if (!defined $output_dir);
554
555 &util::mk_all_dir ($output_dir) unless -e $output_dir;
556
557 my $working_dir = &util::filename_cat($output_dir, $doc_dir);
558 &util::mk_all_dir ($working_dir) unless -e $working_dir;
559
560 my @assoc_files = ();
561 my $filename;;
562
563 my $source_filename = $doc_obj->get_source_filename();
564
565 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
566
567 if (defined $collect_dir) {
568 my $dirsep_regexp = &util::get_os_dirsep();
569
570 if ($collect_dir !~ /$dirsep_regexp$/) {
571 $collect_dir .= &util::get_dirsep(); # ensure there is a slash at the end
572 }
573
574 # This test is never going to fail on Windows -- is this a problem?
575
576 if ($source_filename !~ /^$dirsep_regexp/) {
577 $source_filename = &util::filename_cat($collect_dir, $source_filename);
578 }
579 }
580
581
582 # set the assocfile path (even if we have no assoc files - need this for lucene)
583 $doc_obj->set_utf8_metadata_element ($doc_obj->get_top_section(),
584 "assocfilepath",
585 "$doc_dir");
586 foreach my $assoc_file_rec (@{$doc_obj->get_assoc_files()}) {
587 my ($dir, $afile) = $assoc_file_rec->[1] =~ /^(.*?)([^\/\\]+)$/;
588 $dir = "" unless defined $dir;
589
590
591 my $real_filename = $assoc_file_rec->[0];
592 # for some reasons the image associate file has / before the full path
593 $real_filename =~ s/^\\(.*)/$1/i;
594 if (-e $real_filename) {
595
596 $filename = &util::filename_cat($working_dir, $afile);
597
598 &util::hard_link ($real_filename, $filename);
599
600 $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
601 "gsdlassocfile",
602 "$afile:$assoc_file_rec->[2]:$dir");
603 } elsif ($self->{'verbosity'} > 2) {
604 print $outhandle "BasPlugout::process couldn't copy the associated file " .
605 "$real_filename to $afile\n";
606 }
607 }
608}
609
610sub set_sortmeta {
611 my $self = shift (@_);
612 my ($sortmeta, $removeprefix, $removesuffix) = @_;
613
614 $self->{'sortmeta'} = $sortmeta;
615 if (defined ($removeprefix) && $removeprefix ) {
616 $removeprefix =~ s/^\^//; # don't need a leading ^
617 $self->{'removeprefix'} = $removeprefix;
618 }
619 if (defined ($removesuffix) && $removesuffix) {
620 $removesuffix =~ s/\$$//; # don't need a trailing $
621 $self->{'removesuffix'} = $removesuffix;
622 }
623}
624
625sub open_xslt_pipe
626{
627 my $self = shift @_;
628 my ($output_file_name, $xslt_file)=@_;
629
630 return unless defined $xslt_file and $xslt_file ne "" and -e $xslt_file;
631
632 my $java_class_path = &util::filename_cat ($ENV{'GSDLHOME'},"bin","java");
633 my $cmd = "| java -cp $java_class_path ApplyXSLT $xslt_file";
634
635 open(*XMLWRITER, $cmd)
636 or die "can't open pipe to xslt: $!";
637
638
639 $self->{'xslt_writer'} = *XMLWRITER;
640
641 print XMLWRITER "<?DocStart?>\n";
642 print XMLWRITER "$output_file_name\n";
643
644 }
645
646
647sub close_xslt_pipe
648{
649 my $self = shift @_;
650
651
652 return unless defined $self->{'xslt_writer'} ;
653
654 my $xsltwriter = $self->{'xslt_writer'};
655
656 print $xsltwriter "<?DocEnd?>\n";
657 close($xsltwriter);
658}
659
660sub close_file_output
661{
662 my ($self) = @_;
663
664 # make sure that the handle has been opened - it won't be if we failed
665 # to import any documents...
666 if (defined(fileno(GROUPPROCESS))) {
667 $self->output_xml_footer('GROUPPROCESS','Archive');
668 close GROUPPROCESS;
669 }
670
671 my $OID = $self->{'gs_OID'};
672 my $short_doc_file = $self->{'short_doc_file'};
673
674 if ($self->{'gzip'}) {
675 my $doc_file = $self->{'gs_filename'};
676 `gzip $doc_file`;
677 $doc_file .= ".gz";
678 $short_doc_file .= ".gz";
679 if (!-e $doc_file) {
680 my $outhandle = $self->{'output_handle'};
681 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
682 return 0;
683 }
684 }
685
686 # store reference in output_info
687 my $output_info = $self->{'output_info'};
688 return 0 if (!defined $output_info);
689 $output_info->add_info($OID, $short_doc_file, undef, undef);
690 return 1;
691}
692
693#the subclass should implement this method if is_group method could return 1.
694sub close_group_output{
695 my $self = shift (@_);
696}
697
698sub is_group {
699 my $self = shift (@_);
700 return 0;
701}
702
7031;
Note: See TracBrowser for help on using the repository browser.