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

Last change on this file since 12270 was 12270, checked in by kjdon, 18 years ago

set_OIDtype now takes two arguments, the type and the metadata (used if type=assigned)

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