-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'citationField-724' into 'main'
Obtém citação do documento original a partir do DOI See merge request softwares-pkp/plugins_ojs/scieloTranslationsFields!2
- Loading branch information
Showing
15 changed files
with
464 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
api/v1/translationsFields/TranslationsFieldsHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace APP\plugins\generic\scieloTranslationsFields\api\v1\translationsFields; | ||
|
||
use PKP\handler\APIHandler; | ||
use PKP\security\Role; | ||
use PKP\security\authorization\PolicySet; | ||
use PKP\security\authorization\RoleBasedHandlerOperationPolicy; | ||
use PKP\db\DAORegistry; | ||
use APP\facades\Repo; | ||
use APP\plugins\generic\scieloTranslationsFields\classes\clients\DoiClient; | ||
use APP\plugins\generic\scieloTranslationsFields\classes\DoiValidator; | ||
|
||
class TranslationsFieldsHandler extends APIHandler | ||
{ | ||
public function __construct() | ||
{ | ||
$this->_handlerPath = 'translationsFields'; | ||
$roles = [Role::ROLE_ID_MANAGER, Role::ROLE_ID_SUB_EDITOR, Role::ROLE_ID_AUTHOR]; | ||
$this->_endpoints = [ | ||
'PUT' => [ | ||
[ | ||
'pattern' => $this->getEndpointPattern() . '/saveTranslationFields', | ||
'handler' => [$this, 'saveTranslationFields'], | ||
'roles' => $roles | ||
], | ||
], | ||
]; | ||
parent::__construct(); | ||
} | ||
|
||
public function authorize($request, &$args, $roleAssignments) | ||
{ | ||
$rolePolicy = new PolicySet(PolicySet::COMBINING_PERMIT_OVERRIDES); | ||
|
||
foreach ($roleAssignments as $role => $operations) { | ||
$rolePolicy->addPolicy(new RoleBasedHandlerOperationPolicy($request, $role, $operations)); | ||
} | ||
$this->addPolicy($rolePolicy); | ||
|
||
return parent::authorize($request, $args, $roleAssignments); | ||
} | ||
|
||
private function getSubmission($slimRequest) | ||
{ | ||
$queryParams = $slimRequest->getQueryParams(); | ||
$submissionId = (int) $queryParams['submissionId']; | ||
|
||
return Repo::submission()->get($submissionId); | ||
} | ||
|
||
private function validateOriginalDoi($originalDocumentDoi) | ||
{ | ||
$doiValidator = new DoiValidator(); | ||
return $doiValidator->validate($originalDocumentDoi); | ||
} | ||
|
||
public function saveTranslationFields($slimRequest, $response, $args) | ||
{ | ||
$requestParams = $slimRequest->getParsedBody(); | ||
$placedOn = $slimRequest->getQueryParams()['placedOn']; | ||
$submission = $this->getSubmission($slimRequest); | ||
$publication = $submission->getCurrentPublication(); | ||
|
||
$originalDocumentHasDoi = $requestParams['originalDocumentHasDoi']; | ||
$originalDocumentDoi = $requestParams['originalDocumentDoi']; | ||
$originalDocumentCitation = null; | ||
|
||
if ($originalDocumentHasDoi) { | ||
if ($this->validateOriginalDoi($originalDocumentDoi)) { | ||
$doiClient = new DoiClient(); | ||
$originalDocumentCitation = $doiClient->getApaCitation($originalDocumentDoi); | ||
} elseif ($placedOn == 'workflow') { | ||
return $response->withStatus(400)->withJson([ | ||
'originalDocumentDoi' => [__('plugins.generic.scieloTranslationsFields.originalDocumentDoi.invalidDoi')] | ||
]); | ||
} | ||
} | ||
|
||
Repo::publication()->edit($publication, [ | ||
'originalDocumentHasDoi' => $originalDocumentHasDoi, | ||
'originalDocumentDoi' => $originalDocumentDoi, | ||
'originalDocumentCitation' => $originalDocumentCitation | ||
]); | ||
|
||
$submission = Repo::submission()->get($submission->getId()); | ||
$publication = $submission->getCurrentPublication(); | ||
|
||
$contextId = $submission->getData('contextId'); | ||
$userGroups = Repo::userGroup()->getCollector() | ||
->filterByContextIds([$contextId]) | ||
->getMany(); | ||
|
||
$genreDao = DAORegistry::getDAO('GenreDAO'); | ||
$genres = $genreDao->getByContextId($contextId)->toArray(); | ||
|
||
return $response->withJson( | ||
Repo::publication()->getSchemaMap($submission, $userGroups, $genres)->map($publication), | ||
200 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace APP\plugins\generic\scieloTranslationsFields\classes; | ||
|
||
class DoiValidator | ||
{ | ||
public function validate(string $doi): bool | ||
{ | ||
return (preg_match('/^10\.\d{4,9}\/[-._;()\/:A-Z0-9]+$/i', $doi) === 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace APP\plugins\generic\scieloTranslationsFields\classes\clients; | ||
|
||
use APP\core\Application; | ||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\Exception\ClientException; | ||
|
||
class DoiClient | ||
{ | ||
private $guzzleClient; | ||
|
||
public const DOI_URL = 'https://doi.org/'; | ||
|
||
public function __construct($guzzleClient = null) | ||
{ | ||
if (!is_null($guzzleClient)) { | ||
$this->guzzleClient = $guzzleClient; | ||
} else { | ||
$this->guzzleClient = Application::get()->getHttpClient(); | ||
} | ||
} | ||
|
||
public function getApaCitation(string $doi): ?string | ||
{ | ||
$doiUrl = self::DOI_URL . $doi; | ||
|
||
try { | ||
$response = $this->guzzleClient->request('GET', $doiUrl, [ | ||
'headers' => [ | ||
'Accept' => 'text/x-bibliography; style=apa' | ||
], | ||
]); | ||
|
||
return $response->getBody()->getContents(); | ||
} catch (ClientException $e) { | ||
$errorMsg = $e->getResponse()->getBody()->getContents(); | ||
error_log("Error while getting DOI citation: $errorMsg"); | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.