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 multiread;
|
---|
35 | use encodings;
|
---|
36 | use unicode;
|
---|
37 | use doc;
|
---|
38 | eval "require diagnostics"; # some perl distros (eg mac) don't have this
|
---|
39 | use ghtml;
|
---|
40 | use gsprintf 'gsprintf';
|
---|
41 |
|
---|
42 | use PrintInfo;
|
---|
43 |
|
---|
44 | BEGIN {
|
---|
45 | @BasePlugin::ISA = ( 'PrintInfo' );
|
---|
46 | }
|
---|
47 |
|
---|
48 | our $encoding_list =
|
---|
49 | [ { 'name' => "ascii",
|
---|
50 | 'desc' => "{BasePlugin.encoding.ascii}" },
|
---|
51 | { 'name' => "utf8",
|
---|
52 | 'desc' => "{BasePlugin.encoding.utf8}" },
|
---|
53 | { 'name' => "unicode",
|
---|
54 | 'desc' => "{BasePlugin.encoding.unicode}" } ];
|
---|
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 |
|
---|
70 | push(@{$encoding_plus_auto_list},@{$encoding_list});
|
---|
71 |
|
---|
72 | my $arguments =
|
---|
73 | [ { 'name' => "process_exp",
|
---|
74 | 'desc' => "{BasePlugin.process_exp}",
|
---|
75 | 'type' => "regexp",
|
---|
76 | 'deft' => "",
|
---|
77 | 'reqd' => "no" },
|
---|
78 | { 'name' => "no_blocking",
|
---|
79 | 'desc' => "{BasePlugin.no_blocking}",
|
---|
80 | 'type' => "flag",
|
---|
81 | 'reqd' => "no"},
|
---|
82 | { 'name' => "block_exp",
|
---|
83 | 'desc' => "{BasePlugin.block_exp}",
|
---|
84 | 'type' => "regexp",
|
---|
85 | 'deft' => "",
|
---|
86 | 'reqd' => "no" },
|
---|
87 | { 'name' => "associate_ext",
|
---|
88 | 'desc' => "{BasePlugin.associate_ext}",
|
---|
89 | 'type' => "string",
|
---|
90 | 'reqd' => "no" },
|
---|
91 | { 'name' => "associate_tail_re",
|
---|
92 | 'desc' => "{BasePlugin.associate_tail_re}",
|
---|
93 | 'type' => "string",
|
---|
94 | 'reqd' => "no" },
|
---|
95 | { 'name' => "use_as_doc_identifier",
|
---|
96 | 'desc' => "{BasePlugin.use_as_doc_identifier}",
|
---|
97 | 'type' => "string",
|
---|
98 | 'reqd' => "no" ,
|
---|
99 | 'deft' => "" } ,
|
---|
100 | { 'name' => "no_cover_image",
|
---|
101 | 'desc' => "{BasePlugin.no_cover_image}",
|
---|
102 | 'type' => "flag",
|
---|
103 | 'reqd' => "no" },
|
---|
104 | { 'name' => "filename_encoding",
|
---|
105 | 'desc' => "{BasePlugin.filename_encoding}",
|
---|
106 | 'type' => "enum",
|
---|
107 | 'deft' => "auto",
|
---|
108 | 'list' => $encoding_plus_auto_list,
|
---|
109 | 'reqd' => "no" },
|
---|
110 | { 'name' => "smart_block",
|
---|
111 | 'desc' => "{BasePlugin.smart_block}",
|
---|
112 | 'type' => "flag",
|
---|
113 | 'reqd' => "no",
|
---|
114 | 'hiddengli' => "yes" } # deprecated, but leave in for old collections
|
---|
115 |
|
---|
116 |
|
---|
117 | ];
|
---|
118 |
|
---|
119 |
|
---|
120 | my $options = { 'name' => "BasePlugin",
|
---|
121 | 'desc' => "{BasePlugin.desc}",
|
---|
122 | 'abstract' => "yes",
|
---|
123 | 'inherits' => "no",
|
---|
124 | 'args' => $arguments };
|
---|
125 |
|
---|
126 |
|
---|
127 | sub new {
|
---|
128 |
|
---|
129 | my ($class) = shift (@_);
|
---|
130 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
131 | push(@$pluginlist, $class);
|
---|
132 |
|
---|
133 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
134 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
135 |
|
---|
136 | my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists);
|
---|
137 |
|
---|
138 | if ($self->{'info_only'}) {
|
---|
139 | # don't worry about any options etc
|
---|
140 | return bless $self, $class;
|
---|
141 | }
|
---|
142 |
|
---|
143 | if ($self->{'smart_block'}) {
|
---|
144 | print STDERR "WARNING: -smart_block option has been deprecated and is no longer useful\n";
|
---|
145 | }
|
---|
146 | $self->{'smart_block'} = undef;
|
---|
147 |
|
---|
148 | my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
|
---|
149 | $self->{'plugin_type'} = $plugin_name;
|
---|
150 |
|
---|
151 | $self->{'num_processed'} = 0;
|
---|
152 | $self->{'num_not_processed'} = 0;
|
---|
153 | $self->{'num_blocked'} = 0;
|
---|
154 | $self->{'num_archives'} = 0;
|
---|
155 | $self->{'cover_image'} = 1; # cover image is on by default
|
---|
156 | $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
|
---|
157 | #$self->{'option_list'} = $hashArgOptLists->{"OptList"};
|
---|
158 |
|
---|
159 | my $associate_ext = $self->{'associate_ext'};
|
---|
160 | if ((defined $associate_ext) && ($associate_ext ne "")) {
|
---|
161 |
|
---|
162 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
163 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
164 | my $outhandle = $self->{'outhandle'};
|
---|
165 | print $outhandle "Warning: can only specify 'associate_ext' or 'associate_tail_re'\n";
|
---|
166 | print $outhandle " defaulting to 'associate_tail_re'\n";
|
---|
167 | }
|
---|
168 | else {
|
---|
169 | my @exts = split(/,/,$associate_ext);
|
---|
170 |
|
---|
171 | my @exts_bracketed = map { $_ = "(?:\\.$_)" } @exts;
|
---|
172 | my $associate_tail_re = join("|",@exts_bracketed);
|
---|
173 | $self->{'associate_tail_re'} = $associate_tail_re;
|
---|
174 | }
|
---|
175 |
|
---|
176 | delete $self->{'associate_ext'};
|
---|
177 | }
|
---|
178 |
|
---|
179 | return bless $self, $class;
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 | # initialize BasePlugin options
|
---|
184 | # if init() is overridden in a sub-class, remember to call BasePlugin::init()
|
---|
185 | sub init {
|
---|
186 | my $self = shift (@_);
|
---|
187 | my ($verbosity, $outhandle, $failhandle) = @_;
|
---|
188 |
|
---|
189 | # verbosity is passed through from the processor
|
---|
190 | $self->{'verbosity'} = $verbosity;
|
---|
191 |
|
---|
192 | # as are the outhandle and failhandle
|
---|
193 | $self->{'outhandle'} = $outhandle if defined $outhandle;
|
---|
194 | $self->{'failhandle'} = $failhandle;
|
---|
195 | # $self->SUPER::init(@_);
|
---|
196 |
|
---|
197 | # set process_exp and block_exp to defaults unless they were
|
---|
198 | # explicitly set
|
---|
199 |
|
---|
200 | if ((!$self->is_recursive()) and
|
---|
201 | (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
|
---|
202 |
|
---|
203 | $self->{'process_exp'} = $self->get_default_process_exp ();
|
---|
204 | if ($self->{'process_exp'} eq "") {
|
---|
205 | warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
|
---|
210 | $self->{'block_exp'} = $self->get_default_block_exp ();
|
---|
211 | }
|
---|
212 |
|
---|
213 | }
|
---|
214 |
|
---|
215 | sub begin {
|
---|
216 | my $self = shift (@_);
|
---|
217 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
218 | }
|
---|
219 |
|
---|
220 | sub end {
|
---|
221 | # potentially called at the end of each plugin pass
|
---|
222 | # import.pl only has one plugin pass, but buildcol.pl has multiple ones
|
---|
223 |
|
---|
224 | my ($self) = shift (@_);
|
---|
225 | }
|
---|
226 |
|
---|
227 | sub deinit {
|
---|
228 | # called only once, after all plugin passes have been done
|
---|
229 |
|
---|
230 | my ($self) = @_;
|
---|
231 | }
|
---|
232 |
|
---|
233 | sub set_incremental {
|
---|
234 | my $self = shift(@_);
|
---|
235 | my ($incremental) = @_;
|
---|
236 |
|
---|
237 | $self->{'incremental'} = $incremental;
|
---|
238 | }
|
---|
239 |
|
---|
240 | # this function should be overridden to return 1
|
---|
241 | # in recursive plugins
|
---|
242 | sub is_recursive {
|
---|
243 | my $self = shift (@_);
|
---|
244 |
|
---|
245 | return 0;
|
---|
246 | }
|
---|
247 |
|
---|
248 | sub get_default_block_exp {
|
---|
249 | my $self = shift (@_);
|
---|
250 |
|
---|
251 | return "";
|
---|
252 | }
|
---|
253 |
|
---|
254 | sub get_default_process_exp {
|
---|
255 | my $self = shift (@_);
|
---|
256 |
|
---|
257 | return "";
|
---|
258 | }
|
---|
259 |
|
---|
260 | # default implementation is to do nothing
|
---|
261 | sub store_block_files {
|
---|
262 |
|
---|
263 | my $self =shift (@_);
|
---|
264 | my ($filename_full_path, $block_hash) = @_;
|
---|
265 |
|
---|
266 | }
|
---|
267 |
|
---|
268 | # put files to block into hash
|
---|
269 | sub use_block_expressions {
|
---|
270 |
|
---|
271 | my $self =shift (@_);
|
---|
272 | my ($filename_full_path, $block_hash) = @_;
|
---|
273 |
|
---|
274 | if ($self->{'block_exp'} ne "" && $filename_full_path =~ /$self->{'block_exp'}/) {
|
---|
275 | $block_hash->{'file_blocks'}->{$filename_full_path} = 1;
|
---|
276 | }
|
---|
277 |
|
---|
278 | }
|
---|
279 |
|
---|
280 | #default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
|
---|
281 | sub block_cover_image
|
---|
282 | {
|
---|
283 | my $self =shift;
|
---|
284 | my ($filename, $block_hash) = @_;
|
---|
285 |
|
---|
286 | if ($self->{'cover_image'}) {
|
---|
287 | my $coverfile = $filename;
|
---|
288 | $coverfile =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
289 | if (!-e $coverfile) {
|
---|
290 | $coverfile =~ s/jpg$/JPG/;
|
---|
291 | }
|
---|
292 | if (-e $coverfile) {
|
---|
293 | $block_hash->{'file_blocks'}->{$coverfile} = 1;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | return;
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | # discover all the files that should be blocked by this plugin
|
---|
302 | # check the args ...
|
---|
303 | sub file_block_read {
|
---|
304 |
|
---|
305 | my $self = shift (@_);
|
---|
306 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
|
---|
307 | # Keep track of filenames with same root but different extensions
|
---|
308 | # Used to support -associate_ext and the more generalised
|
---|
309 | # -associate_tail_re
|
---|
310 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
311 |
|
---|
312 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
313 | if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
|
---|
314 |
|
---|
315 | my ($file_prefix,$file_ext)
|
---|
316 | = &util::get_prefix_and_tail_by_regex($filename_full_path,$associate_tail_re);
|
---|
317 |
|
---|
318 | if ((defined $file_prefix) && (defined $file_ext)) {
|
---|
319 | my $shared_fileroot = $block_hash->{'shared_fileroot'};
|
---|
320 | if (!defined $shared_fileroot->{$file_prefix}) {
|
---|
321 | my $file_prefix_rec = { 'tie_to' => undef,
|
---|
322 | 'exts' => {} };
|
---|
323 | $shared_fileroot->{$file_prefix} = $file_prefix_rec;
|
---|
324 | }
|
---|
325 |
|
---|
326 | my $file_prefix_rec = $shared_fileroot->{$file_prefix};
|
---|
327 |
|
---|
328 | if ($self->can_process_this_file($filename_full_path)) {
|
---|
329 | # This is the document the others should be tied to
|
---|
330 | $file_prefix_rec->{'tie_to'} = $file_ext;
|
---|
331 | }
|
---|
332 | else {
|
---|
333 | if ($file_ext =~ m/$associate_tail_re$/) {
|
---|
334 | # this file should be associated to the main one
|
---|
335 | $file_prefix_rec->{'exts'}->{$file_ext} = 1;
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | # check block expressions
|
---|
343 | $self->use_block_expressions($filename_full_path, $block_hash) unless $self->{'no_blocking'};
|
---|
344 |
|
---|
345 | # now check whether we are actually processing this
|
---|
346 | if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
|
---|
347 | return undef; # can't recognise
|
---|
348 | }
|
---|
349 |
|
---|
350 | $self->store_block_files($filename_full_path, $block_hash) unless $self->{'no_blocking'};
|
---|
351 |
|
---|
352 | # block the cover image if there is one
|
---|
353 | if ($self->{'cover_image'}) {
|
---|
354 | $self->block_cover_image($filename_full_path, $block_hash) unless $self->{'no_blocking'};
|
---|
355 | }
|
---|
356 |
|
---|
357 | return 1;
|
---|
358 | }
|
---|
359 |
|
---|
360 | # plugins that rely on more than process_exp (eg XML plugins) can override this method
|
---|
361 | sub can_process_this_file {
|
---|
362 | my $self = shift(@_);
|
---|
363 | my ($filename) = @_;
|
---|
364 |
|
---|
365 | if ($self->{'process_exp'} ne "" && $filename =~ /$self->{'process_exp'}/) {
|
---|
366 | return 1;
|
---|
367 | }
|
---|
368 | return 0;
|
---|
369 |
|
---|
370 | }
|
---|
371 |
|
---|
372 | # just converts path as is to utf8.
|
---|
373 | sub filepath_to_utf8 {
|
---|
374 | my $self = shift (@_);
|
---|
375 | my ($file, $file_encoding) = @_;
|
---|
376 | my $filemeta = $file;
|
---|
377 |
|
---|
378 | my $filename_encoding = $self->{'filename_encoding'};
|
---|
379 | if ($filename_encoding eq "auto") {
|
---|
380 | # we check the locale first
|
---|
381 | if (!defined $self->{'filesystem_encoding'}) {
|
---|
382 | $self->{'filesystem_encoding'} = $self->get_filesystem_encoding();
|
---|
383 | $self->{'filesystem_encoding'} = "undefined" if !defined $self->{'filesystem_encoding'};
|
---|
384 | }
|
---|
385 | if ($self->{'filesystem_encoding'} ne "undefined") {
|
---|
386 | $filename_encoding = $self->{'filesystem_encoding'};
|
---|
387 | } else {
|
---|
388 | # try the encoding of the document, if available
|
---|
389 | if (defined $file_encoding) {
|
---|
390 | $filename_encoding = $file_encoding;
|
---|
391 | } else {
|
---|
392 | # use utf8
|
---|
393 | $filename_encoding = "utf8";
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | }
|
---|
398 |
|
---|
399 | if ($filename_encoding !~ /(?:ascii|utf8|unicode)/) {
|
---|
400 | $filemeta = unicode::unicode2utf8(
|
---|
401 | unicode::convert2unicode($filename_encoding, \$filemeta)
|
---|
402 | );
|
---|
403 | }
|
---|
404 |
|
---|
405 | return $filemeta;
|
---|
406 | }
|
---|
407 |
|
---|
408 | # gets the filename with no path, converts to utf8, and then dm safes it.
|
---|
409 | #filename_encoding set by user
|
---|
410 | sub filename_to_utf8_metadata
|
---|
411 | {
|
---|
412 | my $self = shift (@_);
|
---|
413 | my ($file, $file_encoding) = @_;
|
---|
414 |
|
---|
415 | my $outhandle = $self->{'outhandle'};
|
---|
416 |
|
---|
417 | my ($filemeta) = $file =~ /([^\\\/]+)$/; # getting the tail of the filepath (skips all string parts containing slashes upto the end)
|
---|
418 | $filemeta = $self->filepath_to_utf8($filemeta, $file_encoding);
|
---|
419 |
|
---|
420 | my $dmsafe_filemeta = &ghtml::dmsafe($filemeta);
|
---|
421 |
|
---|
422 | return $dmsafe_filemeta;
|
---|
423 |
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | sub get_filesystem_encoding {
|
---|
428 |
|
---|
429 | my $self = shift(@_);
|
---|
430 |
|
---|
431 | my $outhandle = $self->{'outhandle'};
|
---|
432 | my $filesystem_encoding = undef;
|
---|
433 |
|
---|
434 | eval {
|
---|
435 | use POSIX qw(locale_h);
|
---|
436 |
|
---|
437 | # With only one parameter, setlocale retrieves the
|
---|
438 | # current value
|
---|
439 | my $current_locale = setlocale(LC_CTYPE);
|
---|
440 |
|
---|
441 | if ($current_locale =~ m/^.*\.(.*?)$/) {
|
---|
442 | my $char_encoding = lc($1);
|
---|
443 | if ($char_encoding =~ m/^(iso)(8859)(\d{1,2})$/) {
|
---|
444 | $char_encoding = "$1\_$2\_$3";
|
---|
445 | }
|
---|
446 |
|
---|
447 | $char_encoding =~ s/-/_/g;
|
---|
448 | $char_encoding =~ s/^utf_8$/utf8/;
|
---|
449 |
|
---|
450 | if ($char_encoding =~ m/^\d+$/) {
|
---|
451 | if (defined $encodings::encodings->{"windows_$char_encoding"}) {
|
---|
452 | $char_encoding = "windows_$char_encoding";
|
---|
453 | }
|
---|
454 | elsif (defined $encodings::encodings->{"dos_$char_encoding"}) {
|
---|
455 | $char_encoding = "dos_$char_encoding";
|
---|
456 | }
|
---|
457 | }
|
---|
458 |
|
---|
459 | if (($char_encoding =~ m/(?:ascii|utf8|unicode)/)
|
---|
460 | || (defined $encodings::encodings->{$char_encoding})) {
|
---|
461 | $filesystem_encoding = $char_encoding;
|
---|
462 | }
|
---|
463 | else {
|
---|
464 | print $outhandle "Warning: Unsupported character encoding '$char_encoding' from locale '$current_locale'\n";
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 |
|
---|
469 | };
|
---|
470 | if ($@) {
|
---|
471 | print $outhandle "$@\n";
|
---|
472 | print $outhandle "Warning: Unable to establish locale. Will assume filesytem is UTF-8\n";
|
---|
473 |
|
---|
474 | }
|
---|
475 | return $filesystem_encoding;
|
---|
476 | }
|
---|
477 |
|
---|
478 | # is there ever only one Source? Sometimes this will be called twice, for images etc that are converted.
|
---|
479 | sub set_Source_metadata {
|
---|
480 | my $self = shift (@_);
|
---|
481 | my ($doc_obj, $filename_no_path, $file_encoding) = @_;
|
---|
482 |
|
---|
483 | my $top_section = $doc_obj->get_top_section();
|
---|
484 |
|
---|
485 | # UTF-8 version of filename
|
---|
486 | my $filemeta = $self->filename_to_utf8_metadata($filename_no_path, $file_encoding);
|
---|
487 | $doc_obj->set_utf8_metadata_element($top_section, "Source", $filemeta);
|
---|
488 |
|
---|
489 | }
|
---|
490 |
|
---|
491 | sub add_OID {
|
---|
492 |
|
---|
493 | my $self = shift (@_);
|
---|
494 | my ($doc_obj) = @_;
|
---|
495 |
|
---|
496 | # See if a metadata field is specified as the field
|
---|
497 | if ((defined $self->{'use_as_doc_identifier'}) && ($self->{'use_as_doc_identifier'} ne "")) {
|
---|
498 | my $metadata_doc_id = $self->{'use_as_doc_identifier'};
|
---|
499 |
|
---|
500 | # Consider "tidying" up metadata_doc_id to be something
|
---|
501 | # suitable in a URL
|
---|
502 | # Could even support a user specified plugin RE for this.
|
---|
503 |
|
---|
504 | my $top_section = $doc_obj->get_top_section();
|
---|
505 | my $oid = $doc_obj->get_metadata_element($top_section,$metadata_doc_id);
|
---|
506 | ## print STDERR "**** oid = $oid\n";
|
---|
507 | $doc_obj->set_OID($oid);
|
---|
508 | }
|
---|
509 | # See if there is a plugin-specific set_OID function...
|
---|
510 | elsif (defined ($self->can('set_OID'))) {
|
---|
511 | # it will need $doc_obj to set the Identifier metadata...
|
---|
512 | $self->set_OID(@_); # pass through any extra arguments supplied
|
---|
513 | } else {
|
---|
514 | # use the default set_OID() in doc.pm
|
---|
515 | $doc_obj->set_OID();
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 |
|
---|
520 |
|
---|
521 | # The BasePlugin read_into_doc_obj() function. This function does all the
|
---|
522 | # right things to make general options work for a given plugin. It doesn't do anything with the file other than setting reads in
|
---|
523 | # a file and sets up a slew of metadata all saved in doc_obj, which
|
---|
524 | # it then returns as part of a tuple (process_status,doc_obj)
|
---|
525 | #
|
---|
526 | # Much of this functionality used to reside in read, but it was broken
|
---|
527 | # down into a supporting routine to make the code more flexible.
|
---|
528 | #
|
---|
529 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
530 | # capable of processing many documents within a single file (e.g.
|
---|
531 | # GMLPlug) will normally want to implement their own version of
|
---|
532 | # read_into_doc_obj()
|
---|
533 | #
|
---|
534 | # Note that $base_dir might be "" and that $file might
|
---|
535 | # include directories
|
---|
536 |
|
---|
537 | # currently blocking has been done before it gets here - does this affect secondary plugin stuff??
|
---|
538 | sub read_into_doc_obj {
|
---|
539 | my $self = shift (@_);
|
---|
540 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
541 |
|
---|
542 | my $outhandle = $self->{'outhandle'};
|
---|
543 |
|
---|
544 | # should we move this to read? What about secondary plugins?
|
---|
545 | print STDERR "<Processing n='$file' p='$self->{'plugin_type'}'>\n" if ($gli);
|
---|
546 | print $outhandle "$self->{'plugin_type'} processing $file\n"
|
---|
547 | if $self->{'verbosity'} > 1;
|
---|
548 |
|
---|
549 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
550 | # create a new document
|
---|
551 | my $doc_obj = new doc ($filename_full_path, "indexed_doc");
|
---|
552 | my $top_section = $doc_obj->get_top_section();
|
---|
553 |
|
---|
554 | # this should look at the plugin option too...
|
---|
555 | $doc_obj->set_OIDtype ($processor->{'OIDtype'}, $processor->{'OIDmetadata'});
|
---|
556 | $doc_obj->add_utf8_metadata($top_section, "Plugin", "$self->{'plugin_type'}");
|
---|
557 | $doc_obj->add_utf8_metadata($top_section, "FileSize", (-s $filename_full_path));
|
---|
558 |
|
---|
559 | $self->set_Source_metadata($doc_obj, $filename_no_path);
|
---|
560 |
|
---|
561 | # plugin specific stuff - what args do we need here??
|
---|
562 | unless (defined ($self->process($pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli))) {
|
---|
563 | print STDERR "<ProcessingError n='$file'>\n" if ($gli);
|
---|
564 | return -1;
|
---|
565 | }
|
---|
566 |
|
---|
567 | # include any metadata passed in from previous plugins
|
---|
568 | # note that this metadata is associated with the top level section
|
---|
569 | my $section = $doc_obj->get_top_section();
|
---|
570 | # can we merge these two methods??
|
---|
571 | $self->add_associated_files($doc_obj, $filename_full_path);
|
---|
572 | $self->extra_metadata ($doc_obj, $section, $metadata);
|
---|
573 | $self->auto_extract_metadata($doc_obj);
|
---|
574 |
|
---|
575 | # if we haven't found any Title so far, assign one
|
---|
576 | # this was shifted to here from inside read()
|
---|
577 | $self->title_fallback($doc_obj,$section,$filename_no_path);
|
---|
578 |
|
---|
579 | $self->add_OID($doc_obj);
|
---|
580 |
|
---|
581 | return (1,$doc_obj);
|
---|
582 | }
|
---|
583 |
|
---|
584 | sub add_dummy_text {
|
---|
585 | my $self = shift(@_);
|
---|
586 | my ($doc_obj, $section) = @_;
|
---|
587 |
|
---|
588 | # add NoText metadata so we can hide this dummy text in format statements
|
---|
589 | $doc_obj->add_metadata($section, "NoText", "1");
|
---|
590 | $doc_obj->add_text($section, &gsprintf::lookup_string("{BasePlugin.dummy_text}",1));
|
---|
591 |
|
---|
592 | }
|
---|
593 |
|
---|
594 | # does nothing. Can be overridden by subclass
|
---|
595 | sub auto_extract_metadata {
|
---|
596 | my $self = shift(@_);
|
---|
597 | my ($doc_obj) = @_;
|
---|
598 | }
|
---|
599 |
|
---|
600 | # adds cover image, associate_file options stuff. Should be called by sub class
|
---|
601 | # read_into_doc_obj
|
---|
602 | sub add_associated_files {
|
---|
603 | my $self = shift(@_);
|
---|
604 | # whatis filename??
|
---|
605 | my ($doc_obj, $filename) = @_;
|
---|
606 |
|
---|
607 | # add in the cover image
|
---|
608 | if ($self->{'cover_image'}) {
|
---|
609 | $self->associate_cover_image($doc_obj, $filename);
|
---|
610 | }
|
---|
611 |
|
---|
612 |
|
---|
613 | }
|
---|
614 |
|
---|
615 | # implement this if you are extracting metadata for other documents
|
---|
616 | sub metadata_read {
|
---|
617 | my $self = shift (@_);
|
---|
618 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
|
---|
619 |
|
---|
620 | # can we process this file??
|
---|
621 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
622 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
623 |
|
---|
624 | return 1; # we recognise the file, but don't actually do anything with it
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | # The BasePlugin read() function. This function calls read_into_doc_obj()
|
---|
629 | # to ensure all the right things to make general options work for a
|
---|
630 | # given plugin are done. It then calls the process() function which
|
---|
631 | # does all the work specific to a plugin (like the old read functions
|
---|
632 | # used to do). Most plugins should define their own process() function
|
---|
633 | # and let this read() function keep control.
|
---|
634 | #
|
---|
635 | # recursive plugins (e.g. RecPlug) and specialized plugins like those
|
---|
636 | # capable of processing many documents within a single file (e.g.
|
---|
637 | # GMLPlug) might want to implement their own version of read(), but
|
---|
638 | # more likely need to implement their own version of read_into_doc_obj()
|
---|
639 | #
|
---|
640 | # Return number of files processed, undef if can't recognise, -1 if can't
|
---|
641 | # process
|
---|
642 |
|
---|
643 | sub read {
|
---|
644 | my $self = shift (@_);
|
---|
645 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
646 |
|
---|
647 | # can we process this file??
|
---|
648 | my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
|
---|
649 | return undef unless $self->can_process_this_file($filename_full_path);
|
---|
650 |
|
---|
651 | my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
|
---|
652 |
|
---|
653 | if ((defined $process_status) && ($process_status == 1)) {
|
---|
654 |
|
---|
655 | # process the document
|
---|
656 | $processor->process($doc_obj);
|
---|
657 |
|
---|
658 | $self->{'num_processed'} ++;
|
---|
659 | undef $doc_obj;
|
---|
660 | }
|
---|
661 | # delete any temp files that we may have created
|
---|
662 | $self->clean_up_after_doc_obj_processing();
|
---|
663 |
|
---|
664 | # if process_status == 1, then the file has been processed.
|
---|
665 | return $process_status;
|
---|
666 |
|
---|
667 | }
|
---|
668 |
|
---|
669 | # returns undef if file is rejected by the plugin
|
---|
670 | sub process {
|
---|
671 | my $self = shift (@_);
|
---|
672 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
|
---|
673 |
|
---|
674 | gsprintf(STDERR, "BasePlugin::process {common.must_be_implemented}\n") && die "\n";
|
---|
675 |
|
---|
676 | return undef; # never gets here
|
---|
677 | }
|
---|
678 |
|
---|
679 | # overwrite this method to delete any temp files that we have created
|
---|
680 | sub clean_up_after_doc_obj_processing {
|
---|
681 | my $self = shift(@_);
|
---|
682 |
|
---|
683 | }
|
---|
684 |
|
---|
685 | # write_file -- used by ConvertToPlug, for example in post processing
|
---|
686 | #
|
---|
687 | # where should this go, is here the best place??
|
---|
688 | sub utf8_write_file {
|
---|
689 | my $self = shift (@_);
|
---|
690 | my ($textref, $filename) = @_;
|
---|
691 |
|
---|
692 | if (!open (FILE, ">$filename")) {
|
---|
693 | gsprintf(STDERR, "ConvertToPlug::write_file {ConvertToPlug.could_not_open_for_writing} ($!)\n", $filename);
|
---|
694 | die "\n";
|
---|
695 | }
|
---|
696 | print FILE $$textref;
|
---|
697 |
|
---|
698 | close FILE;
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | sub filename_based_title
|
---|
703 | {
|
---|
704 | my $self = shift (@_);
|
---|
705 | my ($file) = @_;
|
---|
706 |
|
---|
707 | my $file_derived_title = $file;
|
---|
708 | $file_derived_title =~ s/_/ /g;
|
---|
709 | $file_derived_title =~ s/\..*?$//;
|
---|
710 |
|
---|
711 | return $file_derived_title;
|
---|
712 | }
|
---|
713 |
|
---|
714 |
|
---|
715 | sub title_fallback
|
---|
716 | {
|
---|
717 | my $self = shift (@_);
|
---|
718 | my ($doc_obj,$section,$file) = @_;
|
---|
719 |
|
---|
720 | if (!defined $doc_obj->get_metadata_element ($section, "Title") or $doc_obj->get_metadata_element($section, "Title") eq "") {
|
---|
721 |
|
---|
722 | my $file_derived_title = $self->filename_to_utf8_metadata($self->filename_based_title($file));
|
---|
723 | if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
|
---|
724 | $doc_obj->add_utf8_metadata ($section, "Title", $file_derived_title);
|
---|
725 | }
|
---|
726 | else {
|
---|
727 | $doc_obj->set_utf8_metadata ($section, "Title", $file_derived_title);
|
---|
728 | }
|
---|
729 | }
|
---|
730 |
|
---|
731 | }
|
---|
732 |
|
---|
733 | # add any extra metadata that's been passed around from one
|
---|
734 | # plugin to another.
|
---|
735 | # extra_metadata uses add_utf8_metadata so it expects metadata values
|
---|
736 | # to already be in utf8
|
---|
737 | sub extra_metadata {
|
---|
738 | my $self = shift (@_);
|
---|
739 | my ($doc_obj, $cursection, $metadata) = @_;
|
---|
740 |
|
---|
741 | my $associate_tail_re = $self->{'associate_tail_re'};
|
---|
742 |
|
---|
743 | foreach my $field (keys(%$metadata)) {
|
---|
744 | # $metadata->{$field} may be an array reference
|
---|
745 | if ($field eq "gsdlassocfile_tobe") {
|
---|
746 | # 'gsdlassocfile_tobe' is artificially introduced metadata
|
---|
747 | # that is used to signal that certain additional files should
|
---|
748 | # be tied to this document. Useful in situations where a
|
---|
749 | # metadata pass in the plugin pipeline works out some files
|
---|
750 | # need to be associated with a document, but the document hasn't
|
---|
751 | # been formed yet.
|
---|
752 | my $equiv_form = "";
|
---|
753 | foreach my $gaf (@{$metadata->{$field}}) {
|
---|
754 | my ($full_filename,$mimetype) = ($gaf =~ m/^(.*):(.*):$/);
|
---|
755 | my ($tail_filename) = ($full_filename =~ /^.*[\/\\](.+?)$/);
|
---|
756 | my $filename = $full_filename;
|
---|
757 | $doc_obj->associate_file($full_filename,$tail_filename,$mimetype);
|
---|
758 |
|
---|
759 | # work out extended tail extension (i.e. matching tail re)
|
---|
760 |
|
---|
761 | my ($file_prefix,$file_extended_ext)
|
---|
762 | = &util::get_prefix_and_tail_by_regex($tail_filename,$associate_tail_re);
|
---|
763 | my ($pre_doc_ext) = ($file_extended_ext =~ m/^(.*)\..*$/);
|
---|
764 |
|
---|
765 | my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
|
---|
766 | my $start_doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/{Or}{[parent(Top):archivedir],[archivedir]}/$tail_filename\">";
|
---|
767 | my $srcicon = "_icon".$doc_ext."_";
|
---|
768 | my $end_doclink = "</a>";
|
---|
769 |
|
---|
770 | my $assoc_form = "$start_doclink\{If\}{$srcicon,$srcicon,$doc_ext\}$end_doclink";
|
---|
771 |
|
---|
772 | if (defined $pre_doc_ext) {
|
---|
773 | # for metadata such as [mp3._edited] [mp3._full] ...
|
---|
774 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.$pre_doc_ext", $assoc_form);
|
---|
775 | }
|
---|
776 |
|
---|
777 | # for multiple metadata such as [mp3.assoclink]
|
---|
778 | $doc_obj->add_utf8_metadata ($cursection, "$doc_ext.assoclink", $assoc_form);
|
---|
779 |
|
---|
780 | $equiv_form .= " $assoc_form";
|
---|
781 | }
|
---|
782 | $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
|
---|
783 | }
|
---|
784 | elsif (ref ($metadata->{$field}) eq "ARRAY") {
|
---|
785 | map {
|
---|
786 | $doc_obj->add_utf8_metadata ($cursection, $field, $_);
|
---|
787 | } @{$metadata->{$field}};
|
---|
788 | } else {
|
---|
789 | $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
|
---|
790 | }
|
---|
791 | }
|
---|
792 | }
|
---|
793 |
|
---|
794 |
|
---|
795 | sub compile_stats {
|
---|
796 | my $self = shift(@_);
|
---|
797 | my ($stats) = @_;
|
---|
798 |
|
---|
799 | $stats->{'num_processed'} += $self->{'num_processed'};
|
---|
800 | $stats->{'num_not_processed'} += $self->{'num_not_processed'};
|
---|
801 | $stats->{'num_archives'} += $self->{'num_archives'};
|
---|
802 |
|
---|
803 | }
|
---|
804 |
|
---|
805 | sub associate_cover_image {
|
---|
806 | my $self = shift;
|
---|
807 | my ($doc_obj, $filename) = @_;
|
---|
808 |
|
---|
809 | $filename =~ s/\.[^\\\/\.]+$/\.jpg/;
|
---|
810 | if (exists $self->{'covers_missing_cache'}->{$filename}) {
|
---|
811 | # don't stat() for existence eg for multiple document input files
|
---|
812 | # (eg SplitPlug)
|
---|
813 | return;
|
---|
814 | }
|
---|
815 |
|
---|
816 | my $top_section=$doc_obj->get_top_section();
|
---|
817 |
|
---|
818 | if (-e $filename) {
|
---|
819 | $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
|
---|
820 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
821 | } else {
|
---|
822 | my $upper_filename = $filename;
|
---|
823 | $upper_filename =~ s/jpg$/JPG/;
|
---|
824 | if (-e $upper_filename) {
|
---|
825 | $doc_obj->associate_file($upper_filename, "cover.jpg",
|
---|
826 | "image/jpeg");
|
---|
827 | $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
|
---|
828 | } else {
|
---|
829 | # file doesn't exist, so record the fact that it's missing so
|
---|
830 | # we don't stat() again (stat is slow)
|
---|
831 | $self->{'covers_missing_cache'}->{$filename} = 1;
|
---|
832 | }
|
---|
833 | }
|
---|
834 |
|
---|
835 | }
|
---|
836 |
|
---|
837 |
|
---|
838 | # Overridden by exploding plugins (eg. ISISPlug)
|
---|
839 | sub clean_up_after_exploding
|
---|
840 | {
|
---|
841 | my $self = shift(@_);
|
---|
842 | }
|
---|
843 |
|
---|
844 |
|
---|
845 |
|
---|
846 | 1;
|
---|