source: main/trunk/greenstone2/perllib/plugins/EmbeddedMetadataPlugin.pm@ 23810

Last change on this file since 23810 was 23810, checked in by sjm84, 13 years ago

Fixing EmbeddedMetadataPlugin to be Unicode compatible

File size: 6.7 KB
Line 
1###########################################################################
2#
3# EmbeddedMetadataPlugin.pm -- A plugin for EXIF
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 2007 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
27
28package EmbeddedMetadataPlugin;
29
30use BasePlugin;
31
32use Encode;
33use Image::ExifTool qw(:Public);
34use strict;
35
36no strict 'refs'; # allow filehandles to be variables and viceversa
37
38
39sub BEGIN
40{
41 @EmbeddedMetadataPlugin::ISA = ('BasePlugin');
42 binmode(STDERR, ":utf8");
43}
44
45my $encoding_plus_auto_list =
46 [ { 'name' => "auto",
47 'desc' => "{ReadTextFile.input_encoding.auto}" } ];
48push(@{$encoding_plus_auto_list},@{$BasePlugin::encoding_list});
49
50my $arguments =
51 [ { 'name' => "metadata_field_separator",
52 'desc' => "{HTMLPlugin.metadata_field_separator}",
53 'type' => "string",
54 'deft' => "" },
55 { 'name' => "input_encoding",
56 'desc' => "{ReadTextFile.input_encoding}",
57 'type' => "enum",
58 'list' => $encoding_plus_auto_list,
59 'reqd' => "no",
60 'deft' => "auto" }
61];
62
63
64my $options = { 'name' => "EmbeddedMetadataPlugin",
65 'desc' => "{EmbeddedMetadataPlugin.desc}",
66 'abstract' => "no",
67 'inherits' => "yes",
68 'args' => $arguments };
69
70sub new()
71{
72 my ($class) = shift (@_);
73 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
74 push(@$pluginlist, $class);
75
76 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
77 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
78
79 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
80
81
82 # Create a new Image::ExifTool object
83 my $exifTool = new Image::ExifTool;
84 $exifTool->Options(Duplicates => 0);
85 $exifTool->Options(PrintConv => 0);
86 $exifTool->Options(Unknown => 1);
87 $exifTool->Options('Verbose');
88 $self->{'exiftool'} = $exifTool;
89
90
91 return bless $self, $class;
92}
93
94
95# Need to think some more about this
96sub get_default_process_exp()
97{
98 return ".*";
99 #q^(?i)\.(wma|wmv|jpe?g|gif)$^;
100}
101
102
103# This plugin doesn't block any files
104#sub get_default_block_exp()
105#{
106# return '';
107#}
108
109
110sub extractEmbeddedMetadata()
111{
112 my $self = shift(@_);
113 my ($file, $filename, $extrametadata, $extrametakeys) = @_;
114
115 my %exif_metadata = ();
116
117 my $verbosity = $self->{'verbosity'};
118 my $outhandle = $self->{'outhandle'};
119
120 my $metadata_count = 0;
121
122 my $separator = $self->{'metadata_field_separator'};
123 if ($separator eq "") {
124 undef $separator;
125 }
126
127 my @group_list = Image::ExifTool::GetAllGroups(0);
128 foreach my $group (@group_list)
129 {
130## print STDERR "**** group = $group\n";
131
132 # Extract meta information from an image
133 $self->{'exiftool'}->Options(Group0 => [$group]);
134 $self->{'exiftool'}->ExtractInfo($filename);
135
136 # Get list of tags in the order they were found in the file
137 my @tag_list = $self->{'exiftool'}->GetFoundTags('File');
138 foreach my $tag (@tag_list)
139 {
140### print STDERR "**** tag = $tag\n";
141
142 # Strip any numbering suffix
143 $tag =~ s/^([^\s]+)\s.*$/$1/i;
144 my $value = $self->{'exiftool'}->GetValue($tag);
145 if (defined $value && $value =~ /[a-z0-9]+/i) {
146
147 my $field = "ex.$group.$tag";
148
149 my $encoding = $self->{'input_encoding'};
150 if($encoding eq "auto")
151 {
152 $encoding = "utf8"
153 }
154
155
156
157 if (!defined $exif_metadata{$field})
158 {
159 $exif_metadata{$field} = [];
160 }
161
162 $field = Encode::decode($encoding,$field);
163 my $metadata_done = 0;
164 if (ref $value eq 'SCALAR') {
165
166 if ($$value =~ /^Binary data/) {
167
168 $value = "($$value)";
169 }
170 else {
171
172 my $len = length($$value);
173 $value = "(Binary data $len bytes)";
174 }
175 }
176 elsif (ref $value eq 'ARRAY') {
177 $metadata_done = 1;
178 foreach my $v (@$value) {
179 $v = Encode::decode($encoding,$v);
180 push (@{$exif_metadata{$field}}, $self->gsSafe($v));
181 ++$metadata_count;
182 }
183 }
184 else {
185 $value = Encode::decode($encoding,$value);
186 if (defined $separator) {
187 my @vs = split($separator, $value);
188 $metadata_done = 1;
189 foreach my $v (@vs) {
190 if ($v =~ /\S/) {
191 push (@{$exif_metadata{$field}}, $self->gsSafe($v));
192 ++$metadata_count;
193 }
194 }
195 }
196 }
197 if (!$metadata_done) {
198 push (@{$exif_metadata{$field}}, $self->gsSafe($value));
199 ++$metadata_count;
200 }
201 }
202 }
203 }
204
205
206 if ($metadata_count > 0) {
207 print $outhandle " Extracted $metadata_count pieces of metadata from $filename EXIF block\n";
208 }
209
210 # Protect windows directory chars \
211 $file = &util::filename_to_regex($file);
212
213 # Associate the metadata now
214
215 $extrametadata->{$file} = \%exif_metadata;
216 push(@$extrametakeys, $file);
217
218}
219
220
221sub metadata_read()
222{
223 my $self = shift (@_);
224 my ($pluginfo, $base_dir, $file, $block_hash,
225 $extrametakeys, $extrametadata, $extrametafile,
226 $processor, $gli, $aux) = @_;
227
228 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
229
230 # we don't want to process directories
231 if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
232 return undef;
233 }
234 print STDERR "\n<Processing n='$file' p='EmbeddedMetadataPlugin'>\n" if ($gli);
235 print STDERR "EmbeddedMetadataPlugin: processing $file\n" if ($self->{'verbosity'}) > 1;
236
237
238 $self->extractEmbeddedMetadata($filename_no_path,$filename_full_path,
239 $extrametadata,$extrametakeys);
240
241
242 return undef;
243}
244
245
246sub process()
247{
248 # not used
249 return undef;
250}
251
252sub gsSafe()
253 {
254 my $self = shift(@_);
255 my ($text) = @_;
256 # Replace dangerous characters
257 $text =~ s/\(/&#40;/g;
258 $text =~ s/\)/&#41;/g;
259 $text =~ s/,/&#44;/g;
260 $text =~ s/\</&#60;/g;
261 $text =~ s/\</&#62;/g;
262 $text =~ s/\[/&#91;/g;
263 $text =~ s/\]/&#93;/g;
264 # Done
265 return $text;
266 }
267
2681;
Note: See TracBrowser for help on using the repository browser.