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

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

OAI files should be explodable, so added that back in as an option

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