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

Last change on this file since 36373 was 36373, checked in by kjdon, 21 months ago

now I have removed commented out code from last commit

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