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

Last change on this file since 17066 was 17066, checked in by ak19, 16 years ago

OAIPlugin now works again: 1. needs to inherit from ReadTextFile as well in order to have access to the method textcat_get_language_encoding; 2. if dc is not specified as a prefix in each metadata element tag, looks inside the containing/wrapper element to see if that is set to dc and if so, uses that as the prefix.

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