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