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