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

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

extrametadata keys need to be regexs, so windows paths need converting

  • Property svn:keywords set to Author Date Id Revision
File size: 14.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
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' => "document_field",
51 'desc' => "{OAIPlugin.document_field}",
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
179 if (!$self->parse_file($filename_full_path, $file, $gli)) {
180 $self->{'saved_metadata'} = undef;
181 return undef;
182 }
183
184 my $new_metadata = $self->{'saved_metadata'};
185 $self->{'saved_metadata'} = undef;
186
187 # add the pretty metadata table as metadata
188 my $ppmd_table = $self->{'ppmd_table'};
189 $new_metadata->{'prettymd'} = $ppmd_table;
190 $self->{'ppmd_table'} = undef;
191
192 my $document_metadata_field = $self->{'document_field'};
193 my $url_array = $new_metadata->{$document_metadata_field};
194 my $num_urls = (defined $url_array) ? scalar(@$url_array) : 0;
195 ##print STDERR "$num_urls urls for $file\n";
196 my $srcdoc_exists = 0;
197 my $srcdoc_pos = 0;
198 my $filename_dir = &util::filename_head($filename_full_path);
199 my $filename_for_metadata = $file; # this assumes there will only be one record per oai file - is this always the case??
200 for (my $i=0; $i<$num_urls; $i++) {
201
202 if ($url_array->[$i] !~ m/^(https?|ftp):/) {
203
204 my $src_filename = &util::filename_cat($filename_dir, $url_array->[$i]);
205
206 if (-e $src_filename) {
207 $srcdoc_pos = $i;
208 $srcdoc_exists = 1;
209 $filename_for_metadata = $url_array->[$i];
210 last;
211 }
212 }
213 }
214
215 if ($srcdoc_exists) {
216 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 1;
217 }
218 else {
219 # save the rawxml for the source document
220 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 0;
221 $self->{'oai-files'}->{$file}->{'rawxml'} = $self->{'rawxml'};
222 $self->{'rawxml'} = "";
223 }
224
225 # return all the metadata we have extracted to the caller.
226 # Directory plug will pass it back in at read time, so we don't need to extract it again.
227 # extrametadata keys should be regular expressions
228 $filename_for_metadata = &util::filename_to_regex($filename_for_metadata);
229 $extrametadata->{$filename_for_metadata} = $new_metadata;
230 push(@$extrametakeys, $filename_for_metadata);
231
232 return 1;
233
234}
235
236
237sub read {
238 my $self = shift (@_);
239
240 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
241
242 if (!defined $self->{'oai-files'}->{$file}) {
243 return undef;
244 }
245
246 my $srcdoc_exists = $self->{'oai-files'}->{$file}->{'srcdoc_exists'};
247 if ($srcdoc_exists) {
248 # do nothing more - all the metadata has been extracted and associated with the srcdoc
249 # no more need to access details of this $file => tidy up as you go
250 delete $self->{'oai-files'}->{$file};
251 return 0; # not processed here, but don't pass on to rest of plugins
252 }
253
254 my $filename = $file;
255 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
256
257 # Do encoding stuff on metadata
258 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
259
260 # create a new document
261 my $doc_obj = new doc ($filename, "indexed_doc");
262 my $top_section = $doc_obj->get_top_section;
263 my $plugin_type = $self->{'plugin_type'};
264
265 $doc_obj->add_utf8_metadata($top_section, "Language", $language);
266 $doc_obj->add_utf8_metadata($top_section, "Encoding", $encoding);
267 $doc_obj->add_utf8_metadata($top_section, "Plugin", $plugin_type);
268 $doc_obj->add_metadata($top_section, "FileFormat", "OAI");
269 $doc_obj->add_metadata($top_section, "FileSize", (-s $filename));
270
271 # include any metadata passed in from previous plugins
272 # note that this metadata is associated with the top level section
273 # this will include all the metadata from the oai file that we extracted
274 # during metadata_read
275 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
276
277 # do plugin specific processing of doc_obj
278 my $text = $self->{'oai-files'}->{$file}->{'rawxml'};
279 delete $self->{'oai-files'}->{$file};
280
281 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
282 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
283 return -1;
284 }
285
286 # do any automatic metadata extraction
287 $self->auto_extract_metadata ($doc_obj);
288
289 # add an OID
290 $self->add_OID($doc_obj);
291
292 # process the document
293 $processor->process($doc_obj);
294
295 $self->{'num_processed'} ++;
296
297 return 1; # processed the file
298}
299
300
301# do plugin specific processing of doc_obj
302sub process {
303 my $self = shift (@_);
304 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
305 my $outhandle = $self->{'outhandle'};
306
307 print STDERR "<Processing n='$file' p='OAIPlugin'>\n" if ($gli);
308 print $outhandle "OAIPlugin: processing $file\n"
309 if $self->{'verbosity'} > 1;
310
311 my $cursection = $doc_obj->get_top_section();
312
313## $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection);
314
315 # add text to document object
316
317# $$textref =~ s/<(.*?)>/$1 /g;
318 $$textref =~ s/</&lt;/g;
319 $$textref =~ s/>/&gt;/g;
320 $$textref =~ s/\[/&#91;/g;
321 $$textref =~ s/\]/&#93;/g;
322
323 $doc_obj->add_utf8_text($cursection, $$textref);
324
325 return 1;
326}
327
328
329# Improvement is to merge this with newer version in MetadataPass
330
331sub open_prettyprint_metadata_table
332{
333 my $self = shift(@_);
334
335 my $att = "width=100% cellspacing=2";
336 my $style = "style=\'border-bottom: 4px solid #000080\'";
337
338 $self->{'ppmd_table'} = "\n<table $att $style>";
339}
340
341sub add_prettyprint_metadata_line
342{
343 my $self = shift(@_);
344 my ($metaname, $metavalue_utf8) = @_;
345
346 $metavalue_utf8 = &util::hyperlink_text($metavalue_utf8);
347
348 $self->{'ppmd_table'} .= " <tr bgcolor=#b5d3cd>\n";
349 $self->{'ppmd_table'} .= " <td width=30%>\n";
350 $self->{'ppmd_table'} .= " $metaname\n";
351 $self->{'ppmd_table'} .= " </td>\n";
352 $self->{'ppmd_table'} .= " <td>\n";
353 $self->{'ppmd_table'} .= " $metavalue_utf8\n";
354 $self->{'ppmd_table'} .= " </td>\n";
355 $self->{'ppmd_table'} .= " </tr>\n";
356
357}
358
359sub close_prettyprint_metadata_table
360{
361 my $self = shift(@_);
362
363 $self->{'ppmd_table'} .= "</table>\n";
364}
365
366
367sub remap_dcterms_metadata
368{
369 my $self = shift(@_);
370
371 my ($metaname) = @_;
372
373 my $dcterm_mapping = {
374 "alternative" => "dc.title",
375 "tableOfContents" => "dc.description",
376 "abstract" => "dc.description",
377 "created" => "dc.date",
378 "valid" => "dc.date",
379 "available" => "dc.date",
380 "issued" => "dc.date",
381 "modified" => "dc.date",
382 "dateAccepted" => "dc.date",
383 "dateCopyrighted" => "dc.date",
384 "dateSubmitted" => "dc.date",
385 "extent" => "dc.format",
386 "medium" => "dc.format",
387 "isVersionOf" => "dc.relation",
388 "hasVersion" => "dc.relation",
389 "isReplacedBy" => "dc.relation",
390 "replaces" => "dc.relation",
391 "isRequiredBy" => "dc.relation",
392 "requires" => "dc.relation",
393 "isPartOf" => "dc.relation",
394 "hasPart" => "dc.relation",
395 "isReferencedBy" => "dc.relation",
396 "references" => "dc.relation",
397 "isFormatOf" => "dc.relation",
398 "hasFormat" => "dc.relation",
399 "conformsTo" => "dc.relation",
400 "spatial" => "dc.coverage",
401 "temporal" => "dc.coverage",
402 "audience" => "dc.any",
403 "accrualMethod" => "dc.any",
404 "accrualPeriodicity" => "dc.any",
405 "accrualPolicy" => "dc.any",
406 "instructionalMethod" => "dc.any",
407 "provenance" => "dc.any",
408 "rightsHolder" => "dc.any",
409 "mediator" => "audience",
410 "educationLevel" => "audience",
411 "accessRights" => "dc.rights",
412 "license" => "dc.rights",
413 "bibliographicCitation" => "dc.identifier"
414 };
415
416 my ($prefix,$name) = ($metaname =~ m/^(.*?)\.(.*?)$/);
417
418 if ($prefix eq "dcterms")
419 {
420 if (defined $dcterm_mapping->{$name})
421 {
422 return $dcterm_mapping->{$name}."^".$name;
423 }
424
425 }
426 return $metaname; # didn't get a match, return param passed in unchanged
427}
428
429
430sub extract_oai_metadata {
431 my $self = shift (@_);
432 my ($textref, $metadata) = @_;
433 my $outhandle = $self->{'outhandle'};
434
435 # Only handles DC metadata
436
437 $self->open_prettyprint_metadata_table();
438
439 if ($$textref =~ m/<metadata\s*>(.*?)<\/metadata\s*>/s)
440 {
441 my $metadata_text = $1;
442
443 # locate and remove outermost tag (ignoring any attribute information in top-level tag)
444 my ($wrapper_metadata_xml,$inner_metadata_text) = ($metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>/s);
445
446 # split tag into namespace and tag name
447 my($namespace,$top_level_prefix) = ($wrapper_metadata_xml =~ m/^(.*?):(.*?)$/);
448
449 # sometimes, the dc namespace is not specified as the prefix in each element (like <dc:title>)
450 # but is rather defined in the wrapper element containing the various dc meta elements,
451 # like <dc><title></title><creator></creator></dc>.
452 # In such a case, we use this wrapper element as the top_level_prefix
453 if(!defined $top_level_prefix && defined $wrapper_metadata_xml && $wrapper_metadata_xml =~ m/dc$/) {
454 $top_level_prefix = $wrapper_metadata_xml;
455 }
456
457 if ($top_level_prefix !~ m/dc$/) {
458 print $outhandle "Warning: OAIPlugin currently only designed for Dublin Core (or variant) metadata\n";
459 print $outhandle " This recorded metadata section '$top_level_prefix' does not appear to match.\n";
460 print $outhandle " Metadata assumed to be in form: <prefix:tag>value</prefix:tag> and will be converted\n";
461 print $outhandle " into Greenstone metadata as prefix.tag = value\n";
462 }
463
464 while ($inner_metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>(.*)/s)
465 {
466
467 my $metaname = $1;
468 my $metavalue = $2;
469 $inner_metadata_text = $3;
470
471 # $metaname =~ s/^(dc:)?(.)/\u$2/; # strip of optional prefix and uppercase first letter
472 $metaname =~ s/:/\./;
473 if ($metaname !~ m/\./)
474 {
475 $metaname = "$top_level_prefix.$metaname";
476 }
477 $metaname =~ s/\.(.)/\.\u$1/;
478
479 $metaname = $self->remap_dcterms_metadata($metaname);
480
481 $metavalue =~ s/\[/&#91;/g;
482 $metavalue =~ s/\]/&#93;/g;
483
484 if (defined $metadata->{$metaname})
485 {
486 push(@{$metadata->{$metaname}},$metavalue);
487
488 }
489 else
490 {
491 $metadata->{$metaname} = [ $metavalue ];
492 }
493
494 $self->add_prettyprint_metadata_line($metaname, $metavalue);
495
496 }
497 }
498
499 $self->close_prettyprint_metadata_table();
500}
501
502## we know from the file extension, so doesn't need to check the doctype
503sub check_doctype {
504
505 return 1;
506}
507
5081;
Note: See TracBrowser for help on using the repository browser.