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

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

tidied this up and removed some old code

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