[12330] | 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';
|
---|
[23824] | 30 | use utf8;
|
---|
[12330] | 31 | eval {require bytes};
|
---|
| 32 | use util;
|
---|
[27882] | 33 | use FileUtils;
|
---|
[17203] | 34 | use BasePlugout;
|
---|
[12330] | 35 |
|
---|
| 36 | sub BEGIN {
|
---|
[17203] | 37 | @DSpacePlugout::ISA = ('BasePlugout');
|
---|
[12330] | 38 | }
|
---|
| 39 |
|
---|
[23824] | 40 | my $arguments = [
|
---|
| 41 | { 'name' => "metadata_prefix",
|
---|
| 42 | 'desc' => "{DSpacePlugout.metadata_prefix}",
|
---|
| 43 | 'type' => "string",
|
---|
| 44 | 'reqd' => "no",
|
---|
| 45 | 'hiddengli' => "no"} ];
|
---|
[12330] | 46 |
|
---|
[23824] | 47 |
|
---|
[12330] | 48 | my $options = { 'name' => "DSpacePlugout",
|
---|
| 49 | 'desc' => "{DSpacePlugout.desc}",
|
---|
| 50 | 'abstract' => "no",
|
---|
[23824] | 51 | 'inherits' => "yes",
|
---|
| 52 | 'args' => $arguments };
|
---|
[12330] | 53 |
|
---|
| 54 | sub new {
|
---|
| 55 | my ($class) = shift (@_);
|
---|
| 56 | my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
|
---|
| 57 | push(@$plugoutlist, $class);
|
---|
| 58 |
|
---|
[17203] | 59 | push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
|
---|
| 60 | push(@{$hashArgOptLists->{"OptList"}},$options);
|
---|
[12330] | 61 |
|
---|
[17203] | 62 | my $self = new BasePlugout($plugoutlist,$inputargs,$hashArgOptLists);
|
---|
[23824] | 63 |
|
---|
[24402] | 64 | # print STDERR "***** metadata prefix = \"", $self->{'metadata_prefix'}, "\"\n";
|
---|
[23824] | 65 |
|
---|
[17203] | 66 | return bless $self, $class;
|
---|
[12330] | 67 | }
|
---|
| 68 |
|
---|
[23824] | 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 |
|
---|
[27882] | 77 | my $doc_dc_file = &FileUtils::filenameConcatenate ($working_dir, $metadata_file);
|
---|
[23824] | 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 |
|
---|
[24402] | 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 |
|
---|
[23824] | 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 |
|
---|
[12330] | 163 | sub saveas {
|
---|
| 164 | my $self = shift (@_);
|
---|
| 165 | my ($doc_obj,$doc_dir) = @_;
|
---|
| 166 |
|
---|
| 167 | my $output_dir = $self->get_output_dir();
|
---|
[27882] | 168 | &FileUtils::makeAllDirectories ($output_dir) unless -e $output_dir;
|
---|
[12330] | 169 |
|
---|
[27882] | 170 | my $working_dir = &FileUtils::filenameConcatenate ($output_dir, $doc_dir);
|
---|
| 171 | &FileUtils::makeAllDirectories ($working_dir, $doc_dir);
|
---|
[12330] | 172 |
|
---|
| 173 | #########################
|
---|
| 174 | # save the handle file
|
---|
| 175 | #########################
|
---|
| 176 | my $outhandle = $self->{'output_handle'};
|
---|
| 177 |
|
---|
[23824] | 178 | my $generate_handle = 0;
|
---|
| 179 | if ($generate_handle) {
|
---|
| 180 | # Genereate handle file
|
---|
| 181 | # (Note: this section of code would benefit from being restructured)
|
---|
[27882] | 182 | my $doc_handle_file = &FileUtils::filenameConcatenate ($working_dir, "handle");
|
---|
[12330] | 183 |
|
---|
[23824] | 184 | my $env_hp = $ENV{'DSPACE_HANDLE_PREFIX'};
|
---|
| 185 | my $handle_prefix = (defined $env_hp) ? $env_hp : "123456789";
|
---|
[12330] | 186 |
|
---|
[23824] | 187 | my $outhandler = $self->get_output_handler($doc_handle_file);
|
---|
[12330] | 188 |
|
---|
[23824] | 189 | my ($handle) = ($doc_dir =~ m/^(.*)(:?\.dir)?$/);
|
---|
[12330] | 190 |
|
---|
[23824] | 191 | print $outhandler "$handle_prefix/$handle\n";
|
---|
[12330] | 192 |
|
---|
[23824] | 193 | close ($outhandler);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
[12330] | 196 | #########################
|
---|
| 197 | # save the content file
|
---|
| 198 | #########################
|
---|
[27882] | 199 | my $doc_contents_file = &FileUtils::filenameConcatenate ($working_dir, "contents");
|
---|
[12330] | 200 |
|
---|
[23824] | 201 | my $outhandler = $self->get_output_handler($doc_contents_file);
|
---|
[12330] | 202 |
|
---|
| 203 | $self->process_assoc_files ($doc_obj, $doc_dir, $outhandler);
|
---|
| 204 |
|
---|
[20642] | 205 | $self->process_metafiles_metadata ($doc_obj);
|
---|
| 206 |
|
---|
[12330] | 207 | close($outhandler);
|
---|
| 208 |
|
---|
[23824] | 209 | #############################
|
---|
[12330] | 210 | # save the dublin_core.xml file
|
---|
| 211 | ###############################
|
---|
[27882] | 212 | # my $doc_dc_file = &FileUtils::filenameConcatenate ($working_dir, "dublin_core.xml");
|
---|
[23824] | 213 | # $self->open_xslt_pipe($doc_dc_file,$self->{'xslt_file'});
|
---|
[12330] | 214 |
|
---|
[23824] | 215 | # if (defined $self->{'xslt_writer'}){
|
---|
| 216 | # $outhandler = $self->{'xslt_writer'};
|
---|
| 217 | # }
|
---|
| 218 | # else{
|
---|
| 219 | # $outhandler = $self->get_output_handler($doc_dc_file);
|
---|
| 220 | # }
|
---|
[12330] | 221 |
|
---|
[23824] | 222 | # $self->output_general_xml_header($outhandler, "dublin_core");
|
---|
[13172] | 223 |
|
---|
[23824] | 224 | # my $all_text = $self->get_dc_metadata($doc_obj, $doc_obj->get_top_section());
|
---|
| 225 | # print $outhandler $all_text;
|
---|
[13172] | 226 |
|
---|
[23824] | 227 | # $self->output_general_xml_footer($outhandler,"dublin_core");
|
---|
[12330] | 228 |
|
---|
[23824] | 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'};
|
---|
[24402] | 240 | # print STDERR "***!! md prefix = $metadata_prefix_list\n";
|
---|
[23824] | 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);
|
---|
[12330] | 246 | }
|
---|
[23824] | 247 |
|
---|
[27882] | 248 | $self->{'short_doc_file'} = &FileUtils::filenameConcatenate ($doc_dir, "dublin_core.xml");
|
---|
[12363] | 249 | $self->store_output_info_reference($doc_obj);
|
---|
[12330] | 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 |
|
---|
[27882] | 261 | my $working_dir = &FileUtils::filenameConcatenate($output_dir, $doc_dir);
|
---|
[12330] | 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/) {
|
---|
[27882] | 279 | $source_filename = &FileUtils::filenameConcatenate($collect_dir, $source_filename);
|
---|
[12330] | 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[20903] | 283 | my ($tail_filename) = ($source_filename =~ m/([^\/\\]*)$/);
|
---|
[12330] | 284 |
|
---|
| 285 | print $handle "$tail_filename\n";
|
---|
| 286 |
|
---|
[27882] | 287 | $filename = &FileUtils::filenameConcatenate($working_dir, $tail_filename);
|
---|
| 288 | &FileUtils::hardLink ($source_filename, $filename, $self->{'verbosity'});
|
---|
[12330] | 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) {
|
---|
[24829] | 303 | # escape backslashes and brackets in path for upcoming regex match
|
---|
| 304 | my $escaped_source_filename = &util::filename_to_regex($source_filename);
|
---|
[22921] | 305 | if ($real_filename =~ m/$escaped_source_filename$/) {
|
---|
[12330] | 306 | next;
|
---|
| 307 | }
|
---|
| 308 | else {
|
---|
| 309 | my $bundle = "bundle:ORIGINAL";
|
---|
| 310 |
|
---|
| 311 | if ($afile =~ m/^thumbnail\./) {
|
---|
| 312 | $bundle = "bundle:THUMBNAIL";
|
---|
| 313 | }
|
---|
| 314 |
|
---|
[23824] | 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";
|
---|
[12330] | 318 | }
|
---|
[23824] | 319 | }
|
---|
[12330] | 320 |
|
---|
[27882] | 321 | $filename = &FileUtils::filenameConcatenate($working_dir, $afile);
|
---|
[12330] | 322 |
|
---|
[23824] | 323 | if ($afile ne "cover.jpg") {
|
---|
[27882] | 324 | &FileUtils::hardLink ($real_filename, $filename, $self->{'verbosity'});
|
---|
[23824] | 325 | $doc_obj->add_utf8_metadata ($doc_obj->get_top_section(),
|
---|
| 326 | "gsdlassocfile",
|
---|
| 327 | "$afile:$assoc_file_rec->[2]:$dir");
|
---|
| 328 | }
|
---|
[12330] | 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 | }
|
---|