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

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

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

  • Property svn:keywords set to Author Date Id Revision
File size: 13.2 KB
Line 
1###########################################################################
2#
3# ConvertToPlug.pm -- plugin that inherits from HTML or TEXT Plug, depending
4# on plugin argument convert_to
5#
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28# The plugin is inherited by such plugins as WordPlug and PDFPlug.
29# It facilitates the conversion of these document types to either HTML
30# or TEXT by setting up variable that instruct ConvertToBasPlug
31# how to work.
32
33# It works by dynamically inheriting HTMLPlug or TEXTPlug based on
34# the plugin argument 'convert_to'. If the argument is not present,
35# the default is to inherit HTMLPlug.
36
37
38package ConvertToPlug;
39
40use BasPlug;
41use HTMLPlug;
42use TEXTPlug;
43use ghtml;
44
45sub BEGIN {
46 @ISA = ('HTMLPlug');
47# @ISA = ('HTMLPlug', 'TEXTPlug');
48# @ISA = ('BasPlug'); #, 'HTMLPlug', 'TEXTPlug');
49}
50
51my $convert_to_list =
52 [ { 'name' => "html",
53 'desc' => "{ConvertToPlug.convert_to.html}" },
54 { 'name' => "text",
55 'desc' => "{ConvertToPlug.convert_to.text}" } ];
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' => "use_strings",
65 'desc' => "{ConvertToPlug.use_strings}",
66 'type' => "flag",
67 'reqd' => "no" },
68 { 'name' => "extract_keyphrases",
69 'desc' => "{BasPlug.extract_keyphrases}",
70 'type' => "flag",
71 'reqd' => "no",
72 'hiddengli' => "yes" },
73 { 'name' => "extract_keyphrase_options",
74 'desc' => "{BasPlug.extract_keyphrase_options}",
75 'type' => "string",
76 'reqd' => "no",
77 'hiddengli' => "yes" } ];
78
79my $options = { 'name' => "ConvertToPlug",
80 'desc' => "{ConvertToPlug.desc}",
81 'abstract' => "yes",
82 'inherits' => "yes",
83 'args' => $arguments };
84
85
86sub findType
87{
88 my ($inputargs) = @_;
89
90 for(my $intCounter = 0; $intCounter < scalar(@{$inputargs}) ; $intCounter++)
91 {
92 if($inputargs->[$intCounter] eq "-convert_to")
93 {
94 if($inputargs->[$intCounter+1] eq "text" || $inputargs->[$intCounter+1] eq "html")
95 {
96 return $inputargs->[$intCounter+1];
97 }
98 else {return "html";}
99 }
100 }
101 return "html";
102}
103
104sub new {
105 my ($class) = shift (@_);
106 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
107 push(@$pluginlist, $class);
108 my $classPluginName = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
109 my $strConvertTo = findType($inputargs);
110
111 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
112 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
113
114 if ($classPluginName eq "PDFPlug" && $strConvertTo eq "text" &&
115 $ENV{'GSDLOS'} =~ /^windows$/i) {
116 print STDERR "Windows does not support pdf to text. PDFs will be converted to HTML instead\n";
117 $strConvertTo = "html";
118 }
119
120 my $self = {};
121 if ($strConvertTo eq "text")
122 {
123 $self = (defined $hashArgOptLists)? new TEXTPlug($pluginlist,$inputargs,$hashArgOptLists): new TEXTPlug($pluginlist,$inputargs);
124 $self->{'convert_to'} = "TEXT";
125 $self->{'convert_to_ext'} = "txt";
126 }
127 else
128 {
129 $self = (defined $hashArgOptLists)? new HTMLPlug($pluginlist,$inputargs,$hashArgOptLists): new HTMLPlug($pluginlist,$inputargs);
130 $self->{'convert_to'} = "HTML";
131 $self->{'convert_to_ext'} = "html";
132
133 $self->{'rename_assoc_files'} = 1;
134 $self->{'metadata_fields'} .= ",GENERATOR";
135 }
136
137 return bless $self, $class;
138}
139
140# we don't need to block anything, so override the one for HTMLPlug
141# files are converted in a temp dir and extra files not passed down the
142# plugin list
143sub get_default_block_exp {
144 my $self = shift (@_);
145
146 return "";
147}
148
149# Go straight to BasPlug and avoid the special case implemented by HTMLPlug
150sub store_block_files {
151 return BasPlug::store_block_files(@_);
152}
153
154# Run conversion utility on the input file.
155#
156# The conversion takes place in a collection specific 'tmp' directory so
157# that we don't accidentally damage the input.
158#
159# The desired output type is indicated by $output_ext. This is usually
160# something like "html" or "word", but can be "best" (or the empty string)
161# to indicate that the conversion utility should do the best it can.
162
163sub tmp_area_convert_file {
164 my $self = shift (@_);
165 my ($output_ext, $input_filename, $textref) = @_;
166
167 my $outhandle = $self->{'outhandle'};
168 my $convert_to = $self->{'convert_to'};
169 my $failhandle = $self->{'failhandle'};
170
171 # softlink to collection tmp dir
172 my $tmp_dirname
173 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
174 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
175
176 # derive tmp filename from input filename
177 my ($tailname, $dirname, $suffix)
178 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
179
180 # Remove any white space from filename -- no risk of name collision, and
181 # makes later conversion by utils simpler. Leave spaces in path...
182 $tailname =~ s/\s+//g;
183
184 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
185
186 &util::soft_link($input_filename, $tmp_filename);
187
188 my $verbosity = $self->{'verbosity'};
189 if ($verbosity > 0) {
190 print $outhandle "Converting $tailname$suffix to $convert_to format\n";
191 }
192
193 my $errlog = &util::filename_cat($tmp_dirname, "err.log");
194
195 # Execute the conversion command and get the type of the result,
196 # making sure the converter gives us the appropriate output type
197 my $output_type = lc($convert_to);
198 my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
199 if (defined $self->{'convert_options'}) {
200 $cmd .= $self->{'convert_options'} . " ";
201 }
202 if ($self->{'use_strings'}) {
203 $cmd .= "-use_strings ";
204 }
205 $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
206
207 $output_type = `$cmd`;
208
209 # remove symbolic link to original file
210 &util::rm($tmp_filename);
211
212 # Check STDERR here
213 chomp $output_type;
214 if ($output_type eq "fail") {
215 print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
216 print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
217 $self->{'num_not_processed'} ++;
218 if (-s "$errlog") {
219 open(ERRLOG, "$errlog");
220 while (<ERRLOG>) {
221 print $outhandle "$_";
222 }
223 print $outhandle "\n";
224 close ERRLOG;
225 }
226 &util::rm("$errlog") if (-e "$errlog");
227 return "";
228 }
229
230 # store the *actual* output type and return the output filename
231 # it's possible we requested conversion to html, but only to text succeeded
232
233 $self->{'convert_to_ext'} = $output_type;
234 if ($output_type =~ /html/i) {
235 $self->{'converted_to'} = "HTML";
236 } elsif ($output_type =~ /te?xt/i) {
237 $self->{'converted_to'} = "TEXT";
238 }
239 my $output_filename = $tmp_filename;
240
241 $output_filename =~ s/$suffix$/.$output_type/;
242
243 return $output_filename;
244}
245
246
247# Remove collection specific tmp directory and all its contents.
248
249sub cleanup_tmp_area {
250 my $self = shift (@_);
251
252 my $tmp_dirname
253 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
254 &util::rm_r($tmp_dirname);
255 &util::mk_dir($tmp_dirname);
256}
257
258
259
260
261# Override BasPlug read
262# We don't want to get language encoding stuff until after we've converted
263# our file to either TEXT or HTML.
264sub read {
265 my $self = shift (@_);
266 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
267# if ($self->is_recursive()) {
268# die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
269# }
270
271 my $outhandle = $self->{'outhandle'};
272
273 my $filename = $file;
274 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
275
276 if ($self->associate_with($file,$filename,$metadata)) {
277 # a form of smart block
278 $self->{'num_blocked'} ++;
279 return 0; # blocked
280 }
281
282 if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
283 $self->{'num_blocked'} ++;
284 return 0;
285 }
286 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
287 return undef;
288 }
289 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
290
291 # read in file ($text will be in utf8)
292 my $text = "";
293
294 my $output_ext = $self->{'convert_to_ext'};
295
296 my $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
297
298 if ("$conv_filename" eq "") {return 0;} # allows continue on errors
299 if (! -e "$conv_filename") {return 0;} # allows continue on errors
300 $self->{'conv_filename'} = $conv_filename;
301
302 # Do encoding stuff
303 my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
304
305 &BasPlug::read_file($self, $conv_filename, $encoding, $language, \$text);
306 if (!length ($text)) {
307 my $plugin_name = ref ($self);
308 print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
309 return 0;
310 }
311
312 # if we converted to HTML, convert &eacute; and etc to utf-8.
313 # this should really happen before language_extraction, but that means
314 # modifying a file on disk...
315 $text =~ s/&([^;]+);/&ghtml::getcharequiv($1,0)/ge;
316
317 # create a new document
318 #my $doc_obj = new doc ($conv_filename, "indexed_doc");
319 # now we use the original filename here
320 my $doc_obj = new doc($filename, "indexed_doc");
321 $doc_obj->set_converted_filename($conv_filename);
322 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
323 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
324 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
325 my ($filemeta) = $file =~ /([^\\\/]+)$/;
326 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
327 if ($self->{'cover_image'}) {
328 $self->associate_cover_image($doc_obj, $filename);
329 }
330 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
331 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
332
333 # include any metadata passed in from previous plugins
334 # note that this metadata is associated with the top level section
335 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
336 # do plugin specific processing of doc_obj
337 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
338 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
339 return -1;
340 }
341 # do any automatic metadata extraction
342 $self->auto_extract_metadata ($doc_obj);
343 # add an OID
344 $doc_obj->set_OID();
345 # process the document
346 $processor->process($doc_obj);
347 $self->cleanup_tmp_area();
348
349 $self->{'num_processed'} ++;
350
351 return 1;
352}
353
354
355# do plugin specific processing of doc_obj for HTML type
356sub process_type {
357 my $self = shift (@_);
358 my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
359
360 my $conv_filename = $self->{'conv_filename'};
361 my $tmp_dirname = File::Basename::dirname($conv_filename);
362 my $tmp_tailname = File::Basename::basename($conv_filename);
363
364 my $converted_to = $self->{'converted_to'};
365 my $ret_val;
366
367 if ($converted_to eq "TEXT")
368 {
369
370 $ret_val = &TEXTPlug::process($self, $textref, $pluginfo,
371 $tmp_dirname, $tmp_tailname,
372 $metadata, $doc_obj);
373 }
374 else
375 {
376 $ret_val = &HTMLPlug::process($self, $textref, $pluginfo,
377 $tmp_dirname, $tmp_tailname,
378 $metadata, $doc_obj);
379 }
380
381 # associate original file with doc object
382 my $cursection = $doc_obj->get_top_section();
383 my $filename = &util::filename_cat($base_dir, $file);
384 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
385
386 my $file_type;
387
388 if ($doc_ext eq "doc") {
389 $file_type = "Word";
390 } elsif ($doc_ext eq "xls") {
391 $file_type = "Excel";
392 } elsif ($doc_ext eq "ppt") {
393 $file_type = "PPT";
394 } elsif ($doc_ext eq "pdf") {
395 $file_type = "PDF";
396 } elsif ($doc_ext eq "rtf") {
397 $file_type = "RTF";
398 } elsif ($doc_ext eq "ps") {
399 $file_type = "PS";
400 }
401
402 my $file_format = $file_type || "unknown";
403
404 # We use set instead of add here because we only want one value
405 $doc_obj->set_utf8_metadata_element($cursection, "FileFormat", $file_format);
406
407 my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext\">";
408 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
409 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
410 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
411
412 return $ret_val;
413}
414
4151;
Note: See TracBrowser for help on using the repository browser.