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