[10450] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15906] | 3 | # ConvertBinaryFile.pm -- plugin that facilitates conversion of binary files
|
---|
| 4 | # through gsConvert.pl
|
---|
[10450] | 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 |
|
---|
[17723] | 28 | # This plugin is inherited by such plugins as WordPlugin, PowerPointPlugin,
|
---|
[17726] | 29 | # PostScriptPlugin,
|
---|
[17723] | 30 | # RTFPlugin and PDFPlugin. It facilitates the conversion of these document types
|
---|
[15871] | 31 | # to either HTML, Text or a series of images. It works by dynamically loading
|
---|
[12741] | 32 | # an appropriate secondary plugin (HTMLPlug, StructuredHTMLPlug,
|
---|
[15871] | 33 | # PagedImagePlugin or TextPlugin) based on the plugin argument 'convert_to'.
|
---|
[12741] | 34 |
|
---|
[15871] | 35 | package ConvertBinaryFile;
|
---|
[10450] | 36 |
|
---|
[15906] | 37 | use AutoExtractMetadata;
|
---|
[10450] | 38 | use ghtml;
|
---|
[15871] | 39 | use HTMLPlugin;
|
---|
| 40 | use TextPlugin;
|
---|
| 41 | use PagedImagePlugin;
|
---|
[10450] | 42 |
|
---|
[10453] | 43 | use strict;
|
---|
| 44 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[11680] | 45 | no strict 'subs';
|
---|
[24362] | 46 | use util;
|
---|
[27509] | 47 | use FileUtils;
|
---|
[15871] | 48 |
|
---|
[24192] | 49 |
|
---|
[10450] | 50 | sub BEGIN {
|
---|
[15906] | 51 | @ConvertBinaryFile::ISA = ('AutoExtractMetadata');
|
---|
[10450] | 52 | }
|
---|
| 53 |
|
---|
| 54 | my $convert_to_list =
|
---|
| 55 | [ { 'name' => "auto",
|
---|
[15871] | 56 | 'desc' => "{ConvertBinaryFile.convert_to.auto}" },
|
---|
[10450] | 57 | { 'name' => "html",
|
---|
[15871] | 58 | 'desc' => "{ConvertBinaryFile.convert_to.html}" },
|
---|
[10450] | 59 | { 'name' => "text",
|
---|
[15871] | 60 | 'desc' => "{ConvertBinaryFile.convert_to.text}" }
|
---|
[10450] | 61 | ];
|
---|
| 62 |
|
---|
| 63 | my $arguments =
|
---|
| 64 | [ { 'name' => "convert_to",
|
---|
[15871] | 65 | 'desc' => "{ConvertBinaryFile.convert_to}",
|
---|
[10450] | 66 | 'type' => "enum",
|
---|
| 67 | 'reqd' => "yes",
|
---|
| 68 | 'list' => $convert_to_list,
|
---|
[10890] | 69 | 'deft' => "auto" },
|
---|
[12961] | 70 | { 'name' => "keep_original_filename",
|
---|
[15871] | 71 | 'desc' => "{ConvertBinaryFile.keep_original_filename}",
|
---|
[12961] | 72 | 'type' => "flag" },
|
---|
[10450] | 73 | { 'name' => "title_sub",
|
---|
[16013] | 74 | 'desc' => "{HTMLPlugin.title_sub}",
|
---|
[10450] | 75 | 'type' => "string",
|
---|
| 76 | #'type' => "regexp",
|
---|
| 77 | 'deft' => "" },
|
---|
[11008] | 78 | { 'name' => "apply_fribidi",
|
---|
[15871] | 79 | 'desc' => "{ConvertBinaryFile.apply_fribidi}",
|
---|
[11008] | 80 | 'type' => "flag",
|
---|
| 81 | 'reqd' => "no" },
|
---|
[10450] | 82 | { 'name' => "use_strings",
|
---|
[15871] | 83 | 'desc' => "{ConvertBinaryFile.use_strings}",
|
---|
[10450] | 84 | 'type' => "flag",
|
---|
| 85 | 'reqd' => "no" },
|
---|
[15871] | 86 | ];
|
---|
[10450] | 87 |
|
---|
[15871] | 88 | my $options = { 'name' => "ConvertBinaryFile",
|
---|
| 89 | 'desc' => "{ConvertBinaryFile.desc}",
|
---|
[10450] | 90 | 'abstract' => "yes",
|
---|
| 91 | 'inherits' => "yes",
|
---|
| 92 | 'args' => $arguments };
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 | sub load_secondary_plugins
|
---|
| 96 | {
|
---|
| 97 | my $self = shift (@_);
|
---|
| 98 | my ($class,$input_args,$hashArgOptLists) = @_;
|
---|
| 99 |
|
---|
[22597] | 100 | my @convert_to_list = split(",",$self->{'convert_to_plugin'});
|
---|
[10453] | 101 | my $secondary_plugins = {};
|
---|
[11680] | 102 | # find the plugin
|
---|
[10450] | 103 |
|
---|
| 104 | foreach my $convert_to (@convert_to_list) {
|
---|
| 105 | # load in "convert_to" plugin package
|
---|
[22597] | 106 | my $plugin_class = $convert_to;
|
---|
[10450] | 107 | my $plugin_package = $plugin_class.".pm";
|
---|
| 108 |
|
---|
[15116] | 109 | my $colplugname = undef;
|
---|
| 110 | if (defined $ENV{'GSDLCOLLECTDIR'}) {
|
---|
[27509] | 111 | $colplugname = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},
|
---|
[15116] | 112 | "perllib","plugins",
|
---|
| 113 | $plugin_package);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[27509] | 116 | my $mainplugname = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},
|
---|
[15116] | 117 | "perllib","plugins",
|
---|
[11680] | 118 | $plugin_package);
|
---|
[10450] | 119 |
|
---|
[15116] | 120 | if ((defined $colplugname) && (-e $colplugname)) { require $colplugname;}
|
---|
[11680] | 121 | elsif (-e $mainplugname) { require $mainplugname; }
|
---|
| 122 | else {
|
---|
| 123 | &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
|
---|
| 124 | $plugin_class);
|
---|
| 125 | die "\n";
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[10450] | 128 | # call its constructor with extra options that we've worked out!
|
---|
| 129 | my $arglist = $input_args->{$plugin_class};
|
---|
[11680] | 130 |
|
---|
| 131 | my ($secondary_plugin);
|
---|
| 132 | eval("\$secondary_plugin = new $plugin_class([],\$arglist)");
|
---|
| 133 | die "$@" if $@;
|
---|
[10450] | 134 | $secondary_plugins->{$plugin_class} = $secondary_plugin;
|
---|
| 135 | }
|
---|
| 136 | $self->{'secondary_plugins'} = $secondary_plugins;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | sub new {
|
---|
| 140 | my ($class) = shift (@_);
|
---|
| 141 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 142 | push(@$pluginlist, $class);
|
---|
| 143 | my $classPluginName = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
|
---|
[15871] | 144 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 145 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10450] | 146 |
|
---|
[15906] | 147 | my $self = new AutoExtractMetadata($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[22597] | 148 |
|
---|
| 149 | return bless $self, $class;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | # should be called by subclasses after checking and setting
|
---|
| 153 | # $self->{'convert_to'}
|
---|
| 154 | sub set_standard_convert_settings {
|
---|
| 155 | my $self =shift (@_);
|
---|
[10453] | 156 |
|
---|
[22597] | 157 | my $convert_to = $self->{'convert_to'};
|
---|
| 158 | if ($convert_to eq "auto") {
|
---|
| 159 | $convert_to = "html";
|
---|
| 160 | $self->{'convert_to'} = "html";
|
---|
[11680] | 161 | }
|
---|
| 162 |
|
---|
[22879] | 163 | if ($convert_to =~ /^html/) { # may be html or html_multi
|
---|
[22597] | 164 | $self->{'convert_to_plugin'} = "HTMLPlugin";
|
---|
[10450] | 165 | $self->{'convert_to_ext'} = "html";
|
---|
[22597] | 166 | } elsif ($convert_to eq "text") {
|
---|
| 167 | $self->{'convert_to_plugin'} = "TextPlugin";
|
---|
[10450] | 168 | $self->{'convert_to_ext'} = "txt";
|
---|
[22597] | 169 | } elsif ($convert_to eq "structuredhtml") {
|
---|
| 170 | $self->{'convert_to_plugin'} = "StructuredHTMLPlugin";
|
---|
[10453] | 171 | $self->{'convert_to_ext'} = "html";
|
---|
[22597] | 172 | } elsif ($convert_to =~ /^pagedimg/) {
|
---|
| 173 | $self->{'convert_to_plugin'} = "PagedImagePlugin";
|
---|
| 174 | my ($convert_to_ext) = $convert_to =~ /pagedimg\_(jpg|gif|png)/i;
|
---|
[10453] | 175 | $convert_to_ext = 'jpg' unless defined $convert_to_ext;
|
---|
| 176 | $self->{'convert_to_ext'} = $convert_to_ext;
|
---|
[10450] | 177 | }
|
---|
| 178 | }
|
---|
| 179 | sub init {
|
---|
| 180 | my $self = shift (@_);
|
---|
| 181 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
| 182 |
|
---|
| 183 | $self->SUPER::init($verbosity,$outhandle,$failhandle);
|
---|
| 184 |
|
---|
| 185 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 186 |
|
---|
| 187 | foreach my $plug_name (keys %$secondary_plugins) {
|
---|
| 188 | my $plugin = $secondary_plugins->{$plug_name};
|
---|
| 189 | $plugin->init($verbosity,$outhandle,$failhandle);
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | sub deinit {
|
---|
| 194 | # called only once, after all plugin passes have been done
|
---|
| 195 |
|
---|
| 196 | my ($self) = @_;
|
---|
| 197 |
|
---|
| 198 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 199 |
|
---|
| 200 | foreach my $plug_name (keys %$secondary_plugins) {
|
---|
| 201 | my $plugin = $secondary_plugins->{$plug_name};
|
---|
| 202 | $plugin->deinit();
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | sub convert_post_process
|
---|
| 207 | {
|
---|
| 208 | # by default do no post processing
|
---|
| 209 | return;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | # Run conversion utility on the input file.
|
---|
| 214 | #
|
---|
| 215 | # The conversion takes place in a collection specific 'tmp' directory so
|
---|
| 216 | # that we don't accidentally damage the input.
|
---|
| 217 | #
|
---|
| 218 | # The desired output type is indicated by $output_ext. This is usually
|
---|
| 219 | # something like "html" or "word", but can be "best" (or the empty string)
|
---|
| 220 | # to indicate that the conversion utility should do the best it can.
|
---|
| 221 | sub tmp_area_convert_file {
|
---|
| 222 | my $self = shift (@_);
|
---|
| 223 | my ($output_ext, $input_filename, $textref) = @_;
|
---|
| 224 |
|
---|
| 225 | my $outhandle = $self->{'outhandle'};
|
---|
| 226 | my $convert_to = $self->{'convert_to'};
|
---|
| 227 | my $failhandle = $self->{'failhandle'};
|
---|
| 228 | my $convert_to_ext = $self->{'convert_to_ext'};
|
---|
| 229 |
|
---|
[23387] | 230 |
|
---|
| 231 | my $upgraded_input_filename = &util::upgrade_if_dos_filename($input_filename);
|
---|
| 232 |
|
---|
[10450] | 233 | # derive tmp filename from input filename
|
---|
| 234 | my ($tailname, $dirname, $suffix)
|
---|
[23387] | 235 | = &File::Basename::fileparse($upgraded_input_filename, "\\.[^\\.]+\$");
|
---|
[10450] | 236 |
|
---|
[15116] | 237 | # softlink to collection tmp dir
|
---|
[22887] | 238 | my $tmp_dirname = &util::get_timestamped_tmp_folder();
|
---|
| 239 | if (defined $tmp_dirname) {
|
---|
| 240 | $self->{'tmp_dir'} = $tmp_dirname;
|
---|
| 241 | } else {
|
---|
| 242 | $tmp_dirname = $dirname;
|
---|
[15116] | 243 | }
|
---|
[22887] | 244 |
|
---|
[23387] | 245 | # # convert to utf-8 otherwise we have problems with the doc.xml file later on
|
---|
| 246 | # my $utf8_tailname = (&unicode::check_is_utf8($tailname)) ? $tailname : $self->filepath_to_utf8($tailname);
|
---|
[12688] | 247 |
|
---|
[23387] | 248 | # make sure filename to be used can be stored OK in a UTF-8 compliant doc.xml file
|
---|
| 249 | my $utf8_tailname = &unicode::raw_filename_to_utf8_url_encoded($tailname);
|
---|
| 250 |
|
---|
| 251 |
|
---|
[16888] | 252 | # URLEncode this since htmls with images where the html filename is utf8 don't seem
|
---|
| 253 | # to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
|
---|
| 254 | # files on the filesystem.
|
---|
[23387] | 255 | $utf8_tailname = &util::rename_file($utf8_tailname, $self->{'file_rename_method'}, "without_suffix");
|
---|
[16888] | 256 |
|
---|
[23387] | 257 | my $lc_suffix = lc($suffix);
|
---|
[27509] | 258 | my $tmp_filename = &FileUtils::filenameConcatenate($tmp_dirname, "$utf8_tailname$lc_suffix");
|
---|
[15166] | 259 |
|
---|
[28381] | 260 | # If gsdl is remote, we're given relative path to input file, of the form import/utf8_tailname.suffix
|
---|
[15166] | 261 | # But we can't softlink to relative paths. Therefore, we need to ensure that
|
---|
| 262 | # the input_filename is the absolute path, see http://perldoc.perl.org/File/Spec.html
|
---|
| 263 | my $ensure_path_absolute = 1; # true
|
---|
[27509] | 264 | &FileUtils::softLink($input_filename, $tmp_filename, $ensure_path_absolute);
|
---|
[31766] | 265 |
|
---|
| 266 | my $output_filename = $self->run_conversion_command($tmp_dirname, $tmp_filename,
|
---|
| 267 | $utf8_tailname, $lc_suffix, $tailname, $suffix);
|
---|
| 268 |
|
---|
| 269 | return $output_filename;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | # The latter half of tmp_area_convert_file: runs the conversion command and returns the output file name
|
---|
| 273 | # Split from tmp_area_convert_file because UnknownConverterPlugin can then inherit all of
|
---|
| 274 | # tmp_area_convert_file and only needs to override this part:
|
---|
| 275 | sub run_conversion_command {
|
---|
| 276 | my $self = shift (@_);
|
---|
| 277 | my ($tmp_dirname, $tmp_filename, $utf8_tailname, $lc_suffix, $tailname, $suffix) = @_;
|
---|
| 278 |
|
---|
| 279 | my $outhandle = $self->{'outhandle'};
|
---|
| 280 | my $convert_to = $self->{'convert_to'};
|
---|
| 281 | my $failhandle = $self->{'failhandle'};
|
---|
| 282 |
|
---|
[10450] | 283 | my $verbosity = $self->{'verbosity'};
|
---|
| 284 | if ($verbosity > 0) {
|
---|
| 285 | print $outhandle "Converting $tailname$suffix to $convert_to format\n";
|
---|
| 286 | }
|
---|
| 287 |
|
---|
[27509] | 288 | my $errlog = &FileUtils::filenameConcatenate($tmp_dirname, "err.log");
|
---|
[10450] | 289 |
|
---|
| 290 | # Execute the conversion command and get the type of the result,
|
---|
| 291 | # making sure the converter gives us the appropriate output type
|
---|
[22597] | 292 | my $output_type=$self->{'convert_to'};
|
---|
| 293 | # if ($convert_to =~ m/PagedImage/i) {
|
---|
| 294 | # $output_type = lc($convert_to)."_".lc($convert_to_ext);
|
---|
| 295 | # } else {
|
---|
| 296 | # $output_type = lc($convert_to);
|
---|
| 297 | # }
|
---|
[10450] | 298 |
|
---|
[24362] | 299 | my $cmd = "\"".&util::get_perl_exec()."\" -S gsConvert.pl -verbose $verbosity ";
|
---|
[10450] | 300 | if (defined $self->{'convert_options'}) {
|
---|
| 301 | $cmd .= $self->{'convert_options'} . " ";
|
---|
| 302 | }
|
---|
| 303 | if ($self->{'use_strings'}) {
|
---|
| 304 | $cmd .= "-use_strings ";
|
---|
| 305 | }
|
---|
| 306 | $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
|
---|
[22597] | 307 | print STDERR "calling cmd $cmd\n";
|
---|
[10450] | 308 | $output_type = `$cmd`;
|
---|
[24290] | 309 |
|
---|
[10450] | 310 | # remove symbolic link to original file
|
---|
[27509] | 311 | &FileUtils::removeFiles($tmp_filename);
|
---|
[15116] | 312 |
|
---|
[10450] | 313 | # Check STDERR here
|
---|
| 314 | chomp $output_type;
|
---|
| 315 | if ($output_type eq "fail") {
|
---|
| 316 | print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
|
---|
| 317 | print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
|
---|
[10994] | 318 | # The following meant that if a conversion failed, the document would be counted twice - do we need it for anything?
|
---|
| 319 | #$self->{'num_not_processed'} ++;
|
---|
[10450] | 320 | if (-s "$errlog") {
|
---|
| 321 | open(ERRLOG, "$errlog");
|
---|
| 322 | while (<ERRLOG>) {
|
---|
| 323 | print $outhandle "$_";
|
---|
| 324 | }
|
---|
| 325 | print $outhandle "\n";
|
---|
| 326 | close ERRLOG;
|
---|
| 327 | }
|
---|
[27509] | 328 | &FileUtils::removeFiles("$errlog") if (-e "$errlog");
|
---|
[10450] | 329 | return "";
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 | # store the *actual* output type and return the output filename
|
---|
| 333 | # it's possible we requested conversion to html, but only to text succeeded
|
---|
| 334 | #$self->{'convert_to_ext'} = $output_type;
|
---|
| 335 | if ($output_type =~ /html/i) {
|
---|
| 336 | $self->{'converted_to'} = "HTML";
|
---|
| 337 | } elsif ($output_type =~ /te?xt/i) {
|
---|
[15871] | 338 | $self->{'converted_to'} = "Text";
|
---|
[10450] | 339 | } elsif ($output_type =~ /item/i){
|
---|
[15871] | 340 | $self->{'converted_to'} = "PagedImage";
|
---|
[10450] | 341 | }
|
---|
| 342 |
|
---|
| 343 | my $output_filename = $tmp_filename;
|
---|
| 344 | if ($output_type =~ /item/i) {
|
---|
| 345 | # running under windows
|
---|
| 346 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
[28381] | 347 | $output_filename = $tmp_dirname . "\\$utf8_tailname\\" . $utf8_tailname . ".$output_type";
|
---|
[10450] | 348 | } else {
|
---|
[28381] | 349 | $output_filename = $tmp_dirname . "\/$utf8_tailname\/" . $utf8_tailname . ".$output_type";
|
---|
[10450] | 350 | }
|
---|
| 351 | } else {
|
---|
[23387] | 352 | $output_filename =~ s/$lc_suffix$/.$output_type/;
|
---|
[10450] | 353 | }
|
---|
[12688] | 354 |
|
---|
[10450] | 355 | return $output_filename;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 |
|
---|
[15871] | 359 | # Override BasPlug read_into_doc_obj - we need to call secondary plugin stuff
|
---|
| 360 | sub read_into_doc_obj {
|
---|
[10450] | 361 | my $self = shift (@_);
|
---|
[16392] | 362 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[10450] | 363 |
|
---|
| 364 | my $outhandle = $self->{'outhandle'};
|
---|
[15871] | 365 |
|
---|
[16392] | 366 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[15871] | 367 |
|
---|
[10450] | 368 | my $output_ext = $self->{'convert_to_ext'};
|
---|
| 369 | my $conv_filename = "";
|
---|
[15871] | 370 | $conv_filename = $self->tmp_area_convert_file($output_ext, $filename_full_path);
|
---|
[16580] | 371 |
|
---|
[10609] | 372 | if ("$conv_filename" eq "") {return -1;} # had an error, will be passed down pipeline
|
---|
| 373 | if (! -e "$conv_filename") {return -1;}
|
---|
[10450] | 374 | $self->{'conv_filename'} = $conv_filename;
|
---|
| 375 | $self->convert_post_process($conv_filename);
|
---|
[11008] | 376 |
|
---|
| 377 | # Run the "fribidi" (http://fribidi.org) Unicode Bidirectional Algorithm program over the converted file
|
---|
| 378 | # Added for fixing up Persian PDFs after being processed by pdftohtml, but may be useful in other cases too
|
---|
[15871] | 379 | if ($self->{'apply_fribidi'} && $self->{'converted_to'} =~ /(HTML|Text)/) {
|
---|
[11008] | 380 | my $fribidi_command = "fribidi \"$conv_filename\" >\"${conv_filename}.tmp\"";
|
---|
| 381 | if (system($fribidi_command) != 0) {
|
---|
| 382 | print STDERR "ERROR: Cannot run fribidi on \"$conv_filename\".\n";
|
---|
| 383 | }
|
---|
| 384 | else {
|
---|
[27509] | 385 | &FileUtils::moveFiles("${conv_filename}.tmp", $conv_filename);
|
---|
[11008] | 386 | }
|
---|
| 387 | }
|
---|
[24290] | 388 |
|
---|
[10450] | 389 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 390 | my $num_secondary_plugins = scalar(keys %$secondary_plugins);
|
---|
| 391 |
|
---|
| 392 | if ($num_secondary_plugins == 0) {
|
---|
| 393 | print $outhandle "Warning: No secondary plugin to use in conversion. Skipping $file\n";
|
---|
| 394 | return 0; # effectively block it
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | my @plugin_names = keys %$secondary_plugins;
|
---|
| 398 | my $plugin_name = shift @plugin_names;
|
---|
| 399 |
|
---|
| 400 | if ($num_secondary_plugins > 1) {
|
---|
| 401 | print $outhandle "Warning: Multiple secondary plugins not supported yet! Choosing $plugin_name\n.";
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | my $secondary_plugin = $secondary_plugins->{$plugin_name};
|
---|
| 405 |
|
---|
| 406 | # note: metadata is not carried on to the next level
|
---|
[16392] | 407 | ## **** I just replaced $metadata with {} in following
|
---|
[10450] | 408 | my ($rv,$doc_obj)
|
---|
[16392] | 409 | = $secondary_plugin->read_into_doc_obj ($pluginfo,"", $conv_filename, $block_hash, {}, $processor, $maxdocs, $total_count, $gli);
|
---|
[10450] | 410 |
|
---|
| 411 | if ((!defined $rv) || ($rv<1)) {
|
---|
| 412 | # wasn't processed
|
---|
| 413 | return $rv;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | # Override previous gsdlsourcefilename set by secondary plugin
|
---|
[15871] | 417 | my $collect_file = &util::filename_within_collection($filename_full_path);
|
---|
[10450] | 418 | my $collect_conv_file = &util::filename_within_collection($conv_filename);
|
---|
[18320] | 419 | $doc_obj->set_source_filename ($collect_file, $self->{'file_rename_method'});
|
---|
[20766] | 420 | ## set_source_filename does not set the doc_obj source_path which is used in archives dbs for incremental
|
---|
| 421 | # build. so set it manually.
|
---|
[23363] | 422 | $doc_obj->set_source_path($filename_full_path);
|
---|
[10450] | 423 | $doc_obj->set_converted_filename($collect_conv_file);
|
---|
| 424 |
|
---|
[26893] | 425 | my $plugin_filename_encoding = $self->{'filename_encoding'};
|
---|
[23349] | 426 | my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
|
---|
[23352] | 427 | $self->set_Source_metadata($doc_obj, $filename_full_path, $filename_encoding);
|
---|
[15871] | 428 |
|
---|
[10450] | 429 | $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
|
---|
[15871] | 430 | $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename_full_path));
|
---|
[10450] | 431 |
|
---|
[19053] | 432 | # ****
|
---|
| 433 | my ($tailname, $dirname, $suffix)
|
---|
| 434 | = &File::Basename::fileparse($filename_full_path, "\\.[^\\.]+\$");
|
---|
[22879] | 435 | $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FilenameRoot", $tailname);
|
---|
[19053] | 436 |
|
---|
[10450] | 437 | # do plugin specific processing of doc_obj
|
---|
[15871] | 438 | unless (defined ($self->process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
|
---|
[10450] | 439 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
| 440 | return -1;
|
---|
| 441 | }
|
---|
[15871] | 442 |
|
---|
| 443 | my $topsection = $doc_obj->get_top_section();
|
---|
| 444 | $self->add_associated_files($doc_obj, $filename_full_path);
|
---|
[16392] | 445 |
|
---|
| 446 | # extra_metadata is already called by sec plugin in process??
|
---|
[15871] | 447 | $self->extra_metadata($doc_obj, $topsection, $metadata); # do we need this here??
|
---|
[10450] | 448 | # do any automatic metadata extraction
|
---|
| 449 | $self->auto_extract_metadata ($doc_obj);
|
---|
[10592] | 450 |
|
---|
| 451 | # have we found a Title??
|
---|
[15871] | 452 | $self->title_fallback($doc_obj,$topsection,$filename_no_path);
|
---|
[10592] | 453 |
|
---|
[26893] | 454 | # force a new OID - this will use OIDtype option set for this plugin.
|
---|
| 455 | $self->add_OID($doc_obj, 1);
|
---|
[14928] | 456 |
|
---|
[15871] | 457 | return (1, $doc_obj);
|
---|
[14928] | 458 |
|
---|
[15871] | 459 | }
|
---|
[14928] | 460 |
|
---|
[15871] | 461 | sub process {
|
---|
| 462 | my $self = shift (@_);
|
---|
| 463 | my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
[10450] | 464 |
|
---|
[15871] | 465 | return $self->process_type($base_dir, $file, $doc_obj);
|
---|
[10450] | 466 | }
|
---|
| 467 |
|
---|
| 468 | # do plugin specific processing of doc_obj for doc_ext type
|
---|
| 469 | sub process_type {
|
---|
| 470 | my $self = shift (@_);
|
---|
[15871] | 471 | my ($base_dir, $file, $doc_obj) = @_;
|
---|
[10450] | 472 |
|
---|
[15871] | 473 | # need to check that not empty
|
---|
[22874] | 474 | my ($doc_ext) = $file =~ /\.(\w+)$/;
|
---|
[23387] | 475 | $doc_ext = lc($doc_ext);
|
---|
[15871] | 476 | my $file_type = "unknown";
|
---|
| 477 | $file_type = $self->{'file_type'} if defined $self->{'file_type'};
|
---|
| 478 |
|
---|
[10450] | 479 | # associate original file with doc object
|
---|
| 480 | my $cursection = $doc_obj->get_top_section();
|
---|
[27509] | 481 | my $filename = &FileUtils::filenameConcatenate($base_dir, $file);
|
---|
[12961] | 482 | my $assocfilename = "doc.$doc_ext";
|
---|
| 483 | if ($self->{'keep_original_filename'} == 1) {
|
---|
[16922] | 484 | # this should be the same filename that was used for the Source and SourceFile metadata,
|
---|
[22664] | 485 | # as we will use SourceFile in the srclink (below)
|
---|
[16954] | 486 | $assocfilename = $doc_obj->get_assocfile_from_sourcefile();
|
---|
[12961] | 487 | }
|
---|
[23387] | 488 |
|
---|
[12961] | 489 | $doc_obj->associate_file($filename, $assocfilename, undef, $cursection);
|
---|
[10450] | 490 |
|
---|
| 491 | # We use set instead of add here because we only want one value
|
---|
[15871] | 492 | $doc_obj->set_utf8_metadata_element($cursection, "FileFormat", $file_type);
|
---|
[21760] | 493 | my $srclink_filename = "doc.$doc_ext";
|
---|
[12961] | 494 | if ($self->{'keep_original_filename'} == 1) {
|
---|
[22663] | 495 | $srclink_filename = $doc_obj->get_sourcefile();
|
---|
[12961] | 496 | }
|
---|
[24225] | 497 | # srclink_file is now deprecated because of the "_" in the metadataname. Use srclinkFile
|
---|
[10450] | 498 | $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
|
---|
[21760] | 499 | $doc_obj->add_utf8_metadata ($cursection, "srclink_file", $srclink_filename);
|
---|
[24225] | 500 | $doc_obj->add_utf8_metadata ($cursection, "srclinkFile", $srclink_filename);
|
---|
[10450] | 501 | return 1;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[22887] | 504 | sub clean_up_after_doc_obj_processing {
|
---|
| 505 | my $self = shift(@_);
|
---|
| 506 |
|
---|
| 507 | my $tmp_dir = $self->{'tmp_dir'};
|
---|
| 508 | if (defined $tmp_dir && -d $tmp_dir) {
|
---|
[24568] | 509 | ##print STDERR "**** Suppressing clean up of tmp dir\n";
|
---|
[27509] | 510 | &FileUtils::removeFilesRecursive($tmp_dir);
|
---|
[22887] | 511 | $self->{'tmp_dir'} = undef;
|
---|
| 512 | }
|
---|
| 513 |
|
---|
| 514 |
|
---|
| 515 | }
|
---|
[31761] | 516 |
|
---|
| 517 | # This sub is shared across PowerPointPlugin and UnknownConverterPlugin,
|
---|
| 518 | # so it's been copied into here from the former.
|
---|
| 519 | sub generate_item_file {
|
---|
| 520 | my $self = shift(@_);
|
---|
| 521 | my ($input_filename) = @_;
|
---|
| 522 | my $outhandle = $self->{'outhandle'};
|
---|
| 523 | my ($tailname, $dirname, $suffix)
|
---|
| 524 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
| 525 |
|
---|
| 526 | my $plugin_name = $self->{'plugin_type'}; # inherited from BaseImporter
|
---|
| 527 |
|
---|
| 528 | # find all the files in the directory
|
---|
| 529 | if (!opendir (DIR, $dirname)) {
|
---|
| 530 | print $outhandle "$plugin_name: Couldn't read directory $dirname\n";
|
---|
| 531 | return $input_filename;
|
---|
| 532 | }
|
---|
| 533 |
|
---|
| 534 | my @dir = readdir (DIR);
|
---|
| 535 | closedir (DIR);
|
---|
| 536 |
|
---|
| 537 | # start the item file
|
---|
| 538 | my $itemfile_name = &util::filename_cat($dirname, "$tailname.item");
|
---|
| 539 |
|
---|
| 540 | # encoding specification????
|
---|
| 541 | if (!open (ITEMFILE, ">$itemfile_name")) {
|
---|
| 542 | print $outhandle "$plugin_name: Couldn't open $itemfile_name for writing\n";
|
---|
| 543 | }
|
---|
| 544 | print ITEMFILE "<GeneratedBy>$plugin_name\n";
|
---|
| 545 | # print the first page
|
---|
| 546 | my @sorted_dir = sort alphanum_sort @dir;
|
---|
| 547 | for (my $i = 0; $i < scalar(@sorted_dir); $i++) {
|
---|
| 548 | my $file = $sorted_dir[$i];
|
---|
| 549 | if ($file =~ /^img(\d+)\.jpg$/) {
|
---|
| 550 | my $num = $1;
|
---|
| 551 | $self->tidy_up_html(&util::filename_cat($dirname, "text$num.html"));
|
---|
| 552 | print ITEMFILE "$num:img$num.jpg:text$num.html:\n";
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 | close ITEMFILE;
|
---|
| 556 | return $itemfile_name;
|
---|
| 557 |
|
---|
| 558 | }
|
---|
| 559 |
|
---|
[10450] | 560 | 1;
|
---|
| 561 |
|
---|
| 562 |
|
---|
| 563 |
|
---|
| 564 |
|
---|
| 565 |
|
---|
| 566 |
|
---|
| 567 |
|
---|