1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # OpenOfficeConverter - helper plugin that does office document conversion
|
---|
4 | # using jodconverter combined with OpenOffice
|
---|
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) 2010 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 | package OpenOfficeConverter;
|
---|
28 |
|
---|
29 | use ConvertBinaryFile;
|
---|
30 | use BaseMediaConverter;
|
---|
31 |
|
---|
32 | use strict;
|
---|
33 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
34 |
|
---|
35 | use gsprintf 'gsprintf';
|
---|
36 |
|
---|
37 | # these two variables mustn't be initialised here or they will get stuck
|
---|
38 | # at those values.
|
---|
39 | our $openoffice_conversion_available;
|
---|
40 | our $no_openoffice_conversion_reason;
|
---|
41 |
|
---|
42 | BEGIN {
|
---|
43 | @OpenOfficeConverter::ISA = ('BaseMediaConverter');
|
---|
44 |
|
---|
45 | # Check that OpenOffice and jodconverter are installed and available on
|
---|
46 | # the path
|
---|
47 | $openoffice_conversion_available = 1;
|
---|
48 | $no_openoffice_conversion_reason = "";
|
---|
49 |
|
---|
50 | if (! defined $ENV{'GEXT_OPENOFFICE'}) {
|
---|
51 | $openoffice_conversion_available = 0;
|
---|
52 | $no_openoffice_conversion_reason = "gextopenofficenotinstalled";
|
---|
53 | }
|
---|
54 | else {
|
---|
55 | my $gextoo_home = $ENV{'GEXT_OPENOFFICE'};
|
---|
56 | my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverterx.jar");
|
---|
57 |
|
---|
58 | if (!-e $jodjar) {
|
---|
59 | print STDERR "Failed to find $jodjar\n";
|
---|
60 | $openoffice_conversion_available = 0;
|
---|
61 | $no_openoffice_conversion_reason = "gextjodconverternotinstalled";
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | # test to see if soffice in in path
|
---|
65 | if ($ENV{'GSDLOS'} =~ m/^windows$/) {
|
---|
66 | my $ooffice_dir_guess =
|
---|
67 | &util::filename_cat($ENV{'ProgramFiles'},"OpenOffice.org 3",
|
---|
68 | "program");
|
---|
69 | if (-d $ooffice_dir_guess) {
|
---|
70 | &util::envvar_append("PATH",$ooffice_dir_guess);
|
---|
71 | }
|
---|
72 | }
|
---|
73 | my $cmd = "soffice -headless 2>&1";
|
---|
74 |
|
---|
75 | my $status = system($cmd);
|
---|
76 | if ($status != 0) {
|
---|
77 | print STDERR "Failed to run: $cmd\n";
|
---|
78 | print STDERR "$!\n";
|
---|
79 | $openoffice_conversion_available = 0;
|
---|
80 | $no_openoffice_conversion_reason = "openofficenotinstalled";
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | }
|
---|
86 |
|
---|
87 | my $arguments = [
|
---|
88 | { 'name' => "openoffice_port",
|
---|
89 | 'desc' => "{OpenOfficeConverter.openoffice_port}",
|
---|
90 | 'type' => "int",
|
---|
91 | 'deft' => "8100",
|
---|
92 | 'range' => "81,",
|
---|
93 | 'reqd' => "no" },
|
---|
94 | ];
|
---|
95 |
|
---|
96 | my $options = { 'name' => "OpenOfficeConverter",
|
---|
97 | 'desc' => "{OpenOfficeConverter.desc}",
|
---|
98 | 'abstract' => "yes",
|
---|
99 | 'inherits' => "yes",
|
---|
100 | 'args' => $arguments };
|
---|
101 |
|
---|
102 | sub new {
|
---|
103 | my ($class) = shift (@_);
|
---|
104 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
105 | push(@$pluginlist, $class);
|
---|
106 |
|
---|
107 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
108 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
109 |
|
---|
110 | my $self = new BaseMediaConverter($pluginlist, $inputargs, $hashArgOptLists);
|
---|
111 |
|
---|
112 | if ($self->{'info_only'}) {
|
---|
113 | # don't worry about any options etc
|
---|
114 | return bless $self, $class;
|
---|
115 | }
|
---|
116 | if ($openoffice_conversion_available) {
|
---|
117 | my $oo_port = $self->{'openoffice_port'};
|
---|
118 | my $launch_cmd = "soffice";
|
---|
119 | $launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
|
---|
120 | $launch_cmd .= " -headless";
|
---|
121 |
|
---|
122 | $self->{'openoffice_launch_cmd'} = $launch_cmd;
|
---|
123 | }
|
---|
124 | else {
|
---|
125 | $self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
|
---|
126 |
|
---|
127 | my $outhandle = $self->{'outhandle'};
|
---|
128 | &gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
|
---|
129 | }
|
---|
130 |
|
---|
131 | $self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
|
---|
132 |
|
---|
133 | return bless $self, $class;
|
---|
134 |
|
---|
135 | }
|
---|
136 |
|
---|
137 | sub init {
|
---|
138 | my $self = shift(@_);
|
---|
139 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
140 |
|
---|
141 | $self->{'ootmp_file_paths'} = ();
|
---|
142 | }
|
---|
143 |
|
---|
144 | sub deinit {
|
---|
145 | my $self = shift(@_);
|
---|
146 |
|
---|
147 | $self->clean_up_temporary_files();
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | sub convert {
|
---|
152 | my $self = shift(@_);
|
---|
153 | my ($source_file_full_path, $target_file_type) = @_;
|
---|
154 |
|
---|
155 | return 0 unless $openoffice_conversion_available;
|
---|
156 | # check the filename
|
---|
157 | return 0 if ( !-f $source_file_full_path);
|
---|
158 |
|
---|
159 | my $source_file_no_path = &File::Basename::basename($source_file_full_path);
|
---|
160 | # Determine the full name and path of the output file
|
---|
161 | my $target_file_path;
|
---|
162 | if ($self->{'enable_cache'}) {
|
---|
163 | $self->init_cache_for_file($source_file_full_path);
|
---|
164 | my $cache_dir = $self->{'cached_dir'};
|
---|
165 | my $file_root = $self->{'cached_file_root'};
|
---|
166 | #$file_root .= "_$convert_id" if ($convert_id ne "");
|
---|
167 | my $target_file = "$file_root.$target_file_type";
|
---|
168 | $target_file_path = &util::filename_cat($cache_dir,$target_file);
|
---|
169 | }
|
---|
170 | else {
|
---|
171 | # this is in gsdl/tmp. get a tmp filename in collection instead???
|
---|
172 | $target_file_path = &util::get_tmp_filename($target_file_type);
|
---|
173 | push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
|
---|
174 | }
|
---|
175 |
|
---|
176 | # Generate and run the convert command
|
---|
177 | my $convert_command = "oo2html \"$source_file_full_path\" \"$target_file_path\"";
|
---|
178 | print STDERR "convert_command = $convert_command\n";
|
---|
179 | my $print_info = { 'message_prefix' => "OpenOffice Conversion",
|
---|
180 | 'message' => "Converting $source_file_no_path to: $target_file_type" };
|
---|
181 | #$print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
|
---|
182 |
|
---|
183 | my ($regenerated,$result,$had_error)
|
---|
184 | = $self->autorun_general_cmd($convert_command,$source_file_full_path, $target_file_path,$print_info);
|
---|
185 | if ($had_error) {
|
---|
186 | return (0, $result,$target_file_path);
|
---|
187 | }
|
---|
188 | return (1, $result,$target_file_path);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 |
|
---|
193 | sub convert_orig {
|
---|
194 | my $self = shift(@_);
|
---|
195 | my $source_file_path = shift(@_);
|
---|
196 | my $target_file_type = shift(@_);
|
---|
197 | my $convert_options = shift(@_) || "";
|
---|
198 | my $convert_id = shift(@_) || "";
|
---|
199 | my $cache_mode = shift(@_) || "";
|
---|
200 |
|
---|
201 | my $outhandle = $self->{'outhandle'};
|
---|
202 | my $verbosity = $self->{'verbosity'};
|
---|
203 |
|
---|
204 | my $source_file_no_path = &File::Basename::basename($source_file_path);
|
---|
205 |
|
---|
206 | # Determine the full name and path of the output file
|
---|
207 | my $target_file_path;
|
---|
208 | if ($self->{'enable_cache'}) {
|
---|
209 | my $cached_image_dir = $self->{'cached_dir'};
|
---|
210 | my $image_root = $self->{'cached_file_root'};
|
---|
211 | $image_root .= "_$convert_id" if ($convert_id ne "");
|
---|
212 | my $image_file = "$image_root.$target_file_type";
|
---|
213 | $target_file_path = &util::filename_cat($cached_image_dir,$image_file);
|
---|
214 | }
|
---|
215 | else {
|
---|
216 | $target_file_path = &util::get_tmp_filename($target_file_type);
|
---|
217 | push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
|
---|
218 | }
|
---|
219 |
|
---|
220 | # Generate and run the convert command
|
---|
221 | my $convert_command = "oo2html $convert_options \"$source_file_path\" \"$target_file_path\"";
|
---|
222 |
|
---|
223 | my $print_info = { 'message_prefix' => $convert_id,
|
---|
224 | 'message' => "Converting $source_file_no_path to: $convert_id $target_file_type" };
|
---|
225 | $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
|
---|
226 |
|
---|
227 | my ($regenerated,$result,$had_error)
|
---|
228 | = $self->autorun_general_cmd($convert_command,$target_file_path,$print_info);
|
---|
229 |
|
---|
230 | return ($result,$target_file_path);
|
---|
231 | }
|
---|
232 |
|
---|
233 | sub convert_without_result {
|
---|
234 | my $self = shift(@_);
|
---|
235 |
|
---|
236 | my $source_file_path = shift(@_);
|
---|
237 | my $target_file_type = shift(@_);
|
---|
238 | my $convert_options = shift(@_) || "";
|
---|
239 | my $convert_id = shift(@_) || "";
|
---|
240 |
|
---|
241 | return $self->convert($source_file_path,$target_file_type,
|
---|
242 | $convert_options,$convert_id,"without_result");
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | sub tmp_area_convert_fileXX {
|
---|
247 | my $self = shift (@_);
|
---|
248 | my ($output_ext, $input_filename, $textref) = @_;
|
---|
249 |
|
---|
250 | my $outhandle = $self->{'outhandle'};
|
---|
251 | my $convert_to = $self->{'convert_to'};
|
---|
252 | my $failhandle = $self->{'failhandle'};
|
---|
253 | my $convert_to_ext = $self->{'convert_to_ext'};
|
---|
254 |
|
---|
255 | # derive tmp filename from input filename
|
---|
256 | my ($tailname, $dirname, $suffix)
|
---|
257 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | sub clean_up_temporary_files {
|
---|
262 | my $self = shift(@_);
|
---|
263 |
|
---|
264 | foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
|
---|
265 | if (-e $ootmp_file_path) {
|
---|
266 | &util::rm($ootmp_file_path);
|
---|
267 | }
|
---|
268 | }
|
---|
269 |
|
---|
270 | $self->{'ootmp_file_paths'} = ();
|
---|
271 | }
|
---|
272 |
|
---|
273 |
|
---|
274 |
|
---|
275 | 1;
|
---|