[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # IndexPlugin.pm --
|
---|
[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 | #
|
---|
| 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 |
|
---|
[286] | 26 | # This recursive plugin processes an index.txt file.
|
---|
| 27 | # The index.txt file should contain the list of files to be
|
---|
| 28 | # included in the collection followed by any extra metadata to
|
---|
| 29 | # be associated with each file.
|
---|
| 30 |
|
---|
| 31 | # The index.txt file should be formatted as follows:
|
---|
| 32 | # The first line may be a key (beginning with key:)
|
---|
| 33 | # to name the metadata fields
|
---|
| 34 | # (e.g. key: Subject Organization Date)
|
---|
| 35 | # The following lines will contain a filename followed
|
---|
| 36 | # by the value that metadata entry is to be set to.
|
---|
| 37 | # (e.g. 'irma/iw097e 3.2 unesco 1993' will associate the
|
---|
| 38 | # metadata Subject=3.2, Organization=unesco, and Date=1993
|
---|
| 39 | # with the file irma/iw097e if the above key line was used)
|
---|
| 40 |
|
---|
| 41 | # Note that if any of the metadata fields use the Hierarchy
|
---|
| 42 | # classifier plugin then the value they're set to should
|
---|
| 43 | # correspond to the first field (the descriptor) in the
|
---|
| 44 | # appropriate classification file.
|
---|
| 45 |
|
---|
| 46 | # Metadata values may be named separately using a tag
|
---|
| 47 | # (e.g. <Subject>3.2) and this will override any name
|
---|
| 48 | # given to them by the key line.
|
---|
| 49 | # If there's no key line any unnamed metadata value will be
|
---|
| 50 | # named 'Subject'.
|
---|
| 51 |
|
---|
[15872] | 52 | package IndexPlugin;
|
---|
[3540] | 53 |
|
---|
[286] | 54 | use plugin;
|
---|
[31492] | 55 | use BaseImporter;
|
---|
[286] | 56 | use doc;
|
---|
| 57 | use util;
|
---|
| 58 | use cfgread;
|
---|
| 59 |
|
---|
[10254] | 60 | use strict;
|
---|
| 61 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
| 62 |
|
---|
[286] | 63 | sub BEGIN {
|
---|
[31492] | 64 | @IndexPlugin::ISA = ('BaseImporter');
|
---|
[286] | 65 | }
|
---|
| 66 |
|
---|
[15872] | 67 | #my $arguments = [
|
---|
| 68 | # ];
|
---|
[10254] | 69 |
|
---|
[15872] | 70 | my $options = { 'name' => "IndexPlugin",
|
---|
| 71 | 'desc' => "{IndexPlugin.desc}",
|
---|
[6408] | 72 | 'abstract' => "no",
|
---|
[3540] | 73 | 'inherits' => "yes" };
|
---|
| 74 |
|
---|
[286] | 75 | sub new {
|
---|
[10218] | 76 | my ($class) = shift (@_);
|
---|
| 77 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 78 | push(@$pluginlist, $class);
|
---|
[286] | 79 |
|
---|
[15872] | 80 | #push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 81 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[10218] | 82 |
|
---|
[31492] | 83 | my $self = new BaseImporter($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[10218] | 84 |
|
---|
[286] | 85 | return bless $self, $class;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | # return 1 if this class might recurse using $pluginfo
|
---|
| 89 | sub is_recursive {
|
---|
| 90 | my $self = shift (@_);
|
---|
| 91 |
|
---|
| 92 | return 1;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[317] | 95 | # return number of files processed, undef if can't process
|
---|
[286] | 96 | # Note that $base_dir might be "" and that $file might
|
---|
| 97 | # include directories
|
---|
| 98 | sub read {
|
---|
| 99 | my $self = shift (@_);
|
---|
[16392] | 100 | my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
|
---|
[1424] | 101 | my $outhandle = $self->{'outhandle'};
|
---|
[286] | 102 |
|
---|
| 103 | my $indexfile = &util::filename_cat($base_dir, $file, "index.txt");
|
---|
| 104 | if (!-f $indexfile) {
|
---|
| 105 | # not a directory containing an index file
|
---|
[317] | 106 | return undef;
|
---|
[286] | 107 | }
|
---|
| 108 |
|
---|
| 109 | # found an index.txt file
|
---|
[15872] | 110 | print STDERR "<Processing n='$file' p='IndexPlugin'>\n" if ($gli);
|
---|
| 111 | print $outhandle "IndexPlugin: processing $indexfile\n";
|
---|
[286] | 112 |
|
---|
| 113 | # read in the index.txt
|
---|
| 114 | my $list = &cfgread::read_cfg_file ($indexfile, undef, '^[^#]\w');
|
---|
| 115 | my @fields = ();
|
---|
| 116 | # see if there's a 'key:' line
|
---|
[317] | 117 | if (defined $list->{'key:'}) {
|
---|
| 118 | @fields = @{$list->{'key:'}};
|
---|
[286] | 119 | }
|
---|
| 120 |
|
---|
[1482] | 121 | my $index_base_dir = &util::filename_cat($base_dir, $file);
|
---|
| 122 |
|
---|
[286] | 123 | # process each document
|
---|
| 124 | my $count = 0;
|
---|
[1244] | 125 | foreach my $docfile (keys (%$list)) {
|
---|
[9853] | 126 | last if ($maxdocs != -1 && ($total_count + $count) >= $maxdocs);
|
---|
[286] | 127 | $metadata = {}; # at present we can do this as metadata
|
---|
| 128 | # will always be empty when it arrives
|
---|
[317] | 129 | # at this plugin - this might cause
|
---|
[286] | 130 | # problems if things change though
|
---|
| 131 |
|
---|
| 132 | # note that $list->{$docfile} is an array reference
|
---|
| 133 | if ($docfile !~ /key:/i) {
|
---|
[1244] | 134 | my $i = 0;
|
---|
[286] | 135 | for ($i = 0; $i < scalar (@{$list->{$docfile}}); $i ++) {
|
---|
| 136 | if ($list->{$docfile}->[$i] =~ /^<([^>]+)>(.+)$/) {
|
---|
| 137 | unless (defined ($metadata->{$1})) {
|
---|
| 138 | $metadata->{$1} = [];
|
---|
| 139 | }
|
---|
| 140 | push (@{$metadata->{$1}}, $2);
|
---|
| 141 | } elsif (scalar @fields >= $i) {
|
---|
| 142 | unless (defined ($metadata->{$fields[$i]})) {
|
---|
| 143 | $metadata->{$fields[$i]} = [];
|
---|
| 144 | }
|
---|
| 145 | push (@{$metadata->{$fields[$i]}}, $list->{$docfile}->[$i]);
|
---|
| 146 | } else {
|
---|
| 147 | $metadata->{'Subject'} = $list->{$docfile};
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
[16392] | 150 | $count += &plugin::read ($pluginfo, $index_base_dir, $docfile, $block_hash, $metadata, $processor, $maxdocs, ($total_count +$count), $gli);
|
---|
[286] | 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[317] | 154 | return $count; # was processed
|
---|
[286] | 155 | }
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | 1;
|
---|