source: main/trunk/greenstone2/perllib/plugins/OAIPlugin.pm@ 21763

Last change on this file since 21763 was 21763, checked in by kjdon, 14 years ago

don't modify document_field is info_only - doesn't appear to be defined for a -listall

  • Property svn:keywords set to Author Date Id Revision
File size: 15.5 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
43my $set_list =
44 [ { 'name' => "auto",
45 'desc' => "{OAIPlugin.metadata_set.auto}" },
46 { 'name' => "dc",
47 'desc' => "{OAIPlugin.metadata_set.dc}" }
48 ];
49
50my $arguments =
51 [ { 'name' => "process_exp",
52 'desc' => "{BasePlugin.process_exp}",
53 'type' => "regexp",
54 'reqd' => "no",
55 'deft' => &get_default_process_exp() },
56 { 'name' => "metadata_set",
57 'desc' => "{OAIPlugin.metadata_set}",
58 'type' => "enumstring",
59 'reqd' => "no",
60 'list' => $set_list,
61 'deft' => "dc" },
62 { 'name' => "document_field",
63 'desc' => "{OAIPlugin.document_field}",
64 'type' => "metadata",
65 'reqd' => "no",
66 'deft' => "gi.Sourcedoc" }
67 ];
68
69my $options = { 'name' => "OAIPlugin",
70 'desc' => "{OAIPlugin.desc}",
71 'abstract' => "no",
72 'inherits' => "yes",
73 'explodes' => "yes",
74 'args' => $arguments };
75
76
77sub new {
78 my ($class) = shift (@_);
79 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
80 push(@$pluginlist, $class);
81
82 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
83 push(@{$hashArgOptLists->{"OptList"}},$options);
84
85 new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists,1);
86 my $self = new ReadXMLFile($pluginlist, $inputargs, $hashArgOptLists);
87
88 if ($self->{'info_only'}) {
89 # don't worry about modifying options
90 return bless $self, $class;
91 }
92 # trim ex. from document field (if there)
93 $self->{'document_field'} =~ s/^ex\.//;
94 return bless $self, $class;
95}
96
97sub get_default_process_exp {
98 my $self = shift (@_);
99
100 return q^(?i)(\.oai)$^;
101}
102
103sub get_doctype {
104 my $self = shift(@_);
105
106 return "OAI-PMH";
107}
108
109sub xml_start_document {
110 my $self = shift (@_);
111 $self->{'in_metadata_node'} = 0;
112 $self->{'rawxml'} = "";
113 $self->{'saved_metadata'} = {};
114}
115
116sub xml_end_document {
117}
118
119sub xml_doctype {
120 my $self = shift(@_);
121
122 my ($expat, $name, $sysid, $pubid, $internal) = @_;
123
124 ##die "" if ($name !~ /^OAI-PMH$/);
125
126 my $outhandle = $self->{'outhandle'};
127 print $outhandle "OAIPlugin: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
128 print STDERR "<Processing n='$self->{'file'}' p='OAIPlugin'>\n" if $self->{'gli'};
129
130}
131
132
133sub xml_start_tag {
134 my $self = shift(@_);
135 my ($expat,$element) = @_;
136
137 my %attr_hash = %_;
138
139 my $attr = "";
140 map { $attr .= " $_=$attr_hash{$_}"; } keys %attr_hash;
141
142 $self->{'rawxml'} .= "<$element$attr>";
143
144 if ($element eq "metadata") {
145 $self->{'in_metadata_node'} = 1;
146 $self->{'metadata_xml'} = "";
147 }
148
149 if ($self->{'in_metadata_node'}) {
150 $self->{'metadata_xml'} .= "<$element$attr>";
151 }
152}
153
154sub xml_end_tag {
155 my $self = shift(@_);
156 my ($expat, $element) = @_;
157
158 $self->{'rawxml'} .= "</$element>";
159
160 if ($self->{'in_metadata_node'}) {
161 $self->{'metadata_xml'} .= "</$element>";
162 }
163
164 if ($element eq "metadata") {
165 my $textref = \$self->{'metadata_xml'};
166 #my $metadata = $self->{'metadata'};
167 my $metadata = $self->{'saved_metadata'};
168 $self->extract_oai_metadata($textref,$metadata);
169
170 $self->{'in_metadata_node'} = 0;
171 }
172
173
174}
175
176sub xml_text {
177 my $self = shift(@_);
178 my ($expat) = @_;
179
180 $self->{'rawxml'} .= $_;
181
182 if ($self->{'in_metadata_node'}) {
183 $self->{'metadata_xml'} .= $_;
184 }
185}
186
187
188sub metadata_read {
189 my $self = shift (@_);
190
191 my ($pluginfo, $base_dir, $file, $block_hash,
192 $extrametakeys, $extrametadata, $extrametafile,
193 $processor, $maxdocs, $gli) = @_;
194
195 # can we process this file??
196 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
197 return undef unless $self->can_process_this_file($filename_full_path);
198
199 if (!$self->parse_file($filename_full_path, $file, $gli)) {
200 $self->{'saved_metadata'} = undef;
201 return undef;
202 }
203
204 my $new_metadata = $self->{'saved_metadata'};
205 $self->{'saved_metadata'} = undef;
206
207 # add the pretty metadata table as metadata
208 my $ppmd_table = $self->{'ppmd_table'};
209 $new_metadata->{'prettymd'} = $ppmd_table;
210 $self->{'ppmd_table'} = undef;
211
212 my $document_metadata_field = $self->{'document_field'};
213 my $url_array = $new_metadata->{$document_metadata_field};
214 my $num_urls = (defined $url_array) ? scalar(@$url_array) : 0;
215 ##print STDERR "$num_urls urls for $file\n";
216 my $srcdoc_exists = 0;
217 my $srcdoc_pos = 0;
218 my $filename_dir = &util::filename_head($filename_full_path);
219
220 # filenames in extrametadata must be relative to current dir, as
221 # DirectoryPlugin adds path info on itself
222 my ($filename_for_metadata) = $file =~ /([^\\\/]+)$/; # this assumes there will only be one record per oai file - is this always the case??
223 for (my $i=0; $i<$num_urls; $i++) {
224
225 if ($url_array->[$i] !~ m/^(https?|ftp):/) {
226
227 my $src_filename = &util::filename_cat($filename_dir, $url_array->[$i]);
228 if (-e $src_filename) {
229 $srcdoc_pos = $i;
230 $srcdoc_exists = 1;
231 # get the slashes the right way, use filename_cat
232 $filename_for_metadata = &util::filename_cat($url_array->[$i]);
233 last;
234 }
235 }
236 }
237
238 if ($srcdoc_exists) {
239 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 1;
240 }
241 else {
242 # save the rawxml for the source document
243 $self->{'oai-files'}->{$file}->{'srcdoc_exists'} = 0;
244 $self->{'oai-files'}->{$file}->{'rawxml'} = $self->{'rawxml'};
245 $self->{'rawxml'} = "";
246 }
247
248 # return all the metadata we have extracted to the caller.
249 # Directory plug will pass it back in at read time, so we don't need to extract it again.
250 # extrametadata keys should be regular expressions
251 $filename_for_metadata = &util::filename_to_regex($filename_for_metadata);
252 $extrametadata->{$filename_for_metadata} = $new_metadata;
253 push(@$extrametakeys, $filename_for_metadata);
254 if ($srcdoc_exists) {
255 if (!defined $extrametafile->{$filename_for_metadata}) {
256 $extrametafile->{$filename_for_metadata} = {};
257 }
258 #maps the file to full path
259 $extrametafile->{$filename_for_metadata}->{$file} = $filename_full_path;
260 }
261 return 1;
262
263}
264
265
266sub read {
267 my $self = shift (@_);
268
269 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
270
271 if (!defined $self->{'oai-files'}->{$file}) {
272 return undef;
273 }
274
275 my $srcdoc_exists = $self->{'oai-files'}->{$file}->{'srcdoc_exists'};
276 if ($srcdoc_exists) {
277 # do nothing more - all the metadata has been extracted and associated with the srcdoc
278 # no more need to access details of this $file => tidy up as you go
279 delete $self->{'oai-files'}->{$file};
280 return 0; # not processed here, but don't pass on to rest of plugins
281 }
282
283 my $filename = $file;
284 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
285
286 # Do encoding stuff on metadata
287 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
288
289 # create a new document
290 my $doc_obj = new doc ($filename, "indexed_doc", $self->{'file_rename_method'});
291 my $top_section = $doc_obj->get_top_section;
292 my $plugin_type = $self->{'plugin_type'};
293
294 my ($filemeta) = $file =~ /([^\\\/]+)$/;
295 $self->set_Source_metadata($doc_obj, $filemeta, $encoding);
296 $doc_obj->add_utf8_metadata($top_section, "Language", $language);
297 $doc_obj->add_utf8_metadata($top_section, "Encoding", $encoding);
298 $doc_obj->add_utf8_metadata($top_section, "Plugin", $plugin_type);
299 $doc_obj->add_metadata($top_section, "FileFormat", "OAI");
300 $doc_obj->add_metadata($top_section, "FileSize", (-s $filename));
301
302 # include any metadata passed in from previous plugins
303 # note that this metadata is associated with the top level section
304 # this will include all the metadata from the oai file that we extracted
305 # during metadata_read
306 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
307
308 # do plugin specific processing of doc_obj
309 my $text = $self->{'oai-files'}->{$file}->{'rawxml'};
310 delete $self->{'oai-files'}->{$file};
311
312 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
313 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
314 return -1;
315 }
316
317 # do any automatic metadata extraction
318 $self->auto_extract_metadata ($doc_obj);
319
320 # add an OID
321 $self->add_OID($doc_obj);
322
323 # process the document
324 $processor->process($doc_obj);
325
326 $self->{'num_processed'} ++;
327
328 return 1; # processed the file
329}
330
331
332# do plugin specific processing of doc_obj
333sub process {
334 my $self = shift (@_);
335 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
336 my $outhandle = $self->{'outhandle'};
337
338 print STDERR "<Processing n='$file' p='OAIPlugin'>\n" if ($gli);
339 print $outhandle "OAIPlugin: processing $file\n"
340 if $self->{'verbosity'} > 1;
341
342 my $cursection = $doc_obj->get_top_section();
343
344## $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection);
345
346 # add text to document object
347
348# $$textref =~ s/<(.*?)>/$1 /g;
349 $$textref =~ s/</&lt;/g;
350 $$textref =~ s/>/&gt;/g;
351 $$textref =~ s/\[/&#91;/g;
352 $$textref =~ s/\]/&#93;/g;
353
354 $doc_obj->add_utf8_text($cursection, $$textref);
355
356 return 1;
357}
358
359
360# Improvement is to merge this with newer version in MetadataPass
361
362sub open_prettyprint_metadata_table
363{
364 my $self = shift(@_);
365
366 my $att = "width=100% cellspacing=2";
367 my $style = "style=\'border-bottom: 4px solid #000080\'";
368
369 $self->{'ppmd_table'} = "\n<table $att $style>";
370}
371
372sub add_prettyprint_metadata_line
373{
374 my $self = shift(@_);
375 my ($metaname, $metavalue_utf8) = @_;
376
377 $metavalue_utf8 = &util::hyperlink_text($metavalue_utf8);
378
379 $self->{'ppmd_table'} .= " <tr bgcolor=#b5d3cd>\n";
380 $self->{'ppmd_table'} .= " <td width=30%>\n";
381 $self->{'ppmd_table'} .= " $metaname\n";
382 $self->{'ppmd_table'} .= " </td>\n";
383 $self->{'ppmd_table'} .= " <td>\n";
384 $self->{'ppmd_table'} .= " $metavalue_utf8\n";
385 $self->{'ppmd_table'} .= " </td>\n";
386 $self->{'ppmd_table'} .= " </tr>\n";
387
388}
389
390sub close_prettyprint_metadata_table
391{
392 my $self = shift(@_);
393
394 $self->{'ppmd_table'} .= "</table>\n";
395}
396
397my $qualified_dc_mapping = {
398 "alternative" => "dc.title",
399 "tableOfContents" => "dc.description",
400 "abstract" => "dc.description",
401 "created" => "dc.date",
402 "valid" => "dc.date",
403 "available" => "dc.date",
404 "issued" => "dc.date",
405 "modified" => "dc.date",
406 "dateAccepted" => "dc.date",
407 "dateCopyrighted" => "dc.date",
408 "dateSubmitted" => "dc.date",
409 "extent" => "dc.format",
410 "medium" => "dc.format",
411 "isVersionOf" => "dc.relation",
412 "hasVersion" => "dc.relation",
413 "isReplacedBy" => "dc.relation",
414 "replaces" => "dc.relation",
415 "isRequiredBy" => "dc.relation",
416 "requires" => "dc.relation",
417 "isPartOf" => "dc.relation",
418 "hasPart" => "dc.relation",
419 "isReferencedBy" => "dc.relation",
420 "references" => "dc.relation",
421 "isFormatOf" => "dc.relation",
422 "hasFormat" => "dc.relation",
423 "conformsTo" => "dc.relation",
424 "spatial" => "dc.coverage",
425 "temporal" => "dc.coverage",
426# these are now top level elements in our qualified dc metadata set
427# "audience" => "dc.any",
428# "accrualMethod" => "dc.any",
429# "accrualPeriodicity" => "dc.any",
430# "accrualPolicy" => "dc.any",
431# "instructionalMethod" => "dc.any",
432# "provenance" => "dc.any",
433# "rightsHolder" => "dc.any",
434 "mediator" => "dc.audience",
435 "educationLevel" => "dc.audience",
436 "accessRights" => "dc.rights",
437 "license" => "dc.rights",
438 "bibliographicCitation" => "dc.identifier"
439 };
440
441sub remap_dc_metadata
442{
443 my $self = shift(@_);
444
445 my ($metaname) = @_;
446
447 my ($prefix,$name) = ($metaname =~ m/^(.*?)\.(.*?)$/);
448
449 if (defined $qualified_dc_mapping->{$name}) {
450
451 return $qualified_dc_mapping->{$name}."^".$name;
452 }
453
454
455 return $metaname; # didn't get a match, return param passed in unchanged
456}
457
458
459sub extract_oai_metadata {
460 my $self = shift (@_);
461 my ($textref, $metadata) = @_;
462 my $outhandle = $self->{'outhandle'};
463
464 $self->open_prettyprint_metadata_table();
465
466 if ($$textref =~ m/<metadata\s*>(.*?)<\/metadata\s*>/s)
467 {
468 my $metadata_text = $1;
469
470 # locate and remove outermost tag (ignoring any attribute information in top-level tag)
471 my ($outer_tagname,$inner_metadata_text) = ($metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>/s);
472 # split tag into namespace and tag name
473 my($namespace,$top_level_prefix) = ($outer_tagname =~ m/^(.*?):(.*?)$/);
474 # sometimes, the dc namespace is not specified as the prefix in each element (like <dc:title>)
475 # but is rather defined in the wrapper element containing the various dc meta elements,
476 # like <dc><title></title><creator></creator></dc>.
477 # In such a case, we use this wrapper element as the top_level_prefix
478
479 # if there was no prefix, then the tag itself becomes the top_level_prefix
480 if(!defined $top_level_prefix && defined $outer_tagname) {
481 $top_level_prefix = $outer_tagname;
482 }
483
484 #process each element one by one
485 while ($inner_metadata_text =~ m/<([^ >]+).*?>(.*?)<\/\1>(.*)/s)
486 {
487
488 my $metaname = $1;
489 my $metavalue = $2;
490 $inner_metadata_text = $3;
491
492 # greenstone uses . for namespace, while oai uses :
493 $metaname =~ s/:/\./;
494 # if there is no namespace, then we use the outer tag name or
495 # namespace for this element
496 if ($metaname !~ m/\./)
497 {
498 $metaname = "$top_level_prefix.$metaname";
499 }
500
501 # if metadata set is auto, leave as is, otherwise convert to
502 # specified namespace
503 if ($self->{'metadata_set'} ne "auto") {
504 if ($metaname !~ /^gi\./) { # hack to not overwrite gi metadata
505 $metaname =~ s/^([^\.]*)\./$self->{'metadata_set'}\./;
506 if ($self->{'metadata_set'} eq "dc") {
507 # convert qualified dc terms to gs version, e.g.
508 # spatial becomes coverage^spatial
509 $metaname = $self->remap_dc_metadata($metaname);
510 }
511 }
512 }
513
514 # uppercase the first char of the name
515 $metaname =~ s/\.(.)/\.\u$1/;
516 $metavalue =~ s/\[/&#91;/g;
517 $metavalue =~ s/\]/&#93;/g;
518
519 if (defined $metadata->{$metaname})
520 {
521 push(@{$metadata->{$metaname}},$metavalue);
522
523 }
524 else
525 {
526 $metadata->{$metaname} = [ $metavalue ];
527 }
528
529 $self->add_prettyprint_metadata_line($metaname, $metavalue);
530
531 }
532 }
533
534 $self->close_prettyprint_metadata_table();
535}
536
537## we know from the file extension, so doesn't need to check the doctype
538sub check_doctype {
539
540 return 1;
541}
542
5431;
Note: See TracBrowser for help on using the repository browser.