[12606] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
[15872] | 3 | # MetadataCSVPlugin.pm -- A plugin for metadata in comma-separated value format
|
---|
[12606] | 4 | #
|
---|
| 5 | # A component of the Greenstone digital library software
|
---|
| 6 | # from the New Zealand Digital Library Project at the
|
---|
| 7 | # University of Waikato, New Zealand.
|
---|
| 8 | #
|
---|
| 9 | # Copyright 2006 New Zealand Digital Library Project
|
---|
| 10 | #
|
---|
| 11 | # This program is free software; you can redistribute it and/or modify
|
---|
| 12 | # it under the terms of the GNU General Public License as published by
|
---|
| 13 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 14 | # (at your option) any later version.
|
---|
| 15 | #
|
---|
| 16 | # This program is distributed in the hope that it will be useful,
|
---|
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | # GNU General Public License for more details.
|
---|
| 20 | #
|
---|
| 21 | # You should have received a copy of the GNU General Public License
|
---|
| 22 | # along with this program; if not, write to the Free Software
|
---|
| 23 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | #
|
---|
| 25 | ###########################################################################
|
---|
| 26 |
|
---|
[15872] | 27 | package MetadataCSVPlugin;
|
---|
[12606] | 28 |
|
---|
| 29 |
|
---|
[15872] | 30 | use BasePlugin;
|
---|
[12606] | 31 | use strict;
|
---|
| 32 |
|
---|
[17717] | 33 | use multiread;
|
---|
[12606] | 34 |
|
---|
[17717] | 35 |
|
---|
[12606] | 36 | sub BEGIN {
|
---|
[15872] | 37 | @MetadataCSVPlugin::ISA = ('BasePlugin');
|
---|
[12606] | 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
[16386] | 41 | my $arguments = [
|
---|
| 42 | { 'name' => "process_exp",
|
---|
| 43 | 'desc' => "{BasePlugin.process_exp}",
|
---|
[12606] | 44 | 'type' => "regexp",
|
---|
| 45 | 'reqd' => "no",
|
---|
[16386] | 46 | 'deft' => &get_default_process_exp() }
|
---|
[12606] | 47 |
|
---|
[16386] | 48 | ];
|
---|
[12606] | 49 |
|
---|
[16386] | 50 |
|
---|
[15872] | 51 | my $options = { 'name' => "MetadataCSVPlugin",
|
---|
| 52 | 'desc' => "{MetadataCSVPlugin.desc}",
|
---|
[12606] | 53 | 'abstract' => "no",
|
---|
| 54 | 'inherits' => "yes",
|
---|
| 55 | 'args' => $arguments };
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | sub new
|
---|
| 59 | {
|
---|
| 60 | my ($class) = shift (@_);
|
---|
| 61 | my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
|
---|
| 62 | push(@$pluginlist, $class);
|
---|
| 63 |
|
---|
[15872] | 64 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 65 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[12606] | 66 |
|
---|
[15872] | 67 | my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
|
---|
[12606] | 68 |
|
---|
| 69 | return bless $self, $class;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
| 73 | sub get_default_process_exp
|
---|
| 74 | {
|
---|
| 75 | return q^(?i)\.csv$^;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | sub metadata_read
|
---|
| 79 | {
|
---|
| 80 | my $self = shift (@_);
|
---|
[17300] | 81 | my ($pluginfo, $base_dir, $file, $block_hash, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
|
---|
[12606] | 82 |
|
---|
| 83 | # Read metadata from CSV files
|
---|
| 84 | my $filename = &util::filename_cat($base_dir, $file);
|
---|
| 85 | if ($filename !~ /\.csv$/ || !-f $filename) {
|
---|
| 86 | return undef;
|
---|
| 87 | }
|
---|
[15872] | 88 | print STDERR "\n<Processing n='$file' p='MetadataCSVPlugin'>\n" if ($gli);
|
---|
| 89 | print STDERR "MetadataCSVPlugin: processing $file\n" if ($self->{'verbosity'}) > 1;
|
---|
[12606] | 90 |
|
---|
[16386] | 91 | # add the file to the block list so that it won't be processed in read, as we will do all we can with it here
|
---|
| 92 | $block_hash->{'file_blocks'}->{$filename} = 1;
|
---|
| 93 |
|
---|
[12606] | 94 | # Read the CSV file to get the metadata
|
---|
| 95 | my $csv_file_content;
|
---|
| 96 | open(CSV_FILE, "$filename");
|
---|
| 97 | my $csv_file_reader = new multiread();
|
---|
[15872] | 98 | $csv_file_reader->set_handle('MetadataCSVPlugin::CSV_FILE');
|
---|
[12606] | 99 | $csv_file_reader->read_file(\$csv_file_content);
|
---|
| 100 | close(CSV_FILE);
|
---|
| 101 |
|
---|
| 102 | # Split the file into lines and read the first line (contains the metadata names)
|
---|
[13173] | 103 | $csv_file_content =~ s/\r/\n/g; # Handle non-Unix line endings
|
---|
| 104 | $csv_file_content =~ s/\n+/\n/g;
|
---|
| 105 | my @csv_file_lines = split(/\n/, $csv_file_content);
|
---|
[12606] | 106 | my $csv_file_field_line = shift(@csv_file_lines);
|
---|
| 107 | my @csv_file_fields = split(/\,/, $csv_file_field_line);
|
---|
[13174] | 108 | my $found_filename_field = 0;
|
---|
[12606] | 109 | for (my $i = 0; $i < scalar(@csv_file_fields); $i++) {
|
---|
| 110 | # Remove any spaces from the field names
|
---|
| 111 | $csv_file_fields[$i] =~ s/ //g;
|
---|
[13174] | 112 | if ($csv_file_fields[$i] eq "Filename") {
|
---|
| 113 | $found_filename_field = 1;
|
---|
| 114 | }
|
---|
[12606] | 115 | }
|
---|
| 116 |
|
---|
[13174] | 117 | if (!$found_filename_field) {
|
---|
[15872] | 118 | print STDERR "MetadataCSVPlugin Error: No Filename field in CSV file: $filename\n";
|
---|
[13174] | 119 | return -1; # error
|
---|
| 120 | }
|
---|
[12606] | 121 | # Read each line of the file and assign the metadata appropriately
|
---|
| 122 | foreach my $csv_line (@csv_file_lines) {
|
---|
| 123 | # Ignore lines containing only whitespace
|
---|
| 124 | next if ($csv_line =~ /^\s*$/);
|
---|
[13174] | 125 | my $orig_csv_line = $csv_line;
|
---|
[12606] | 126 | # Build a hash of metadata name to metadata value for this line
|
---|
| 127 | my %csv_line_metadata;
|
---|
| 128 | my $i = 0;
|
---|
| 129 | $csv_line .= ","; # To make the regular expressions simpler
|
---|
| 130 | while ($csv_line ne "") {
|
---|
| 131 | # Metadata values containing commas are quoted
|
---|
| 132 | if ($csv_line =~ s/^\"(.*?)\"\,//) {
|
---|
| 133 | # Only bother with non-empty values
|
---|
| 134 | if ($1 ne "" && defined($csv_file_fields[$i])) {
|
---|
[13174] | 135 | if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
|
---|
| 136 | $csv_line_metadata{$csv_file_fields[$i]} = [];
|
---|
| 137 | }
|
---|
| 138 | push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
|
---|
[12606] | 139 | }
|
---|
| 140 | }
|
---|
| 141 | # Normal comma-separated case
|
---|
| 142 | elsif ($csv_line =~ s/^(.*?)\,//) {
|
---|
| 143 | # Only bother with non-empty values
|
---|
| 144 | if ($1 ne "" && defined($csv_file_fields[$i])) {
|
---|
[13174] | 145 | if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
|
---|
| 146 | $csv_line_metadata{$csv_file_fields[$i]} = [];
|
---|
| 147 | }
|
---|
| 148 | push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
|
---|
[12606] | 149 | }
|
---|
| 150 | }
|
---|
| 151 | # The line must be formatted incorrectly
|
---|
| 152 | else {
|
---|
[15872] | 153 | print STDERR "MetadataCSVPlugin Error: Badly formatted CSV line: $csv_line.\n";
|
---|
[12606] | 154 | last;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | $i++;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | # We can't associate any metadata without knowing the file to associate it with
|
---|
[13174] | 161 | my $csv_line_filename_array = $csv_line_metadata{"Filename"};
|
---|
| 162 | if (!defined $csv_line_filename_array) {
|
---|
[15872] | 163 | print STDERR "MetadataCSVPlugin Error: No Filename metadata in CSV line: $orig_csv_line\n";
|
---|
[12606] | 164 | next;
|
---|
| 165 | }
|
---|
[13174] | 166 | my $csv_line_filename = shift(@$csv_line_filename_array);
|
---|
[12606] | 167 | delete $csv_line_metadata{"Filename"};
|
---|
| 168 |
|
---|
| 169 | # Associate the metadata now
|
---|
| 170 | $extrametadata->{$csv_line_filename} = \%csv_line_metadata;
|
---|
| 171 | push(@$extrametakeys, $csv_line_filename);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | 1;
|
---|