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

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

sort_metadata renamed to store_output_info_reference, and modified the sortmeta stuff to use a comma separated list

  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 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 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
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->{'gs_short_filename'} = $short_doc_file;
461 $self->{'gs_OID'} = $OID;
462 $self->{'gs_doc_dir'} = $doc_dir;
463
464 $self->output_xml_header(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(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
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.