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