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 |
|
---|
38 | package ConvertToPlug;
|
---|
39 |
|
---|
40 | use BasPlug;
|
---|
41 | use HTMLPlug;
|
---|
42 | use TEXTPlug;
|
---|
43 | use ghtml;
|
---|
44 |
|
---|
45 | sub BEGIN {
|
---|
46 | @ISA = ('HTMLPlug');
|
---|
47 | # @ISA = ('HTMLPlug', 'TEXTPlug');
|
---|
48 | # @ISA = ('BasPlug'); #, 'HTMLPlug', 'TEXTPlug');
|
---|
49 | }
|
---|
50 |
|
---|
51 | my $convert_to_list =
|
---|
52 | [ { 'name' => "html",
|
---|
53 | 'desc' => "{ConvertToPlug.convert_to.html}" },
|
---|
54 | { 'name' => "text",
|
---|
55 | 'desc' => "{ConvertToPlug.convert_to.text}" } ];
|
---|
56 |
|
---|
57 | my $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 |
|
---|
69 | my $options = { 'name' => "ConvertToPlug",
|
---|
70 | 'desc' => "{ConvertToPlug.desc}",
|
---|
71 | 'abstract' => "yes",
|
---|
72 | 'inherits' => "yes",
|
---|
73 | 'args' => $arguments };
|
---|
74 |
|
---|
75 | sub 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 |
|
---|
101 | sub 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 | # Go straight to BasPlug and avoid the special case implemented by HTMLPlug
|
---|
144 | sub metadata_read {
|
---|
145 | return BasPlug::metadata_read(@_);
|
---|
146 | }
|
---|
147 |
|
---|
148 | # Run conversion utility on the input file.
|
---|
149 | #
|
---|
150 | # The conversion takes place in a collection specific 'tmp' directory so
|
---|
151 | # that we don't accidentally damage the input.
|
---|
152 | #
|
---|
153 | # The desired output type is indicated by $output_ext. This is usually
|
---|
154 | # something like "html" or "word", but can be "best" (or the empty string)
|
---|
155 | # to indicate that the conversion utility should do the best it can.
|
---|
156 |
|
---|
157 | sub tmp_area_convert_file {
|
---|
158 | my $self = shift (@_);
|
---|
159 | my ($output_ext, $input_filename, $textref) = @_;
|
---|
160 |
|
---|
161 | my $outhandle = $self->{'outhandle'};
|
---|
162 | my $convert_to = $self->{'convert_to'};
|
---|
163 | my $failhandle = $self->{'failhandle'};
|
---|
164 |
|
---|
165 | # softlink to collection tmp dir
|
---|
166 | my $tmp_dirname
|
---|
167 | = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
|
---|
168 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
169 |
|
---|
170 | # derive tmp filename from input filename
|
---|
171 | my ($tailname, $dirname, $suffix)
|
---|
172 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
173 |
|
---|
174 | # Remove any white space from filename -- no risk of name collision, and
|
---|
175 | # makes later conversion by utils simpler. Leave spaces in path...
|
---|
176 | $tailname =~ s/\s+//g;
|
---|
177 |
|
---|
178 | my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
|
---|
179 |
|
---|
180 | &util::soft_link($input_filename, $tmp_filename);
|
---|
181 |
|
---|
182 | my $verbosity = $self->{'verbosity'};
|
---|
183 | if ($verbosity > 0) {
|
---|
184 | print $outhandle "Converting $tailname$suffix to $convert_to format\n";
|
---|
185 | }
|
---|
186 |
|
---|
187 | my $errlog = &util::filename_cat($tmp_dirname, "err.log");
|
---|
188 |
|
---|
189 | # Execute the conversion command and get the type of the result,
|
---|
190 | # making sure the converter gives us the appropriate output type
|
---|
191 | my $output_type = lc($convert_to);
|
---|
192 | my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
|
---|
193 | if (defined $self->{'convert_options'}) {
|
---|
194 | $cmd .= $self->{'convert_options'} . " ";
|
---|
195 | }
|
---|
196 | if ($self->{'use_strings'}) {
|
---|
197 | $cmd .= "-use_strings ";
|
---|
198 | }
|
---|
199 | $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
|
---|
200 |
|
---|
201 | $output_type = `$cmd`;
|
---|
202 |
|
---|
203 | # remove symbolic link to original file
|
---|
204 | &util::rm($tmp_filename);
|
---|
205 |
|
---|
206 | # Check STDERR here
|
---|
207 | chomp $output_type;
|
---|
208 | if ($output_type eq "fail") {
|
---|
209 | print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
|
---|
210 | print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
|
---|
211 | $self->{'num_not_processed'} ++;
|
---|
212 | if (-s "$errlog") {
|
---|
213 | open(ERRLOG, "$errlog");
|
---|
214 | while (<ERRLOG>) {
|
---|
215 | print $outhandle "$_";
|
---|
216 | }
|
---|
217 | print $outhandle "\n";
|
---|
218 | close ERRLOG;
|
---|
219 | }
|
---|
220 | &util::rm("$errlog") if (-e "$errlog");
|
---|
221 | return "";
|
---|
222 | }
|
---|
223 |
|
---|
224 | # store the *actual* output type and return the output filename
|
---|
225 | # it's possible we requested conversion to html, but only to text succeeded
|
---|
226 |
|
---|
227 | $self->{'convert_to_ext'} = $output_type;
|
---|
228 | if ($output_type =~ /html/i) {
|
---|
229 | $self->{'converted_to'} = "HTML";
|
---|
230 | } elsif ($output_type =~ /te?xt/i) {
|
---|
231 | $self->{'converted_to'} = "TEXT";
|
---|
232 | }
|
---|
233 | my $output_filename = $tmp_filename;
|
---|
234 |
|
---|
235 | $output_filename =~ s/$suffix$/.$output_type/;
|
---|
236 |
|
---|
237 | return $output_filename;
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | # Remove collection specific tmp directory and all its contents.
|
---|
242 |
|
---|
243 | sub cleanup_tmp_area {
|
---|
244 | my $self = shift (@_);
|
---|
245 |
|
---|
246 | my $tmp_dirname
|
---|
247 | = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
|
---|
248 | &util::rm_r($tmp_dirname);
|
---|
249 | &util::mk_dir($tmp_dirname);
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 |
|
---|
254 |
|
---|
255 | # Override BasPlug read
|
---|
256 | # We don't want to get language encoding stuff until after we've converted
|
---|
257 | # our file to either TEXT or HTML.
|
---|
258 | sub read {
|
---|
259 | my $self = shift (@_);
|
---|
260 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
|
---|
261 | # if ($self->is_recursive()) {
|
---|
262 | # die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
|
---|
263 | # }
|
---|
264 |
|
---|
265 | my $outhandle = $self->{'outhandle'};
|
---|
266 |
|
---|
267 | my $filename = $file;
|
---|
268 | $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
|
---|
269 |
|
---|
270 | if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
|
---|
271 | $self->{'num_blocked'} ++;
|
---|
272 | return 0;
|
---|
273 | }
|
---|
274 | if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
|
---|
275 | return undef;
|
---|
276 | }
|
---|
277 | $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
|
---|
278 |
|
---|
279 | # read in file ($text will be in utf8)
|
---|
280 | my $text = "";
|
---|
281 |
|
---|
282 | my $output_ext = $self->{'convert_to_ext'};
|
---|
283 | my $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
|
---|
284 |
|
---|
285 | if ("$conv_filename" eq "") {return 0;} # allows continue on errors
|
---|
286 | if (! -e "$conv_filename") {return 0;} # allows continue on errors
|
---|
287 | $self->{'conv_filename'} = $conv_filename;
|
---|
288 |
|
---|
289 | # Do encoding stuff
|
---|
290 | my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);
|
---|
291 |
|
---|
292 | &BasPlug::read_file($self, $conv_filename, $encoding, $language, \$text);
|
---|
293 | if (!length ($text)) {
|
---|
294 | my $plugin_name = ref ($self);
|
---|
295 | print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
|
---|
296 | return 0;
|
---|
297 | }
|
---|
298 |
|
---|
299 | # if we converted to HTML, convert é and etc to utf-8.
|
---|
300 | # this should really happen before language_extraction, but that means
|
---|
301 | # modifying a file on disk...
|
---|
302 | $text =~ s/&([^;]+);/&ghtml::getcharequiv($1,0)/ge;
|
---|
303 |
|
---|
304 | # create a new document
|
---|
305 | #my $doc_obj = new doc ($conv_filename, "indexed_doc");
|
---|
306 | # now we use the original filename here
|
---|
307 | my $doc_obj = new doc($filename, "indexed_doc");
|
---|
308 | $doc_obj->set_converted_filename($conv_filename);
|
---|
309 | $doc_obj->set_OIDtype ($processor->{'OIDtype'});
|
---|
310 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
|
---|
311 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
|
---|
312 | my ($filemeta) = $file =~ /([^\\\/]+)$/;
|
---|
313 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
|
---|
314 | if ($self->{'cover_image'}) {
|
---|
315 | $self->associate_cover_image($doc_obj, $filename);
|
---|
316 | }
|
---|
317 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
|
---|
318 | $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
|
---|
319 |
|
---|
320 | # include any metadata passed in from previous plugins
|
---|
321 | # note that this metadata is associated with the top level section
|
---|
322 | $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
|
---|
323 | # do plugin specific processing of doc_obj
|
---|
324 | return -1 unless defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj));
|
---|
325 | # do any automatic metadata extraction
|
---|
326 | $self->auto_extract_metadata ($doc_obj);
|
---|
327 | # add an OID
|
---|
328 | $doc_obj->set_OID();
|
---|
329 | # process the document
|
---|
330 | $processor->process($doc_obj);
|
---|
331 | $self->cleanup_tmp_area();
|
---|
332 |
|
---|
333 | $self->{'num_processed'} ++;
|
---|
334 |
|
---|
335 | return 1;
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | # do plugin specific processing of doc_obj for HTML type
|
---|
340 | sub process_type {
|
---|
341 | my $self = shift (@_);
|
---|
342 | my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
|
---|
343 |
|
---|
344 | my $conv_filename = $self->{'conv_filename'};
|
---|
345 | my $tmp_dirname = File::Basename::dirname($conv_filename);
|
---|
346 | my $tmp_tailname = File::Basename::basename($conv_filename);
|
---|
347 |
|
---|
348 | my $converted_to = $self->{'converted_to'};
|
---|
349 | my $ret_val;
|
---|
350 |
|
---|
351 | if ($converted_to eq "TEXT")
|
---|
352 | {
|
---|
353 |
|
---|
354 | $ret_val = &TEXTPlug::process($self, $textref, $pluginfo,
|
---|
355 | $tmp_dirname, $tmp_tailname,
|
---|
356 | $metadata, $doc_obj);
|
---|
357 | }
|
---|
358 | else
|
---|
359 | {
|
---|
360 | $ret_val = &HTMLPlug::process($self, $textref, $pluginfo,
|
---|
361 | $tmp_dirname, $tmp_tailname,
|
---|
362 | $metadata, $doc_obj);
|
---|
363 | }
|
---|
364 |
|
---|
365 | # associate original file with doc object
|
---|
366 | my $cursection = $doc_obj->get_top_section();
|
---|
367 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
368 | $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
|
---|
369 |
|
---|
370 | my $file_type;
|
---|
371 |
|
---|
372 | if ($doc_ext eq "doc") {
|
---|
373 | $file_type = "Word";
|
---|
374 | } elsif ($doc_ext eq "xls") {
|
---|
375 | $file_type = "Excel";
|
---|
376 | } elsif ($doc_ext eq "ppt") {
|
---|
377 | $file_type = "PPT";
|
---|
378 | } elsif ($doc_ext eq "pdf") {
|
---|
379 | $file_type = "PDF";
|
---|
380 | } elsif ($doc_ext eq "rtf") {
|
---|
381 | $file_type = "RTF";
|
---|
382 | } elsif ($doc_ext eq "ps") {
|
---|
383 | $file_type = "PS";
|
---|
384 | }
|
---|
385 |
|
---|
386 | my $file_format = $file_type || "unknown";
|
---|
387 |
|
---|
388 | # We use set instead of add here because we only want one value
|
---|
389 | $doc_obj->set_utf8_metadata_element($cursection, "FileFormat", $file_format);
|
---|
390 |
|
---|
391 | my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext\">";
|
---|
392 | $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
|
---|
393 | $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
|
---|
394 | $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
|
---|
395 |
|
---|
396 | return $ret_val;
|
---|
397 | }
|
---|
398 |
|
---|
399 | 1;
|
---|