source: gs2-extensions/open-office/trunk/src/perllib/plugins/OpenOfficeConverter.pm@ 23731

Last change on this file since 23731 was 23731, checked in by ak19, 13 years ago

First part of changes for getting OpenOffice conversion to work on a remote WINDOWS Greenstone (Linux was working already).

  • Property svn:executable set to *
File size: 9.7 KB
Line 
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###########################################################################
27package OpenOfficeConverter;
28
29use ConvertBinaryFile;
30use BaseMediaConverter;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
35use gsprintf 'gsprintf';
36
37# these two variables mustn't be initialised here or they will get stuck
38# at those values.
39our $openoffice_conversion_available;
40our $no_openoffice_conversion_reason;
41
42BEGIN {
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","jodconverter.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 is in path
65 my $cmd = "soffice -headless 2>&1"; # for linux and mac
66
67 if ($ENV{'GSDLOS'} =~ m/^windows$/) {
68 if(!defined $ENV{'SOFFICE_HOST'}) {
69 $ENV{'SOFFICE_HOST'} = "localhost";
70 }
71 if(!defined $ENV{'SOFFICE_PORT'}) {
72 $ENV{'SOFFICE_PORT'} = "8200";
73 }
74 # important to have this set when the Greenstone server
75 # and open office is on a remote windows machine
76 if(!defined $ENV{'SOFFICE_HOME'}) {
77 $ENV{'SOFFICE_HOME'} = &util::filename_cat($ENV{'ProgramFiles'},"OpenOffice.org 3");
78 }
79 my $ooffice_dir_guess =
80 &util::filename_cat($ENV{'SOFFICE_HOME'},"program");
81 if (-d $ooffice_dir_guess) {
82 &util::envvar_append("PATH",$ooffice_dir_guess);
83 }
84
85 # for windows, when working on a remote system, want to be able to start OO if
86 # not already running. We'll use the uno socket method to do so. Else client-gli
87 # tends to hang, waiting for the prompt to return after OO has been started up
88 # (which doesn't happen, so need to Ctrl-C GLI and run it again for it to work).
89 $cmd = "start \"soffice process\" soffice \"-accept=socket,host=$ENV{'SOFFICE_HOST'},port=$ENV{'SOFFICE_PORT'};urp;StarOffice.ServiceManager\" -headless 2>&1";
90 $cmd .= " >nul";
91 #print STDERR "***** Tried to start-up OpenOffice with:\n$cmd\n";
92 }
93 else {
94 # Windows seems to launch OpenOffice as a service (i.e.
95 # automatically puts it in the background).
96 # For Unix putting it in the background needs to be done
97 # explicitly
98
99 $cmd .= " >/dev/null &";
100 }
101
102 my $status = system($cmd);
103 if ($status != 0) {
104 #print STDERR "Failed to run: $cmd\n";
105 #print STDERR "$!\n";
106 $openoffice_conversion_available = 0;
107 $no_openoffice_conversion_reason = "openofficenotinstalled";
108 }
109 }
110 }
111}
112
113my $arguments = [
114 { 'name' => "openoffice_port",
115 'desc' => "{OpenOfficeConverter.openoffice_port}",
116 'type' => "int",
117 'deft' => "8100",
118 'range' => "81,",
119 'reqd' => "no" },
120 ];
121
122
123my $options = { 'name' => "OpenOfficeConverter",
124 'desc' => "{OpenOfficeConverter.desc}",
125 'abstract' => "yes",
126 'inherits' => "yes",
127 'args' => $arguments };
128
129sub new {
130 my ($class) = shift (@_);
131 my ($pluginlist,$inputargs,$hashArgOptLists,$auxilary) = @_;
132 push(@$pluginlist, $class);
133
134 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
135 push(@{$hashArgOptLists->{"OptList"}},$options);
136
137 my $self = new BaseMediaConverter($pluginlist, $inputargs,
138 $hashArgOptLists, $auxilary);
139
140 if ($self->{'info_only'}) {
141 # don't worry about any options etc
142 return bless $self, $class;
143 }
144 if (!$openoffice_conversion_available) {
145 $self->{'no_openoffice_conversion_reason'} = $no_openoffice_conversion_reason;
146
147 my $outhandle = $self->{'outhandle'};
148 &gsprintf($outhandle, "OpenOfficeConverter: {OpenOfficeConverter.noconversionavailable} ({OpenOfficeConverter.$no_openoffice_conversion_reason})\n");
149 }
150
151 $self->{'openoffice_conversion_available'} = $openoffice_conversion_available;
152
153 return bless $self, $class;
154
155}
156
157sub init {
158 my $self = shift(@_);
159 my ($verbosity, $outhandle, $failhandle) = @_;
160
161 if ($openoffice_conversion_available) {
162 my $oo_port = $self->{'openoffice_port'};
163
164 my $launch_cmd = "soffice";
165 $launch_cmd .= " \"-accept=socket,host=localhost,port=$oo_port;urp;StarOffice.ServiceManager\"";
166 $launch_cmd .= " -headless";
167 $self->{'openoffice_launch_cmd'} = $launch_cmd;
168 }
169
170 $self->{'ootmp_file_paths'} = ();
171}
172
173sub deinit {
174 my $self = shift(@_);
175
176 $self->clean_up_temporary_files();
177}
178
179
180sub convert {
181 my $self = shift(@_);
182 my $source_file_full_path = shift(@_);
183 my $target_file_type = shift(@_);
184 my $convert_options = shift(@_) || "";
185 my $convert_id = shift(@_) || "";
186 my $cache_mode = shift(@_) || "";
187
188 return (0,undef,undef) unless $openoffice_conversion_available;
189 # check the filename
190 return (0,undef,undef) if ( !-f $source_file_full_path);
191
192 my $outhandle = $self->{'outhandle'};
193 my $verbosity = $self->{'verbosity'};
194
195 my $source_file_no_path = &File::Basename::basename($source_file_full_path);
196 # Determine the full name and path of the output file
197 my $target_file_path;
198 if ($self->{'enable_cache'}) {
199 $self->init_cache_for_file($source_file_full_path);
200 my $cache_dir = $self->{'cached_dir'};
201 my $file_root = $self->{'cached_file_root'};
202 $file_root .= "_$convert_id" if ($convert_id ne "");
203 my $target_file = "$file_root.$target_file_type";
204 $target_file_path = &util::filename_cat($cache_dir,$target_file);
205 }
206 else {
207 $target_file_path = &util::get_timestamped_tmp_filename_in_collection($source_file_full_path, $target_file_type);
208 push(@{$self->{'ootmp_file_paths'}}, $target_file_path);
209 }
210
211 # Generate and run the convert command
212
213 my $gextoo_home = $ENV{'GEXT_OPENOFFICE'};
214 my $jodjar = &util::filename_cat($gextoo_home,"lib","java","jodconverter.jar");
215
216 # debugging: print out all the env vars, when import's verbosity is high
217 #foreach my $key (sort(keys %ENV)) {
218 # print $outhandle "$key = $ENV{$key}\n";
219 #}
220
221# *If* SOFFICE_HOME\program was not added to the PATH, *another* way is to insert:
222# -Doffice.home=C:\\PROGRA~1\\OPENOF~1.ORG
223# into the command to launch jodconverter so that, when using the remote GS server
224# on Windows, jodconverter can find the openoffice installation:
225# my $office_short_path = Win32::GetShortPathName($ENV{'SOFFICE_HOME'});
226# my $convert_cmd = "java -Doffice.home=\"".$office_short_path."\" -jar \"$jodjar\"";
227
228 my $convert_cmd = "java -jar \"$jodjar\"";
229 $convert_cmd .= " \"$source_file_full_path\" \"$target_file_path\"";
230
231 if ($verbosity>2) {
232 print $outhandle "Convert command: $convert_cmd\n";
233 }
234
235 my $print_info = { 'message_prefix' => "OpenOffice Conversion",
236 'message' => "Converting $source_file_no_path to: $target_file_type" };
237 $print_info->{'cache_mode'} = $cache_mode if ($cache_mode ne "");
238
239 my ($regenerated,$result,$had_error)
240 = $self->autorun_general_cmd($convert_cmd,$source_file_full_path, $target_file_path,$print_info);
241 if ($had_error) {
242 return (0, $result,$target_file_path);
243 }
244 return (1, $result,$target_file_path);
245}
246
247
248sub convert_without_result {
249 my $self = shift(@_);
250
251 my $source_file_path = shift(@_);
252 my $target_file_type = shift(@_);
253 my $convert_options = shift(@_) || "";
254 my $convert_id = shift(@_) || "";
255
256 return $self->convert($source_file_path,$target_file_type,
257 $convert_options,$convert_id,"without_result");
258}
259
260
261sub tmp_area_convert_fileXX {
262 my $self = shift (@_);
263 my ($output_ext, $input_filename, $textref) = @_;
264
265 my $outhandle = $self->{'outhandle'};
266 my $convert_to = $self->{'convert_to'};
267 my $failhandle = $self->{'failhandle'};
268 my $convert_to_ext = $self->{'convert_to_ext'};
269
270 # derive tmp filename from input filename
271 my ($tailname, $dirname, $suffix)
272 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
273}
274
275
276sub clean_up_temporary_files {
277 my $self = shift(@_);
278
279 foreach my $ootmp_file_path (@{$self->{'ootmp_file_paths'}}) {
280 if (-e $ootmp_file_path) {
281 &util::rm($ootmp_file_path);
282 }
283 }
284
285 $self->{'ootmp_file_paths'} = ();
286}
287
288
289
2901;
Note: See TracBrowser for help on using the repository browser.