Skip to content

Commit 95648dd

Browse files
Merge branch 'fixNewDraftVersion340-760' into 'main'
Corrige problema com nova versão em rascunho - 3.4.0 See merge request softwares-pkp/plugins_ojs/dataverse!189
2 parents 25aa89f + e8f8320 commit 95648dd

8 files changed

Lines changed: 404 additions & 218 deletions

File tree

api/v1/datasets/DatasetHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,13 @@ public function getCitation($slimRequest, $response, $args)
294294

295295
try {
296296
$dataverseClient = new DataverseClient();
297-
$citation = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId(), $datasetIsPublished);
297+
$citationData = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId(), $datasetIsPublished);
298298
} catch (DataverseException $e) {
299299
error_log('Error getting citation: ' . $e->getMessage());
300300
return $response->withStatus($e->getCode())->withJsonError('api.error.researchDataCitationNotFound');
301301
}
302302

303-
return $response->withJson(['citation' => $citation], 200);
303+
return $response->withJson(['citation' => $citationData['citation']], 200);
304304
}
305305

306306
public function deleteFile($slimRequest, $response, $args)

classes/dispatchers/DatasetInformationDispatcher.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ public function addDatasetInformation(string $hookName, array $params): bool
2828
$dataverseClient = new DataverseClient();
2929

3030
try {
31-
$citation = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId(), null);
32-
$templateMgr->assign('datasetInfo', $citation);
33-
$output .= $templateMgr->fetch($this->plugin->getTemplateResource('dataCitation.tpl'));
31+
$citationData = $dataverseClient->getDatasetActions()->getCitation($study->getPersistentId(), null);
32+
if ($citationData['datasetIsPublished']) {
33+
$templateMgr->assign('datasetInfo', $citationData['citation']);
34+
$output .= $templateMgr->fetch($this->plugin->getTemplateResource('dataCitation.tpl'));
35+
}
3436
} catch (DataverseException $e) {
3537
error_log('Error getting citation: ' . $e->getMessage());
3638
}

classes/factories/JsonDatasetFactory.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,26 @@ public function __construct(string $jsonContent)
1717
$this->jsonContent = $jsonContent;
1818
}
1919

20+
private function getCurrentDatasetVersion()
21+
{
22+
$datasetVersions = (json_decode($this->jsonContent))->data;
23+
$currentVersion = $datasetVersions[0];
24+
25+
if (count($datasetVersions) > 1) {
26+
foreach ($datasetVersions as $version) {
27+
if ($version->versionState == 'RELEASED') {
28+
$currentVersion = $version;
29+
break;
30+
}
31+
}
32+
}
33+
34+
return $currentVersion;
35+
}
36+
2037
protected function sanitizeProps(): array
2138
{
22-
$responseData = json_decode($this->jsonContent);
23-
$datasetVersion = $responseData->data->latestVersion;
39+
$datasetVersion = $this->getCurrentDatasetVersion();
2440
$datasetData = $datasetVersion->metadataBlocks->citation->fields;
2541

2642
$props = [];

dataverseAPI/actions/DatasetActions.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ class DatasetActions extends DataverseActions implements DatasetActionsInterface
1616
public function get(string $persistentId): Dataset
1717
{
1818
$args = '?persistentId=' . $persistentId;
19-
$uri = $this->createNativeAPIURI('datasets', ':persistentId' . $args);
19+
$uri = $this->createNativeAPIURI('datasets', ':persistentId', 'versions' . $args);
2020
$response = $this->nativeAPIRequest('GET', $uri);
2121

2222
$datasetFactory = new JsonDatasetFactory($response->getBody());
2323
return $datasetFactory->getDataset();
2424
}
2525

26-
public function getCitation(string $persistentId, ?bool $datasetIsPublished): string
26+
public function getCitation(string $persistentId, ?bool $datasetIsPublished): array
2727
{
2828
if (is_null($datasetIsPublished)) {
2929
$dataset = $this->get($persistentId);
@@ -43,21 +43,12 @@ public function getCitation(string $persistentId, ?bool $datasetIsPublished): st
4343
'<a href="' . $persistentUrl . '">' . $persistentUrl . '</a>',
4444
$citation
4545
);
46-
return preg_replace('/,+.UNF[^]]+]/', '', $citation);
46+
$citation = preg_replace('/,+.UNF[^]]+]/', '', $citation);
4747
} else {
48-
return $this->getSWORDCitation($persistentId);
48+
$citation = $this->getSWORDCitation($persistentId);
4949
}
50-
}
5150

52-
public function getNativeCitation(string $persistentId): string
53-
{
54-
$args = '?persistentId=' . $persistentId;
55-
$uri = $this->createNativeAPIURI('datasets', ':persistentId', 'versions', ':latest', 'citation' . $args);
56-
$response = $this->nativeAPIRequest('GET', $uri);
57-
58-
$jsonContent = json_decode($response->getBody(), true);
59-
$citation = $jsonContent['data']['message'];
60-
return preg_replace('/,+.UNF[^]]+]/', '', $citation);
51+
return ['datasetIsPublished' => $datasetIsPublished, 'citation' => $citation];
6152
}
6253

6354
private function getSWORDCitation(string $persistentId): string

tests/factories/JsonDatasetFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class JsonDatasetFactoryTest extends PKPTestCase
77
{
88
public function testCreateDataset()
99
{
10-
$jsonContent = file_get_contents(__DIR__ . '/../fixtures/datasetResponseExample.json');
10+
$jsonContent = file_get_contents(__DIR__ . '/../fixtures/datasetVersionsResponseExample.json');
1111
$datasetFactory = new JsonDatasetFactory($jsonContent);
1212
$dataset = $datasetFactory->getDataset();
1313

@@ -26,7 +26,7 @@ public function testCreateDataset()
2626
$this->assertEquals('Dataverse', $dataset->getContact()->getAffiliation());
2727
$this->assertEquals('Related publication', $dataset->getPubCitation());
2828
$this->assertEquals('Test, Depositor', $dataset->getDepositor());
29-
$this->assertEquals('DRAFT', $dataset->getVersionState());
29+
$this->assertEquals('RELEASED', $dataset->getVersionState());
3030

3131
$this->assertEquals(9876543, $dataset->getFiles()[0]->getId());
3232
$this->assertEquals('sample.pdf', $dataset->getFiles()[0]->getFileName());

tests/fixtures/datasetResponseExample.json

Lines changed: 0 additions & 193 deletions
This file was deleted.

0 commit comments

Comments
 (0)