source: main/trunk/greenstone2/perllib/plugins/BasePlugin.pm@ 26536

Last change on this file since 26536 was 26536, checked in by davidb, 11 years ago

Introduction of two new OIDtype values (hash_on_full_filename and full_filename) designed to help provide more stable document IDs for collections that are rebuilt over time, including rebuilt after the Greenstone install has been upgraded

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