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

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

inherit and use args form ReadTextFile cos we want the file encoding stuff

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