[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15868] | 3 | # BasePlugin.pm -- base class for all the import plugins
|
---|
[537] | 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
[9413] | 8 | # Copyright (C) 1999-2005 New Zealand Digital Library Project
|
---|
[537] | 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
[4] | 25 |
|
---|
[15868] | 26 | package BasePlugin;
|
---|
[2219] | 27 |
|
---|
[10254] | 28 | use strict;
|
---|
| 29 | no strict 'subs';
|
---|
| 30 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[9413] | 31 |
|
---|
[8892] | 32 | use File::Basename;
|
---|
[23335] | 33 | use Encode;
|
---|
[23832] | 34 | use Unicode::Normalize 'normalize';
|
---|
[8892] | 35 |
|
---|
[1870] | 36 | use encodings;
|
---|
[11389] | 37 | use unicode;
|
---|
[1242] | 38 | use doc;
|
---|
[2751] | 39 | use ghtml;
|
---|
[9413] | 40 | use gsprintf 'gsprintf';
|
---|
[27306] | 41 | use util;
|
---|
| 42 | use FileUtils;
|
---|
[4] | 43 |
|
---|
[15868] | 44 | use PrintInfo;
|
---|
[10218] | 45 |
|
---|
[15868] | 46 | BEGIN {
|
---|
| 47 | @BasePlugin::ISA = ( 'PrintInfo' );
|
---|
| 48 | }
|
---|
[5681] | 49 |
|
---|
[18320] | 50 | # the different methods that can be applied when renaming
|
---|
| 51 | # imported documents and their associated files
|
---|
| 52 | our $file_rename_method_list =
|
---|
| 53 | [ { 'name' => "url",
|
---|
| 54 | 'desc' => "{BasePlugin.rename_method.url}" },
|
---|
| 55 | { 'name' => "base64",
|
---|
[18398] | 56 | 'desc' => "{BasePlugin.rename_method.base64}" },
|
---|
[18404] | 57 | { 'name' => "none",
|
---|
| 58 | 'desc' => "{BasePlugin.rename_method.none}",
|
---|
[18398] | 59 | 'hiddengli' => "yes" } ];
|
---|
[18320] | 60 |
|
---|
[15868] | 61 | our $encoding_list =
|
---|
[10218] | 62 | [ { 'name' => "ascii",
|
---|
[16014] | 63 | 'desc' => "{BasePlugin.encoding.ascii}" },
|
---|
[4744] | 64 | { 'name' => "utf8",
|
---|
[16014] | 65 | 'desc' => "{BasePlugin.encoding.utf8}" },
|
---|
[4744] | 66 | { 'name' => "unicode",
|
---|
[16014] | 67 | 'desc' => "{BasePlugin.encoding.unicode}" } ];
|
---|
[3540] | 68 |
|
---|
[23457] | 69 |
|
---|
[10620] | 70 | my $e = $encodings::encodings;
|
---|
| 71 | foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
|
---|
| 72 | {
|
---|
| 73 | my $hashEncode =
|
---|
| 74 | {'name' => $enc,
|
---|
| 75 | 'desc' => $e->{$enc}->{'name'}};
|
---|
| 76 |
|
---|
[15868] | 77 | push(@{$encoding_list},$hashEncode);
|
---|
[10620] | 78 | }
|
---|
| 79 |
|
---|
[15868] | 80 | our $encoding_plus_auto_list =
|
---|
| 81 | [ { 'name' => "auto",
|
---|
[16557] | 82 | 'desc' => "{BasePlugin.filename_encoding.auto}" },
|
---|
[23457] | 83 | { 'name' => "auto-language-analysis",
|
---|
[16557] | 84 | 'desc' => "{BasePlugin.filename_encoding.auto_language_analysis}" }, # textcat
|
---|
| 85 | { 'name' => "auto-filesystem-encoding",
|
---|
| 86 | 'desc' => "{BasePlugin.filename_encoding.auto_filesystem_encoding}" }, # locale
|
---|
| 87 | { 'name' => "auto-fl",
|
---|
| 88 | 'desc' => "{BasePlugin.filename_encoding.auto_fl}" }, # locale followed by textcat
|
---|
| 89 | { 'name' => "auto-lf",
|
---|
| 90 | 'desc' => "{BasePlugin.filename_encoding.auto_lf}" } ]; # texcat followed by locale
|
---|
[10620] | 91 |
|
---|
[15868] | 92 | push(@{$encoding_plus_auto_list},@{$encoding_list});
|
---|
| 93 |
|
---|
[16698] | 94 | our $oidtype_list =
|
---|
[17026] | 95 | [ { 'name' => "auto",
|
---|
| 96 | 'desc' => "{BasePlugin.OIDtype.auto}" },
|
---|
| 97 | { 'name' => "hash",
|
---|
[16698] | 98 | 'desc' => "{import.OIDtype.hash}" },
|
---|
[24290] | 99 | { 'name' => "hash_on_ga_xml",
|
---|
| 100 | 'desc' => "{import.OIDtype.hash_on_ga_xml}" },
|
---|
[26536] | 101 | { 'name' => "hash_on_full_filename",
|
---|
| 102 | 'desc' => "{import.OIDtype.hash_on_full_filename}" },
|
---|
[16698] | 103 | { 'name' => "assigned",
|
---|
| 104 | 'desc' => "{import.OIDtype.assigned}" },
|
---|
| 105 | { 'name' => "incremental",
|
---|
| 106 | 'desc' => "{import.OIDtype.incremental}" },
|
---|
[26221] | 107 | { 'name' => "filename",
|
---|
| 108 | 'desc' => "{import.OIDtype.filename}" },
|
---|
[16698] | 109 | { 'name' => "dirname",
|
---|
[26536] | 110 | 'desc' => "{import.OIDtype.dirname}" },
|
---|
| 111 | { 'name' => "full_filename",
|
---|
| 112 | 'desc' => "{import.OIDtype.full_filename}" } ];
|
---|
[16698] | 113 |
|
---|
[4873] | 114 | my $arguments =
|
---|
[3540] | 115 | [ { 'name' => "process_exp",
|
---|
[15868] | 116 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[6408] | 117 | 'type' => "regexp",
|
---|
[3540] | 118 | 'deft' => "",
|
---|
| 119 | 'reqd' => "no" },
|
---|
[16390] | 120 | { 'name' => "no_blocking",
|
---|
| 121 | 'desc' => "{BasePlugin.no_blocking}",
|
---|
| 122 | 'type' => "flag",
|
---|
| 123 | 'reqd' => "no"},
|
---|
[3540] | 124 | { 'name' => "block_exp",
|
---|
[15868] | 125 | 'desc' => "{BasePlugin.block_exp}",
|
---|
[6408] | 126 | 'type' => "regexp",
|
---|
[3540] | 127 | 'deft' => "",
|
---|
| 128 | 'reqd' => "no" },
|
---|
[22215] | 129 | { 'name' => "store_original_file",
|
---|
| 130 | 'desc' => "{BasePlugin.store_original_file}",
|
---|
| 131 | 'type' => "flag",
|
---|
| 132 | 'reqd' => "no" },
|
---|
[8892] | 133 | { 'name' => "associate_ext",
|
---|
[15868] | 134 | 'desc' => "{BasePlugin.associate_ext}",
|
---|
[8892] | 135 | 'type' => "string",
|
---|
| 136 | 'reqd' => "no" },
|
---|
[11122] | 137 | { 'name' => "associate_tail_re",
|
---|
[15868] | 138 | 'desc' => "{BasePlugin.associate_tail_re}",
|
---|
[11122] | 139 | 'type' => "string",
|
---|
| 140 | 'reqd' => "no" },
|
---|
[16698] | 141 | { 'name' => "OIDtype",
|
---|
| 142 | 'desc' => "{import.OIDtype}",
|
---|
| 143 | 'type' => "enum",
|
---|
| 144 | 'list' => $oidtype_list,
|
---|
[16847] | 145 | # leave default empty so we can tell if its been set or not - if not set will use option from import.pl
|
---|
[17026] | 146 | 'deft' => "auto",
|
---|
[18591] | 147 | 'reqd' => "no" },
|
---|
[16698] | 148 | { 'name' => "OIDmetadata",
|
---|
| 149 | 'desc' => "{import.OIDmetadata}",
|
---|
| 150 | 'type' => "metadata",
|
---|
| 151 | 'deft' => "dc.Identifier",
|
---|
[18591] | 152 | 'reqd' => "no" },
|
---|
[16698] | 153 | # { 'name' => "use_as_doc_identifier",
|
---|
| 154 | # 'desc' => "{BasePlugin.use_as_doc_identifier}",
|
---|
| 155 | # 'type' => "string",
|
---|
| 156 | # 'reqd' => "no" ,
|
---|
| 157 | # 'deft' => "" } ,
|
---|
[18320] | 158 | { 'name' => "no_cover_image",
|
---|
[15868] | 159 | 'desc' => "{BasePlugin.no_cover_image}",
|
---|
[3540] | 160 | 'type' => "flag",
|
---|
| 161 | 'reqd' => "no" },
|
---|
[15868] | 162 | { 'name' => "filename_encoding",
|
---|
| 163 | 'desc' => "{BasePlugin.filename_encoding}",
|
---|
| 164 | 'type' => "enum",
|
---|
| 165 | 'deft' => "auto",
|
---|
| 166 | 'list' => $encoding_plus_auto_list,
|
---|
[16390] | 167 | 'reqd' => "no" },
|
---|
| 168 | { 'name' => "smart_block",
|
---|
[16520] | 169 | 'desc' => "{common.deprecated}. {BasePlugin.smart_block}",
|
---|
[16390] | 170 | 'type' => "flag",
|
---|
| 171 | 'reqd' => "no",
|
---|
[18320] | 172 | 'hiddengli' => "yes" }, # deprecated, but leave in for old collections
|
---|
| 173 | { 'name' => "file_rename_method",
|
---|
| 174 | 'desc' => "{BasePlugin.file_rename_method}",
|
---|
| 175 | 'type' => "enum",
|
---|
| 176 | 'deft' => &get_default_file_rename_method(), # by default rename imported files and assoc files using this encoding
|
---|
| 177 | 'list' => $file_rename_method_list,
|
---|
| 178 | 'reqd' => "no"
|
---|
[23457] | 179 | }
|
---|
[15868] | 180 |
|
---|
| 181 | ];
|
---|
[3540] | 182 |
|
---|
[9398] | 183 |
|
---|
[15868] | 184 | my $options = { 'name' => "BasePlugin",
|
---|
| 185 | 'desc' => "{BasePlugin.desc}",
|
---|
[6408] | 186 | 'abstract' => "yes",
|
---|
| 187 | 'inherits' => "no",
|
---|
[4750] | 188 | 'args' => $arguments };
|
---|
[3540] | 189 |
|
---|
[4] | 190 | sub new {
|
---|
[10218] | 191 |
|
---|
[15868] | 192 | my ($class) = shift (@_);
|
---|
[16698] | 193 | my ($pluginlist,$inputargs,$hashArgOptLists,$auxiliary) = @_;
|
---|
[10218] | 194 | push(@$pluginlist, $class);
|
---|
[9398] | 195 |
|
---|
[15868] | 196 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 197 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10218] | 198 |
|
---|
[16698] | 199 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists,$auxiliary);
|
---|
[16390] | 200 |
|
---|
| 201 | if ($self->{'info_only'}) {
|
---|
| 202 | # don't worry about any options etc
|
---|
| 203 | return bless $self, $class;
|
---|
| 204 | }
|
---|
[10579] | 205 |
|
---|
[16390] | 206 | if ($self->{'smart_block'}) {
|
---|
| 207 | print STDERR "WARNING: -smart_block option has been deprecated and is no longer useful\n";
|
---|
| 208 | }
|
---|
| 209 | $self->{'smart_block'} = undef;
|
---|
| 210 |
|
---|
[15868] | 211 | my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
|
---|
| 212 | $self->{'plugin_type'} = $plugin_name;
|
---|
[10579] | 213 |
|
---|
[24404] | 214 | # remove ex. from OIDmetadata iff it's the only namespace prefix
|
---|
| 215 | $self->{'OIDmetadata'} =~ s/^ex\.([^.]+)$/$1/ if defined $self->{'OIDmetadata'};
|
---|
[2785] | 216 | $self->{'num_processed'} = 0;
|
---|
| 217 | $self->{'num_not_processed'} = 0;
|
---|
| 218 | $self->{'num_blocked'} = 0;
|
---|
| 219 | $self->{'num_archives'} = 0;
|
---|
[8678] | 220 | $self->{'cover_image'} = 1; # cover image is on by default
|
---|
[10218] | 221 | $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
|
---|
[19222] | 222 | $self->{'can_process_directories'} = 0;
|
---|
[10579] | 223 | #$self->{'option_list'} = $hashArgOptLists->{"OptList"};
|
---|
[3540] | 224 |
|
---|
[8892] | 225 | my $associate_ext = $self->{'associate_ext'};
|
---|
| 226 | if ((defined $associate_ext) && ($associate_ext ne "")) {
|
---|
[9351] | 227 |
|
---|
[11122] | 228 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
| 229 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
| 230 | my $outhandle = $self->{'outhandle'};
|
---|
| 231 | print $outhandle "Warning: can only specify 'associate_ext' or 'associate_tail_re'\n";
|
---|
| 232 | print $outhandle " defaulting to 'associate_tail_re'\n";
|
---|
[8892] | 233 | }
|
---|
[11122] | 234 | else {
|
---|
| 235 | my @exts = split(/,/,$associate_ext);
|
---|
[8892] | 236 |
|
---|
[11122] | 237 | my @exts_bracketed = map { $_ = "(?:\\.$_)" } @exts;
|
---|
| 238 | my $associate_tail_re = join("|",@exts_bracketed);
|
---|
| 239 | $self->{'associate_tail_re'} = $associate_tail_re;
|
---|
| 240 | }
|
---|
| 241 |
|
---|
| 242 | delete $self->{'associate_ext'};
|
---|
[8892] | 243 | }
|
---|
| 244 |
|
---|
[15868] | 245 | return bless $self, $class;
|
---|
[11089] | 246 |
|
---|
[4] | 247 | }
|
---|
| 248 |
|
---|
[16821] | 249 | sub merge_inheritance
|
---|
| 250 | {
|
---|
| 251 | my $self = {};
|
---|
| 252 | my @child_selfs = @_;
|
---|
| 253 |
|
---|
| 254 | foreach my $child_self (@child_selfs) {
|
---|
| 255 | foreach my $key (keys %$child_self) {
|
---|
| 256 | if (defined $self->{$key}) {
|
---|
| 257 | if ($self->{$key} ne $child_self->{$key}) {
|
---|
| 258 | # print STDERR "Warning: Conflicting value in multiple inheritance for '$key'\n";
|
---|
| 259 | # print STDERR "Existing stored value = $self->{$key}\n";
|
---|
| 260 | # print STDERR "New (child) value = $child_self->{$key}\n";
|
---|
| 261 | # print STDERR "Keeping existing value\n";
|
---|
| 262 | # Existing value seems to be option specified in collect.cfg
|
---|
| 263 |
|
---|
| 264 | ### $self->{$key} = $child_self->{$key};
|
---|
| 265 |
|
---|
| 266 | }
|
---|
| 267 | else {
|
---|
| 268 | ## print STDERR "****Info: Value $self->{$key} for $key already defined through multiple inheritance as the same value\n";
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | }
|
---|
| 272 | else {
|
---|
| 273 | $self->{$key} = $child_self->{$key};
|
---|
| 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | return $self;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[15868] | 281 | # initialize BasePlugin options
|
---|
| 282 | # if init() is overridden in a sub-class, remember to call BasePlugin::init()
|
---|
[1242] | 283 | sub init {
|
---|
| 284 | my $self = shift (@_);
|
---|
[2785] | 285 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
[1242] | 286 |
|
---|
| 287 | # verbosity is passed through from the processor
|
---|
| 288 | $self->{'verbosity'} = $verbosity;
|
---|
| 289 |
|
---|
[2785] | 290 | # as are the outhandle and failhandle
|
---|
[1424] | 291 | $self->{'outhandle'} = $outhandle if defined $outhandle;
|
---|
[2785] | 292 | $self->{'failhandle'} = $failhandle;
|
---|
[16390] | 293 | # $self->SUPER::init(@_);
|
---|
| 294 |
|
---|
[1242] | 295 | # set process_exp and block_exp to defaults unless they were
|
---|
| 296 | # explicitly set
|
---|
[1244] | 297 |
|
---|
| 298 | if ((!$self->is_recursive()) and
|
---|
[1242] | 299 | (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
|
---|
[1244] | 300 |
|
---|
[1242] | 301 | $self->{'process_exp'} = $self->get_default_process_exp ();
|
---|
| 302 | if ($self->{'process_exp'} eq "") {
|
---|
[1244] | 303 | warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
|
---|
[1242] | 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
|
---|
| 308 | $self->{'block_exp'} = $self->get_default_block_exp ();
|
---|
| 309 | }
|
---|
[11089] | 310 |
|
---|
[1242] | 311 | }
|
---|
| 312 |
|
---|
[839] | 313 | sub begin {
|
---|
| 314 | my $self = shift (@_);
|
---|
| 315 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
[16821] | 316 |
|
---|
[17026] | 317 | if ($self->{'OIDtype'} eq "auto") {
|
---|
| 318 | # hasn't been set in the plugin, use the processor values
|
---|
| 319 | $self->{'OIDtype'} = $processor->{'OIDtype'};
|
---|
| 320 | $self->{'OIDmetadata'} = $processor->{'OIDmetadata'};
|
---|
| 321 | }
|
---|
| 322 | if ($self->{'OIDtype'} eq "hash") {
|
---|
| 323 | # should we hash on the file or on the doc xml??
|
---|
| 324 | $self->{'OIDtype'} = $self->get_oid_hash_type();
|
---|
| 325 | if ($self->{'OIDtype'} !~ /^(hash_on_file|hash_on_ga_xml)$/) {
|
---|
| 326 | $self->{'OIDtype'} = "hash_on_file";
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
[839] | 329 | }
|
---|
| 330 |
|
---|
[21308] | 331 | # This is called once if removeold is set with import.pl. Most plugins will do
|
---|
| 332 | # nothing but if a plugin does any stuff outside of creating doc obj, then
|
---|
| 333 | # it may need to clear something.
|
---|
| 334 | sub remove_all {
|
---|
[21286] | 335 | my $self = shift (@_);
|
---|
| 336 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
[21308] | 339 | # This is called per document for docs that have been deleted from the
|
---|
| 340 | # collection. Most plugins will do nothing
|
---|
| 341 | # but if a plugin does any stuff outside of creating doc obj, then it may need
|
---|
| 342 | # to clear something.
|
---|
| 343 | sub remove_one {
|
---|
| 344 | my $self = shift (@_);
|
---|
| 345 |
|
---|
[21315] | 346 | my ($file, $oids, $archivedir) = @_;
|
---|
| 347 | return 0 if $self->can_process_this_file($file);
|
---|
| 348 | return undef;
|
---|
[21308] | 349 | }
|
---|
| 350 |
|
---|
[839] | 351 | sub end {
|
---|
[10155] | 352 | # potentially called at the end of each plugin pass
|
---|
| 353 | # import.pl only has one plugin pass, but buildcol.pl has multiple ones
|
---|
| 354 |
|
---|
[15868] | 355 | my ($self) = shift (@_);
|
---|
[839] | 356 | }
|
---|
| 357 |
|
---|
[10155] | 358 | sub deinit {
|
---|
| 359 | # called only once, after all plugin passes have been done
|
---|
| 360 |
|
---|
| 361 | my ($self) = @_;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
[17026] | 364 | # default hashing type is to hash on the original file (or converted file)
|
---|
| 365 | # override this to return hash_on_ga_xml for filetypes where hashing on the
|
---|
| 366 | # file is no good eg video
|
---|
| 367 | sub get_oid_hash_type {
|
---|
| 368 |
|
---|
| 369 | my $self = shift (@_);
|
---|
| 370 |
|
---|
| 371 | return "hash_on_file";
|
---|
| 372 | }
|
---|
| 373 |
|
---|
[15868] | 374 |
|
---|
[1242] | 375 | # this function should be overridden to return 1
|
---|
| 376 | # in recursive plugins
|
---|
[4] | 377 | sub is_recursive {
|
---|
| 378 | my $self = shift (@_);
|
---|
| 379 |
|
---|
[1242] | 380 | return 0;
|
---|
[4] | 381 | }
|
---|
| 382 |
|
---|
[1242] | 383 | sub get_default_block_exp {
|
---|
| 384 | my $self = shift (@_);
|
---|
| 385 |
|
---|
| 386 | return "";
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | sub get_default_process_exp {
|
---|
| 390 | my $self = shift (@_);
|
---|
| 391 |
|
---|
| 392 | return "";
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[23419] | 395 |
|
---|
[18320] | 396 | # rename imported files and assoc files using URL encoding by default
|
---|
| 397 | # as this will work for most plugins and give more legible filenames
|
---|
| 398 | sub get_default_file_rename_method() {
|
---|
| 399 | my $self = shift (@_);
|
---|
| 400 | return "url";
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[18398] | 403 | # returns this plugin's active (possibly user-selected) file_rename_method
|
---|
| 404 | sub get_file_rename_method() {
|
---|
| 405 | my $self = shift (@_);
|
---|
| 406 | my $rename_method = $self->{'file_rename_method'};
|
---|
| 407 | if($rename_method) {
|
---|
| 408 | return $rename_method;
|
---|
| 409 | } else {
|
---|
| 410 | return $self->get_default_file_rename_method();
|
---|
| 411 | }
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[16390] | 414 | # default implementation is to do nothing
|
---|
| 415 | sub store_block_files {
|
---|
| 416 |
|
---|
[9067] | 417 | my $self =shift (@_);
|
---|
[16390] | 418 | my ($filename_full_path, $block_hash) = @_;
|
---|
| 419 |
|
---|
[9067] | 420 | }
|
---|
| 421 |
|
---|
[16390] | 422 | # put files to block into hash
|
---|
| 423 | sub use_block_expressions {
|
---|
| 424 |
|
---|
| 425 | my $self =shift (@_);
|
---|
| 426 | my ($filename_full_path, $block_hash) = @_;
|
---|
| 427 |
|
---|
[23363] | 428 | $filename_full_path = &util::upgrade_if_dos_filename($filename_full_path);
|
---|
| 429 |
|
---|
[16390] | 430 | if ($self->{'block_exp'} ne "" && $filename_full_path =~ /$self->{'block_exp'}/) {
|
---|
[23561] | 431 | &util::block_filename($block_hash,$filename_full_path);
|
---|
[16390] | 432 | }
|
---|
| 433 |
|
---|
| 434 | }
|
---|
| 435 |
|
---|
[9067] | 436 | #default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
|
---|
| 437 | sub block_cover_image
|
---|
| 438 | {
|
---|
[10833] | 439 | my $self =shift;
|
---|
[16390] | 440 | my ($filename, $block_hash) = @_;
|
---|
[10833] | 441 |
|
---|
[23363] | 442 | $filename = &util::upgrade_if_dos_filename($filename);
|
---|
| 443 |
|
---|
[9067] | 444 | if ($self->{'cover_image'}) {
|
---|
| 445 | my $coverfile = $filename;
|
---|
| 446 | $coverfile =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
[23759] | 447 |
|
---|
[25743] | 448 | #if there is no file extension, coverfile will be the same as filename
|
---|
| 449 | return if $coverfile eq $filename;
|
---|
| 450 |
|
---|
[27306] | 451 | if (!&FileUtils::fileExists($coverfile)) {
|
---|
[9067] | 452 | $coverfile =~ s/jpg$/JPG/;
|
---|
| 453 | }
|
---|
[27306] | 454 | if (&FileUtils::fileExists($coverfile)) {
|
---|
[23561] | 455 | &util::block_filename($block_hash,$coverfile);
|
---|
[11089] | 456 | }
|
---|
[9067] | 457 | }
|
---|
| 458 |
|
---|
| 459 | return;
|
---|
| 460 | }
|
---|
[11122] | 461 |
|
---|
| 462 |
|
---|
[16390] | 463 | # discover all the files that should be blocked by this plugin
|
---|
| 464 | # check the args ...
|
---|
| 465 | sub file_block_read {
|
---|
[11122] | 466 |
|
---|
[8510] | 467 | my $self = shift (@_);
|
---|
[16390] | 468 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
[8892] | 469 | # Keep track of filenames with same root but different extensions
|
---|
[11122] | 470 | # Used to support -associate_ext and the more generalised
|
---|
| 471 | # -associate_tail_re
|
---|
[16390] | 472 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[8892] | 473 |
|
---|
[18441] | 474 | if (!-d $filename_full_path) {
|
---|
| 475 | $block_hash->{'all_files'}->{$file} = 1;
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[11122] | 478 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
| 479 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
| 480 | my ($file_prefix,$file_ext)
|
---|
[16390] | 481 | = &util::get_prefix_and_tail_by_regex($filename_full_path,$associate_tail_re);
|
---|
[8892] | 482 | if ((defined $file_prefix) && (defined $file_ext)) {
|
---|
[16390] | 483 | my $shared_fileroot = $block_hash->{'shared_fileroot'};
|
---|
[8892] | 484 | if (!defined $shared_fileroot->{$file_prefix}) {
|
---|
[11122] | 485 | my $file_prefix_rec = { 'tie_to' => undef,
|
---|
| 486 | 'exts' => {} };
|
---|
[8892] | 487 | $shared_fileroot->{$file_prefix} = $file_prefix_rec;
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | my $file_prefix_rec = $shared_fileroot->{$file_prefix};
|
---|
| 491 |
|
---|
[30358] | 492 | if ($self->can_process_this_file($filename_full_path) && $file_ext !~ m/.\./) {
|
---|
[8892] | 493 | # This is the document the others should be tied to
|
---|
| 494 | $file_prefix_rec->{'tie_to'} = $file_ext;
|
---|
| 495 | }
|
---|
| 496 | else {
|
---|
[11122] | 497 | if ($file_ext =~ m/$associate_tail_re$/) {
|
---|
[16390] | 498 | # this file should be associated to the main one
|
---|
[9351] | 499 | $file_prefix_rec->{'exts'}->{$file_ext} = 1;
|
---|
| 500 | }
|
---|
[8892] | 501 | }
|
---|
[11122] | 502 |
|
---|
[8892] | 503 | }
|
---|
| 504 | }
|
---|
[11122] | 505 |
|
---|
[16390] | 506 | # check block expressions
|
---|
| 507 | $self->use_block_expressions($filename_full_path, $block_hash) unless $self->{'no_blocking'};
|
---|
| 508 |
|
---|
[9067] | 509 | # now check whether we are actually processing this
|
---|
[16390] | 510 | if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
|
---|
[9067] | 511 | return undef; # can't recognise
|
---|
| 512 | }
|
---|
[23457] | 513 |
|
---|
[16852] | 514 | # if we have a block_exp, then this overrides the normal 'smart' blocking
|
---|
| 515 | $self->store_block_files($filename_full_path, $block_hash) unless ($self->{'no_blocking'} || $self->{'block_exp'} ne "");
|
---|
[8892] | 516 |
|
---|
[11089] | 517 | # block the cover image if there is one
|
---|
| 518 | if ($self->{'cover_image'}) {
|
---|
[16852] | 519 | $self->block_cover_image($filename_full_path, $block_hash);
|
---|
[11089] | 520 | }
|
---|
[23457] | 521 |
|
---|
[9067] | 522 | return 1;
|
---|
[8510] | 523 | }
|
---|
| 524 |
|
---|
[16390] | 525 | # plugins that rely on more than process_exp (eg XML plugins) can override this method
|
---|
| 526 | sub can_process_this_file {
|
---|
| 527 | my $self = shift(@_);
|
---|
| 528 | my ($filename) = @_;
|
---|
[8892] | 529 |
|
---|
[19222] | 530 | if (-d $filename && !$self->{'can_process_directories'}) {
|
---|
| 531 | return 0;
|
---|
| 532 | }
|
---|
[22427] | 533 |
|
---|
[16390] | 534 | if ($self->{'process_exp'} ne "" && $filename =~ /$self->{'process_exp'}/) {
|
---|
| 535 | return 1;
|
---|
[8892] | 536 | }
|
---|
| 537 | return 0;
|
---|
[10280] | 538 |
|
---|
| 539 | }
|
---|
| 540 |
|
---|
[24403] | 541 | # Even if a plugin can extract metadata in its metadata_read pass,
|
---|
| 542 | # make the default return 'undef' so processing of the file continues
|
---|
| 543 | # down the pipeline, so other plugins can also have the opportunity to
|
---|
| 544 | # locate metadata and set it up in the extrametakeys variables that
|
---|
| 545 | # are passed around.
|
---|
| 546 |
|
---|
| 547 | sub can_process_this_file_for_metadata {
|
---|
| 548 | my $self = shift(@_);
|
---|
| 549 |
|
---|
| 550 | return undef;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 |
|
---|
[16390] | 554 | # just converts path as is to utf8.
|
---|
| 555 | sub filepath_to_utf8 {
|
---|
[10280] | 556 | my $self = shift (@_);
|
---|
[15868] | 557 | my ($file, $file_encoding) = @_;
|
---|
[16390] | 558 | my $filemeta = $file;
|
---|
[10280] | 559 |
|
---|
[16557] | 560 | my $filename_encoding = $self->{'filename_encoding'}; # filename encoding setting
|
---|
| 561 |
|
---|
| 562 | # Whenever filename-encoding is set to any of the auto settings, we
|
---|
| 563 | # check if the filename is already in UTF8. If it is, then we're done.
|
---|
| 564 | if($filename_encoding =~ m/auto/) {
|
---|
[23457] | 565 | if(&unicode::check_is_utf8($filemeta))
|
---|
| 566 | {
|
---|
| 567 | $filename_encoding = "utf8";
|
---|
| 568 | return $filemeta;
|
---|
| 569 | }
|
---|
[16557] | 570 | }
|
---|
| 571 |
|
---|
| 572 | # Auto setting, but filename is not utf8
|
---|
| 573 | if ($filename_encoding eq "auto")
|
---|
| 574 | {
|
---|
[23457] | 575 | # try textcat
|
---|
| 576 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
[16557] | 577 |
|
---|
[23457] | 578 | # check the locale next
|
---|
| 579 | $filename_encoding = $self->locale_encoding() if $filename_encoding eq "undefined";
|
---|
[16557] | 580 |
|
---|
[23457] | 581 |
|
---|
| 582 | # now try the encoding of the document, if available
|
---|
| 583 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 584 | $filename_encoding = $file_encoding;
|
---|
| 585 | }
|
---|
[16557] | 586 |
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | elsif ($filename_encoding eq "auto-language-analysis")
|
---|
| 590 | {
|
---|
| 591 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
| 592 |
|
---|
| 593 | # now try the encoding of the document, if available
|
---|
| 594 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 595 | $filename_encoding = $file_encoding;
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | elsif ($filename_encoding eq "auto-filesystem-encoding")
|
---|
[23352] | 600 | {
|
---|
[16557] | 601 | # try locale
|
---|
| 602 | $filename_encoding = $self->locale_encoding();
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | elsif ($filename_encoding eq "auto-fl")
|
---|
| 606 | {
|
---|
| 607 | # filesystem-encoding (locale) then language-analysis (textcat)
|
---|
| 608 | $filename_encoding = $self->locale_encoding();
|
---|
| 609 |
|
---|
| 610 | # try textcat
|
---|
| 611 | $filename_encoding = $self->textcat_encoding($filemeta) if $filename_encoding eq "undefined";
|
---|
[23457] | 612 |
|
---|
[16557] | 613 | # else assume filename encoding is encoding of file content, if that's available
|
---|
| 614 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 615 | $filename_encoding = $file_encoding;
|
---|
[15868] | 616 | }
|
---|
[16557] | 617 | }
|
---|
[23457] | 618 |
|
---|
[16557] | 619 | elsif ($filename_encoding eq "auto-lf")
|
---|
| 620 | {
|
---|
| 621 | # language-analysis (textcat) then filesystem-encoding (locale)
|
---|
| 622 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
| 623 |
|
---|
| 624 | # guess filename encoding from encoding of file content, if available
|
---|
| 625 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 626 | $filename_encoding = $file_encoding;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | # try locale
|
---|
| 630 | $filename_encoding = $self->locale_encoding() if $filename_encoding eq "undefined";
|
---|
[15868] | 631 | }
|
---|
[23457] | 632 |
|
---|
[16557] | 633 | # if still undefined, use utf8 as fallback
|
---|
| 634 | if ($filename_encoding eq "undefined") {
|
---|
| 635 | $filename_encoding = "utf8";
|
---|
| 636 | }
|
---|
| 637 |
|
---|
[18171] | 638 | #print STDERR "**** UTF8 encoding the filename $filemeta ";
|
---|
[16767] | 639 |
|
---|
[16557] | 640 | # if the filename encoding is set to utf8 but it isn't utf8 already--such as when
|
---|
| 641 | # 1. the utf8 fallback is used, or 2. if the system locale is used and happens to
|
---|
| 642 | # be always utf8 (in which case the filename's encoding is also set as utf8 even
|
---|
| 643 | # though the filename need not be if it originates from another system)--in such
|
---|
| 644 | # cases attempt to make the filename utf8 to match.
|
---|
| 645 | if($filename_encoding eq "utf8" && !&unicode::check_is_utf8($filemeta)) {
|
---|
| 646 | &unicode::ensure_utf8(\$filemeta);
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | # convert non-unicode encodings to utf8
|
---|
| 650 | if ($filename_encoding !~ m/(?:ascii|utf8|unicode)/) {
|
---|
| 651 | $filemeta = &unicode::unicode2utf8(
|
---|
[23457] | 652 | &unicode::convert2unicode($filename_encoding, \$filemeta)
|
---|
| 653 | );
|
---|
[15868] | 654 | }
|
---|
[16390] | 655 |
|
---|
[18171] | 656 | #print STDERR " from encoding $filename_encoding -> $filemeta\n";
|
---|
[16390] | 657 | return $filemeta;
|
---|
| 658 | }
|
---|
| 659 |
|
---|
| 660 | # gets the filename with no path, converts to utf8, and then dm safes it.
|
---|
[18320] | 661 | # filename_encoding set by user
|
---|
[16390] | 662 | sub filename_to_utf8_metadata
|
---|
| 663 | {
|
---|
| 664 | my $self = shift (@_);
|
---|
| 665 | my ($file, $file_encoding) = @_;
|
---|
| 666 |
|
---|
| 667 | my $outhandle = $self->{'outhandle'};
|
---|
| 668 |
|
---|
[23457] | 669 | print $outhandle "****!!!!**** BasePlugin::filename_to_utf8_metadata now deprecated\n";
|
---|
| 670 | my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(0);
|
---|
| 671 | print $outhandle "Calling method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
[23347] | 672 |
|
---|
[16390] | 673 | my ($filemeta) = $file =~ /([^\\\/]+)$/; # getting the tail of the filepath (skips all string parts containing slashes upto the end)
|
---|
| 674 | $filemeta = $self->filepath_to_utf8($filemeta, $file_encoding);
|
---|
[15868] | 675 |
|
---|
[22652] | 676 | return $filemeta;
|
---|
[10280] | 677 | }
|
---|
| 678 |
|
---|
[16557] | 679 | sub locale_encoding {
|
---|
| 680 | my $self = shift(@_);
|
---|
| 681 |
|
---|
| 682 | if (!defined $self->{'filesystem_encoding'}) {
|
---|
| 683 | $self->{'filesystem_encoding'} = $self->get_filesystem_encoding();
|
---|
| 684 | }
|
---|
[10280] | 685 |
|
---|
[18171] | 686 | #print STDERR "*** filename encoding determined based on locale: " . $self->{'filesystem_encoding'} . "\n";
|
---|
[16557] | 687 | return $self->{'filesystem_encoding'}; # can be the string "undefined"
|
---|
| 688 | }
|
---|
| 689 |
|
---|
| 690 | sub textcat_encoding {
|
---|
| 691 | my $self = shift(@_);
|
---|
| 692 | my ($filemeta) = @_;
|
---|
| 693 |
|
---|
[22705] | 694 | # analyse filenames without extensions and digits (and trimmed of
|
---|
| 695 | # surrounding whitespace), so that irrelevant chars don't confuse
|
---|
| 696 | # textcat
|
---|
[16557] | 697 | my $strictfilemeta = $filemeta;
|
---|
| 698 | $strictfilemeta =~ s/\.[^\.]+$//g;
|
---|
| 699 | $strictfilemeta =~ s/\d//g;
|
---|
| 700 | $strictfilemeta =~ s/^\s*//g;
|
---|
| 701 | $strictfilemeta =~ s/\s*$//g;
|
---|
| 702 |
|
---|
| 703 | my $filename_encoding = $self->encoding_from_language_analysis($strictfilemeta);
|
---|
| 704 | if(!defined $filename_encoding) {
|
---|
| 705 | $filename_encoding = "undefined";
|
---|
| 706 | }
|
---|
| 707 |
|
---|
| 708 | return $filename_encoding; # can be the string "undefined"
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | # performs textcat
|
---|
| 712 | sub encoding_from_language_analysis {
|
---|
| 713 | my $self = shift(@_);
|
---|
| 714 | my ($text) = @_;
|
---|
| 715 |
|
---|
| 716 | my $outhandle = $self->{'outhandle'};
|
---|
| 717 | my $best_encoding = undef;
|
---|
[23457] | 718 |
|
---|
[17212] | 719 | # get the language/encoding of the textstring using textcat
|
---|
[22632] | 720 | require textcat; # Only load the textcat module if it is required
|
---|
[16557] | 721 | $self->{'textcat'} = new textcat() unless defined($self->{'textcat'});
|
---|
[16767] | 722 | my $results = $self->{'textcat'}->classify_cached_filename(\$text);
|
---|
[16557] | 723 |
|
---|
| 724 |
|
---|
| 725 | if (scalar @$results < 0) {
|
---|
| 726 | return undef;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | # We have some results, we choose the first
|
---|
| 730 | my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
|
---|
| 731 |
|
---|
| 732 | $best_encoding = $encoding;
|
---|
| 733 | if (!defined $best_encoding) {
|
---|
| 734 | return undef;
|
---|
| 735 | }
|
---|
[23457] | 736 |
|
---|
[16557] | 737 | if (defined $best_encoding && $best_encoding =~ m/^iso_8859/ && &unicode::check_is_utf8($text)) {
|
---|
| 738 | # the text is valid utf8, so assume that's the real encoding (since textcat is based on probabilities)
|
---|
| 739 | $best_encoding = 'utf8';
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 |
|
---|
| 743 | # check for equivalents where textcat doesn't have some encodings...
|
---|
| 744 | # eg MS versions of standard encodings
|
---|
| 745 | if (defined $best_encoding && $best_encoding =~ /^iso_8859_(\d+)/) {
|
---|
| 746 | my $iso = $1; # which variant of the iso standard?
|
---|
| 747 | # iso-8859 sets don't use chars 0x80-0x9f, windows codepages do
|
---|
| 748 | if ($text =~ /[\x80-\x9f]/) {
|
---|
| 749 | # Western Europe
|
---|
| 750 | if ($iso == 1 or $iso == 15) { $best_encoding = 'windows_1252' }
|
---|
| 751 | elsif ($iso == 2) {$best_encoding = 'windows_1250'} # Central Europe
|
---|
| 752 | elsif ($iso == 5) {$best_encoding = 'windows_1251'} # Cyrillic
|
---|
| 753 | elsif ($iso == 6) {$best_encoding = 'windows_1256'} # Arabic
|
---|
| 754 | elsif ($iso == 7) {$best_encoding = 'windows_1253'} # Greek
|
---|
| 755 | elsif ($iso == 8) {$best_encoding = 'windows_1255'} # Hebrew
|
---|
| 756 | elsif ($iso == 9) {$best_encoding = 'windows_1254'} # Turkish
|
---|
| 757 | }
|
---|
| 758 | }
|
---|
| 759 |
|
---|
| 760 | if (defined $best_encoding && $best_encoding !~ /^(ascii|utf8|unicode)$/ &&
|
---|
| 761 | !defined $encodings::encodings->{$best_encoding})
|
---|
| 762 | {
|
---|
| 763 | if ($self->{'verbosity'}) {
|
---|
| 764 | gsprintf($outhandle, "BasePlugin: {ReadTextFile.unsupported_encoding}\n", $text, $best_encoding, "undef");
|
---|
| 765 | }
|
---|
| 766 | $best_encoding = undef;
|
---|
| 767 | }
|
---|
| 768 |
|
---|
| 769 | return $best_encoding;
|
---|
| 770 | }
|
---|
| 771 |
|
---|
| 772 | # uses locale
|
---|
[23352] | 773 | sub get_filesystem_encoding
|
---|
| 774 | {
|
---|
[10280] | 775 |
|
---|
[15868] | 776 | my $self = shift(@_);
|
---|
| 777 |
|
---|
[14961] | 778 | my $outhandle = $self->{'outhandle'};
|
---|
| 779 | my $filesystem_encoding = undef;
|
---|
| 780 |
|
---|
| 781 | eval {
|
---|
[23352] | 782 | # Works for Windows as well, returning the DOS code page in use
|
---|
[14961] | 783 | use POSIX qw(locale_h);
|
---|
[15868] | 784 |
|
---|
| 785 | # With only one parameter, setlocale retrieves the
|
---|
| 786 | # current value
|
---|
[14961] | 787 | my $current_locale = setlocale(LC_CTYPE);
|
---|
[15868] | 788 |
|
---|
[23364] | 789 | my $char_encoding = undef;
|
---|
| 790 | if ($current_locale =~ m/\./) {
|
---|
| 791 | ($char_encoding) = ($current_locale =~ m/^.*\.(.*?)$/);
|
---|
| 792 | $char_encoding = lc($char_encoding);
|
---|
| 793 | }
|
---|
| 794 | else {
|
---|
| 795 | if ($current_locale =~ m/^(posix|c)$/i) {
|
---|
| 796 | $char_encoding = "ascii";
|
---|
| 797 | }
|
---|
| 798 | }
|
---|
| 799 |
|
---|
| 800 | if (defined $char_encoding) {
|
---|
[24564] | 801 | if ($char_encoding =~ m/^(iso)(8859)-?(\d{1,2})$/) {
|
---|
[15446] | 802 | $char_encoding = "$1\_$2\_$3";
|
---|
| 803 | }
|
---|
| 804 |
|
---|
[14961] | 805 | $char_encoding =~ s/-/_/g;
|
---|
| 806 | $char_encoding =~ s/^utf_8$/utf8/;
|
---|
[15868] | 807 |
|
---|
[14961] | 808 | if ($char_encoding =~ m/^\d+$/) {
|
---|
[15607] | 809 | if (defined $encodings::encodings->{"windows_$char_encoding"}) {
|
---|
[14961] | 810 | $char_encoding = "windows_$char_encoding";
|
---|
| 811 | }
|
---|
[15607] | 812 | elsif (defined $encodings::encodings->{"dos_$char_encoding"}) {
|
---|
[14961] | 813 | $char_encoding = "dos_$char_encoding";
|
---|
| 814 | }
|
---|
| 815 | }
|
---|
[15868] | 816 |
|
---|
[14961] | 817 | if (($char_encoding =~ m/(?:ascii|utf8|unicode)/)
|
---|
[15446] | 818 | || (defined $encodings::encodings->{$char_encoding})) {
|
---|
[14961] | 819 | $filesystem_encoding = $char_encoding;
|
---|
| 820 | }
|
---|
| 821 | else {
|
---|
| 822 | print $outhandle "Warning: Unsupported character encoding '$char_encoding' from locale '$current_locale'\n";
|
---|
| 823 | }
|
---|
| 824 | }
|
---|
[15868] | 825 |
|
---|
[14961] | 826 |
|
---|
| 827 | };
|
---|
| 828 | if ($@) {
|
---|
| 829 | print $outhandle "$@\n";
|
---|
[18398] | 830 | print $outhandle "Warning: Unable to establish locale. Will assume filesystem is UTF-8\n";
|
---|
[14961] | 831 |
|
---|
| 832 | }
|
---|
[23352] | 833 |
|
---|
[15868] | 834 | return $filesystem_encoding;
|
---|
| 835 | }
|
---|
[14961] | 836 |
|
---|
[23335] | 837 |
|
---|
[31445] | 838 | sub guess_filesystem_encoding
|
---|
| 839 | {
|
---|
| 840 | my $self = shift (@_);
|
---|
| 841 | # Look to file system to provide a character encoding
|
---|
| 842 | my $deduced_filename_encoding = "";
|
---|
| 843 | # If Windows NTFS, then -- assuming we work with long file names got through
|
---|
| 844 | # Win32::GetLongFilePath() -- then the underlying file system is UTF16
|
---|
[23335] | 845 |
|
---|
[31445] | 846 | if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
|
---|
| 847 | # Can do better than working with the DOS character encoding returned by locale
|
---|
| 848 | $deduced_filename_encoding = "unicode";
|
---|
| 849 | }
|
---|
| 850 | else {
|
---|
| 851 | # Unix of some form or other
|
---|
| 852 |
|
---|
| 853 | # See if we can determine the file system encoding through locale
|
---|
| 854 | $deduced_filename_encoding = $self->locale_encoding();
|
---|
| 855 |
|
---|
| 856 | }
|
---|
| 857 | print STDERR "guessing filesystem encoding is $deduced_filename_encoding\n";
|
---|
| 858 | return $deduced_filename_encoding;
|
---|
| 859 | }
|
---|
| 860 |
|
---|
| 861 |
|
---|
| 862 |
|
---|
[23335] | 863 | sub deduce_filename_encoding
|
---|
| 864 | {
|
---|
| 865 | my $self = shift (@_);
|
---|
[23347] | 866 | my ($file,$metadata,$plugin_filename_encoding) = @_;
|
---|
[23335] | 867 |
|
---|
[23465] | 868 | my $gs_filename_encoding = $metadata->{"gs.filenameEncoding"};
|
---|
[23335] | 869 | my $deduced_filename_encoding = undef;
|
---|
| 870 |
|
---|
| 871 | # Start by looking for manually assigned metadata
|
---|
| 872 | if (defined $gs_filename_encoding) {
|
---|
[23457] | 873 | if (ref ($gs_filename_encoding) eq "ARRAY") {
|
---|
| 874 | my $outhandle = $self->{'outhandle'};
|
---|
| 875 |
|
---|
| 876 | $deduced_filename_encoding = $gs_filename_encoding->[0];
|
---|
| 877 |
|
---|
| 878 | my $num_vals = scalar(@$gs_filename_encoding);
|
---|
| 879 | if ($num_vals>1) {
|
---|
[23465] | 880 | print $outhandle "Warning: gs.filenameEncoding multiply defined for $file\n";
|
---|
[23457] | 881 | print $outhandle " Selecting first value: $deduced_filename_encoding\n";
|
---|
| 882 | }
|
---|
| 883 | }
|
---|
| 884 | else {
|
---|
| 885 | $deduced_filename_encoding = $gs_filename_encoding;
|
---|
| 886 | }
|
---|
[23335] | 887 | }
|
---|
[23457] | 888 |
|
---|
[23335] | 889 | if (!defined $deduced_filename_encoding || ($deduced_filename_encoding =~ m/^\s*$/)) {
|
---|
[23457] | 890 | # Look to see if plugin specifies this value
|
---|
[23347] | 891 |
|
---|
[23457] | 892 | if (defined $plugin_filename_encoding) {
|
---|
| 893 | # First look to see if we're using any of the "older" (i.e. deprecated auto-... plugin options)
|
---|
| 894 | if ($plugin_filename_encoding =~ m/^auto-.*$/) {
|
---|
| 895 | my $outhandle = $self->{'outhandle'};
|
---|
| 896 | print $outhandle "Warning: $plugin_filename_encoding is no longer supported\n";
|
---|
| 897 | print $outhandle " default to 'auto'\n";
|
---|
| 898 | $self->{'filename_encoding'} = $plugin_filename_encoding = "auto";
|
---|
| 899 | }
|
---|
| 900 |
|
---|
| 901 | if ($plugin_filename_encoding ne "auto") {
|
---|
| 902 | # We've been given a specific filenamne encoding
|
---|
| 903 | # => so use it!
|
---|
| 904 | $deduced_filename_encoding = $plugin_filename_encoding;
|
---|
| 905 | }
|
---|
| 906 | }
|
---|
[23335] | 907 | }
|
---|
| 908 |
|
---|
| 909 | if (!defined $deduced_filename_encoding || ($deduced_filename_encoding =~ m/^\s*$/)) {
|
---|
[23347] | 910 |
|
---|
[23352] | 911 | # Look to file system to provide a character encoding
|
---|
[23347] | 912 |
|
---|
[23352] | 913 | # If Windows NTFS, then -- assuming we work with long file names got through
|
---|
| 914 | # Win32::GetLongFilePath() -- then the underlying file system is UTF16
|
---|
| 915 |
|
---|
[28375] | 916 | if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
|
---|
[23352] | 917 | # Can do better than working with the DOS character encoding returned by locale
|
---|
| 918 | $deduced_filename_encoding = "unicode";
|
---|
| 919 | }
|
---|
| 920 | else {
|
---|
| 921 | # Unix of some form or other
|
---|
| 922 |
|
---|
| 923 | # See if we can determine the file system encoding through locale
|
---|
| 924 | $deduced_filename_encoding = $self->locale_encoding();
|
---|
[23457] | 925 |
|
---|
[23352] | 926 | # if locale shows us filesystem is utf8, check to see filename is consistent
|
---|
| 927 | # => if not, then we have an "alien" filename on our hands
|
---|
[23457] | 928 |
|
---|
[23458] | 929 | if (defined $deduced_filename_encoding && $deduced_filename_encoding =~ m/^utf-?8$/i) {
|
---|
[23352] | 930 | if (!&unicode::check_is_utf8($file)) {
|
---|
| 931 | # "alien" filename, so revert
|
---|
| 932 | $deduced_filename_encoding = undef;
|
---|
[23347] | 933 | }
|
---|
[23352] | 934 | }
|
---|
| 935 | }
|
---|
[23335] | 936 | }
|
---|
| 937 |
|
---|
[23347] | 938 | # if (!defined $deduced_filename_encoding || ($deduced_filename_encoding =~ m/^\s*$/)) {
|
---|
| 939 | # # Last chance, apply textcat to deduce filename encoding
|
---|
| 940 | # $deduced_filename_encoding = $self->textcat_encoding($file);
|
---|
| 941 | # }
|
---|
| 942 |
|
---|
[23457] | 943 | if ($self->{'verbosity'}>3) {
|
---|
| 944 | my $outhandle = $self->{'outhandle'};
|
---|
[23347] | 945 |
|
---|
[23457] | 946 | if (defined $deduced_filename_encoding) {
|
---|
| 947 | print $outhandle " Deduced filename encoding as: $deduced_filename_encoding\n";
|
---|
[23347] | 948 | }
|
---|
[23457] | 949 | else {
|
---|
| 950 | print $outhandle " No filename encoding deduced\n";
|
---|
| 951 | }
|
---|
| 952 | }
|
---|
| 953 |
|
---|
[23335] | 954 | return $deduced_filename_encoding;
|
---|
| 955 | }
|
---|
| 956 |
|
---|
| 957 |
|
---|
| 958 |
|
---|
| 959 |
|
---|
| 960 | # Notionally written to be called once for each document, it is however safe to
|
---|
| 961 | # call multiple times (as in the case of ImagePlugin) which calls this later on
|
---|
| 962 | # after the original image has potentially been converted to a *new* source image
|
---|
| 963 | # format (e.g. TIFF to PNG)
|
---|
| 964 |
|
---|
[15868] | 965 | sub set_Source_metadata {
|
---|
| 966 | my $self = shift (@_);
|
---|
[23461] | 967 | my ($doc_obj, $raw_filename, $filename_encoding, $section) = @_;
|
---|
| 968 |
|
---|
[23335] | 969 | # 1. Sets the filename (Source) for display encoded as Unicode if possible,
|
---|
| 970 | # and (as a fallback) using %xx if not for non-ascii chars
|
---|
| 971 | # 2. Sets the url ref (SourceFile) to the URL encoded version
|
---|
| 972 | # of filename for generated files
|
---|
[23352] | 973 |
|
---|
| 974 | my ($unused_full_rf, $raw_file) = &util::get_full_filenames("", $raw_filename);
|
---|
[23335] | 975 |
|
---|
[23461] | 976 | my $this_section = (defined $section)? $section : $doc_obj->get_top_section();
|
---|
[23352] | 977 |
|
---|
| 978 | my $octet_file = $raw_file;
|
---|
| 979 |
|
---|
| 980 | # UTF-8 version of filename
|
---|
[23387] | 981 | # if ((defined $ENV{"DEBUG_UNICODE"}) && ($ENV{"DEBUG_UNICODE"})) {
|
---|
| 982 | # print STDERR "**** Setting Source Metadata given: $octet_file\n";
|
---|
| 983 | # }
|
---|
[14961] | 984 |
|
---|
[23352] | 985 | # Deal with (on Windows) raw filenames that are in their
|
---|
| 986 | # abbreviated DOS form
|
---|
| 987 |
|
---|
[28375] | 988 | if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
|
---|
[23352] | 989 | if ((defined $filename_encoding) && ($filename_encoding eq "unicode")) {
|
---|
| 990 | if (-e $raw_filename) {
|
---|
| 991 | my $unicode_filename = Win32::GetLongPathName($raw_filename);
|
---|
| 992 |
|
---|
| 993 | my $unused_full_uf;
|
---|
| 994 | ($unused_full_uf, $octet_file) = &util::get_full_filenames("", $unicode_filename);
|
---|
| 995 | }
|
---|
[23347] | 996 | }
|
---|
[23352] | 997 | }
|
---|
[23335] | 998 |
|
---|
| 999 | my $url_encoded_filename;
|
---|
[23364] | 1000 | if ((defined $filename_encoding) && ($filename_encoding ne "ascii")) {
|
---|
[23352] | 1001 | # => Generate a pretty print version of filename that is mapped to Unicode
|
---|
| 1002 |
|
---|
| 1003 | # Use filename_encoding to map raw filename to a Perl unicode-aware string
|
---|
| 1004 | $url_encoded_filename = decode($filename_encoding,$octet_file);
|
---|
[23335] | 1005 | }
|
---|
| 1006 | else {
|
---|
[23352] | 1007 | # otherwise generate %xx encoded version of filename for char > 127
|
---|
| 1008 | $url_encoded_filename = &unicode::raw_filename_to_url_encoded($octet_file);
|
---|
[23335] | 1009 | }
|
---|
[18320] | 1010 |
|
---|
[23387] | 1011 | # if ((defined $ENV{"DEBUG_UNICODE"}) && ($ENV{"DEBUG_UNICODE"})) {
|
---|
| 1012 | # print STDERR "****** saving Source as: $url_encoded_filename\n";
|
---|
| 1013 | # }
|
---|
[23759] | 1014 |
|
---|
| 1015 | # In the case of converted files and (generalized) exploded documents, there
|
---|
| 1016 | # will already be a source filename => store as OrigSource before overriding
|
---|
| 1017 | my $orig_source = $doc_obj->get_metadata_element ($this_section, "Source");
|
---|
| 1018 | if ((defined $orig_source) && ($orig_source !~ m/^\s*$/)) {
|
---|
| 1019 | $doc_obj->set_utf8_metadata_element($this_section, "OrigSource", $orig_source);
|
---|
| 1020 | }
|
---|
| 1021 |
|
---|
[16919] | 1022 | # Source is the UTF8 display name - not necessarily the name of the file on the system
|
---|
[23829] | 1023 | if ($ENV{'GSDLOS'} =~ m/^darwin$/i) {
|
---|
| 1024 | # on Darwin want all display strings to be in composed form, then can search on that
|
---|
| 1025 | $url_encoded_filename = normalize('C', $url_encoded_filename); # Normalisation Form 'C' (composition)
|
---|
| 1026 | }
|
---|
[31438] | 1027 | # set_utf8_metadata actually sets perl unicode aware strings. not utf8
|
---|
[23461] | 1028 | $doc_obj->set_utf8_metadata_element($this_section, "Source", $url_encoded_filename);
|
---|
[23759] | 1029 |
|
---|
[23352] | 1030 |
|
---|
[23335] | 1031 | my $renamed_raw_file = &util::rename_file($raw_file, $self->{'file_rename_method'});
|
---|
[18320] | 1032 | # If using URL encoding, then SourceFile is the url-reference to url-encoded
|
---|
[23335] | 1033 | # renamed_raw_url: it's a url that refers to the actual file on the system
|
---|
[31438] | 1034 | # this call just replaces % with %25
|
---|
[23335] | 1035 | my $renamed_raw_url = &unicode::filename_to_url($renamed_raw_file);
|
---|
[18320] | 1036 |
|
---|
[23461] | 1037 | $doc_obj->set_utf8_metadata_element($this_section, "SourceFile",
|
---|
[23335] | 1038 | $renamed_raw_url);
|
---|
| 1039 |
|
---|
[23387] | 1040 | # if ((defined $ENV{"DEBUG_UNICODE"}) && ($ENV{"DEBUG_UNICODE"})) {
|
---|
| 1041 | # print STDERR "****** saving SourceFile as: $renamed_raw_url\n";
|
---|
| 1042 | # }
|
---|
[14961] | 1043 | }
|
---|
[23457] | 1044 |
|
---|
[17026] | 1045 | # this should be called by all plugins to set the oid of the doc obj, rather
|
---|
| 1046 | # than calling doc_obj->set_OID directly
|
---|
[15868] | 1047 | sub add_OID {
|
---|
[15018] | 1048 | my $self = shift (@_);
|
---|
[26893] | 1049 | my ($doc_obj, $force) = @_;
|
---|
[15018] | 1050 |
|
---|
[26893] | 1051 | # don't add one if there is one already set, unless we are forced to do so
|
---|
| 1052 | return unless ($doc_obj->get_OID() =~ /^NULL$/ || $force);
|
---|
[17026] | 1053 | $doc_obj->set_OIDtype($self->{'OIDtype'}, $self->{'OIDmetadata'});
|
---|
[15018] | 1054 |
|
---|
[17026] | 1055 | # see if there is a plugin specific set_OID function
|
---|
| 1056 | if (defined ($self->can('set_OID'))) {
|
---|
| 1057 | $self->set_OID(@_); # pass through doc_obj and any extra arguments
|
---|
[15018] | 1058 | }
|
---|
[17026] | 1059 | else {
|
---|
[15018] | 1060 | # use the default set_OID() in doc.pm
|
---|
| 1061 | $doc_obj->set_OID();
|
---|
| 1062 | }
|
---|
[17026] | 1063 |
|
---|
[15018] | 1064 | }
|
---|
[23457] | 1065 |
|
---|
[15868] | 1066 | # The BasePlugin read_into_doc_obj() function. This function does all the
|
---|
| 1067 | # right things to make general options work for a given plugin. It doesn't do anything with the file other than setting reads in
|
---|
[10280] | 1068 | # a file and sets up a slew of metadata all saved in doc_obj, which
|
---|
| 1069 | # it then returns as part of a tuple (process_status,doc_obj)
|
---|
| 1070 | #
|
---|
| 1071 | # Much of this functionality used to reside in read, but it was broken
|
---|
| 1072 | # down into a supporting routine to make the code more flexible.
|
---|
| 1073 | #
|
---|
| 1074 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
| 1075 | # capable of processing many documents within a single file (e.g.
|
---|
| 1076 | # GMLPlug) will normally want to implement their own version of
|
---|
| 1077 | # read_into_doc_obj()
|
---|
| 1078 | #
|
---|
| 1079 | # Note that $base_dir might be "" and that $file might
|
---|
| 1080 | # include directories
|
---|
[15868] | 1081 |
|
---|
| 1082 | # currently blocking has been done before it gets here - does this affect secondary plugin stuff??
|
---|
[10280] | 1083 | sub read_into_doc_obj {
|
---|
| 1084 | my $self = shift (@_);
|
---|
[16390] | 1085 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[10280] | 1086 |
|
---|
[15868] | 1087 | my $outhandle = $self->{'outhandle'};
|
---|
[10280] | 1088 |
|
---|
[15868] | 1089 | # should we move this to read? What about secondary plugins?
|
---|
[23759] | 1090 | my $pp_file = &util::prettyprint_file($base_dir,$file,$gli);
|
---|
[15868] | 1091 | print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
|
---|
[23363] | 1092 | print $outhandle "$self->{'plugin_type'} processing $pp_file\n"
|
---|
[23457] | 1093 | if $self->{'verbosity'} > 1;
|
---|
[10280] | 1094 |
|
---|
[16390] | 1095 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[23457] | 1096 |
|
---|
[1242] | 1097 | # create a new document
|
---|
[18320] | 1098 | my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
|
---|
[14961] | 1099 | my $top_section = $doc_obj->get_top_section();
|
---|
| 1100 |
|
---|
| 1101 | $doc_obj->add_utf8_metadata($top_section, "Plugin", "$self->{'plugin_type'}");
|
---|
[15868] | 1102 | $doc_obj->add_utf8_metadata($top_section, "FileSize", (-s $filename_full_path));
|
---|
[23457] | 1103 |
|
---|
[18469] | 1104 |
|
---|
[23352] | 1105 | my $plugin_filename_encoding = $self->{'filename_encoding'};
|
---|
[23347] | 1106 | my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
|
---|
[23472] | 1107 | $self->set_Source_metadata($doc_obj,$filename_full_path,$filename_encoding,$top_section);
|
---|
[8166] | 1108 |
|
---|
[15868] | 1109 | # plugin specific stuff - what args do we need here??
|
---|
| 1110 | unless (defined ($self->process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
|
---|
| 1111 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
| 1112 | return -1;
|
---|
[2816] | 1113 | }
|
---|
[1242] | 1114 |
|
---|
[15868] | 1115 | # include any metadata passed in from previous plugins
|
---|
| 1116 | # note that this metadata is associated with the top level section
|
---|
| 1117 | my $section = $doc_obj->get_top_section();
|
---|
| 1118 | # can we merge these two methods??
|
---|
| 1119 | $self->add_associated_files($doc_obj, $filename_full_path);
|
---|
| 1120 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
| 1121 | $self->auto_extract_metadata($doc_obj);
|
---|
[1242] | 1122 |
|
---|
[15868] | 1123 | # if we haven't found any Title so far, assign one
|
---|
| 1124 | # this was shifted to here from inside read()
|
---|
| 1125 | $self->title_fallback($doc_obj,$section,$filename_no_path);
|
---|
| 1126 |
|
---|
| 1127 | $self->add_OID($doc_obj);
|
---|
| 1128 |
|
---|
[21219] | 1129 | $self->post_process_doc_obj($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli);
|
---|
[15868] | 1130 | return (1,$doc_obj);
|
---|
| 1131 | }
|
---|
[2785] | 1132 |
|
---|
[21219] | 1133 | sub post_process_doc_obj {
|
---|
| 1134 | my $self = shift (@_);
|
---|
| 1135 | my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
| 1136 |
|
---|
| 1137 | return 1;
|
---|
| 1138 | }
|
---|
| 1139 |
|
---|
[15868] | 1140 | sub add_dummy_text {
|
---|
| 1141 | my $self = shift(@_);
|
---|
| 1142 | my ($doc_obj, $section) = @_;
|
---|
[2785] | 1143 |
|
---|
[15868] | 1144 | # add NoText metadata so we can hide this dummy text in format statements
|
---|
| 1145 | $doc_obj->add_metadata($section, "NoText", "1");
|
---|
[31420] | 1146 |
|
---|
| 1147 | # lookup_string with extra '1' arg returns perl internal unicode aware text, so we use add_utf8_text so no encoding is done on it.
|
---|
| 1148 | $doc_obj->add_utf8_text($section, &gsprintf::lookup_string("{BasePlugin.dummy_text}",1));
|
---|
| 1149 | #$doc_obj->add_text($section, &gsprintf::lookup_string("{BasePlugin.dummy_text}",1));
|
---|
[15868] | 1150 |
|
---|
[31420] | 1151 |
|
---|
[15868] | 1152 | }
|
---|
[8510] | 1153 |
|
---|
[15868] | 1154 | # does nothing. Can be overridden by subclass
|
---|
| 1155 | sub auto_extract_metadata {
|
---|
| 1156 | my $self = shift(@_);
|
---|
| 1157 | my ($doc_obj) = @_;
|
---|
| 1158 | }
|
---|
[11122] | 1159 |
|
---|
[15868] | 1160 | # adds cover image, associate_file options stuff. Should be called by sub class
|
---|
| 1161 | # read_into_doc_obj
|
---|
| 1162 | sub add_associated_files {
|
---|
| 1163 | my $self = shift(@_);
|
---|
| 1164 | # whatis filename??
|
---|
| 1165 | my ($doc_obj, $filename) = @_;
|
---|
| 1166 |
|
---|
| 1167 | # add in the cover image
|
---|
| 1168 | if ($self->{'cover_image'}) {
|
---|
| 1169 | $self->associate_cover_image($doc_obj, $filename);
|
---|
[8716] | 1170 | }
|
---|
[22215] | 1171 | # store the original (used for eg TextPlugin to store the original for OAI)
|
---|
| 1172 | if ($self->{'store_original_file'}) {
|
---|
| 1173 | $self->associate_source_file($doc_obj, $filename);
|
---|
| 1174 | }
|
---|
[23457] | 1175 |
|
---|
[15018] | 1176 |
|
---|
[10280] | 1177 | }
|
---|
[1242] | 1178 |
|
---|
[16390] | 1179 | # implement this if you are extracting metadata for other documents
|
---|
| 1180 | sub metadata_read {
|
---|
| 1181 | my $self = shift (@_);
|
---|
[19493] | 1182 | my ($pluginfo, $base_dir, $file, $block_hash,
|
---|
| 1183 | $extrametakeys, $extrametadata, $extrametafile,
|
---|
[23212] | 1184 | $processor, $gli, $aux) = @_;
|
---|
[16390] | 1185 |
|
---|
| 1186 | # can we process this file??
|
---|
| 1187 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[24403] | 1188 | return undef unless $self->can_process_this_file_for_metadata($filename_full_path);
|
---|
[16390] | 1189 |
|
---|
| 1190 | return 1; # we recognise the file, but don't actually do anything with it
|
---|
| 1191 | }
|
---|
| 1192 |
|
---|
| 1193 |
|
---|
[15868] | 1194 | # The BasePlugin read() function. This function calls read_into_doc_obj()
|
---|
[10280] | 1195 | # to ensure all the right things to make general options work for a
|
---|
| 1196 | # given plugin are done. It then calls the process() function which
|
---|
| 1197 | # does all the work specific to a plugin (like the old read functions
|
---|
| 1198 | # used to do). Most plugins should define their own process() function
|
---|
| 1199 | # and let this read() function keep control.
|
---|
| 1200 | #
|
---|
| 1201 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
| 1202 | # capable of processing many documents within a single file (e.g.
|
---|
| 1203 | # GMLPlug) might want to implement their own version of read(), but
|
---|
| 1204 | # more likely need to implement their own version of read_into_doc_obj()
|
---|
| 1205 | #
|
---|
| 1206 | # Return number of files processed, undef if can't recognise, -1 if can't
|
---|
| 1207 | # process
|
---|
| 1208 |
|
---|
| 1209 | sub read {
|
---|
| 1210 | my $self = shift (@_);
|
---|
[16390] | 1211 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[10280] | 1212 |
|
---|
[16390] | 1213 | # can we process this file??
|
---|
| 1214 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[20577] | 1215 |
|
---|
[16390] | 1216 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
| 1217 |
|
---|
[31445] | 1218 | #print STDERR "**** BEFORE READ INTO DOC OBJ: $file\n";
|
---|
[10280] | 1219 | my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
|
---|
[31445] | 1220 | #print STDERR "**** AFTER READ INTO DOC OBJ: $file\n";
|
---|
| 1221 |
|
---|
[10280] | 1222 | if ((defined $process_status) && ($process_status == 1)) {
|
---|
[21219] | 1223 |
|
---|
[10280] | 1224 | # process the document
|
---|
| 1225 | $processor->process($doc_obj);
|
---|
[15868] | 1226 |
|
---|
[10280] | 1227 | $self->{'num_processed'} ++;
|
---|
| 1228 | undef $doc_obj;
|
---|
[9398] | 1229 | }
|
---|
[15868] | 1230 | # delete any temp files that we may have created
|
---|
| 1231 | $self->clean_up_after_doc_obj_processing();
|
---|
[9398] | 1232 |
|
---|
[18469] | 1233 |
|
---|
[10280] | 1234 | # if process_status == 1, then the file has been processed.
|
---|
| 1235 | return $process_status;
|
---|
| 1236 |
|
---|
[4] | 1237 | }
|
---|
| 1238 |
|
---|
[1244] | 1239 | # returns undef if file is rejected by the plugin
|
---|
[1242] | 1240 | sub process {
|
---|
| 1241 | my $self = shift (@_);
|
---|
[11089] | 1242 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
[1242] | 1243 |
|
---|
[23419] | 1244 | gsprintf(STDERR, "BasePlugin::process {common.must_be_implemented}\n");
|
---|
[1244] | 1245 |
|
---|
[23457] | 1246 | my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
[23419] | 1247 | print STDERR "Calling method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
| 1248 |
|
---|
[23457] | 1249 | die "\n";
|
---|
[23419] | 1250 |
|
---|
[1244] | 1251 | return undef; # never gets here
|
---|
[1242] | 1252 | }
|
---|
| 1253 |
|
---|
[15868] | 1254 | # overwrite this method to delete any temp files that we have created
|
---|
| 1255 | sub clean_up_after_doc_obj_processing {
|
---|
| 1256 | my $self = shift(@_);
|
---|
[4] | 1257 |
|
---|
[10280] | 1258 | }
|
---|
[16390] | 1259 |
|
---|
[10280] | 1260 | # write_file -- used by ConvertToPlug, for example in post processing
|
---|
| 1261 | #
|
---|
[15868] | 1262 | # where should this go, is here the best place??
|
---|
[10280] | 1263 | sub utf8_write_file {
|
---|
| 1264 | my $self = shift (@_);
|
---|
| 1265 | my ($textref, $filename) = @_;
|
---|
| 1266 |
|
---|
[22953] | 1267 | if (!open (FILE, ">:utf8", $filename)) {
|
---|
[10280] | 1268 | gsprintf(STDERR, "ConvertToPlug::write_file {ConvertToPlug.could_not_open_for_writing} ($!)\n", $filename);
|
---|
[23457] | 1269 | die "\n";
|
---|
| 1270 | }
|
---|
[10280] | 1271 | print FILE $$textref;
|
---|
| 1272 |
|
---|
[1219] | 1273 | close FILE;
|
---|
| 1274 | }
|
---|
| 1275 |
|
---|
[10280] | 1276 |
|
---|
[7504] | 1277 | sub filename_based_title
|
---|
| 1278 | {
|
---|
| 1279 | my $self = shift (@_);
|
---|
| 1280 | my ($file) = @_;
|
---|
| 1281 |
|
---|
| 1282 | my $file_derived_title = $file;
|
---|
| 1283 | $file_derived_title =~ s/_/ /g;
|
---|
[23335] | 1284 | $file_derived_title =~ s/\.[^.]+$//;
|
---|
[7504] | 1285 |
|
---|
| 1286 | return $file_derived_title;
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
[9398] | 1289 |
|
---|
[7504] | 1290 | sub title_fallback
|
---|
| 1291 | {
|
---|
| 1292 | my $self = shift (@_);
|
---|
| 1293 | my ($doc_obj,$section,$file) = @_;
|
---|
| 1294 |
|
---|
[22705] | 1295 | if (!defined $doc_obj->get_metadata_element ($section, "Title")
|
---|
[23335] | 1296 | || $doc_obj->get_metadata_element($section, "Title") eq "") {
|
---|
[7504] | 1297 |
|
---|
[23335] | 1298 | my $source_file = $doc_obj->get_metadata_element($section, "Source");
|
---|
| 1299 | my $file_derived_title;
|
---|
| 1300 | if (defined $source_file) {
|
---|
| 1301 | $file_derived_title = $self->filename_based_title($source_file);
|
---|
| 1302 | }
|
---|
| 1303 | else {
|
---|
| 1304 | # pp = pretty print
|
---|
| 1305 | my $pp_file = (defined $source_file) ? $source_file : $file;
|
---|
| 1306 |
|
---|
| 1307 | my $raw_title = $self->filename_based_title($file);
|
---|
| 1308 | my $file_derived_title = &unicode::raw_filename_to_url_encoded($raw_title);
|
---|
| 1309 | }
|
---|
| 1310 |
|
---|
| 1311 |
|
---|
[15868] | 1312 | if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
|
---|
| 1313 | $doc_obj->add_utf8_metadata ($section, "Title", $file_derived_title);
|
---|
[9413] | 1314 | }
|
---|
[15868] | 1315 | else {
|
---|
[16995] | 1316 | $doc_obj->set_utf8_metadata_element ($section, "Title", $file_derived_title);
|
---|
[15868] | 1317 | }
|
---|
[9413] | 1318 | }
|
---|
[23457] | 1319 |
|
---|
[1844] | 1320 | }
|
---|
[23457] | 1321 |
|
---|
[1219] | 1322 | # add any extra metadata that's been passed around from one
|
---|
| 1323 | # plugin to another.
|
---|
| 1324 | # extra_metadata uses add_utf8_metadata so it expects metadata values
|
---|
| 1325 | # to already be in utf8
|
---|
| 1326 | sub extra_metadata {
|
---|
| 1327 | my $self = shift (@_);
|
---|
| 1328 | my ($doc_obj, $cursection, $metadata) = @_;
|
---|
| 1329 |
|
---|
[11122] | 1330 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
| 1331 |
|
---|
[27949] | 1332 | # Sort the extra metadata for diffcol so these meta appear in a consistent order
|
---|
| 1333 | # in doc.xml. Necessary for the ex.PDF.* and ex.File.* meta that's extracted in
|
---|
| 1334 | # the PDFBox collection, as the order of these varies between CentOS and Ubuntu.
|
---|
[27973] | 1335 | foreach my $field (sort keys(%$metadata)) {
|
---|
| 1336 | # foreach my $field (keys(%$metadata)) {
|
---|
[839] | 1337 | # $metadata->{$field} may be an array reference
|
---|
[8510] | 1338 | if ($field eq "gsdlassocfile_tobe") {
|
---|
| 1339 | # 'gsdlassocfile_tobe' is artificially introduced metadata
|
---|
| 1340 | # that is used to signal that certain additional files should
|
---|
| 1341 | # be tied to this document. Useful in situations where a
|
---|
| 1342 | # metadata pass in the plugin pipeline works out some files
|
---|
| 1343 | # need to be associated with a document, but the document hasn't
|
---|
| 1344 | # been formed yet.
|
---|
| 1345 | my $equiv_form = "";
|
---|
| 1346 | foreach my $gaf (@{$metadata->{$field}}) {
|
---|
| 1347 | my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
|
---|
[18171] | 1348 | my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
|
---|
| 1349 |
|
---|
| 1350 | # we need to make sure the filename is valid utf-8 - we do
|
---|
[18320] | 1351 | # this by url or base64 encoding it
|
---|
[18171] | 1352 | # $tail_filename is the name that we store the file as
|
---|
[18320] | 1353 | $tail_filename = &util::rename_file($tail_filename, $self->{'file_rename_method'});
|
---|
[8510] | 1354 | $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
|
---|
[20778] | 1355 | $doc_obj->associate_source_file($full_filename);
|
---|
[18320] | 1356 | # If the filename is url_encoded, we need to encode the % signs
|
---|
| 1357 | # in the filename, so that it works in a url
|
---|
[18404] | 1358 | my $url_tail_filename = &unicode::filename_to_url($tail_filename);
|
---|
[11122] | 1359 | # work out extended tail extension (i.e. matching tail re)
|
---|
| 1360 |
|
---|
| 1361 | my ($file_prefix,$file_extended_ext)
|
---|
[16390] | 1362 | = &util::get_prefix_and_tail_by_regex($tail_filename,$associate_tail_re);
|
---|
[11122] | 1363 | my ($pre_doc_ext) = ($file_extended_ext =~ m/^(.*)\..*$/);
|
---|
[8510] | 1364 | my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
|
---|
[24219] | 1365 |
|
---|
[25345] | 1366 | # the greenstone 2 stuff
|
---|
| 1367 | my $start_doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/{Or}{[parent(Top):assocfilepath],[assocfilepath]}/$url_tail_filename\">";
|
---|
| 1368 | #my $start_doclink = "<a href=\'_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$url_tail_filename\'>";
|
---|
[24219] | 1369 | my $start_doclink_gs3 = "<a href=\'_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$url_tail_filename\'>";
|
---|
| 1370 |
|
---|
[8510] | 1371 | my $srcicon = "_icon".$doc_ext."_";
|
---|
| 1372 | my $end_doclink = "</a>";
|
---|
[24219] | 1373 |
|
---|
[11122] | 1374 | my $assoc_form = "$start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
|
---|
| 1375 |
|
---|
[24219] | 1376 |
|
---|
[18171] | 1377 | if (defined $pre_doc_ext && $pre_doc_ext ne "") {
|
---|
[11122] | 1378 | # for metadata such as [mp3._edited] [mp3._full] ...
|
---|
| 1379 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.$pre_doc_ext", $assoc_form);
|
---|
| 1380 | }
|
---|
| 1381 |
|
---|
| 1382 | # for multiple metadata such as [mp3.assoclink]
|
---|
| 1383 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.assoclink", $assoc_form);
|
---|
[24219] | 1384 |
|
---|
[11122] | 1385 | $equiv_form .= " $assoc_form";
|
---|
[25345] | 1386 |
|
---|
| 1387 | # following are used for greenstone 3,
|
---|
| 1388 | $doc_obj->add_utf8_metadata ($cursection, "equivDocLink", $start_doclink_gs3);
|
---|
| 1389 | $doc_obj->add_utf8_metadata ($cursection, "equivDocIcon", $srcicon);
|
---|
| 1390 | $doc_obj->add_utf8_metadata ($cursection, "/equivDocLink", $end_doclink);
|
---|
| 1391 |
|
---|
[8510] | 1392 | }
|
---|
| 1393 | $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
|
---|
| 1394 | }
|
---|
[23279] | 1395 | elsif ($field eq "gsdlzipfilename") {
|
---|
| 1396 | # special case for when files have come out of a zip. source_path
|
---|
| 1397 | # (used for archives dbs and keeping track for incremental import)
|
---|
| 1398 | # must be set to the zip file name
|
---|
| 1399 | my $zip_filename = $metadata->{$field};
|
---|
| 1400 | # overwrite the source_path
|
---|
[23363] | 1401 | $doc_obj->set_source_path($zip_filename);
|
---|
[23279] | 1402 | # and set the metadata
|
---|
| 1403 | $zip_filename = &util::filename_within_collection($zip_filename);
|
---|
| 1404 | $zip_filename = $doc_obj->encode_filename($zip_filename, $self->{'file_rename_method'});
|
---|
| 1405 | $doc_obj->add_utf8_metadata ($cursection, $field, $zip_filename);
|
---|
| 1406 | }
|
---|
[8510] | 1407 | elsif (ref ($metadata->{$field}) eq "ARRAY") {
|
---|
[839] | 1408 | map {
|
---|
[1219] | 1409 | $doc_obj->add_utf8_metadata ($cursection, $field, $_);
|
---|
[839] | 1410 | } @{$metadata->{$field}};
|
---|
| 1411 | } else {
|
---|
[1219] | 1412 | $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
|
---|
[839] | 1413 | }
|
---|
| 1414 | }
|
---|
| 1415 | }
|
---|
| 1416 |
|
---|
[1396] | 1417 |
|
---|
[2785] | 1418 | sub compile_stats {
|
---|
| 1419 | my $self = shift(@_);
|
---|
| 1420 | my ($stats) = @_;
|
---|
| 1421 |
|
---|
| 1422 | $stats->{'num_processed'} += $self->{'num_processed'};
|
---|
| 1423 | $stats->{'num_not_processed'} += $self->{'num_not_processed'};
|
---|
[2796] | 1424 | $stats->{'num_archives'} += $self->{'num_archives'};
|
---|
[2785] | 1425 |
|
---|
| 1426 | }
|
---|
[22215] | 1427 | sub associate_source_file {
|
---|
| 1428 | my $self = shift(@_);
|
---|
| 1429 |
|
---|
| 1430 | my ($doc_obj, $filename) = @_;
|
---|
| 1431 | my $cursection = $doc_obj->get_top_section();
|
---|
| 1432 | my $assocfilename = $doc_obj->get_assocfile_from_sourcefile();
|
---|
| 1433 |
|
---|
| 1434 | $doc_obj->associate_file($filename, $assocfilename, undef, $cursection);
|
---|
[24225] | 1435 | # srclink_file is now deprecated because of the "_" in the metadataname. Use srclinkFile
|
---|
[22663] | 1436 | $doc_obj->add_utf8_metadata ($cursection, "srclink_file", $doc_obj->get_sourcefile());
|
---|
[24225] | 1437 | $doc_obj->add_utf8_metadata ($cursection, "srclinkFile", $doc_obj->get_sourcefile());
|
---|
[22215] | 1438 | }
|
---|
[2785] | 1439 |
|
---|
[2816] | 1440 | sub associate_cover_image {
|
---|
[22215] | 1441 | my $self = shift(@_);
|
---|
[2816] | 1442 | my ($doc_obj, $filename) = @_;
|
---|
| 1443 |
|
---|
[23363] | 1444 | my $upgraded_filename = &util::upgrade_if_dos_filename($filename);
|
---|
| 1445 |
|
---|
[10833] | 1446 | $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
[23363] | 1447 | $upgraded_filename =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
| 1448 |
|
---|
| 1449 | if (exists $self->{'covers_missing_cache'}->{$upgraded_filename}) {
|
---|
| 1450 | # don't stat() for existence e.g. for multiple document input files
|
---|
[10833] | 1451 | # (eg SplitPlug)
|
---|
| 1452 | return;
|
---|
| 1453 | }
|
---|
| 1454 |
|
---|
[9413] | 1455 | my $top_section=$doc_obj->get_top_section();
|
---|
| 1456 |
|
---|
[27306] | 1457 | if (&FileUtils::fileExists($upgraded_filename)) {
|
---|
[20778] | 1458 | $doc_obj->associate_source_file($filename);
|
---|
[13968] | 1459 | $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
|
---|
[9413] | 1460 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
[3086] | 1461 | } else {
|
---|
[10833] | 1462 | my $upper_filename = $filename;
|
---|
[23363] | 1463 | my $upgraded_upper_filename = $upgraded_filename;
|
---|
| 1464 |
|
---|
[10833] | 1465 | $upper_filename =~ s/jpg$/JPG/;
|
---|
[23363] | 1466 | $upgraded_upper_filename =~ s/jpg$/JPG/;
|
---|
| 1467 |
|
---|
[27306] | 1468 | if (&FileUtils::fileExists($upgraded_upper_filename)) {
|
---|
[20778] | 1469 | $doc_obj->associate_source_file($upper_filename);
|
---|
[10833] | 1470 | $doc_obj->associate_file($upper_filename, "cover.jpg",
|
---|
| 1471 | "image/jpeg");
|
---|
[9413] | 1472 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
[10833] | 1473 | } else {
|
---|
| 1474 | # file doesn't exist, so record the fact that it's missing so
|
---|
| 1475 | # we don't stat() again (stat is slow)
|
---|
[23363] | 1476 | $self->{'covers_missing_cache'}->{$upgraded_filename} = 1;
|
---|
[3086] | 1477 | }
|
---|
[2816] | 1478 | }
|
---|
[10833] | 1479 |
|
---|
[2816] | 1480 | }
|
---|
| 1481 |
|
---|
[11332] | 1482 |
|
---|
| 1483 | # Overridden by exploding plugins (eg. ISISPlug)
|
---|
| 1484 | sub clean_up_after_exploding
|
---|
| 1485 | {
|
---|
| 1486 | my $self = shift(@_);
|
---|
| 1487 | }
|
---|
| 1488 |
|
---|
| 1489 |
|
---|
[16390] | 1490 |
|
---|
[4] | 1491 | 1;
|
---|