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

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

removed the metadata argument from metadata_read as its not used and just confuses things when implementing this

  • Property svn:keywords set to Author Date Id Revision
File size: 18.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 unicode;
30use util;
31
32use strict;
33no strict 'refs'; # allow filehandles to be variables and viceversa
34
35use ReadXMLFile;
36use ReadTextFile; # needed for subroutine textcat_get_language_encoding
37use metadatautil;
38
39sub BEGIN {
40 @OAIPlugin::ISA = ('ReadXMLFile', 'ReadTextFile');
41}
42
43
44my $arguments =
45 [ { 'name' => "process_exp",
46 'desc' => "{BasePlugin.process_exp}",
47 'type' => "regexp",
48 'reqd' => "no",
49 'deft' => &get_default_process_exp() },
50 { 'name' => "xxx",
51 'desc' => "{OAIPlugin.xxx}",
52 'type' => "metadata",
53 'reqd' => "no",
54 'deft' => "gi.Sourcedoc" }
55 ];
56
57my $options = { 'name' => "OAIPlugin",
58 'desc' => "{OAIPlugin.desc}",
59 'abstract' => "no",
60 'inherits' => "yes",
61 'explodes' => "yes",
62 'args' => $arguments };
63
64
65sub new {
66 my ($class) = shift (@_);
67 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
68 push(@$pluginlist, $class);
69
70 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
71 push(@{$hashArgOptLists->{"OptList"}},$options);
72
73 new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists,1);
74 my $self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
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
85sub get_doctype {
86 my $self = shift(@_);
87
88 return "OAI-PMH";
89}
90
91sub xml_start_document {
92 my $self = shift (@_);
93 $self->{'in_metadata_node'} = 0;
94 $self->{'rawxml'} = "";
95 $self->{'saved_metadata'} = {};
96}
97
98sub xml_end_document {
99}
100
101sub xml_doctype {
102 my $self = shift(@_);
103
104 my ($expat, $name, $sysid, $pubid, $internal) = @_;
105
106 ##die "" if ($name !~ /^OAI-PMH$/);
107
108 my $outhandle = $self->{'outhandle'};
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'};
111
112}
113
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'} = "";
129 }
130
131 if ($self->{'in_metadata_node'}) {
132 $self->{'metadata_xml'} .= "<$element$attr>";
133 }
134}
135
136sub xml_end_tag {
137 my $self = shift(@_);
138 my ($expat, $element) = @_;
139
140 $self->{'rawxml'} .= "</$element>";
141
142 if ($self->{'in_metadata_node'}) {
143 $self->{'metadata_xml'} .= "</$element>";
144 }
145
146 if ($element eq "metadata") {
147 my $textref = \$self->{'metadata_xml'};
148 #my $metadata = $self->{'metadata'};
149 my $metadata = $self->{'saved_metadata'};
150 $self->extract_oai_metadata($textref,$metadata);
151
152 $self->{'in_metadata_node'} = 0;
153 }
154
155
156}
157
158sub xml_text {
159 my $self = shift(@_);
160 my ($expat) = @_;
161
162 $self->{'rawxml'} .= $_;
163
164 if ($self->{'in_metadata_node'}) {
165 $self->{'metadata_xml'} .= $_;
166 }
167}
168
169
170sub metadata_read {
171 my $self = shift (@_);
172
173 my ($pluginfo, $base_dir, $file, $block_hash, $extrametakeys, $extrametadata, $processor, $maxdocs, $gli) = @_;
174
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# print STDERR "initial\n";
179# foreach my $k (keys %$metadata) {
180# print STDERR "$k=".join (", ", @{$metadata->{$k}})."; ";
181# }
182# print STDERR "\n";
183
184 my $total_count = 0; # is total count used?
185 if (!$self->parse_file($filename_full_path, $file, $gli)) {
186 $self->{'saved_metadata'} = undef;
187 return undef;
188 }
189
190 my $new_metadata = $self->{'saved_metadata'};
191 $self->{'saved_metadata'} = undef;
192 # add the pretty metadata table as metadata
193 my $ppmd_table = $self->{'ppmd_table'};
194 $new_metadata->{'prettymd'} = $ppmd_table;
195 $self->{'ppmd_table'} = undef;
196
197 print STDERR "after parse\n";
198 foreach my $k (keys %$new_metadata) {
199 print STDERR "$k=".join (", ", @{$new_metadata->{$k}})."; ";
200 }
201 print STDERR "\n";
202
203
204 # if ($self->SUPER::read($pluginfo,$base_dir,$file,$block_hash,$new_metadata,$processor,$maxdocs,$total_count, $gli)) {
205 # calling "SUPER::read" at this point sets up $metadata
206 # data-structure. We can then, later, in OAIPlug::read decide
207 # whether this $metadata will stick to an accompanying file,
208 # or else needs a new doc object to be formed that contains
209 # purely metadata
210
211# $self->{'metadata'} = undef;
212# print STDERR "after erad\n";
213# foreach my $k (keys %$metadata) {
214# print STDERR "$k=".join (", ", @{$metadata->{$k}})."; ";
215# }
216# print STDERR "\n";
217 my $url_array = $new_metadata->{'dc.Identifier'};
218 my $num_urls = (defined $url_array) ? scalar(@$url_array) : 0;
219 print STDERR "$num_urls urls for $file\n";
220 my $srcdoc_exists = 0;
221 my $srcdoc_pos = 0;
222 my $filename_dir = &util::filename_head($filename_full_path);
223 my $filename_for_metadata = $file;
224 for (my $i=0; $i<$num_urls; $i++) {
225
226 if ($url_array->[$i] !~ m/^(https?|ftp):/) {
227
228 my $src_filename = &util::filename_cat($filename_dir, $url_array->[$i]);
229
230 if (-e $src_filename) {
231 $srcdoc_pos = $i;
232 $srcdoc_exists = 1;
233 $filename_for_metadata = $url_array->[$i];
234 last;
235 }
236 }
237 }
238
239
240 if ($srcdoc_exists)
241 {
242 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 1;
243 }
244 else {
245 # save the rawxml for the source document
246 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 0;
247 $self->{'oai-files'}->{$file}->{'rawxml'} = $self->{'rawxml'};
248 $self->{'rawxml'} = "";
249 print STDERR "raw xml = $self->{'oai-files'}->{$file}->{'rawxml'}\n";
250 }
251
252### print STDERR "**** storing OAI file: $file\n";
253
254 # return all the metadata we have extracted to the caller.
255 # Directory plug will pass it back in at read time, so we don't need to extract it again.
256 $extrametadata->{$filename_for_metadata} = $new_metadata;
257 push(@$extrametakeys, $filename_for_metadata);
258
259 return 1;
260
261}
262
263
264sub read {
265 my $self = shift (@_);
266
267 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
268
269
270### print STDERR "**** checking OAI read: $file\n";
271
272 if (!defined $self->{'oai-files'}->{$file}) {
273 return undef;
274 }
275
276
277 my $srcdoc_exists = $self->{'oai-files'}->{$file}->{'srcdoc_exists'};
278 if ($srcdoc_exists) {
279 # do nothing more
280 # no more need to access details of this $file => tidy up as you go
281 delete $self->{'oai-files'}->{$file};
282 return 0; # not processed here, but don't pass on to rest of plugins
283 }
284
285### print STDERR "**** !!!!! srcdoc_exists = $srcdoc_exists\n";
286
287 my $filename = $file;
288 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
289
290 # Do encoding stuff on metadata
291 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
292
293 # create a new document
294 my $doc_obj = new doc ($filename, "indexed_doc");
295 my $top_section = $doc_obj->get_top_section;
296 my $plugin_type = $self->{'plugin_type'};
297
298 $doc_obj->add_utf8_metadata($top_section, "Language", $language);
299 $doc_obj->add_utf8_metadata($top_section, "Encoding", $encoding);
300 $doc_obj->add_utf8_metadata($top_section, "Plugin", $plugin_type);
301 $doc_obj->add_metadata($top_section, "FileFormat", "OAI");
302 $doc_obj->add_metadata($top_section, "FileSize", (-s $filename));
303
304 # include any metadata passed in from previous plugins
305 # note that this metadata is associated with the top level section
306 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
307
308 # do plugin specific processing of doc_obj
309 print STDERR "raw xml 2 = $self->{'oai-files'}->{$file}->{'rawxml'}\n";
310 my $text = $self->{'oai-files'}->{$file}->{'rawxml'};
311 delete $self->{'oai-files'}->{$file};
312
313 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
314 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
315 return -1;
316 }
317
318 # do any automatic metadata extraction
319 $self->auto_extract_metadata ($doc_obj);
320
321 # add an OID
322 $self->add_OID($doc_obj);
323
324 # process the document
325 $processor->process($doc_obj);
326
327 $self->{'num_processed'} ++;
328
329 return 1; # processed the file
330}
331
332
333sub read_old {
334 my $self = shift (@_);
335
336 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
337
338 my $outhandle = $self->{'outhandle'};
339
340 my $filename = $file;
341 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
342
343 # block the srcdocs dir - we will process files in them when we find an OAI record for them
344 return 0 if ((-d $filename) && ($filename =~ m/srcdocs$/));
345 if ($self->SUPER::read(@_)) {
346 # Do encoding stuff
347 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
348
349 my $url_array = $metadata->{'dc.Identifier'};
350 my $num_urls = (defined $url_array) ? scalar(@$url_array) : 0;
351
352 my $srcdoc_exists = 0;
353 my $srcdoc_pos = 0;
354 my $filename_dir = &util::filename_head($filename);
355
356 for (my $i=0; $i<$num_urls; $i++) {
357 if ($url_array->[$i] !~ m/^(http|ftp):/) {
358
359 my $src_filename = &util::filename_cat($filename_dir, $url_array->[$i]);
360 if (-e $src_filename) {
361 $srcdoc_pos = $i;
362 $srcdoc_exists = 1;
363 }
364 }
365 }
366
367 if ($srcdoc_exists)
368 {
369 print $outhandle "OAIPlugin: passing metadata on to $url_array->[0]\n"
370 if ($self->{'verbosity'}>1);
371
372
373 # Make pretty print metadata table stick with src filename
374 my $ppmd_table = $self->{'ppmd_table'};
375 $metadata->{'prettymd'} = [ $ppmd_table ];
376 $self->{'ppmd_table'} = undef;
377
378 return &plugin::read ($pluginfo, $filename_dir, $url_array->[0],
379 $block_hash, $metadata, $processor, $maxdocs,
380 $total_count, $gli);
381 }
382 else
383 {
384 # create a new document
385 my $doc_obj = new doc ($filename, "indexed_doc");
386 my $top_section = $doc_obj->get_top_section;
387 my $plugin_type = $self->{'plugin_type'};
388
389 $doc_obj->add_utf8_metadata($top_section, "Language", $language);
390 $doc_obj->add_utf8_metadata($top_section, "Encoding", $encoding);
391 $doc_obj->add_utf8_metadata($top_section, "Plugin", $plugin_type);
392 $doc_obj->add_metadata($top_section, "FileFormat", "OAI");
393 $doc_obj->add_metadata($top_section, "FileSize", (-s $filename));
394
395 # include any metadata passed in from previous plugins
396 # note that this metadata is associated with the top level section
397 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
398
399 # do plugin specific processing of doc_obj
400 my $textref = \$self->{'rawxml'};
401 unless (defined ($self->process($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
402 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
403 return -1;
404 }
405
406 # do any automatic metadata extraction
407 $self->auto_extract_metadata ($doc_obj);
408
409 # add an OID
410 $self->add_OID($doc_obj);
411
412 my $ppmd_table = $self->{'ppmd_table'};
413 $doc_obj->add_utf8_metadata($top_section,"prettymd",$ppmd_table);
414 $self->{'ppmd_table'} = undef;
415
416 # process the document
417 $processor->process($doc_obj);
418
419 $self->{'num_processed'} ++;
420
421 return 1; # processed the file
422 }
423 }
424 else {
425 return undef;
426 }
427}
428
429
430# do plugin specific processing of doc_obj
431sub process {
432 my $self = shift (@_);
433 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
434 my $outhandle = $self->{'outhandle'};
435
436 print STDERR "<Processing n='$file' p='OAIPlugin'>\n" if ($gli);
437 print $outhandle "OAIPlugin: processing $file\n"
438 if $self->{'verbosity'} > 1;
439
440 my $cursection = $doc_obj->get_top_section();
441
442## $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection);
443
444 # add text to document object
445
446# $$textref =~ s/<(.*?)>/$1 /g;
447 $$textref =~ s/</&lt;/g;
448 $$textref =~ s/>/&gt;/g;
449 $$textref =~ s/\[/&#91;/g;
450 $$textref =~ s/\]/&#93;/g;
451
452## print STDERR "*** adding text: $$textref\n";
453
454 $doc_obj->add_utf8_text($cursection, $$textref);
455
456 return 1;
457}
458
459
460# Improvement is to merge this with newer version in MetadataPass
461
462sub open_prettyprint_metadata_table
463{
464 my $self = shift(@_);
465
466 my $att = "width=100% cellspacing=2";
467 my $style = "style=\'border-bottom: 4px solid #000080\'";
468
469 $self->{'ppmd_table'} = "\n<table $att $style>";
470}
471
472sub add_prettyprint_metadata_line
473{
474 my $self = shift(@_);
475 my ($metaname, $metavalue_utf8) = @_;
476
477### $metavalue_utf8 =~ s/hdl\.handle\.net/mcgonagall.cs.waikato.ac.nz:8080\/dspace\/handle/;
478 $metavalue_utf8 = &util::hyperlink_text($metavalue_utf8);
479
480 $self->{'ppmd_table'} .= " <tr bgcolor=#b5d3cd>\n";
481 $self->{'ppmd_table'} .= " <td width=30%>\n";
482 $self->{'ppmd_table'} .= " $metaname\n";
483 $self->{'ppmd_table'} .= " </td>\n";
484 $self->{'ppmd_table'} .= " <td>\n";
485 $self->{'ppmd_table'} .= " $metavalue_utf8\n";
486 $self->{'ppmd_table'} .= " </td>\n";
487 $self->{'ppmd_table'} .= " </tr>\n";
488
489}
490
491sub close_prettyprint_metadata_table
492{
493 my $self = shift(@_);
494
495 $self->{'ppmd_table'} .= "</table>\n";
496}
497
498
499sub remap_dcterms_metadata
500{
501 my $self = shift(@_);
502
503 my ($metaname) = @_;
504
505 my $dcterm_mapping = {
506 "alternative" => "dc.title",
507 "tableOfContents" => "dc.description",
508 "abstract" => "dc.description",
509 "created" => "dc.date",
510 "valid" => "dc.date",
511 "available" => "dc.date",
512 "issued" => "dc.date",
513 "modified" => "dc.date",
514 "dateAccepted" => "dc.date",
515 "dateCopyrighted" => "dc.date",
516 "dateSubmitted" => "dc.date",
517 "extent" => "dc.format",
518 "medium" => "dc.format",
519 "isVersionOf" => "dc.relation",
520 "hasVersion" => "dc.relation",
521 "isReplacedBy" => "dc.relation",
522 "replaces" => "dc.relation",
523 "isRequiredBy" => "dc.relation",
524 "requires" => "dc.relation",
525 "isPartOf" => "dc.relation",
526 "hasPart" => "dc.relation",
527 "isReferencedBy" => "dc.relation",
528 "references" => "dc.relation",
529 "isFormatOf" => "dc.relation",
530 "hasFormat" => "dc.relation",
531 "conformsTo" => "dc.relation",
532 "spatial" => "dc.coverage",
533 "temporal" => "dc.coverage",
534 "audience" => "dc.any",
535 "accrualMethod" => "dc.any",
536 "accrualPeriodicity" => "dc.any",
537 "accrualPolicy" => "dc.any",
538 "instructionalMethod" => "dc.any",
539 "provenance" => "dc.any",
540 "rightsHolder" => "dc.any",
541 "mediator" => "audience",
542 "educationLevel" => "audience",
543 "accessRights" => "dc.rights",
544 "license" => "dc.rights",
545 "bibliographicCitation" => "dc.identifier"
546 };
547
548 my ($prefix,$name) = ($metaname =~ m/^(.*?)\.(.*?)$/);
549
550 if ($prefix eq "dcterms")
551 {
552 if (defined $dcterm_mapping->{$name})
553 {
554 return $dcterm_mapping->{$name}."^".$name;
555 }
556
557 }
558 return $metaname; # didn't get a match, return param passed in unchanged
559}
560
561
562sub extract_oai_metadata {
563 my $self = shift (@_);
564 my ($textref, $metadata) = @_;
565 my $outhandle = $self->{'outhandle'};
566
567 # Only handles DC metadata
568
569 $self->open_prettyprint_metadata_table();
570
571 if ($$textref =~ m/<metadata\s*>(.*?)<\/metadata\s*>/s)
572 {
573 my $metadata_text = $1;
574
575 # locate and remove outermost tag (ignoring any attribute information in top-level tag)
576 my ($wrapper_metadata_xml,$inner_metadata_text) = ($metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>/s);
577
578 # split tag into namespace and tag name
579 my($namespace,$top_level_prefix) = ($wrapper_metadata_xml =~ m/^(.*?):(.*?)$/);
580
581 # sometimes, the dc namespace is not specified as the prefix in each element (like <dc:title>)
582 # but is rather defined in the wrapper element containing the various dc meta elements,
583 # like <dc><title></title><creator></creator></dc>.
584 # In such a case, we use this wrapper element as the top_level_prefix
585 if(!defined $top_level_prefix && defined $wrapper_metadata_xml && $wrapper_metadata_xml =~ m/dc$/) {
586 $top_level_prefix = $wrapper_metadata_xml;
587 }
588
589 if ($top_level_prefix !~ m/dc$/) {
590 print $outhandle "Warning: OAIPlugin currently only designed for Dublin Core (or variant) metadata\n";
591 print $outhandle " This recorded metadata section '$top_level_prefix' does not appear to match.\n";
592 print $outhandle " Metadata assumed to be in form: <prefix:tag>value</prefix:tag> and will be converted\n";
593 print $outhandle " into Greenstone metadata as prefix.tag = value\n";
594 }
595
596 while ($inner_metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>(.*)/s)
597 {
598 # if URL given for document as identifier metadata, store it ...
599 # $doc_obj->add_utf8_metadata($cursection, "URL", $web_url);
600
601 my $metaname = $1;
602 my $metavalue = $2;
603 $inner_metadata_text = $3;
604
605# print STDERR "*** metaname = $metaname\n";
606# print STDERR "*** metavalue = $metavalue\n";
607
608 # $metaname =~ s/^(dc:)?(.)/\u$2/; # strip of optional prefix and uppercase first letter
609 $metaname =~ s/:/\./;
610 if ($metaname !~ m/\./)
611 {
612 $metaname = "$top_level_prefix.$metaname";
613# print STDERR "*** metaname = $metaname\tmetavalue = $metavalue\n";
614 }
615 $metaname =~ s/\.(.)/\.\u$1/;
616
617 $metaname = $self->remap_dcterms_metadata($metaname);
618
619 $metavalue =~ s/\[/&#91;/g;
620 $metavalue =~ s/\]/&#93;/g;
621
622
623# if ($metaname eq "Identifier")
624# {
625# # name clashes with GSDL reserved metadata name for hash id
626# $metaname = "URL";
627# }
628
629 if (defined $metadata->{$metaname})
630 {
631 push(@{$metadata->{$metaname}},$metavalue);
632
633 }
634 else
635 {
636 $metadata->{$metaname} = [ $metavalue ];
637 }
638
639 $self->add_prettyprint_metadata_line($metaname, $metavalue);
640
641 }
642 }
643
644 $self->close_prettyprint_metadata_table();
645}
646
647## we know from the file extension, so doesn't need to check the doctype
648sub check_doctype {
649
650 return 1;
651}
652
6531;
Note: See TracBrowser for help on using the repository browser.