source: main/trunk/greenstone2/perllib/plugins/MetadataCSVPlugin.pm@ 24756

Last change on this file since 24756 was 24756, checked in by ak19, 13 years ago

Diego found an oversight in the previous commit: forgot to use Encode library needed for call to decode.

  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1###########################################################################
2#
3# MetadataCSVPlugin.pm -- A plugin for metadata in comma-separated value format
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
27package MetadataCSVPlugin;
28
29
30use BasePlugin;
31use MetadataRead;
32
33use strict;
34no strict 'refs';
35use multiread;
36
37use Encode;
38
39# methods with identical signatures take precedence in the order given in the ISA list.
40sub BEGIN {
41 @MetadataCSVPlugin::ISA = ('MetadataRead', 'BasePlugin');
42}
43
44
45my $arguments = [
46 { 'name' => "process_exp",
47 'desc' => "{BasePlugin.process_exp}",
48 'type' => "regexp",
49 'reqd' => "no",
50 'deft' => &get_default_process_exp() }
51
52];
53
54
55my $options = { 'name' => "MetadataCSVPlugin",
56 'desc' => "{MetadataCSVPlugin.desc}",
57 'abstract' => "no",
58 'inherits' => "yes",
59 'args' => $arguments };
60
61
62sub new
63{
64 my ($class) = shift (@_);
65 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
66 push(@$pluginlist, $class);
67
68 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
69 push(@{$hashArgOptLists->{"OptList"}},$options);
70
71 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
72
73 return bless $self, $class;
74}
75
76
77sub get_default_process_exp
78{
79 return q^(?i)\.csv$^;
80}
81
82sub file_block_read {
83 my $self = shift (@_);
84 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
85
86 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
87
88 if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
89 return undef; # can't recognise
90 }
91
92 # set this so we know this is a metadata file - needed for incremental
93 # build
94 # if this file changes, then we need to reimport everything
95 $block_hash->{'metadata_files'}->{$filename_full_path} = 1;
96
97 return 1;
98}
99
100sub metadata_read
101{
102 my $self = shift (@_);
103 my ($pluginfo, $base_dir, $file, $block_hash,
104 $extrametakeys, $extrametadata, $extrametafile,
105 $processor, $gli, $aux) = @_;
106
107 # Read metadata from CSV files
108 my $filename = &util::filename_cat($base_dir, $file);
109 if ($filename !~ /\.csv$/ || !-f $filename) {
110 return undef;
111 }
112 print STDERR "\n<Processing n='$file' p='MetadataCSVPlugin'>\n" if ($gli);
113 print STDERR "MetadataCSVPlugin: processing $file\n" if ($self->{'verbosity'}) > 1;
114
115 my $outhandle = $self->{'outhandle'};
116 my $failhandle = $self->{'failhandle'};
117
118 # 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
119 &util::block_filename($block_hash,$filename);
120
121
122 # Read the CSV file to get the metadata
123 my $csv_file_content;
124 open(CSV_FILE, "$filename");
125 my $csv_file_reader = new multiread();
126 $csv_file_reader->set_handle('MetadataCSVPlugin::CSV_FILE');
127 $csv_file_reader->read_file(\$csv_file_content);
128
129 # Would be nice if MetadataCSVPlugin was extended to support a minus
130 # option to choose the character encoding the CSV file is in
131 # For now we will assume it is always in UTF8
132 $csv_file_content = decode("utf8",$csv_file_content);
133
134 close(CSV_FILE);
135
136 # Split the file into lines and read the first line (contains the metadata names)
137 $csv_file_content =~ s/\r/\n/g; # Handle non-Unix line endings
138 $csv_file_content =~ s/\n+/\n/g;
139 my @csv_file_lines = split(/\n/, $csv_file_content);
140 my $csv_file_field_line = shift(@csv_file_lines);
141 my @csv_file_fields = split(/\,/, $csv_file_field_line);
142 my $found_filename_field = 0;
143 for (my $i = 0; $i < scalar(@csv_file_fields); $i++) {
144 # Remove any spaces from the field names
145 $csv_file_fields[$i] =~ s/ //g;
146 if ($csv_file_fields[$i] eq "Filename") {
147 $found_filename_field = 1;
148 }
149 }
150
151 if (!$found_filename_field) {
152 $self->print_error($outhandle, $failhandle, $gli, $filename, "No Filename field in CSV file");
153 return -1; # error
154 }
155 # Read each line of the file and assign the metadata appropriately
156 foreach my $csv_line (@csv_file_lines) {
157 # Ignore lines containing only whitespace
158 next if ($csv_line =~ /^\s*$/);
159 my $orig_csv_line = $csv_line;
160 # Build a hash of metadata name to metadata value for this line
161 my %csv_line_metadata;
162 my $i = 0;
163 $csv_line .= ","; # To make the regular expressions simpler
164 while ($csv_line ne "") {
165 # Metadata values containing commas are quoted
166 if ($csv_line =~ s/^\"(.*?)\"\,//) {
167 # Only bother with non-empty values
168 if ($1 ne "" && defined($csv_file_fields[$i])) {
169 if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
170 $csv_line_metadata{$csv_file_fields[$i]} = [];
171 }
172 push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
173 }
174 }
175 # Normal comma-separated case
176 elsif ($csv_line =~ s/^(.*?)\,//) {
177 # Only bother with non-empty values
178 if ($1 ne "" && defined($csv_file_fields[$i])) {
179 if (!defined $csv_line_metadata{$csv_file_fields[$i]}) {
180 $csv_line_metadata{$csv_file_fields[$i]} = [];
181 }
182 push (@{$csv_line_metadata{$csv_file_fields[$i]}}, $1);
183 }
184 }
185 # The line must be formatted incorrectly
186 else {
187 $self->print_error($outhandle, $failhandle, $gli, $filename, "Badly formatted CSV line: $csv_line");
188 last;
189 }
190
191 $i++;
192 }
193
194 # We can't associate any metadata without knowing the file to associate it with
195 my $csv_line_filename_array = $csv_line_metadata{"Filename"};
196 if (!defined $csv_line_filename_array) {
197 $self->print_error($outhandle, $failhandle, $gli, $filename, "No Filename metadata in CSV line: $orig_csv_line");
198 next;
199 }
200 my $csv_line_filename = shift(@$csv_line_filename_array);
201 delete $csv_line_metadata{"Filename"};
202
203
204 # Associate the metadata now
205 $csv_line_filename = &util::filename_to_regex($csv_line_filename);
206
207 $extrametadata->{$csv_line_filename} = \%csv_line_metadata;
208 push(@$extrametakeys, $csv_line_filename);
209 # record which file the metadata came from
210 if (!defined $extrametafile->{$csv_line_filename}) {
211 $extrametafile->{$csv_line_filename} = {};
212 }
213 # maps the file to full path
214 $extrametafile->{$csv_line_filename}->{$file} = $filename;
215 }
216}
217
218sub print_error
219{
220
221 my $self = shift(@_);
222 my ($outhandle, $failhandle, $gli, $file, $error) = @_;
223
224 print $outhandle "MetadataCSVPlugin Error: $file: $error\n";
225 print $failhandle "MetadataCSVPlugin Error: $file: $error\n";
226 print STDERR "<ProcessingError n='$file' r='$error'/>\n" if ($gli);
227}
2281;
Note: See TracBrowser for help on using the repository browser.