[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;
|
---|
| 33 |
|
---|
[1870] | 34 | use encodings;
|
---|
[11389] | 35 | use unicode;
|
---|
[16557] | 36 | use textcat;
|
---|
[1242] | 37 | use doc;
|
---|
[2751] | 38 | use ghtml;
|
---|
[9413] | 39 | use gsprintf 'gsprintf';
|
---|
[4] | 40 |
|
---|
[15868] | 41 | use PrintInfo;
|
---|
[10218] | 42 |
|
---|
[15868] | 43 | BEGIN {
|
---|
| 44 | @BasePlugin::ISA = ( 'PrintInfo' );
|
---|
| 45 | }
|
---|
[5681] | 46 |
|
---|
[18320] | 47 | # the different methods that can be applied when renaming
|
---|
| 48 | # imported documents and their associated files
|
---|
| 49 | our $file_rename_method_list =
|
---|
| 50 | [ { 'name' => "url",
|
---|
| 51 | 'desc' => "{BasePlugin.rename_method.url}" },
|
---|
| 52 | { 'name' => "base64",
|
---|
[18398] | 53 | 'desc' => "{BasePlugin.rename_method.base64}" },
|
---|
[18404] | 54 | { 'name' => "none",
|
---|
| 55 | 'desc' => "{BasePlugin.rename_method.none}",
|
---|
[18398] | 56 | 'hiddengli' => "yes" } ];
|
---|
[18320] | 57 |
|
---|
[15868] | 58 | our $encoding_list =
|
---|
[10218] | 59 | [ { 'name' => "ascii",
|
---|
[16014] | 60 | 'desc' => "{BasePlugin.encoding.ascii}" },
|
---|
[4744] | 61 | { 'name' => "utf8",
|
---|
[16014] | 62 | 'desc' => "{BasePlugin.encoding.utf8}" },
|
---|
[4744] | 63 | { 'name' => "unicode",
|
---|
[16014] | 64 | 'desc' => "{BasePlugin.encoding.unicode}" } ];
|
---|
[16557] | 65 |
|
---|
[3540] | 66 |
|
---|
[10620] | 67 | my $e = $encodings::encodings;
|
---|
| 68 | foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
|
---|
| 69 | {
|
---|
| 70 | my $hashEncode =
|
---|
| 71 | {'name' => $enc,
|
---|
| 72 | 'desc' => $e->{$enc}->{'name'}};
|
---|
| 73 |
|
---|
[15868] | 74 | push(@{$encoding_list},$hashEncode);
|
---|
[10620] | 75 | }
|
---|
| 76 |
|
---|
[15868] | 77 | our $encoding_plus_auto_list =
|
---|
| 78 | [ { 'name' => "auto",
|
---|
[16557] | 79 | 'desc' => "{BasePlugin.filename_encoding.auto}" },
|
---|
| 80 | { 'name' => "auto-language-analysis",
|
---|
| 81 | 'desc' => "{BasePlugin.filename_encoding.auto_language_analysis}" }, # textcat
|
---|
| 82 | { 'name' => "auto-filesystem-encoding",
|
---|
| 83 | 'desc' => "{BasePlugin.filename_encoding.auto_filesystem_encoding}" }, # locale
|
---|
| 84 | { 'name' => "auto-fl",
|
---|
| 85 | 'desc' => "{BasePlugin.filename_encoding.auto_fl}" }, # locale followed by textcat
|
---|
| 86 | { 'name' => "auto-lf",
|
---|
| 87 | 'desc' => "{BasePlugin.filename_encoding.auto_lf}" } ]; # texcat followed by locale
|
---|
[10620] | 88 |
|
---|
[15868] | 89 | push(@{$encoding_plus_auto_list},@{$encoding_list});
|
---|
| 90 |
|
---|
[16698] | 91 | our $oidtype_list =
|
---|
[17026] | 92 | [ { 'name' => "auto",
|
---|
| 93 | 'desc' => "{BasePlugin.OIDtype.auto}" },
|
---|
| 94 | { 'name' => "hash",
|
---|
[16698] | 95 | 'desc' => "{import.OIDtype.hash}" },
|
---|
| 96 | { 'name' => "assigned",
|
---|
| 97 | 'desc' => "{import.OIDtype.assigned}" },
|
---|
| 98 | { 'name' => "incremental",
|
---|
| 99 | 'desc' => "{import.OIDtype.incremental}" },
|
---|
| 100 | { 'name' => "dirname",
|
---|
| 101 | 'desc' => "{import.OIDtype.dirname}" } ];
|
---|
| 102 |
|
---|
[4873] | 103 | my $arguments =
|
---|
[3540] | 104 | [ { 'name' => "process_exp",
|
---|
[15868] | 105 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[6408] | 106 | 'type' => "regexp",
|
---|
[3540] | 107 | 'deft' => "",
|
---|
| 108 | 'reqd' => "no" },
|
---|
[16390] | 109 | { 'name' => "no_blocking",
|
---|
| 110 | 'desc' => "{BasePlugin.no_blocking}",
|
---|
| 111 | 'type' => "flag",
|
---|
| 112 | 'reqd' => "no"},
|
---|
[3540] | 113 | { 'name' => "block_exp",
|
---|
[15868] | 114 | 'desc' => "{BasePlugin.block_exp}",
|
---|
[6408] | 115 | 'type' => "regexp",
|
---|
[3540] | 116 | 'deft' => "",
|
---|
| 117 | 'reqd' => "no" },
|
---|
[8892] | 118 | { 'name' => "associate_ext",
|
---|
[15868] | 119 | 'desc' => "{BasePlugin.associate_ext}",
|
---|
[8892] | 120 | 'type' => "string",
|
---|
| 121 | 'reqd' => "no" },
|
---|
[11122] | 122 | { 'name' => "associate_tail_re",
|
---|
[15868] | 123 | 'desc' => "{BasePlugin.associate_tail_re}",
|
---|
[11122] | 124 | 'type' => "string",
|
---|
| 125 | 'reqd' => "no" },
|
---|
[16698] | 126 | { 'name' => "OIDtype",
|
---|
| 127 | 'desc' => "{import.OIDtype}",
|
---|
| 128 | 'type' => "enum",
|
---|
| 129 | 'list' => $oidtype_list,
|
---|
[16847] | 130 | # leave default empty so we can tell if its been set or not - if not set will use option from import.pl
|
---|
[17026] | 131 | 'deft' => "auto",
|
---|
[16698] | 132 | 'reqd' => "no",
|
---|
| 133 | 'modegli' => "2" },
|
---|
| 134 | { 'name' => "OIDmetadata",
|
---|
| 135 | 'desc' => "{import.OIDmetadata}",
|
---|
| 136 | 'type' => "metadata",
|
---|
| 137 | 'deft' => "dc.Identifier",
|
---|
| 138 | 'reqd' => "no",
|
---|
[18320] | 139 | 'modegli' => "2" },
|
---|
[16698] | 140 | # { 'name' => "use_as_doc_identifier",
|
---|
| 141 | # 'desc' => "{BasePlugin.use_as_doc_identifier}",
|
---|
| 142 | # 'type' => "string",
|
---|
| 143 | # 'reqd' => "no" ,
|
---|
| 144 | # 'deft' => "" } ,
|
---|
[18320] | 145 | { 'name' => "no_cover_image",
|
---|
[15868] | 146 | 'desc' => "{BasePlugin.no_cover_image}",
|
---|
[3540] | 147 | 'type' => "flag",
|
---|
| 148 | 'reqd' => "no" },
|
---|
[15868] | 149 | { 'name' => "filename_encoding",
|
---|
| 150 | 'desc' => "{BasePlugin.filename_encoding}",
|
---|
| 151 | 'type' => "enum",
|
---|
| 152 | 'deft' => "auto",
|
---|
| 153 | 'list' => $encoding_plus_auto_list,
|
---|
[16390] | 154 | 'reqd' => "no" },
|
---|
| 155 | { 'name' => "smart_block",
|
---|
[16520] | 156 | 'desc' => "{common.deprecated}. {BasePlugin.smart_block}",
|
---|
[16390] | 157 | 'type' => "flag",
|
---|
| 158 | 'reqd' => "no",
|
---|
[18320] | 159 | 'hiddengli' => "yes" }, # deprecated, but leave in for old collections
|
---|
| 160 | { 'name' => "file_rename_method",
|
---|
| 161 | 'desc' => "{BasePlugin.file_rename_method}",
|
---|
| 162 | 'type' => "enum",
|
---|
| 163 | 'deft' => &get_default_file_rename_method(), # by default rename imported files and assoc files using this encoding
|
---|
| 164 | 'list' => $file_rename_method_list,
|
---|
| 165 | 'reqd' => "no"
|
---|
| 166 | }
|
---|
[15868] | 167 |
|
---|
| 168 | ];
|
---|
[3540] | 169 |
|
---|
[9398] | 170 |
|
---|
[15868] | 171 | my $options = { 'name' => "BasePlugin",
|
---|
| 172 | 'desc' => "{BasePlugin.desc}",
|
---|
[6408] | 173 | 'abstract' => "yes",
|
---|
| 174 | 'inherits' => "no",
|
---|
[4750] | 175 | 'args' => $arguments };
|
---|
[3540] | 176 |
|
---|
[4] | 177 | sub new {
|
---|
[10218] | 178 |
|
---|
[15868] | 179 | my ($class) = shift (@_);
|
---|
[16698] | 180 | my ($pluginlist,$inputargs,$hashArgOptLists,$auxiliary) = @_;
|
---|
[10218] | 181 | push(@$pluginlist, $class);
|
---|
[9398] | 182 |
|
---|
[15868] | 183 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 184 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10218] | 185 |
|
---|
[16698] | 186 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists,$auxiliary);
|
---|
[16390] | 187 |
|
---|
| 188 | if ($self->{'info_only'}) {
|
---|
| 189 | # don't worry about any options etc
|
---|
| 190 | return bless $self, $class;
|
---|
| 191 | }
|
---|
[10579] | 192 |
|
---|
[16390] | 193 | if ($self->{'smart_block'}) {
|
---|
| 194 | print STDERR "WARNING: -smart_block option has been deprecated and is no longer useful\n";
|
---|
| 195 | }
|
---|
| 196 | $self->{'smart_block'} = undef;
|
---|
| 197 |
|
---|
[15868] | 198 | my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
|
---|
| 199 | $self->{'plugin_type'} = $plugin_name;
|
---|
[10579] | 200 |
|
---|
[2785] | 201 | $self->{'num_processed'} = 0;
|
---|
| 202 | $self->{'num_not_processed'} = 0;
|
---|
| 203 | $self->{'num_blocked'} = 0;
|
---|
| 204 | $self->{'num_archives'} = 0;
|
---|
[8678] | 205 | $self->{'cover_image'} = 1; # cover image is on by default
|
---|
[10218] | 206 | $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
|
---|
[10579] | 207 | #$self->{'option_list'} = $hashArgOptLists->{"OptList"};
|
---|
[3540] | 208 |
|
---|
[8892] | 209 | my $associate_ext = $self->{'associate_ext'};
|
---|
| 210 | if ((defined $associate_ext) && ($associate_ext ne "")) {
|
---|
[9351] | 211 |
|
---|
[11122] | 212 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
| 213 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
| 214 | my $outhandle = $self->{'outhandle'};
|
---|
| 215 | print $outhandle "Warning: can only specify 'associate_ext' or 'associate_tail_re'\n";
|
---|
| 216 | print $outhandle " defaulting to 'associate_tail_re'\n";
|
---|
[8892] | 217 | }
|
---|
[11122] | 218 | else {
|
---|
| 219 | my @exts = split(/,/,$associate_ext);
|
---|
[8892] | 220 |
|
---|
[11122] | 221 | my @exts_bracketed = map { $_ = "(?:\\.$_)" } @exts;
|
---|
| 222 | my $associate_tail_re = join("|",@exts_bracketed);
|
---|
| 223 | $self->{'associate_tail_re'} = $associate_tail_re;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | delete $self->{'associate_ext'};
|
---|
[8892] | 227 | }
|
---|
| 228 |
|
---|
[15868] | 229 | return bless $self, $class;
|
---|
[11089] | 230 |
|
---|
[4] | 231 | }
|
---|
| 232 |
|
---|
[16821] | 233 | sub merge_inheritance
|
---|
| 234 | {
|
---|
| 235 | my $self = {};
|
---|
| 236 | my @child_selfs = @_;
|
---|
| 237 |
|
---|
| 238 | foreach my $child_self (@child_selfs) {
|
---|
| 239 | foreach my $key (keys %$child_self) {
|
---|
| 240 | if (defined $self->{$key}) {
|
---|
| 241 | if ($self->{$key} ne $child_self->{$key}) {
|
---|
| 242 | # print STDERR "Warning: Conflicting value in multiple inheritance for '$key'\n";
|
---|
| 243 | # print STDERR "Existing stored value = $self->{$key}\n";
|
---|
| 244 | # print STDERR "New (child) value = $child_self->{$key}\n";
|
---|
| 245 | # print STDERR "Keeping existing value\n";
|
---|
| 246 | # Existing value seems to be option specified in collect.cfg
|
---|
| 247 |
|
---|
| 248 | ### $self->{$key} = $child_self->{$key};
|
---|
| 249 |
|
---|
| 250 | }
|
---|
| 251 | else {
|
---|
| 252 | ## print STDERR "****Info: Value $self->{$key} for $key already defined through multiple inheritance as the same value\n";
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | }
|
---|
| 256 | else {
|
---|
| 257 | $self->{$key} = $child_self->{$key};
|
---|
| 258 | }
|
---|
| 259 | }
|
---|
| 260 | }
|
---|
| 261 |
|
---|
| 262 | return $self;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[15868] | 265 | # initialize BasePlugin options
|
---|
| 266 | # if init() is overridden in a sub-class, remember to call BasePlugin::init()
|
---|
[1242] | 267 | sub init {
|
---|
| 268 | my $self = shift (@_);
|
---|
[2785] | 269 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
[1242] | 270 |
|
---|
| 271 | # verbosity is passed through from the processor
|
---|
| 272 | $self->{'verbosity'} = $verbosity;
|
---|
| 273 |
|
---|
[2785] | 274 | # as are the outhandle and failhandle
|
---|
[1424] | 275 | $self->{'outhandle'} = $outhandle if defined $outhandle;
|
---|
[2785] | 276 | $self->{'failhandle'} = $failhandle;
|
---|
[16390] | 277 | # $self->SUPER::init(@_);
|
---|
| 278 |
|
---|
[1242] | 279 | # set process_exp and block_exp to defaults unless they were
|
---|
| 280 | # explicitly set
|
---|
[1244] | 281 |
|
---|
| 282 | if ((!$self->is_recursive()) and
|
---|
[1242] | 283 | (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
|
---|
[1244] | 284 |
|
---|
[1242] | 285 | $self->{'process_exp'} = $self->get_default_process_exp ();
|
---|
| 286 | if ($self->{'process_exp'} eq "") {
|
---|
[1244] | 287 | warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
|
---|
[1242] | 288 | }
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
|
---|
| 292 | $self->{'block_exp'} = $self->get_default_block_exp ();
|
---|
| 293 | }
|
---|
[11089] | 294 |
|
---|
[1242] | 295 | }
|
---|
| 296 |
|
---|
[839] | 297 | sub begin {
|
---|
| 298 | my $self = shift (@_);
|
---|
| 299 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
[16821] | 300 |
|
---|
[17026] | 301 | if ($self->{'OIDtype'} eq "auto") {
|
---|
| 302 | # hasn't been set in the plugin, use the processor values
|
---|
| 303 | $self->{'OIDtype'} = $processor->{'OIDtype'};
|
---|
| 304 | $self->{'OIDmetadata'} = $processor->{'OIDmetadata'};
|
---|
| 305 | }
|
---|
| 306 | if ($self->{'OIDtype'} eq "hash") {
|
---|
| 307 | # should we hash on the file or on the doc xml??
|
---|
| 308 | $self->{'OIDtype'} = $self->get_oid_hash_type();
|
---|
| 309 | if ($self->{'OIDtype'} !~ /^(hash_on_file|hash_on_ga_xml)$/) {
|
---|
| 310 | $self->{'OIDtype'} = "hash_on_file";
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
[839] | 313 | }
|
---|
| 314 |
|
---|
| 315 | sub end {
|
---|
[10155] | 316 | # potentially called at the end of each plugin pass
|
---|
| 317 | # import.pl only has one plugin pass, but buildcol.pl has multiple ones
|
---|
| 318 |
|
---|
[15868] | 319 | my ($self) = shift (@_);
|
---|
[839] | 320 | }
|
---|
| 321 |
|
---|
[10155] | 322 | sub deinit {
|
---|
| 323 | # called only once, after all plugin passes have been done
|
---|
| 324 |
|
---|
| 325 | my ($self) = @_;
|
---|
| 326 | }
|
---|
| 327 |
|
---|
[17026] | 328 | # default hashing type is to hash on the original file (or converted file)
|
---|
| 329 | # override this to return hash_on_ga_xml for filetypes where hashing on the
|
---|
| 330 | # file is no good eg video
|
---|
| 331 | sub get_oid_hash_type {
|
---|
| 332 |
|
---|
| 333 | my $self = shift (@_);
|
---|
| 334 |
|
---|
| 335 | return "hash_on_file";
|
---|
| 336 | }
|
---|
| 337 |
|
---|
[15868] | 338 | sub set_incremental {
|
---|
| 339 | my $self = shift(@_);
|
---|
| 340 | my ($incremental) = @_;
|
---|
| 341 |
|
---|
| 342 | $self->{'incremental'} = $incremental;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[1242] | 345 | # this function should be overridden to return 1
|
---|
| 346 | # in recursive plugins
|
---|
[4] | 347 | sub is_recursive {
|
---|
| 348 | my $self = shift (@_);
|
---|
| 349 |
|
---|
[1242] | 350 | return 0;
|
---|
[4] | 351 | }
|
---|
| 352 |
|
---|
[1242] | 353 | sub get_default_block_exp {
|
---|
| 354 | my $self = shift (@_);
|
---|
| 355 |
|
---|
| 356 | return "";
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | sub get_default_process_exp {
|
---|
| 360 | my $self = shift (@_);
|
---|
| 361 |
|
---|
| 362 | return "";
|
---|
| 363 | }
|
---|
| 364 |
|
---|
[18320] | 365 | # rename imported files and assoc files using URL encoding by default
|
---|
| 366 | # as this will work for most plugins and give more legible filenames
|
---|
| 367 | sub get_default_file_rename_method() {
|
---|
| 368 | my $self = shift (@_);
|
---|
| 369 | return "url";
|
---|
| 370 | }
|
---|
| 371 |
|
---|
[18398] | 372 | # returns this plugin's active (possibly user-selected) file_rename_method
|
---|
| 373 | sub get_file_rename_method() {
|
---|
| 374 | my $self = shift (@_);
|
---|
| 375 | my $rename_method = $self->{'file_rename_method'};
|
---|
| 376 | if($rename_method) {
|
---|
| 377 | return $rename_method;
|
---|
| 378 | } else {
|
---|
| 379 | return $self->get_default_file_rename_method();
|
---|
| 380 | }
|
---|
| 381 | }
|
---|
| 382 |
|
---|
[16390] | 383 | # default implementation is to do nothing
|
---|
| 384 | sub store_block_files {
|
---|
| 385 |
|
---|
[9067] | 386 | my $self =shift (@_);
|
---|
[16390] | 387 | my ($filename_full_path, $block_hash) = @_;
|
---|
| 388 |
|
---|
[9067] | 389 | }
|
---|
| 390 |
|
---|
[16390] | 391 | # put files to block into hash
|
---|
| 392 | sub use_block_expressions {
|
---|
| 393 |
|
---|
| 394 | my $self =shift (@_);
|
---|
| 395 | my ($filename_full_path, $block_hash) = @_;
|
---|
| 396 |
|
---|
| 397 | if ($self->{'block_exp'} ne "" && $filename_full_path =~ /$self->{'block_exp'}/) {
|
---|
| 398 | $block_hash->{'file_blocks'}->{$filename_full_path} = 1;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[9067] | 403 | #default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
|
---|
| 404 | sub block_cover_image
|
---|
| 405 | {
|
---|
[10833] | 406 | my $self =shift;
|
---|
[16390] | 407 | my ($filename, $block_hash) = @_;
|
---|
[10833] | 408 |
|
---|
[9067] | 409 | if ($self->{'cover_image'}) {
|
---|
| 410 | my $coverfile = $filename;
|
---|
| 411 | $coverfile =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
| 412 | if (!-e $coverfile) {
|
---|
| 413 | $coverfile =~ s/jpg$/JPG/;
|
---|
| 414 | }
|
---|
| 415 | if (-e $coverfile) {
|
---|
[16390] | 416 | $block_hash->{'file_blocks'}->{$coverfile} = 1;
|
---|
[11089] | 417 | }
|
---|
[9067] | 418 | }
|
---|
| 419 |
|
---|
| 420 | return;
|
---|
| 421 | }
|
---|
[11122] | 422 |
|
---|
| 423 |
|
---|
[16390] | 424 | # discover all the files that should be blocked by this plugin
|
---|
| 425 | # check the args ...
|
---|
| 426 | sub file_block_read {
|
---|
[11122] | 427 |
|
---|
[8510] | 428 | my $self = shift (@_);
|
---|
[16390] | 429 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
[8892] | 430 | # Keep track of filenames with same root but different extensions
|
---|
[11122] | 431 | # Used to support -associate_ext and the more generalised
|
---|
| 432 | # -associate_tail_re
|
---|
[16390] | 433 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[8892] | 434 |
|
---|
[18441] | 435 | if (!-d $filename_full_path) {
|
---|
| 436 | $block_hash->{'all_files'}->{$file} = 1;
|
---|
| 437 | }
|
---|
| 438 |
|
---|
[11122] | 439 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
| 440 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
| 441 | my ($file_prefix,$file_ext)
|
---|
[16390] | 442 | = &util::get_prefix_and_tail_by_regex($filename_full_path,$associate_tail_re);
|
---|
[8892] | 443 | if ((defined $file_prefix) && (defined $file_ext)) {
|
---|
[16390] | 444 | my $shared_fileroot = $block_hash->{'shared_fileroot'};
|
---|
[8892] | 445 | if (!defined $shared_fileroot->{$file_prefix}) {
|
---|
[11122] | 446 | my $file_prefix_rec = { 'tie_to' => undef,
|
---|
| 447 | 'exts' => {} };
|
---|
[8892] | 448 | $shared_fileroot->{$file_prefix} = $file_prefix_rec;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | my $file_prefix_rec = $shared_fileroot->{$file_prefix};
|
---|
| 452 |
|
---|
[16390] | 453 | if ($self->can_process_this_file($filename_full_path)) {
|
---|
[8892] | 454 | # This is the document the others should be tied to
|
---|
| 455 | $file_prefix_rec->{'tie_to'} = $file_ext;
|
---|
| 456 | }
|
---|
| 457 | else {
|
---|
[11122] | 458 | if ($file_ext =~ m/$associate_tail_re$/) {
|
---|
[16390] | 459 | # this file should be associated to the main one
|
---|
[9351] | 460 | $file_prefix_rec->{'exts'}->{$file_ext} = 1;
|
---|
| 461 | }
|
---|
[8892] | 462 | }
|
---|
[11122] | 463 |
|
---|
[8892] | 464 | }
|
---|
| 465 | }
|
---|
[11122] | 466 |
|
---|
[16390] | 467 | # check block expressions
|
---|
| 468 | $self->use_block_expressions($filename_full_path, $block_hash) unless $self->{'no_blocking'};
|
---|
| 469 |
|
---|
[9067] | 470 | # now check whether we are actually processing this
|
---|
[16390] | 471 | if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
|
---|
[9067] | 472 | return undef; # can't recognise
|
---|
| 473 | }
|
---|
[16390] | 474 |
|
---|
[16852] | 475 | # if we have a block_exp, then this overrides the normal 'smart' blocking
|
---|
| 476 | $self->store_block_files($filename_full_path, $block_hash) unless ($self->{'no_blocking'} || $self->{'block_exp'} ne "");
|
---|
[8892] | 477 |
|
---|
[11089] | 478 | # block the cover image if there is one
|
---|
| 479 | if ($self->{'cover_image'}) {
|
---|
[16852] | 480 | $self->block_cover_image($filename_full_path, $block_hash);
|
---|
[11089] | 481 | }
|
---|
[9067] | 482 |
|
---|
| 483 | return 1;
|
---|
[8510] | 484 | }
|
---|
| 485 |
|
---|
[16390] | 486 | # plugins that rely on more than process_exp (eg XML plugins) can override this method
|
---|
| 487 | sub can_process_this_file {
|
---|
| 488 | my $self = shift(@_);
|
---|
| 489 | my ($filename) = @_;
|
---|
[8892] | 490 |
|
---|
[16390] | 491 | if ($self->{'process_exp'} ne "" && $filename =~ /$self->{'process_exp'}/) {
|
---|
| 492 | return 1;
|
---|
[8892] | 493 | }
|
---|
| 494 | return 0;
|
---|
[10280] | 495 |
|
---|
| 496 | }
|
---|
| 497 |
|
---|
[16390] | 498 | # just converts path as is to utf8.
|
---|
| 499 | sub filepath_to_utf8 {
|
---|
[10280] | 500 | my $self = shift (@_);
|
---|
[15868] | 501 | my ($file, $file_encoding) = @_;
|
---|
[16390] | 502 | my $filemeta = $file;
|
---|
[10280] | 503 |
|
---|
[16557] | 504 | my $filename_encoding = $self->{'filename_encoding'}; # filename encoding setting
|
---|
| 505 |
|
---|
| 506 | # Whenever filename-encoding is set to any of the auto settings, we
|
---|
| 507 | # check if the filename is already in UTF8. If it is, then we're done.
|
---|
| 508 | if($filename_encoding =~ m/auto/) {
|
---|
| 509 | if(&unicode::check_is_utf8($filemeta))
|
---|
| 510 | {
|
---|
| 511 | $filename_encoding = "utf8";
|
---|
| 512 | return $filemeta;
|
---|
| 513 | }
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | # Auto setting, but filename is not utf8
|
---|
| 517 | if ($filename_encoding eq "auto")
|
---|
| 518 | {
|
---|
| 519 | # try textcat
|
---|
| 520 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
| 521 |
|
---|
| 522 | # check the locale next
|
---|
| 523 | $filename_encoding = $self->locale_encoding() if $filename_encoding eq "undefined";
|
---|
| 524 |
|
---|
| 525 |
|
---|
| 526 | # now try the encoding of the document, if available
|
---|
| 527 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 528 | $filename_encoding = $file_encoding;
|
---|
[15868] | 529 | }
|
---|
[16557] | 530 |
|
---|
| 531 | }
|
---|
| 532 |
|
---|
| 533 | elsif ($filename_encoding eq "auto-language-analysis")
|
---|
| 534 | {
|
---|
| 535 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
| 536 |
|
---|
| 537 | # now try the encoding of the document, if available
|
---|
| 538 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 539 | $filename_encoding = $file_encoding;
|
---|
| 540 | }
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | elsif ($filename_encoding eq "auto-filesystem-encoding")
|
---|
| 544 | {
|
---|
| 545 | # try locale
|
---|
| 546 | $filename_encoding = $self->locale_encoding();
|
---|
| 547 | }
|
---|
| 548 |
|
---|
| 549 | elsif ($filename_encoding eq "auto-fl")
|
---|
| 550 | {
|
---|
| 551 | # filesystem-encoding (locale) then language-analysis (textcat)
|
---|
| 552 | $filename_encoding = $self->locale_encoding();
|
---|
| 553 |
|
---|
| 554 | # try textcat
|
---|
| 555 | $filename_encoding = $self->textcat_encoding($filemeta) if $filename_encoding eq "undefined";
|
---|
| 556 |
|
---|
| 557 | # else assume filename encoding is encoding of file content, if that's available
|
---|
| 558 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 559 | $filename_encoding = $file_encoding;
|
---|
[15868] | 560 | }
|
---|
[16557] | 561 | }
|
---|
[15868] | 562 |
|
---|
[16557] | 563 | elsif ($filename_encoding eq "auto-lf")
|
---|
| 564 | {
|
---|
| 565 | # language-analysis (textcat) then filesystem-encoding (locale)
|
---|
| 566 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
| 567 |
|
---|
| 568 | # guess filename encoding from encoding of file content, if available
|
---|
| 569 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
| 570 | $filename_encoding = $file_encoding;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | # try locale
|
---|
| 574 | $filename_encoding = $self->locale_encoding() if $filename_encoding eq "undefined";
|
---|
[15868] | 575 | }
|
---|
[16557] | 576 |
|
---|
| 577 | # if still undefined, use utf8 as fallback
|
---|
| 578 | if ($filename_encoding eq "undefined") {
|
---|
| 579 | $filename_encoding = "utf8";
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[18171] | 582 | #print STDERR "**** UTF8 encoding the filename $filemeta ";
|
---|
[16767] | 583 |
|
---|
[16557] | 584 | # if the filename encoding is set to utf8 but it isn't utf8 already--such as when
|
---|
| 585 | # 1. the utf8 fallback is used, or 2. if the system locale is used and happens to
|
---|
| 586 | # be always utf8 (in which case the filename's encoding is also set as utf8 even
|
---|
| 587 | # though the filename need not be if it originates from another system)--in such
|
---|
| 588 | # cases attempt to make the filename utf8 to match.
|
---|
| 589 | if($filename_encoding eq "utf8" && !&unicode::check_is_utf8($filemeta)) {
|
---|
| 590 | &unicode::ensure_utf8(\$filemeta);
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | # convert non-unicode encodings to utf8
|
---|
| 594 | if ($filename_encoding !~ m/(?:ascii|utf8|unicode)/) {
|
---|
| 595 | $filemeta = &unicode::unicode2utf8(
|
---|
| 596 | &unicode::convert2unicode($filename_encoding, \$filemeta)
|
---|
[15868] | 597 | );
|
---|
| 598 | }
|
---|
[16390] | 599 |
|
---|
[18171] | 600 | #print STDERR " from encoding $filename_encoding -> $filemeta\n";
|
---|
[16390] | 601 | return $filemeta;
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | # gets the filename with no path, converts to utf8, and then dm safes it.
|
---|
[18320] | 605 | # filename_encoding set by user
|
---|
[16390] | 606 | sub filename_to_utf8_metadata
|
---|
| 607 | {
|
---|
| 608 | my $self = shift (@_);
|
---|
| 609 | my ($file, $file_encoding) = @_;
|
---|
| 610 |
|
---|
| 611 | my $outhandle = $self->{'outhandle'};
|
---|
| 612 |
|
---|
| 613 | my ($filemeta) = $file =~ /([^\\\/]+)$/; # getting the tail of the filepath (skips all string parts containing slashes upto the end)
|
---|
| 614 | $filemeta = $self->filepath_to_utf8($filemeta, $file_encoding);
|
---|
[16767] | 615 |
|
---|
[15868] | 616 | my $dmsafe_filemeta = &ghtml::dmsafe($filemeta);
|
---|
| 617 |
|
---|
| 618 | return $dmsafe_filemeta;
|
---|
| 619 |
|
---|
[10280] | 620 | }
|
---|
| 621 |
|
---|
[16557] | 622 | sub locale_encoding {
|
---|
| 623 | my $self = shift(@_);
|
---|
| 624 |
|
---|
| 625 | if (!defined $self->{'filesystem_encoding'}) {
|
---|
| 626 | $self->{'filesystem_encoding'} = $self->get_filesystem_encoding();
|
---|
| 627 | $self->{'filesystem_encoding'} = "undefined" if !defined $self->{'filesystem_encoding'};
|
---|
| 628 | }
|
---|
[10280] | 629 |
|
---|
[18171] | 630 | #print STDERR "*** filename encoding determined based on locale: " . $self->{'filesystem_encoding'} . "\n";
|
---|
[16557] | 631 | return $self->{'filesystem_encoding'}; # can be the string "undefined"
|
---|
| 632 | }
|
---|
| 633 |
|
---|
| 634 | sub textcat_encoding {
|
---|
| 635 | my $self = shift(@_);
|
---|
| 636 | my ($filemeta) = @_;
|
---|
| 637 |
|
---|
| 638 | # analyse filenames without extensions and digits (and trimmed of surrounding
|
---|
| 639 | # whitespace), so that irrelevant chars don't confuse textcat
|
---|
| 640 | my $strictfilemeta = $filemeta;
|
---|
| 641 | $strictfilemeta =~ s/\.[^\.]+$//g;
|
---|
| 642 | $strictfilemeta =~ s/\d//g;
|
---|
| 643 | $strictfilemeta =~ s/^\s*//g;
|
---|
| 644 | $strictfilemeta =~ s/\s*$//g;
|
---|
| 645 |
|
---|
| 646 | my $filename_encoding = $self->encoding_from_language_analysis($strictfilemeta);
|
---|
| 647 | if(!defined $filename_encoding) {
|
---|
| 648 | $filename_encoding = "undefined";
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | return $filename_encoding; # can be the string "undefined"
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | # performs textcat
|
---|
| 655 | sub encoding_from_language_analysis {
|
---|
| 656 | my $self = shift(@_);
|
---|
| 657 | my ($text) = @_;
|
---|
| 658 |
|
---|
| 659 | my $outhandle = $self->{'outhandle'};
|
---|
| 660 | my $best_encoding = undef;
|
---|
| 661 |
|
---|
[17212] | 662 | # get the language/encoding of the textstring using textcat
|
---|
[16557] | 663 | $self->{'textcat'} = new textcat() unless defined($self->{'textcat'});
|
---|
[16767] | 664 | my $results = $self->{'textcat'}->classify_cached_filename(\$text);
|
---|
[16557] | 665 |
|
---|
| 666 |
|
---|
| 667 | if (scalar @$results < 0) {
|
---|
| 668 | return undef;
|
---|
| 669 | }
|
---|
| 670 |
|
---|
| 671 | # We have some results, we choose the first
|
---|
| 672 | my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
|
---|
| 673 |
|
---|
| 674 | $best_encoding = $encoding;
|
---|
| 675 | if (!defined $best_encoding) {
|
---|
| 676 | return undef;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | if (defined $best_encoding && $best_encoding =~ m/^iso_8859/ && &unicode::check_is_utf8($text)) {
|
---|
| 680 | # the text is valid utf8, so assume that's the real encoding (since textcat is based on probabilities)
|
---|
| 681 | $best_encoding = 'utf8';
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 |
|
---|
| 685 | # check for equivalents where textcat doesn't have some encodings...
|
---|
| 686 | # eg MS versions of standard encodings
|
---|
| 687 | if (defined $best_encoding && $best_encoding =~ /^iso_8859_(\d+)/) {
|
---|
| 688 | my $iso = $1; # which variant of the iso standard?
|
---|
| 689 | # iso-8859 sets don't use chars 0x80-0x9f, windows codepages do
|
---|
| 690 | if ($text =~ /[\x80-\x9f]/) {
|
---|
| 691 | # Western Europe
|
---|
| 692 | if ($iso == 1 or $iso == 15) { $best_encoding = 'windows_1252' }
|
---|
| 693 | elsif ($iso == 2) {$best_encoding = 'windows_1250'} # Central Europe
|
---|
| 694 | elsif ($iso == 5) {$best_encoding = 'windows_1251'} # Cyrillic
|
---|
| 695 | elsif ($iso == 6) {$best_encoding = 'windows_1256'} # Arabic
|
---|
| 696 | elsif ($iso == 7) {$best_encoding = 'windows_1253'} # Greek
|
---|
| 697 | elsif ($iso == 8) {$best_encoding = 'windows_1255'} # Hebrew
|
---|
| 698 | elsif ($iso == 9) {$best_encoding = 'windows_1254'} # Turkish
|
---|
| 699 | }
|
---|
| 700 | }
|
---|
| 701 |
|
---|
| 702 | if (defined $best_encoding && $best_encoding !~ /^(ascii|utf8|unicode)$/ &&
|
---|
| 703 | !defined $encodings::encodings->{$best_encoding})
|
---|
| 704 | {
|
---|
| 705 | if ($self->{'verbosity'}) {
|
---|
| 706 | gsprintf($outhandle, "BasePlugin: {ReadTextFile.unsupported_encoding}\n", $text, $best_encoding, "undef");
|
---|
| 707 | }
|
---|
| 708 | $best_encoding = undef;
|
---|
| 709 | }
|
---|
| 710 |
|
---|
| 711 | return $best_encoding;
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | # uses locale
|
---|
[15868] | 715 | sub get_filesystem_encoding {
|
---|
[10280] | 716 |
|
---|
[15868] | 717 | my $self = shift(@_);
|
---|
| 718 |
|
---|
[14961] | 719 | my $outhandle = $self->{'outhandle'};
|
---|
| 720 | my $filesystem_encoding = undef;
|
---|
| 721 |
|
---|
| 722 | eval {
|
---|
| 723 | use POSIX qw(locale_h);
|
---|
[15868] | 724 |
|
---|
| 725 | # With only one parameter, setlocale retrieves the
|
---|
| 726 | # current value
|
---|
[14961] | 727 | my $current_locale = setlocale(LC_CTYPE);
|
---|
[15868] | 728 |
|
---|
[14961] | 729 | if ($current_locale =~ m/^.*\.(.*?)$/) {
|
---|
| 730 | my $char_encoding = lc($1);
|
---|
[15446] | 731 | if ($char_encoding =~ m/^(iso)(8859)(\d{1,2})$/) {
|
---|
| 732 | $char_encoding = "$1\_$2\_$3";
|
---|
| 733 | }
|
---|
| 734 |
|
---|
[14961] | 735 | $char_encoding =~ s/-/_/g;
|
---|
| 736 | $char_encoding =~ s/^utf_8$/utf8/;
|
---|
[15868] | 737 |
|
---|
[14961] | 738 | if ($char_encoding =~ m/^\d+$/) {
|
---|
[15607] | 739 | if (defined $encodings::encodings->{"windows_$char_encoding"}) {
|
---|
[14961] | 740 | $char_encoding = "windows_$char_encoding";
|
---|
| 741 | }
|
---|
[15607] | 742 | elsif (defined $encodings::encodings->{"dos_$char_encoding"}) {
|
---|
[14961] | 743 | $char_encoding = "dos_$char_encoding";
|
---|
| 744 | }
|
---|
| 745 | }
|
---|
[15868] | 746 |
|
---|
[14961] | 747 | if (($char_encoding =~ m/(?:ascii|utf8|unicode)/)
|
---|
[15446] | 748 | || (defined $encodings::encodings->{$char_encoding})) {
|
---|
[14961] | 749 | $filesystem_encoding = $char_encoding;
|
---|
| 750 | }
|
---|
| 751 | else {
|
---|
| 752 | print $outhandle "Warning: Unsupported character encoding '$char_encoding' from locale '$current_locale'\n";
|
---|
| 753 | }
|
---|
| 754 | }
|
---|
[15868] | 755 |
|
---|
[14961] | 756 |
|
---|
| 757 | };
|
---|
| 758 | if ($@) {
|
---|
| 759 | print $outhandle "$@\n";
|
---|
[18398] | 760 | print $outhandle "Warning: Unable to establish locale. Will assume filesystem is UTF-8\n";
|
---|
[14961] | 761 |
|
---|
| 762 | }
|
---|
[15868] | 763 | return $filesystem_encoding;
|
---|
| 764 | }
|
---|
[14961] | 765 |
|
---|
[15868] | 766 | # is there ever only one Source? Sometimes this will be called twice, for images etc that are converted.
|
---|
| 767 | sub set_Source_metadata {
|
---|
| 768 | my $self = shift (@_);
|
---|
| 769 | my ($doc_obj, $filename_no_path, $file_encoding) = @_;
|
---|
| 770 |
|
---|
| 771 | my $top_section = $doc_obj->get_top_section();
|
---|
[14961] | 772 |
|
---|
[15868] | 773 | # UTF-8 version of filename
|
---|
| 774 | my $filemeta = $self->filename_to_utf8_metadata($filename_no_path, $file_encoding);
|
---|
[18320] | 775 |
|
---|
[16919] | 776 | # Source is the UTF8 display name - not necessarily the name of the file on the system
|
---|
| 777 | $doc_obj->set_utf8_metadata_element($top_section, "Source", $filemeta);
|
---|
| 778 |
|
---|
[18320] | 779 | $filemeta = &util::rename_file($filemeta, $self->{'file_rename_method'});
|
---|
| 780 | # If using URL encoding, then SourceFile is the url-reference to url-encoded
|
---|
| 781 | # filemeta: it's a url that refers to the actual file on the system
|
---|
[18404] | 782 | $filemeta = &unicode::filename_to_url($filemeta);
|
---|
[18320] | 783 |
|
---|
[16919] | 784 | $doc_obj->set_utf8_metadata_element($top_section, "SourceFile", $filemeta);
|
---|
[14961] | 785 | }
|
---|
[17026] | 786 |
|
---|
| 787 | # this should be called by all plugins to set the oid of the doc obj, rather
|
---|
| 788 | # than calling doc_obj->set_OID directly
|
---|
[15868] | 789 | sub add_OID {
|
---|
[15018] | 790 | my $self = shift (@_);
|
---|
| 791 | my ($doc_obj) = @_;
|
---|
| 792 |
|
---|
[17026] | 793 | $doc_obj->set_OIDtype($self->{'OIDtype'}, $self->{'OIDmetadata'});
|
---|
[15018] | 794 |
|
---|
[17026] | 795 | # see if there is a plugin specific set_OID function
|
---|
| 796 | if (defined ($self->can('set_OID'))) {
|
---|
| 797 | $self->set_OID(@_); # pass through doc_obj and any extra arguments
|
---|
[15018] | 798 | }
|
---|
[17026] | 799 | else {
|
---|
[15018] | 800 | # use the default set_OID() in doc.pm
|
---|
| 801 | $doc_obj->set_OID();
|
---|
| 802 | }
|
---|
[17026] | 803 |
|
---|
[15018] | 804 | }
|
---|
[17026] | 805 |
|
---|
[15868] | 806 | # The BasePlugin read_into_doc_obj() function. This function does all the
|
---|
| 807 | # 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] | 808 | # a file and sets up a slew of metadata all saved in doc_obj, which
|
---|
| 809 | # it then returns as part of a tuple (process_status,doc_obj)
|
---|
| 810 | #
|
---|
| 811 | # Much of this functionality used to reside in read, but it was broken
|
---|
| 812 | # down into a supporting routine to make the code more flexible.
|
---|
| 813 | #
|
---|
| 814 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
| 815 | # capable of processing many documents within a single file (e.g.
|
---|
| 816 | # GMLPlug) will normally want to implement their own version of
|
---|
| 817 | # read_into_doc_obj()
|
---|
| 818 | #
|
---|
| 819 | # Note that $base_dir might be "" and that $file might
|
---|
| 820 | # include directories
|
---|
[15868] | 821 |
|
---|
| 822 | # currently blocking has been done before it gets here - does this affect secondary plugin stuff??
|
---|
[10280] | 823 | sub read_into_doc_obj {
|
---|
| 824 | my $self = shift (@_);
|
---|
[16390] | 825 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[10280] | 826 |
|
---|
[15868] | 827 | my $outhandle = $self->{'outhandle'};
|
---|
[10280] | 828 |
|
---|
[15868] | 829 | # should we move this to read? What about secondary plugins?
|
---|
| 830 | print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
|
---|
| 831 | print $outhandle "$self->{'plugin_type'} processing $file\n"
|
---|
| 832 | if $self->{'verbosity'} > 1;
|
---|
[10280] | 833 |
|
---|
[16390] | 834 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
[1242] | 835 | # create a new document
|
---|
[18320] | 836 | my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
|
---|
[14961] | 837 | my $top_section = $doc_obj->get_top_section();
|
---|
| 838 |
|
---|
| 839 | $doc_obj->add_utf8_metadata($top_section, "Plugin", "$self->{'plugin_type'}");
|
---|
[15868] | 840 | $doc_obj->add_utf8_metadata($top_section, "FileSize", (-s $filename_full_path));
|
---|
| 841 |
|
---|
[16997] | 842 | # sets the UTF8 filename (Source) for display and sets the url ref to URL encoded version
|
---|
| 843 | # of the UTF8 filename (SourceFile) for generated files
|
---|
[15877] | 844 | $self->set_Source_metadata($doc_obj, $filename_no_path);
|
---|
[8166] | 845 |
|
---|
[15868] | 846 | # plugin specific stuff - what args do we need here??
|
---|
| 847 | unless (defined ($self->process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
|
---|
| 848 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
| 849 | return -1;
|
---|
[2816] | 850 | }
|
---|
[1242] | 851 |
|
---|
[15868] | 852 | # include any metadata passed in from previous plugins
|
---|
| 853 | # note that this metadata is associated with the top level section
|
---|
| 854 | my $section = $doc_obj->get_top_section();
|
---|
| 855 | # can we merge these two methods??
|
---|
| 856 | $self->add_associated_files($doc_obj, $filename_full_path);
|
---|
| 857 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
| 858 | $self->auto_extract_metadata($doc_obj);
|
---|
[1242] | 859 |
|
---|
[15868] | 860 | # if we haven't found any Title so far, assign one
|
---|
| 861 | # this was shifted to here from inside read()
|
---|
| 862 | $self->title_fallback($doc_obj,$section,$filename_no_path);
|
---|
| 863 |
|
---|
| 864 | $self->add_OID($doc_obj);
|
---|
| 865 |
|
---|
| 866 | return (1,$doc_obj);
|
---|
| 867 | }
|
---|
[2785] | 868 |
|
---|
[15868] | 869 | sub add_dummy_text {
|
---|
| 870 | my $self = shift(@_);
|
---|
| 871 | my ($doc_obj, $section) = @_;
|
---|
[2785] | 872 |
|
---|
[15868] | 873 | # add NoText metadata so we can hide this dummy text in format statements
|
---|
| 874 | $doc_obj->add_metadata($section, "NoText", "1");
|
---|
| 875 | $doc_obj->add_text($section, &gsprintf::lookup_string("{BasePlugin.dummy_text}",1));
|
---|
| 876 |
|
---|
| 877 | }
|
---|
[8510] | 878 |
|
---|
[15868] | 879 | # does nothing. Can be overridden by subclass
|
---|
| 880 | sub auto_extract_metadata {
|
---|
| 881 | my $self = shift(@_);
|
---|
| 882 | my ($doc_obj) = @_;
|
---|
| 883 | }
|
---|
[11122] | 884 |
|
---|
[15868] | 885 | # adds cover image, associate_file options stuff. Should be called by sub class
|
---|
| 886 | # read_into_doc_obj
|
---|
| 887 | sub add_associated_files {
|
---|
| 888 | my $self = shift(@_);
|
---|
| 889 | # whatis filename??
|
---|
| 890 | my ($doc_obj, $filename) = @_;
|
---|
| 891 |
|
---|
| 892 | # add in the cover image
|
---|
| 893 | if ($self->{'cover_image'}) {
|
---|
| 894 | $self->associate_cover_image($doc_obj, $filename);
|
---|
[8716] | 895 | }
|
---|
[9398] | 896 |
|
---|
[15018] | 897 |
|
---|
[10280] | 898 | }
|
---|
[1242] | 899 |
|
---|
[16390] | 900 | # implement this if you are extracting metadata for other documents
|
---|
| 901 | sub metadata_read {
|
---|
| 902 | my $self = shift (@_);
|
---|
[17300] | 903 | my ($pluginfo, $base_dir, $file, $block_hash, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
|
---|
[16390] | 904 |
|
---|
| 905 | # can we process this file??
|
---|
| 906 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
| 907 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
| 908 |
|
---|
| 909 | return 1; # we recognise the file, but don't actually do anything with it
|
---|
| 910 | }
|
---|
| 911 |
|
---|
| 912 |
|
---|
[15868] | 913 | # The BasePlugin read() function. This function calls read_into_doc_obj()
|
---|
[10280] | 914 | # to ensure all the right things to make general options work for a
|
---|
| 915 | # given plugin are done. It then calls the process() function which
|
---|
| 916 | # does all the work specific to a plugin (like the old read functions
|
---|
| 917 | # used to do). Most plugins should define their own process() function
|
---|
| 918 | # and let this read() function keep control.
|
---|
| 919 | #
|
---|
| 920 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
| 921 | # capable of processing many documents within a single file (e.g.
|
---|
| 922 | # GMLPlug) might want to implement their own version of read(), but
|
---|
| 923 | # more likely need to implement their own version of read_into_doc_obj()
|
---|
| 924 | #
|
---|
| 925 | # Return number of files processed, undef if can't recognise, -1 if can't
|
---|
| 926 | # process
|
---|
| 927 |
|
---|
| 928 | sub read {
|
---|
| 929 | my $self = shift (@_);
|
---|
[16390] | 930 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[10280] | 931 |
|
---|
[16390] | 932 | # can we process this file??
|
---|
| 933 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
| 934 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
| 935 |
|
---|
[10280] | 936 | my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
|
---|
[16821] | 937 |
|
---|
[10280] | 938 | if ((defined $process_status) && ($process_status == 1)) {
|
---|
[15868] | 939 |
|
---|
[10280] | 940 | # process the document
|
---|
| 941 | $processor->process($doc_obj);
|
---|
[15868] | 942 |
|
---|
[10280] | 943 | $self->{'num_processed'} ++;
|
---|
| 944 | undef $doc_obj;
|
---|
[9398] | 945 | }
|
---|
[15868] | 946 | # delete any temp files that we may have created
|
---|
| 947 | $self->clean_up_after_doc_obj_processing();
|
---|
[9398] | 948 |
|
---|
[10280] | 949 | # if process_status == 1, then the file has been processed.
|
---|
| 950 | return $process_status;
|
---|
| 951 |
|
---|
[4] | 952 | }
|
---|
| 953 |
|
---|
[1244] | 954 | # returns undef if file is rejected by the plugin
|
---|
[1242] | 955 | sub process {
|
---|
| 956 | my $self = shift (@_);
|
---|
[11089] | 957 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
[1242] | 958 |
|
---|
[15868] | 959 | gsprintf(STDERR, "BasePlugin::process {common.must_be_implemented}\n") && die "\n";
|
---|
[1244] | 960 |
|
---|
| 961 | return undef; # never gets here
|
---|
[1242] | 962 | }
|
---|
| 963 |
|
---|
[15868] | 964 | # overwrite this method to delete any temp files that we have created
|
---|
| 965 | sub clean_up_after_doc_obj_processing {
|
---|
| 966 | my $self = shift(@_);
|
---|
[4] | 967 |
|
---|
[10280] | 968 | }
|
---|
[16390] | 969 |
|
---|
[10280] | 970 | # write_file -- used by ConvertToPlug, for example in post processing
|
---|
| 971 | #
|
---|
[15868] | 972 | # where should this go, is here the best place??
|
---|
[10280] | 973 | sub utf8_write_file {
|
---|
| 974 | my $self = shift (@_);
|
---|
| 975 | my ($textref, $filename) = @_;
|
---|
| 976 |
|
---|
| 977 | if (!open (FILE, ">$filename")) {
|
---|
| 978 | gsprintf(STDERR, "ConvertToPlug::write_file {ConvertToPlug.could_not_open_for_writing} ($!)\n", $filename);
|
---|
| 979 | die "\n";
|
---|
| 980 | }
|
---|
| 981 | print FILE $$textref;
|
---|
| 982 |
|
---|
[1219] | 983 | close FILE;
|
---|
| 984 | }
|
---|
| 985 |
|
---|
[10280] | 986 |
|
---|
[7504] | 987 | sub filename_based_title
|
---|
| 988 | {
|
---|
| 989 | my $self = shift (@_);
|
---|
| 990 | my ($file) = @_;
|
---|
| 991 |
|
---|
| 992 | my $file_derived_title = $file;
|
---|
| 993 | $file_derived_title =~ s/_/ /g;
|
---|
| 994 | $file_derived_title =~ s/\..*?$//;
|
---|
| 995 |
|
---|
| 996 | return $file_derived_title;
|
---|
| 997 | }
|
---|
| 998 |
|
---|
[9398] | 999 |
|
---|
[7504] | 1000 | sub title_fallback
|
---|
| 1001 | {
|
---|
| 1002 | my $self = shift (@_);
|
---|
| 1003 | my ($doc_obj,$section,$file) = @_;
|
---|
| 1004 |
|
---|
[15868] | 1005 | if (!defined $doc_obj->get_metadata_element ($section, "Title") or $doc_obj->get_metadata_element($section, "Title") eq "") {
|
---|
[7504] | 1006 |
|
---|
[15877] | 1007 | my $file_derived_title = $self->filename_to_utf8_metadata($self->filename_based_title($file));
|
---|
[15868] | 1008 | if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
|
---|
| 1009 | $doc_obj->add_utf8_metadata ($section, "Title", $file_derived_title);
|
---|
[9413] | 1010 | }
|
---|
[15868] | 1011 | else {
|
---|
[16995] | 1012 | $doc_obj->set_utf8_metadata_element ($section, "Title", $file_derived_title);
|
---|
[15868] | 1013 | }
|
---|
[9413] | 1014 | }
|
---|
[15868] | 1015 |
|
---|
[1844] | 1016 | }
|
---|
[15868] | 1017 |
|
---|
[1219] | 1018 | # add any extra metadata that's been passed around from one
|
---|
| 1019 | # plugin to another.
|
---|
| 1020 | # extra_metadata uses add_utf8_metadata so it expects metadata values
|
---|
| 1021 | # to already be in utf8
|
---|
| 1022 | sub extra_metadata {
|
---|
| 1023 | my $self = shift (@_);
|
---|
| 1024 | my ($doc_obj, $cursection, $metadata) = @_;
|
---|
| 1025 |
|
---|
[11122] | 1026 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
| 1027 |
|
---|
[1219] | 1028 | foreach my $field (keys(%$metadata)) {
|
---|
[839] | 1029 | # $metadata->{$field} may be an array reference
|
---|
[8510] | 1030 | if ($field eq "gsdlassocfile_tobe") {
|
---|
| 1031 | # 'gsdlassocfile_tobe' is artificially introduced metadata
|
---|
| 1032 | # that is used to signal that certain additional files should
|
---|
| 1033 | # be tied to this document. Useful in situations where a
|
---|
| 1034 | # metadata pass in the plugin pipeline works out some files
|
---|
| 1035 | # need to be associated with a document, but the document hasn't
|
---|
| 1036 | # been formed yet.
|
---|
| 1037 | my $equiv_form = "";
|
---|
| 1038 | foreach my $gaf (@{$metadata->{$field}}) {
|
---|
| 1039 | my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
|
---|
[18171] | 1040 | my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
|
---|
| 1041 |
|
---|
| 1042 | # we need to make sure the filename is valid utf-8 - we do
|
---|
[18320] | 1043 | # this by url or base64 encoding it
|
---|
[18171] | 1044 | # $tail_filename is the name that we store the file as
|
---|
[18320] | 1045 | $tail_filename = &util::rename_file($tail_filename, $self->{'file_rename_method'});
|
---|
[8510] | 1046 | $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
|
---|
[18320] | 1047 | # If the filename is url_encoded, we need to encode the % signs
|
---|
| 1048 | # in the filename, so that it works in a url
|
---|
[18404] | 1049 | my $url_tail_filename = &unicode::filename_to_url($tail_filename);
|
---|
[11122] | 1050 | # work out extended tail extension (i.e. matching tail re)
|
---|
| 1051 |
|
---|
| 1052 | my ($file_prefix,$file_extended_ext)
|
---|
[16390] | 1053 | = &util::get_prefix_and_tail_by_regex($tail_filename,$associate_tail_re);
|
---|
[11122] | 1054 | my ($pre_doc_ext) = ($file_extended_ext =~ m/^(.*)\..*$/);
|
---|
[8510] | 1055 | my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
|
---|
[18171] | 1056 | my $start_doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/{Or}{[parent(Top):archivedir],[archivedir]}/$url_tail_filename\">";
|
---|
[8510] | 1057 | my $srcicon = "_icon".$doc_ext."_";
|
---|
| 1058 | my $end_doclink = "</a>";
|
---|
| 1059 |
|
---|
[11122] | 1060 | my $assoc_form = "$start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
|
---|
| 1061 |
|
---|
[18171] | 1062 | if (defined $pre_doc_ext && $pre_doc_ext ne "") {
|
---|
[11122] | 1063 | # for metadata such as [mp3._edited] [mp3._full] ...
|
---|
| 1064 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.$pre_doc_ext", $assoc_form);
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | # for multiple metadata such as [mp3.assoclink]
|
---|
| 1068 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.assoclink", $assoc_form);
|
---|
| 1069 |
|
---|
| 1070 | $equiv_form .= " $assoc_form";
|
---|
[8510] | 1071 | }
|
---|
| 1072 | $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
|
---|
| 1073 | }
|
---|
| 1074 | elsif (ref ($metadata->{$field}) eq "ARRAY") {
|
---|
[839] | 1075 | map {
|
---|
[1219] | 1076 | $doc_obj->add_utf8_metadata ($cursection, $field, $_);
|
---|
[839] | 1077 | } @{$metadata->{$field}};
|
---|
| 1078 | } else {
|
---|
[1219] | 1079 | $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
|
---|
[839] | 1080 | }
|
---|
| 1081 | }
|
---|
| 1082 | }
|
---|
| 1083 |
|
---|
[1396] | 1084 |
|
---|
[2785] | 1085 | sub compile_stats {
|
---|
| 1086 | my $self = shift(@_);
|
---|
| 1087 | my ($stats) = @_;
|
---|
| 1088 |
|
---|
| 1089 | $stats->{'num_processed'} += $self->{'num_processed'};
|
---|
| 1090 | $stats->{'num_not_processed'} += $self->{'num_not_processed'};
|
---|
[2796] | 1091 | $stats->{'num_archives'} += $self->{'num_archives'};
|
---|
[2785] | 1092 |
|
---|
| 1093 | }
|
---|
| 1094 |
|
---|
[2816] | 1095 | sub associate_cover_image {
|
---|
[10833] | 1096 | my $self = shift;
|
---|
[2816] | 1097 | my ($doc_obj, $filename) = @_;
|
---|
| 1098 |
|
---|
[10833] | 1099 | $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
| 1100 | if (exists $self->{'covers_missing_cache'}->{$filename}) {
|
---|
| 1101 | # don't stat() for existence eg for multiple document input files
|
---|
| 1102 | # (eg SplitPlug)
|
---|
| 1103 | return;
|
---|
| 1104 | }
|
---|
| 1105 |
|
---|
[9413] | 1106 | my $top_section=$doc_obj->get_top_section();
|
---|
| 1107 |
|
---|
[2816] | 1108 | if (-e $filename) {
|
---|
[13968] | 1109 | $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
|
---|
[9413] | 1110 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
[3086] | 1111 | } else {
|
---|
[10833] | 1112 | my $upper_filename = $filename;
|
---|
| 1113 | $upper_filename =~ s/jpg$/JPG/;
|
---|
| 1114 | if (-e $upper_filename) {
|
---|
| 1115 | $doc_obj->associate_file($upper_filename, "cover.jpg",
|
---|
| 1116 | "image/jpeg");
|
---|
[9413] | 1117 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
[10833] | 1118 | } else {
|
---|
| 1119 | # file doesn't exist, so record the fact that it's missing so
|
---|
| 1120 | # we don't stat() again (stat is slow)
|
---|
| 1121 | $self->{'covers_missing_cache'}->{$filename} = 1;
|
---|
[3086] | 1122 | }
|
---|
[2816] | 1123 | }
|
---|
[10833] | 1124 |
|
---|
[2816] | 1125 | }
|
---|
| 1126 |
|
---|
[11332] | 1127 |
|
---|
| 1128 | # Overridden by exploding plugins (eg. ISISPlug)
|
---|
| 1129 | sub clean_up_after_exploding
|
---|
| 1130 | {
|
---|
| 1131 | my $self = shift(@_);
|
---|
| 1132 | }
|
---|
| 1133 |
|
---|
| 1134 |
|
---|
[16390] | 1135 |
|
---|
[4] | 1136 | 1;
|
---|