source: main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm@ 25333

Last change on this file since 25333 was 25333, checked in by kjdon, 12 years ago

added some output messages. make sure we keep previous metadata - there may be metadata for a file from a previous plugin, not just from OAIPlugin

  • Property svn:keywords set to Author Date Id Revision
File size: 17.6 KB
Line 
1###########################################################################
2#
3# OAIPlug.pm -- basic Open Archives Initiative (OAI) plugin
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 (C) 1999 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 OAIPlugin;
28
29use extrametautil;
30use unicode;
31use util;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36use ReadXMLFile;
37use ReadTextFile; # needed for subroutine textcat_get_language_encoding
38use metadatautil;
39use MetadataRead;
40use util;
41
42# methods with identical signatures take precedence in the order given in the ISA list.
43sub BEGIN {
44 @OAIPlugin::ISA = ('MetadataRead', 'ReadXMLFile', 'ReadTextFile');
45}
46
47my $set_list =
48 [ { 'name' => "auto",
49 'desc' => "{OAIPlugin.metadata_set.auto}" },
50 { 'name' => "dc",
51 'desc' => "{OAIPlugin.metadata_set.dc}" }
52 ];
53
54my $arguments =
55 [ { 'name' => "process_exp",
56 'desc' => "{BasePlugin.process_exp}",
57 'type' => "regexp",
58 'reqd' => "no",
59 'deft' => &get_default_process_exp() },
60 { 'name' => "metadata_set",
61 'desc' => "{OAIPlugin.metadata_set}",
62 'type' => "enumstring",
63 'reqd' => "no",
64 'list' => $set_list,
65 'deft' => "dc" },
66 { 'name' => "document_field",
67 'desc' => "{OAIPlugin.document_field}",
68 'type' => "metadata",
69 'reqd' => "no",
70 'deft' => "gi.Sourcedoc" }
71 ];
72
73my $options = { 'name' => "OAIPlugin",
74 'desc' => "{OAIPlugin.desc}",
75 'abstract' => "no",
76 'inherits' => "yes",
77 'explodes' => "yes",
78 'args' => $arguments };
79
80
81sub new {
82 my ($class) = shift (@_);
83 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
84 push(@$pluginlist, $class);
85
86 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
87 push(@{$hashArgOptLists->{"OptList"}},$options);
88
89 new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists,1);
90 my $self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
91
92 if ($self->{'info_only'}) {
93 # don't worry about modifying options
94 return bless $self, $class;
95 }
96 # trim any ex. from document field iff it's the only metadata namespace prefix
97 $self->{'document_field'} =~ s/^ex\.([^.]+)$/$1/;
98 return bless $self, $class;
99}
100
101sub get_default_process_exp {
102 my $self = shift (@_);
103
104 return q^(?i)(\.oai)$^;
105}
106
107sub get_doctype {
108 my $self = shift(@_);
109
110 return "OAI-PMH";
111}
112
113sub xml_start_document {
114 my $self = shift (@_);
115 $self->{'in_metadata_node'} = 0;
116 $self->{'rawxml'} = "";
117 $self->{'saved_metadata'} = {};
118}
119
120sub xml_end_document {
121}
122
123sub xml_doctype {
124 my $self = shift(@_);
125
126 my ($expat, $name, $sysid, $pubid, $internal) = @_;
127
128 ##die "" if ($name !~ /^OAI-PMH$/);
129
130 my $outhandle = $self->{'outhandle'};
131 print $outhandle "OAIPlugin: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
132 print STDERR "<Processing n='$self->{'file'}' p='OAIPlugin'>\n" if $self->{'gli'};
133
134}
135
136
137sub xml_start_tag {
138 my $self = shift(@_);
139 my ($expat,$element) = @_;
140
141 my %attr_hash = %_;
142
143 my $attr = "";
144 map { $attr .= " $_=$attr_hash{$_}"; } keys %attr_hash;
145
146 $self->{'rawxml'} .= "<$element$attr>";
147
148 if ($element eq "metadata") {
149 $self->{'in_metadata_node'} = 1;
150 $self->{'metadata_xml'} = "";
151 }
152
153 if ($self->{'in_metadata_node'}) {
154 $self->{'metadata_xml'} .= "<$element$attr>";
155 }
156}
157
158sub xml_end_tag {
159 my $self = shift(@_);
160 my ($expat, $element) = @_;
161
162 $self->{'rawxml'} .= "</$element>";
163
164 if ($self->{'in_metadata_node'}) {
165 $self->{'metadata_xml'} .= "</$element>";
166 }
167
168 if ($element eq "metadata") {
169 my $textref = \$self->{'metadata_xml'};
170 #my $metadata = $self->{'metadata'};
171 my $metadata = $self->{'saved_metadata'};
172 $self->extract_oai_metadata($textref,$metadata);
173
174 $self->{'in_metadata_node'} = 0;
175 }
176
177
178}
179
180sub xml_text {
181 my $self = shift(@_);
182 my ($expat) = @_;
183
184 $self->{'rawxml'} .= $_;
185
186 if ($self->{'in_metadata_node'}) {
187 $self->{'metadata_xml'} .= $_;
188 }
189}
190
191
192sub metadata_read {
193 my $self = shift (@_);
194
195 my ($pluginfo, $base_dir, $file, $block_hash,
196 $extrametakeys, $extrametadata, $extrametafile,
197 $processor, $gli, $aux) = @_;
198
199 # can we process this file??
200 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
201 return undef unless $self->can_process_this_file_for_metadata($filename_full_path);
202
203 print STDERR "\n<Processing n='$file' p='OAIPlugin'>\n" if ($gli);
204 print STDERR "OAIPlugin: processing $file\n" if ($self->{'verbosity'}) > 1;
205
206 if (!$self->parse_file($filename_full_path, $file, $gli)) {
207 $self->{'saved_metadata'} = undef;
208 return undef;
209 }
210
211 my $verbosity = $self->{'verbosity'};
212 my $new_metadata = $self->{'saved_metadata'};
213 $self->{'saved_metadata'} = undef;
214
215 # add the pretty metadata table as metadata
216 my $ppmd_table = $self->{'ppmd_table'};
217 $new_metadata->{'prettymd'} = $ppmd_table;
218 $self->{'ppmd_table'} = undef;
219
220 my $document_metadata_field = $self->{'document_field'};
221 my $url_array = $new_metadata->{$document_metadata_field};
222 if (!defined $url_array) {
223 # try ex.
224 $url_array = $new_metadata->{"ex.$document_metadata_field"};
225 }
226 my $num_urls = (defined $url_array) ? scalar(@$url_array) : 0;
227 ##print STDERR "$num_urls urls for $file\n";
228 my $srcdoc_exists = 0;
229 my $srcdoc_pos = 0;
230 my $filename_dir = &util::filename_head($filename_full_path);
231
232 # filenames in extrametadata must be relative to current dir, as
233 # DirectoryPlugin adds path info on itself
234 my ($filename_for_metadata) = $file =~ /([^\\\/]+)$/; # this assumes there will only be one record per oai file - is this always the case??
235 for (my $i=0; $i<$num_urls; $i++) {
236
237 if ($url_array->[$i] !~ m/^(https?|ftp):/) {
238
239 my $src_filename = &util::filename_cat($filename_dir, $url_array->[$i]);
240 if (-e $src_filename) {
241 $srcdoc_pos = $i;
242 $srcdoc_exists = 1;
243 # get the slashes the right way, use filename_cat
244 $filename_for_metadata = &util::filename_cat($url_array->[$i]);
245 last;
246 }
247 }
248 }
249
250 if ($srcdoc_exists) {
251 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 1;
252 }
253 else {
254 # save the rawxml for the source document
255 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 0;
256 $self->{'oai-files'}->{$file}->{'rawxml'} = $self->{'rawxml'};
257 $self->{'rawxml'} = "";
258 }
259
260 # return all the metadata we have extracted to the caller.
261 # Directory plug will pass it back in at read time, so we don't need to extract it again.
262
263 # Extrametadata keys should be regular expressions
264 # Indexing into the extrameta data structures requires the filename's style of slashes to be in URL format
265 # Then need to convert the filename to a regex, no longer to protect windows directory chars \, but for
266 # protecting special characters like brackets in the filepath such as "C:\Program Files (x86)\Greenstone".
267 $filename_for_metadata = &util::filepath_to_url_format($filename_for_metadata);
268 $filename_for_metadata = &util::filename_to_regex($filename_for_metadata);
269
270 # Check that we haven't already got some metadata
271 if (defined &extrametautil::getmetadata($extrametadata, $filename_for_metadata)) {
272 print STDERR "\n**** OAIPlugin: Need to merge new metadata with existing stored metadata: file = $filename_for_metadata\n" if $verbosity > 3;
273
274 my $file_metadata_table = &extrametautil::getmetadata($extrametadata, $filename_for_metadata);
275
276 foreach my $metaname (keys %{$new_metadata}) {
277 # will create new entry if one does not already exist
278 push(@{$file_metadata_table->{$metaname}}, @{$new_metadata->{$metaname}});
279 }
280
281 } else {
282 &extrametautil::setmetadata($extrametadata, $filename_for_metadata, $new_metadata);
283 &extrametautil::addmetakey($extrametakeys, $filename_for_metadata);
284 }
285
286 if ($srcdoc_exists) {
287 if (!defined &extrametautil::getmetafile($extrametafile, $filename_for_metadata)) {
288 &extrametautil::setmetafile($extrametafile, $filename_for_metadata, {});
289 }
290 #maps the file to full path
291 &extrametautil::setmetafile_for_named_file($extrametafile, $filename_for_metadata, $file, $filename_full_path);
292
293 }
294 return 1;
295
296}
297
298
299sub read {
300 my $self = shift (@_);
301
302 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
303
304 if (!defined $self->{'oai-files'}->{$file}) {
305 return undef;
306 }
307
308 my $srcdoc_exists = $self->{'oai-files'}->{$file}->{'srcdoc_exists'};
309 if ($srcdoc_exists) {
310 # do nothing more - all the metadata has been extracted and associated with the srcdoc
311 # no more need to access details of this $file => tidy up as you go
312 delete $self->{'oai-files'}->{$file};
313 return 0; # not processed here, but don't pass on to rest of plugins
314 }
315
316 my $filename = $file;
317 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
318
319 # Do encoding stuff on metadata
320 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
321
322 # create a new document
323 my $doc_obj = new doc ($filename, "indexed_doc", $self->{'file_rename_method'});
324 my $top_section = $doc_obj->get_top_section;
325 my $plugin_type = $self->{'plugin_type'};
326
327 my ($filemeta) = $file =~ /([^\\\/]+)$/;
328 my $plugin_filename_encoding = $self->{'filename_encoding'};
329 my $filename_encoding = $self->deduce_filename_encoding($file,$metadata,$plugin_filename_encoding);
330 $self->set_Source_metadata($doc_obj, $filename, $filename_encoding);
331
332 $doc_obj->add_utf8_metadata($top_section, "Language", $language);
333 $doc_obj->add_utf8_metadata($top_section, "Encoding", $encoding);
334 $doc_obj->add_utf8_metadata($top_section, "Plugin", $plugin_type);
335 $doc_obj->add_metadata($top_section, "FileFormat", "OAI");
336 $doc_obj->add_metadata($top_section, "FileSize", (-s $filename));
337
338 # include any metadata passed in from previous plugins
339 # note that this metadata is associated with the top level section
340 # this will include all the metadata from the oai file that we extracted
341 # during metadata_read
342 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
343
344 # do plugin specific processing of doc_obj
345 my $text = $self->{'oai-files'}->{$file}->{'rawxml'};
346 delete $self->{'oai-files'}->{$file};
347
348 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
349 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
350 return -1;
351 }
352
353 # do any automatic metadata extraction
354 $self->auto_extract_metadata ($doc_obj);
355
356 # add an OID
357 $self->add_OID($doc_obj);
358
359 # process the document
360 $processor->process($doc_obj);
361
362 $self->{'num_processed'} ++;
363
364 return 1; # processed the file
365}
366
367
368# do plugin specific processing of doc_obj
369sub process {
370 my $self = shift (@_);
371 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
372 my $outhandle = $self->{'outhandle'};
373
374 print STDERR "<Processing n='$file' p='OAIPlugin'>\n" if ($gli);
375 print $outhandle "OAIPlugin: processing $file\n"
376 if $self->{'verbosity'} > 1;
377
378 my $cursection = $doc_obj->get_top_section();
379
380## $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection);
381
382 # add text to document object
383
384# $$textref =~ s/<(.*?)>/$1 /g;
385 $$textref =~ s/</&lt;/g;
386 $$textref =~ s/>/&gt;/g;
387 $$textref =~ s/\[/&#91;/g;
388 $$textref =~ s/\]/&#93;/g;
389
390 $doc_obj->add_utf8_text($cursection, $$textref);
391
392 return 1;
393}
394
395
396# Improvement is to merge this with newer version in MetadataPass
397
398sub open_prettyprint_metadata_table
399{
400 my $self = shift(@_);
401
402 my $att = "width=100% cellspacing=2";
403 my $style = "style=\'border-bottom: 4px solid #000080\'";
404
405 $self->{'ppmd_table'} = "\n<table $att $style>";
406}
407
408sub add_prettyprint_metadata_line
409{
410 my $self = shift(@_);
411 my ($metaname, $metavalue_utf8) = @_;
412
413 $metavalue_utf8 = &util::hyperlink_text($metavalue_utf8);
414
415 $self->{'ppmd_table'} .= " <tr bgcolor=#b5d3cd>\n";
416 $self->{'ppmd_table'} .= " <td width=30%>\n";
417 $self->{'ppmd_table'} .= " $metaname\n";
418 $self->{'ppmd_table'} .= " </td>\n";
419 $self->{'ppmd_table'} .= " <td>\n";
420 $self->{'ppmd_table'} .= " $metavalue_utf8\n";
421 $self->{'ppmd_table'} .= " </td>\n";
422 $self->{'ppmd_table'} .= " </tr>\n";
423
424}
425
426sub close_prettyprint_metadata_table
427{
428 my $self = shift(@_);
429
430 $self->{'ppmd_table'} .= "</table>\n";
431}
432
433my $qualified_dc_mapping = {
434 "alternative" => "dc.title",
435 "tableOfContents" => "dc.description",
436 "abstract" => "dc.description",
437 "created" => "dc.date",
438 "valid" => "dc.date",
439 "available" => "dc.date",
440 "issued" => "dc.date",
441 "modified" => "dc.date",
442 "dateAccepted" => "dc.date",
443 "dateCopyrighted" => "dc.date",
444 "dateSubmitted" => "dc.date",
445 "extent" => "dc.format",
446 "medium" => "dc.format",
447 "isVersionOf" => "dc.relation",
448 "hasVersion" => "dc.relation",
449 "isReplacedBy" => "dc.relation",
450 "replaces" => "dc.relation",
451 "isRequiredBy" => "dc.relation",
452 "requires" => "dc.relation",
453 "isPartOf" => "dc.relation",
454 "hasPart" => "dc.relation",
455 "isReferencedBy" => "dc.relation",
456 "references" => "dc.relation",
457 "isFormatOf" => "dc.relation",
458 "hasFormat" => "dc.relation",
459 "conformsTo" => "dc.relation",
460 "spatial" => "dc.coverage",
461 "temporal" => "dc.coverage",
462# these are now top level elements in our qualified dc metadata set
463# "audience" => "dc.any",
464# "accrualMethod" => "dc.any",
465# "accrualPeriodicity" => "dc.any",
466# "accrualPolicy" => "dc.any",
467# "instructionalMethod" => "dc.any",
468# "provenance" => "dc.any",
469# "rightsHolder" => "dc.any",
470 "mediator" => "dc.audience",
471 "educationLevel" => "dc.audience",
472 "accessRights" => "dc.rights",
473 "license" => "dc.rights",
474 "bibliographicCitation" => "dc.identifier"
475 };
476
477sub remap_dc_metadata
478{
479 my $self = shift(@_);
480
481 my ($metaname) = @_;
482
483 my ($prefix,$name) = ($metaname =~ m/^(.*?)\.(.*?)$/);
484
485 if (defined $qualified_dc_mapping->{$name}) {
486
487 return $qualified_dc_mapping->{$name}."^".$name;
488 }
489
490
491 return $metaname; # didn't get a match, return param passed in unchanged
492}
493
494
495sub extract_oai_metadata {
496 my $self = shift (@_);
497 my ($textref, $metadata) = @_;
498 my $outhandle = $self->{'outhandle'};
499
500 $self->open_prettyprint_metadata_table();
501
502 if ($$textref =~ m/<metadata\s*>(.*?)<\/metadata\s*>/s)
503 {
504 my $metadata_text = $1;
505
506 # locate and remove outermost tag (ignoring any attribute information in top-level tag)
507 my ($outer_tagname,$inner_metadata_text) = ($metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>/s);
508 # split tag into namespace and tag name
509 my($namespace,$top_level_prefix) = ($outer_tagname =~ m/^(.*?):(.*?)$/);
510 # sometimes, the dc namespace is not specified as the prefix in each element (like <dc:title>)
511 # but is rather defined in the wrapper element containing the various dc meta elements,
512 # like <dc><title></title><creator></creator></dc>.
513 # In such a case, we use this wrapper element as the top_level_prefix
514
515 # if there was no prefix, then the tag itself becomes the top_level_prefix
516 if(!defined $top_level_prefix && defined $outer_tagname) {
517 $top_level_prefix = $outer_tagname;
518 }
519
520 #process each element one by one
521 while ($inner_metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>(.*)/s)
522 {
523
524 my $metaname = $1;
525 my $metavalue = $2;
526 $inner_metadata_text = $3;
527
528 # greenstone uses . for namespace, while oai uses :
529 $metaname =~ s/:/\./;
530 # if there is no namespace, then we use the outer tag name or
531 # namespace for this element
532 if ($metaname !~ m/\./)
533 {
534 $metaname = "$top_level_prefix.$metaname";
535 }
536
537 # if metadata set is auto, leave as is, otherwise convert to
538 # specified namespace
539 if ($self->{'metadata_set'} ne "auto") {
540 if ($metaname !~ /^gi\./) { # hack to not overwrite gi metadata
541 $metaname =~ s/^([^\.]*)\./$self->{'metadata_set'}\./;
542 if ($self->{'metadata_set'} eq "dc") {
543 # convert qualified dc terms to gs version, e.g.
544 # spatial becomes coverage^spatial
545 $metaname = $self->remap_dc_metadata($metaname);
546 }
547 }
548 }
549
550 # uppercase the first char of the name
551 $metaname =~ s/\.(.)/\.\u$1/;
552 $metavalue =~ s/\[/&#91;/g;
553 $metavalue =~ s/\]/&#93;/g;
554
555 # so that GLI can see this metadata, store here as ex.dc.Title etc
556 my $ex_metaname = $metaname;
557 $ex_metaname =~ s/^ex\.//; # remove any pre-existing ex. prefix
558 $ex_metaname = "ex.$ex_metaname"; # at last can prefix ex.
559
560 if (defined $metadata->{$ex_metaname})
561 {
562 push(@{$metadata->{$ex_metaname}},$metavalue);
563
564 }
565 else
566 {
567 $metadata->{$ex_metaname} = [ $metavalue ];
568 }
569
570 # but don't add ex to the pretty print line
571 $self->add_prettyprint_metadata_line($metaname, $metavalue);
572
573 }
574 }
575
576 $self->close_prettyprint_metadata_table();
577}
578
579## we know from the file extension, so doesn't need to check the doctype
580sub check_doctype {
581
582 return 1;
583}
584
5851;
Note: See TracBrowser for help on using the repository browser.