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

Last change on this file since 12330 was 12330, checked in by shaoqun, 18 years ago

plugouts modules

  • Property svn:keywords set to Author Date Id Revision
File size: 18.4 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';
32
33use gsprintf 'gsprintf';
34use printusage;
35
36# suppress the annoying "subroutine redefined" warning that various
37# gets cause under perl 5.6
38$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
39
40my $arguments = [
41 { 'name' => "group_size",
42 'desc' => "{BasPlugout.group_size}",
43 'type' => "int",
44 'deft' => "1",
45 'reqd' => "no",
46 'hiddengli' => "no"},
47 { 'name' => "output_info",
48 'desc' => "{BasPlugout.output_info}",
49 'type' => "string",
50 'reqd' => "yes",
51 'hiddengli' => "yes"},
52 { 'name' => "xslt_file",
53 'desc' => "{BasPlugout.xslt_file}",
54 'type' => "string",
55 'reqd' => "no",
56 'hiddengli' => "no"},
57 { 'name' => "output_handle",
58 'desc' => "{BasPlugout.output_handle}",
59 'type' => "string",
60 'deft' => 'STDERR',
61 'reqd' => "no",
62 'hiddengli' => "yes"},
63 { 'name' => "verbosity",
64 'desc' => "{BasPlugout.verbosity}",
65 'type' => "int",
66 'deft' => "0",
67 'reqd' => "no",
68 'hiddengli' => "no"},
69 { 'name' => "gzip_output",
70 'desc' => "{BasPlugout.gzip_output}",
71 'type' => "flag",
72 'reqd' => "no",
73 'hiddengli' => "no"}
74];
75
76my $options = { 'name' => "BasPlugout",
77 'desc' => "{BasPlugout.desc}",
78 'abstract' => "yes",
79 'inherits' => "no",
80 'args' => $arguments};
81
82sub new
83{
84 my $class = shift (@_);
85
86 my ($plugoutlist,$args,$hashArgOptLists) = @_;
87
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 sort_metadata{
391 my $self = shift (@_);
392 my ($doc_obj) = @_;
393
394 # do the sortmeta thing
395 my $metadata = $doc_obj->get_metadata_element($doc_obj->get_top_section(),$self->{'sortmeta'})
396 if defined $self->{'sortmeta'};
397
398 if ($metadata) {
399 # do remove prefix/suffix
400 $metadata =~ s/^$self->{'removeprefix'}// if defined $self->{'removeprefix'};
401 $metadata =~ s/$self->{'removesuffix'}$// if defined $self->{'removesuffix'};
402
403 $metadata = &sorttools::format_metadata_for_sorting($self->{'sortmeta'}, $metadata, $doc_obj);
404 }
405
406 my $output_info = $self->{'output_info'};
407
408 # store reference in the output_info
409 $output_info->add_info($doc_obj->get_OID(),$self->{'short_doc_file'}, undef, $metadata);
410
411}
412
413sub group_process {
414
415 my $self = shift (@_);
416 my ($doc_obj) = @_;
417
418 my $OID = $doc_obj->get_OID();
419 $OID = "NULL" unless defined $OID;
420
421 my $groupsize = $self->{'group_size'};
422 my $gs_count = $self->{'gs_count'};
423 my $open_new_file = (($gs_count % $groupsize)==0);
424 my $outhandle = $self->{'output_handle'};
425
426 # opening a new file, or document has assoicated files => directory needed
427 if (($open_new_file) || (scalar(@{$doc_obj->get_assoc_files()})>0))
428 {
429 # The directory the archive file (doc.xml) and all associated files
430 # should end up in
431 my $doc_dir;
432 # If we've determined its time for a new file, open it now
433 if ($open_new_file || !defined($self->{'gs_doc_dir'}))
434 {
435 $doc_dir = $self->get_doc_dir ($OID, $doc_obj->get_source_filename());
436 # only if opening new file
437 my $output_dir = $self->get_output_dir();
438 &util::mk_all_dir ($output_dir) unless -e $output_dir;
439
440 my $doc_file = &util::filename_cat ($output_dir, $doc_dir, "doc.xml");
441 my $short_doc_file = &util::filename_cat ($doc_dir, "doc.xml");
442
443 if ($gs_count>0)
444 {
445 return if (!$self->close_file_output());
446 }
447
448 open (GROUPPROCESS, ">$doc_file") or (print $outhandle "BasPlugout::group_process could not write to file $doc_file\n" and return);
449
450
451 $self->{'gs_filename'} = $doc_file;
452 $self->{'gs_short_filename'} = $short_doc_file;
453 $self->{'gs_OID'} = $OID;
454 $self->{'gs_doc_dir'} = $doc_dir;
455
456 $self->output_xml_header(GROUPPROCESS,'Archive');
457 }
458 # Otherwise load the same archive document directory used last time
459 else
460 {
461 $doc_dir = $self->{'gs_doc_dir'};
462 }
463
464 # copy all the associated files, add this information as metadata
465 # to the document
466 print STDERR "Writing associated files to $doc_dir\n";
467 $self->process_assoc_files ($doc_obj, $doc_dir);
468 }
469
470 # save this document
471 $doc_obj->output_section(GROUPPROCESS, $doc_obj->get_top_section());
472
473 $self->{'gs_count'}++;
474}
475
476
477sub saveas {
478 my $self = shift (@_);
479
480 die "Basplug::saveas function must be implemented in sub classes\n";
481}
482
483sub get_doc_dir {
484 my $self = shift (@_);
485 my ($OID, $source_filename) = @_;
486
487 my $working_dir = $self->get_output_dir();
488 my $working_info = $self->{output_info};
489 return if (!defined $working_info);
490
491 my $doc_info = $working_info->get_info($OID);
492 my $doc_dir = '';
493
494 if (defined $doc_info && scalar(@$doc_info) >= 1) {
495 # this OID already has an assigned directory, use the
496 # same one.
497 $doc_dir = $doc_info->[0];
498 $doc_dir =~ s/\/?((doc(mets)?)|(dublin_core))\.xml(\.gz)?$//;
499 } elsif ($self->{'keep_import_structure'}) {
500 $source_filename = &File::Basename::dirname($source_filename);
501 $source_filename =~ s/[\\\/]+/\//g;
502 $source_filename =~ s/\/$//;
503
504 $doc_dir = substr($source_filename, length($ENV{'GSDLIMPORTDIR'}) + 1);
505
506 }
507
508 # have to get a new document directory
509 $doc_dir = $self->get_new_doc_dir($working_info,$working_dir,$OID) unless $doc_dir ne "";
510
511 $doc_dir .= ".dir";
512
513 return $doc_dir;
514}
515
516sub get_new_doc_dir{
517 my $self = shift (@_);
518 my($working_info,$working_dir,$OID) = @_;
519
520 my $doc_dir = "";
521 my $doc_dir_rest = $OID;
522 my $doc_dir_num = 0;
523
524 do {
525 $doc_dir .= "/" if $doc_dir_num > 0;
526 if ($doc_dir_rest =~ s/^(.{1,8})//) {
527 $doc_dir .= $1;
528 $doc_dir_num++;
529 }
530 } while ($doc_dir_rest ne "" &&
531 ((-d &util::filename_cat ($working_dir, "$doc_dir.dir")) ||
532 ($working_info->size() >= 1024 && $doc_dir_num < 2)));
533
534
535 return $doc_dir;
536}
537
538sub process_assoc_files {
539 my $self = shift (@_);
540 my ($doc_obj, $doc_dir, $handle) = @_;
541
542 my $outhandle = $self->{'output_handle'};
543
544 my $output_dir = $self->get_output_dir();
545 return if (!defined $output_dir);
546
547 &util::mk_all_dir ($output_dir) unless -e $output_dir;
548
549 my $working_dir = &util::filename_cat($output_dir, $doc_dir);
550 &util::mk_all_dir ($working_dir) unless -e $working_dir;
551
552 my @assoc_files = ();
553 my $filename;;
554
555 my $source_filename = $doc_obj->get_source_filename();
556
557 my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
558
559 if (defined $collect_dir) {
560 my $dirsep_regexp = &util::get_os_dirsep();
561
562 if ($collect_dir !~ /$dirsep_regexp$/) {
563 $collect_dir .= &util::get_dirsep(); # ensure there is a slash at the end
564 }
565
566 # This test is never going to fail on Windows -- is this a problem?
567
568 if ($source_filename !~ /^$dirsep_regexp/) {
569 $source_filename = &util::filename_cat($collect_dir, $source_filename);
570 }
571 }
572
573
574 # set the assocfile path (even if we have no assoc files - need this for lucene)
575 $doc_obj->set_utf8_metadata_element ($doc_obj->get_top_section(),
576 "assocfilepath",
577 "$doc_dir");
578 foreach my $assoc_file_rec (@{$doc_obj->get_assoc_files()}) {
579 my ($dir, $afile) = $assoc_file_rec->[1] =~ /^(.*?)([^\/\\]+)$/;
580 $dir = "" unless defined $dir;
581
582
583 my $real_filename = $assoc_file_rec->[0];
584 # for some reasons the image associate file has / before the full path
585 $real_filename =~ s/^\\(.*)/$1/i;
586 if (-e $real_filename) {
587
588 $filename = &util::filename_cat($working_dir, $afile);
589
590 &util::hard_link ($real_filename, $filename);
591
592 $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
593 "gsdlassocfile",
594 "$afile:$assoc_file_rec->[2]:$dir");
595 } elsif ($self->{'verbosity'} > 2) {
596 print $outhandle "BasPlugout::process couldn't copy the associated file " .
597 "$real_filename to $afile\n";
598 }
599 }
600}
601
602sub set_sortmeta {
603 my $self = shift (@_);
604 my ($sortmeta, $removeprefix, $removesuffix) = @_;
605
606 $self->{'sortmeta'} = $sortmeta;
607 if (defined ($removeprefix) && $removeprefix ) {
608 $removeprefix =~ s/^\^//; # don't need a leading ^
609 $self->{'removeprefix'} = $removeprefix;
610 }
611 if (defined ($removesuffix) && $removesuffix) {
612 $removesuffix =~ s/\$$//; # don't need a trailing $
613 $self->{'removesuffix'} = $removesuffix;
614 }
615}
616
617sub open_xslt_pipe
618{
619 my $self = shift @_;
620 my ($output_file_name, $xslt_file)=@_;
621
622 return unless defined $xslt_file and $xslt_file ne "" and -e $xslt_file;
623
624 my $java_class_path = &util::filename_cat ($ENV{'GSDLHOME'},"bin","java");
625 my $cmd = "| java -cp $java_class_path ApplyXSLT $xslt_file";
626
627 open(*XMLWRITER, $cmd)
628 or die "can't open pipe to xslt: $!";
629
630
631 $self->{'xslt_writer'} = *XMLWRITER;
632
633 print XMLWRITER "<?DocStart?>\n";
634 print XMLWRITER "$output_file_name\n";
635
636 }
637
638
639sub close_xslt_pipe
640{
641 my $self = shift @_;
642
643
644 return unless defined $self->{'xslt_writer'} ;
645
646 my $xsltwriter = $self->{'xslt_writer'};
647
648 print $xsltwriter "<?DocEnd?>\n";
649 close($xsltwriter);
650}
651
652sub close_file_output
653{
654 my ($self) = @_;
655
656 # make sure that the handle has been opened - it won't be if we failed
657 # to import any documents...
658 if (defined(fileno(GROUPPROCESS))) {
659 $self->output_xml_footer('GROUPPROCESS','Archive');
660 close GROUPPROCESS;
661 }
662
663 my $OID = $self->{'gs_OID'};
664 my $short_doc_file = $self->{'short_doc_file'};
665
666 if ($self->{'gzip'}) {
667 my $doc_file = $self->{'gs_filename'};
668 `gzip $doc_file`;
669 $doc_file .= ".gz";
670 $short_doc_file .= ".gz";
671 if (!-e $doc_file) {
672 my $outhandle = $self->{'output_handle'};
673 print $outhandle "error while gzipping: $doc_file doesn't exist\n";
674 return 0;
675 }
676 }
677
678 # store reference in output_info
679 my $output_info = $self->{'output_info'};
680 return 0 if (!defined $output_info);
681 $output_info->add_info($OID, $short_doc_file, undef, undef);
682 return 1;
683}
684
685#the subclass should implement this method if is_group method could return 1.
686sub close_group_output{
687 my $self = shift (@_);
688}
689
690sub is_group {
691 my $self = shift (@_);
692 return 0;
693}
694
6951;
Note: See TracBrowser for help on using the repository browser.