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

Last change on this file since 15116 was 15116, checked in by ak19, 16 years ago
  1. Doesn't presume that GSDLCOLLECT env var is set. 2. Sub tmp_area_convert_file tries GSDLCOLLECT folder, else goes to GSDLHOME folder to create tmp folder in
  • Property svn:keywords set to Author Date Id Revision
File size: 17.1 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,
28# RTFPlug and PDFPlug. It facilitates the conversion of these document types
29# to either HTML, TEXT or a series of images. It works by dynamically loading
30# an appropriate secondary plugin (HTMLPlug, StructuredHTMLPlug,
31# PagedImgPlug or TEXTPlug) based on the plugin argument 'convert_to'.
32
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
43no strict 'subs';
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' => "auto" },
64 { 'name' => "keep_original_filename",
65 'desc' => "{ConvertToPlug.keep_original_filename}",
66 'type' => "flag" },
67 { 'name' => "title_sub",
68 'desc' => "{HTMLPlug.title_sub}",
69 'type' => "string",
70 #'type' => "regexp",
71 'deft' => "" },
72 { 'name' => "apply_fribidi",
73 'desc' => "{ConvertToPlug.apply_fribidi}",
74 'type' => "flag",
75 'reqd' => "no" },
76 { 'name' => "use_strings",
77 'desc' => "{ConvertToPlug.use_strings}",
78 'type' => "flag",
79 'reqd' => "no" },
80 { 'name' => "extract_keyphrases",
81 'desc' => "{BasPlug.extract_keyphrases}",
82 'type' => "flag",
83 'reqd' => "no",
84 'hiddengli' => "yes" },
85 { 'name' => "extract_keyphrase_options",
86 'desc' => "{BasPlug.extract_keyphrase_options}",
87 'type' => "string",
88 'reqd' => "no",
89 'hiddengli' => "yes" } ];
90
91my $options = { 'name' => "ConvertToPlug",
92 'desc' => "{ConvertToPlug.desc}",
93 'abstract' => "yes",
94 'inherits' => "yes",
95 'args' => $arguments };
96
97
98sub load_secondary_plugins
99{
100 my $self = shift (@_);
101 my ($class,$input_args,$hashArgOptLists) = @_;
102
103 my @convert_to_list = split(",",$self->{'convert_to'});
104 my $secondary_plugins = {};
105 # find the plugin
106
107 foreach my $convert_to (@convert_to_list) {
108 # load in "convert_to" plugin package
109 my $plugin_class = $convert_to."Plug";
110 my $plugin_package = $plugin_class.".pm";
111
112 my $colplugname = undef;
113 if (defined $ENV{'GSDLCOLLECTDIR'}) {
114 $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},
115 "perllib","plugins",
116 $plugin_package);
117 }
118
119 my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'},
120 "perllib","plugins",
121 $plugin_package);
122
123 if ((defined $colplugname) && (-e $colplugname)) { require $colplugname;}
124 elsif (-e $mainplugname) { require $mainplugname; }
125 else {
126 &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
127 $plugin_class);
128 die "\n";
129 }
130
131 # call its constructor with extra options that we've worked out!
132 my $arglist = $input_args->{$plugin_class};
133
134 my ($secondary_plugin);
135 eval("\$secondary_plugin = new $plugin_class([],\$arglist)");
136 die "$@" if $@;
137 $secondary_plugins->{$plugin_class} = $secondary_plugin;
138 }
139 $self->{'secondary_plugins'} = $secondary_plugins;
140}
141
142sub new {
143 my ($class) = shift (@_);
144 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
145 push(@$pluginlist, $class);
146 my $classPluginName = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
147 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
148 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
149
150 my $self = new BasPlug($pluginlist, $inputargs, $hashArgOptLists);
151
152 if ($self->{'info_only'}) {
153 # don't worry about any options etc
154 return bless $self, $class;
155 }
156
157 my $convert_to_type = $self->{'convert_to'};
158 if (!defined $convert_to_type || $convert_to_type eq "") {
159 $convert_to_type = "auto";
160 }
161 my $windows_scripting = $self->{'windows_scripting'};
162 $windows_scripting = 0 unless defined $windows_scripting;
163 if ($classPluginName eq "PDFPlug") {
164 if ($convert_to_type eq "text" &&
165 $ENV{'GSDLOS'} =~ /^windows$/i) {
166 print STDERR "Windows does not support pdf to text. PDFs will be converted to HTML instead\n";
167 $convert_to_type = "html";
168 }
169 } elsif ($classPluginName eq "WordPlug") {
170 if ($windows_scripting && $ENV{'GSDLOS'} =~ /^windows$/i && $convert_to_type =~ /^(html|auto)$/) {
171 # we use structured HTML, not normal html
172 $convert_to_type = "structuredhtml";
173 }
174 } elsif ($classPluginName eq "PPTPlug") {
175 if ($windows_scripting && $ENV{'GSDLOS'} =~ /^windows$/i && $convert_to_type eq "auto") {
176 # we use paged img
177 $convert_to_type = "pagedimg_jpg";
178 }
179 } elsif ($classPluginName eq "PSPlug") {
180 if ($convert_to_type eq "auto") {
181 # we use text
182 $convert_to_type = "text";
183 }
184 }
185
186 if ($convert_to_type eq "auto") {
187 # choose html for now - should choose a format based on doc type
188 $convert_to_type = "html";
189 }
190
191 if ($convert_to_type eq "html") {
192 $self->{'convert_to'} = "HTML";
193 $self->{'convert_to_ext'} = "html";
194 } elsif ($convert_to_type eq "text") {
195 $self->{'convert_to'} = "TEXT";
196 $self->{'convert_to_ext'} = "txt";
197 } elsif ($convert_to_type eq "structuredhtml") {
198 $self->{'convert_to'} = "StructuredHTML";
199 $self->{'convert_to_ext'} = "html";
200 } elsif ($convert_to_type =~ /^pagedimg/) {
201 $self->{'convert_to'} = "PagedImg";
202 my ($convert_to_ext) = $convert_to_type =~ /pagedimg\_(jpg|gif|png)/i;
203 $convert_to_ext = 'jpg' unless defined $convert_to_ext;
204 $self->{'convert_to_ext'} = $convert_to_ext;
205 }
206
207 return bless $self, $class;
208}
209
210
211sub init {
212 my $self = shift (@_);
213 my ($verbosity, $outhandle, $failhandle) = @_;
214
215 $self->SUPER::init($verbosity,$outhandle,$failhandle);
216
217 my $secondary_plugins = $self->{'secondary_plugins'};
218
219 foreach my $plug_name (keys %$secondary_plugins) {
220 my $plugin = $secondary_plugins->{$plug_name};
221 $plugin->init($verbosity,$outhandle,$failhandle);
222 }
223}
224
225sub deinit {
226 # called only once, after all plugin passes have been done
227
228 my ($self) = @_;
229
230 my $secondary_plugins = $self->{'secondary_plugins'};
231
232 foreach my $plug_name (keys %$secondary_plugins) {
233 my $plugin = $secondary_plugins->{$plug_name};
234 $plugin->deinit();
235 }
236}
237
238sub convert_post_process
239{
240 # by default do no post processing
241 return;
242}
243
244
245# Run conversion utility on the input file.
246#
247# The conversion takes place in a collection specific 'tmp' directory so
248# that we don't accidentally damage the input.
249#
250# The desired output type is indicated by $output_ext. This is usually
251# something like "html" or "word", but can be "best" (or the empty string)
252# to indicate that the conversion utility should do the best it can.
253sub tmp_area_convert_file {
254 my $self = shift (@_);
255 my ($output_ext, $input_filename, $textref) = @_;
256
257 my $outhandle = $self->{'outhandle'};
258 my $convert_to = $self->{'convert_to'};
259 my $failhandle = $self->{'failhandle'};
260 my $convert_to_ext = $self->{'convert_to_ext'};
261
262 # derive tmp filename from input filename
263 my ($tailname, $dirname, $suffix)
264 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
265
266 # softlink to collection tmp dir
267 my $tmp_dirname = $dirname;
268 if(defined $ENV{'GSDLCOLLECTDIR'}) {
269 $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
270 } elsif(defined $ENV{'GSDLHOME'}) {
271 $tmp_dirname = $ENV{'GSDLHOME'};
272 }
273 $tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
274 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
275
276 # Remove any white space from filename -- no risk of name collision, and
277 # makes later conversion by utils simpler. Leave spaces in path...
278 # tidy up the filename with space, dot, hyphen between
279 $tailname =~ s/\s+//g;
280 $tailname =~ s/\.+//g;
281 $tailname =~ s/\-+//g;
282
283 # convert to utf-8 otherwise we have problems with the doc.xml file
284 # later on
285 &unicode::ensure_utf8(\$tailname);
286
287 $suffix = lc($suffix);
288 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
289 &util::soft_link($input_filename, $tmp_filename);
290 my $verbosity = $self->{'verbosity'};
291 if ($verbosity > 0) {
292 print $outhandle "Converting $tailname$suffix to $convert_to format\n";
293 }
294
295 my $errlog = &util::filename_cat($tmp_dirname, "err.log");
296
297 # Execute the conversion command and get the type of the result,
298 # making sure the converter gives us the appropriate output type
299 my $output_type="";
300 if ($convert_to =~ m/PagedImg/i) {
301 $output_type = lc($convert_to)."_".lc($convert_to_ext);
302 } else {
303 $output_type = lc($convert_to);
304 }
305
306 my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
307 if (defined $self->{'convert_options'}) {
308 $cmd .= $self->{'convert_options'} . " ";
309 }
310 if ($self->{'use_strings'}) {
311 $cmd .= "-use_strings ";
312 }
313 $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
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 # The following meant that if a conversion failed, the document would be counted twice - do we need it for anything?
325 #$self->{'num_not_processed'} ++;
326 if (-s "$errlog") {
327 open(ERRLOG, "$errlog");
328 while (<ERRLOG>) {
329 print $outhandle "$_";
330 }
331 print $outhandle "\n";
332 close ERRLOG;
333 }
334 &util::rm("$errlog") if (-e "$errlog");
335 return "";
336 }
337
338 # store the *actual* output type and return the output filename
339 # it's possible we requested conversion to html, but only to text succeeded
340 #$self->{'convert_to_ext'} = $output_type;
341 if ($output_type =~ /html/i) {
342 $self->{'converted_to'} = "HTML";
343 } elsif ($output_type =~ /te?xt/i) {
344 $self->{'converted_to'} = "TEXT";
345 } elsif ($output_type =~ /item/i){
346 $self->{'converted_to'} = "PagedImg";
347 }
348
349 my $output_filename = $tmp_filename;
350 if ($output_type =~ /item/i) {
351 # running under windows
352 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
353 $output_filename = $tmp_dirname . "\\$tailname\\" . $tailname . ".$output_type";
354 } else {
355 $output_filename = $tmp_dirname . "\/$tailname\/" . $tailname . ".$output_type";
356 }
357 } else {
358 $output_filename =~ s/$suffix$/.$output_type/;
359 }
360
361 return $output_filename;
362}
363
364
365# Override BasPlug read
366# We don't want to get language encoding stuff until after we've converted
367# our file to either TEXT or HTML or PagedImage.
368sub read {
369 my $self = shift (@_);
370 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
371
372 my $outhandle = $self->{'outhandle'};
373
374 my ($block_status,$filename) = $self->read_block(@_);
375 return $block_status if ((!defined $block_status) || ($block_status==0));
376 $file = $self->read_tidy_file($file);
377
378 my $output_ext = $self->{'convert_to_ext'};
379 my $conv_filename = "";
380 $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
381
382 if ("$conv_filename" eq "") {return -1;} # had an error, will be passed down pipeline
383 if (! -e "$conv_filename") {return -1;}
384 $self->{'conv_filename'} = $conv_filename;
385 $self->convert_post_process($conv_filename);
386
387 # Run the "fribidi" (http://fribidi.org) Unicode Bidirectional Algorithm program over the converted file
388 # Added for fixing up Persian PDFs after being processed by pdftohtml, but may be useful in other cases too
389 if ($self->{'apply_fribidi'} && $self->{'converted_to'} =~ /(HTML|TEXT)/) {
390 my $fribidi_command = "fribidi \"$conv_filename\" >\"${conv_filename}.tmp\"";
391 if (system($fribidi_command) != 0) {
392 print STDERR "ERROR: Cannot run fribidi on \"$conv_filename\".\n";
393 }
394 else {
395 &util::mv("${conv_filename}.tmp", $conv_filename);
396 }
397 }
398
399 my $secondary_plugins = $self->{'secondary_plugins'};
400 my $num_secondary_plugins = scalar(keys %$secondary_plugins);
401
402 if ($num_secondary_plugins == 0) {
403 print $outhandle "Warning: No secondary plugin to use in conversion. Skipping $file\n";
404 return 0; # effectively block it
405 }
406
407 my @plugin_names = keys %$secondary_plugins;
408 my $plugin_name = shift @plugin_names;
409
410 if ($num_secondary_plugins > 1) {
411 print $outhandle "Warning: Multiple secondary plugins not supported yet! Choosing $plugin_name\n.";
412 }
413
414 my $secondary_plugin = $secondary_plugins->{$plugin_name};
415
416 # note: metadata is not carried on to the next level
417 my ($rv,$doc_obj)
418 = $secondary_plugin->read_into_doc_obj ($pluginfo,"", $conv_filename,
419 $metadata, $processor, $maxdocs, $total_count,
420 $gli);
421
422 if ((!defined $rv) || ($rv<1)) {
423 # wasn't processed
424 return $rv;
425 }
426
427 # Override previous gsdlsourcefilename set by secondary plugin
428 my $collect_file = &util::filename_within_collection($filename);
429 my $collect_conv_file = &util::filename_within_collection($conv_filename);
430 $doc_obj->set_source_filename ($collect_file);
431 $doc_obj->set_converted_filename($collect_conv_file);
432
433 my ($filemeta) = $file =~ /([^\\\/]+)$/;
434 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
435 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
436 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename));
437
438 if ($self->{'cover_image'}) {
439 $self->associate_cover_image($doc_obj, $filename);
440 }
441
442 # do plugin specific processing of doc_obj
443 unless (defined ($self->process(undef, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
444 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
445 return -1;
446 }
447 # do any automatic metadata extraction
448 $self->auto_extract_metadata ($doc_obj);
449
450 # have we found a Title??
451 $self->title_fallback($doc_obj,$doc_obj->get_top_section(),$filemeta);
452
453# # add an OID
454# $doc_obj->set_OID();
455
456 # add an OID
457 # see if there is a plugin-specific set_OID function...
458 if (defined ($self->can('set_OID'))) {
459 # it will need $doc_obj to set the Identifier metadata...
460 $self->set_OID($doc_obj);
461 } else {
462 # use the default set_OID() in doc.pm
463 $doc_obj->set_OID();
464 }
465
466
467 # process the document
468 $processor->process($doc_obj);
469
470 $self->{'num_processed'} ++;
471
472 return 1;
473}
474
475
476# do plugin specific processing of doc_obj for doc_ext type
477sub process_type {
478 my $self = shift (@_);
479 my ($doc_ext, $base_dir, $file, $doc_obj) = @_;
480
481 # associate original file with doc object
482 my $cursection = $doc_obj->get_top_section();
483 my $filename = &util::filename_cat($base_dir, $file);
484 my $assocfilename = "doc.$doc_ext";
485 if ($self->{'keep_original_filename'} == 1) {
486 # this should be the same filename that was used for the Source metadata, as we will use [Source] in the srclink
487 ($assocfilename) = $file =~ /([^\\\/]+)$/;
488 }
489 $doc_obj->associate_file($filename, $assocfilename, undef, $cursection);
490
491 my $file_type;
492
493 if ($doc_ext eq "doc") {
494 $file_type = "Word";
495 } elsif ($doc_ext eq "xls") {
496 $file_type = "Excel";
497 } elsif ($doc_ext eq "ppt") {
498 $file_type = "PPT";
499 } elsif ($doc_ext eq "pdf") {
500 $file_type = "PDF";
501 } elsif ($doc_ext eq "rtf") {
502 $file_type = "RTF";
503 } elsif ($doc_ext eq "ps") {
504 $file_type = "PS";
505 }
506
507 my $file_format = $file_type || "unknown";
508
509 # We use set instead of add here because we only want one value
510 $doc_obj->set_utf8_metadata_element($cursection, "FileFormat", $file_format);
511 my $doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[archivedir]/doc.$doc_ext\">";
512 if ($self->{'keep_original_filename'} == 1) {
513 $doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[archivedir]/[Source]\">";
514 }
515 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
516 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
517 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
518
519 return 1;
520}
521
5221;
523
524
525
526
527
528
529
Note: See TracBrowser for help on using the repository browser.