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