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

Last change on this file was 36373, checked in by kjdon, 20 months ago

now I have removed commented out code from last commit

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