From 1683cd6d1441e060dd0cc7d26b5dda6c8fe2151c Mon Sep 17 00:00:00 2001 From: Andrew Hercules Date: Fri, 12 Aug 2016 15:51:04 +0100 Subject: [PATCH] Updating plugin code to resolve server error issue --- .../EPrints/Plugin/Event/DataCiteEvent.pm | 36 +++++++------- .../EPrints/Plugin/Export/DataCiteXML.pm | 49 ++++++++++--------- 2 files changed, 45 insertions(+), 40 deletions(-) diff --git a/lib/plugins/EPrints/Plugin/Event/DataCiteEvent.pm b/lib/plugins/EPrints/Plugin/Event/DataCiteEvent.pm index 7008437..3e0d210 100644 --- a/lib/plugins/EPrints/Plugin/Event/DataCiteEvent.pm +++ b/lib/plugins/EPrints/Plugin/Event/DataCiteEvent.pm @@ -5,27 +5,27 @@ EPrints::Plugin::Event::DataCiteEvent =cut package EPrints::Plugin::Event::DataCiteEvent; - + use EPrints::Plugin::Event; use LWP; -use Crypt::SSLeay; +# use Crypt::SSLeay; use HTTP::Headers::Util; @ISA = qw( EPrints::Plugin::Event ); - + sub datacite_doi { my( $self, $dataobj) = @_; my $repository = $self->repository(); - + #Check object status first.... TODO Make work for dataobj == doument (just in case) - my $shoulddoi = $repository->get_conf( "datacitedoi", "eprintstatus", $dataobj->value( "eprint_status" )); + my $shoulddoi = $repository->get_conf( "datacitedoi", "eprintstatus", $dataobj->value( "eprint_status" )); #Check Doi Status - if(!$shoulddoi){ + if(!$shoulddoi){ $repository->log("Attempt to coin DOI for item that is not in the required area (see \$c->{datacitedoi}->{eprintstatus})"); return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR; - } + } my $thisdoi = $self->coin_doi($repository,$dataobj); #coin_doi may return an event error code if no prefix present assume this is the case @@ -41,12 +41,12 @@ sub datacite_doi $url.="/" if($url !~ /\/$/); #attach slash if config has forgotten my $user_name = $repository->get_conf( "datacitedoi", "user"); my $user_pw = $repository->get_conf( "datacitedoi", "pass"); - + #register metadata; my($response_content, $response_code) = datacite_request("POST", $url."metadata", $user_name, $user_pw, $xml, "application/xml;charset=UTF-8"); if($response_code !~ /20(1|0)/){ $repository->log("Metadata response from datacite api: $response_code: $response_content"); - $repository->log("BTW the \$doi was:\n$thisdoi"); + $repository->log("BTW the \$doi was:\n$thisdoi"); return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR; } #register doi @@ -94,7 +94,7 @@ sub datacite_request { return ($res->content(),$res->code()); } -#RM lets do the DOI coining somewhere (reasonably) accessible +#RM lets do the DOI coining somewhere (reasonably) accessible sub coin_doi { my( $self, $repository, $dataobj) = @_; @@ -113,7 +113,7 @@ sub coin_doi { my $thisdoi = $prefix.$delim1.$repository->get_conf( "datacitedoi", "repoid").$delim2.$id; my $eprintdoifield = $repository->get_conf( "datacitedoi", "eprintdoifield"); - + #Custom DOIS #if DOI field is set attempt to use that if config allows if($dataobj->exists_and_set( $eprintdoifield) ){ @@ -125,12 +125,14 @@ sub coin_doi { } #we are allowed (check prefix just in case) $thisdoi = $dataobj->get_value( $eprintdoifield ); - if($thisdoi !~ /^$prefix/){ - $repository->log("Prefix does not match ($prefix) for custom DOI: $thisdoi"); - $dataobj->set_value($eprintdoifield, ""); #unset the bad DOI!! - $dataobj->commit(); - return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR; - }#We'll leave Datacite to do any further syntax checking etc... + # AH commented out because when there is an existing DOI (e.g. one issued by the publisher) + # the condition is always true and therefore, existing DOI becomes an empty string + # if($thisdoi !~ /^$prefix/){ + # $repository->log("Prefix does not match ($prefix) for custom DOI: $thisdoi"); + # $dataobj->set_value($eprintdoifield, ""); #unset the bad DOI!! + # $dataobj->commit(); + # return EPrints::Const::HTTP_INTERNAL_SERVER_ERROR; + # }#We'll leave Datacite to do any further syntax checking etc... } return $thisdoi; diff --git a/lib/plugins/EPrints/Plugin/Export/DataCiteXML.pm b/lib/plugins/EPrints/Plugin/Export/DataCiteXML.pm index 67f7e6e..24aea5b 100644 --- a/lib/plugins/EPrints/Plugin/Export/DataCiteXML.pm +++ b/lib/plugins/EPrints/Plugin/Export/DataCiteXML.pm @@ -23,7 +23,7 @@ sub new $self->{suffix} = '.xml'; $self->{mimetype} = 'application/xml; charset=utf-8'; $self->{arguments}->{doi} = undef; - + return $self; } @@ -33,17 +33,20 @@ sub output_dataobj my $repo = $self->{repository}; my $xml = $repo->xml; - + #reference the datacite schema from config - my $entry = $xml->create_element( "resource", - xmlns=> $repo->get_conf( "datacitedoi", "xmlns"), - "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", - "xsi:schemaLocation" => $repo->get_conf( "datacitedoi", "schemaLocation")); - - #RM We pass in the DOI from Event::DataCite... or from --args on the cmd line - my $thisdoi = $opts{doi}; - #RM coin a DOI if either - # - not come via event or + my $entry = $xml->create_element( "resource", + xmlns=> $repo->get_conf( "datacitedoi", "xmlns"), + "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance", + "xsi:schemaLocation" => $repo->get_conf( "datacitedoi", "schemaLocation")); + + #RM We pass in the DOI from Event::DataCite... or from --args on the cmd line + # AH my $thisdoi = $opts{doi}; always returns undefined, even when DOI exists + # Ideally coining should NOT happen in this script but opts{doi} should have it + # but is always blank + my $thisdoi = $dataobj->get_value("id_number"); + #RM coin a DOI if either + # - not come via event or # - no doi arg passed in via cmd_line # ie when someone exports DataCiteXML from the Action tab if(!defined $thisdoi){ @@ -56,13 +59,13 @@ sub output_dataobj } $entry->appendChild( $xml->create_data_element( "identifier", $thisdoi , identifierType=>"DOI" ) ); - + #RM otherwise we'll leave this alone for now my $creators = $xml->create_element( "creators" ); if( $dataobj->exists_and_set( "creators" ) ) { - + my $names = $dataobj->get_value( "creators" ); foreach my $name ( @$names ) { @@ -83,13 +86,13 @@ sub output_dataobj $titles->appendChild( $xml->create_data_element( "title", $dataobj->render_value( "title" ) ) ); $entry->appendChild( $titles ); } - + $entry->appendChild( $xml->create_data_element( "publisher", $repo->get_conf( "datacitedoi", "publisher") ) ); - + if ($dataobj->exists_and_set( "datestamp" )) { $dataobj->get_value( "datestamp" ) =~ /^([0-9]{4})/; $entry->appendChild( $xml->create_data_element( "publicationYear", $1 ) ) if $1; - + } @@ -101,23 +104,23 @@ sub output_dataobj my $subject = EPrints::DataObj::Subject->new( $repo, $val ); next unless defined $subject; $subjects_tag->appendChild( $xml->create_data_element( "subject", $subject->render_description ) ); - + } $entry->appendChild( $subjects_tag ); } } - - - my $thisresourceType = $repo->get_conf( "datacitedoi", "typemap", $dataobj->get_value("type") ); + + + my $thisresourceType = $repo->get_conf( "datacitedoi", "typemap", $dataobj->get_value("type") ); if(defined $thisresourceType ){ $entry->appendChild( $xml->create_data_element( "resourceType", $thisresourceType->{'v'}, resourceTypeGeneral=>$thisresourceType->{'a'}) ); } - - + + my $alternateIdentifiers = $xml->create_element( "alternateIdentifiers" ); $alternateIdentifiers->appendChild( $xml->create_data_element( "alternateIdentifier", $dataobj->get_url() , alternateIdentifierType=>"URL" ) ); $entry->appendChild( $alternateIdentifiers ); - + #TODO Seek, identify and include for registration the optional datacite fields return ''."\n".$xml->to_string($entry);