source: main/trunk/greenstone2/perllib/plugins/UnknownConverterPlugin.pm.bak@ 31744

Last change on this file since 31744 was 31744, checked in by ak19, 7 years ago

Further changes to new UnknownConverterPlugin that's still in progress. Now I can at least run pluginfo.pl on the plugin to get a description of it.

File size: 13.9 KB
Line 
1###########################################################################
2#
3# UnknownConverterPlugin.pm -- plugin that runs the provided cmdline cmd
4# to launch an custom unknown external conversion application that will
5# convert from some custom unknown format to one of txt, html or xml.
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 1999-2005 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28
29package UnknownConverterPlugin;
30
31use strict;
32no strict 'subs';
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
35use ConvertBinaryFile;
36use UnknownPlugin;
37
38# TO DO:
39# - error messages and other display strings need to go into strings.properties
40# - Have a TEMPDIR placeholder in the command, which, if present, gets replaced with the usual tempdir location
41# of a collection, and in which case we have to clean up intermediate files generated in there at the end?
42# Add a check that the generated file or files generated in the output dir match the convert_to option selected
43# before trying to process them
44
45sub BEGIN {
46 @UnknownConverterPlugin::ISA = ('UnknownPlugin', 'ConvertBinaryFile');
47}
48
49my $convert_to_list =
50 [ { 'name' => "text",
51 'desc' => "{ConvertBinaryFile.convert_to.text}" },
52 { 'name' => "html",
53 'desc' => "{ConvertBinaryFile.convert_to.html}" },
54 { 'name' => "pagedimg_jpg",
55 'desc' => "{ConvertBinaryFile.convert_to.pagedimg_jpg}" },
56 { 'name' => "pagedimg_gif",
57 'desc' => "{ConvertBinaryFile.convert_to.pagedimg_gif}" },
58 { 'name' => "pagedimg_png",
59 'desc' => "{ConvertBinaryFile.convert_to.pagedimg_png}" }
60 ];
61
62my $arguments =
63 [ { 'name' => "exec_cmd",
64 'desc' => "{UnknownConverterPlugin.exec_cmd}",
65 'type' => "string",
66 'deft' => "",
67 'reqd' => "yes" },
68 { 'name' => "convert_to",
69 'desc' => "{ConvertBinaryFile.convert_to}",
70 'type' => "enum",
71 'reqd' => "yes",
72 'list' => $convert_to_list,
73 'deft' => "text" },
74 { 'name' => "output_file_or_dir_name",
75 'desc' => "{UnknownConverterPlugin.output_file_or_dir_name}",
76 'type' => "string",
77 'reqd' => "yes",
78 'deft' => "" } ];
79
80my $options = { 'name' => "UnknownConverterPlugin",
81 'desc' => "{UnknownConverterPlugin.desc}",
82 'abstract' => "no",
83 'inherits' => "yes",
84 'args' => $arguments };
85
86
87sub new {
88 my ($class) = shift (@_);
89 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
90 push(@$pluginlist, $class);
91
92 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
93 push(@{$hashArgOptLists->{"OptList"}},$options);
94
95 my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
96 my $unknown_converter_self = new UnknownPlugin($pluginlist, $inputargs, $hashArgOptLists);
97 my $self = BaseImporter::merge_inheritance($cbf_self, $unknown_converter_self);
98
99 $self = bless $self, $class;
100
101
102 # Convert_To set up, including secondary_plugins for processing the text or html generated
103 # set convert_to_plugin and convert_to_ext
104 $self->set_standard_convert_settings();
105
106 my $secondary_plugin_name = $self->{'convert_to_plugin'};
107 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
108
109 if (!defined $secondary_plugin_options->{$secondary_plugin_name}) {
110 $secondary_plugin_options->{$secondary_plugin_name} = [];
111 }
112 my $specific_options = $secondary_plugin_options->{$secondary_plugin_name};
113
114 # using defaults for secondary plugins, taken from RTFPlugin
115 push(@$specific_options, "-file_rename_method", "none");
116 push(@$specific_options, "-extract_language") if $self->{'extract_language'};
117 if ($secondary_plugin_name eq "TextPlugin") {
118 push(@$specific_options, "-input_encoding", "utf8");
119 }
120 elsif ($secondary_plugin_name eq "HTMLPlugin") {
121 push(@$specific_options, "-description_tags") if $self->{'description_tags'};
122 push(@$specific_options, "-processing_tmp_files");
123 }
124 elsif ($secondary_plugin_name eq "PagedImagePlugin") {
125 push(@$specific_options, "-screenviewsize", "1000");
126 push(@$specific_options, "-enable_cache");
127 push(@$specific_options, "-processing_tmp_files");
128 }
129
130 # bless again, copied from PDFPlugin, PowerPointPlugin
131 $self = bless $self, $class;
132 $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
133 return $self;
134}
135
136# Are init, begin and deinit necessary (will they not get called automatically)?
137# Copied here from PDFPlugin, PowerPointPlugin
138# https://stackoverflow.com/questions/42885207/why-doesnt-class-supernew-call-the-constructors-of-all-parent-classes-when
139# "$class->SUPER::new always calls A::new because A comes before B in @ISA. See method resolution order in perlobj: ..."
140# https://stackoverflow.com/questions/15414696/when-using-multiple-inheritance-in-perl-is-there-a-way-to-indicate-which-super-f
141sub init {
142 my $self = shift (@_);
143
144 # ConvertBinaryFile init
145 $self->ConvertBinaryFile::init(@_);
146}
147
148sub begin {
149 my $self = shift (@_);
150
151 $self->ConvertBinaryFile::begin(@_);
152
153}
154
155sub deinit {
156 my $self = shift (@_);
157
158 $self->ConvertBinaryFile::deinit(@_);
159
160}
161
162# overridden to run the custom conversion command here in place of gsConvert.pl called by ConvertBinaryFile.pm
163sub tmp_area_convert_file {
164 # should we first hardlink the output files/folder to tmp area, so we won't be working across drives?
165
166 my $self = shift (@_);
167 my ($output_ext, $input_filename, $textref) = @_;
168
169 my $plugin_name = $self->{'plugin_type'}; # inherited from BaseImporter
170
171 # On Linux: if the program isn't installed, $? tends to come back with 127, in any case neither 0 nor 1.
172 # On Windows: echo %ERRORLEVEL% ends up as 9009 if the program is not installed.
173 # If running the command returns 0, let's assume success and so the act of running the command
174 # should produce either a text file or output to stdout.
175
176 my $outhandle=$self->{'outhandle'};
177
178 my $cmd = $self->{'exec_cmd'};
179 if(!$cmd) { # empty string for instance
180 print $outhandle "$plugin_name Conversion error: invalid cmd $cmd\n";
181 return "";
182 }
183
184 # replace occurrences of '*' placeholder in cmd string with input filename
185 my ($tailname, $dir, $suffix) = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
186 $cmd =~ s/\*/$tailname/g;
187 print STDERR "@@@@ $plugin_name: executing conversion cmd $cmd\n";
188 my $status = system($cmd);
189
190 if($status == 127 || $status == 9009) { # means the cmd isn't recognised on Unix and Windows, respectively
191 print $outhandle "$plugin_name Conversion error: cmd unrecognised, may not be installed (got $status when running $cmd)\n";
192 return "";
193 }
194
195 if($status != 0) {
196 print $outhandle "$plugin_name Conversion error: conversion failed with exit value $status\n";
197 return "";
198 }
199
200 my $output_file_or_dir = $self->{'output_file_or_dir_name'};
201 if (!-e $output_file_or_dir) {
202 print $outhandle "$plugin_name Conversion error: Output file/dir $output_file_or_dir doesn't exist\n";
203 return "";
204 }
205
206 # else, conversion success
207
208 # if multiple images were generated by running the conversion
209 if ($self->{'convert_to'} eq "pagedimg") {
210 my $item_filename = $self->generate_item_file($output_file_or_dir);
211 return $item_filename;
212 }
213
214 return $output_file_or_dir;
215}
216
217# Copied from PowerPointPlugin, with some modifications
218# override default read in some situations, as the conversion of ppt to html results in many files, and we want them all to be processed.
219sub read {
220 my $self = shift (@_);
221 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
222
223 # can we process this file??
224 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
225
226 return undef unless $self->can_process_this_file($filename_full_path);
227
228 my $output_file_or_dir = $self->{'output_file_or_dir_name'};
229 my $is_output_dir = (-d $output_file_or_dir) ? 1 : 0;
230
231 # we are only doing something special if we have a directory of html files
232 if (!$is_output_dir || $self->{'convert_to'} ne "html") {
233 return $self->BaseImporter::read(@_); # no read in ConvertBinaryFile.pm
234 }
235 my $outhandle = $self->{'outhandle'};
236 print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
237 print $outhandle "$self->{'plugin_type'} processing $file\n"
238 if $self->{'verbosity'} > 1;
239
240 my $conv_filename = $self->tmp_area_convert_file("html", $filename_full_path); # uses our overridden version
241 if ("$conv_filename" eq "") {return -1;} # had an error, will be passed down pipeline
242 if (! -e "$conv_filename") {return -1;}
243
244 my ($tailname, $html_dirname, $suffix)
245 = &File::Basename::fileparse($conv_filename, "\\.[^\\.]+\$");
246
247 my $collect_file = &util::filename_within_collection($filename_full_path);
248 my $dirname_within_collection = &util::filename_within_collection($html_dirname);
249 my $secondary_plugin = $self->{'secondary_plugins'}->{"HTMLPlugin"};
250
251 my @dir;
252 if (!opendir (DIR, $html_dirname)) {
253 print $outhandle "PowerPointPlugin: Couldn't read directory $html_dirname\n";
254 # just process the original file
255 @dir = ("$tailname.$suffix");
256
257 } else {
258 @dir = readdir (DIR);
259 closedir (DIR);
260 }
261
262 foreach my $file (@dir) {
263 next unless $file =~ /\.html$/;
264
265 my ($rv, $doc_obj) =
266 $secondary_plugin->read_into_doc_obj ($pluginfo,"", &util::filename_cat($html_dirname,$file), $block_hash, {}, $processor, $maxdocs, $total_count, $gli);
267 if ((!defined $rv) || ($rv<1)) {
268 # wasn't processed
269 return $rv;
270 }
271
272 # next block copied from ConvertBinaryFile
273 # from here ...
274 # Override previous gsdlsourcefilename set by secondary plugin
275
276 $doc_obj->set_source_filename ($collect_file, $self->{'file_rename_method'});
277 ## set_source_filename does not set the doc_obj source_path which is used in archives dbs for incremental
278 # build. so set it manually.
279 $doc_obj->set_source_path($filename_full_path);
280 $doc_obj->set_converted_filename(&util::filename_cat($dirname_within_collection, $file));
281
282 my $plugin_filename_encoding = $self->{'filename_encoding'};
283 my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
284 $self->set_Source_metadata($doc_obj, $filename_full_path,$filename_encoding);
285
286 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
287 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename_full_path));
288
289
290 my ($tailname, $dirname, $suffix)
291 = &File::Basename::fileparse($filename_full_path, "\\.[^\\.]+\$");
292 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FilenameRoot", $tailname);
293
294
295 my $topsection = $doc_obj->get_top_section();
296 $self->add_associated_files($doc_obj, $filename_full_path);
297
298 # extra_metadata is already called by sec plugin in process??
299 $self->extra_metadata($doc_obj, $topsection, $metadata); # do we need this here??
300 # do any automatic metadata extraction
301 $self->auto_extract_metadata ($doc_obj);
302
303 # have we found a Title??
304 $self->title_fallback($doc_obj,$topsection,$filename_no_path);
305
306 # use the one generated by HTMLPlugin, otherwise they all end up with same id.
307 #$self->add_OID($doc_obj);
308 # to here...
309
310 # process it
311 $processor->process($doc_obj);
312 undef $doc_obj;
313 }
314 $self->{'num_processed'} ++;
315
316 # deleted some commented out code here that exists in PowerPointPlugin
317
318 # for UnknownConverterPlugin, don't delete any temp files that the conversion may have created
319 # as we don't know where it was created
320 #$self->clean_up_after_doc_obj_processing();
321
322
323 # if process_status == 1, then the file has been processed.
324 return 1;
325
326}
327
328# use the read_into_doc_obj inherited from ConvertBinaryFile "to call secondary plugin stuff"
329sub read_into_doc_obj {
330 my $self = shift (@_);
331 $self->ConvertBinaryFile::deinit(@_);
332}
333
334sub process {
335 my $self = shift (@_);
336 $self->UnknownPlugin::process(@_);
337}
338
339# do we also need a html_multi option to convert_to?
340# move the following, copied from PPT Plugin, into parent ConvertBinaryPlugin, as it's now shared
341sub generate_item_file {
342 my $self = shift(@_);
343 my ($input_filename) = @_;
344 my $outhandle = $self->{'outhandle'};
345 my ($tailname, $dirname, $suffix)
346 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
347
348 my $plugin_name = $self->{'plugin_type'}; # inherited from BaseImporter
349
350 # find all the files in the directory
351 if (!opendir (DIR, $dirname)) {
352 print $outhandle "$plugin_name: Couldn't read directory $dirname\n";
353 return $input_filename;
354 }
355
356 my @dir = readdir (DIR);
357 closedir (DIR);
358
359 # start the item file
360 my $itemfile_name = &util::filename_cat($dirname, "$tailname.item");
361
362 # encoding specification????
363 if (!open (ITEMFILE, ">$itemfile_name")) {
364 print $outhandle "$plugin_name: Couldn't open $itemfile_name for writing\n";
365 }
366 print ITEMFILE "<GeneratedBy>$plugin_name\n";
367 # print the first page
368 my @sorted_dir = sort alphanum_sort @dir;
369 for (my $i = 0; $i < scalar(@sorted_dir); $i++) {
370 my $file = $sorted_dir[$i];
371 if ($file =~ /^img(\d+)\.jpg$/) {
372 my $num = $1;
373 $self->tidy_up_html(&util::filename_cat($dirname, "text$num.html"));
374 print ITEMFILE "$num:img$num.jpg:text$num.html:\n";
375 }
376 }
377 close ITEMFILE;
378 return $itemfile_name;
379
380}
Note: See TracBrowser for help on using the repository browser.