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

Last change on this file since 8121 was 8121, checked in by chi, 20 years ago

Add the "FileFormat" metadata to each of the Plugins.

  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 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
69my $options = { 'name' => "ConvertToPlug",
70 'desc' => "{ConvertToPlug.desc}",
71 'abstract' => "yes",
72 'inherits' => "yes",
73 'args' => $arguments };
74
75sub parse_args
76{
77 my $class = shift (@_);
78 my ($args) = @_;
79
80 my $plugin_name = $class;
81 $plugin_name =~ s/\.pm$//;
82
83 my $newargs = {};
84
85 if (!parsargv::parse($args,
86 q^extract_keyphrases^, \$newargs->{'kea'}, #with extra options (undocumented)
87 q^extract_keyphrase_options/.*/^, \$newargs->{'kea_options'}, #no extra options (undocumented)
88 q^convert_to/(html|text)/html^, \$newargs->{'generate_format'},
89 q^use_strings^, \$newargs->{'use_strings'},
90 "allow_extra_options")) {
91
92 print STDERR "\nIncorrect options passed to $plugin_name, ";
93 print STDERR "check your collect.cfg configuration file\n";
94 $self->print_txt_usage(""); # Use default resource bundle
95 die "\n";
96 }
97
98 return ($plugin_name, $newargs);
99}
100
101sub new {
102 my $class = shift (@_);
103 if ($class eq "ConvertToPlug" && defined $_[0]) {$class = shift (@_);}
104 my $self;
105 # parsargv::parse might modify the list, so we do this by creating a copy
106 # of the argument list.
107 my @arglist = @_;
108 my ($plugin_name, $args) = $class->parse_args(\@_);
109
110 if ($class eq "PDFPlug" && $args->{'generate_format'} eq "text" &&
111 $ENV{'GSDLOS'} =~ /^windows$/i) {
112 print STDERR "Windows does not support pdf to text. PDFs will be converted to HTML instead\n";
113 $args->{'generate_format'} = "html";
114 }
115
116 if ($args->{'generate_format'} eq "text")
117 {
118 $self = new TEXTPlug ($class, @arglist);
119 $self->{'convert_to'} = "TEXT";
120 $self->{'convert_to_ext'} = "txt";
121 }
122 else
123 {
124 $self = new HTMLPlug ($class, @arglist);
125 $self->{'convert_to'} = "HTML";
126 $self->{'convert_to_ext'} = "html";
127
128 $self->{'rename_assoc_files'} = 1;
129 $self->{'metadata_fields'} .= ",GENERATOR";
130 }
131
132 # 14-05-02 To allow for proper inheritance of arguments - John Thompson
133 my $option_list = $self->{'option_list'};
134 push( @{$option_list}, $options );
135
136 foreach my $key (keys %$args) {
137 $self->{$key} = $args->{$key};
138 }
139
140 return bless $self, $class;
141}
142
143
144
145# Run conversion utility on the input file.
146#
147# The conversion takes place in a collection specific 'tmp' directory so
148# that we don't accidentally damage the input.
149#
150# The desired output type is indicated by $output_ext. This is usually
151# something like "html" or "word", but can be "best" (or the empty string)
152# to indicate that the conversion utility should do the best it can.
153
154sub tmp_area_convert_file {
155 my $self = shift (@_);
156 my ($output_ext, $input_filename, $textref) = @_;
157
158 my $outhandle = $self->{'outhandle'};
159 my $convert_to = $self->{'convert_to'};
160 my $failhandle = $self->{'failhandle'};
161
162 # softlink to collection tmp dir
163 my $tmp_dirname
164 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
165 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
166
167 # derive tmp filename from input filename
168 my ($tailname, $dirname, $suffix)
169 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
170
171 # Remove any white space from filename -- no risk of name collision, and
172 # makes later conversion by utils simpler. Leave spaces in path...
173 $tailname =~ s/\s+//g;
174
175 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
176
177 &util::soft_link($input_filename, $tmp_filename);
178
179 my $verbosity = $self->{'verbosity'};
180 if ($verbosity > 0) {
181 print $outhandle "Converting $tailname$suffix to $convert_to format\n";
182 }
183
184 my $errlog = &util::filename_cat($tmp_dirname, "err.log");
185
186 # Execute the conversion command and get the type of the result,
187 # making sure the converter gives us the appropriate output type
188 my $output_type = lc($convert_to);
189 my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
190 if (defined $self->{'convert_options'}) {
191 $cmd .= $self->{'convert_options'} . " ";
192 }
193 if ($self->{'use_strings'}) {
194 $cmd .= "-use_strings ";
195 }
196 $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
197
198 $output_type = `$cmd`;
199
200 # remove symbolic link to original file
201 &util::rm($tmp_filename);
202
203 # Check STDERR here
204 chomp $output_type;
205 if ($output_type eq "fail") {
206 print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
207 print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
208 $self->{'num_not_processed'} ++;
209 if (-s "$errlog") {
210 open(ERRLOG, "$errlog");
211 while (<ERRLOG>) {
212 print $outhandle "$_";
213 }
214 print $outhandle "\n";
215 close ERRLOG;
216 }
217 &util::rm("$errlog") if (-e "$errlog");
218 return "";
219 }
220
221 # store the *actual* output type and return the output filename
222 # it's possible we requested conversion to html, but only to text succeeded
223
224 $self->{'convert_to_ext'} = $output_type;
225 if ($output_type =~ /html/i) {
226 $self->{'converted_to'} = "HTML";
227 } elsif ($output_type =~ /te?xt/i) {
228 $self->{'converted_to'} = "TEXT";
229 }
230 my $output_filename = $tmp_filename;
231
232 $output_filename =~ s/$suffix$/.$output_type/;
233
234 return $output_filename;
235}
236
237
238# Remove collection specific tmp directory and all its contents.
239
240sub cleanup_tmp_area {
241 my $self = shift (@_);
242
243 my $tmp_dirname
244 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
245 &util::rm_r($tmp_dirname);
246 &util::mk_dir($tmp_dirname);
247}
248
249
250
251
252# Override BasPlug read
253# We don't want to get language encoding stuff until after we've converted
254# our file to either TEXT or HTML.
255sub read {
256 my $self = shift (@_);
257 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
258# if ($self->is_recursive()) {
259# die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
260# }
261
262 my $outhandle = $self->{'outhandle'};
263
264 my $filename = $file;
265 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
266
267 if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
268 $self->{'num_blocked'} ++;
269 return 0;
270 }
271 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
272 return undef;
273 }
274 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
275
276 # read in file ($text will be in utf8)
277 my $text = "";
278
279 my $output_ext = $self->{'convert_to_ext'};
280 my $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
281
282 if ("$conv_filename" eq "") {return 0;} # allows continue on errors
283 if (! -e "$conv_filename") {return 0;} # allows continue on errors
284 $self->{'conv_filename'} = $conv_filename;
285
286 # Do encoding stuff
287 my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
288
289 &BasPlug::read_file($self, $conv_filename, $encoding, $language, \$text);
290 if (!length ($text)) {
291 my $plugin_name = ref ($self);
292 print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
293 return 0;
294 }
295
296 # if we converted to HTML, convert &eacute; and etc to utf-8.
297 # this should really happen before language_extraction, but that means
298 # modifying a file on disk...
299 $text =~ s/&([^;]+);/&ghtml::getcharequiv($1,0)/ge;
300
301 # create a new document
302 #my $doc_obj = new doc ($conv_filename, "indexed_doc");
303 # now we use the original filename here
304 my $doc_obj = new doc($filename, "indexed_doc");
305 $doc_obj->set_converted_filename($conv_filename);
306 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
307 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
308 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
309 my ($filemeta) = $file =~ /([^\\\/]+)$/;
310 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
311 if ($self->{'cover_image'}) {
312 $self->associate_cover_image($doc_obj, $filename);
313 }
314 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
315 # include any metadata passed in from previous plugins
316 # note that this metadata is associated with the top level section
317 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
318 # do plugin specific processing of doc_obj
319 return -1 unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
320 # do any automatic metadata extraction
321 $self->auto_extract_metadata ($doc_obj);
322 # add an OID
323 $doc_obj->set_OID();
324 # process the document
325 $processor->process($doc_obj);
326 $self->cleanup_tmp_area();
327
328 $self->{'num_processed'} ++;
329
330 return 1;
331}
332
333
334# do plugin specific processing of doc_obj for HTML type
335sub process_type {
336 my $self = shift (@_);
337 my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
338
339 my $conv_filename = $self->{'conv_filename'};
340 my $tmp_dirname = File::Basename::dirname($conv_filename);
341 my $tmp_tailname = File::Basename::basename($conv_filename);
342
343 my $converted_to = $self->{'converted_to'};
344 my $ret_val;
345
346 if ($converted_to eq "TEXT")
347 {
348
349 $ret_val = &TEXTPlug::process($self, $textref, $pluginfo,
350 $tmp_dirname, $tmp_tailname,
351 $metadata, $doc_obj);
352 }
353 else
354 {
355 $ret_val = &HTMLPlug::process($self, $textref, $pluginfo,
356 $tmp_dirname, $tmp_tailname,
357 $metadata, $doc_obj);
358 }
359
360 # associate original file with doc object
361 my $cursection = $doc_obj->get_top_section();
362 my $filename = &util::filename_cat($base_dir, $file);
363 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
364
365 my $file_type;
366
367 if ($doc_ext eq "doc") {
368 $file_type = "Word";
369 } elsif ($doc_ext eq "xls") {
370 $file_type = "Excel";
371 } elsif ($doc_ext eq "ppt") {
372 $file_type = "PPT";
373 } elsif ($doc_ext eq "pdf") {
374 $file_type = "PDF";
375 } elsif ($doc_ext eq "rtf") {
376 $file_type = "RTF";
377 }
378
379 my $file_format = $file_type || "unknown";
380
381 $doc_obj->add_metadata($cursection, "FileFormat", $file_format);
382
383 my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext\">";
384 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
385 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
386 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
387
388 return $ret_val;
389}
390
3911;
Note: See TracBrowser for help on using the repository browser.