- getMetadataForDoi and updateMetadataForDoi in DoiClient.php

- doi_datacite.xslt: show Subtitle
- further tests in DoiClientTest.php
- DoiController.php: safe DOI metadta in table dataset_identifiers
This commit is contained in:
Arno Kaimbacher 2021-03-01 16:04:02 +01:00
parent 9b6a6469d7
commit 8f0b12fbf0
6 changed files with 141 additions and 57 deletions

View File

@ -78,7 +78,7 @@ class DoiController extends Controller
$dataId = $request->input('publish_id');
// Setup stylesheet
$this->loadStyleSheet(public_path() .'\prefixes\doi_datacite.xslt');
$this->loadStyleSheet(public_path() .'/prefixes/doi_datacite.xslt');
// set timestamp
$date = new \DateTime();
@ -114,14 +114,14 @@ class DoiController extends Controller
$appUrl = config('app.url');
$landingPageUrl = $appUrl . "/dataset/" . $dataset->publish_id;
$response = $this->doiClient->registerDoi($doiValue, $xmlMeta, $landingPageUrl);
// if operation successful
if ($response->getStatusCode() == 201) {
$doi = new DatasetIdentifier();
$doi['value'] = $doiValue;
$doi['dataset_id'] = $dataset->id;
$doi['type'] = "doi";
$doi['status'] = "registered";
$doi['registration_ts'] = now();
// $doi->save();
$doi->save();
}
}

View File

