source: gsdl/trunk/perllib/plugins/BasePlugin.pm@ 16520

Last change on this file since 16520 was 16520, checked in by kjdon, 16 years ago

made smart_block option description say deprecated, and added a common.deprecated string

  • Property svn:keywords set to Author Date Id Revision
File size: 25.1 KB
RevLine 
[537]1###########################################################################
2#
[15868]3# BasePlugin.pm -- base class for all the import plugins
[537]4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
[9413]8# Copyright (C) 1999-2005 New Zealand Digital Library Project
[537]9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
[4]25
[15868]26package BasePlugin;
[2219]27
[10254]28use strict;
29no strict 'subs';
30no strict 'refs'; # allow filehandles to be variables and viceversa
[9413]31
[8892]32use File::Basename;
33
[1219]34use multiread;
[1870]35use encodings;
[11389]36use unicode;
[1242]37use doc;
[7645]38eval "require diagnostics"; # some perl distros (eg mac) don't have this
[2751]39use ghtml;
[9413]40use gsprintf 'gsprintf';
[4]41
[15868]42use PrintInfo;
[10218]43
[15868]44BEGIN {
45 @BasePlugin::ISA = ( 'PrintInfo' );
46}
[5681]47
[15868]48our $encoding_list =
[10218]49 [ { 'name' => "ascii",
[16014]50 'desc' => "{BasePlugin.encoding.ascii}" },
[4744]51 { 'name' => "utf8",
[16014]52 'desc' => "{BasePlugin.encoding.utf8}" },
[4744]53 { 'name' => "unicode",
[16014]54 'desc' => "{BasePlugin.encoding.unicode}" } ];
[3540]55
[10620]56my $e = $encodings::encodings;
57foreach 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
[15868]63 push(@{$encoding_list},$hashEncode);
[10620]64}
65
[15868]66our $encoding_plus_auto_list =
67 [ { 'name' => "auto",
[16014]68 'desc' => "{BasePlugin.filename_encoding.auto}" } ];
[10620]69
[15868]70push(@{$encoding_plus_auto_list},@{$encoding_list});
71
[4873]72my $arguments =
[3540]73 [ { 'name' => "process_exp",
[15868]74 'desc' => "{BasePlugin.process_exp}",
[6408]75 'type' => "regexp",
[3540]76 'deft' => "",
77 'reqd' => "no" },
[16390]78 { 'name' => "no_blocking",
79 'desc' => "{BasePlugin.no_blocking}",
80 'type' => "flag",
81 'reqd' => "no"},
[3540]82 { 'name' => "block_exp",
[15868]83 'desc' => "{BasePlugin.block_exp}",
[6408]84 'type' => "regexp",
[3540]85 'deft' => "",
86 'reqd' => "no" },
[8892]87 { 'name' => "associate_ext",
[15868]88 'desc' => "{BasePlugin.associate_ext}",
[8892]89 'type' => "string",
90 'reqd' => "no" },
[11122]91 { 'name' => "associate_tail_re",
[15868]92 'desc' => "{BasePlugin.associate_tail_re}",
[11122]93 'type' => "string",
94 'reqd' => "no" },
[15018]95 { 'name' => "use_as_doc_identifier",
[15868]96 'desc' => "{BasePlugin.use_as_doc_identifier}",
[15018]97 'type' => "string",
98 'reqd' => "no" ,
99 'deft' => "" } ,
[15868]100 { 'name' => "no_cover_image",
101 'desc' => "{BasePlugin.no_cover_image}",
[3540]102 'type' => "flag",
103 'reqd' => "no" },
[15868]104 { 'name' => "filename_encoding",
105 'desc' => "{BasePlugin.filename_encoding}",
106 'type' => "enum",
107 'deft' => "auto",
108 'list' => $encoding_plus_auto_list,
[16390]109 'reqd' => "no" },
110 { 'name' => "smart_block",
[16520]111 'desc' => "{common.deprecated}. {BasePlugin.smart_block}",
[16390]112 'type' => "flag",
113 'reqd' => "no",
114 'hiddengli' => "yes" } # deprecated, but leave in for old collections
115
[15868]116
117 ];
[3540]118
[9398]119
[15868]120my $options = { 'name' => "BasePlugin",
121 'desc' => "{BasePlugin.desc}",
[6408]122 'abstract' => "yes",
123 'inherits' => "no",
[4750]124 'args' => $arguments };
[3540]125
[4778]126
[4]127sub new {
[10218]128
[15868]129 my ($class) = shift (@_);
130 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
[10218]131 push(@$pluginlist, $class);
[9398]132
[15868]133 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
134 push(@{$hashArgOptLists->{"OptList"}},$options);
[10218]135
[15868]136 my $self = new PrintInfo($pluginlist, $inputargs, $hashArgOptLists);
[16390]137
138 if ($self->{'info_only'}) {
139 # don't worry about any options etc
140 return bless $self, $class;
141 }
[10579]142
[16390]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
[15868]148 my $plugin_name = (defined $pluginlist->[0]) ? $pluginlist->[0] : $class;
149 $self->{'plugin_type'} = $plugin_name;
[10579]150
[2785]151 $self->{'num_processed'} = 0;
152 $self->{'num_not_processed'} = 0;
153 $self->{'num_blocked'} = 0;
154 $self->{'num_archives'} = 0;
[8678]155 $self->{'cover_image'} = 1; # cover image is on by default
[10218]156 $self->{'cover_image'} = 0 if ($self->{'no_cover_image'});
[10579]157 #$self->{'option_list'} = $hashArgOptLists->{"OptList"};
[3540]158
[8892]159 my $associate_ext = $self->{'associate_ext'};
160 if ((defined $associate_ext) && ($associate_ext ne "")) {
[9351]161
[11122]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";
[8892]167 }
[11122]168 else {
169 my @exts = split(/,/,$associate_ext);
[8892]170
[11122]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'};
[8892]177 }
178
[15868]179 return bless $self, $class;
[11089]180
[4]181}
182
[15868]183# initialize BasePlugin options
184# if init() is overridden in a sub-class, remember to call BasePlugin::init()
[1242]185sub init {
186 my $self = shift (@_);
[2785]187 my ($verbosity, $outhandle, $failhandle) = @_;
[1242]188
189 # verbosity is passed through from the processor
190 $self->{'verbosity'} = $verbosity;
191
[2785]192 # as are the outhandle and failhandle
[1424]193 $self->{'outhandle'} = $outhandle if defined $outhandle;
[2785]194 $self->{'failhandle'} = $failhandle;
[16390]195# $self->SUPER::init(@_);
196
[1242]197 # set process_exp and block_exp to defaults unless they were
198 # explicitly set
[1244]199
200 if ((!$self->is_recursive()) and
[1242]201 (!defined $self->{'process_exp'}) || ($self->{'process_exp'} eq "")) {
[1244]202
[1242]203 $self->{'process_exp'} = $self->get_default_process_exp ();
204 if ($self->{'process_exp'} eq "") {
[1244]205 warn ref($self) . " Warning: Non-recursive plugin has no process_exp\n";
[1242]206 }
207 }
208
209 if ((!defined $self->{'block_exp'}) || ($self->{'block_exp'} eq "")) {
210 $self->{'block_exp'} = $self->get_default_block_exp ();
211 }
[11089]212
[1242]213}
214
[839]215sub begin {
216 my $self = shift (@_);
217 my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
218}
219
220sub end {
[10155]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
[15868]224 my ($self) = shift (@_);
[839]225}
226
[10155]227sub deinit {
228 # called only once, after all plugin passes have been done
229
230 my ($self) = @_;
231}
232
[15868]233sub set_incremental {
234 my $self = shift(@_);
235 my ($incremental) = @_;
236
237 $self->{'incremental'} = $incremental;
238}
239
[1242]240# this function should be overridden to return 1
241# in recursive plugins
[4]242sub is_recursive {
243 my $self = shift (@_);
244
[1242]245 return 0;
[4]246}
247
[1242]248sub get_default_block_exp {
249 my $self = shift (@_);
250
251 return "";
252}
253
254sub get_default_process_exp {
255 my $self = shift (@_);
256
257 return "";
258}
259
[16390]260# default implementation is to do nothing
261sub store_block_files {
262
[9067]263 my $self =shift (@_);
[16390]264 my ($filename_full_path, $block_hash) = @_;
265
[9067]266}
267
[16390]268# put files to block into hash
269sub 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
[9067]280#default implementation is to block a file with same name as this, but extension jpg or JPG, if cover_images is on.
281sub block_cover_image
282{
[10833]283 my $self =shift;
[16390]284 my ($filename, $block_hash) = @_;
[10833]285
[9067]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) {
[16390]293 $block_hash->{'file_blocks'}->{$coverfile} = 1;
[11089]294 }
[9067]295 }
296
297 return;
298}
[11122]299
300
[16390]301# discover all the files that should be blocked by this plugin
302# check the args ...
303sub file_block_read {
[11122]304
[8510]305 my $self = shift (@_);
[16390]306 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
[8892]307 # Keep track of filenames with same root but different extensions
[11122]308 # Used to support -associate_ext and the more generalised
309 # -associate_tail_re
[16390]310 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
[8892]311
[11122]312 my $associate_tail_re = $self->{'associate_tail_re'};
313 if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
[16390]314
[11122]315 my ($file_prefix,$file_ext)
[16390]316 = &util::get_prefix_and_tail_by_regex($filename_full_path,$associate_tail_re);
317
[8892]318 if ((defined $file_prefix) && (defined $file_ext)) {
[16390]319 my $shared_fileroot = $block_hash->{'shared_fileroot'};
[8892]320 if (!defined $shared_fileroot->{$file_prefix}) {
[11122]321 my $file_prefix_rec = { 'tie_to' => undef,
322 'exts' => {} };
[8892]323 $shared_fileroot->{$file_prefix} = $file_prefix_rec;
324 }
325
326 my $file_prefix_rec = $shared_fileroot->{$file_prefix};
327
[16390]328 if ($self->can_process_this_file($filename_full_path)) {
[8892]329 # This is the document the others should be tied to
330 $file_prefix_rec->{'tie_to'} = $file_ext;
331 }
332 else {
[11122]333 if ($file_ext =~ m/$associate_tail_re$/) {
[16390]334 # this file should be associated to the main one
[9351]335 $file_prefix_rec->{'exts'}->{$file_ext} = 1;
336 }
[8892]337 }
[11122]338
[8892]339 }
340 }
[11122]341
[16390]342 # check block expressions
343 $self->use_block_expressions($filename_full_path, $block_hash) unless $self->{'no_blocking'};
344
[9067]345 # now check whether we are actually processing this
[16390]346 if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
[9067]347 return undef; # can't recognise
348 }
[16390]349
350 $self->store_block_files($filename_full_path, $block_hash) unless $self->{'no_blocking'};
[8892]351
[11089]352 # block the cover image if there is one
353 if ($self->{'cover_image'}) {
[16390]354 $self->block_cover_image($filename_full_path, $block_hash) unless $self->{'no_blocking'};
[11089]355 }
[9067]356
357 return 1;
[8510]358}
359
[16390]360# plugins that rely on more than process_exp (eg XML plugins) can override this method
361sub can_process_this_file {
362 my $self = shift(@_);
363 my ($filename) = @_;
[8892]364
[16390]365 if ($self->{'process_exp'} ne "" && $filename =~ /$self->{'process_exp'}/) {
366 return 1;
[8892]367 }
368 return 0;
[10280]369
370}
371
[16390]372# just converts path as is to utf8.
373sub filepath_to_utf8 {
[10280]374 my $self = shift (@_);
[15868]375 my ($file, $file_encoding) = @_;
[16390]376 my $filemeta = $file;
[10280]377
[15868]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 }
[16390]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
410sub 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
[15868]420 my $dmsafe_filemeta = &ghtml::dmsafe($filemeta);
421
422 return $dmsafe_filemeta;
423
[10280]424}
425
426
[15868]427sub get_filesystem_encoding {
[10280]428
[15868]429 my $self = shift(@_);
430
[14961]431 my $outhandle = $self->{'outhandle'};
432 my $filesystem_encoding = undef;
433
434 eval {
435 use POSIX qw(locale_h);
[15868]436
437 # With only one parameter, setlocale retrieves the
438 # current value
[14961]439 my $current_locale = setlocale(LC_CTYPE);
[15868]440
[14961]441 if ($current_locale =~ m/^.*\.(.*?)$/) {
442 my $char_encoding = lc($1);
[15446]443 if ($char_encoding =~ m/^(iso)(8859)(\d{1,2})$/) {
444 $char_encoding = "$1\_$2\_$3";
445 }
446
[14961]447 $char_encoding =~ s/-/_/g;
448 $char_encoding =~ s/^utf_8$/utf8/;
[15868]449
[14961]450 if ($char_encoding =~ m/^\d+$/) {
[15607]451 if (defined $encodings::encodings->{"windows_$char_encoding"}) {
[14961]452 $char_encoding = "windows_$char_encoding";
453 }
[15607]454 elsif (defined $encodings::encodings->{"dos_$char_encoding"}) {
[14961]455 $char_encoding = "dos_$char_encoding";
456 }
457 }
[15868]458
[14961]459 if (($char_encoding =~ m/(?:ascii|utf8|unicode)/)
[15446]460 || (defined $encodings::encodings->{$char_encoding})) {
[14961]461 $filesystem_encoding = $char_encoding;
462 }
463 else {
464 print $outhandle "Warning: Unsupported character encoding '$char_encoding' from locale '$current_locale'\n";
465 }
466 }
[15868]467
[14961]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 }
[15868]475 return $filesystem_encoding;
476}
[14961]477
[15868]478# is there ever only one Source? Sometimes this will be called twice, for images etc that are converted.
479sub 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();
[14961]484
[15868]485 # UTF-8 version of filename
486 my $filemeta = $self->filename_to_utf8_metadata($filename_no_path, $file_encoding);
[16022]487 $doc_obj->set_utf8_metadata_element($top_section, "Source", $filemeta);
[14961]488
489}
[15868]490
491sub add_OID {
[14961]492
[15018]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
[15868]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
[10280]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
[15868]536
537# currently blocking has been done before it gets here - does this affect secondary plugin stuff??
[10280]538sub read_into_doc_obj {
539 my $self = shift (@_);
[16390]540 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
[10280]541
[15868]542 my $outhandle = $self->{'outhandle'};
[10280]543
[15868]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;
[10280]548
[16390]549 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
[1242]550 # create a new document
[15868]551 my $doc_obj = new doc ($filename_full_path, "indexed_doc");
[14961]552 my $top_section = $doc_obj->get_top_section();
553
[15868]554 # this should look at the plugin option too...
555 $doc_obj->set_OIDtype ($processor->{'OIDtype'}, $processor->{'OIDmetadata'});
[14961]556 $doc_obj->add_utf8_metadata($top_section, "Plugin", "$self->{'plugin_type'}");
[15868]557 $doc_obj->add_utf8_metadata($top_section, "FileSize", (-s $filename_full_path));
558
[15877]559 $self->set_Source_metadata($doc_obj, $filename_no_path);
[8166]560
[15868]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;
[2816]565 }
[1242]566
[15868]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);
[1242]574
[15868]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}
[2785]583
[15868]584sub add_dummy_text {
585 my $self = shift(@_);
586 my ($doc_obj, $section) = @_;
[2785]587
[15868]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}
[8510]593
[15868]594# does nothing. Can be overridden by subclass
595sub auto_extract_metadata {
596 my $self = shift(@_);
597 my ($doc_obj) = @_;
598}
[11122]599
[15868]600# adds cover image, associate_file options stuff. Should be called by sub class
601# read_into_doc_obj
602sub 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);
[8716]610 }
[9398]611
[15018]612
[10280]613}
[1242]614
[16390]615# implement this if you are extracting metadata for other documents
616sub 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
[15868]628# The BasePlugin read() function. This function calls read_into_doc_obj()
[10280]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
643sub read {
644 my $self = shift (@_);
[16390]645 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
[10280]646
[16390]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
[10280]651 my ($process_status,$doc_obj) = $self->read_into_doc_obj(@_);
[15868]652
[10280]653 if ((defined $process_status) && ($process_status == 1)) {
[15868]654
[10280]655 # process the document
656 $processor->process($doc_obj);
[15868]657
[10280]658 $self->{'num_processed'} ++;
659 undef $doc_obj;
[9398]660 }
[15868]661 # delete any temp files that we may have created
662 $self->clean_up_after_doc_obj_processing();
[9398]663
[10280]664 # if process_status == 1, then the file has been processed.
665 return $process_status;
666
[4]667}
668
[1244]669# returns undef if file is rejected by the plugin
[1242]670sub process {
671 my $self = shift (@_);
[11089]672 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
[1242]673
[15868]674 gsprintf(STDERR, "BasePlugin::process {common.must_be_implemented}\n") && die "\n";
[1244]675
676 return undef; # never gets here
[1242]677}
678
[15868]679# overwrite this method to delete any temp files that we have created
680sub clean_up_after_doc_obj_processing {
681 my $self = shift(@_);
[4]682
[10280]683}
[16390]684
[10280]685# write_file -- used by ConvertToPlug, for example in post processing
686#
[15868]687# where should this go, is here the best place??
[10280]688sub 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
[1219]698 close FILE;
699}
700
[10280]701
[7504]702sub 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
[9398]714
[7504]715sub title_fallback
716{
717 my $self = shift (@_);
718 my ($doc_obj,$section,$file) = @_;
719
[15868]720 if (!defined $doc_obj->get_metadata_element ($section, "Title") or $doc_obj->get_metadata_element($section, "Title") eq "") {
[7504]721
[15877]722 my $file_derived_title = $self->filename_to_utf8_metadata($self->filename_based_title($file));
[15868]723 if (!defined $doc_obj->get_metadata_element ($section, "Title")) {
724 $doc_obj->add_utf8_metadata ($section, "Title", $file_derived_title);
[9413]725 }
[15868]726 else {
727 $doc_obj->set_utf8_metadata ($section, "Title", $file_derived_title);
728 }
[9413]729 }
[15868]730
[1844]731}
[15868]732
[1219]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
737sub extra_metadata {
738 my $self = shift (@_);
739 my ($doc_obj, $cursection, $metadata) = @_;
740
[11122]741 my $associate_tail_re = $self->{'associate_tail_re'};
742
[1219]743 foreach my $field (keys(%$metadata)) {
[839]744 # $metadata->{$field} may be an array reference
[8510]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);
[11122]758
759 # work out extended tail extension (i.e. matching tail re)
760
761 my ($file_prefix,$file_extended_ext)
[16390]762 = &util::get_prefix_and_tail_by_regex($tail_filename,$associate_tail_re);
[11122]763 my ($pre_doc_ext) = ($file_extended_ext =~ m/^(.*)\..*$/);
764
[8510]765 my ($doc_ext) = ($tail_filename =~ m/^.*\.(.*)$/);
[11834]766 my $start_doclink = "<a href=\"_httpprefix_/collect/[collection]/index/assoc/{Or}{[parent(Top):archivedir],[archivedir]}/$tail_filename\">";
[8510]767 my $srcicon = "_icon".$doc_ext."_";
768 my $end_doclink = "</a>";
769
[11122]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";
[8510]781 }
782 $doc_obj->add_utf8_metadata ($cursection, "equivlink", $equiv_form);
783 }
784 elsif (ref ($metadata->{$field}) eq "ARRAY") {
[839]785 map {
[1219]786 $doc_obj->add_utf8_metadata ($cursection, $field, $_);
[839]787 } @{$metadata->{$field}};
788 } else {
[1219]789 $doc_obj->add_utf8_metadata ($cursection, $field, $metadata->{$field});
[839]790 }
791 }
792}
793
[1396]794
[2785]795sub 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'};
[2796]801 $stats->{'num_archives'} += $self->{'num_archives'};
[2785]802
803}
804
[2816]805sub associate_cover_image {
[10833]806 my $self = shift;
[2816]807 my ($doc_obj, $filename) = @_;
808
[10833]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
[9413]816 my $top_section=$doc_obj->get_top_section();
817
[2816]818 if (-e $filename) {
[13968]819 $doc_obj->associate_file($filename, "cover.jpg", "image/jpeg");
[9413]820 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
[3086]821 } else {
[10833]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");
[9413]827 $doc_obj->add_utf8_metadata($top_section, "hascover", 1);
[10833]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;
[3086]832 }
[2816]833 }
[10833]834
[2816]835}
836
[11332]837
838# Overridden by exploding plugins (eg. ISISPlug)
839sub clean_up_after_exploding
840{
841 my $self = shift(@_);
842}
843
844
[16390]845
[4]8461;
Note: See TracBrowser for help on using the repository browser.