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

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

OID generation modifications: OIDtype and OIDmetadata options now available for plugins as well as import. OIDtype for plugins defaults to auto - if set to auto, then use the values from import. All plugins now call self->add_OID instead of doc_obj->set_OID. This sets the doc_obj OIDtype so that doesn't need to be donein other places any more. all plugins have the get_oid_hash_type method - normally returns hash_on_file, but can be overridden to return hash_on_ga_xml for those plugins that don't want hashing on file (MP3,OggVorbis...)

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