source: trunk/gsdl/perllib/plugins/OAIPlug.pm@ 13222

Last change on this file since 13222 was 13222, checked in by shaoqun, 17 years ago

added a method required by XMLPlug

  • Property svn:keywords set to Author Date Id Revision
File size: 9.6 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 OAIPlug;
28
29use BasPlug;
30use unicode;
31use util;
32
33use strict;
34no strict 'refs'; # allow filehandles to be variables and viceversa
35
36use XMLPlug;
37
38sub BEGIN {
39 @OAIPlug::ISA = ('XMLPlug');
40}
41
42
43my $arguments =
44 [ { 'name' => "process_exp",
45 'desc' => "{BasPlug.process_exp}",
46 'type' => "regexp",
47 'reqd' => "no",
48 'deft' => &get_default_process_exp() },
49 ];
50
51my $options = { 'name' => "OAIPlug",
52 'desc' => "{OAIPlug.desc}",
53 'abstract' => "no",
54 'inherits' => "yes",
55 'args' => $arguments };
56
57
58sub new {
59 my ($class) = shift (@_);
60 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
61 push(@$pluginlist, $class);
62
63 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
64 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
65
66 my $self = new XMLPlug($pluginlist, $inputargs, $hashArgOptLists);
67
68 return bless $self, $class;
69}
70
71sub get_default_process_exp {
72 my $self = shift (@_);
73
74 return q^(?i)(\.oai)$^;
75}
76
77sub get_doctype {
78 my $self = shift(@_);
79
80 return "OAI-PMH";
81}
82
83sub xml_start_document {
84 my $self = shift (@_);
85 $self->{'in_metadata_node'} = 0;
86 $self->{'rawxml'} = "";
87}
88
89sub xml_end_document {
90}
91
92sub xml_doctype {
93 my $self = shift(@_);
94
95 my ($expat, $name, $sysid, $pubid, $internal) = @_;
96
97 # allow the short-lived and badly named "GreenstoneArchive" files to be processed
98 # as well as the "Archive" files which should now be created by import.pl
99 die "" if ($name !~ /^OAI-PMH$/);
100
101 my $outhandle = $self->{'outhandle'};
102 print $outhandle "OAIPlug: processing $self->{'file'}\n" if $self->{'verbosity'} > 1;
103 print STDERR "<Processing n='$self->{'file'}' p='OAIPlug'>\n" if $self->{'gli'};
104
105}
106
107
108sub xml_start_tag {
109 my $self = shift(@_);
110 my ($expat,$element) = @_;
111
112 my %attr_hash = %_;
113
114 my $attr = "";
115 map { $attr .= " $_=$attr_hash{$_}"; } keys %attr_hash;
116
117 $self->{'rawxml'} .= "<$element$attr>";
118
119 if ($element eq "metadata") {
120 $self->{'in_metadata_node'} = 1;
121 $self->{'metadata_xml'} = "";
122 }
123
124 if ($self->{'in_metadata_node'}) {
125 $self->{'metadata_xml'} .= "<$element$attr>";
126 }
127}
128
129sub xml_end_tag {
130 my $self = shift(@_);
131 my ($expat, $element) = @_;
132
133 $self->{'rawxml'} .= "</$element>";
134
135 if ($self->{'in_metadata_node'}) {
136 $self->{'metadata_xml'} .= "</$element>";
137 }
138
139 if ($element eq "metadata") {
140 my $textref = \$self->{'metadata_xml'};
141 my $metadata = $self->{'metadata'};
142 $self->extract_oai_metadata($textref,$metadata);
143
144 $self->{'in_metadata_node'} = 0;
145 }
146
147
148}
149
150sub xml_text {
151 my $self = shift(@_);
152 my ($expat) = @_;
153
154 $self->{'rawxml'} .= $_;
155
156 if ($self->{'in_metadata_node'}) {
157 $self->{'metadata_xml'} .= $_;
158 }
159}
160
161
162
163
164sub read {
165 my $self = shift (@_);
166
167 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
168
169 my $outhandle = $self->{'outhandle'};
170
171 my $filename = $file;
172 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
173
174 return 0 if ((-d $filename) && ($filename =~ m/srcdocs$/));
175
176 if ($self->SUPER::read(@_)) {
177
178 # Do encoding stuff
179 my ($language, $encoding) = $self->textcat_get_language_encoding ($filename);
180
181 my $url_array = $metadata->{'URL'};
182 my $num_urls = (defined $url_array) ? scalar(@$url_array) : 0;
183
184 my $srcdoc_exists = 0;
185 my $srcdoc_pos = 0;
186 my $filename_dir = &util::filename_head($filename);
187
188 for (my $i=0; $i<$num_urls; $i++) {
189
190 if ($url_array->[$i] !~ m/^(http|ftp):/) {
191
192 my $src_filename = &util::filename_cat($filename_dir, $url_array->[$i]);
193
194 if (-e $src_filename) {
195 $srcdoc_pos = $i;
196 $srcdoc_exists = 1;
197 }
198 }
199 }
200
201 if ($srcdoc_exists)
202 {
203 print $outhandle "OAIPlug: passing metadata on to $url_array->[0]\n"
204 if ($self->{'verbosity'}>1);
205
206
207 # Make pretty print metadata table stick with src filename
208 my $ppmd_table = $self->{'ppmd_table'};
209 $metadata->{'prettymd'} = [ $ppmd_table ];
210 $self->{'ppmd_table'} = undef;
211
212 return &plugin::read ($pluginfo, $filename_dir, $url_array->[0],
213 $metadata, $processor, $maxdocs, $total_count, $gli);
214 }
215 else
216 {
217 # create a new document
218 my $doc_obj = new doc ($filename, "indexed_doc");
219 my $top_section = $doc_obj->get_top_section;
220 my $plugin_type = $self->{'plugin_type'};
221
222 $doc_obj->add_utf8_metadata($top_section, "Language", $language);
223 $doc_obj->add_utf8_metadata($top_section, "Encoding", $encoding);
224 $doc_obj->add_utf8_metadata($top_section, "Plugin", $plugin_type);
225 $doc_obj->add_metadata($top_section, "FileFormat", "OAI");
226 $doc_obj->add_metadata($top_section, "FileSize", (-s $filename));
227
228 # include any metadata passed in from previous plugins
229 # note that this metadata is associated with the top level section
230 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
231
232 # do plugin specific processing of doc_obj
233 my $textref = \$self->{'rawxml'};
234 unless (defined ($self->process($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
235 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
236 return -1;
237 }
238
239 # do any automatic metadata extraction
240 $self->auto_extract_metadata ($doc_obj);
241
242 # add an OID
243 $doc_obj->set_OID();
244
245 my $ppmd_table = $self->{'ppmd_table'};
246 $doc_obj->add_utf8_metadata($top_section,"prettymd",$ppmd_table);
247 $self->{'ppmd_table'} = undef;
248
249 # process the document
250 $processor->process($doc_obj);
251
252 $self->{'num_processed'} ++;
253
254 return 1; # processed the file
255 }
256 }
257 else {
258 return undef;
259 }
260}
261
262
263# do plugin specific processing of doc_obj
264sub process {
265 my $self = shift (@_);
266 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
267 my $outhandle = $self->{'outhandle'};
268
269 print STDERR "<Processing n='$file' p='OAIPlug'>\n" if ($gli);
270 print $outhandle "OAIPlug: processing $file\n"
271 if $self->{'verbosity'} > 1;
272
273 my $cursection = $doc_obj->get_top_section();
274
275## $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection);
276
277 # add text to document object
278
279# $$textref =~ s/<(.*?)>/$1 /g;
280 $$textref =~ s/</&lt;/g;
281 $$textref =~ s/>/&gt;/g;
282
283## print STDERR "*** adding text: $$textref\n";
284
285 $doc_obj->add_utf8_text($cursection, $$textref);
286
287 return 1;
288}
289
290
291# Improvement is to merge this with newer version in MetadataPass
292
293sub open_prettyprint_metadata_table
294{
295 my $self = shift(@_);
296
297 my $att = "width=100% cellspacing=2";
298 my $style = "style=\'border-bottom: 4px solid #000080\'";
299
300 $self->{'ppmd_table'} = "\n<table $att $style>";
301}
302
303sub add_prettyprint_metadata_line
304{
305 my $self = shift(@_);
306 my ($metaname, $metavalue_utf8) = @_;
307
308 $metavalue_utf8 =~ s/hdl\.handle\.net/mcgonagall.cs.waikato.ac.nz:8080\/dspace\/handle/;
309 $metavalue_utf8 = &util::hyperlink_text($metavalue_utf8);
310
311 $self->{'ppmd_table'} .= " <tr bgcolor=#b5d3cd>\n";
312 $self->{'ppmd_table'} .= " <td width=30%>\n";
313 $self->{'ppmd_table'} .= " $metaname\n";
314 $self->{'ppmd_table'} .= " </td>\n";
315 $self->{'ppmd_table'} .= " <td>\n";
316 $self->{'ppmd_table'} .= " $metavalue_utf8\n";
317 $self->{'ppmd_table'} .= " </td>\n";
318 $self->{'ppmd_table'} .= " </tr>\n";
319
320}
321
322sub close_prettyprint_metadata_table
323{
324 my $self = shift(@_);
325
326 $self->{'ppmd_table'} .= "</table>\n";
327}
328
329
330
331
332sub extract_oai_metadata {
333 my $self = shift (@_);
334 my ($textref, $metadata) = @_;
335 my $outhandle = $self->{'outhandle'};
336
337 # Only handles DC metadata
338
339 $self->open_prettyprint_metadata_table();
340
341 if ($$textref =~ m/<metadata\s*>(.*?)<\/metadata\s*>/s)
342 {
343 my $metadata_text = $1;
344 $metadata_text =~ s/^.*?<(oai_dc:)?dc.*?>(.*?)<\/(oai_dc:)?dc>.*?/$2/s;
345
346 while ($metadata_text =~ m/<(.*?)>(.*?)<\/(.*?)>(.*)/s)
347 {
348 # if URL given for document as identifier metadata, store it ...
349 # $doc_obj->add_utf8_metadata($cursection, "URL", $web_url);
350
351 my $metaname = $1;
352 my $metavalue = $2;
353 $metadata_text = $4;
354
355 $metaname =~ s/^(dc:)?(.)/\u$2/;
356
357 if ($metaname eq "Identifier")
358 {
359 # name clashes with GSDL reserved metadata name for hash id
360 $metaname = "URL";
361 }
362
363 if (defined $metadata->{$metaname})
364 {
365 push(@{$metadata->{$metaname}},$metavalue);
366
367 }
368 else
369 {
370 $metadata->{$metaname} = [ $metavalue ];
371 }
372
373 $self->add_prettyprint_metadata_line($metaname, $metavalue);
374
375 }
376 }
377
378 $self->close_prettyprint_metadata_table();
379}
380
3811;
Note: See TracBrowser for help on using the repository browser.