source: main/trunk/greenstone2/perllib/plugins/PowerPointPlugin.pm@ 22882

Last change on this file since 22882 was 22882, checked in by kjdon, 14 years ago

set up convert_to list for the case when windows_scripting and openoffice_conversion are both available

  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
RevLine 
[2981]1###########################################################################
2#
[17722]3# PowerPointPlugin.pm -- plugin for importing Microsoft PowerPoint files.
[22709]4# (basic version supports versions 95 and 97)
[22861]5# (through OpenOffice extension, supports all contemporary formats)
[2981]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) 2002 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
[17722]29package PowerPointPlugin;
[2981]30
[10254]31use strict;
32no strict 'refs'; # allow filehandles to be variables and viceversa
[22515]33no strict 'subs';
[22705]34
[22515]35use gsprintf 'gsprintf';
[2981]36
[22861]37use AutoLoadConverters;
38use ConvertBinaryFile;
[2981]39
[22861]40sub BEGIN {
41 @PowerPointPlugin::ISA = ('ConvertBinaryFile', 'AutoLoadConverters');
42}
[22705]43
[22861]44my $openoffice_available = 0;
[22705]45
[22515]46my $windows_convert_to_list =
[10466]47 [ { 'name' => "auto",
[15872]48 'desc' => "{ConvertBinaryFile.convert_to.auto}" },
[10466]49 { 'name' => "html",
[15872]50 'desc' => "{ConvertBinaryFile.convert_to.html}" },
[10466]51 { 'name' => "text",
[15872]52 'desc' => "{ConvertBinaryFile.convert_to.text}" },
[10466]53 { 'name' => "pagedimg_jpg",
[22882]54 'desc' => "{PowerPointPlugin.convert_to.pagedimg_jpg}" },
[10466]55 { 'name' => "pagedimg_gif",
[22882]56 'desc' => "{PowerPointPlugin.convert_to.pagedimg_gif}" },
[10466]57 { 'name' => "pagedimg_png",
[22882]58 'desc' => "{PowerPointPlugin.convert_to.pagedimg_png}" }
[10466]59 ];
60
[22861]61my $openoffice_convert_to_list =
62 [ { 'name' => "auto",
63 'desc' => "{ConvertBinaryFile.convert_to.auto}" },
[22880]64 { 'name' => "html_multi",
65 'desc' => "{PowerPointPlugin.convert_to.html_multi}" },
[22861]66 { 'name' => "text",
67 'desc' => "{ConvertBinaryFile.convert_to.text}" },
68 { 'name' => "pagedimg",
69 'desc' => "{PowerPointPlugin.convert_to.pagedimg}" }
70 ];
71
[22882]72my $openoffice_extra_convert_to_list =
73 [ { 'name' => "html_multi",
74 'desc' => "{PowerPointPlugin.convert_to.html_multi}" },
75 { 'name' => "pagedimg",
76 'desc' => "{PowerPointPlugin.convert_to.pagedimg}" }
77 ];
78
[6408]79my $arguments =
80 [ { 'name' => "process_exp",
[15872]81 'desc' => "{BasePlugin.process_exp}",
[6408]82 'type' => "regexp",
83 'reqd' => "no",
[22861]84 'deft' => "&get_default_process_exp()", # delayed (see below)
[22709]85 }
[6408]86 ];
87
[22515]88my $opt_windows_args =
89 [ { 'name' => "convert_to",
90 'desc' => "{ConvertBinaryFile.convert_to}",
91 'type' => "enum",
92 'reqd' => "yes",
93 'list' => $windows_convert_to_list,
94 'deft' => "html" },
95 { 'name' => "windows_scripting",
96 'desc' => "{PowerPointPlugin.windows_scripting}",
97 'type' => "flag",
98 'reqd' => "no" }
99 ];
100
[22861]101my $opt_office_args =
102 [ { 'name' => "convert_to",
103 'desc' => "{ConvertBinaryFile.convert_to}",
104 'type' => "enum",
105 'reqd' => "yes",
106 'list' => $openoffice_convert_to_list,
107 'deft' => "html" }
108 ];
109
[17722]110my $options = { 'name' => "PowerPointPlugin",
[17744]111 'desc' => "{PowerPointPlugin.desc}",
[6408]112 'abstract' => "no",
[11679]113 'inherits' => "yes",
[15114]114 'srcreplaceable' => "yes", # Source docs in PPT format can be replaced with GS-generated html
[6408]115 'args' => $arguments };
[4744]116
[2981]117sub new {
[10218]118 my ($class) = shift (@_);
119 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
120 push(@$pluginlist, $class);
[2981]121
[22861]122 # this bit needs to happen later after the arguments array has been
123 # finished - used for parsing the input args.
124 # push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
125 # this one needs to go in first, to get the print info in the right order
126 push(@{$hashArgOptLists->{"OptList"}},$options);
127
[22882]128 my $auto_converter_self = new AutoLoadConverters($pluginlist,$inputargs,$hashArgOptLists,["OpenOfficeConverter"],1);
129
[10275]130 if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
[22882]131 if ($auto_converter_self->{'openoffice_available'}) {
132 # add openoffice convert_to options into list
133 push (@$windows_convert_to_list, @$openoffice_extra_convert_to_list);
134 }
[22515]135 push(@$arguments,@$opt_windows_args);
[10275]136 }
[22882]137 elsif ($auto_converter_self->{'openoffice_available'}) {
[22861]138 push (@$arguments,@$opt_office_args);
139 $openoffice_available = 1;
140 }
141 # TODO need to do the case where they are both enabled!!! what will the convert to list be???
[10427]142
[22861]143 # evaluate the default for process_exp - it needs to be delayed till here so we know if openoffice is available or not. But needs to be done before parsing the args.
144 foreach my $a (@$arguments) {
[22709]145 if ($a->{'name'} eq "process_exp") {
146 my $eval_expr = $a->{'deft'};
147 $a->{'deft'} = eval "$eval_expr";
[22861]148 last;
[22709]149 }
150 }
151
[22861]152 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
153
154 my $cbf_self = new ConvertBinaryFile($pluginlist, $inputargs, $hashArgOptLists);
155 my $self = BasePlugin::merge_inheritance($auto_converter_self, $cbf_self);
156
[10580]157 if ($self->{'info_only'}) {
158 # don't worry about any options etc
159 return bless $self, $class;
160 }
161
[22861]162 $self = bless $self, $class;
[15872]163 $self->{'file_type'} = "PPT";
164
[22597]165 if ($self->{'convert_to'} eq "auto") {
166 if ($self->{'windows_scripting'}) {
167 $self->{'convert_to'} = "pagedimg_jpg";
168 }
169 else {
170 $self->{'convert_to'} = "html";
171 }
172 }
173
174 my $outhandle = $self->{'outhandle'};
175
[22861]176 # can't have windows_scripting and openoffice_conversion at the same time
177 if ($self->{'windows_scripting'} && $self->{'openoffice_conversion'}) {
178 print $outhandle "Warning: Cannot have -windows_scripting and -openoffice_conversion\n";
[22515]179 print $outhandle " on at the same time. Defaulting to -windows_scripting\n";
[22861]180 $self->{'openoffice_conversion'} = 0;
[22515]181 }
182
[15872]183 #these are passed through to gsConvert.pl by ConvertBinaryFile.pm
[10491]184 $self->{'convert_options'} = "-windows_scripting" if $self->{'windows_scripting'};
[22597]185
186 # set convert_to_plugin and convert_to_ext
[22640]187 $self->set_standard_convert_settings();
[22597]188
189 my $secondary_plugin_name = $self->{'convert_to_plugin'};
[10275]190 my $secondary_plugin_options = $self->{'secondary_plugin_options'};
[10218]191
[22597]192 if (!defined $secondary_plugin_options->{$secondary_plugin_name}) {
193 $secondary_plugin_options->{$secondary_plugin_name} = [];
[10275]194 }
[22597]195 my $specific_options = $secondary_plugin_options->{$secondary_plugin_name};
196
197 push(@$specific_options, "-file_rename_method", "none");
198 push(@$specific_options, "-extract_language") if $self->{'extract_language'};
199
200 if ($secondary_plugin_name eq "HTMLPlugin") {
201 push(@$specific_options, "-processing_tmp_files");
202 push(@$specific_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
[18406]203 }
[22597]204 elsif ($secondary_plugin_name eq "PagedImagePlugin") {
205 push(@$specific_options, "-processing_tmp_files");
[21958]206 #is this true??
[22597]207 push(@$specific_options,"-input_encoding", "utf8");
[22871]208 if ($self->{'openoffice_conversion'}) {
209 push(@$specific_options, "-create_thumbnail", "false", "-create_screenview", "false");
210 }
[15903]211 }
[2981]212
[10275]213 $self = bless $self, $class;
214
[10427]215 $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);
[10275]216 return $self;
[2981]217}
218
219sub get_default_process_exp {
220 my $self = shift (@_);
[22709]221
[22861]222 if ($openoffice_available) {
[22709]223 return q^(?i)\.(ppt|pptx|odp)$^;
224 }
225
[2981]226 return q^(?i)\.ppt$^;
227}
[10275]228
[22861]229sub init {
230 my $self = shift (@_);
231
232 # ConvertBinaryFile init
233 $self->SUPER::init(@_);
234 $self->AutoLoadConverters::init();
235
236}
237
238sub begin {
239 my $self = shift (@_);
240
241 $self->AutoLoadConverters::begin();
242 $self->SUPER::begin(@_);
243
244}
245
246sub deinit {
247 my $self = shift (@_);
248
249 $self->AutoLoadConverters::deinit();
250 $self->SUPER::deinit(@_);
251
252}
253
254# override AutoLoadConverters version, as we need to do more stuff once its converted if we are converting to item file
255sub tmp_area_convert_file {
256 my $self = shift (@_);
257 my ($output_ext, $input_filename, $textref) = @_;
258
259 if ($self->{'openoffice_conversion'}) {
260 if ($self->{'convert_to'} eq "pagedimg") {
261 $output_ext = "html"; # first convert to html
262 }
263 my ($result, $result_str, $new_filename) = $self->OpenOfficeConverter::convert($input_filename, $output_ext);
264 if ($result == 0) {
265 my $outhandle=$self->{'outhandle'};
266 print $outhandle "OpenOfficeConverter Conversion error\n";
267 print $outhandle $result_str;
268 return "";
269
270 }
271 #print STDERR "result = $result\n";
272 if ($self->{'convert_to'} eq "pagedimg") {
[22871]273 my $item_filename = $self->generate_item_file($new_filename);
274 return $item_filename;
[22861]275 }
276 return $new_filename;
277
278 }
279 else {
280 return $self->ConvertBinaryFile::tmp_area_convert_file(@_);
281 }
282 # get tmp filename
283}
284
285# 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.
[22880]286sub read {
[22861]287 my $self = shift (@_);
288 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
289
290 # can we process this file??
291 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
292
293 return undef unless $self->can_process_this_file($filename_full_path);
294
[22880]295 # we are only doing something special for html_multi
296 if (!($self->{'openoffice_conversion'} && $self->{'convert_to'} eq "html_multi")) {
297 return $self->BasePlugin::read(@_);
298 }
299 my $outhandle = $self->{'outhandle'};
300 print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
301 print $outhandle "$self->{'plugin_type'} processing $file\n"
302 if $self->{'verbosity'} > 1;
303
304 my $conv_filename = $self->tmp_area_convert_file("html", $filename_full_path);
305 if ("$conv_filename" eq "") {return -1;} # had an error, will be passed down pipeline
306 if (! -e "$conv_filename") {return -1;}
307
308 my ($tailname, $html_dirname, $suffix)
309 = &File::Basename::fileparse($conv_filename, "\\.[^\\.]+\$");
310
311 my $collect_file = &util::filename_within_collection($filename_full_path);
312 my $dirname_within_collection = &util::filename_within_collection($html_dirname);
313 my $secondary_plugin = $self->{'secondary_plugins'}->{"HTMLPlugin"};
314
315 my @dir;
316 if (!opendir (DIR, $html_dirname)) {
317 print $outhandle "PowerPointPlugin: Couldn't read directory $html_dirname\n";
318 # just process the original file
319 @dir = ("$tailname.$suffix");
320
321 } else {
322 @dir = readdir (DIR);
323 closedir (DIR);
324 }
325
326 foreach my $file (@dir) {
327 next unless $file =~ /\.html$/;
328
329 my ($rv, $doc_obj) =
330 $secondary_plugin->read_into_doc_obj ($pluginfo,"", &util::filename_cat($html_dirname,$file), $block_hash, {}, $processor, $maxdocs, $total_count, $gli);
331 if ((!defined $rv) || ($rv<1)) {
332 # wasn't processed
333 return $rv;
334 }
335
336 # next block copied from ConvertBinaryFile
337 # from here ...
338 # Override previous gsdlsourcefilename set by secondary plugin
339
340 $doc_obj->set_source_filename ($collect_file, $self->{'file_rename_method'});
341 ## set_source_filename does not set the doc_obj source_path which is used in archives dbs for incremental
342 # build. so set it manually.
343 $doc_obj->{'source_path'} = $filename_full_path;
344 $doc_obj->set_converted_filename(&util::filename_cat($dirname_within_collection, $file));
345
346 $self->set_Source_metadata($doc_obj, $filename_no_path);
347
348 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
349 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename_full_path));
350
351
352 my ($tailname, $dirname, $suffix)
353 = &File::Basename::fileparse($filename_full_path, "\\.[^\\.]+\$");
354 $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FilenameRoot", $tailname);
355
356
357 my $topsection = $doc_obj->get_top_section();
358 $self->add_associated_files($doc_obj, $filename_full_path);
359
360 # extra_metadata is already called by sec plugin in process??
361 $self->extra_metadata($doc_obj, $topsection, $metadata); # do we need this here??
362 # do any automatic metadata extraction
363 $self->auto_extract_metadata ($doc_obj);
364
365 # have we found a Title??
366 $self->title_fallback($doc_obj,$topsection,$filename_no_path);
367
368 # use the one generated by HTMLPlugin, otherwise they all end up with same id.
369 #$self->add_OID($doc_obj);
370 # to here...
371
372 # process it
373 $processor->process($doc_obj);
374 undef $doc_obj;
375 }
376 $self->{'num_processed'} ++;
377
378# my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
[22861]379
[22880]380# if ((defined $process_status) && ($process_status == 1)) {
[22861]381
382 # process the document
[22880]383# $processor->process($doc_obj);
[22861]384
[22880]385# $self->{'num_processed'} ++;
386# undef $doc_obj;
387# }
[22861]388 # delete any temp files that we may have created
389 $self->clean_up_after_doc_obj_processing();
390
391
392 # if process_status == 1, then the file has been processed.
[22880]393 return 1;
[22861]394
395}
396
[22871]397sub generate_item_file
398{
399 my $self = shift(@_);
400 my ($input_filename) = @_;
401 my $outhandle = $self->{'outhandle'};
402 my ($tailname, $dirname, $suffix)
403 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
404
405 # find all the files in the directory
406 if (!opendir (DIR, $dirname)) {
407 print $outhandle "PowerPointPlugin: Couldn't read directory $dirname\n";
408 return $input_filename;
409 }
410
411 my @dir = readdir (DIR);
412 closedir (DIR);
413
414 # start the item file
415 my $itemfile_name = &util::filename_cat($dirname, "$tailname.item");
416
417 # encoding specification????
418 if (!open (ITEMFILE, ">$itemfile_name")) {
419 print $outhandle "PowerPOintPlugin: Couldn't open $itemfile_name for writing\n";
420 }
421 print ITEMFILE "<GeneratedBy>PowerPointPlugin\n";
422 # print the first page
423 my @sorted_dir = sort alphanum_sort @dir;
424 for (my $i = 0; $i < scalar(@sorted_dir); $i++) {
425 my $file = $sorted_dir[$i];
426 if ($file =~ /^img(\d+)\.jpg$/) {
427 my $num = $1;
428 $self->tidy_up_html(&util::filename_cat($dirname, "text$num.html"));
429 print ITEMFILE "$num:img$num.jpg:text$num.html:\n";
430 }
431 }
432 close ITEMFILE;
433 return $itemfile_name;
434
435
436}
437
438# want to sort img1, img2, ...img10, img11 etc.
439sub alphanum_sort {
440
441 my ($a_txt, $a_num) = $a =~ /^([^\d]*)(\d*)/;
442 my ($b_txt, $b_num) = $b =~ /^([^\d]*)(\d*)/;
443
444 if ($a_txt ne $b_txt) { return ($a cmp $b) };
445 return ($a_num <=> $b_num);
446}
447
448# Want to remove the line that links to first page, last page, next page, text etc.
449sub tidy_up_html {
450
451 my $self = shift(@_);
452 my ($filename) = @_;
453 return unless (-f $filename);
454 my $backup_filename = "$filename.bak";
455
456 &File::Copy::copy($filename, $backup_filename);
457
458 open (ORIGINAL, $backup_filename) || return;
459 open(HTMLFILE, ">$filename") || return;
460
461 my $line ="";
462 while ($line = <ORIGINAL>) {
463 if ($line =~ /\<body\>/) {
464 print HTMLFILE $line;
465 $line = <ORIGINAL>;
466 next if $line =~ /\<center\>/;
467 }
468 next if $line =~ /First page/;
469 print HTMLFILE ($line);
470 }
471
472 close HTMLFILE;
473 close ORIGINAL;
474}
[2981]4751;
[10275]476
Note: See TracBrowser for help on using the repository browser.