[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 |
|
---|
[12741] | 28 | # This plugin is inherited by such plugins as WordPlug, PPTPlug, PSPlug,
|
---|
| 29 | # RTFPlug and PDFPlug. It facilitates the conversion of these document types
|
---|
[15871] | 30 | # to either HTML, Text or a series of images. It works by dynamically loading
|
---|
[12741] | 31 | # an appropriate secondary plugin (HTMLPlug, StructuredHTMLPlug,
|
---|
[15871] | 32 | # PagedImagePlugin or TextPlugin) based on the plugin argument 'convert_to'.
|
---|
[12741] | 33 |
|
---|
[15871] | 34 | package ConvertBinaryFile;
|
---|
[10450] | 35 |
|
---|
[15906] | 36 | use AutoExtractMetadata;
|
---|
[10450] | 37 | use ghtml;
|
---|
[15871] | 38 | use HTMLPlugin;
|
---|
| 39 | use TextPlugin;
|
---|
| 40 | use PagedImagePlugin;
|
---|
[10450] | 41 |
|
---|
[10453] | 42 | use strict;
|
---|
| 43 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[11680] | 44 | no strict 'subs';
|
---|
[15871] | 45 |
|
---|
[10450] | 46 | sub BEGIN {
|
---|
[15906] | 47 | @ConvertBinaryFile::ISA = ('AutoExtractMetadata');
|
---|
[10450] | 48 | }
|
---|
| 49 |
|
---|
| 50 | my $convert_to_list =
|
---|
| 51 | [ { 'name' => "auto",
|
---|
[15871] | 52 | 'desc' => "{ConvertBinaryFile.convert_to.auto}" },
|
---|
[10450] | 53 | { 'name' => "html",
|
---|
[15871] | 54 | 'desc' => "{ConvertBinaryFile.convert_to.html}" },
|
---|
[10450] | 55 | { 'name' => "text",
|
---|
[15871] | 56 | 'desc' => "{ConvertBinaryFile.convert_to.text}" }
|
---|
[10450] | 57 | ];
|
---|
| 58 |
|
---|
| 59 | my $arguments =
|
---|
| 60 | [ { 'name' => "convert_to",
|
---|
[15871] | 61 | 'desc' => "{ConvertBinaryFile.convert_to}",
|
---|
[10450] | 62 | 'type' => "enum",
|
---|
| 63 | 'reqd' => "yes",
|
---|
| 64 | 'list' => $convert_to_list,
|
---|
[10890] | 65 | 'deft' => "auto" },
|
---|
[12961] | 66 | { 'name' => "keep_original_filename",
|
---|
[15871] | 67 | 'desc' => "{ConvertBinaryFile.keep_original_filename}",
|
---|
[12961] | 68 | 'type' => "flag" },
|
---|
[10450] | 69 | { 'name' => "title_sub",
|
---|
[16013] | 70 | 'desc' => "{HTMLPlugin.title_sub}",
|
---|
[10450] | 71 | 'type' => "string",
|
---|
| 72 | #'type' => "regexp",
|
---|
| 73 | 'deft' => "" },
|
---|
[11008] | 74 | { 'name' => "apply_fribidi",
|
---|
[15871] | 75 | 'desc' => "{ConvertBinaryFile.apply_fribidi}",
|
---|
[11008] | 76 | 'type' => "flag",
|
---|
| 77 | 'reqd' => "no" },
|
---|
[10450] | 78 | { 'name' => "use_strings",
|
---|
[15871] | 79 | 'desc' => "{ConvertBinaryFile.use_strings}",
|
---|
[10450] | 80 | 'type' => "flag",
|
---|
| 81 | 'reqd' => "no" },
|
---|
[15871] | 82 | ];
|
---|
[10450] | 83 |
|
---|
[15871] | 84 | my $options = { 'name' => "ConvertBinaryFile",
|
---|
| 85 | 'desc' => "{ConvertBinaryFile.desc}",
|
---|
[10450] | 86 | 'abstract' => "yes",
|
---|
| 87 | 'inherits' => "yes",
|
---|
| 88 | 'args' => $arguments };
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | sub load_secondary_plugins
|
---|
| 92 | {
|
---|
| 93 | my $self = shift (@_);
|
---|
| 94 | my ($class,$input_args,$hashArgOptLists) = @_;
|
---|
| 95 |
|
---|
| 96 | my @convert_to_list = split(",",$self->{'convert_to'});
|
---|
[10453] | 97 | my $secondary_plugins = {};
|
---|
[11680] | 98 | # find the plugin
|
---|
[10450] | 99 |
|
---|
| 100 | foreach my $convert_to (@convert_to_list) {
|
---|
| 101 | # load in "convert_to" plugin package
|
---|
[15871] | 102 | my $plugin_class = $convert_to."Plugin";
|
---|
[10450] | 103 | my $plugin_package = $plugin_class.".pm";
|
---|
| 104 |
|
---|
[15116] | 105 | my $colplugname = undef;
|
---|
| 106 | if (defined $ENV{'GSDLCOLLECTDIR'}) {
|
---|
| 107 | $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},
|
---|
| 108 | "perllib","plugins",
|
---|
| 109 | $plugin_package);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[11680] | 112 | my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'},
|
---|
[15116] | 113 | "perllib","plugins",
|
---|
[11680] | 114 | $plugin_package);
|
---|
[10450] | 115 |
|
---|
[15116] | 116 | if ((defined $colplugname) && (-e $colplugname)) { require $colplugname;}
|
---|
[11680] | 117 | elsif (-e $mainplugname) { require $mainplugname; }
|
---|
| 118 | else {
|
---|
| 119 | &gsprintf(STDERR, "{plugin.could_not_find_plugin}\n",
|
---|
| 120 | $plugin_class);
|
---|
| 121 | die "\n";
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[10450] | 124 | # call its constructor with extra options that we've worked out!
|
---|
| 125 | my $arglist = $input_args->{$plugin_class};
|
---|
[11680] | 126 |
|
---|
| 127 | my ($secondary_plugin);
|
---|
| 128 | eval("\$secondary_plugin = new $plugin_class([],\$arglist)");
|
---|
| 129 | die "$@" if $@;
|
---|
[10450] | 130 | $secondary_plugins->{$plugin_class} = $secondary_plugin;
|
---|
| 131 | }
|
---|
| 132 | $self->{'secondary_plugins'} = $secondary_plugins;
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | sub new {
|
---|
| 136 | my ($class) = shift (@_);
|
---|
| 137 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 138 | push(@$pluginlist, $class);
|
---|
| 139 | my $classPluginName = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
|
---|
[15871] | 140 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 141 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10450] | 142 |
|
---|
[15906] | 143 | my $self = new AutoExtractMetadata($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[10453] | 144 |
|
---|
[11680] | 145 | if ($self->{'info_only'}) {
|
---|
| 146 | # don't worry about any options etc
|
---|
| 147 | return bless $self, $class;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[10890] | 150 | my $convert_to_type = $self->{'convert_to'};
|
---|
| 151 | if (!defined $convert_to_type || $convert_to_type eq "") {
|
---|
| 152 | $convert_to_type = "auto";
|
---|
| 153 | }
|
---|
| 154 | my $windows_scripting = $self->{'windows_scripting'};
|
---|
[11833] | 155 | $windows_scripting = 0 unless defined $windows_scripting;
|
---|
[15871] | 156 | if ($classPluginName eq "PDFPlugin") {
|
---|
[10890] | 157 | if ($convert_to_type eq "text" &&
|
---|
[10503] | 158 | $ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 159 | print STDERR "Windows does not support pdf to text. PDFs will be converted to HTML instead\n";
|
---|
[10890] | 160 | $convert_to_type = "html";
|
---|
[10503] | 161 | }
|
---|
[15871] | 162 | } elsif ($classPluginName eq "WordPlugin") {
|
---|
[11833] | 163 | if ($windows_scripting && $ENV{'GSDLOS'} =~ /^windows$/i && $convert_to_type =~ /^(html|auto)$/) {
|
---|
[10503] | 164 | # we use structured HTML, not normal html
|
---|
[10890] | 165 | $convert_to_type = "structuredhtml";
|
---|
[10503] | 166 | }
|
---|
[15871] | 167 | } elsif ($classPluginName eq "PPTPlugin") {
|
---|
[11833] | 168 | if ($windows_scripting && $ENV{'GSDLOS'} =~ /^windows$/i && $convert_to_type eq "auto") {
|
---|
[10503] | 169 | # we use paged img
|
---|
[10890] | 170 | $convert_to_type = "pagedimg_jpg";
|
---|
[10503] | 171 | }
|
---|
[15871] | 172 | } elsif ($classPluginName eq "PSPlugin") {
|
---|
[10890] | 173 | if ($convert_to_type eq "auto") {
|
---|
[10537] | 174 | # we use text
|
---|
[10890] | 175 | $convert_to_type = "text";
|
---|
[10537] | 176 | }
|
---|
[10503] | 177 | }
|
---|
[10504] | 178 |
|
---|
[10890] | 179 | if ($convert_to_type eq "auto") {
|
---|
[10453] | 180 | # choose html for now - should choose a format based on doc type
|
---|
[10890] | 181 | $convert_to_type = "html";
|
---|
[10450] | 182 | }
|
---|
| 183 |
|
---|
[10890] | 184 | if ($convert_to_type eq "html") {
|
---|
[10453] | 185 | $self->{'convert_to'} = "HTML";
|
---|
[10450] | 186 | $self->{'convert_to_ext'} = "html";
|
---|
[10890] | 187 | } elsif ($convert_to_type eq "text") {
|
---|
[15871] | 188 | $self->{'convert_to'} = "Text";
|
---|
[10450] | 189 | $self->{'convert_to_ext'} = "txt";
|
---|
[10890] | 190 | } elsif ($convert_to_type eq "structuredhtml") {
|
---|
[10453] | 191 | $self->{'convert_to'} = "StructuredHTML";
|
---|
| 192 | $self->{'convert_to_ext'} = "html";
|
---|
[10890] | 193 | } elsif ($convert_to_type =~ /^pagedimg/) {
|
---|
[15871] | 194 | $self->{'convert_to'} = "PagedImage";
|
---|
[10890] | 195 | my ($convert_to_ext) = $convert_to_type =~ /pagedimg\_(jpg|gif|png)/i;
|
---|
[10453] | 196 | $convert_to_ext = 'jpg' unless defined $convert_to_ext;
|
---|
| 197 | $self->{'convert_to_ext'} = $convert_to_ext;
|
---|
[10450] | 198 | }
|
---|
[10453] | 199 |
|
---|
[10450] | 200 | return bless $self, $class;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | sub init {
|
---|
| 205 | my $self = shift (@_);
|
---|
| 206 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
| 207 |
|
---|
| 208 | $self->SUPER::init($verbosity,$outhandle,$failhandle);
|
---|
| 209 |
|
---|
| 210 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 211 |
|
---|
| 212 | foreach my $plug_name (keys %$secondary_plugins) {
|
---|
| 213 | my $plugin = $secondary_plugins->{$plug_name};
|
---|
| 214 | $plugin->init($verbosity,$outhandle,$failhandle);
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | sub deinit {
|
---|
| 219 | # called only once, after all plugin passes have been done
|
---|
| 220 |
|
---|
| 221 | my ($self) = @_;
|
---|
| 222 |
|
---|
| 223 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 224 |
|
---|
| 225 | foreach my $plug_name (keys %$secondary_plugins) {
|
---|
| 226 | my $plugin = $secondary_plugins->{$plug_name};
|
---|
| 227 | $plugin->deinit();
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | sub convert_post_process
|
---|
| 232 | {
|
---|
| 233 | # by default do no post processing
|
---|
| 234 | return;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 |
|
---|
| 238 | # Run conversion utility on the input file.
|
---|
| 239 | #
|
---|
| 240 | # The conversion takes place in a collection specific 'tmp' directory so
|
---|
| 241 | # that we don't accidentally damage the input.
|
---|
| 242 | #
|
---|
| 243 | # The desired output type is indicated by $output_ext. This is usually
|
---|
| 244 | # something like "html" or "word", but can be "best" (or the empty string)
|
---|
| 245 | # to indicate that the conversion utility should do the best it can.
|
---|
| 246 | sub tmp_area_convert_file {
|
---|
| 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 |
|
---|
[15116] | 259 | # softlink to collection tmp dir
|
---|
| 260 | my $tmp_dirname = $dirname;
|
---|
| 261 | if(defined $ENV{'GSDLCOLLECTDIR'}) {
|
---|
| 262 | $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 263 | } elsif(defined $ENV{'GSDLHOME'}) {
|
---|
| 264 | $tmp_dirname = $ENV{'GSDLHOME'};
|
---|
| 265 | }
|
---|
| 266 | $tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
|
---|
| 267 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
[15151] | 268 |
|
---|
| 269 | # The following is not necessary and will cause problems with
|
---|
| 270 | # replacing_srcdoc_with_html in the GSDLremote case:
|
---|
[10450] | 271 | # Remove any white space from filename -- no risk of name collision, and
|
---|
| 272 | # makes later conversion by utils simpler. Leave spaces in path...
|
---|
| 273 | # tidy up the filename with space, dot, hyphen between
|
---|
[15151] | 274 | #$tailname =~ s/\s+//g;
|
---|
| 275 | #$tailname =~ s/\.+//g;
|
---|
| 276 | #$tailname =~ s/\-+//g;
|
---|
[12688] | 277 |
|
---|
| 278 | # convert to utf-8 otherwise we have problems with the doc.xml file
|
---|
| 279 | # later on
|
---|
[16580] | 280 | print STDERR "**** filename $tailname$suffix is already UTF8\n" if &unicode::check_is_utf8($tailname);
|
---|
| 281 | $tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
|
---|
[12688] | 282 |
|
---|
[10450] | 283 | $suffix = lc($suffix);
|
---|
| 284 | my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
|
---|
[15166] | 285 |
|
---|
| 286 | # If gsdl is remote, we're given relative path to input file, of the form import/tailname.suffix
|
---|
| 287 | # But we can't softlink to relative paths. Therefore, we need to ensure that
|
---|
| 288 | # the input_filename is the absolute path, see http://perldoc.perl.org/File/Spec.html
|
---|
| 289 | my $ensure_path_absolute = 1; # true
|
---|
| 290 | &util::soft_link($input_filename, $tmp_filename, $ensure_path_absolute);
|
---|
[10450] | 291 | my $verbosity = $self->{'verbosity'};
|
---|
| 292 | if ($verbosity > 0) {
|
---|
| 293 | print $outhandle "Converting $tailname$suffix to $convert_to format\n";
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | my $errlog = &util::filename_cat($tmp_dirname, "err.log");
|
---|
| 297 |
|
---|
| 298 | # Execute the conversion command and get the type of the result,
|
---|
| 299 | # making sure the converter gives us the appropriate output type
|
---|
| 300 | my $output_type="";
|
---|
[15871] | 301 | if ($convert_to =~ m/PagedImage/i) {
|
---|
[10450] | 302 | $output_type = lc($convert_to)."_".lc($convert_to_ext);
|
---|
| 303 | } else {
|
---|
| 304 | $output_type = lc($convert_to);
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | my $cmd = "perl -S gsConvert.pl -verbose $verbosity ";
|
---|
| 308 | if (defined $self->{'convert_options'}) {
|
---|
| 309 | $cmd .= $self->{'convert_options'} . " ";
|
---|
| 310 | }
|
---|
| 311 | if ($self->{'use_strings'}) {
|
---|
| 312 | $cmd .= "-use_strings ";
|
---|
| 313 | }
|
---|
| 314 | $cmd .= "-errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
|
---|
| 315 | $output_type = `$cmd`;
|
---|
| 316 |
|
---|
| 317 | # remove symbolic link to original file
|
---|
| 318 | &util::rm($tmp_filename);
|
---|
[15116] | 319 |
|
---|
[10450] | 320 | # Check STDERR here
|
---|
| 321 | chomp $output_type;
|
---|
| 322 | if ($output_type eq "fail") {
|
---|
| 323 | print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
|
---|
| 324 | print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
|
---|
[10994] | 325 | # The following meant that if a conversion failed, the document would be counted twice - do we need it for anything?
|
---|
| 326 | #$self->{'num_not_processed'} ++;
|
---|
[10450] | 327 | if (-s "$errlog") {
|
---|
| 328 | open(ERRLOG, "$errlog");
|
---|
| 329 | while (<ERRLOG>) {
|
---|
| 330 | print $outhandle "$_";
|
---|
| 331 | }
|
---|
| 332 | print $outhandle "\n";
|
---|
| 333 | close ERRLOG;
|
---|
| 334 | }
|
---|
| 335 | &util::rm("$errlog") if (-e "$errlog");
|
---|
| 336 | return "";
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | # store the *actual* output type and return the output filename
|
---|
| 340 | # it's possible we requested conversion to html, but only to text succeeded
|
---|
| 341 | #$self->{'convert_to_ext'} = $output_type;
|
---|
| 342 | if ($output_type =~ /html/i) {
|
---|
| 343 | $self->{'converted_to'} = "HTML";
|
---|
| 344 | } elsif ($output_type =~ /te?xt/i) {
|
---|
[15871] | 345 | $self->{'converted_to'} = "Text";
|
---|
[10450] | 346 | } elsif ($output_type =~ /item/i){
|
---|
[15871] | 347 | $self->{'converted_to'} = "PagedImage";
|
---|
[10450] | 348 | }
|
---|
| 349 |
|
---|
| 350 | my $output_filename = $tmp_filename;
|
---|
| 351 | if ($output_type =~ /item/i) {
|
---|
| 352 | # running under windows
|
---|
| 353 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 354 | $output_filename = $tmp_dirname . "\\$tailname\\" . $tailname . ".$output_type";
|
---|
| 355 | } else {
|
---|
| 356 | $output_filename = $tmp_dirname . "\/$tailname\/" . $tailname . ".$output_type";
|
---|
| 357 | }
|
---|
| 358 | } else {
|
---|
| 359 | $output_filename =~ s/$suffix$/.$output_type/;
|
---|
| 360 | }
|
---|
[12688] | 361 |
|
---|
[10450] | 362 | return $output_filename;
|
---|
| 363 | }
|
---|
| 364 |
|
---|
| 365 |
|
---|
[15871] | 366 | # Override BasPlug read_into_doc_obj - we need to call secondary plugin stuff
|
---|
| 367 | sub read_into_doc_obj {
|
---|
[10450] | 368 | my $self = shift (@_);
|
---|
[16392] | 369 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[10450] | 370 |
|
---|
| 371 | my $outhandle = $self->{'outhandle'};
|
---|
[15871] | 372 |
|
---|
[16392] | 373 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[15871] | 374 |
|
---|
[10450] | 375 | my $output_ext = $self->{'convert_to_ext'};
|
---|
| 376 | my $conv_filename = "";
|
---|
[15871] | 377 | $conv_filename = $self->tmp_area_convert_file($output_ext, $filename_full_path);
|
---|
[16580] | 378 |
|
---|
[10609] | 379 | if ("$conv_filename" eq "") {return -1;} # had an error, will be passed down pipeline
|
---|
| 380 | if (! -e "$conv_filename") {return -1;}
|
---|
[10450] | 381 | $self->{'conv_filename'} = $conv_filename;
|
---|
| 382 | $self->convert_post_process($conv_filename);
|
---|
[11008] | 383 |
|
---|
| 384 | # Run the "fribidi" (http://fribidi.org) Unicode Bidirectional Algorithm program over the converted file
|
---|
| 385 | # Added for fixing up Persian PDFs after being processed by pdftohtml, but may be useful in other cases too
|
---|
[15871] | 386 | if ($self->{'apply_fribidi'} && $self->{'converted_to'} =~ /(HTML|Text)/) {
|
---|
[11008] | 387 | my $fribidi_command = "fribidi \"$conv_filename\" >\"${conv_filename}.tmp\"";
|
---|
| 388 | if (system($fribidi_command) != 0) {
|
---|
| 389 | print STDERR "ERROR: Cannot run fribidi on \"$conv_filename\".\n";
|
---|
| 390 | }
|
---|
| 391 | else {
|
---|
| 392 | &util::mv("${conv_filename}.tmp", $conv_filename);
|
---|
| 393 | }
|
---|
| 394 | }
|
---|
| 395 |
|
---|
[10450] | 396 | my $secondary_plugins = $self->{'secondary_plugins'};
|
---|
| 397 | my $num_secondary_plugins = scalar(keys %$secondary_plugins);
|
---|
| 398 |
|
---|
| 399 | if ($num_secondary_plugins == 0) {
|
---|
| 400 | print $outhandle "Warning: No secondary plugin to use in conversion. Skipping $file\n";
|
---|
| 401 | return 0; # effectively block it
|
---|
| 402 | }
|
---|
| 403 |
|
---|
| 404 | my @plugin_names = keys %$secondary_plugins;
|
---|
| 405 | my $plugin_name = shift @plugin_names;
|
---|
| 406 |
|
---|
| 407 | if ($num_secondary_plugins > 1) {
|
---|
| 408 | print $outhandle "Warning: Multiple secondary plugins not supported yet! Choosing $plugin_name\n.";
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | my $secondary_plugin = $secondary_plugins->{$plugin_name};
|
---|
| 412 |
|
---|
| 413 | # note: metadata is not carried on to the next level
|
---|
[16392] | 414 | ## **** I just replaced $metadata with {} in following
|
---|
[10450] | 415 | my ($rv,$doc_obj)
|
---|
[16392] | 416 | = $secondary_plugin->read_into_doc_obj ($pluginfo,"", $conv_filename, $block_hash, {}, $processor, $maxdocs, $total_count, $gli);
|
---|
[10450] | 417 |
|
---|
| 418 | if ((!defined $rv) || ($rv<1)) {
|
---|
| 419 | # wasn't processed
|
---|
| 420 | return $rv;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | # Override previous gsdlsourcefilename set by secondary plugin
|
---|
[15871] | 424 | my $collect_file = &util::filename_within_collection($filename_full_path);
|
---|
[10450] | 425 | my $collect_conv_file = &util::filename_within_collection($conv_filename);
|
---|
| 426 | $doc_obj->set_source_filename ($collect_file);
|
---|
| 427 | $doc_obj->set_converted_filename($collect_conv_file);
|
---|
| 428 |
|
---|
[15871] | 429 | $self->set_Source_metadata($doc_obj, $filename_no_path);
|
---|
| 430 |
|
---|
[10450] | 431 | $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
|
---|
[15871] | 432 | $doc_obj->set_utf8_metadata_element($doc_obj->get_top_section(), "FileSize", (-s $filename_full_path));
|
---|
[10450] | 433 |
|
---|
| 434 | # do plugin specific processing of doc_obj
|
---|
[15871] | 435 | unless (defined ($self->process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
|
---|
[10450] | 436 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
| 437 | return -1;
|
---|
| 438 | }
|
---|
[15871] | 439 |
|
---|
| 440 | my $topsection = $doc_obj->get_top_section();
|
---|
| 441 | $self->add_associated_files($doc_obj, $filename_full_path);
|
---|
[16392] | 442 |
|
---|
| 443 | # extra_metadata is already called by sec plugin in process??
|
---|
[15871] | 444 | $self->extra_metadata($doc_obj, $topsection, $metadata); # do we need this here??
|
---|
[10450] | 445 | # do any automatic metadata extraction
|
---|
| 446 | $self->auto_extract_metadata ($doc_obj);
|
---|
[10592] | 447 |
|
---|
| 448 | # have we found a Title??
|
---|
[15871] | 449 | $self->title_fallback($doc_obj,$topsection,$filename_no_path);
|
---|
[10592] | 450 |
|
---|
[15871] | 451 | $self->add_OID($doc_obj);
|
---|
[14928] | 452 |
|
---|
[15871] | 453 | return (1, $doc_obj);
|
---|
[14928] | 454 |
|
---|
[15871] | 455 | }
|
---|
[14928] | 456 |
|
---|
[15871] | 457 | sub process {
|
---|
| 458 | my $self = shift (@_);
|
---|
| 459 | my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
[10450] | 460 |
|
---|
[15871] | 461 | return $self->process_type($base_dir, $file, $doc_obj);
|
---|
[10450] | 462 | }
|
---|
| 463 |
|
---|
| 464 | # do plugin specific processing of doc_obj for doc_ext type
|
---|
| 465 | sub process_type {
|
---|
| 466 | my $self = shift (@_);
|
---|
[15871] | 467 | my ($base_dir, $file, $doc_obj) = @_;
|
---|
[10450] | 468 |
|
---|
[15871] | 469 | # need to check that not empty
|
---|
| 470 | my $doc_ext = $self->{'filename_extension'};
|
---|
| 471 | my $file_type = "unknown";
|
---|
| 472 | $file_type = $self->{'file_type'} if defined $self->{'file_type'};
|
---|
| 473 |
|
---|
[10450] | 474 | # associate original file with doc object
|
---|
| 475 | my $cursection = $doc_obj->get_top_section();
|
---|
| 476 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
[12961] | 477 | my $assocfilename = "doc.$doc_ext";
|
---|
| 478 | if ($self->{'keep_original_filename'} == 1) {
|
---|
| 479 | # this should be the same filename that was used for the Source metadata, as we will use [Source] in the srclink
|
---|
| 480 | ($assocfilename) = $file =~ /([^\\\/]+)$/;
|
---|
| 481 | }
|
---|
| 482 | $doc_obj->associate_file($filename, $assocfilename, undef, $cursection);
|
---|
[10450] | 483 |
|
---|
| 484 | # We use set instead of add here because we only want one value
|
---|
[15871] | 485 | $doc_obj->set_utf8_metadata_element($cursection, "FileFormat", $file_type);
|
---|
[11834] | 486 | my $doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[archivedir]/doc.$doc_ext\">";
|
---|
[12961] | 487 | if ($self->{'keep_original_filename'} == 1) {
|
---|
| 488 | $doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/[archivedir]/[Source]\">";
|
---|
| 489 | }
|
---|
[10450] | 490 | $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
|
---|
| 491 | $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
|
---|
| 492 | $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
|
---|
| 493 |
|
---|
| 494 | return 1;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | 1;
|
---|
| 498 |
|
---|
| 499 |
|
---|
| 500 |
|
---|
| 501 |
|
---|
| 502 |
|
---|
| 503 |
|
---|
| 504 |
|
---|