1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # DSpacePlugout.pm -- the plugout module for DSpace archives
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 2006 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 | package DSpacePlugout;
|
---|
27 |
|
---|
28 | use strict;
|
---|
29 | no strict 'refs';
|
---|
30 | use utf8;
|
---|
31 | eval {require bytes};
|
---|
32 | use util;
|
---|
33 | use FileUtils;
|
---|
34 | use BasePlugout;
|
---|
35 |
|
---|
36 | sub BEGIN {
|
---|
37 | @DSpacePlugout::ISA = ('BasePlugout');
|
---|
38 | }
|
---|
39 |
|
---|
40 | my $arguments = [
|
---|
41 | { 'name' => "metadata_prefix",
|
---|
42 | 'desc' => "{DSpacePlugout.metadata_prefix}",
|
---|
43 | 'type' => "string",
|
---|
44 | 'reqd' => "no",
|
---|
45 | 'hiddengli' => "no"} ];
|
---|
46 |
|
---|
47 |
|
---|
48 | my $options = { 'name' => "DSpacePlugout",
|
---|
49 | 'desc' => "{DSpacePlugout.desc}",
|
---|
50 | 'abstract' => "no",
|
---|
51 | 'inherits' => "yes",
|
---|
52 | 'args' => $arguments };
|
---|
53 |
|
---|
54 | sub new {
|
---|
55 | my ($class) = shift (@_);
|
---|
56 | my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
|
---|
57 | push(@$plugoutlist, $class);
|
---|
58 |
|
---|
59 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
60 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
61 |
|
---|
62 | my $self = new BasePlugout($plugoutlist,$inputargs,$hashArgOptLists);
|
---|
63 |
|
---|
64 | # print STDERR "***** metadata prefix = \"", $self->{'metadata_prefix'}, "\"\n";
|
---|
65 |
|
---|
66 | return bless $self, $class;
|
---|
67 | }
|
---|
68 |
|
---|
69 | sub saveas_dspace_metadata
|
---|
70 | {
|
---|
71 | my $self = shift (@_);
|
---|
72 | my ($doc_obj,$working_dir,$metadata_file,$docroot,$metadata_prefix) = @_;
|
---|
73 |
|
---|
74 | # my $docroot_attributes = ($metadata_prefix eq "dc") ? undef : "schema=\"$metadata_prefix\"";
|
---|
75 | my $docroot_attributes = "schema=\"$metadata_prefix\"";
|
---|
76 |
|
---|
77 | my $doc_dc_file = &FileUtils::filenameConcatenate ($working_dir, $metadata_file);
|
---|
78 | $self->open_xslt_pipe($doc_dc_file,$self->{'xslt_file'});
|
---|
79 |
|
---|
80 | my $outhandler;
|
---|
81 | if (defined $self->{'xslt_writer'}){
|
---|
82 | $outhandler = $self->{'xslt_writer'};
|
---|
83 | }
|
---|
84 | else{
|
---|
85 | $outhandler = $self->get_output_handler($doc_dc_file);
|
---|
86 | }
|
---|
87 |
|
---|
88 | $self->output_general_xml_header($outhandler, $docroot, $docroot_attributes);
|
---|
89 |
|
---|
90 | my $metadata_hashmap = $doc_obj->get_metadata_hashmap($doc_obj->get_top_section(),
|
---|
91 | $metadata_prefix);
|
---|
92 |
|
---|
93 | if(defined $metadata_prefix && $metadata_prefix ne "") {
|
---|
94 | # merge dc with any ex.dc
|
---|
95 |
|
---|
96 | my $ex_dc_metadata_hashmap = $doc_obj->get_metadata_hashmap($doc_obj->get_top_section(),
|
---|
97 | "ex.dc");
|
---|
98 |
|
---|
99 | foreach my $metaname (keys %$ex_dc_metadata_hashmap) {
|
---|
100 | my $metaname_without_ex_prefix = $metaname;
|
---|
101 | $metaname_without_ex_prefix =~ s/^ex\.(.*)/$1/; # remove any ex from the ex.dc prefix
|
---|
102 |
|
---|
103 | # if there's an ex.dc value for a metaname for which no dc
|
---|
104 | # value was assigned, put the ex.dc value into the hashmap
|
---|
105 | if(!defined $metadata_hashmap->{$metaname_without_ex_prefix}) {
|
---|
106 | $metadata_hashmap->{$metaname_without_ex_prefix} = [];
|
---|
107 | push(@{$metadata_hashmap->{$metaname_without_ex_prefix}},@{$ex_dc_metadata_hashmap->{$metaname}});
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | foreach my $metaname (keys %$metadata_hashmap) {
|
---|
114 | my $metavals = $metadata_hashmap->{$metaname};
|
---|
115 |
|
---|
116 | my $qualifier = undef;
|
---|
117 | my $element;
|
---|
118 | if ($metaname =~ m/^(.*?)\^(.*?)$/) {
|
---|
119 | $element = $1;
|
---|
120 | $qualifier = $2;
|
---|
121 | $qualifier = lc($qualifier);
|
---|
122 | }
|
---|
123 | else {
|
---|
124 | $element = $metaname;
|
---|
125 | }
|
---|
126 | $element =~ s/^.*?\.//;
|
---|
127 | $element = lc($element);
|
---|
128 |
|
---|
129 | foreach my $metaval (@$metavals) {
|
---|
130 |
|
---|
131 | #if element is empty then no need to export it.
|
---|
132 |
|
---|
133 | if ($metaval =~ /\S/) {
|
---|
134 | print $outhandler " <dcvalue element=\"$element\"";
|
---|
135 | #If no qualifier then add qualifier="none"
|
---|
136 | #print $outhandler " qualifier=\"$qualifier\"" if defined $qualifier;
|
---|
137 | if (defined $qualifier) {
|
---|
138 | print $outhandler " qualifier=\"$qualifier\"" ;
|
---|
139 | }
|
---|
140 | else {
|
---|
141 | print $outhandler " qualifier=\"none\" language=\"\"" ;
|
---|
142 | }
|
---|
143 | print $outhandler ">$metaval";
|
---|
144 | print $outhandler "</dcvalue>\n";
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 |
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | $self->output_general_xml_footer($outhandler,$docroot);
|
---|
153 |
|
---|
154 | if (defined $self->{'xslt_writer'}){
|
---|
155 | $self->close_xslt_pipe();
|
---|
156 | }
|
---|
157 | else{
|
---|
158 | close($outhandler);
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | sub saveas {
|
---|
164 | my $self = shift (@_);
|
---|
165 | my ($doc_obj,$doc_dir) = @_;
|
---|
166 |
|
---|
167 | my $output_dir = $self->get_output_dir();
|
---|
168 | &FileUtils::makeAllDirectories ($output_dir) unless -e $output_dir;
|
---|
169 |
|
---|
170 | my $working_dir = &FileUtils::filenameConcatenate ($output_dir, $doc_dir);
|
---|
171 | &FileUtils::makeAllDirectories ($working_dir, $doc_dir);
|
---|
172 |
|
---|
173 | #########################
|
---|
174 | # save the handle file
|
---|
175 | #########################
|
---|
176 | my $outhandle = $self->{'output_handle'};
|
---|
177 |
|
---|
178 | my $generate_handle = 0;
|
---|
179 | if ($generate_handle) {
|
---|
180 | # Genereate handle file
|
---|
181 | # (Note: this section of code would benefit from being restructured)
|
---|
182 | my $doc_handle_file = &FileUtils::filenameConcatenate ($working_dir, "handle");
|
---|
183 |
|
---|
184 | my $env_hp = $ENV{'DSPACE_HANDLE_PREFIX'};
|
---|
185 | my $handle_prefix = (defined $env_hp) ? $env_hp : "123456789";
|
---|
186 |
|
---|
187 | my $outhandler = $self->get_output_handler($doc_handle_file);
|
---|
188 |
|
---|
189 | my ($handle) = ($doc_dir =~ m/^(.*)(:?\.dir)?$/);
|
---|
190 |
|
---|
191 | print $outhandler "$handle_prefix/$handle\n";
|
---|
192 |
|
---|
193 | close ($outhandler);
|
---|
194 | }
|
---|
195 |
|
---|
196 | #########################
|
---|
197 | # save the content file
|
---|
198 | #########################
|
---|
199 | my $doc_contents_file = &FileUtils::filenameConcatenate ($working_dir, "contents");
|
---|
200 |
|
---|
201 | my $outhandler = $self->get_output_handler($doc_contents_file);
|
---|
202 |
|
---|
203 | $self->process_assoc_files ($doc_obj, $doc_dir, $outhandler);
|
---|
204 |
|
---|
205 | $self->process_metafiles_metadata ($doc_obj);
|
---|
206 |
|
---|
207 | close($outhandler);
|
---|
208 |
|
---|
209 | #############################
|
---|
210 | # save the dublin_core.xml file
|
---|
211 | ###############################
|
---|
212 | # my $doc_dc_file = &FileUtils::filenameConcatenate ($working_dir, "dublin_core.xml");
|
---|
213 | # $self->open_xslt_pipe($doc_dc_file,$self->{'xslt_file'});
|
---|
214 |
|
---|
215 | # if (defined $self->{'xslt_writer'}){
|
---|
216 | # $outhandler = $self->{'xslt_writer'};
|
---|
217 | # }
|
---|
218 | # else{
|
---|
219 | # $outhandler = $self->get_output_handler($doc_dc_file);
|
---|
220 | # }
|
---|
221 |
|
---|
222 | # $self->output_general_xml_header($outhandler, "dublin_core");
|
---|
223 |
|
---|
224 | # my $all_text = $self->get_dc_metadata($doc_obj, $doc_obj->get_top_section());
|
---|
225 | # print $outhandler $all_text;
|
---|
226 |
|
---|
227 | # $self->output_general_xml_footer($outhandler,"dublin_core");
|
---|
228 |
|
---|
229 | # if (defined $self->{'xslt_writer'}){
|
---|
230 | # $self->close_xslt_pipe();
|
---|
231 | # }
|
---|
232 | # else{
|
---|
233 | # close($outhandler);
|
---|
234 | # }
|
---|
235 |
|
---|
236 | $self->saveas_dspace_metadata($doc_obj,$working_dir,
|
---|
237 | "dublin_core.xml","dublin_core","dc");
|
---|
238 |
|
---|
239 | my $metadata_prefix_list = $self->{'metadata_prefix'};
|
---|
240 | # print STDERR "***!! md prefix = $metadata_prefix_list\n";
|
---|
241 |
|
---|
242 | my @metadata_prefixes = split(/,\s*/,$metadata_prefix_list);
|
---|
243 | foreach my $ep (@metadata_prefixes) {
|
---|
244 | $self->saveas_dspace_metadata($doc_obj,$working_dir,
|
---|
245 | "metadata_$ep.xml","dublin_core",$ep);
|
---|
246 | }
|
---|
247 |
|
---|
248 | $self->{'short_doc_file'} = &FileUtils::filenameConcatenate ($doc_dir, "dublin_core.xml");
|
---|
249 | $self->store_output_info_reference($doc_obj);
|
---|
250 | }
|
---|
251 |
|
---|
252 | sub process_assoc_files {
|
---|
253 | my $self = shift (@_);
|
---|
254 | my ($doc_obj, $doc_dir, $handle) = @_;
|
---|
255 |
|
---|
256 | my $outhandler = $self->{'output_handle'};
|
---|
257 |
|
---|
258 | my $output_dir = $self->get_output_dir();
|
---|
259 | return if (!defined $output_dir);
|
---|
260 |
|
---|
261 | my $working_dir = &FileUtils::filenameConcatenate($output_dir, $doc_dir);
|
---|
262 |
|
---|
263 | my @assoc_files = ();
|
---|
264 | my $filename;;
|
---|
265 |
|
---|
266 | my $source_filename = $doc_obj->get_source_filename();
|
---|
267 |
|
---|
268 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
269 |
|
---|
270 | if (defined $collect_dir) {
|
---|
271 | my $dirsep_regexp = &util::get_os_dirsep();
|
---|
272 |
|
---|
273 | if ($collect_dir !~ /$dirsep_regexp$/) {
|
---|
274 | $collect_dir .= &util::get_dirsep(); # ensure there is a slash at the end
|
---|
275 | }
|
---|
276 |
|
---|
277 | # This test is never going to fail on Windows -- is this a problem?
|
---|
278 | if ($source_filename !~ /^$dirsep_regexp/) {
|
---|
279 | $source_filename = &FileUtils::filenameConcatenate($collect_dir, $source_filename);
|
---|
280 | }
|
---|
281 | }
|
---|
282 |
|
---|
283 | my ($tail_filename) = ($source_filename =~ m/([^\/\\]*)$/);
|
---|
284 |
|
---|
285 | print $handle "$tail_filename\n";
|
---|
286 |
|
---|
287 | $filename = &FileUtils::filenameConcatenate($working_dir, $tail_filename);
|
---|
288 | &FileUtils::hardLink ($source_filename, $filename, $self->{'verbosity'});
|
---|
289 |
|
---|
290 | # set the assocfile path (even if we have no assoc files - need this for lucene)
|
---|
291 | $doc_obj->set_utf8_metadata_element ($doc_obj->get_top_section(),
|
---|
292 | "assocfilepath",
|
---|
293 | "$doc_dir");
|
---|
294 | foreach my $assoc_file_rec (@{$doc_obj->get_assoc_files()}) {
|
---|
295 | my ($dir, $afile) = $assoc_file_rec->[1] =~ /^(.*?)([^\/\\]+)$/;
|
---|
296 | $dir = "" unless defined $dir;
|
---|
297 |
|
---|
298 |
|
---|
299 | my $real_filename = $assoc_file_rec->[0];
|
---|
300 | # for some reasons the image associate file has / before the full path
|
---|
301 | $real_filename =~ s/^\\(.*)/$1/i;
|
---|
302 | if (-e $real_filename) {
|
---|
303 | # escape backslashes and brackets in path for upcoming regex match
|
---|
304 | my $escaped_source_filename = &util::filename_to_regex($source_filename);
|
---|
305 | if ($real_filename =~ m/$escaped_source_filename$/) {
|
---|
306 | next;
|
---|
307 | }
|
---|
308 | else {
|
---|
309 | my $bundle = "bundle:ORIGINAL";
|
---|
310 |
|
---|
311 | if ($afile =~ m/^thumbnail\./) {
|
---|
312 | $bundle = "bundle:THUMBNAIL";
|
---|
313 | }
|
---|
314 |
|
---|
315 | # Store the associated file to the "contents" file. Cover.pdf not needed.
|
---|
316 | if ($afile ne "cover.jpg") {
|
---|
317 | print $handle "$assoc_file_rec->[1]\t$bundle\n";
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | $filename = &FileUtils::filenameConcatenate($working_dir, $afile);
|
---|
322 |
|
---|
323 | if ($afile ne "cover.jpg") {
|
---|
324 | &FileUtils::hardLink ($real_filename, $filename, $self->{'verbosity'});
|
---|
325 | $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
|
---|
326 | "gsdlassocfile",
|
---|
327 | "$afile:$assoc_file_rec->[2]:$dir");
|
---|
328 | }
|
---|
329 | } elsif ($self->{'verbosity'} > 2) {
|
---|
330 | print $outhandler "DSpacePlugout::process couldn't copy the associated file " .
|
---|
331 | "$real_filename to $afile\n";
|
---|
332 | }
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | sub get_new_doc_dir{
|
---|
338 | my $self = shift (@_);
|
---|
339 | my($working_info,$working_dir,$OID) = @_;
|
---|
340 |
|
---|
341 | return $OID;
|
---|
342 |
|
---|
343 | }
|
---|