1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # plugin.pm -- functions to handle using 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 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 plugin;
|
---|
27 |
|
---|
28 | require util;
|
---|
29 |
|
---|
30 | sub load_plugins {
|
---|
31 | my ($plugin_list, $verbosity) = @_;
|
---|
32 | my @plugin_objects = ();
|
---|
33 |
|
---|
34 | $verbosity = 2 unless defined $verbosity;
|
---|
35 |
|
---|
36 | foreach $pluginoptions (@$plugin_list) {
|
---|
37 | my $pluginname = shift @$pluginoptions;
|
---|
38 | next unless defined $pluginname;
|
---|
39 |
|
---|
40 | # find the plugin
|
---|
41 | my $colplugname = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"perllib/plugins",
|
---|
42 | "${pluginname}.pm");
|
---|
43 | my $mainplugname = &util::filename_cat($ENV{'GSDLHOME'},"perllib/plugins",
|
---|
44 | "${pluginname}.pm");
|
---|
45 | if (-e $colplugname) { require $colplugname; }
|
---|
46 | elsif (-e $mainplugname) { require $mainplugname; }
|
---|
47 | else { die "ERROR - couldn't find plugin \"$pluginname\"\n"; }
|
---|
48 |
|
---|
49 | # create a plugin object
|
---|
50 | my ($plugobj);
|
---|
51 | map { $_ = "\"$_\""; } @$pluginoptions;
|
---|
52 | my $options = join (",", @$pluginoptions);
|
---|
53 | $options =~ s/\$/\\\$/g;
|
---|
54 | eval ("\$plugobj = new \$pluginname($options)");
|
---|
55 | die "$@" if $@;
|
---|
56 |
|
---|
57 | # initialize plugin
|
---|
58 | $plugobj->init($verbosity);
|
---|
59 |
|
---|
60 | # add this object to the list
|
---|
61 | push (@plugin_objects, $plugobj);
|
---|
62 | }
|
---|
63 |
|
---|
64 | return \@plugin_objects;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | sub begin {
|
---|
69 | my ($pluginfo, $base_dir, $processor, $maxdocs) = @_;
|
---|
70 |
|
---|
71 | map { $_->begin($pluginfo, $base_dir, $processor, $maxdocs); } @$pluginfo;
|
---|
72 | }
|
---|
73 |
|
---|
74 | sub read {
|
---|
75 | my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $aux) = @_;
|
---|
76 |
|
---|
77 | $maxdocs = -1 unless defined $maxdocs && $maxdocs =~ /\d/;
|
---|
78 | my $rv = 0;
|
---|
79 |
|
---|
80 | # pass this file by each of the plugins in turn until one
|
---|
81 | # is found which will process it
|
---|
82 | foreach $plugobj (@$pluginfo) {
|
---|
83 | $rv = $plugobj->read($pluginfo, $base_dir, $file,
|
---|
84 | $metadata, $processor, $maxdocs, $aux);
|
---|
85 | return $rv if defined $rv;
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (defined $processor->{'verbosity'} && $processor->{'verbosity'} >= 2) {
|
---|
89 | print STDERR "WARNING - no plugin could process " .
|
---|
90 | &util::filename_cat($base_dir,$file) . "\n";
|
---|
91 | }
|
---|
92 | return 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | sub end {
|
---|
96 | my ($pluginfo) = @_;
|
---|
97 | map { $_->end(); } @$pluginfo;
|
---|
98 | }
|
---|
99 |
|
---|
100 | 1;
|
---|