1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # BasePlugin.pm -- base class for all the import plugins
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999-2005 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 | package BasePlugin;
|
---|
27 |
|
---|
28 | use strict;
|
---|
29 | no strict 'subs';
|
---|
30 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
31 |
|
---|
32 | use File::Basename;
|
---|
33 |
|
---|
34 | use encodings;
|
---|
35 | use unicode;
|
---|
36 | use textcat;
|
---|
37 | use doc;
|
---|
38 | use ghtml;
|
---|
39 | use gsprintf 'gsprintf';
|
---|
40 |
|
---|
41 | use PrintInfo;
|
---|
42 |
|
---|
43 | BEGIN {
|
---|
44 | @BasePlugin::ISA = ( 'PrintInfo' );
|
---|
45 | }
|
---|
46 |
|
---|
47 | our $encoding_list =
|
---|
48 | [ { 'name' => "ascii",
|
---|
49 | 'desc' => "{BasePlugin.encoding.ascii}" },
|
---|
50 | { 'name' => "utf8",
|
---|
51 | 'desc' => "{BasePlugin.encoding.utf8}" },
|
---|
52 | { 'name' => "unicode",
|
---|
53 | 'desc' => "{BasePlugin.encoding.unicode}" } ];
|
---|
54 |
|
---|
55 |
|
---|
56 | my $e = $encodings::encodings;
|
---|
57 | foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
|
---|
58 | {
|
---|
59 | my $hashEncode =
|
---|
60 | {'name' => $enc,
|
---|
61 | 'desc' => $e->{$enc}->{'name'}};
|
---|
62 |
|
---|
63 | push(@{$encoding_list},$hashEncode);
|
---|
64 | }
|
---|
65 |
|
---|
66 | our $encoding_plus_auto_list =
|
---|
67 | [ { 'name' => "auto",
|
---|
68 | 'desc' => "{BasePlugin.filename_encoding.auto}" },
|
---|
69 | { 'name' => "auto-language-analysis",
|
---|
70 | 'desc' => "{BasePlugin.filename_encoding.auto_language_analysis}" }, # textcat
|
---|
71 | { 'name' => "auto-filesystem-encoding",
|
---|
72 | 'desc' => "{BasePlugin.filename_encoding.auto_filesystem_encoding}" }, # locale
|
---|
73 | { 'name' => "auto-fl",
|
---|
74 | 'desc' => "{BasePlugin.filename_encoding.auto_fl}" }, # locale followed by textcat
|
---|
75 | { 'name' => "auto-lf",
|
---|
76 | 'desc' => "{BasePlugin.filename_encoding.auto_lf}" } ]; # texcat followed by locale
|
---|
77 |
|
---|
78 | push(@{$encoding_plus_auto_list},@{$encoding_list});
|
---|
79 |
|
---|
80 | our $oidtype_list =
|
---|
81 | [ { 'name' => "auto",
|
---|
82 | 'desc' => "{BasePlugin.OIDtype.auto}" },
|
---|
83 | { 'name' => "hash",
|
---|
84 | 'desc' => "{import.OIDtype.hash}" },
|
---|
85 | { 'name' => "assigned",
|
---|
86 | 'desc' => "{import.OIDtype.assigned}" },
|
---|
87 | { 'name' => "incremental",
|
---|
88 | 'desc' => "{import.OIDtype.incremental}" },
|
---|
89 | { 'name' => "dirname",
|
---|
90 | 'desc' => "{import.OIDtype.dirname}" } ];
|
---|
91 |
|
---|
92 | my $arguments =
|
---|
93 | [ { 'name' => "process_exp",
|
---|
94 | 'desc' => "{BasePlugin.process_exp}",
|
---|
95 | 'type' => "regexp",
|
---|
96 | 'deft' => "",
|
---|
97 | 'reqd' => "no" },
|
---|
98 | { 'name' => "no_blocking",
|
---|
99 | 'desc' => "{BasePlugin.no_blocking}",
|
---|
100 | 'type' => "flag",
|
---|
101 | 'reqd' => "no"},
|
---|
102 | { 'name' => "block_exp",
|
---|
103 | 'desc' => "{BasePlugin.block_exp}",
|
---|
104 | 'type' => "regexp",
|
---|
105 | 'deft' => "",
|
---|
106 | 'reqd' => "no" },
|
---|
107 | { 'name' => "associate_ext",
|
---|
108 | 'desc' => "{BasePlugin.associate_ext}",
|
---|
109 | 'type' => "string",
|
---|
110 | 'reqd' => "no" },
|
---|
111 | { 'name' => "associate_tail_re",
|
---|
112 | 'desc' => "{BasePlugin.associate_tail_re}",
|
---|
113 | 'type' => "string",
|
---|
114 | 'reqd' => "no" },
|
---|
115 | { 'name' => "OIDtype",
|
---|
116 | 'desc' => "{import.OIDtype}",
|
---|
117 | 'type' => "enum",
|
---|
118 | 'list' => $oidtype_list,
|
---|
119 | # leave default empty so we can tell if its been set or not - if not set will use option from import.pl
|
---|
120 | 'deft' => "auto",
|
---|
121 | 'reqd' => "no",
|
---|
122 | 'modegli' => "2" },
|
---|
123 | { 'name' => "OIDmetadata",
|
---|
124 | 'desc' => "{import.OIDmetadata}",
|
---|
125 | 'type' => "metadata",
|
---|
126 | 'deft' => "dc.Identifier",
|
---|
127 | 'reqd' => "no",
|
---|
128 | 'modegli' => "2" },
|
---|
129 |
|
---|
130 | # { 'name' => "use_as_doc_identifier",
|
---|
131 | # 'desc' => "{BasePlugin.use_as_doc_identifier}",
|
---|
132 | # 'type' => "string",
|
---|
133 | # 'reqd' => "no" ,
|
---|
134 | # 'deft' => "" } ,
|
---|
135 | { 'name' => "no_cover_image",
|
---|
136 | 'desc' => "{BasePlugin.no_cover_image}",
|
---|
137 | 'type' => "flag",
|
---|
138 | 'reqd' => "no" },
|
---|
139 | { 'name' => "filename_encoding",
|
---|
140 | 'desc' => "{BasePlugin.filename_encoding}",
|
---|
141 | 'type' => "enum",
|
---|
142 | 'deft' => "auto",
|
---|
143 | 'list' => $encoding_plus_auto_list,
|
---|
144 | 'reqd' => "no" },
|
---|
145 | { 'name' => "smart_block",
|
---|
146 | 'desc' => "{common.deprecated}. {BasePlugin.smart_block}",
|
---|
147 | 'type' => "flag",
|
---|
148 | 'reqd' => "no",
|
---|
149 | 'hiddengli' => "yes" } # deprecated, but leave in for old collections
|
---|
150 |
|
---|
151 |
|
---|
152 | ];
|
---|
153 |
|
---|
154 |
|
---|
155 | my $options = { 'name' => "BasePlugin",
|
---|
156 | 'desc' => "{BasePlugin.desc}",
|
---|
157 | 'abstract' => "yes",
|
---|
158 | 'inherits' => "no",
|
---|
159 | 'args' => $arguments };
|
---|
160 |
|
---|
161 |
|
---|
162 | sub new {
|
---|
163 |
|
---|
164 | my ($class) = shift (@_);
|
---|
165 | my ($pluginlist,$inputargs,$hashArgOptLists,$auxiliary) = @_;
|
---|
166 | push(@$pluginlist, $class);
|
---|
167 |
|
---|
168 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
169 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
170 |
|
---|
171 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists,$auxiliary);
|
---|
172 |
|
---|
173 | if ($self->{'info_only'}) {
|
---|
174 | # don't worry about any options etc
|
---|
175 | return bless $self, $class;
|
---|
176 | }
|
---|
177 |
|
---|
178 | if ($self->{'smart_block'}) {
|
---|
179 | print STDERR "WARNING: -smart_block option has been deprecated and is no longer useful\n";
|
---|
180 | }
|
---|
181 | $self->{'smart_block'} = undef;
|
---|
182 |
|
---|
183 | my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
|
---|
184 | $self->{'plugin_type'} = $plugin_name;
|
---|
185 |
|
---|
186 | $self->{'num_processed'} = 0;
|
---|
187 | $self->{'num_not_processed'} = 0;
|
---|
188 | $self->{'num_blocked'} = 0;
|
---|
189 | $self->{'num_archives'} = 0;
|
---|
190 | $self->{'cover_image'} = 1; # cover image is on by default
|
---|
191 | $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
|
---|
192 | #$self->{'option_list'} = $hashArgOptLists->{"OptList"};
|
---|
193 |
|
---|
194 | my $associate_ext = $self->{'associate_ext'};
|
---|
195 | if ((defined $associate_ext) && ($associate_ext ne "")) {
|
---|
196 |
|
---|
197 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
198 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
199 | my $outhandle = $self->{'outhandle'};
|
---|
200 | print $outhandle "Warning: can only specify 'associate_ext' or 'associate_tail_re'\n";
|
---|
201 | print $outhandle " defaulting to 'associate_tail_re'\n";
|
---|
202 | }
|
---|
203 | else {
|
---|
204 | my @exts = split(/,/,$associate_ext);
|
---|
205 |
|
---|
206 | my @exts_bracketed = map { $_ = "(?:\\.$_)" } @exts;
|
---|
207 | my $associate_tail_re = join("|",@exts_bracketed);
|
---|
208 | $self->{'associate_tail_re'} = $associate_tail_re;
|
---|
209 | }
|
---|
210 |
|
---|
211 | delete $self->{'associate_ext'};
|
---|
212 | }
|
---|
213 |
|
---|
214 | return bless $self, $class;
|
---|
215 |
|
---|
216 | }
|
---|
217 |
|
---|
218 | sub merge_inheritance
|
---|
219 | {
|
---|
220 | my $self = {};
|
---|
221 | my @child_selfs = @_;
|
---|
222 |
|
---|
223 | foreach my $child_self (@child_selfs) {
|
---|
224 | foreach my $key (keys %$child_self) {
|
---|
225 | if (defined $self->{$key}) {
|
---|
226 | if ($self->{$key} ne $child_self->{$key}) {
|
---|
227 | # print STDERR "Warning: Conflicting value in multiple inheritance for '$key'\n";
|
---|
228 | # print STDERR "Existing stored value = $self->{$key}\n";
|
---|
229 | # print STDERR "New (child) value = $child_self->{$key}\n";
|
---|
230 | # print STDERR "Keeping existing value\n";
|
---|
231 | # Existing value seems to be option specified in collect.cfg
|
---|
232 |
|
---|
233 | ### $self->{$key} = $child_self->{$key};
|
---|
234 |
|
---|
235 | }
|
---|
236 | else {
|
---|
237 | ## print STDERR "****Info: Value $self->{$key} for $key already defined through multiple inheritance as the same value\n";
|
---|
238 | }
|
---|
239 |
|
---|
240 | }
|
---|
241 | else {
|
---|
242 | $self->{$key} = $child_self->{$key};
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | return $self;
|
---|
248 | }
|
---|
249 |
|
---|
250 | # initialize BasePlugin options
|
---|
251 | # if init() is overridden in a sub-class, remember to call BasePlugin::init()
|
---|
252 | sub init {
|
---|
253 | my $self = shift (@_);
|
---|
254 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
255 |
|
---|
256 | # verbosity is passed through from the processor
|
---|
257 | $self->{'verbosity'} = $verbosity;
|
---|
258 |
|
---|
259 | # as are the outhandle and failhandle
|
---|
260 | $self->{'outhandle'} = $outhandle if defined $outhandle;
|
---|
261 | $self->{'failhandle'} = $failhandle;
|
---|
262 | # $self->SUPER::init(@_);
|
---|
263 |
|
---|
264 | # set process_exp and block_exp to defaults unless they were
|
---|
265 | # explicitly set
|
---|
266 |
|
---|
267 | if ((!$self->is_recursive()) and
|
---|
268 | (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
|
---|
269 |
|
---|
270 | $self->{'process_exp'} = $self->get_default_process_exp ();
|
---|
271 | if ($self->{'process_exp'} eq "") {
|
---|
272 | warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
|
---|
277 | $self->{'block_exp'} = $self->get_default_block_exp ();
|
---|
278 | }
|
---|
279 |
|
---|
280 | }
|
---|
281 |
|
---|
282 | sub begin {
|
---|
283 | my $self = shift (@_);
|
---|
284 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
285 |
|
---|
286 | if ($self->{'OIDtype'} eq "auto") {
|
---|
287 | # hasn't been set in the plugin, use the processor values
|
---|
288 | $self->{'OIDtype'} = $processor->{'OIDtype'};
|
---|
289 | $self->{'OIDmetadata'} = $processor->{'OIDmetadata'};
|
---|
290 | }
|
---|
291 | if ($self->{'OIDtype'} eq "hash") {
|
---|
292 | # should we hash on the file or on the doc xml??
|
---|
293 | $self->{'OIDtype'} = $self->get_oid_hash_type();
|
---|
294 | if ($self->{'OIDtype'} !~ /^(hash_on_file|hash_on_ga_xml)$/) {
|
---|
295 | $self->{'OIDtype'} = "hash_on_file";
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | sub end {
|
---|
301 | # potentially called at the end of each plugin pass
|
---|
302 | # import.pl only has one plugin pass, but buildcol.pl has multiple ones
|
---|
303 |
|
---|
304 | my ($self) = shift (@_);
|
---|
305 | }
|
---|
306 |
|
---|
307 | sub deinit {
|
---|
308 | # called only once, after all plugin passes have been done
|
---|
309 |
|
---|
310 | my ($self) = @_;
|
---|
311 | }
|
---|
312 |
|
---|
313 | # default hashing type is to hash on the original file (or converted file)
|
---|
314 | # override this to return hash_on_ga_xml for filetypes where hashing on the
|
---|
315 | # file is no good eg video
|
---|
316 | sub get_oid_hash_type {
|
---|
317 |
|
---|
318 | my $self = shift (@_);
|
---|
319 |
|
---|
320 | return "hash_on_file";
|
---|
321 | }
|
---|
322 |
|
---|
323 | sub set_incremental {
|
---|
324 | my $self = shift(@_);
|
---|
325 | my ($incremental) = @_;
|
---|
326 |
|
---|
327 | $self->{'incremental'} = $incremental;
|
---|
328 | }
|
---|
329 |
|
---|
330 | # this function should be overridden to return 1
|
---|
331 | # in recursive plugins
|
---|
332 | sub is_recursive {
|
---|
333 | my $self = shift (@_);
|
---|
334 |
|
---|
335 | return 0;
|
---|
336 | }
|
---|
337 |
|
---|
338 | sub get_default_block_exp {
|
---|
339 | my $self = shift (@_);
|
---|
340 |
|
---|
341 | return "";
|
---|
342 | }
|
---|
343 |
|
---|
344 | sub get_default_process_exp {
|
---|
345 | my $self = shift (@_);
|
---|
346 |
|
---|
347 | return "";
|
---|
348 | }
|
---|
349 |
|
---|
350 | # default implementation is to do nothing
|
---|
351 | sub store_block_files {
|
---|
352 |
|
---|
353 | my $self =shift (@_);
|
---|
354 | my ($filename_full_path, $block_hash) = @_;
|
---|
355 |
|
---|
356 | }
|
---|
357 |
|
---|
358 | # put files to block into hash
|
---|
359 | sub use_block_expressions {
|
---|
360 |
|
---|
361 | my $self =shift (@_);
|
---|
362 | my ($filename_full_path, $block_hash) = @_;
|
---|
363 |
|
---|
364 | if ($self->{'block_exp'} ne "" && $filename_full_path =~ /$self->{'block_exp'}/) {
|
---|
365 | $block_hash->{'file_blocks'}->{$filename_full_path} = 1;
|
---|
366 | }
|
---|
367 |
|
---|
368 | }
|
---|
369 |
|
---|
370 | #default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
|
---|
371 | sub block_cover_image
|
---|
372 | {
|
---|
373 | my $self =shift;
|
---|
374 | my ($filename, $block_hash) = @_;
|
---|
375 |
|
---|
376 | if ($self->{'cover_image'}) {
|
---|
377 | my $coverfile = $filename;
|
---|
378 | $coverfile =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
379 | if (!-e $coverfile) {
|
---|
380 | $coverfile =~ s/jpg$/JPG/;
|
---|
381 | }
|
---|
382 | if (-e $coverfile) {
|
---|
383 | $block_hash->{'file_blocks'}->{$coverfile} = 1;
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | return;
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 | # discover all the files that should be blocked by this plugin
|
---|
392 | # check the args ...
|
---|
393 | sub file_block_read {
|
---|
394 |
|
---|
395 | my $self = shift (@_);
|
---|
396 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
397 | # Keep track of filenames with same root but different extensions
|
---|
398 | # Used to support -associate_ext and the more generalised
|
---|
399 | # -associate_tail_re
|
---|
400 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
401 |
|
---|
402 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
403 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
404 | my ($file_prefix,$file_ext)
|
---|
405 | = &util::get_prefix_and_tail_by_regex($filename_full_path,$associate_tail_re);
|
---|
406 | if ((defined $file_prefix) && (defined $file_ext)) {
|
---|
407 | my $shared_fileroot = $block_hash->{'shared_fileroot'};
|
---|
408 | if (!defined $shared_fileroot->{$file_prefix}) {
|
---|
409 | my $file_prefix_rec = { 'tie_to' => undef,
|
---|
410 | 'exts' => {} };
|
---|
411 | $shared_fileroot->{$file_prefix} = $file_prefix_rec;
|
---|
412 | }
|
---|
413 |
|
---|
414 | my $file_prefix_rec = $shared_fileroot->{$file_prefix};
|
---|
415 |
|
---|
416 | if ($self->can_process_this_file($filename_full_path)) {
|
---|
417 | # This is the document the others should be tied to
|
---|
418 | $file_prefix_rec->{'tie_to'} = $file_ext;
|
---|
419 | }
|
---|
420 | else {
|
---|
421 | if ($file_ext =~ m/$associate_tail_re$/) {
|
---|
422 | # this file should be associated to the main one
|
---|
423 | $file_prefix_rec->{'exts'}->{$file_ext} = 1;
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | }
|
---|
428 | }
|
---|
429 |
|
---|
430 | # check block expressions
|
---|
431 | $self->use_block_expressions($filename_full_path, $block_hash) unless $self->{'no_blocking'};
|
---|
432 |
|
---|
433 | # now check whether we are actually processing this
|
---|
434 | if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
|
---|
435 | return undef; # can't recognise
|
---|
436 | }
|
---|
437 |
|
---|
438 | # if we have a block_exp, then this overrides the normal 'smart' blocking
|
---|
439 | $self->store_block_files($filename_full_path, $block_hash) unless ($self->{'no_blocking'} || $self->{'block_exp'} ne "");
|
---|
440 |
|
---|
441 | # block the cover image if there is one
|
---|
442 | if ($self->{'cover_image'}) {
|
---|
443 | $self->block_cover_image($filename_full_path, $block_hash);
|
---|
444 | }
|
---|
445 |
|
---|
446 | return 1;
|
---|
447 | }
|
---|
448 |
|
---|
449 | # plugins that rely on more than process_exp (eg XML plugins) can override this method
|
---|
450 | sub can_process_this_file {
|
---|
451 | my $self = shift(@_);
|
---|
452 | my ($filename) = @_;
|
---|
453 |
|
---|
454 | if ($self->{'process_exp'} ne "" && $filename =~ /$self->{'process_exp'}/) {
|
---|
455 | return 1;
|
---|
456 | }
|
---|
457 | return 0;
|
---|
458 |
|
---|
459 | }
|
---|
460 |
|
---|
461 | # just converts path as is to utf8.
|
---|
462 | sub filepath_to_utf8 {
|
---|
463 | my $self = shift (@_);
|
---|
464 | my ($file, $file_encoding) = @_;
|
---|
465 | my $filemeta = $file;
|
---|
466 |
|
---|
467 | my $filename_encoding = $self->{'filename_encoding'}; # filename encoding setting
|
---|
468 |
|
---|
469 | # Whenever filename-encoding is set to any of the auto settings, we
|
---|
470 | # check if the filename is already in UTF8. If it is, then we're done.
|
---|
471 | if($filename_encoding =~ m/auto/) {
|
---|
472 | if(&unicode::check_is_utf8($filemeta))
|
---|
473 | {
|
---|
474 | $filename_encoding = "utf8";
|
---|
475 | return $filemeta;
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | # Auto setting, but filename is not utf8
|
---|
480 | if ($filename_encoding eq "auto")
|
---|
481 | {
|
---|
482 | # try textcat
|
---|
483 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
484 |
|
---|
485 | # check the locale next
|
---|
486 | $filename_encoding = $self->locale_encoding() if $filename_encoding eq "undefined";
|
---|
487 |
|
---|
488 |
|
---|
489 | # now try the encoding of the document, if available
|
---|
490 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
491 | $filename_encoding = $file_encoding;
|
---|
492 | }
|
---|
493 |
|
---|
494 | }
|
---|
495 |
|
---|
496 | elsif ($filename_encoding eq "auto-language-analysis")
|
---|
497 | {
|
---|
498 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
499 |
|
---|
500 | # now try the encoding of the document, if available
|
---|
501 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
502 | $filename_encoding = $file_encoding;
|
---|
503 | }
|
---|
504 | }
|
---|
505 |
|
---|
506 | elsif ($filename_encoding eq "auto-filesystem-encoding")
|
---|
507 | {
|
---|
508 | # try locale
|
---|
509 | $filename_encoding = $self->locale_encoding();
|
---|
510 | }
|
---|
511 |
|
---|
512 | elsif ($filename_encoding eq "auto-fl")
|
---|
513 | {
|
---|
514 | # filesystem-encoding (locale) then language-analysis (textcat)
|
---|
515 | $filename_encoding = $self->locale_encoding();
|
---|
516 |
|
---|
517 | # try textcat
|
---|
518 | $filename_encoding = $self->textcat_encoding($filemeta) if $filename_encoding eq "undefined";
|
---|
519 |
|
---|
520 | # else assume filename encoding is encoding of file content, if that's available
|
---|
521 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
522 | $filename_encoding = $file_encoding;
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | elsif ($filename_encoding eq "auto-lf")
|
---|
527 | {
|
---|
528 | # language-analysis (textcat) then filesystem-encoding (locale)
|
---|
529 | $filename_encoding = $self->textcat_encoding($filemeta);
|
---|
530 |
|
---|
531 | # guess filename encoding from encoding of file content, if available
|
---|
532 | if ($filename_encoding eq "undefined" && defined $file_encoding) {
|
---|
533 | $filename_encoding = $file_encoding;
|
---|
534 | }
|
---|
535 |
|
---|
536 | # try locale
|
---|
537 | $filename_encoding = $self->locale_encoding() if $filename_encoding eq "undefined";
|
---|
538 | }
|
---|
539 |
|
---|
540 | # if still undefined, use utf8 as fallback
|
---|
541 | if ($filename_encoding eq "undefined") {
|
---|
542 | $filename_encoding = "utf8";
|
---|
543 | }
|
---|
544 |
|
---|
545 | #print STDERR "**** UTF8 encoding the filename $filemeta ";
|
---|
546 |
|
---|
547 | # if the filename encoding is set to utf8 but it isn't utf8 already--such as when
|
---|
548 | # 1. the utf8 fallback is used, or 2. if the system locale is used and happens to
|
---|
549 | # be always utf8 (in which case the filename's encoding is also set as utf8 even
|
---|
550 | # though the filename need not be if it originates from another system)--in such
|
---|
551 | # cases attempt to make the filename utf8 to match.
|
---|
552 | if($filename_encoding eq "utf8" && !&unicode::check_is_utf8($filemeta)) {
|
---|
553 | &unicode::ensure_utf8(\$filemeta);
|
---|
554 | }
|
---|
555 |
|
---|
556 | # convert non-unicode encodings to utf8
|
---|
557 | if ($filename_encoding !~ m/(?:ascii|utf8|unicode)/) {
|
---|
558 | $filemeta = &unicode::unicode2utf8(
|
---|
559 | &unicode::convert2unicode($filename_encoding, \$filemeta)
|
---|
560 | );
|
---|
561 | }
|
---|
562 |
|
---|
563 | #print STDERR " from encoding $filename_encoding -> $filemeta\n";
|
---|
564 | return $filemeta;
|
---|
565 | }
|
---|
566 |
|
---|
567 | # gets the filename with no path, converts to utf8, and then dm safes it.
|
---|
568 | #filename_encoding set by user
|
---|
569 | sub filename_to_utf8_metadata
|
---|
570 | {
|
---|
571 | my $self = shift (@_);
|
---|
572 | my ($file, $file_encoding) = @_;
|
---|
573 |
|
---|
574 | my $outhandle = $self->{'outhandle'};
|
---|
575 |
|
---|
576 | my ($filemeta) = $file =~ /([^\\\/]+)$/; # getting the tail of the filepath (skips all string parts containing slashes upto the end)
|
---|
577 | $filemeta = $self->filepath_to_utf8($filemeta, $file_encoding);
|
---|
578 |
|
---|
579 | my $dmsafe_filemeta = &ghtml::dmsafe($filemeta);
|
---|
580 |
|
---|
581 | return $dmsafe_filemeta;
|
---|
582 |
|
---|
583 | }
|
---|
584 |
|
---|
585 | sub locale_encoding {
|
---|
586 | my $self = shift(@_);
|
---|
587 |
|
---|
588 | if (!defined $self->{'filesystem_encoding'}) {
|
---|
589 | $self->{'filesystem_encoding'} = $self->get_filesystem_encoding();
|
---|
590 | $self->{'filesystem_encoding'} = "undefined" if !defined $self->{'filesystem_encoding'};
|
---|
591 | }
|
---|
592 |
|
---|
593 | #print STDERR "*** filename encoding determined based on locale: " . $self->{'filesystem_encoding'} . "\n";
|
---|
594 | return $self->{'filesystem_encoding'}; # can be the string "undefined"
|
---|
595 | }
|
---|
596 |
|
---|
597 | sub textcat_encoding {
|
---|
598 | my $self = shift(@_);
|
---|
599 | my ($filemeta) = @_;
|
---|
600 |
|
---|
601 | # analyse filenames without extensions and digits (and trimmed of surrounding
|
---|
602 | # whitespace), so that irrelevant chars don't confuse textcat
|
---|
603 | my $strictfilemeta = $filemeta;
|
---|
604 | $strictfilemeta =~ s/\.[^\.]+$//g;
|
---|
605 | $strictfilemeta =~ s/\d//g;
|
---|
606 | $strictfilemeta =~ s/^\s*//g;
|
---|
607 | $strictfilemeta =~ s/\s*$//g;
|
---|
608 |
|
---|
609 | my $filename_encoding = $self->encoding_from_language_analysis($strictfilemeta);
|
---|
610 | if(!defined $filename_encoding) {
|
---|
611 | $filename_encoding = "undefined";
|
---|
612 | }
|
---|
613 |
|
---|
614 | return $filename_encoding; # can be the string "undefined"
|
---|
615 | }
|
---|
616 |
|
---|
617 | # performs textcat
|
---|
618 | sub encoding_from_language_analysis {
|
---|
619 | my $self = shift(@_);
|
---|
620 | my ($text) = @_;
|
---|
621 |
|
---|
622 | my $outhandle = $self->{'outhandle'};
|
---|
623 | my $best_encoding = undef;
|
---|
624 |
|
---|
625 | # get the language/encoding of the textstring using textcat
|
---|
626 | $self->{'textcat'} = new textcat() unless defined($self->{'textcat'});
|
---|
627 | my $results = $self->{'textcat'}->classify_cached_filename(\$text);
|
---|
628 |
|
---|
629 |
|
---|
630 | if (scalar @$results < 0) {
|
---|
631 | return undef;
|
---|
632 | }
|
---|
633 |
|
---|
634 | # We have some results, we choose the first
|
---|
635 | my ($language, $encoding) = $results->[0] =~ /^([^-]*)(?:-(.*))?$/;
|
---|
636 |
|
---|
637 | $best_encoding = $encoding;
|
---|
638 | if (!defined $best_encoding) {
|
---|
639 | return undef;
|
---|
640 | }
|
---|
641 |
|
---|
642 | if (defined $best_encoding && $best_encoding =~ m/^iso_8859/ && &unicode::check_is_utf8($text)) {
|
---|
643 | # the text is valid utf8, so assume that's the real encoding (since textcat is based on probabilities)
|
---|
644 | $best_encoding = 'utf8';
|
---|
645 | }
|
---|
646 |
|
---|
647 |
|
---|
648 | # check for equivalents where textcat doesn't have some encodings...
|
---|
649 | # eg MS versions of standard encodings
|
---|
650 | if (defined $best_encoding && $best_encoding =~ /^iso_8859_(\d+)/) {
|
---|
651 | my $iso = $1; # which variant of the iso standard?
|
---|
652 | # iso-8859 sets don't use chars 0x80-0x9f, windows codepages do
|
---|
653 | if ($text =~ /[\x80-\x9f]/) {
|
---|
654 | # Western Europe
|
---|
655 | if ($iso == 1 or $iso == 15) { $best_encoding = 'windows_1252' }
|
---|
656 | elsif ($iso == 2) {$best_encoding = 'windows_1250'} # Central Europe
|
---|
657 | elsif ($iso == 5) {$best_encoding = 'windows_1251'} # Cyrillic
|
---|
658 | elsif ($iso == 6) {$best_encoding = 'windows_1256'} # Arabic
|
---|
659 | elsif ($iso == 7) {$best_encoding = 'windows_1253'} # Greek
|
---|
660 | elsif ($iso == 8) {$best_encoding = 'windows_1255'} # Hebrew
|
---|
661 | elsif ($iso == 9) {$best_encoding = 'windows_1254'} # Turkish
|
---|
662 | }
|
---|
663 | }
|
---|
664 |
|
---|
665 | if (defined $best_encoding && $best_encoding !~ /^(ascii|utf8|unicode)$/ &&
|
---|
666 | !defined $encodings::encodings->{$best_encoding})
|
---|
667 | {
|
---|
668 | if ($self->{'verbosity'}) {
|
---|
669 | gsprintf($outhandle, "BasePlugin: {ReadTextFile.unsupported_encoding}\n", $text, $best_encoding, "undef");
|
---|
670 | }
|
---|
671 | $best_encoding = undef;
|
---|
672 | }
|
---|
673 |
|
---|
674 | return $best_encoding;
|
---|
675 | }
|
---|
676 |
|
---|
677 | # uses locale
|
---|
678 | sub get_filesystem_encoding {
|
---|
679 |
|
---|
680 | my $self = shift(@_);
|
---|
681 |
|
---|
682 | my $outhandle = $self->{'outhandle'};
|
---|
683 | my $filesystem_encoding = undef;
|
---|
684 |
|
---|
685 | eval {
|
---|
686 | use POSIX qw(locale_h);
|
---|
687 |
|
---|
688 | # With only one parameter, setlocale retrieves the
|
---|
689 | # current value
|
---|
690 | my $current_locale = setlocale(LC_CTYPE);
|
---|
691 |
|
---|
692 | if ($current_locale =~ m/^.*\.(.*?)$/) {
|
---|
693 | my $char_encoding = lc($1);
|
---|
694 | if ($char_encoding =~ m/^(iso)(8859)(\d{1,2})$/) {
|
---|
695 | $char_encoding = "$1\_$2\_$3";
|
---|
696 | }
|
---|
697 |
|
---|
698 | $char_encoding =~ s/-/_/g;
|
---|
699 | $char_encoding =~ s/^utf_8$/utf8/;
|
---|
700 |
|
---|
701 | if ($char_encoding =~ m/^\d+$/) {
|
---|
702 | if (defined $encodings::encodings->{"windows_$char_encoding"}) {
|
---|
703 | $char_encoding = "windows_$char_encoding";
|
---|
704 | }
|
---|
705 | elsif (defined $encodings::encodings->{"dos_$char_encoding"}) {
|
---|
706 | $char_encoding = "dos_$char_encoding";
|
---|
707 | }
|
---|
708 | }
|
---|
709 |
|
---|
710 | if (($char_encoding =~ m/(?:ascii|utf8|unicode)/)
|
---|
711 | || (defined $encodings::encodings->{$char_encoding})) {
|
---|
712 | $filesystem_encoding = $char_encoding;
|
---|
713 | }
|
---|
714 | else {
|
---|
715 | print $outhandle "Warning: Unsupported character encoding '$char_encoding' from locale '$current_locale'\n";
|
---|
716 | }
|
---|
717 | }
|
---|
718 |
|
---|
719 |
|
---|
720 | };
|
---|
721 | if ($@) {
|
---|
722 | print $outhandle "$@\n";
|
---|
723 | print $outhandle "Warning: Unable to establish locale. Will assume filesytem is UTF-8\n";
|
---|
724 |
|
---|
725 | }
|
---|
726 | return $filesystem_encoding;
|
---|
727 | }
|
---|
728 |
|
---|
729 | # is there ever only one Source? Sometimes this will be called twice, for images etc that are converted.
|
---|
730 | sub set_Source_metadata {
|
---|
731 | my $self = shift (@_);
|
---|
732 | my ($doc_obj, $filename_no_path, $file_encoding) = @_;
|
---|
733 |
|
---|
734 |
|
---|
735 | my $top_section = $doc_obj->get_top_section();
|
---|
736 |
|
---|
737 | # UTF-8 version of filename
|
---|
738 | my $filemeta = $self->filename_to_utf8_metadata($filename_no_path, $file_encoding);
|
---|
739 |
|
---|
740 | # Source is the UTF8 display name - not necessarily the name of the file on the system
|
---|
741 | $doc_obj->set_utf8_metadata_element($top_section, "Source", $filemeta);
|
---|
742 |
|
---|
743 | # SourceFile is the url-reference to url-encoded filemeta:
|
---|
744 | # it's a url that refers to the actual file on the system
|
---|
745 | $filemeta = &unicode::url_encode($filemeta);
|
---|
746 | $filemeta = &unicode::filename_to_url($filemeta);
|
---|
747 | $doc_obj->set_utf8_metadata_element($top_section, "SourceFile", $filemeta);
|
---|
748 | }
|
---|
749 |
|
---|
750 | # this should be called by all plugins to set the oid of the doc obj, rather
|
---|
751 | # than calling doc_obj->set_OID directly
|
---|
752 | sub add_OID {
|
---|
753 | my $self = shift (@_);
|
---|
754 | my ($doc_obj) = @_;
|
---|
755 |
|
---|
756 | $doc_obj->set_OIDtype($self->{'OIDtype'}, $self->{'OIDmetadata'});
|
---|
757 |
|
---|
758 | # see if there is a plugin specific set_OID function
|
---|
759 | if (defined ($self->can('set_OID'))) {
|
---|
760 | $self->set_OID(@_); # pass through doc_obj and any extra arguments
|
---|
761 | }
|
---|
762 | else {
|
---|
763 | # use the default set_OID() in doc.pm
|
---|
764 | $doc_obj->set_OID();
|
---|
765 | }
|
---|
766 |
|
---|
767 | }
|
---|
768 |
|
---|
769 | # The BasePlugin read_into_doc_obj() function. This function does all the
|
---|
770 | # right things to make general options work for a given plugin. It doesn't do anything with the file other than setting reads in
|
---|
771 | # a file and sets up a slew of metadata all saved in doc_obj, which
|
---|
772 | # it then returns as part of a tuple (process_status,doc_obj)
|
---|
773 | #
|
---|
774 | # Much of this functionality used to reside in read, but it was broken
|
---|
775 | # down into a supporting routine to make the code more flexible.
|
---|
776 | #
|
---|
777 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
778 | # capable of processing many documents within a single file (e.g.
|
---|
779 | # GMLPlug) will normally want to implement their own version of
|
---|
780 | # read_into_doc_obj()
|
---|
781 | #
|
---|
782 | # Note that $base_dir might be "" and that $file might
|
---|
783 | # include directories
|
---|
784 |
|
---|
785 | # currently blocking has been done before it gets here - does this affect secondary plugin stuff??
|
---|
786 | sub read_into_doc_obj {
|
---|
787 | my $self = shift (@_);
|
---|
788 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
789 |
|
---|
790 | my $outhandle = $self->{'outhandle'};
|
---|
791 |
|
---|
792 | # should we move this to read? What about secondary plugins?
|
---|
793 | print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
|
---|
794 | print $outhandle "$self->{'plugin_type'} processing $file\n"
|
---|
795 | if $self->{'verbosity'} > 1;
|
---|
796 |
|
---|
797 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
798 | # create a new document
|
---|
799 | my $doc_obj = new doc ($filename_full_path, "indexed_doc");
|
---|
800 | my $top_section = $doc_obj->get_top_section();
|
---|
801 |
|
---|
802 | $doc_obj->add_utf8_metadata($top_section, "Plugin", "$self->{'plugin_type'}");
|
---|
803 | $doc_obj->add_utf8_metadata($top_section, "FileSize", (-s $filename_full_path));
|
---|
804 |
|
---|
805 | # sets the UTF8 filename (Source) for display and sets the url ref to URL encoded version
|
---|
806 | # of the UTF8 filename (SourceFile) for generated files
|
---|
807 | $self->set_Source_metadata($doc_obj, $filename_no_path);
|
---|
808 |
|
---|
809 | # plugin specific stuff - what args do we need here??
|
---|
810 | unless (defined ($self->process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
|
---|
811 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
812 | return -1;
|
---|
813 | }
|
---|
814 |
|
---|
815 | # include any metadata passed in from previous plugins
|
---|
816 | # note that this metadata is associated with the top level section
|
---|
817 | my $section = $doc_obj->get_top_section();
|
---|
818 | # can we merge these two methods??
|
---|
819 | $self->add_associated_files($doc_obj, $filename_full_path);
|
---|
820 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
821 | $self->auto_extract_metadata($doc_obj);
|
---|
822 |
|
---|
823 | # if we haven't found any Title so far, assign one
|
---|
824 | # this was shifted to here from inside read()
|
---|
825 | $self->title_fallback($doc_obj,$section,$filename_no_path);
|
---|
826 |
|
---|
827 | $self->add_OID($doc_obj);
|
---|
828 |
|
---|
829 | return (1,$doc_obj);
|
---|
830 | }
|
---|
831 |
|
---|
832 | sub add_dummy_text {
|
---|
833 | my $self = shift(@_);
|
---|
834 | my ($doc_obj, $section) = @_;
|
---|
835 |
|
---|
836 | # add NoText metadata so we can hide this dummy text in format statements
|
---|
837 | $doc_obj->add_metadata($section, "NoText", "1");
|
---|
838 | $doc_obj->add_text($section, &gsprintf::lookup_string("{BasePlugin.dummy_text}",1));
|
---|
839 |
|
---|
840 | }
|
---|
841 |
|
---|
842 | # does nothing. Can be overridden by subclass
|
---|
843 | sub auto_extract_metadata {
|
---|
844 | my $self = shift(@_);
|
---|
845 | my ($doc_obj) = @_;
|
---|
846 | }
|
---|
847 |
|
---|
848 | # adds cover image, associate_file options stuff. Should be called by sub class
|
---|
849 | # read_into_doc_obj
|
---|
850 | sub add_associated_files {
|
---|
851 | my $self = shift(@_);
|
---|
852 | # whatis filename??
|
---|
853 | my ($doc_obj, $filename) = @_;
|
---|
854 |
|
---|
855 | # add in the cover image
|
---|
856 | if ($self->{'cover_image'}) {
|
---|
857 | $self->associate_cover_image($doc_obj, $filename);
|
---|
858 | }
|
---|
859 |
|
---|
860 |
|
---|
861 | }
|
---|
862 |
|
---|
863 | # implement this if you are extracting metadata for other documents
|
---|
864 | sub metadata_read {
|
---|
865 | my $self = shift (@_);
|
---|
866 | my ($pluginfo, $base_dir, $file, $block_hash, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
|
---|
867 |
|
---|
868 | # can we process this file??
|
---|
869 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
870 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
871 |
|
---|
872 | return 1; # we recognise the file, but don't actually do anything with it
|
---|
873 | }
|
---|
874 |
|
---|
875 |
|
---|
876 | # The BasePlugin read() function. This function calls read_into_doc_obj()
|
---|
877 | # to ensure all the right things to make general options work for a
|
---|
878 | # given plugin are done. It then calls the process() function which
|
---|
879 | # does all the work specific to a plugin (like the old read functions
|
---|
880 | # used to do). Most plugins should define their own process() function
|
---|
881 | # and let this read() function keep control.
|
---|
882 | #
|
---|
883 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
884 | # capable of processing many documents within a single file (e.g.
|
---|
885 | # GMLPlug) might want to implement their own version of read(), but
|
---|
886 | # more likely need to implement their own version of read_into_doc_obj()
|
---|
887 | #
|
---|
888 | # Return number of files processed, undef if can't recognise, -1 if can't
|
---|
889 | # process
|
---|
890 |
|
---|
891 | sub read {
|
---|
892 | my $self = shift (@_);
|
---|
893 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
894 |
|
---|
895 | # can we process this file??
|
---|
896 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
897 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
898 |
|
---|
899 | my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
|
---|
900 |
|
---|
901 | if ((defined $process_status) && ($process_status == 1)) {
|
---|
902 |
|
---|
903 | # process the document
|
---|
904 | $processor->process($doc_obj);
|
---|
905 |
|
---|
906 | $self->{'num_processed'} ++;
|
---|
907 | undef $doc_obj;
|
---|
908 | }
|
---|
909 | # delete any temp files that we may have created
|
---|
910 | $self->clean_up_after_doc_obj_processing();
|
---|
911 |
|
---|
912 | # if process_status == 1, then the file has been processed.
|
---|
913 | return $process_status;
|
---|
914 |
|
---|
915 | }
|
---|
916 |
|
---|
917 | # returns undef if file is rejected by the plugin
|
---|
918 | sub process {
|
---|
919 | my $self = shift (@_);
|
---|
920 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
921 |
|
---|
922 | gsprintf(STDERR, "BasePlugin::process {common.must_be_implemented}\n") && die "\n";
|
---|
923 |
|
---|
924 | return undef; # never gets here
|
---|
925 | }
|
---|
926 |
|
---|
927 | # overwrite this method to delete any temp files that we have created
|
---|
928 | sub clean_up_after_doc_obj_processing {
|
---|
929 | my $self = shift(@_);
|
---|
930 |
|
---|
931 | }
|
---|
932 |
|
---|
933 | # write_file -- used by ConvertToPlug, for example in post processing
|
---|
934 | #
|
---|
935 | # where should this go, is here the best place??
|
---|
936 | sub utf8_write_file {
|
---|
937 | my $self = shift (@_);
|
---|
938 | my ($textref, $filename) = @_;
|
---|
939 |
|
---|
940 | if (!open (FILE, ">$filename")) {
|
---|
941 | gsprintf(STDERR, "ConvertToPlug::write_file {ConvertToPlug.could_not_open_for_writing} ($!)\n", $filename);
|
---|
942 | die "\n";
|
---|
943 | }
|
---|
944 | print FILE $$textref;
|
---|
945 |
|
---|
946 | close FILE;
|
---|
947 | }
|
---|
948 |
|
---|
949 |
|
---|
950 | sub filename_based_title
|
---|
951 | {
|
---|
952 | my $self = shift (@_);
|
---|
953 | my ($file) = @_;
|
---|
954 |
|
---|
955 | my $file_derived_title = $file;
|
---|
956 | $file_derived_title =~ s/_/ /g;
|
---|
957 | $file_derived_title =~ s/\..*?$//;
|
---|
958 |
|
---|
959 | return $file_derived_title;
|
---|
960 | }
|
---|
961 |
|
---|
962 |
|
---|
963 | sub title_fallback
|
---|
964 | {
|
---|
965 | my $self = shift (@_);
|
---|
966 | my ($doc_obj,$section,$file) = @_;
|
---|
967 |
|
---|
968 | if (!defined $doc_obj->get_metadata_element ($section, "Title") or $doc_obj->get_metadata_element($section, "Title") eq "") {
|
---|
969 |
|
---|
970 | my $file_derived_title = $self->filename_to_utf8_metadata($self->filename_based_title($file));
|
---|
971 | if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
|
---|
972 | $doc_obj->add_utf8_metadata ($section, "Title", $file_derived_title);
|
---|
973 | }
|
---|
974 | else {
|
---|
975 | $doc_obj->set_utf8_metadata_element ($section, "Title", $file_derived_title);
|
---|
976 | }
|
---|
977 | }
|
---|
978 |
|
---|
979 | }
|
---|
980 |
|
---|
981 | # add any extra metadata that's been passed around from one
|
---|
982 | # plugin to another.
|
---|
983 | # extra_metadata uses add_utf8_metadata so it expects metadata values
|
---|
984 | # to already be in utf8
|
---|
985 | sub extra_metadata {
|
---|
986 | my $self = shift (@_);
|
---|
987 | my ($doc_obj, $cursection, $metadata) = @_;
|
---|
988 |
|
---|
989 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
990 |
|
---|
991 | foreach my $field (keys(%$metadata)) {
|
---|
992 | # $metadata->{$field} may be an array reference
|
---|
993 | if ($field eq "gsdlassocfile_tobe") {
|
---|
994 | # 'gsdlassocfile_tobe' is artificially introduced metadata
|
---|
995 | # that is used to signal that certain additional files should
|
---|
996 | # be tied to this document. Useful in situations where a
|
---|
997 | # metadata pass in the plugin pipeline works out some files
|
---|
998 | # need to be associated with a document, but the document hasn't
|
---|
999 | # been formed yet.
|
---|
1000 | my $equiv_form = "";
|
---|
1001 | foreach my $gaf (@{$metadata->{$field}}) {
|
---|
1002 | my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
|
---|
1003 | my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
|
---|
1004 |
|
---|
1005 | # we need to make sure the filename is valid utf-8 - we do
|
---|
1006 | # this by url encoding it
|
---|
1007 | # $tail_filename is the name that we store the file as
|
---|
1008 | $tail_filename = &unicode::url_encode($tail_filename);
|
---|
1009 | $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
|
---|
1010 | # then we need to encode the % signs in the filename, so that
|
---|
1011 | # it works in a url
|
---|
1012 | my $url_tail_filename = &unicode::filename_to_url($tail_filename);
|
---|
1013 | # work out extended tail extension (i.e. matching tail re)
|
---|
1014 |
|
---|
1015 | my ($file_prefix,$file_extended_ext)
|
---|
1016 | = &util::get_prefix_and_tail_by_regex($tail_filename,$associate_tail_re);
|
---|
1017 | my ($pre_doc_ext) = ($file_extended_ext =~ m/^(.*)\..*$/);
|
---|
1018 | my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
|
---|
1019 | my $start_doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/{Or}{[parent(Top):archivedir],[archivedir]}/$url_tail_filename\">";
|
---|
1020 | my $srcicon = "_icon".$doc_ext."_";
|
---|
1021 | my $end_doclink = "</a>";
|
---|
1022 |
|
---|
1023 | my $assoc_form = "$start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
|
---|
1024 |
|
---|
1025 | if (defined $pre_doc_ext && $pre_doc_ext ne "") {
|
---|
1026 | # for metadata such as [mp3._edited] [mp3._full] ...
|
---|
1027 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.$pre_doc_ext", $assoc_form);
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | # for multiple metadata such as [mp3.assoclink]
|
---|
1031 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.assoclink", $assoc_form);
|
---|
1032 |
|
---|
1033 | $equiv_form .= " $assoc_form";
|
---|
1034 | }
|
---|
1035 | $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
|
---|
1036 | }
|
---|
1037 | elsif (ref ($metadata->{$field}) eq "ARRAY") {
|
---|
1038 | map {
|
---|
1039 | $doc_obj->add_utf8_metadata ($cursection, $field, $_);
|
---|
1040 | } @{$metadata->{$field}};
|
---|
1041 | } else {
|
---|
1042 | $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
|
---|
1043 | }
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 |
|
---|
1048 | sub compile_stats {
|
---|
1049 | my $self = shift(@_);
|
---|
1050 | my ($stats) = @_;
|
---|
1051 |
|
---|
1052 | $stats->{'num_processed'} += $self->{'num_processed'};
|
---|
1053 | $stats->{'num_not_processed'} += $self->{'num_not_processed'};
|
---|
1054 | $stats->{'num_archives'} += $self->{'num_archives'};
|
---|
1055 |
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | sub associate_cover_image {
|
---|
1059 | my $self = shift;
|
---|
1060 | my ($doc_obj, $filename) = @_;
|
---|
1061 |
|
---|
1062 | $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
1063 | if (exists $self->{'covers_missing_cache'}->{$filename}) {
|
---|
1064 | # don't stat() for existence eg for multiple document input files
|
---|
1065 | # (eg SplitPlug)
|
---|
1066 | return;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | my $top_section=$doc_obj->get_top_section();
|
---|
1070 |
|
---|
1071 | if (-e $filename) {
|
---|
1072 | $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
|
---|
1073 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
1074 | } else {
|
---|
1075 | my $upper_filename = $filename;
|
---|
1076 | $upper_filename =~ s/jpg$/JPG/;
|
---|
1077 | if (-e $upper_filename) {
|
---|
1078 | $doc_obj->associate_file($upper_filename, "cover.jpg",
|
---|
1079 | "image/jpeg");
|
---|
1080 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
1081 | } else {
|
---|
1082 | # file doesn't exist, so record the fact that it's missing so
|
---|
1083 | # we don't stat() again (stat is slow)
|
---|
1084 | $self->{'covers_missing_cache'}->{$filename} = 1;
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 |
|
---|
1091 | # Overridden by exploding plugins (eg. ISISPlug)
|
---|
1092 | sub clean_up_after_exploding
|
---|
1093 | {
|
---|
1094 | my $self = shift(@_);
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 |
|
---|
1098 |
|
---|
1099 | 1;
|
---|