Skip to content

Commit

Permalink
Merge pull request #141 from bozana/6887-3_3_1
Browse files Browse the repository at this point in the history
pkp/pkp-lib#6887 fix crossref errors handling
  • Loading branch information
bozana committed Apr 9, 2021
2 parents 5ea6231 + cbe2805 commit c10987c
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions plugins/importexport/crossref/CrossRefExportPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//TESTING
define('CROSSREF_API_URL_DEV', 'https://test.crossref.org/v2/deposits');

define('CROSSREF_API_STATUS_URL', 'https://api.crossref.org/servlet/submissionDownload');
define('CROSSREF_API_STATUS_URL', 'https://doi.crossref.org/servlet/submissionDownload');
//TESTING
define('CROSSREF_API_STATUS_URL_DEV', 'https://test.crossref.org/servlet/submissionDownload');

Expand Down Expand Up @@ -118,21 +118,25 @@ function getStatusMessage($request) {
$context = $request->getContext();

$httpClient = Application::get()->getHttpClient();
$response = $httpClient->request(
'POST',
$this->isTestMode($context) ? CROSSREF_API_STATUS_URL_DEV : CROSSREF_API_STATUS_URL,
[
'form_params' => [
'doi_batch_id' => $request->getUserVar('batchId'),
'type' => 'result',
'usr' => $this->getSetting($context->getId(), 'username'),
'pwd' => $this->getSetting($context->getId(), 'password'),
try {
$response = $httpClient->request(
'POST',
$this->isTestMode($context) ? CROSSREF_API_STATUS_URL_DEV : CROSSREF_API_STATUS_URL,
[
'form_params' => [
'doi_batch_id' => $request->getUserVar('batchId'),
'type' => 'result',
'usr' => $this->getSetting($context->getId(), 'username'),
'pwd' => $this->getSetting($context->getId(), 'password'),
]
]
]
);

if ($response->getStatusCode() != 200) {
return __('plugins.importexport.common.register.error.mdsError', array('param' => 'No response from server.'));
);
} catch (GuzzleHttp\Exception\RequestException $e) {
$returnMessage = $e->getMessage();
if ($e->hasResponse()) {
$returnMessage = $e->getResponse()->getBody(true) . ' (' .$e->getResponse()->getStatusCode() . ' ' . $e->getResponse()->getReasonPhrase() . ')';
}
return __('plugins.importexport.common.register.error.mdsError', array('param' => $returnMessage));
}

return (string) $response->getBody();
Expand Down Expand Up @@ -337,7 +341,7 @@ function depositOnPublish($hookName, $args) {
*/
function depositXML($objects, $context, $filename) {
$status = null;
$msg = null;
$msgSave = null;

$httpClient = Application::get()->getHttpClient();
assert(is_readable($filename));
Expand Down Expand Up @@ -368,16 +372,29 @@ function depositXML($objects, $context, $filename) {
]
);
} catch (GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() != CROSSREF_API_DEPOSIT_ERROR_FROM_CROSSREF) {
return [['plugins.importexport.common.register.error.mdsError', 'No response from server.']];
$returnMessage = $e->getMessage();
if ($e->hasResponse()) {
$eResponseBody = $e->getResponse()->getBody(true);
$eStatusCode = $e->getResponse()->getStatusCode();
if ($eStatusCode == CROSSREF_API_DEPOSIT_ERROR_FROM_CROSSREF) {
$xmlDoc = new DOMDocument();
$xmlDoc->loadXML($eResponseBody);
$batchIdNode = $xmlDoc->getElementsByTagName('batch_id')->item(0);
$msg = $xmlDoc->getElementsByTagName('msg')->item(0)->nodeValue;
$msgSave = $msg . PHP_EOL . $eResponseBody;
$status = CROSSREF_STATUS_FAILED;
$this->updateDepositStatus($context, $objects, $status, $batchIdNode->nodeValue, $msgSave);
$this->updateObject($objects);
$returnMessage = $msg . ' (' .$eStatusCode . ' ' . $e->getResponse()->getReasonPhrase() . ')';
} else {
$returnMessage = $eResponseBody . ' (' .$eStatusCode . ' ' . $e->getResponse()->getReasonPhrase() . ')';
}
}

$response = $e->getResponse();
return [['plugins.importexport.common.register.error.mdsError', $returnMessage]];
}

// Get DOMDocument from the response XML string
$xmlDoc = new DOMDocument();
$resp = $response->getBody();
$xmlDoc->loadXML($response->getBody());
$batchIdNode = $xmlDoc->getElementsByTagName('batch_id')->item(0);

Expand Down Expand Up @@ -405,7 +422,7 @@ function depositXML($objects, $context, $filename) {

// Update the status
if ($status) {
$this->updateDepositStatus($context, $objects, $status, $batchIdNode->nodeValue, $msg);
$this->updateDepositStatus($context, $objects, $status, $batchIdNode->nodeValue, $msgSave);
$this->updateObject($objects);
}

Expand Down

0 comments on commit c10987c

Please sign in to comment.