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

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

fixed a bug with -convert_to auto handling

  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 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
41use strict;
42no 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 ];
56
57my $arguments =
58 [ { 'name' => "convert_to",
59 'desc' => "{ConvertToPlug.convert_to}",
60 'type' => "enum",
61 'reqd' => "yes",
62 'list' => $convert_to_list,
63 'deft' => "html" },
64 { 'name' => "title_sub",
65 'desc' => "{HTMLPlug.title_sub}",
66 'type' => "string",
67 #'type' => "regexp",
68 'deft' => "" },
69 { 'name' => "use_strings",
70 'desc' => "{ConvertToPlug.use_strings}",
71 'type' => "flag",
72 'reqd' => "no" },
73 { 'name' => "extract_keyphrases",
74 'desc' => "{BasPlug.extract_keyphrases}",
75 'type' => "flag",
76 'reqd' => "no",
77 'hiddengli' => "yes" },
78 { 'name' => "extract_keyphrase_options",
79 'desc' => "{BasPlug.extract_keyphrase_options}",
80 'type' => "string",
81 'reqd' => "no",
82 'hiddengli' => "yes" } ];
83
84my $options = { 'name' => "ConvertToPlug",
85 'desc' => "{ConvertToPlug.desc}",
86 'abstract' => "yes",
87 'inherits' => "yes",
88 'args' => $arguments };
89
90sub findType
91{
92 my ($inputargs) = @_;
93 my $convert_to_type = "html";
94 my $windows_scripting = "false";
95 for(my $intCounter = 0; $intCounter < scalar(@{$inputargs}) ; $intCounter++)
96 {
97 if($inputargs->[$intCounter] eq "-convert_to")
98 {
99 if($inputargs->[$intCounter+1] eq "auto" || $inputargs->[$intCounter+1] =~ /pagedimg.*/i || $inputargs->[$intCounter+1] eq "text" || $inputargs->[$intCounter+1] eq "html")
100 {
101 $convert_to_type = $inputargs->[$intCounter+1];
102 }
103 }
104 if ($inputargs->[$intCounter] eq "-windows_scripting")
105 {
106 $windows_scripting = "true";
107 }
108 }
109 return ($convert_to_type,$windows_scripting);
110}
111
112sub load_secondary_plugins
113{
114 my $self = shift (@_);
115 my ($class,$input_args,$hashArgOptLists) = @_;
116
117 my @convert_to_list = split(",",$self->{'convert_to'});
118 my $secondary_plugins = {};
119
120 foreach my $convert_to (@convert_to_list) {
121 # load in "convert_to" plugin package
122 my $plugin_class = $convert_to."Plug";
123 my $plugin_package = $plugin_class.".pm";
124
125 require $plugin_package;
126
127 # call its constructor with extra options that we've worked out!
128 my $arglist = $input_args->{$plugin_class};
129 my $secondary_plugin = new $plugin_class([],$arglist, $hashArgOptLists);
130 $secondary_plugins->{$plugin_class} = $secondary_plugin;
131 }
132 $self->{'secondary_plugins'} = $secondary_plugins;
133}
134
135sub new {
136 my ($class) = shift (@_);
137 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
138 push(@$pluginlist, $class);
139 my $classPluginName = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
140 my ($strConvertTo,$blnWindowsScripting) = findType($inputargs);
141
142 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
143 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
144
145 my $self = (defined $hashArgOptLists)? new BasPlug($pluginlist,$inputargs,$hashArgOptLists): new BasPlug($pluginlist,$inputargs);
146
147
148 if ($classPluginName eq "PDFPlug") {
149 if ($strConvertTo eq "text" &&
150 $ENV{'GSDLOS'} =~ /^windows$/i) {
151 print STDERR "Windows does not support pdf to text. PDFs will be converted to HTML instead\n";
152 $strConvertTo = "html";
153 }
154 } elsif ($classPluginName eq "WordPlug") {
155 if ($blnWindowsScripting eq "true" && $ENV{'GSDLOS'} =~ /^windows$/i && $strConvertTo =~ /^(html|auto)$/) {
156 # we use structured HTML, not normal html
157 $strConvertTo = "structuredhtml";
158 }
159 }
160 elsif ($classPluginName eq "PPTPlug") {
161 if ($blnWindowsScripting eq "true" && $ENV{'GSDLOS'} =~ /^windows$/i && $strConvertTo eq "auto") {
162 # we use paged img
163 $strConvertTo = "pagedimg_jpg";
164 }
165 }
166
167 if ($strConvertTo eq "auto") {
168 # choose html for now - should choose a format based on doc type
169 $strConvertTo = "html";
170 }
171
172 if ($strConvertTo eq "html") {
173 $self->{'convert_to'} = "HTML";
174 $self->{'convert_to_ext'} = "html";
175 } elsif ($strConvertTo eq "text") {
176 $self->{'convert_to'} = "TEXT";
177 $self->{'convert_to_ext'} = "txt";
178 } elsif ($strConvertTo eq "structuredhtml") {
179 $self->{'convert_to'} = "StructuredHTML";
180 $self->{'convert_to_ext'} = "html";
181 } elsif ($strConvertTo =~ /^pagedimg/) {
182 $self->{'convert_to'} = "PagedImg";
183 my ($convert_to_ext) = $strConvertTo =~ /pagedimg\_(jpg|gif|png)/i;
184 $convert_to_ext = 'jpg' unless defined $convert_to_ext;
185 $self->{'convert_to_ext'} = $convert_to_ext;
186 }
187
188 return bless $self, $class;
189}
190
191
192sub init {
193 my $self = shift (@_);
194 my ($verbosity, $outhandle, $failhandle) = @_;
195
196 $self->SUPER::init($verbosity,$outhandle,$failhandle);
197
198 my $secondary_plugins = $self->{'secondary_plugins'};
199
200 foreach my $plug_name (keys %$secondary_plugins) {
201 my $plugin = $secondary_plugins->{$plug_name};
202 $plugin->init($verbosity,$outhandle,$failhandle);
203 }
204}
205
206sub deinit {
207 # called only once, after all plugin passes have been done
208
209 my ($self) = @_;
210
211 my $secondary_plugins = $self->{'secondary_plugins'};
212
213 foreach my $plug_name (keys %$secondary_plugins) {
214 my $plugin = $secondary_plugins->{$plug_name};
215 $plugin->deinit();
216 }
217}
218
219sub convert_post_process
220{
221 # by default do no post processing
222 return;
223}
224
225
226# Run conversion utility on the input file.
227#
228# The conversion takes place in a collection specific 'tmp' directory so
229# that we don't accidentally damage the input.
230#
231# The desired output type is indicated by $output_ext. This is usually
232# something like "html" or "word", but can be "best" (or the empty string)
233# to indicate that the conversion utility should do the best it can.
234sub tmp_area_convert_file {
235 my $self = shift (@_);
236 my ($output_ext, $input_filename, $textref) = @_;
237
238 my $outhandle = $self->{'outhandle'};
239 my $convert_to = $self->{'convert_to'};
240 my $failhandle = $self->{'failhandle'};
241 my $convert_to_ext = $self->{'convert_to_ext'};
242
243 # softlink to collection tmp dir
244 my $tmp_dirname
245 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
246 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
247
248 # derive tmp filename from input filename
249 my ($tailname, $dirname, $suffix)
250 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
251
252 # Remove any white space from filename -- no risk of name collision, and
253 # makes later conversion by utils simpler. Leave spaces in path...
254 # tidy up the filename with space, dot, hyphen between
255 $tailname =~ s/\s+//g;
256 $tailname =~ s/\.+//g;
257 $tailname =~ s/\-+//g;
258 $suffix = lc($suffix);
259 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
260 &util::soft_link($input_filename, $tmp_filename);
261 my $verbosity = $self->{'verbosity'};
262 if ($verbosity > 0) {
263 print $outhandle "Converting $tailname$suffix to $convert_to format\n";
264 }
265
266 my $errlog = &util::filename_cat($tmp_dirname, "err.log");
267
268 # Execute the conversion command and get the type of the result,
269 # making sure the converter gives us the appropriate output type
270 my $output_type="";
271 if ($convert_to =~ m/PagedImg/i) {
272 $output_type = lc($convert_to)."_".lc($convert_to_ext);
273 } else {
274 $output_type = lc($convert_to);
275 }
276
277 my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
278 if (defined $self->{'convert_options'}) {
279 $cmd .= $self->{'convert_options'} . " ";
280 }
281 if ($self->{'use_strings'}) {
282 $cmd .= "-use_strings ";
283 }
284 $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
285
286 $output_type = `$cmd`;
287
288 # remove symbolic link to original file
289 &util::rm($tmp_filename);
290
291 # Check STDERR here
292 chomp $output_type;
293 if ($output_type eq "fail") {
294 print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
295 print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
296 $self->{'num_not_processed'} ++;
297 if (-s "$errlog") {
298 open(ERRLOG, "$errlog");
299 while (<ERRLOG>) {
300 print $outhandle "$_";
301 }
302 print $outhandle "\n";
303 close ERRLOG;
304 }
305 &util::rm("$errlog") if (-e "$errlog");
306 return "";
307 }
308
309 # store the *actual* output type and return the output filename
310 # it's possible we requested conversion to html, but only to text succeeded
311 #$self->{'convert_to_ext'} = $output_type;
312 if ($output_type =~ /html/i) {
313 $self->{'converted_to'} = "HTML";
314 } elsif ($output_type =~ /te?xt/i) {
315 $self->{'converted_to'} = "TEXT";
316 } elsif ($output_type =~ /item/i){
317 $self->{'converted_to'} = "PagedImg";
318 }
319
320 my $output_filename = $tmp_filename;
321 if ($output_type =~ /item/i) {
322 # running under windows
323 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
324 $output_filename = $tmp_dirname . "\\$tailname\\" . $tailname . ".$output_type";
325 } else {
326 $output_filename = $tmp_dirname . "\/$tailname\/" . $tailname . ".$output_type";
327 }
328 } else {
329 $output_filename =~ s/$suffix$/.$output_type/;
330 }
331 return $output_filename;
332}
333
334
335# Override BasPlug read
336# We don't want to get language encoding stuff until after we've converted
337# our file to either TEXT or HTML or PagedImage.
338sub read {
339 my $self = shift (@_);
340 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
341 #if ($self->is_recursive()) {
342 # die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
343 # }
344
345 my $outhandle = $self->{'outhandle'};
346
347 my ($block_status,$filename) = $self->read_block(@_);
348 return $block_status if ((!defined $block_status) || ($block_status==0));
349 $file = $self->read_tidy_file($file);
350
351 my $output_ext = $self->{'convert_to_ext'};
352 my $conv_filename = "";
353 $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
354
355 if ("$conv_filename" eq "") {return 0;} # allows continue on errors
356 if (! -e "$conv_filename") {return 0;} # allows continue on errors
357 $self->{'conv_filename'} = $conv_filename;
358 $self->convert_post_process($conv_filename);
359
360 my $secondary_plugins = $self->{'secondary_plugins'};
361 my $num_secondary_plugins = scalar(keys %$secondary_plugins);
362
363 if ($num_secondary_plugins == 0) {
364 print $outhandle "Warning: No secondary plugin to use in conversion. Skipping $file\n";
365 return 0; # effectively block it
366 }
367
368 my @plugin_names = keys %$secondary_plugins;
369 my $plugin_name = shift @plugin_names;
370
371 if ($num_secondary_plugins > 1) {
372 print $outhandle "Warning: Multiple secondary plugins not supported yet! Choosing $plugin_name\n.";
373 }
374
375 my $secondary_plugin = $secondary_plugins->{$plugin_name};
376
377 # note: metadata is not carried on to the next level
378 my ($rv,$doc_obj)
379 = $secondary_plugin->read_into_doc_obj ($pluginfo,"", $conv_filename,
380 $metadata, $processor, $maxdocs, $total_count,
381 $gli);
382
383 if ((!defined $rv) || ($rv<1)) {
384 # wasn't processed
385 return $rv;
386 }
387
388 # Override previous gsdlsourcefilename set by secondary plugin
389 my $collect_file = &util::filename_within_collection($filename);
390 my $collect_conv_file = &util::filename_within_collection($conv_filename);
391 $doc_obj->set_source_filename ($collect_file);
392 $doc_obj->set_converted_filename($collect_conv_file);
393
394 my ($filemeta) = $file =~ /([^\\\/]+)$/;
395 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
396 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
397 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename));
398
399 # do plugin specific processing of doc_obj
400 unless (defined ($self->process(undef, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
401 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
402 return -1;
403 }
404 # do any automatic metadata extraction
405 $self->auto_extract_metadata ($doc_obj);
406 # add an OID
407 $doc_obj->set_OID();
408 # process the document
409 $processor->process($doc_obj);
410
411 $self->{'num_processed'} ++;
412
413 return 1;
414}
415
416
417# do plugin specific processing of doc_obj for doc_ext type
418sub process_type {
419 my $self = shift (@_);
420 my ($doc_ext, $base_dir, $file, $doc_obj) = @_;
421
422 # associate original file with doc object
423 my $cursection = $doc_obj->get_top_section();
424 my $filename = &util::filename_cat($base_dir, $file);
425 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
426
427 my $file_type;
428
429 if ($doc_ext eq "doc") {
430 $file_type = "Word";
431 } elsif ($doc_ext eq "xls") {
432 $file_type = "Excel";
433 } elsif ($doc_ext eq "ppt") {
434 $file_type = "PPT";
435 } elsif ($doc_ext eq "pdf") {
436 $file_type = "PDF";
437 } elsif ($doc_ext eq "rtf") {
438 $file_type = "RTF";
439 } elsif ($doc_ext eq "ps") {
440 $file_type = "PS";
441 }
442
443 my $file_format = $file_type || "unknown";
444
445 # We use set instead of add here because we only want one value
446 $doc_obj->set_utf8_metadata_element($cursection, "FileFormat", $file_format);
447
448 my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext\">";
449 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
450 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
451 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
452
453 return 1;
454}
455
4561;
457
458
459
460
461
462
463
Note: See TracBrowser for help on using the repository browser.