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

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