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
RevLine 
[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]27package MetadataCSVPlugin;
[12606]28
29
[15872]30use BasePlugin;
[24547]31use MetadataRead;
32
[12606]33use strict;
[22852]34no strict 'refs';
[24951]35
36use extrametautil;
[17717]37use multiread;
[12606]38
[24756]39use Encode;
[17717]40
[24547]41# methods with identical signatures take precedence in the order given in the ISA list.
[12606]42sub BEGIN {
[24547]43 @MetadataCSVPlugin::ISA = ('MetadataRead', 'BasePlugin');
[12606]44}
45
46
[16386]47my $arguments = [
48 { 'name' => "process_exp",
49 'desc' => "{BasePlugin.process_exp}",
[12606]50 'type' => "regexp",
51 'reqd' => "no",
[16386]52 'deft' => &get_default_process_exp() }
[12606]53
[16386]54];
[12606]55
[16386]56
[15872]57my $options = { 'name' => "MetadataCSVPlugin",
58 'desc' => "{MetadataCSVPlugin.desc}",
[12606]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
[15872]70 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
71 push(@{$hashArgOptLists->{"OptList"}},$options);
[12606]72
[15872]73 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
[12606]74
75 return bless $self, $class;
76}
77
78
79sub get_default_process_exp
80{
81 return q^(?i)\.csv$^;
82}
83
[20771]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
[12606]102sub metadata_read
103{
104 my $self = shift (@_);
[19493]105 my ($pluginfo, $base_dir, $file, $block_hash,
106 $extrametakeys, $extrametadata, $extrametafile,
[23212]107 $processor, $gli, $aux) = @_;
[12606]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 }
[15872]114 print STDERR "\n<Processing n='$file' p='MetadataCSVPlugin'>\n" if ($gli);
115 print STDERR "MetadataCSVPlugin: processing $file\n" if ($self->{'verbosity'}) > 1;
[12606]116
[22852]117 my $outhandle = $self->{'outhandle'};
118 my $failhandle = $self->{'failhandle'};
119
[16386]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
[23561]121 &util::block_filename($block_hash,$filename);
[16386]122
[24736]123
[12606]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();
[15872]128 $csv_file_reader->set_handle('MetadataCSVPlugin::CSV_FILE');
[12606]129 $csv_file_reader->read_file(\$csv_file_content);
[24736]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
[12606]136 close(CSV_FILE);
137
138 # Split the file into lines and read the first line (contains the metadata names)
[13173]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);
[12606]142 my $csv_file_field_line = shift(@csv_file_lines);
143 my @csv_file_fields = split(/\,/, $csv_file_field_line);
[13174]144 my $found_filename_field = 0;
[12606]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;
[13174]148 if ($csv_file_fields[$i] eq "Filename") {
149 $found_filename_field = 1;
150 }
[12606]151 }
152
[13174]153 if (!$found_filename_field) {
[22852]154 $self->print_error($outhandle, $failhandle, $gli, $filename, "No Filename field in CSV file");
[13174]155 return -1; # error
156 }
[12606]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*$/);
[13174]161 my $orig_csv_line = $csv_line;
[12606]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])) {
[13174]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);
[12606]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])) {
[13174]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);
[12606]185 }
186 }
187 # The line must be formatted incorrectly
188 else {
[22852]189 $self->print_error($outhandle, $failhandle, $gli, $filename, "Badly formatted CSV line: $csv_line");
[12606]190 last;
191 }
192
193 $i++;
194 }
195
196 # We can't associate any metadata without knowing the file to associate it with
[13174]197 my $csv_line_filename_array = $csv_line_metadata{"Filename"};
198 if (!defined $csv_line_filename_array) {
[22852]199 $self->print_error($outhandle, $failhandle, $gli, $filename, "No Filename metadata in CSV line: $orig_csv_line");
[12606]200 next;
201 }
[13174]202 my $csv_line_filename = shift(@$csv_line_filename_array);
[12606]203 delete $csv_line_metadata{"Filename"};
204
[20000]205
[12606]206 # Associate the metadata now
[20000]207 $csv_line_filename = &util::filename_to_regex($csv_line_filename);
208
[24951]209 &extrametautil::setmetadata($extrametadata, $csv_line_filename, \%csv_line_metadata);
210 &extrametautil::addmetakey($extrametakeys, $csv_line_filename);
[20804]211 # record which file the metadata came from
[24951]212 if (!defined &extrametautil::getmetafile($extrametafile, $csv_line_filename)) {
213 &extrametautil::setmetafile($extrametafile, $csv_line_filename, {});
[20804]214 }
[20771]215 # maps the file to full path
[24951]216 &extrametautil::setmetafile_for_named_file($extrametafile, $csv_line_filename, $file, $filename);
[12606]217 }
218}
219
[22852]220sub print_error
221{
[12606]222
[22852]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}
[12606]2301;
Note: See TracBrowser for help on using the repository browser.