source: trunk/gsdl/perllib/plugins/BasPlug.pm@ 10994

Last change on this file since 10994 was 10833, checked in by jrm21, 19 years ago

store the names of files we've already checked when looking for a cover
image, otherwise splitplug documents (eg email from the same file) causes
lots of file accesses to check if the cover image exists, which is slow

  • Property svn:keywords set to Author Date Id Revision
File size: 43.1 KB
Line 
1###########################################################################
2#
3# BasPlug.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 BasPlug;
27
28BEGIN {
29 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
30}
31
32eval {require bytes};
33
34# suppress the annoying "subroutine redefined" warning that various
35# plugins cause under perl 5.6
36$SIG{__WARN__} = sub {warn($_[0]) unless ($_[0] =~ /Subroutine\s+\S+\sredefined/)};
37
38use strict;
39no strict 'subs';
40no strict 'refs'; # allow filehandles to be variables and viceversa
41
42use File::Basename;
43
44use Kea;
45use multiread;
46use encodings;
47use cnseg;
48use acronym;
49use textcat;
50use doc;
51eval "require diagnostics"; # some perl distros (eg mac) don't have this
52use DateExtract;
53use ghtml;
54use gsprintf 'gsprintf';
55use printusage;
56use parse2;
57
58
59use GISBasPlug;
60
61@BasPlug::ISA = ( GISBasPlug );
62
63my $unicode_list =
64 [ { 'name' => "ascii",
65 'desc' => "{BasPlug.input_encoding.ascii}" },
66 { 'name' => "utf8",
67 'desc' => "{BasPlug.input_encoding.utf8}" },
68 { 'name' => "unicode",
69 'desc' => "{BasPlug.input_encoding.unicode}" } ];
70
71my $auto_unicode_list =
72 [ { 'name' => "auto",
73 'desc' => "{BasPlug.input_encoding.auto}" } ];
74
75my $e = $encodings::encodings;
76foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
77{
78 my $hashEncode =
79 {'name' => $enc,
80 'desc' => $e->{$enc}->{'name'}};
81
82 push(@{$unicode_list},$hashEncode);
83}
84
85push(@{$auto_unicode_list},@{$unicode_list});
86
87my $arguments =
88 [ { 'name' => "process_exp",
89 'desc' => "{BasPlug.process_exp}",
90 'type' => "regexp",
91 'deft' => "",
92 'reqd' => "no" },
93 { 'name' => "block_exp",
94 'desc' => "{BasPlug.block_exp}",
95 'type' => "regexp",
96 'deft' => "",
97 'reqd' => "no" },
98 { 'name' => "smart_block",
99 'desc' => "{BasPlug.smart_block}",
100 'type' => "flag",
101 'reqd' => "no" },
102 { 'name' => "associate_ext",
103 'desc' => "{BasPlug.associate_ext}",
104 'type' => "string",
105 'reqd' => "no" },
106 { 'name' => "input_encoding",
107 'desc' => "{BasPlug.input_encoding}",
108 'type' => "enum",
109 'list' => $auto_unicode_list,
110 'reqd' => "no" ,
111 'deft' => "auto" } ,
112 { 'name' => "default_encoding",
113 'desc' => "{BasPlug.default_encoding}",
114 'type' => "enum",
115 'list' => $unicode_list,
116 'reqd' => "no",
117 'deft' => "utf8" },
118 { 'name' => "extract_language",
119 'desc' => "{BasPlug.extract_language}",
120 'type' => "flag",
121 'reqd' => "no" },
122 { 'name' => "default_language",
123 'desc' => "{BasPlug.default_language}",
124 'type' => "string",
125 'deft' => "en",
126 'reqd' => "no" },
127 { 'name' => "extract_acronyms",
128 'desc' => "{BasPlug.extract_acronyms}",
129 'type' => "flag",
130 'reqd' => "no" },
131 { 'name' => "markup_acronyms",
132 'desc' => "{BasPlug.markup_acronyms}",
133 'type' => "flag",
134 'reqd' => "no" },
135 { 'name' => "extract_keyphrases",
136 'desc' => "{BasPlug.extract_keyphrases}",
137 'type' => "flag",
138 'reqd' => "no" },
139 { 'name' => "extract_keyphrase_options",
140 'desc' => "{BasPlug.extract_keyphrase_options}",
141 'type' => "string",
142 'deft' => "",
143 'reqd' => "no" },
144 { 'name' => "first",
145 'desc' => "{BasPlug.first}",
146 'type' => "string",
147 'reqd' => "no" },
148 { 'name' => "extract_email",
149 'desc' => "{BasPlug.extract_email}",
150 'type' => "flag",
151 'reqd' => "no" },
152 { 'name' => "extract_historical_years",
153 'desc' => "{BasPlug.extract_historical_years}",
154 'type' => "flag",
155 'reqd' => "no" },
156 { 'name' => "maximum_year",
157 'desc' => "{BasPlug.maximum_year}",
158 'type' => "int",
159 'deft' => (localtime)[5]+1900,
160 'char_length' => "4",
161 #'range' => "2,100",
162 'reqd' => "no"},
163 { 'name' => "maximum_century",
164 'desc' => "{BasPlug.maximum_century}",
165 'type' => "string",
166 'deft' => "-1",
167 'reqd' => "no" },
168 { 'name' => "no_bibliography",
169 'desc' => "{BasPlug.no_bibliography}",
170 'type' => "flag",
171 'reqd' => "no"},
172 { 'name' => "no_cover_image",
173 'desc' => "{BasPlug.no_cover_image}",
174 'type' => "flag",
175 'reqd' => "no" },
176 { 'name' => "extract_keyphrases",
177 'desc' => "{BasPlug.extract_keyphrases}",
178 'type' => "flag",
179 'reqd' => "no",
180 'hiddengli' => "yes" },
181 { 'name' => "extract_keyphrase_options",
182 'desc' => "{BasPlug.extract_keyphrase_options}",
183 'type' => "string",
184 'reqd' => "no",
185 'hiddengli' => "yes" },
186 { 'name' => "separate_cjk",
187 'desc' => "{BasPlug.separate_cjk}",
188 'type' => "flag",
189 'reqd' => "no",
190 'hiddengli' => "yes" },
191 { 'name' => "smart_block",
192 'desc' => "{BasPlug.smart_block}",
193 'type' => "flag",
194 'reqd' => "no",
195 'hiddengli' => "yes" },
196 { 'name' => "new_extract_email",
197 'desc' => "",
198 'type' => "flag",
199 'reqd' => "no",
200 'hiddengli' => "yes" } ];
201
202my $gis_arguments =
203 [ { 'name' => "extract_placenames",
204 'desc' => "{GISBasPlug.extract_placenames}",
205 'type' => "flag",
206 'reqd' => "no" },
207 { 'name' => "gazetteer",
208 'desc' => "{GISBasPlug.gazetteer}",
209 'type' => "string",
210 'reqd' => "no" },
211 { 'name' => "place_list",
212 'desc' => "{GISBasPlug.place_list}",
213 'type' => "flag",
214 'reqd' => "no" } ];
215
216
217my $options = { 'name' => "BasPlug",
218 'desc' => "{BasPlug.desc}",
219 'abstract' => "yes",
220 'inherits' => "no",
221 'args' => $arguments };
222
223
224sub set_keepold {
225 my $self = shift(@_);
226 my ($keepold) = @_;
227
228 $self->{'keepold'} = $keepold;
229}
230
231sub get_arguments
232{
233 my $self = shift(@_);
234 my $optionlistref = $self->{'option_list'};
235 my @optionlist = @$optionlistref;
236 my $pluginoptions = pop(@$optionlistref);
237 my $pluginarguments = $pluginoptions->{'args'};
238 return $pluginarguments;
239}
240
241
242sub print_xml_usage
243{
244 my $self = shift(@_);
245
246 # XML output is always in UTF-8
247 gsprintf::output_strings_in_UTF8;
248
249 PrintUsage::print_xml_header();
250 $self->print_xml();
251}
252
253
254sub print_xml
255{
256 my $self = shift(@_);
257
258 my $optionlistref = $self->{'option_list'};
259 my @optionlist = @$optionlistref;
260 my $pluginoptions = shift(@$optionlistref);
261 return if (!defined($pluginoptions));
262
263 gsprintf(STDERR, "<PlugInfo>\n");
264 gsprintf(STDERR, " <Name>$pluginoptions->{'name'}</Name>\n");
265 my $desc = gsprintf::lookup_string($pluginoptions->{'desc'});
266 $desc =~ s/</&amp;lt;/g; # doubly escaped
267 $desc =~ s/>/&amp;gt;/g;
268
269 gsprintf(STDERR, " <Desc>$desc</Desc>\n");
270 gsprintf(STDERR, " <Abstract>$pluginoptions->{'abstract'}</Abstract>\n");
271 gsprintf(STDERR, " <Inherits>$pluginoptions->{'inherits'}</Inherits>\n");
272 gsprintf(STDERR, " <Explodes>" . ($pluginoptions->{'explodes'} || "no") . "</Explodes>\n");
273 gsprintf(STDERR, " <Arguments>\n");
274 if (defined($pluginoptions->{'args'})) {
275 &PrintUsage::print_options_xml($pluginoptions->{'args'});
276 }
277
278 # Recurse up the plugin hierarchy
279 $self->print_xml();
280
281 gsprintf(STDERR, " </Arguments>\n");
282 gsprintf(STDERR, "</PlugInfo>\n");
283}
284
285
286sub print_txt_usage
287{
288 my $self = shift(@_);
289 # Print the usage message for a plugin (recursively)
290 my $descoffset = $self->determine_description_offset(0);
291 $self->print_plugin_usage($descoffset, 1);
292}
293
294
295sub determine_description_offset
296{
297 my $self = shift(@_);
298 my $maxoffset = shift(@_);
299
300 my $optionlistref = $self->{'option_list'};
301 my @optionlist = @$optionlistref;
302 my $pluginoptions = shift(@$optionlistref);
303 return $maxoffset if (!defined($pluginoptions));
304
305 # Find the length of the longest option string of this plugin
306 my $pluginargs = $pluginoptions->{'args'};
307 if (defined($pluginargs)) {
308 my $longest = &PrintUsage::find_longest_option_string($pluginargs);
309 if ($longest > $maxoffset) {
310 $maxoffset = $longest;
311 }
312 }
313
314 # Recurse up the plugin hierarchy
315 $maxoffset = $self->determine_description_offset($maxoffset);
316 $self->{'option_list'} = \@optionlist;
317 return $maxoffset;
318}
319
320
321sub print_plugin_usage
322{
323 my $self = shift(@_);
324 my $descoffset = shift(@_);
325 my $isleafclass = shift(@_);
326
327 my $optionlistref = $self->{'option_list'};
328 my @optionlist = @$optionlistref;
329 my $pluginoptions = shift(@$optionlistref);
330 return if (!defined($pluginoptions));
331
332 my $pluginname = $pluginoptions->{'name'};
333 my $pluginargs = $pluginoptions->{'args'};
334 my $plugindesc = $pluginoptions->{'desc'};
335
336 # Produce the usage information using the data structure above
337 if ($isleafclass) {
338 if (defined($plugindesc)) {
339 gsprintf(STDERR, "$plugindesc\n\n");
340 }
341 gsprintf(STDERR, " {common.usage}: plugin $pluginname [{common.options}]\n\n");
342 }
343
344 # Display the plugin options, if there are some
345 if (defined($pluginargs)) {
346 # Calculate the column offset of the option descriptions
347 my $optiondescoffset = $descoffset + 2; # 2 spaces between options & descriptions
348
349 if ($isleafclass) {
350 gsprintf(STDERR, " {common.specific_options}:\n");
351 }
352 else {
353 gsprintf(STDERR, " {common.general_options}:\n", $pluginname);
354 }
355
356 # Display the plugin options
357 &PrintUsage::print_options_txt($pluginargs, $optiondescoffset);
358 }
359
360 # Recurse up the plugin hierarchy
361 $self->print_plugin_usage($descoffset, 0);
362 $self->{'option_list'} = \@optionlist;
363}
364
365
366sub new {
367 # Set Encodings to the list!!
368
369
370 # Start the BasPlug Constructor
371 my $class = shift (@_);
372 my ($pluginlist,$args,$hashArgOptLists) = @_;
373 push(@$pluginlist, $class);
374 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
375
376 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
377 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
378
379 if (GISBasPlug::has_mapdata()) {
380 push(@$arguments,@$gis_arguments);
381 }
382
383 my $self = {};
384 $self->{'outhandle'} = STDERR;
385 $self->{'option_list'} = $hashArgOptLists->{"OptList"};
386 $self->{"info_only"} = 0;
387
388 # Check if gsdlinfo is in the argument list or not - if it is, don't parse
389 # the args, just return the object.
390 foreach my $strArg (@{$args})
391 {
392 if($strArg eq "-gsdlinfo")
393 {
394 $self->{"info_only"} = 1;
395 return bless $self, $class;
396 }
397 }
398
399 if(!parse2::parse($args,$hashArgOptLists->{"ArgList"},$self))
400 {
401 my $classTempClass = bless $self, $class;
402 print STDERR "<BadPlugin p=$plugin_name>\n";
403 &gsprintf(STDERR, "\n{BasPlug.bad_general_option}\n", $plugin_name);
404 $classTempClass->print_txt_usage(""); # Use default resource bundle
405 die "\n";
406 }
407
408
409 delete $self->{"info_only"};
410 # else parsing was successful.
411
412 $self->{'plugin_type'} = $plugin_name;
413 #$self->{'outhandle'} = STDERR;
414 $self->{'textcat'} = new textcat();
415 $self->{'num_processed'} = 0;
416 $self->{'num_not_processed'} = 0;
417 $self->{'num_blocked'} = 0;
418 $self->{'num_archives'} = 0;
419 $self->{'cover_image'} = 1; # cover image is on by default
420 $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
421 $self->{'file_blocks'} = {};
422 #$self->{'option_list'} = $hashArgOptLists->{"OptList"};
423
424 my $associate_ext = $self->{'associate_ext'};
425 if ((defined $associate_ext) && ($associate_ext ne "")) {
426 my @exts = split(/,/,$associate_ext);
427
428 my %associate_ext_lookup = ();
429 foreach my $e (@exts) {
430 $associate_ext_lookup{$e} = 1;
431 }
432
433 $self->{'associate_ext_lookup'} = \%associate_ext_lookup;
434 }
435
436 $self->{'shared_fileroot'} = {};
437 $self->{'file_blocks'} = {};
438
439 if ($self->{'extract_placenames'}) {
440
441 my $outhandle = $self->{'outhandle'};
442
443 my $places_ref
444 = GISBasPlug::loadGISDatabase($outhandle,$self->{'gazetteer'});
445
446 if (!defined $places_ref) {
447 print $outhandle "Warning: Error loading mapdata gazetteer \"$self->{'gazetteer'}\"\n";
448 print $outhandle " No placename extraction will take place.\n";
449 $self->{'extract_placenames'} = undef;
450 }
451 else {
452 $self->{'places'} = $places_ref;
453 }
454 }
455 return bless $self, $class;
456
457}
458
459# initialize BasPlug options
460# if init() is overridden in a sub-class, remember to call BasPlug::init()
461sub init {
462 my $self = shift (@_);
463 my ($verbosity, $outhandle, $failhandle) = @_;
464
465 # verbosity is passed through from the processor
466 $self->{'verbosity'} = $verbosity;
467
468 # as are the outhandle and failhandle
469 $self->{'outhandle'} = $outhandle if defined $outhandle;
470 $self->{'failhandle'} = $failhandle;
471
472 # set process_exp and block_exp to defaults unless they were
473 # explicitly set
474
475 if ((!$self->is_recursive()) and
476 (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
477
478 $self->{'process_exp'} = $self->get_default_process_exp ();
479 if ($self->{'process_exp'} eq "") {
480 warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
481 }
482 }
483
484 if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
485 $self->{'block_exp'} = $self->get_default_block_exp ();
486 }
487}
488
489sub begin {
490 my $self = shift (@_);
491 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
492 $self->initialise_extractors();
493}
494
495sub end {
496 # potentially called at the end of each plugin pass
497 # import.pl only has one plugin pass, but buildcol.pl has multiple ones
498
499 my ($self) = @_;
500 $self->finalise_extractors();
501}
502
503sub deinit {
504 # called only once, after all plugin passes have been done
505
506 my ($self) = @_;
507}
508
509# this function should be overridden to return 1
510# in recursive plugins
511sub is_recursive {
512 my $self = shift (@_);
513
514 return 0;
515}
516
517sub get_default_block_exp {
518 my $self = shift (@_);
519
520 return "";
521}
522
523sub get_default_process_exp {
524 my $self = shift (@_);
525
526 return "";
527}
528
529# default implementation is to do nothing.
530sub store_block_files
531{
532 my $self =shift (@_);
533 my ($filename) = @_;
534 return;
535}
536
537#default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
538sub block_cover_image
539{
540 my $self =shift;
541 my $filename = shift;
542
543 if (exists $self->{'file_blocks'}->{$filename}) {
544 # we've already checked if this file exists, so don't stat() again.
545 # (eg for multi-sectioned files using SplitPlug)
546 return;
547 }
548
549 if ($self->{'cover_image'}) {
550 my $coverfile = $filename;
551 $coverfile =~ s/\.[^\\\/\.]+$/\.jpg/;
552 if (!-e $coverfile) {
553 $coverfile =~ s/jpg$/JPG/;
554 }
555 if (-e $coverfile) {
556 $self->{'file_blocks'}->{$coverfile} = 1;
557 } else {
558 $self->{'file_blocks'}->{$coverfile} = 0;
559 }
560 }
561
562 return;
563}
564
565sub metadata_read {
566 my $self = shift (@_);
567 my ($pluginfo, $base_dir, $file, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
568 # Keep track of filenames with same root but different extensions
569 # Used to support -associate_ext
570
571 my $associate_ext = $self->{'associate_ext'};
572 if ((defined $associate_ext) && ($associate_ext ne "")) {
573
574 my ($file_prefix,$file_ext) = ($file =~ m/^(.*)\.(.*?)$/);
575 if ((defined $file_prefix) && (defined $file_ext)) {
576
577 my $shared_fileroot = $self->{'shared_fileroot'};
578 if (!defined $shared_fileroot->{$file_prefix}) {
579 my $file_prefix_rec = { 'tie_to' => undef, 'exts' => {} };
580 $shared_fileroot->{$file_prefix} = $file_prefix_rec;
581 }
582
583 my $file_prefix_rec = $shared_fileroot->{$file_prefix};
584
585 my $process_exp = $self->{'process_exp'};
586
587 if ($file =~ m/$self->{'process_exp'}/) {
588 # This is the document the others should be tied to
589 $file_prefix_rec->{'tie_to'} = $file_ext;
590 }
591 else {
592 if (defined $self->{'associate_ext_lookup'}->{$file_ext}) {
593 $file_prefix_rec->{'exts'}->{$file_ext} = 1;
594 }
595 }
596 }
597 }
598
599 # now check whether we are actually processing this
600 my $filename = $file;
601 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
602 if ($self->{'process_exp'} eq "" || $filename !~ /$self->{'process_exp'}/ || !-f $filename) {
603 return undef; # can't recognise
604 }
605
606 # do smart blocking if appropriate
607 if (defined $self->{'smart_block'}) {
608 $self->block_cover_image($filename);
609 $self->store_block_files($filename);
610 }
611
612 return 1;
613}
614
615sub tie_to_filename
616{
617 my $self = shift (@_);
618
619 my ($file_ext,$file_prefix_rec) = @_;
620
621 if (defined $file_prefix_rec) {
622 my $tie_to = $file_prefix_rec->{'tie_to'};
623
624 if (defined $tie_to) {
625 if ($tie_to eq $file_ext) {
626 return 1;
627 }
628 }
629 }
630
631 return 0;
632}
633
634sub tie_to_assoc_file
635{
636 my $self = shift (@_);
637 my ($file_ext,$file_prefix_rec) = @_;
638
639 if (defined $file_prefix_rec) {
640 my $tie_to = $file_prefix_rec->{'tie_to'};
641 if (defined $tie_to) {
642
643 my $exts = $file_prefix_rec->{'exts'};
644
645 my $has_file_ext = $exts->{$file_ext};
646
647 if ($has_file_ext) {
648 return 1;
649 }
650 }
651 }
652
653 return 0;
654}
655
656
657sub associate_with
658{
659 my $self = shift (@_);
660 my ($file, $filename, $metadata) = @_;
661
662 my $associate_ext = $self->{'associate_ext'};
663
664
665 return 0 if (!$associate_ext);
666
667 # If file, see if matches with "tie_to" doc or is one of the
668 # associated filename extensions.
669
670 my ($file_prefix,$file_ext) = ($file =~ m/^(.*)\.(.*?)$/);
671 if ((defined $file_prefix) && (defined $file_ext)) {
672
673 my $file_prefix_rec = $self->{'shared_fileroot'}->{$file_prefix};
674
675 if ($self->tie_to_filename($file_ext,$file_prefix_rec)) {
676
677 # Set up gsdlassocfile_tobe
678
679 my $exts = $file_prefix_rec->{'exts'};
680
681 if (!defined $metadata->{'gsdlassocfile_tobe'}) {
682 $metadata->{'gsdlassocfile_tobe'} = [];
683 }
684
685 my $assoc_tobe = $metadata->{'gsdlassocfile_tobe'};
686
687 my ($full_prefix) = ($filename =~ m/^(.*)\..*?$/);
688 foreach my $e (keys %$exts) {
689 my $assoc_file = "$full_prefix.$e";
690 my $mime_type = ""; # let system auto detect this
691 push(@$assoc_tobe,"$assoc_file:$mime_type:");
692 }
693 }
694 elsif ($self->tie_to_assoc_file($file_ext,$file_prefix_rec)) {
695 # a form of smart block
696
697 return 1;
698 }
699 }
700
701 return 0;
702}
703
704
705sub read_block {
706 my $self = shift (@_);
707
708 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
709
710
711 my $filename = $file;
712 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
713
714 if ($self->associate_with($file,$filename,$metadata)) {
715 # a form of smart block
716 $self->{'num_blocked'} ++;
717 return (0,undef); # blocked
718 }
719
720 my $smart_block = $self->{'smart_block'};
721 my $smart_block_BN = $self->{'smart_block_BN'};
722
723 if ($smart_block || $smart_block_BN) {
724 if (defined $self->{'file_blocks'}->{$filename} && $self->{'file_blocks'}->{$filename} == 1){
725 $self->{'num_blocked'} ++;
726 return (0,undef); # blocked
727 }
728 } elsif ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
729 $self->{'num_blocked'} ++;
730 return (0,undef); # blocked
731 }
732
733 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
734 return (undef,undef); # can't recognise
735 }
736
737 return (1,$filename);
738}
739
740sub read_tidy_file {
741
742 my $self = shift (@_);
743
744 my ($file) = @_;
745
746 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
747
748 return $file;
749}
750
751
752
753# The BasPlug read_into_doc_obj() function. This function does all the
754# right things to make general options work for a given plugin. It reads in
755# a file and sets up a slew of metadata all saved in doc_obj, which
756# it then returns as part of a tuple (process_status,doc_obj)
757#
758# Much of this functionality used to reside in read, but it was broken
759# down into a supporting routine to make the code more flexible.
760#
761# recursive plugins (e.g. RecPlug) and specialized plugins like those
762# capable of processing many documents within a single file (e.g.
763# GMLPlug) will normally want to implement their own version of
764# read_into_doc_obj()
765#
766# Note that $base_dir might be "" and that $file might
767# include directories
768sub read_into_doc_obj {
769 my $self = shift (@_);
770 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
771
772 if ($self->is_recursive()) {
773 gsprintf(STDERR, "{BasPlug.read_must_be_implemented}") && die "\n";
774 }
775
776 my $outhandle = $self->{'outhandle'};
777
778 my ($block_status,$filename) = $self->read_block(@_);
779 return $block_status if ((!defined $block_status) || ($block_status==0));
780 $file = $self->read_tidy_file($file);
781
782 # Do encoding stuff
783 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
784
785 # create a new document
786 my $doc_obj = new doc ($filename, "indexed_doc");
787 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
788 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Language", $language);
789 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Encoding", $encoding);
790 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
791 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
792
793 my ($filemeta) = $file =~ /([^\\\/]+)$/;
794 # how do we know what encoding the filename is in?
795 $doc_obj->add_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
796 if ($self->{'cover_image'}) {
797 $self->associate_cover_image($doc_obj, $filename);
798 }
799
800 # read in file ($text will be in utf8)
801 my $text = "";
802 $self->read_file ($filename, $encoding, $language, \$text);
803
804 if (!length ($text)) {
805 my $plugin_name = ref ($self);
806 if ($gli) {
807 print STDERR "<ProcessingError n='$file' r='File contains no text'>\n";
808 }
809 gsprintf($outhandle, "$plugin_name: {BasPlug.file_has_no_text}\n", $filename) if $self->{'verbosity'};
810
811 my $failhandle = $self->{'failhandle'};
812 gsprintf($failhandle, "$file: " . ref($self) . ": {BasPlug.empty_file}\n");
813 # print $failhandle "$file: " . ref($self) . ": file contains no text\n";
814 $self->{'num_not_processed'} ++;
815
816 return (0,undef); # what should we return here?? error but don't want to pass it on
817 }
818
819 # include any metadata passed in from previous plugins
820 # note that this metadata is associated with the top level section
821
822 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
823
824 # do plugin specific processing of doc_obj
825 unless (defined ($self->process (\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
826 $text = '';
827 undef $text;
828 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
829 return (-1,undef);
830 }
831 $text='';
832 undef $text;
833
834 # do any automatic metadata extraction
835 $self->auto_extract_metadata ($doc_obj);
836
837 # add an OID
838 # see if there is a plugin-specific set_OID function...
839 if (defined ($self->can('set_OID'))) {
840 # it will need $doc_obj to set the Identifier metadata...
841 $self->set_OID($doc_obj);
842 } else {
843 # use the default set_OID() in doc.pm
844 $doc_obj->set_OID();
845 }
846
847 return (1,$doc_obj);
848}
849
850
851# The BasPlug read() function. This function calls read_into_doc_obj()
852# to ensure all the right things to make general options work for a
853# given plugin are done. It then calls the process() function which
854# does all the work specific to a plugin (like the old read functions
855# used to do). Most plugins should define their own process() function
856# and let this read() function keep control.
857#
858# recursive plugins (e.g. RecPlug) and specialized plugins like those
859# capable of processing many documents within a single file (e.g.
860# GMLPlug) might want to implement their own version of read(), but
861# more likely need to implement their own version of read_into_doc_obj()
862#
863# Return number of files processed, undef if can't recognise, -1 if can't
864# process
865
866sub read {
867 my $self = shift (@_);
868 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
869
870 my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
871
872 if ((defined $process_status) && ($process_status == 1)) {
873 # process the document
874 $processor->process($doc_obj);
875
876 if(defined($self->{'places_filename'})){
877 &util::rm($self->{'places_filename'});
878 $self->{'places_filename'} = undef;
879 }
880
881 $self->{'num_processed'} ++;
882 undef $doc_obj;
883 }
884
885 # if process_status == 1, then the file has been processed.
886 return $process_status;
887
888}
889
890# returns undef if file is rejected by the plugin
891sub process {
892 my $self = shift (@_);
893 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
894
895 gsprintf(STDERR, "BasPlug::process {common.must_be_implemented}\n") && die "\n";
896 # die "Basplug::process function must be implemented in sub-class\n";
897
898 return undef; # never gets here
899}
900
901# uses the multiread package to read in the entire file pointed to
902# by filename and loads the resulting text into $$textref. Input text
903# may be in any of the encodings handled by multiread, output text
904# will be in utf8
905sub read_file {
906 my $self = shift (@_);
907 my ($filename, $encoding, $language, $textref) = @_;
908
909 if (!-r $filename)
910 {
911 my $outhandle = $self->{'outhandle'};
912 gsprintf($outhandle, "{BasPlug.read_denied}\n", $filename) if $self->{'verbosity'};
913 # print $outhandle "Read permission denied for $filename\n" if $self->{'verbosity'};
914 return;
915 }
916 $$textref = "";
917 if (!open (FILE, $filename)) {
918 gsprintf(STDERR, "BasPlug::read_file {BasPlug.could_not_open_for_reading} ($!)\n", $filename);
919 die "\n";
920 }
921
922 if ($encoding eq "ascii") {
923 undef $/;
924 $$textref = <FILE>;
925 $/ = "\n";
926 } else {
927 my $reader = new multiread();
928 $reader->set_handle ('BasPlug::FILE');
929 $reader->set_encoding ($encoding);
930 $reader->read_file ($textref);
931 #Now segments chinese if the separate_cjk option is set
932 if ($self->{'separate_cjk'}) {
933 # segment the Chinese words
934 $$textref = &cnseg::segment($$textref);
935 }
936 }
937 close FILE;
938}
939
940# write_file -- used by ConvertToPlug, for example in post processing
941#
942sub utf8_write_file {
943 my $self = shift (@_);
944 my ($textref, $filename) = @_;
945
946 if (!open (FILE, ">$filename")) {
947 gsprintf(STDERR, "ConvertToPlug::write_file {ConvertToPlug.could_not_open_for_writing} ($!)\n", $filename);
948 die "\n";
949 }
950 print FILE $$textref;
951
952 close FILE;
953}
954
955
956sub filename_based_title
957{
958 my $self = shift (@_);
959 my ($file) = @_;
960
961 my $file_derived_title = $file;
962 $file_derived_title =~ s/_/ /g;
963 $file_derived_title =~ s/\..*?$//;
964
965 return $file_derived_title;
966}
967
968
969sub title_fallback
970{
971 my $self = shift (@_);
972 my ($doc_obj,$section,$file) = @_;
973
974 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
975
976 my $file_derived_title = $self->filename_based_title($file);
977 $doc_obj->add_metadata ($section, "Title", $file_derived_title);
978 }
979}
980
981sub textcat_get_language_encoding {
982 my $self = shift (@_);
983 my ($filename) = @_;
984
985
986 my ($language, $encoding, $extracted_encoding);
987 if ($self->{'input_encoding'} eq "auto") {
988 # use textcat to automatically work out the input encoding and language
989 ($language, $encoding) = $self->get_language_encoding ($filename);
990 } elsif ($self->{'extract_language'}) {
991 # use textcat to get language metadata
992 ($language, $extracted_encoding) = $self->get_language_encoding ($filename);
993 $encoding = $self->{'input_encoding'};
994 # don't print this message for english... english in utf8 is identical
995 # to english in iso-8859-1 (except for some punctuation). We don't have
996 # a language model for en_utf8, so textcat always says iso-8859-1!
997 if ($extracted_encoding ne $encoding && $language ne "en"
998 && $self->{'verbosity'}) {
999 my $plugin_name = ref ($self);
1000 my $outhandle = $self->{'outhandle'};
1001 gsprintf($outhandle, "$plugin_name: {BasPlug.wrong_encoding}\n", $filename, $encoding, $extracted_encoding);
1002 # print $outhandle "$plugin_name: WARNING: $filename was read using $encoding encoding but ";
1003 # print $outhandle "appears to be encoded as $extracted_encoding.\n";
1004 }
1005 } else {
1006 $language = $self->{'default_language'};
1007 $encoding = $self->{'input_encoding'};
1008 }
1009
1010 return ($language, $encoding);
1011}
1012
1013# Uses textcat to work out the encoding and language of the text in
1014# $filename. All html tags are removed before processing.
1015# returns an array containing "language" and "encoding"
1016sub get_language_encoding {
1017 my $self = shift (@_);
1018 my ($filename) = @_;
1019 my $outhandle = $self->{'outhandle'};
1020 my $unicode_format = "";
1021 # read in file
1022 open (FILE, $filename) || (gsprintf(STDERR, "BasPlug::get_language_encoding {BasPlug.could_not_open_for_reading} ($!)\n", $filename) && die "\n"); # die "BasPlug::get_language_encoding could not open $filename for reading ($!)\n";
1023 undef $/;
1024 my $text = <FILE>;
1025 $/ = "\n";
1026 close FILE;
1027
1028 # check if first few bytes have a Byte Order Marker
1029 my $bom=substr($text,0,2); # check 16bit unicode
1030 if ($bom eq "\xff\xfe") { # little endian 16bit unicode
1031 $unicode_format="unicode";
1032 } elsif ($bom eq "\xfe\xff") { # big endian 16bit unicode
1033 $unicode_format="unicode";
1034 } else {
1035 $bom=substr($text,0,3); # check utf-8
1036 if ($bom eq "\xef\xbb\xbf") { # utf-8 coded FEFF bom
1037 $unicode_format="utf8";
1038# } elsif ($bom eq "\xef\xbf\xbe") { # utf-8 coded FFFE bom. Error!?
1039# $unicode_format="utf8";
1040 }
1041 }
1042
1043 # VB scripting generated Word to HTML file
1044 if ($text =~ /charset=(windows.*)[\"]/ig){
1045 my $vbhtml_encoding = $1;
1046 $vbhtml_encoding =~ s/-+/_/g;
1047 $self->{'input_encoding'} = $vbhtml_encoding;
1048 }
1049
1050 # remove <title>stuff</title> -- as titles tend often to be in English
1051 # for foreign language documents
1052 $text =~ s/<title>(.|\n)*?<\/title>//i;
1053
1054 # remove all HTML tags
1055 # XXX this doesn't match plugins derived from HTMLPlug (except ConvertTo)
1056 if (ref($self) eq 'HTMLPlug' ||
1057 (exists $self->{'converted_to'} && $self->{'converted_to'} eq 'HTML')){
1058 $text =~ s/<[^>]*>//sg;
1059 }
1060
1061 # get the language/encoding
1062 my $results = $self->{'textcat'}->classify(\$text);
1063
1064 # if textcat returns 3 or less possibilities we'll use the
1065 # first one in the list - otherwise use the defaults
1066 if (scalar @$results > 3) {
1067 my $best_encoding="";
1068 if ($unicode_format) { # in case the first had a BOM
1069 $best_encoding=$unicode_format;
1070 } else {
1071 my %guessed_encodings = ();
1072 foreach my $result (@$results) {
1073 $result =~ /([^\-]+)$/;
1074 my $enc=$1;
1075 if (!defined($guessed_encodings{$enc})) {
1076 $guessed_encodings{$enc}=0;
1077 }
1078 $guessed_encodings{$enc}++;
1079 }
1080
1081 $guessed_encodings{""}=-1; # for default best_encoding of ""
1082 foreach my $enc (keys %guessed_encodings) {
1083 if ($guessed_encodings{$enc} >
1084 $guessed_encodings{$best_encoding}){
1085 $best_encoding=$enc;
1086 }
1087 }
1088 }
1089
1090 if ($self->{'input_encoding'} ne 'auto') {
1091 if ($self->{'extract_language'} && ($self->{'verbosity'}>2)) {
1092 gsprintf($outhandle,
1093 "BasPlug: {BasPlug.could_not_extract_language}\n",
1094 $filename, $self->{'default_language'});
1095 }
1096 return ($self->{'default_language'}, $self->{'input_encoding'});
1097
1098 } else {
1099 if ($self->{'verbosity'}>2) {
1100 gsprintf($outhandle,
1101 "BasPlug: {BasPlug.could_not_extract_language}\n",
1102 $filename, $self->{'default_language'});
1103 }
1104 return ($self->{'default_language'}, $best_encoding);
1105 }
1106 }
1107
1108 # format language/encoding
1109 my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
1110 if (!defined $language) {
1111 if ($self->{'verbosity'}>2) {
1112 gsprintf($outhandle,
1113 "BasPlug: {BasPlug.could_not_extract_language}\n",
1114 $filename, $self->{'default_language'});
1115 }
1116 $language = $self->{'default_language'};
1117 }
1118 if (!defined $encoding) {
1119 if ($self->{'verbosity'}>2) {
1120 gsprintf($outhandle,
1121 "BasPlug: {BasPlug.could_not_extract_encoding}\n",
1122 $filename, $self->{'default_encoding'});
1123 }
1124 $encoding = $self->{'default_encoding'};
1125 }
1126
1127
1128 # check for equivalents where textcat doesn't have some encodings...
1129 # eg MS versions of standard encodings
1130 if ($encoding =~ /^iso_8859_(\d+)/) {
1131 my $iso = $1; # which variant of the iso standard?
1132 # iso-8859 sets don't use chars 0x80-0x9f, windows codepages do
1133 if ($text =~ /[\x80-\x9f]/) {
1134 # Western Europe
1135 if ($iso == 1 or $iso == 15) { $encoding = 'windows_1252' }
1136 elsif ($iso == 2) { $encoding = 'windows_1250' } # Central Europe
1137 elsif ($iso == 5) { $encoding = 'windows_1251' } # Cyrillic
1138 elsif ($iso == 6) { $encoding = 'windows_1256' } # Arabic
1139 elsif ($iso == 7) { $encoding = 'windows_1253' } # Greek
1140 elsif ($iso == 8) { $encoding = 'windows_1255' } # Hebrew
1141 elsif ($iso == 9) { $encoding = 'windows_1254' } # Turkish
1142 }
1143 }
1144
1145 if ($encoding !~ /^(ascii|utf8|unicode)$/ &&
1146 !defined $encodings::encodings->{$encoding}) {
1147 if ($self->{'verbosity'}) {
1148 gsprintf($outhandle, "BasPlug: {BasPlug.unsupported_encoding}\n",
1149 $filename, $encoding, $self->{'default_encoding'});
1150 }
1151 $encoding = $self->{'default_encoding'};
1152 }
1153
1154 return ($language, $encoding);
1155}
1156
1157# add any extra metadata that's been passed around from one
1158# plugin to another.
1159# extra_metadata uses add_utf8_metadata so it expects metadata values
1160# to already be in utf8
1161sub extra_metadata {
1162 my $self = shift (@_);
1163 my ($doc_obj, $cursection, $metadata) = @_;
1164
1165 foreach my $field (keys(%$metadata)) {
1166 # $metadata->{$field} may be an array reference
1167 if ($field eq "gsdlassocfile_tobe") {
1168 # 'gsdlassocfile_tobe' is artificially introduced metadata
1169 # that is used to signal that certain additional files should
1170 # be tied to this document. Useful in situations where a
1171 # metadata pass in the plugin pipeline works out some files
1172 # need to be associated with a document, but the document hasn't
1173 # been formed yet.
1174
1175 my $equiv_form = "";
1176 foreach my $gaf (@{$metadata->{$field}}) {
1177 my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
1178 my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
1179 my $filename = $full_filename;
1180
1181 $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
1182
1183 my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
1184 my $start_doclink = "<a href=\"_httpcollection_/index/assoc/{Or}{[parent(Top):archivedir],[archivedir]}/$tail_filename\">";
1185 my $srcicon = "_icon".$doc_ext."_";
1186 my $end_doclink = "</a>";
1187
1188 $equiv_form .= " $start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
1189 }
1190 $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
1191 }
1192 elsif (ref ($metadata->{$field}) eq "ARRAY") {
1193 map {
1194 $doc_obj->add_utf8_metadata ($cursection, $field, $_);
1195 } @{$metadata->{$field}};
1196 } else {
1197 $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
1198 }
1199 }
1200}
1201
1202# initialise metadata extractors
1203sub initialise_extractors {
1204 my $self = shift (@_);
1205
1206 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
1207 &acronym::initialise_acronyms();
1208 }
1209}
1210
1211# finalise metadata extractors
1212sub finalise_extractors {
1213 my $self = shift (@_);
1214
1215 if ($self->{'extract_acronyms'} || $self->{'markup_acronyms'}) {
1216 &acronym::finalise_acronyms();
1217 }
1218}
1219
1220# FIRSTNNN: extract the first NNN characters as metadata
1221sub extract_first_NNNN_characters {
1222 my $self = shift (@_);
1223 my ($textref, $doc_obj, $thissection) = @_;
1224
1225 foreach my $size (split /,/, $self->{'first'}) {
1226 my $tmptext = $$textref;
1227 $tmptext =~ s/^\s+//;
1228 $tmptext =~ s/\s+$//;
1229 $tmptext =~ s/\s+/ /gs;
1230 $tmptext = substr ($tmptext, 0, $size);
1231 $tmptext =~ s/\s\S*$/&#8230;/;
1232 $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
1233 }
1234}
1235
1236sub extract_email {
1237 my $self = shift (@_);
1238 my ($textref, $doc_obj, $thissection) = @_;
1239 my $outhandle = $self->{'outhandle'};
1240
1241 gsprintf($outhandle, " {BasPlug.extracting_emails}...\n")
1242 if ($self->{'verbosity'} > 2);
1243
1244 my @email = ($$textref =~ m/([-a-z0-9\.@+_=]+@(?:[-a-z0-9]+\.)+(?:com|org|edu|mil|int|net|[a-z][a-z]))/g);
1245 @email = sort @email;
1246
1247# if($self->{"new_extract_email"} == 0)
1248# {
1249# my @email2 = ();
1250# foreach my $address (@email)
1251# {
1252# if (!(join(" ",@email2) =~ m/(^| )$address( |$)/ ))
1253# {
1254# push @email2, $address;
1255# $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
1256# # print $outhandle " extracting $address\n"
1257# &gsprintf($outhandle, " {BasPlug.extracting} $address\n")
1258# if ($self->{'verbosity'} > 3);
1259# }
1260# }
1261# }
1262# else
1263# {
1264 my $hashExistMail = {};
1265 foreach my $address (@email) {
1266 if (!(defined $hashExistMail->{$address}))
1267 {
1268 $hashExistMail->{$address} = 1;
1269 $doc_obj->add_utf8_metadata ($thissection, "emailAddress", $address);
1270 gsprintf($outhandle, " {BasPlug.extracting} $address\n")
1271 if ($self->{'verbosity'} > 3);
1272 }
1273 }
1274 gsprintf($outhandle, " {BasPlug.done_email_extract}\n")
1275 if ($self->{'verbosity'} > 2);
1276}
1277
1278# extract metadata
1279sub auto_extract_metadata {
1280
1281 my $self = shift (@_);
1282 my ($doc_obj) = @_;
1283
1284 if ($self->{'extract_email'}) {
1285 my $thissection = $doc_obj->get_top_section();
1286 while (defined $thissection) {
1287 my $text = $doc_obj->get_text($thissection);
1288 $self->extract_email (\$text, $doc_obj, $thissection) if $text =~ /./;
1289 $thissection = $doc_obj->get_next_section ($thissection);
1290 }
1291 }
1292 if ($self->{'extract_placenames'}) {
1293 my $thissection = $doc_obj->get_top_section();
1294 while (defined $thissection) {
1295 my $text = $doc_obj->get_text($thissection);
1296 $self->extract_placenames (\$text, $doc_obj, $thissection) if $text =~ /./;
1297 $thissection = $doc_obj->get_next_section ($thissection);
1298 }
1299 }
1300
1301 #adding kea keyphrases
1302 if ($self->{'extract_keyphrases'}) {
1303
1304 my $thissection = $doc_obj->get_top_section();
1305 my $text = "";
1306 my $list;
1307
1308 #loop through sections to gather whole doc
1309 while (defined $thissection) {
1310 my $sectiontext = $doc_obj->get_text($thissection);
1311 $text = $text.$sectiontext;
1312 $thissection = $doc_obj->get_next_section ($thissection);
1313 }
1314
1315 if($self->{'extract_keyphrase_options'}) { #if kea options flag is set, call Kea with specified options
1316 $list = &Kea::extract_KeyPhrases ($text, $self->{'extract_keyphrase_options'});
1317 } else { #otherwise call Kea with no options
1318 $list = &Kea::extract_KeyPhrases ($text);
1319 }
1320
1321 if ($list){
1322 # if a list of kea keyphrases was returned (ie not empty)
1323 if ($self->{'verbosity'}) {
1324 gsprintf(STDERR, "{BasPlug.keyphrases}: $list\n");
1325 }
1326
1327 #add metadata to top section
1328 $thissection = $doc_obj->get_top_section();
1329
1330 # add all key phrases as one metadata
1331 $doc_obj->add_metadata($thissection, "Keyphrases", $list);
1332
1333 # add individual key phrases as multiple metadata
1334 foreach my $keyphrase (split(',', $list)) {
1335 $keyphrase =~ s/^\s+|\s+$//g;
1336 $doc_obj->add_metadata($thissection, "Keyphrase", $keyphrase);
1337 }
1338 }
1339 } #end of kea
1340
1341 if ($self->{'first'}) {
1342 my $thissection = $doc_obj->get_top_section();
1343 while (defined $thissection) {
1344 my $text = $doc_obj->get_text($thissection);
1345 $self->extract_first_NNNN_characters (\$text, $doc_obj, $thissection) if $text =~ /./;
1346 $thissection = $doc_obj->get_next_section ($thissection);
1347 }
1348 }
1349
1350 if ($self->{'extract_acronyms'}) {
1351 my $thissection = $doc_obj->get_top_section();
1352 while (defined $thissection) {
1353 my $text = $doc_obj->get_text($thissection);
1354 $self->extract_acronyms (\$text, $doc_obj, $thissection) if $text =~ /./;
1355 $thissection = $doc_obj->get_next_section ($thissection);
1356 }
1357 }
1358
1359 if ($self->{'markup_acronyms'}) {
1360 my $thissection = $doc_obj->get_top_section();
1361 while (defined $thissection) {
1362 my $text = $doc_obj->get_text($thissection);
1363 $text = $self->markup_acronyms ($text, $doc_obj, $thissection);
1364 $doc_obj->delete_text($thissection);
1365 $doc_obj->add_text($thissection, $text);
1366 $thissection = $doc_obj->get_next_section ($thissection);
1367 }
1368 }
1369
1370 if($self->{'extract_historical_years'}) {
1371 my $thissection = $doc_obj->get_top_section();
1372 while (defined $thissection) {
1373
1374 my $text = $doc_obj->get_text($thissection);
1375 &DateExtract::get_date_metadata($text, $doc_obj,
1376 $thissection,
1377 $self->{'no_bibliography'},
1378 $self->{'maximum_year'},
1379 $self->{'maximum_century'});
1380 $thissection = $doc_obj->get_next_section ($thissection);
1381 }
1382 }
1383}
1384
1385# extract acronyms from a section in a document. progress is
1386# reported to outhandle based on the verbosity. both the Acronym
1387# and the AcronymKWIC metadata items are created.
1388
1389sub extract_acronyms {
1390 my $self = shift (@_);
1391 my ($textref, $doc_obj, $thissection) = @_;
1392 my $outhandle = $self->{'outhandle'};
1393
1394 # print $outhandle " extracting acronyms ...\n"
1395 gsprintf($outhandle, " {BasPlug.extracting_acronyms}...\n")
1396 if ($self->{'verbosity'} > 2);
1397
1398 my $acro_array = &acronym::acronyms($textref);
1399
1400 foreach my $acro (@$acro_array) {
1401
1402 #check that this is the first time ...
1403 my $seen_before = "false";
1404 my $previous_data = $doc_obj->get_metadata($thissection, "Acronym");
1405 foreach my $thisAcro (@$previous_data) {
1406 if ($thisAcro eq $acro->to_string()) {
1407 $seen_before = "true";
1408 if ($self->{'verbosity'} >= 4) {
1409 gsprintf($outhandle, " {BasPlug.already_seen} " .
1410 $acro->to_string() . "\n");
1411 }
1412 }
1413 }
1414
1415 if ($seen_before eq "false") {
1416 #write it to the file ...
1417 $acro->write_to_file();
1418
1419 #do the normal acronym
1420 $doc_obj->add_utf8_metadata($thissection, "Acronym", $acro->to_string());
1421 gsprintf($outhandle, " {BasPlug.adding} ".$acro->to_string()."\n")
1422 if ($self->{'verbosity'} > 3);
1423 }
1424 }
1425
1426 gsprintf($outhandle, " {BasPlug.done_acronym_extract}\n")
1427 if ($self->{'verbosity'} > 2);
1428}
1429
1430sub markup_acronyms {
1431 my $self = shift (@_);
1432 my ($text, $doc_obj, $thissection) = @_;
1433 my $outhandle = $self->{'outhandle'};
1434
1435 gsprintf($outhandle, " {BasPlug.marking_up_acronyms}...\n")
1436 if ($self->{'verbosity'} > 2);
1437
1438 #self is passed in to check for verbosity ...
1439 $text = &acronym::markup_acronyms($text, $self);
1440
1441 gsprintf($outhandle, " {BasPlug.done_acronym_markup}\n")
1442 if ($self->{'verbosity'} > 2);
1443
1444 return $text;
1445}
1446
1447sub compile_stats {
1448 my $self = shift(@_);
1449 my ($stats) = @_;
1450
1451 $stats->{'num_processed'} += $self->{'num_processed'};
1452 $stats->{'num_not_processed'} += $self->{'num_not_processed'};
1453 $stats->{'num_archives'} += $self->{'num_archives'};
1454
1455}
1456
1457sub associate_cover_image {
1458 my $self = shift;
1459 my ($doc_obj, $filename) = @_;
1460
1461 $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
1462 if (exists $self->{'covers_missing_cache'}->{$filename}) {
1463 # don't stat() for existence eg for multiple document input files
1464 # (eg SplitPlug)
1465 return;
1466 }
1467
1468 my $top_section=$doc_obj->get_top_section();
1469
1470 if (-e $filename) {
1471 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
1472 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1473 } else {
1474 my $upper_filename = $filename;
1475 $upper_filename =~ s/jpg$/JPG/;
1476 if (-e $upper_filename) {
1477 $doc_obj->associate_file($upper_filename, "cover.jpg",
1478 "image/jpeg");
1479 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
1480 } else {
1481 # file doesn't exist, so record the fact that it's missing so
1482 # we don't stat() again (stat is slow)
1483 $self->{'covers_missing_cache'}->{$filename} = 1;
1484 }
1485 }
1486
1487}
1488
14891;
Note: See TracBrowser for help on using the repository browser.