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

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

fixed up groupsize handling, so it should work now

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