source: gsdl/trunk/perllib/plugins/OAIPlugin.pm@ 20451

Last change on this file since 20451 was 20451, checked in by kjdon, 15 years ago

metadata args may have ex. now. Trim ex. from values

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