source: main/trunk/greenstone2/perllib/plugins/BaseImporter.pm

Last change on this file was 38749, checked in by davidb, 3 months ago

Code introduced to set SourceDirectory as a piece of metadata for all plugins. Done in read_into_doc_obj(), and so with the inheritance we have across plugins, this needed in be added into 4-5 of our existing plugins. In doing so, it was noticed that not all of them called the post_process_doc_obj() and/or the newer apply_metadata_mapping() subroutine. These were fixed up as part of this coding change, along with improved consistency of declaring the top_section local variable

  • Property svn:keywords set to Author Date Id Revision
File size: 44.1 KB
Line 
1###########################################################################
2#
3# BaseImporter.pm -- base class for all the import plugins
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#
8# Copyright (C) 1999-2005 New Zealand Digital Library Project
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###########################################################################
25
26package BaseImporter;
27
28use strict;
29no strict 'subs';
30no strict 'refs'; # allow filehandles to be variables and viceversa
31
32use File::Basename;
33use Encode;
34use Unicode::Normalize 'normalize';
35
36use encodings;
37use unicode;
38use doc;
39use ghtml;
40use gsprintf 'gsprintf';
41use util;
42use FileUtils;
43
44use CommonUtil;
45
46BEGIN {
47 @BaseImporter::ISA = ( 'CommonUtil' );
48}
49
50# the different methods that can be applied when renaming
51# imported documents and their associated files
52our $file_rename_method_list =
53 [ { 'name' => "url",
54 'desc' => "{BaseImporter.rename_method.url}" },
55 { 'name' => "base64",
56 'desc' => "{BaseImporter.rename_method.base64}" },
57 { 'name' => "none",
58 'desc' => "{BaseImporter.rename_method.none}",
59 'hiddengli' => "yes" } ];
60
61# here went encoding list stuff
62
63our $oidtype_list =
64 [ { 'name' => "auto",
65 'desc' => "{BaseImporter.OIDtype.auto}" },
66 { 'name' => "hash",
67 'desc' => "{import.OIDtype.hash}" },
68 { 'name' => "hash_on_ga_xml",
69 'desc' => "{import.OIDtype.hash_on_ga_xml}" },
70 { 'name' => "hash_on_full_filename",
71 'desc' => "{import.OIDtype.hash_on_full_filename}" },
72 { 'name' => "assigned",
73 'desc' => "{import.OIDtype.assigned}" },
74 { 'name' => "incremental",
75 'desc' => "{import.OIDtype.incremental}" },
76 { 'name' => "filename",
77 'desc' => "{import.OIDtype.filename}" },
78 { 'name' => "dirname",
79 'desc' => "{import.OIDtype.dirname}" },
80 { 'name' => "full_filename",
81 'desc' => "{import.OIDtype.full_filename}" } ];
82
83my $arguments =
84 [ { 'name' => "process_exp",
85 'desc' => "{BaseImporter.process_exp}",
86 'type' => "regexp",
87 'deft' => "",
88 'reqd' => "no" },
89 { 'name' => "store_original_file",
90 'desc' => "{BaseImporter.store_original_file}",
91 'type' => "flag",
92 'reqd' => "no" },
93 { 'name' => "associate_ext",
94 'desc' => "{BaseImporter.associate_ext}",
95 'type' => "string",
96 'reqd' => "no" },
97 { 'name' => "associate_tail_re",
98 'desc' => "{BaseImporter.associate_tail_re}",
99 'type' => "string",
100 'reqd' => "no" },
101 { 'name' => "metadata_mapping_file",
102 'desc' => "{BaseImporter.metadata_mapping_file}",
103 'type' => "string",
104 #'deft' => "metadata_mapping_rules.csv", # commenting out, explicitly giving this as the default means every plugin automatically activates it
105 'deft' => "",
106 'reqd' => "no" },
107 { 'name' => "OIDtype",
108 'desc' => "{import.OIDtype}",
109 'type' => "enum",
110 'list' => $oidtype_list,
111 # leave default empty so we can tell if its been set or not - if not set will use option from import.pl
112 'deft' => "auto",
113 'reqd' => "no" },
114 { 'name' => "OIDmetadata",
115 'desc' => "{import.OIDmetadata}",
116 'type' => "metadata",
117 'deft' => "dc.Identifier",
118 'reqd' => "no" },
119# { 'name' => "use_as_doc_identifier",
120# 'desc' => "{BaseImporter.use_as_doc_identifier}",
121# 'type' => "string",
122# 'reqd' => "no" ,
123# 'deft' => "" } ,
124 { 'name' => "no_cover_image",
125 'desc' => "{BaseImporter.no_cover_image}",
126 'type' => "flag",
127 'reqd' => "no" },
128 { 'name' => "file_rename_method",
129 'desc' => "{BaseImporter.file_rename_method}",
130 'type' => "enum",
131 'deft' => &get_default_file_rename_method(), # by default rename imported files and assoc files using this encoding
132 'list' => $file_rename_method_list,
133 'reqd' => "no"
134 }
135
136 # These options take a metadata field, and store its values as individual
137 #fields with true value.
138 #eg wmtb.TKLabel=tk.Clan,tk.Attribution =>tk.Clan=true, tk.Attribution=true.
139 #Implemented for tk lables, but then not needed as this is handled by
140 # xslt instead. Leaving this here in case useful in the future
141# { 'name' => "store_metadata_values_as_fields_for",
142# 'desc' => "For these (comma-separated list of) fields, take all values and store them as metadata fields in their own write, value=true ",
143# 'type' => "metadata",
144# 'reqd' => "no" },
145# { 'name' => "namespace_for_new_fields",
146# 'desc' => "add the specified namespace to the new fields (comma separated list, corresponding to the fields above",
147# 'type'=> "string",
148# 'reqd' => "no",
149# 'deft' => ""},
150# { 'name' => "include_section_level",
151# 'desc' => "do we look for this metadata at subsection level too?",
152# 'type' => "string",
153# 'deft' => "false"}
154 ];
155
156
157my $options = { 'name' => "BaseImporter",
158 'desc' => "{BaseImporter.desc}",
159 'abstract' => "yes",
160 'inherits' => "yes",
161 'args' => $arguments };
162
163sub setup_metadata_mapping_rules
164{
165 my $self = shift (@_);
166 my ($metadata_mapping_full_filename) = @_;
167
168 my $csv_file = &util::read_utf8_textfile($metadata_mapping_full_filename);
169
170 my @csv_lines = split('\n',$csv_file);
171
172 my $line_num = 0;
173
174 my $metadata_mapping_rules = [];
175
176 foreach my $csv_line (@csv_lines) {
177 chomp($csv_line);
178
179 $line_num++;
180
181 next if ($csv_line =~ m/^\s*$/);
182 next if ($csv_line =~ m/^#/);
183
184 # my @tsv_entries = split('\t',$tsv_line);
185 my @csv_entries = split(/\s*,\s*/,$csv_line);
186
187 my $num_csv_entries = scalar(@csv_entries);
188
189 # print STDERR join(",",@tsv_entries), "\n\n";
190
191 if (($num_csv_entries == 5) || ($num_csv_entries >= 6 && $csv_entries[4] =~ m/\s*#.*$/)) {
192
193 my $src_metadata_name = $csv_entries[0];
194 my ($regex_type,$src_metadata_regex) = ($csv_entries[1] =~ m/^(s|m)\/(.*)\/$/); # use matching to extract regex_type ('s' or 'm') and src_metadata_regex
195 (my $dst_metadata_regex = $csv_entries[2]) =~ s/(^\/)|(\/$)//g; # strips off leading and trailing slash
196 (my $regex_modifiers = $csv_entries[3]) =~ s/(^')|('$)//g; # similar to above but strips out single quotes
197 my $dst_metadata_name = $csv_entries[4];
198
199 if ((defined $regex_type) && (defined $src_metadata_regex)) {
200 # If the last entry has a '# comments' after it, needs a bit more string manipulation
201 # to tidy it up
202 $dst_metadata_name =~ s/#.*$//;
203 $dst_metadata_name =~ s/\s*$//;
204
205 my $regex_sub = "s/$src_metadata_regex/$dst_metadata_regex/$regex_modifiers";
206
207 my $metadata_mapping_rule_rec = {
208 "regex_type" => $regex_type,
209 "src_metadata_name" => $src_metadata_name,
210 "regex_sub" => $regex_sub,
211 "dst_metadata_name" => $dst_metadata_name
212 };
213
214 if ($regex_type eq "m") {
215 my $regex_match = "m/$src_metadata_regex/";
216 $metadata_mapping_rule_rec->{"regex_match"} = $regex_match;
217 }
218
219 push(@$metadata_mapping_rules,$metadata_mapping_rule_rec);
220 }
221 else {
222 print STDERR "Warning: syntax error in $metadata_mapping_full_filename, line $line_num\n";
223 print STDERR " $csv_line\n";
224 print STDERR "Did not specify source metadata regular expression entry inside slashes, e.g., s/(-|_)/\n";
225 if (!defined $regex_type) {
226 print STDERR "Prefix to source metadata regular expression entry must be either s/ or m/ for substitute or match respectively\n";
227 }
228 }
229 }
230 else {
231 print STDERR "Warning: syntax error in $metadata_mapping_full_filename, line $line_num\n";
232 print STDERR " $csv_line\n";
233 print STDERR "Did not contain 5 tab-delimited entries\n";
234 }
235
236 }
237
238 if (scalar(@$metadata_mapping_rules)>0) {
239 $self->{'metadata_mapping_rules'} = $metadata_mapping_rules;
240 }
241}
242
243sub new {
244
245 my ($class) = shift (@_);
246 my ($pluginlist,$inputargs,$hashArgOptLists,$auxiliary) = @_;
247 push(@$pluginlist, $class);
248
249 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
250 push(@{$hashArgOptLists->{"OptList"}},$options);
251
252 my $self = new CommonUtil($pluginlist, $inputargs, $hashArgOptLists,$auxiliary);
253
254 if ($self->{'info_only'}) {
255 # don't worry about any options etc
256 return bless $self, $class;
257 }
258
259 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
260 $self->{'plugin_type'} = $plugin_name;
261
262 # remove ex. from OIDmetadata iff it's the only namespace prefix
263 $self->{'OIDmetadata'} =~ s/^ex\.([^.]+)$/$1/ if defined $self->{'OIDmetadata'};
264 $self->{'num_processed'} = 0;
265 $self->{'num_not_processed'} = 0;
266 $self->{'num_blocked'} = 0;
267 $self->{'num_archives'} = 0;
268 $self->{'cover_image'} = 1; # cover image is on by default
269 $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
270 $self->{'can_process_directories'} = 0;
271 #$self->{'option_list'} = $hashArgOptLists->{"OptList"};
272
273 my $associate_ext = $self->{'associate_ext'};
274 if ((defined $associate_ext) && ($associate_ext ne "")) {
275
276 my $associate_tail_re = $self->{'associate_tail_re'};
277 if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
278 my $outhandle = $self->{'outhandle'};
279 print $outhandle "Warning: can only specify 'associate_ext' or 'associate_tail_re'\n";
280 print $outhandle " defaulting to 'associate_tail_re'\n";
281 }
282 else {
283 my @exts = split(/,/,$associate_ext);
284
285 my @exts_bracketed = map { $_ = "(?:\\.$_)" } @exts;
286 my $associate_tail_re = join("|",@exts_bracketed);
287 $self->{'associate_tail_re'} = $associate_tail_re;
288 }
289
290 delete $self->{'associate_ext'};
291 }
292
293 my $blessed_self = bless $self, $class;
294
295 if ($blessed_self->{'metadata_mapping_file'}) {
296 my $metadata_mapping_file = $blessed_self->{'metadata_mapping_file'};
297 my $mmf_full_filename;
298 $mmf_full_filename = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},"etc", $metadata_mapping_file);
299 if (!-e $mmf_full_filename) {
300 my $mmf_full_col_filename = $mmf_full_filename;
301 $mmf_full_filename = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"etc", $metadata_mapping_file);
302
303 if (!-e $mmf_full_filename) {
304 my $outhandle = $blessed_self->{'outhandle'};
305 print STDERR "\nBaseImporter Error: Can't locate metadata_mapping_file '$metadata_mapping_file'\n";
306 print STDERR "This file should be in $mmf_full_col_filename or $mmf_full_filename\n";
307 $blessed_self->print_txt_usage(""); # Use default resource bundle
308 print STDERR "\nBaseImporter Error: Can't locate metadata_mapping_file '$metadata_mapping_file'\n";
309 print STDERR "This file should be in $mmf_full_col_filename or $mmf_full_filename\n";
310 die "\n";
311 }
312 }
313
314 $blessed_self->{'metadata_mapping_full_filename'} = $mmf_full_filename;
315 $blessed_self->setup_metadata_mapping_rules($mmf_full_filename);
316 }
317
318
319 return $blessed_self;
320
321}
322
323sub merge_inheritance
324{
325 my $self = {};
326 my @child_selfs = @_;
327
328 foreach my $child_self (@child_selfs) {
329 foreach my $key (keys %$child_self) {
330 if (defined $self->{$key}) {
331 if ($self->{$key} ne $child_self->{$key}) {
332# print STDERR "Warning: Conflicting value in multiple inheritance for '$key'\n";
333# print STDERR "Existing stored value = $self->{$key}\n";
334# print STDERR "New (child) value = $child_self->{$key}\n";
335# print STDERR "Keeping existing value\n";
336 # Existing value seems to be option specified in collect.cfg
337
338 ### $self->{$key} = $child_self->{$key};
339
340 }
341 else {
342## print STDERR "****Info: Value $self->{$key} for $key already defined through multiple inheritance as the same value\n";
343 }
344
345 }
346 else {
347 $self->{$key} = $child_self->{$key};
348 }
349 }
350 }
351
352 return $self;
353}
354
355# initialize BaseImporter options
356# if init() is overridden in a sub-class, remember to call BaseImporter::init()
357sub init {
358 my $self = shift (@_);
359 my ($verbosity, $outhandle, $failhandle) = @_;
360
361 $self->SUPER::init(@_);
362
363 # set process_exp and block_exp to defaults unless they were
364 # explicitly set
365
366 if ((!$self->is_recursive()) and
367 (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
368
369 $self->{'process_exp'} = $self->get_default_process_exp ();
370 if ($self->{'process_exp'} eq "") {
371 warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
372 }
373 }
374
375 if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
376 $self->{'block_exp'} = $self->get_default_block_exp ();
377 }
378
379}
380
381sub begin {
382 my $self = shift (@_);
383 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
384
385 if ($self->{'OIDtype'} eq "auto") {
386 # hasn't been set in the plugin, use the processor values
387 $self->{'OIDtype'} = $processor->{'OIDtype'};
388 $self->{'OIDmetadata'} = $processor->{'OIDmetadata'};
389 }
390 if ($self->{'OIDtype'} eq "hash") {
391 # should we hash on the file or on the doc xml??
392 $self->{'OIDtype'} = $self->get_oid_hash_type();
393 if ($self->{'OIDtype'} !~ /^(hash_on_file|hash_on_ga_xml)$/) {
394 $self->{'OIDtype'} = "hash_on_file";
395 }
396 }
397}
398
399# This is called once if removeold is set with import.pl. Most plugins will do
400# nothing but if a plugin does any stuff outside of creating doc obj, then
401# it may need to clear something.
402sub remove_all {
403 my $self = shift (@_);
404 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
405}
406
407# This is called per document for docs that have been deleted from the
408# collection. Most plugins will do nothing
409# but if a plugin does any stuff outside of creating doc obj, then it may need
410# to clear something.
411sub remove_one {
412 my $self = shift (@_);
413
414 my ($file, $oids, $archivedir) = @_;
415 return 0 if $self->can_process_this_file($file);
416 return undef;
417}
418
419sub end {
420 # potentially called at the end of each plugin pass
421 # import.pl only has one plugin pass, but buildcol.pl has multiple ones
422
423 my ($self) = shift (@_);
424}
425
426sub deinit {
427 # called only once, after all plugin passes have been done
428
429 my ($self) = @_;
430}
431
432# default hashing type is to hash on the original file (or converted file)
433# override this to return hash_on_ga_xml for filetypes where hashing on the
434# file is no good eg video
435sub get_oid_hash_type {
436
437 my $self = shift (@_);
438
439 return "hash_on_file";
440}
441
442
443# this function should be overridden to return 1
444# in recursive plugins
445sub is_recursive {
446 my $self = shift (@_);
447
448 return 0;
449}
450
451sub get_default_block_exp {
452 my $self = shift (@_);
453
454 return "";
455}
456
457sub get_default_process_exp {
458 my $self = shift (@_);
459
460 return "";
461}
462
463
464# rename imported files and assoc files using URL encoding by default
465# as this will work for most plugins and give more legible filenames
466sub get_default_file_rename_method() {
467 my $self = shift (@_);
468 return "url";
469}
470
471# returns this plugin's active (possibly user-selected) file_rename_method
472sub get_file_rename_method() {
473 my $self = shift (@_);
474 my $rename_method = $self->{'file_rename_method'};
475 if($rename_method) {
476 return $rename_method;
477 } else {
478 return $self->get_default_file_rename_method();
479 }
480}
481
482# default implementation is to do nothing
483sub store_block_files {
484
485 my $self =shift (@_);
486 my ($filename_full_path, $block_hash) = @_;
487
488}
489
490# put files to block into hash
491sub use_block_expressions {
492
493 my $self =shift (@_);
494 my ($filename_full_path, $block_hash) = @_;
495
496 $filename_full_path = &util::upgrade_if_dos_filename($filename_full_path);
497
498 if ($self->{'block_exp'} ne "" && $filename_full_path =~ /$self->{'block_exp'}/) {
499 $self->block_filename($block_hash,$filename_full_path);
500 }
501
502}
503
504#default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
505sub block_cover_image
506{
507 my $self =shift;
508 my ($filename, $block_hash) = @_;
509
510 $filename = &util::upgrade_if_dos_filename($filename);
511
512 if ($self->{'cover_image'}) {
513 my $coverfile = $filename;
514 $coverfile =~ s/\.[^\\\/\.]+$/\.jpg/;
515
516 #if there is no file extension, coverfile will be the same as filename
517 return if $coverfile eq $filename;
518
519 if (!&FileUtils::fileExists($coverfile)) {
520 $coverfile =~ s/jpg$/JPG/;
521 }
522 if (&FileUtils::fileExists($coverfile)) {
523 $self->block_filename($block_hash,$coverfile);
524 }
525 }
526
527 return;
528}
529
530
531# discover all the files that should be blocked by this plugin
532# check the args ...
533sub file_block_read {
534
535 my $self = shift (@_);
536 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
537 # Keep track of filenames with same root but different extensions
538 # Used to support -associate_ext and the more generalised
539 # -associate_tail_re
540 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
541
542 if (!-d $filename_full_path) {
543 $block_hash->{'all_files'}->{$file} = 1;
544 }
545
546 my $associate_tail_re = $self->{'associate_tail_re'};
547 if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
548 my ($file_prefix,$file_ext)
549 = &util::get_prefix_and_tail_by_regex($filename_full_path,$associate_tail_re);
550 if ((defined $file_prefix) && (defined $file_ext)) {
551 my $shared_fileroot = $block_hash->{'shared_fileroot'};
552 if (!defined $shared_fileroot->{$file_prefix}) {
553 my $file_prefix_rec = { 'tie_to' => undef,
554 'exts' => {} };
555 $shared_fileroot->{$file_prefix} = $file_prefix_rec;
556 }
557
558 my $file_prefix_rec = $shared_fileroot->{$file_prefix};
559
560 if ($self->can_process_this_file($filename_full_path) && $file_ext !~ m/.\./) {
561 # This is the document the others should be tied to
562 $file_prefix_rec->{'tie_to'} = $file_ext;
563 }
564 else {
565 if ($file_ext =~ m/$associate_tail_re$/) {
566 # this file should be associated to the main one
567 $file_prefix_rec->{'exts'}->{$file_ext} = 1;
568 }
569 }
570
571 }
572 }
573
574 # check block expressions
575 $self->use_block_expressions($filename_full_path, $block_hash) unless $self->{'no_blocking'};
576
577 # now check whether we are actually processing this
578 if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
579 return undef; # can't recognise
580 }
581
582 # if we have a block_exp, then this overrides the normal 'smart' blocking
583 $self->store_block_files($filename_full_path, $block_hash) unless ($self->{'no_blocking'} || $self->{'block_exp'} ne "");
584
585 # block the cover image if there is one
586 if ($self->{'cover_image'}) {
587 $self->block_cover_image($filename_full_path, $block_hash);
588 }
589
590 return 1;
591}
592
593# plugins that rely on more than process_exp (eg XML plugins) can override this method
594sub can_process_this_file {
595 my $self = shift(@_);
596 my ($filename) = @_;
597
598 if (-d $filename && !$self->{'can_process_directories'}) {
599 return 0;
600 }
601
602 # print STDERR "**** BaseImport::can_process_this_file(): ", ref($self), " checking $filename =~ /$self->{'process_exp'}/\n";
603
604 if ($self->{'process_exp'} ne "" && $filename =~ /$self->{'process_exp'}/) {
605 return 1;
606 }
607 return 0;
608
609}
610
611# Even if a plugin can extract metadata in its metadata_read pass,
612# make the default return 'undef' so processing of the file continues
613# down the pipeline, so other plugins can also have the opportunity to
614# locate metadata and set it up in the extrametakeys variables that
615# are passed around.
616
617sub can_process_this_file_for_metadata {
618 my $self = shift(@_);
619
620 return undef;
621}
622
623
624
625# Notionally written to be called once for each document, it is however safe to
626# call multiple times (as in the case of ImagePlugin) which calls this later on
627# after the original image has potentially been converted to a *new* source image
628# format (e.g. TIFF to PNG)
629
630sub set_Source_metadata {
631 my $self = shift (@_);
632 my ($doc_obj, $raw_filename, $filename_encoding, $section) = @_;
633
634 # 1. Sets the filename (Source) for display encoded as Unicode if possible,
635 # and (as a fallback) using %xx if not for non-ascii chars
636 # 2. Sets the url ref (SourceFile) to the URL encoded version
637 # of filename for generated files
638
639 my ($unused_full_rf, $raw_file) = &util::get_full_filenames("", $raw_filename);
640
641 my $this_section = (defined $section)? $section : $doc_obj->get_top_section();
642
643 my $octet_file = $raw_file;
644
645 # UTF-8 version of filename
646# if ((defined $ENV{"DEBUG_UNICODE"}) && ($ENV{"DEBUG_UNICODE"})) {
647# print STDERR "**** Setting Source Metadata given: $octet_file\n";
648# }
649
650 # Deal with (on Windows) raw filenames that are in their
651 # abbreviated DOS form
652
653 if (($ENV{'GSDLOS'} =~ m/^windows$/i) && ($^O ne "cygwin")) {
654 if ((defined $filename_encoding) && ($filename_encoding eq "unicode")) {
655 if (-e $raw_filename) {
656 my $unicode_filename = Win32::GetLongPathName($raw_filename);
657
658 my $unused_full_uf;
659 ($unused_full_uf, $octet_file) = &util::get_full_filenames("", $unicode_filename);
660 }
661 }
662 }
663
664 my $url_encoded_filename;
665 if ((defined $filename_encoding) && ($filename_encoding ne "ascii")) {
666 # => Generate a pretty print version of filename that is mapped to Unicode
667
668 # Use filename_encoding to map raw filename to a Perl unicode-aware string
669 $url_encoded_filename = decode($filename_encoding,$octet_file);
670 }
671 else {
672 # otherwise generate %xx encoded version of filename for char > 127
673 $url_encoded_filename = &unicode::raw_filename_to_url_encoded($octet_file);
674 }
675
676# if ((defined $ENV{"DEBUG_UNICODE"}) && ($ENV{"DEBUG_UNICODE"})) {
677# print STDERR "****** saving Source as: $url_encoded_filename\n";
678# }
679
680 # In the case of converted files and (generalized) exploded documents, there
681 # will already be a source filename => store as OrigSource before overriding
682 my $orig_source = $doc_obj->get_metadata_element ($this_section, "Source");
683 if ((defined $orig_source) && ($orig_source !~ m/^\s*$/)) {
684 $doc_obj->set_utf8_metadata_element($this_section, "OrigSource", $orig_source);
685 }
686
687 # Source is the UTF8 display name - not necessarily the name of the file on the system
688 if ($ENV{'GSDLOS'} =~ m/^darwin$/i) {
689 # on Darwin want all display strings to be in composed form, then can search on that
690 $url_encoded_filename = normalize('C', $url_encoded_filename); # Normalisation Form 'C' (composition)
691 }
692 # set_utf8_metadata actually sets perl unicode aware strings. not utf8
693 $doc_obj->set_utf8_metadata_element($this_section, "Source", $url_encoded_filename);
694
695
696 my $renamed_raw_file = &util::rename_file($raw_file, $self->{'file_rename_method'});
697 # If using URL encoding, then SourceFile is the url-reference to url-encoded
698 # renamed_raw_url: it's a url that refers to the actual file on the system
699 # this call just replaces % with %25
700 my $renamed_raw_url = &unicode::filename_to_url($renamed_raw_file);
701
702 $doc_obj->set_utf8_metadata_element($this_section, "SourceFile",
703 $renamed_raw_url);
704
705# if ((defined $ENV{"DEBUG_UNICODE"}) && ($ENV{"DEBUG_UNICODE"})) {
706# print STDERR "****** saving SourceFile as: $renamed_raw_url\n";
707# }
708}
709
710
711# this should be called by all plugins to set the oid of the doc obj, rather
712# than calling doc_obj->set_OID directly
713sub add_OID {
714 my $self = shift (@_);
715 my ($doc_obj, $force) = @_;
716
717 # don't add one if there is one already set, unless we are forced to do so
718 return unless ($doc_obj->get_OID() =~ /^NULL$/ || $force);
719 $doc_obj->set_OIDtype($self->{'OIDtype'}, $self->{'OIDmetadata'});
720
721 # see if there is a plugin specific set_OID function
722 if (defined ($self->can('set_OID'))) {
723 $self->set_OID(@_); # pass through doc_obj and any extra arguments
724 }
725 else {
726 # use the default set_OID() in doc.pm
727 $doc_obj->set_OID();
728 }
729
730}
731
732# The BaseImporter read_into_doc_obj() function. This function does all the
733# right things to make general options work for a given plugin. It doesn't do anything with the file other than setting reads in
734# a file and sets up a slew of metadata all saved in doc_obj, which
735# it then returns as part of a tuple (process_status,doc_obj)
736#
737# Much of this functionality used to reside in read, but it was broken
738# down into a supporting routine to make the code more flexible.
739#
740# recursive plugins (e.g. RecPlug) and specialized plugins like those
741# capable of processing many documents within a single file (e.g.
742# GMLPlug) will normally want to implement their own version of
743# read_into_doc_obj()
744#
745# Note that $base_dir might be "" and that $file might
746# include directories
747
748# currently blocking has been done before it gets here - does this affect secondary plugin stuff??
749sub read_into_doc_obj {
750 my $self = shift (@_);
751 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
752
753 my $outhandle = $self->{'outhandle'};
754
755 # should we move this to read? What about secondary plugins?
756 my $pp_file = &util::prettyprint_file($base_dir,$file,$gli);
757 print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
758 print $outhandle "$self->{'plugin_type'} processing $pp_file\n"
759 if $self->{'verbosity'} > 1;
760
761 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
762
763 # create a new document
764 my $doc_obj = new doc ($filename_full_path, "indexed_doc", $self->{'file_rename_method'});
765 my $top_section = $doc_obj->get_top_section();
766
767 $doc_obj->add_utf8_metadata($top_section, "Plugin", "$self->{'plugin_type'}");
768 $doc_obj->add_utf8_metadata($top_section, "FileSize", (-s $filename_full_path));
769
770 my $file_dirname = &File::Basename::dirname($file);
771 $doc_obj->add_utf8_metadata($top_section, "SourceDirectory", $file_dirname);
772
773 my $plugin_filename_encoding = $self->{'filename_encoding'};
774 my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
775 $self->set_Source_metadata($doc_obj,$filename_full_path,$filename_encoding,$top_section);
776
777 # plugin specific stuff - what args do we need here??
778 unless (defined ($self->process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
779 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
780 return -1;
781 }
782
783 # include any metadata passed in from previous plugins
784 # note that this metadata is associated with the top level section
785 my $section = $doc_obj->get_top_section();
786 # can we merge these two methods??
787 $self->add_associated_files($doc_obj, $filename_full_path);
788 $self->extra_metadata ($doc_obj, $section, $metadata);
789 $self->auto_extract_metadata($doc_obj);
790
791 # to use tk label options commented out at top
792 #$self->convert_special_metadata($doc_obj);
793
794 # if we haven't found any Title so far, assign one
795 # this was shifted to here from inside read()
796 $self->title_fallback($doc_obj,$section,$filename_no_path);
797
798 if ($self->{'metadata_mapping_rules'}) {
799 $self->apply_metadata_mapping_file($doc_obj);
800 }
801
802 # force a new OID - this will use OIDtype option set for this plugin.
803 $self->add_OID($doc_obj);
804
805 $self->post_process_doc_obj($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli);
806 return (1,$doc_obj);
807}
808
809
810sub apply_metadata_mapping_file {
811 my $self = shift (@_);
812 my ($doc_obj) = @_;
813
814 my $verbosity = $self->{'verbosity'};
815 my $outhandle = $self->{'outhandle'};
816
817 my $top_section = $doc_obj->get_top_section();
818
819 my $metadata_mapping_rules = $self->{'metadata_mapping_rules'};
820
821 my $transient_metadata_names = {};
822 my $new_metadata_names = {};
823
824 print $outhandle " Applying metadata_mapping_file to document\n";
825
826 foreach my $metadata_mapping_rule_rec (@$metadata_mapping_rules) {
827
828 my $regex_type = $metadata_mapping_rule_rec->{"regex_type"};
829 my $regex_match = undef;
830 if ($regex_type eq "m") {
831 $regex_match = $metadata_mapping_rule_rec->{"regex_match"};
832 }
833
834 my $src_metadata_name = $metadata_mapping_rule_rec->{"src_metadata_name"};
835 my $dst_metadata_name = $metadata_mapping_rule_rec->{"dst_metadata_name"};
836
837 my $regex_sub = $metadata_mapping_rule_rec->{"regex_sub"};
838
839 my $metadata_vals = $doc_obj->get_metadata($top_section,$src_metadata_name);
840
841 foreach my $metadata_val (@$metadata_vals) {
842
843 if (defined $regex_match) {
844 if ($verbosity >= 4) {
845 print $outhandle " Applying match guard: '$metadata_val' must match $regex_match ...\n"
846 }
847 my $match_guard = 0;
848
849 # print STDERR "***** \$match_guard = 1 if \$metadata_val =~ $regex_match\n" ;
850 eval ( "\$match_guard = 1 if (\$metadata_val =~ $regex_match)" );
851 if ($@) {
852 warn "$@";
853 }
854 if ($match_guard) {
855 if ($verbosity >= 4) {
856 print $outhandle " ... Matched!\n"
857 }
858 }
859 else {
860 if ($verbosity >= 4) {
861 print $outhandle " ... Did not match\n"
862 }
863 next;
864 }
865 }
866
867 my $store_metadata_val = $metadata_val;
868
869
870 if ($verbosity >= 4) {
871 print $outhandle " Testing '$store_metadata_val' for match with: \$metadata_val =~ $regex_sub\n"
872 }
873 eval ( "\$metadata_val =~ $regex_sub" );
874 if ($@) {
875 warn "$@";
876 }
877 else {
878 # print STDERR "**** after metadata_val = $metadata_val\n\n";
879 if ($verbosity >=2) {
880 if ($metadata_val ne $store_metadata_val) {
881
882 if ($verbosity >= 3) {
883 print $outhandle " Transformed metadata to $dst_metadata_name: '$store_metadata_val' -> '$metadata_val'\n"
884 }
885 if ($verbosity >= 2 && ($dst_metadata_name !~ m/^_transient/)) {
886 print $outhandle " Added new metadata $dst_metadata_name: '$metadata_val'\n"
887 }
888 }
889 }
890 $doc_obj->add_utf8_metadata($top_section,$dst_metadata_name,$metadata_val);
891
892 if ($dst_metadata_name =~ m/^_transient/) {
893 $transient_metadata_names->{$dst_metadata_name} = 1;
894 }
895 else {
896 if ($metadata_val ne $store_metadata_val) {
897 $new_metadata_names->{$dst_metadata_name} = 1;
898 }
899 }
900 }
901 }
902
903 }
904
905 if ($verbosity >=1) {
906 print $outhandle " Number of metadata_mapping_file transformations for this document: ", scalar(keys %$new_metadata_names), "\n";
907
908 if ($verbosity >=2) {
909 print $outhandle " Generated metadata names: ", join(", ",keys %$new_metadata_names), "\n";
910 }
911 }
912
913 # Now remove any of dst_metadata_name which began _transient
914 foreach my $transient_metadata_name (keys %$transient_metadata_names) {
915 #print STDERR "transient_metadata_name = $transient_metadata_name\n";
916 $doc_obj->delete_metadata($top_section,$transient_metadata_name);
917 }
918}
919
920sub post_process_doc_obj {
921 my $self = shift (@_);
922 my ($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
923
924 return 1;
925}
926
927sub add_dummy_text {
928 my $self = shift(@_);
929 my ($doc_obj, $section) = @_;
930
931 # add NoText metadata so we can hide this dummy text in format statements
932 $doc_obj->add_metadata($section, "NoText", "1");
933
934 # lookup_string with extra '1' arg returns perl internal unicode aware text, so we use add_utf8_text so no encoding is done on it.
935 $doc_obj->add_utf8_text($section, &gsprintf::lookup_string("{BaseImporter.dummy_text}",1));
936 #$doc_obj->add_text($section, &gsprintf::lookup_string("{BaseImporter.dummy_text}",1));
937}
938
939sub add_dummy_text_if_empty {
940 my $self = shift(@_);
941 my ($doc_obj, $section) = @_;
942
943 my $section_text_len = $doc_obj->get_text_length($section);
944
945 if ($section_text_len == 0) {
946 $self->add_dummy_text($doc_obj,$section);
947 }
948}
949
950# does nothing. Can be overridden by subclass
951sub auto_extract_metadata {
952 my $self = shift(@_);
953 my ($doc_obj) = @_;
954}
955
956# used with the tk label options commented out at the start
957sub convert_special_metadata {
958 my $self = shift(@_);
959 my ($doc_obj) = @_;
960 print STDERR "in convert special meta\n";
961 return unless defined $self->{'store_metadata_values_as_fields_for'};
962 print STDERR "we have fields to convert\n";
963
964 my @existing_meta_fields = split(',', $self->{'store_metadata_values_as_fields_for'});
965 my @new_ns = split(',', $self->{'namespace_for_new_fields'});
966 my $include_section_level = $self->{'include_section_level'};
967
968 foreach my $old_meta (@existing_meta_fields) {
969 my $ns = shift(@new_ns);
970 my $ns_prefix = "";
971 if (defined $ns && $ns =~/\S/) {
972 $ns_prefix = "$ns.";
973 }
974
975
976 my $section = $doc_obj->get_top_section();
977 while (defined $section) {
978 my @section_meta = @{$doc_obj->get_metadata($section, $old_meta)};
979 foreach my $meta (@section_meta) {
980 foreach my $submeta (split /,/, $meta) {
981 # add meta, ns+submeta, value = true
982 # remove whitepace
983 $submeta =~ s/\s+//g;
984 $doc_obj->add_utf8_metadata($section, "$ns_prefix$submeta", "true");
985 }
986 }
987
988 last if (!$include_section_level);
989 $section = $doc_obj->get_next_section($section);
990 }
991 }
992}
993
994# adds cover image, associate_file options stuff. Should be called by sub class
995# read_into_doc_obj
996sub add_associated_files {
997 my $self = shift(@_);
998 # whatis filename??
999 my ($doc_obj, $filename) = @_;
1000
1001 # add in the cover image
1002 if ($self->{'cover_image'}) {
1003 $self->associate_cover_image($doc_obj, $filename);
1004 }
1005 # store the original (used for eg TextPlugin to store the original for OAI)
1006 if ($self->{'store_original_file'}) {
1007 $self->associate_source_file($doc_obj, $filename);
1008 }
1009
1010
1011}
1012
1013# implement this if you are extracting metadata for other documents
1014sub metadata_read {
1015 my $self = shift (@_);
1016 my ($pluginfo, $base_dir, $file, $block_hash,
1017 $extrametakeys, $extrametadata, $extrametafile,
1018 $processor, $gli, $aux) = @_;
1019
1020 # can we process this file??
1021 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
1022 return undef unless $self->can_process_this_file_for_metadata($filename_full_path);
1023
1024 return 1; # we recognise the file, but don't actually do anything with it
1025}
1026
1027
1028# The BaseImporter read() function. This function calls read_into_doc_obj()
1029# to ensure all the right things to make general options work for a
1030# given plugin are done. It then calls the process() function which
1031# does all the work specific to a plugin (like the old read functions
1032# used to do). Most plugins should define their own process() function
1033# and let this read() function keep control.
1034#
1035# recursive plugins (e.g. RecPlug) and specialized plugins like those
1036# capable of processing many documents within a single file (e.g.
1037# GMLPlug) might want to implement their own version of read(), but
1038# more likely need to implement their own version of read_into_doc_obj()
1039#
1040# Return number of files processed, undef if can't recognise, -1 if can't
1041# process
1042
1043sub read {
1044 my $self = shift (@_);
1045 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
1046
1047 # can we process this file??
1048 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
1049
1050 return undef unless $self->can_process_this_file($filename_full_path);
1051
1052 #print STDERR "**** BEFORE READ INTO DOC OBJ: $file\n";
1053 my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
1054 #print STDERR "**** AFTER READ INTO DOC OBJ: $file\n";
1055
1056 if ((defined $process_status) && ($process_status == 1)) {
1057
1058 # process the document
1059 $processor->process($doc_obj);
1060
1061 $self->{'num_processed'} ++;
1062 undef $doc_obj;
1063 }
1064 # delete any temp files that we may have created
1065 $self->clean_up_after_doc_obj_processing();
1066
1067
1068 # if process_status == 1, then the file has been processed.
1069 return $process_status;
1070
1071}
1072
1073# returns undef if file is rejected by the plugin
1074sub process {
1075 my $self = shift (@_);
1076 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
1077
1078 gsprintf(STDERR, "BaseImporter::process {common.must_be_implemented}\n");
1079
1080 my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
1081 print STDERR "Calling method: $cfilename:$cline $cpackage->$csubr\n";
1082
1083 die "\n";
1084
1085 return undef; # never gets here
1086}
1087
1088# overwrite this method to delete any temp files that we have created
1089sub clean_up_after_doc_obj_processing {
1090 my $self = shift(@_);
1091
1092}
1093
1094
1095
1096sub filename_based_title
1097{
1098 my $self = shift (@_);
1099 my ($file) = @_;
1100
1101 my $file_derived_title = $file;
1102 $file_derived_title =~ s/_/ /g;
1103 $file_derived_title =~ s/\.[^.]+$//;
1104
1105 return $file_derived_title;
1106}
1107
1108
1109sub title_fallback
1110{
1111 my $self = shift (@_);
1112 my ($doc_obj,$section,$file) = @_;
1113
1114 if (!defined $doc_obj->get_metadata_element ($section, "Title")
1115 || $doc_obj->get_metadata_element($section, "Title") eq "") {
1116
1117 my $source_file = $doc_obj->get_metadata_element($section, "Source");
1118 my $file_derived_title;
1119 if (defined $source_file) {
1120 $file_derived_title = $self->filename_based_title($source_file);
1121 }
1122 else {
1123 # pp = pretty print
1124 my $pp_file = (defined $source_file) ? $source_file : $file;
1125
1126 my $raw_title = $self->filename_based_title($file);
1127 my $file_derived_title = &unicode::raw_filename_to_url_encoded($raw_title);
1128 }
1129
1130
1131 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
1132 $doc_obj->add_utf8_metadata ($section, "Title", $file_derived_title);
1133 }
1134 else {
1135 $doc_obj->set_utf8_metadata_element ($section, "Title", $file_derived_title);
1136 }
1137 }
1138
1139}
1140
1141# add any extra metadata that's been passed around from one
1142# plugin to another.
1143# extra_metadata uses add_utf8_metadata so it expects metadata values
1144# to already be in utf8
1145sub extra_metadata {
1146 my $self = shift (@_);
1147 my ($doc_obj, $cursection, $metadata) = @_;
1148
1149 my $associate_tail_re = $self->{'associate_tail_re'};
1150
1151# Sort the extra metadata for diffcol so these meta appear in a consistent order
1152# in doc.xml. Necessary for the ex.PDF.* and ex.File.* meta that's extracted in
1153# the PDFBox collection, as the order of these varies between CentOS and Ubuntu.
1154 foreach my $field (sort keys(%$metadata)) {
1155# foreach my $field (keys(%$metadata)) {
1156 # $metadata->{$field} may be an array reference
1157 if ($field eq "gsdlassocfile_tobe") {
1158 # 'gsdlassocfile_tobe' is artificially introduced metadata
1159 # that is used to signal that certain additional files should
1160 # be tied to this document. Useful in situations where a
1161 # metadata pass in the plugin pipeline works out some files
1162 # need to be associated with a document, but the document hasn't
1163 # been formed yet.
1164 my $equiv_form = "";
1165 foreach my $gaf (@{$metadata->{$field}}) {
1166 my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
1167 my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
1168
1169 # we need to make sure the filename is valid utf-8 - we do
1170 # this by url or base64 encoding it
1171 # $tail_filename is the name that we store the file as
1172 $tail_filename = &util::rename_file($tail_filename, $self->{'file_rename_method'});
1173 $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
1174 $doc_obj->associate_source_file($full_filename);
1175 # If the filename is url_encoded, we need to encode the % signs
1176 # in the filename, so that it works in a url
1177 my $url_tail_filename = &unicode::filename_to_url($tail_filename);
1178 # work out extended tail extension (i.e. matching tail re)
1179
1180 my ($file_prefix,$file_extended_ext)
1181 = &util::get_prefix_and_tail_by_regex($tail_filename,$associate_tail_re);
1182 my ($pre_doc_ext) = ($file_extended_ext =~ m/^(.*)\..*$/);
1183 my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
1184
1185 # the greenstone 2 stuff
1186 my $start_doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/{Or}{[parent(Top):assocfilepath],[assocfilepath]}/$url_tail_filename\">";
1187 #my $start_doclink = "<a href=\'_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$url_tail_filename\'>";
1188 my $start_doclink_gs3 = "<a href=\'_httpprefix_/collect/[collection]/index/assoc/[assocfilepath]/$url_tail_filename\'>";
1189
1190 my $srcicon = "_icon".$doc_ext."_";
1191 my $end_doclink = "</a>";
1192
1193 my $assoc_form = "$start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
1194
1195
1196 if (defined $pre_doc_ext && $pre_doc_ext ne "") {
1197 # for metadata such as [mp3._edited] [mp3._full] ...
1198 $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.$pre_doc_ext", $assoc_form);
1199 }
1200
1201 # for multiple metadata such as [mp3.assoclink]
1202 $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.assoclink", $assoc_form);
1203
1204 $equiv_form .= " $assoc_form";
1205
1206 # following are used for greenstone 3,
1207 $doc_obj->add_utf8_metadata ($cursection, "equivDocLink", $start_doclink_gs3);
1208 $doc_obj->add_utf8_metadata ($cursection, "equivDocIcon", $srcicon);
1209 $doc_obj->add_utf8_metadata ($cursection, "/equivDocLink", $end_doclink);
1210
1211 }
1212 $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
1213 }
1214 elsif ($field eq "gsdlzipfilename") {
1215 # special case for when files have come out of a zip. source_path
1216 # (used for archives dbs and keeping track for incremental import)
1217 # must be set to the zip file name
1218 my $zip_filename = $metadata->{$field};
1219 # overwrite the source_path
1220 $doc_obj->set_source_path($zip_filename);
1221 # and set the metadata
1222 $zip_filename = &util::filename_within_collection($zip_filename);
1223 $zip_filename = $doc_obj->encode_filename($zip_filename, $self->{'file_rename_method'});
1224 $doc_obj->add_utf8_metadata ($cursection, $field, $zip_filename);
1225 }
1226 elsif (ref ($metadata->{$field}) eq "ARRAY") {
1227 if ($field =~ /(.+?)\/\/\/Section\/([\d.]*)/m){
1228 my $field_new_name = $1;
1229 my $specified_section = $2;
1230 map {
1231 $doc_obj->add_utf8_metadata ($specified_section, $field_new_name, $_);
1232 } @{$metadata->{$field}};
1233 } else {
1234 map {
1235 $doc_obj->add_utf8_metadata ($cursection, $field, $_);
1236 } @{$metadata->{$field}};
1237 }
1238
1239 } else {
1240 if ($field =~ /(.+?)\/\/\/Section\/([\d.]*)/m){
1241 my $field_new_name = $1;
1242 my $specified_section = $2;
1243 $doc_obj->add_utf8_metadata ($specified_section, $field_new_name, $metadata->{$field});
1244 } else {
1245 $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
1246 }
1247 }
1248 }
1249}
1250
1251
1252sub compile_stats {
1253 my $self = shift(@_);
1254 my ($stats) = @_;
1255
1256 $stats->{'num_processed'} += $self->{'num_processed'};
1257 $stats->{'num_not_processed'} += $self->{'num_not_processed'};
1258 $stats->{'num_archives'} += $self->{'num_archives'};
1259
1260}
1261sub associate_source_file {
1262 my $self = shift(@_);
1263
1264 my ($doc_obj, $filename) = @_;
1265 my $cursection = $doc_obj->get_top_section();
1266 my $assocfilename = $doc_obj->get_assocfile_from_sourcefile();
1267
1268 $doc_obj->associate_file($filename, $assocfilename, undef, $cursection);
1269 # srclink_file is now deprecated because of the "_" in the metadataname. Use srclinkFile
1270 $doc_obj->add_utf8_metadata ($cursection, "srclink_file", $doc_obj->get_sourcefile());
1271 $doc_obj->add_utf8_metadata ($cursection, "srclinkFile", $doc_obj->get_sourcefile());
1272}
1273
1274sub associate_cover_image {
1275 my $self = shift(@_);
1276 my ($doc_obj, $filename) = @_;
1277
1278 my $upgraded_filename = &util::upgrade_if_dos_filename($filename);
1279
1280 $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
1281 $upgraded_filename =~ s/\.[^\\\/\.]+$/\.jpg/;
1282
1283 if (exists $self->{'covers_missing_cache'}->{$upgraded_filename}) {
1284 # don't stat() for existence e.g. for multiple document input files
1285 # (eg SplitPlug)
1286 return;
1287 }
1288
1289 my $top_section=$doc_obj->get_top_section();
1290
1291 if (&FileUtils::fileExists($upgraded_filename)) {
1292 $doc_obj->associate_source_file($filename);
1293 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
1294 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1295 } else {
1296 my $upper_filename = $filename;
1297 my $upgraded_upper_filename = $upgraded_filename;
1298
1299 $upper_filename =~ s/jpg$/JPG/;
1300 $upgraded_upper_filename =~ s/jpg$/JPG/;
1301
1302 if (&FileUtils::fileExists($upgraded_upper_filename)) {
1303 $doc_obj->associate_source_file($upper_filename);
1304 $doc_obj->associate_file($upper_filename, "cover.jpg",
1305 "image/jpeg");
1306 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1307 } else {
1308 # file doesn't exist, so record the fact that it's missing so
1309 # we don't stat() again (stat is slow)
1310 $self->{'covers_missing_cache'}->{$upgraded_filename} = 1;
1311 }
1312 }
1313
1314}
1315
1316
1317# Overridden by exploding plugins (eg. ISISPlug)
1318sub clean_up_after_exploding
1319{
1320 my $self = shift(@_);
1321}
1322
1323
1324
13251;
Note: See TracBrowser for help on using the repository browser.