@ -6,7 +6,7 @@ use Illuminate\Database\Eloquent\Model;
class DatasetIdentifier extends Model
{
protected $table = 'dataset_identifierss';
protected $table = 'dataset_identifiers';
protected $guarded = array();
/**

View File

@ -33,7 +33,7 @@ class DoiClient implements DoiInterface
}
/**
* Creates a DOI or ARK with the given identifier
* Creates a DOI with the given identifier
*
* @param string $identifier The desired DOI identifier e.g. '10.5072/tethys.999',
* @param $xmlMeta
@ -68,7 +68,6 @@ class DoiClient implements DoiInterface
}
// Response Codes
// 201 Created: operation successful
// 400 Bad Request: invalid XML, wrong prefix
// 401 Unauthorised: no login
// 403 Forbidden: login problem, quota exceeded
// 415 Wrong Content Type : Not including content type in the header.
@ -167,18 +166,74 @@ class DoiClient implements DoiInterface
return ($statusCode == 200 && $landingPageURL == $body);
}
public function getMetadataForDoi($identifier)
public function getMetadataForDoi($doiValue)
{
//
$response = null;
$url = $this->serviceUrl . '/metadata/' . $doiValue;
try {
$client = new Client([
'auth' => [$this->username, $this->password],
'base_uri' => $url,
'verify' => false,
]);
$response = $client->request('GET');
} catch (\Exception $e) {
$message = 'request to ' . $url . ' failed with ' . $e->getMessage();
throw new DoiClientException($message);
}
// Response Codes
// 200 OK: operation successful;
// 204 No Content: the DOI is known to DataCite Metadata Store (MDS), but no metadata have been registered;
// 401 Unauthorised: no login
// 403 Forbidden: permission problem or dataset belongs to another party;
// 404 Not Found: DOI does not exist in our database.
// 422 Unprocessable Entity Metadata failed validation against the DataCite Schema.
if ($response->getStatusCode() != 200) {
$message = 'unexpected DataCite MDS response code ' . $response->getStatusCode();
// $this->log($message, 'err');
throw new DoiClientException($message);
}
return $response;
}
public function updateMetadataForDoi($identifier, $new_meta)
public function updateMetadataForDoi($doiValue, $newMeta)
{
//
$response = null;
$url = $this->serviceUrl . '/metadata/' . $doiValue;
try {
$client = new Client([
'auth' => [$this->username, $this->password],
// 'base_uri' => $url,
'verify' => false,
'headers' => [
'Content-Type' => 'application/xml;charset=UTF-8',
],
// 'body' => $xmlMeta,
]);
// Provide the body as a string.
$response = $client->request('PUT', $url, [
'body' => $newMeta,
]);
} catch (\Exception $e) {
$message = 'request to ' . $url . ' failed with ' . $e->getMessage();
// $this->log($message, 'err');
throw new DoiClientException($message);
}
// Response Codes
// 201 Created: operation successful
// 401 Unauthorised: no login
// 403 Forbidden: login problem, quota exceeded
// 415 Wrong Content Type : Not including content type in the header.
// 422 Unprocessable Entity : invalid XML
if ($response->getStatusCode() != 201) {
$message = 'unexpected DataCite MDS response code ' . $response->getStatusCode();
// $this->log($message, 'err');
throw new DoiClientException($message);
}
}
/**
* Markiert den Datensatz zur übergebenen DOI als inaktiv.
* Markiert den Datensatz zur übergebenen DOI als inaktiv - Status registered (not findable)
*
* @param $doiValue
*

View File

@ -19,7 +19,7 @@ class CreateDatasetIdentifiersTable extends Migration
// foreign key to: documents.id
$table->integer('dataset_id')->unsigned();
;
$table->foreign('dataset_id')->references('id')->on('documents')
->onUpdate('cascade')->onDelete('cascade');
@ -34,8 +34,6 @@ class CreateDatasetIdentifiersTable extends Migration
'status',
['draft', 'registered', 'findable']
)->nullable();
// timestamp of DOI registration
$table->timestamp('registration_ts');
$table->timestamps();
});
}

View File

@ -227,11 +227,21 @@
<xsl:value-of select="@Language" />
</xsl:attribute>
</xsl:if>
<xsl:if test="@Type != '' and @Type != 'Main'">
<xsl:attribute name="titleType">
<xsl:value-of select="@Type" />
</xsl:attribute>
</xsl:if>
<xsl:choose>
<xsl:when test="@Type != '' and @Type != 'Sub' and @Type != 'Main'">
<xsl:attribute name="titleType">
<xsl:value-of select="@Type" />
<xsl:text>Title</xsl:text>
</xsl:attribute>
</xsl:when>
<xsl:when test="@Type = 'Sub'">
<xsl:attribute name="titleType">
<xsl:value-of select="@Type" />
<xsl:text>title</xsl:text>
</xsl:attribute>
</xsl:when>
</xsl:choose>
<xsl:value-of select="@Value"/>
</title>
</xsl:template>

View File

@ -7,6 +7,7 @@ use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Tethys\Utils\DoiClient;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class DoiClientTest extends TestCase
{
@ -21,48 +22,68 @@ class DoiClientTest extends TestCase
// $response->assertStatus(200);
// }
// public function testCheckDoi()
// {
// $client = new DoiClient();
// // $this->setExpectedException('Opus\Doi\ClientException');
// $result = $client->checkDoi(
// '10.5072/tethys-999',
// 'http://localhost/opus4/frontdoor/index/index/999'
// );
// $this->assertTrue($result);
public function testCheckDoi()
{
$client = new DoiClient();
// $this->setExpectedException('Opus\Doi\ClientException');
$publish_id = 1;
$prefix = config('tethys.datacite_prefix');
$doiValue = $prefix . '/tethys.' . $publish_id;
$appUrl = config('app.url');
$landingPageUrl = $appUrl . "/dataset/" . $publish_id;
$result = $client->checkDoi(
$doiValue,
$landingPageUrl
);
$this->assertTrue($result);
// $result = $client->checkDoi(
// '10.5072/tethys-999',
// 'http://localhost/opus4/frontdoor/index/index/111'
$result = $client->checkDoi(
'10.5072/tethys-999',
'http://localhost/opus4/frontdoor/index/index/111'
);
$this->assertFalse($result);
}
// public function testRegisterDoiWithDataCiteTestAccount()
// {
// // $this->markTestSkipped(
// // 'Test kann nur manuell gestartet werden (Zugangsdaten zum MDS-Testservice von DataCite erforderlich)'
// // );
// $myRequest = new Request();
// $myRequest->setMethod('POST');
// $myRequest->request->add(
// [
// 'publish_id' => 1,
// // 'path' => 'https://www.demo.laudatio-repository.org/foo'
// ]
// );
// $this->assertFalse($result);
// $doiController = new \App\Http\Controllers\DoiController(new DoiClient());
// $doiController->store($myRequest);
// // $client = new DoiClient();
// // $client->registerDoi(
// // '10.5072/tethys-999',
// // xmlMeta,
// // 'http://localhost/opus4/frontdoor/index/index/999'
// // );
// }
public function testRegisterDoiWithDataCiteTestAccount()
public function testGetMetadataForDoi()
{
// $this->markTestSkipped(
// 'Test kann nur manuell gestartet werden (Zugangsdaten zum MDS-Testservice von DataCite erforderlich)'
// );
$myRequest = new Request();
$myRequest->setMethod('POST');
$myRequest->request->add(
[
'publish_id' => 1,
'path' => 'https://www.demo.laudatio-repository.org/foo'
]
$client = new DoiClient();
// $this->setExpectedException('Opus\Doi\ClientException');
$publish_id = 1;
$prefix = config('tethys.datacite_prefix');
$doiValue = $prefix . '/tethys.' . $publish_id;
$response = $client->getMetadataForDoi(
$doiValue
);
$doiController = new \App\Http\Controllers\DoiController(new DoiClient());
$doiController->store($myRequest);
// $client = new DoiClient();
// $client->registerDoi(
// '10.5072/tethys-999',
// xmlMeta,
// 'http://localhost/opus4/frontdoor/index/index/999'
// );
$this->assertEquals(200, $response->getStatusCode());
$testXml = new \SimpleXMLElement($response->getBody()->getContents());
Log::alert($testXml->saveXML());
}
}