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

Last change on this file since 24951 was 24951, checked in by ak19, 12 years ago

All perlcode that accesses extrametakeys, extrametadata, extrametafile data structures has been moved into a new perl module called extrametautil.pm. The next step will be to ensure that the file_regexes used to index into these data structures are consistent (using consistent slashes, like URL style slashes).

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