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

Last change on this file since 24951 was 24951, checked in by ak19, 12 years ago

All perlcode that accesses extrametakeys, extrametadata, extrametafile data structures has been moved into a new perl module called extrametautil.pm. The next step will be to ensure that the file_regexes used to index into these data structures are consistent (using consistent slashes, like URL style slashes).

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