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

Last change on this file since 23352 was 23352, checked in by davidb, 13 years ago

Modifications to code to support filename encoding issues when tested under Windows

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