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

Last change on this file since 20792 was 20792, checked in by kjdon, 15 years ago

store the oai file as a metadata fiels for the source doc if is exists, using the extrametafile mechanism

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