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

Last change on this file since 17319 was 17319, checked in by kjdon, 16 years ago

tidied this up and removed some old code

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