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

Last change on this file since 19622 was 19622, checked in by ak19, 15 years ago

the filename for metadata needs to have the slashes the right way

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