source: trunk/gsdl/perllib/plugins/ConvertToPlug.pm@ 10452

Last change on this file since 10452 was 10450, checked in by kjdon, 19 years ago

changed from a dos file to unix file (no hat Ms)

  • Property svn:keywords set to Author Date Id Revision
File size: 16.5 KB
Line 
1###########################################################################
2#
3# ConvertToPlug.pm -- plugin that inherits from BasPlug
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 1999 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27# This plugin is inherited by such plugins as WordPlug, PPTPlug, PSPlug, RTFPlug
28# and PDFPlug. It facilitates the conversion of these document types to either
29# HTML, Text or auto (allow user to choose which format to convert to).
30# It works by dynamically inheriting BasPlug and base on the plugin type in
31# secondary_plugins to devide which format to 'convert_to'. If the argument is
32# not present, the default is to inherit auto.
33package ConvertToPlug;
34
35use BasPlug;
36use ghtml;
37use HTMLPlug;
38use TEXTPlug;
39use PagedImgPlug;
40
41#use strict;
42#no strict 'refs'; # allow filehandles to be variables and viceversa
43
44sub BEGIN {
45 @ConvertToPlug::ISA = ('BasPlug');
46}
47
48my $convert_to_list =
49 [ { 'name' => "auto",
50 'desc' => "{ConvertToPlug.convert_to.auto}" },
51 { 'name' => "html",
52 'desc' => "{ConvertToPlug.convert_to.html}" },
53 { 'name' => "text",
54 'desc' => "{ConvertToPlug.convert_to.text}" },
55 { 'name' => "pagedimg_jpg",
56 'desc' => "{ConvertToPlug.convert_to.pagedimg_jpg}"},
57 { 'name' => "pagedimg_gif",
58 'desc' => "{ConvertToPlug.convert_to.pagedimg_gif}"},
59 { 'name' => "pagedimg_png",
60 'desc' => "{ConvertToPlug.convert_to.pagedimg_png}"},
61 ];
62
63my $arguments =
64 [ { 'name' => "convert_to",
65 'desc' => "{ConvertToPlug.convert_to}",
66 'type' => "enum",
67 'reqd' => "yes",
68 'list' => $convert_to_list,
69 'deft' => "html" },
70 { 'name' => "title_sub",
71 'desc' => "{HTMLPlug.title_sub}",
72 'type' => "string",
73 #'type' => "regexp",
74 'deft' => "" },
75 { 'name' => "use_strings",
76 'desc' => "{ConvertToPlug.use_strings}",
77 'type' => "flag",
78 'reqd' => "no" },
79 { 'name' => "extract_keyphrases",
80 'desc' => "{BasPlug.extract_keyphrases}",
81 'type' => "flag",
82 'reqd' => "no",
83 'hiddengli' => "yes" },
84 { 'name' => "extract_keyphrase_options",
85 'desc' => "{BasPlug.extract_keyphrase_options}",
86 'type' => "string",
87 'reqd' => "no",
88 'hiddengli' => "yes" } ];
89
90my $options = { 'name' => "ConvertToPlug",
91 'desc' => "{ConvertToPlug.desc}",
92 'abstract' => "yes",
93 'inherits' => "yes",
94 'args' => $arguments };
95
96sub findType
97{
98 my ($inputargs) = @_;
99 my ($convert_to_type,$windows_scripting);
100 $conver_to_type = "html";
101 $windows_scripting = "false";
102 for(my $intCounter = 0; $intCounter < scalar(@{$inputargs}) ; $intCounter++)
103 {
104 if($inputargs->[$intCounter] eq "-convert_to")
105 {
106 if($inputargs->[$intCounter+1] eq "auto" || $inputargs->[$intCounter+1] =~ /pagedimg.*/i || $inputargs->[$intCounter+1] eq "text" || $inputargs->[$intCounter+1] eq "html")
107 # if the setting is "auto" then refer to html for now
108 #if($inputargs->[$intCounter+1] =~ /pagedimg.*/i || $inputargs->[$intCounter+1] eq "text" || $inputargs->[$intCounter+1] eq "html")
109 {
110 $conver_to_type = $inputargs->[$intCounter+1];
111 }
112 }
113 if ($inputargs->[$intCounter] eq "-windows_scripting")
114 {
115 $windows_scripting = "true";
116 }
117 }
118 return ($conver_to_type,$windows_scripting);
119}
120
121sub load_secondary_plugins
122{
123 my $self = shift (@_);
124 my ($class,$input_args,$hashArgOptLists) = @_;
125
126 my @convert_to_list = split(",",$self->{'convert_to'});
127 $secondary_plugins = {};
128
129 foreach my $convert_to (@convert_to_list) {
130 # load in "convert_to" plugin package
131 my $plugin_class = $convert_to."Plug";
132 my $plugin_package = $plugin_class.".pm";
133
134 require $plugin_package;
135
136 # call its constructor with extra options that we've worked out!
137 my $arglist = $input_args->{$plugin_class};
138 my $secondary_plugin = new $plugin_class([],$arglist, $hashArgOptLists);
139 $secondary_plugins->{$plugin_class} = $secondary_plugin;
140 }
141 $self->{'secondary_plugins'} = $secondary_plugins;
142}
143
144sub new {
145 my ($class) = shift (@_);
146 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
147 push(@$pluginlist, $class);
148 my $classPluginName = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
149 my ($strConvertTo,$blnWindowsScripting) = findType($inputargs);
150
151 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
152 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
153
154 if ($classPluginName eq "PDFPlug" && $strConvertTo eq "text" &&
155 $ENV{'GSDLOS'} =~ /^windows$/i) {
156 print STDERR "Windows does not support pdf to text. PDFs will be converted to HTML instead\n";
157 $strConvertTo = "html";
158 }
159 my $self = {};
160 my $plugin_class ="";
161 if ($classPluginName eq "WordPlug" && $blnWindowsScripting eq "true" &&
162 $ENV{'GSDLOS'} =~ /^windows$/i) {
163 $plugin_class = "StructuredHTML"."Plug";
164 } elsif ($strConvertTo =~ /pagedimg.*/i){
165 $plugin_class = "PagedImg"."Plug";
166 } else {
167 $plugin_class = uc($strConvertTo)."Plug";
168 }
169
170 my $plugin_package = $plugin_class.".pm";
171 require $plugin_package;
172
173 #call its constructor with extra options that we've worked out!
174 my $arglist = $plugin_options->{$plugin_class};
175 $self = (defined $hashArgOptLists)? new $plugin_class($pluginlist,$inputargs,$hashArgOptLists): new $plugin_class($pluginlist,$inputargs);
176
177 if ($plugin_class eq "StructuredHTMLPlug") {
178 $self->{'convert_to'} = "StructuredHTML";
179 $self->{'convert_to_ext'} = "html";
180# my $structhtml_options = [];
181# push(@$structhtml_options,"-metadata_fields","Title,GENERATOR");
182# $secondary_plugin_options->{'StructuredHTMLPlug'} = $structhtml_options;
183 }
184 elsif ($strConvertTo eq "text")
185 {
186 $self->{'convert_to'} = "TEXT";
187 $self->{'convert_to_ext'} = "txt";
188# my $text_options = [];
189# push(@$text_options,"-metadata_fields","Title,GENERATOR");
190# $secondary_plugin_options->{'TextPlug'} = $text_options;
191 }
192 elsif ($strConvertTo =~ /pagedimg.*/i){
193 $self->{'convert_to'} = "PagedImg";
194 my $convert_to_ext = $strConvertTo;
195 $convert_to_ext =~ s/.*\_(.*)/$1/i;
196 if ($convert_to_ext eq "gif"){
197 $self->{'convert_to_ext'} = "gif";
198 } elsif ($convert_to_ext eq "jpg"){
199 $self->{'convert_to_ext'} = "jpg";
200 } elsif ($convert_to_ext eq "png") {
201 $self->{'convert_to_ext'} = "png";
202 }
203# my $pagedimg_options = [];
204# push(@$pagedimg_options,"-metadata_fields","Title,GENERATOR");
205# $secondary_plugin_options->{'PagedImgPlug'} = $pagedimg_options;
206 } else {
207 # HTML or auto
208 $self = (defined $hashArgOptLists)? new HTMLPlug($pluginlist,$inputargs,$hashArgOptLists): new HTMLPlug($pluginlist,$inputargs);
209 $self->{'convert_to'} = "HTML";
210 $self->{'convert_to_ext'} = "html";
211# my $html_options = [];
212# push(@$html_options,"-rename_assoc_files","1");
213# push(@$html_options,"-metadata_fields","Title,GENERATOR");
214# $secondary_plugin_options->{'HTMLPlug'} = $html_options;
215 }
216 return bless $self, $class;
217}
218
219
220sub init {
221 my $self = shift (@_);
222 my ($verbosity, $outhandle, $failhandle) = @_;
223
224 $self->SUPER::init($verbosity,$outhandle,$failhandle);
225
226 my $secondary_plugins = $self->{'secondary_plugins'};
227
228 foreach my $plug_name (keys %$secondary_plugins) {
229 my $plugin = $secondary_plugins->{$plug_name};
230 $plugin->init($verbosity,$outhandle,$failhandle);
231 }
232}
233
234sub deinit {
235 # called only once, after all plugin passes have been done
236
237 my ($self) = @_;
238
239 my $secondary_plugins = $self->{'secondary_plugins'};
240
241 foreach my $plug_name (keys %$secondary_plugins) {
242 my $plugin = $secondary_plugins->{$plug_name};
243 $plugin->deinit();
244 }
245}
246
247sub convert_post_process
248{
249 # by default do no post processing
250 return;
251}
252
253
254# Run conversion utility on the input file.
255#
256# The conversion takes place in a collection specific 'tmp' directory so
257# that we don't accidentally damage the input.
258#
259# The desired output type is indicated by $output_ext. This is usually
260# something like "html" or "word", but can be "best" (or the empty string)
261# to indicate that the conversion utility should do the best it can.
262sub tmp_area_convert_file {
263 my $self = shift (@_);
264 my ($output_ext, $input_filename, $textref) = @_;
265
266 my $outhandle = $self->{'outhandle'};
267 my $convert_to = $self->{'convert_to'};
268 my $failhandle = $self->{'failhandle'};
269 my $convert_to_ext = $self->{'convert_to_ext'};
270
271 # softlink to collection tmp dir
272 my $tmp_dirname
273 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
274 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
275
276 # derive tmp filename from input filename
277 my ($tailname, $dirname, $suffix)
278 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
279
280 # Remove any white space from filename -- no risk of name collision, and
281 # makes later conversion by utils simpler. Leave spaces in path...
282 # tidy up the filename with space, dot, hyphen between
283 $tailname =~ s/\s+//g;
284 $tailname =~ s/\.+//g;
285 $tailname =~ s/\-+//g;
286 $suffix = lc($suffix);
287 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
288 &util::soft_link($input_filename, $tmp_filename);
289 my $verbosity = $self->{'verbosity'};
290 if ($verbosity > 0) {
291 print $outhandle "Converting $tailname$suffix to $convert_to format\n";
292 }
293
294 my $errlog = &util::filename_cat($tmp_dirname, "err.log");
295
296 # Execute the conversion command and get the type of the result,
297 # making sure the converter gives us the appropriate output type
298 my $output_type="";
299 if ($convert_to =~ m/PagedImg/i) {
300 $output_type = lc($convert_to)."_".lc($convert_to_ext);
301 } else {
302 $output_type = lc($convert_to);
303 }
304
305 my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
306 if (defined $self->{'convert_options'}) {
307 $cmd .= $self->{'convert_options'} . " ";
308 }
309 if ($self->{'use_strings'}) {
310 $cmd .= "-use_strings ";
311 }
312 $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
313
314 $output_type = `$cmd`;
315
316 # remove symbolic link to original file
317 &util::rm($tmp_filename);
318
319 # Check STDERR here
320 chomp $output_type;
321 if ($output_type eq "fail") {
322 print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
323 print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
324 $self->{'num_not_processed'} ++;
325 if (-s "$errlog") {
326 open(ERRLOG, "$errlog");
327 while (<ERRLOG>) {
328 print $outhandle "$_";
329 }
330 print $outhandle "\n";
331 close ERRLOG;
332 }
333 &util::rm("$errlog") if (-e "$errlog");
334 return "";
335 }
336
337 # store the *actual* output type and return the output filename
338 # it's possible we requested conversion to html, but only to text succeeded
339 #$self->{'convert_to_ext'} = $output_type;
340 if ($output_type =~ /html/i) {
341 $self->{'converted_to'} = "HTML";
342 } elsif ($output_type =~ /te?xt/i) {
343 $self->{'converted_to'} = "TEXT";
344 } elsif ($output_type =~ /item/i){
345 $self->{'converted_to'} = "PagedImg";
346 }
347
348 my $output_filename = $tmp_filename;
349 if ($output_type =~ /item/i) {
350 # running under windows
351 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
352 $output_filename = $tmp_dirname . "\\$tailname\\" . $tailname . ".$output_type";
353 } else {
354 $output_filename = $tmp_dirname . "\/$tailname\/" . $tailname . ".$output_type";
355 }
356 } else {
357 $output_filename =~ s/$suffix$/.$output_type/;
358 }
359 return $output_filename;
360}
361
362
363# Remove collection specific tmp directory and all its contents.
364sub cleanup_tmp_area {
365 my $self = shift (@_);
366
367 my $tmp_dirname
368 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
369 &util::rm_r($tmp_dirname);
370 &util::mk_dir($tmp_dirname);
371}
372
373# Override BasPlug read
374# We don't want to get language encoding stuff until after we've converted
375# our file to either TEXT or HTML or PagedImage.
376sub read {
377 my $self = shift (@_);
378 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
379 #if ($self->is_recursive()) {
380 # die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
381 # }
382
383 my $outhandle = $self->{'outhandle'};
384
385 my ($block_status,$filename) = $self->read_block(@_);
386 return $block_status if ((!defined $block_status) || ($block_status==0));
387 $file = $self->read_tidy_file($file);
388
389 my $output_ext = $self->{'convert_to_ext'};
390 my $conv_filename = "";
391 $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
392
393 if ("$conv_filename" eq "") {return 0;} # allows continue on errors
394 if (! -e "$conv_filename") {return 0;} # allows continue on errors
395 $self->{'conv_filename'} = $conv_filename;
396 $self->convert_post_process($conv_filename);
397
398 my $secondary_plugins = $self->{'secondary_plugins'};
399 my $num_secondary_plugins = scalar(keys %$secondary_plugins);
400
401 if ($num_secondary_plugins == 0) {
402 print $outhandle "Warning: No secondary plugin to use in conversion. Skipping $file\n";
403 return 0; # effectively block it
404 }
405
406 my @plugin_names = keys %$secondary_plugins;
407 my $plugin_name = shift @plugin_names;
408
409 if ($num_secondary_plugins > 1) {
410 print $outhandle "Warning: Multiple secondary plugins not supported yet! Choosing $plugin_name\n.";
411 }
412
413 my $secondary_plugin = $secondary_plugins->{$plugin_name};
414
415 # note: metadata is not carried on to the next level
416 my ($rv,$doc_obj)
417 = $secondary_plugin->read_into_doc_obj ($pluginfo,"", $conv_filename,
418 $metadata, $processor, $maxdocs, $total_count,
419 $gli);
420
421 if ((!defined $rv) || ($rv<1)) {
422 # wasn't processed
423 return $rv;
424 }
425
426 # Override previous gsdlsourcefilename set by secondary plugin
427 my $collect_file = &util::filename_within_collection($filename);
428 my $collect_conv_file = &util::filename_within_collection($conv_filename);
429 $doc_obj->set_source_filename ($collect_file);
430 $doc_obj->set_converted_filename($collect_conv_file);
431
432 my ($filemeta) = $file =~ /([^\\\/]+)$/;
433 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
434 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
435 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename));
436
437 # do plugin specific processing of doc_obj
438 unless (defined ($self->process(undef, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
439 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
440 return -1;
441 }
442 # do any automatic metadata extraction
443 $self->auto_extract_metadata ($doc_obj);
444 # add an OID
445 $doc_obj->set_OID();
446 # process the document
447 $processor->process($doc_obj);
448 ##$self->cleanup_tmp_area();
449
450 $self->{'num_processed'} ++;
451
452 return 1;
453}
454
455
456# do plugin specific processing of doc_obj for doc_ext type
457sub process_type {
458 my $self = shift (@_);
459 my ($doc_ext, $base_dir, $file, $doc_obj) = @_;
460
461 # associate original file with doc object
462 my $cursection = $doc_obj->get_top_section();
463 my $filename = &util::filename_cat($base_dir, $file);
464 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
465
466 my $file_type;
467
468 if ($doc_ext eq "doc") {
469 $file_type = "Word";
470 } elsif ($doc_ext eq "xls") {
471 $file_type = "Excel";
472 } elsif ($doc_ext eq "ppt") {
473 $file_type = "PPT";
474 } elsif ($doc_ext eq "pdf") {
475 $file_type = "PDF";
476 } elsif ($doc_ext eq "rtf") {
477 $file_type = "RTF";
478 } elsif ($doc_ext eq "ps") {
479 $file_type = "PS";
480 }
481
482 my $file_format = $file_type || "unknown";
483
484 # We use set instead of add here because we only want one value
485 $doc_obj->set_utf8_metadata_element($cursection, "FileFormat", $file_format);
486
487 my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext\">";
488 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
489 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
490 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
491
492 return 1;
493}
494
4951;
496
497
498
499
500
501
502
Note: See TracBrowser for help on using the repository browser.