Skip to content

Commit

Permalink
Merge branch 'addOpenAlex-715' into 'main'
Browse files Browse the repository at this point in the history
Obtém número de citações do OpenAlex

See merge request softwares-pkp/plugins_ojs/submissionsCitationsReport!3
  • Loading branch information
JhonathanLepidus committed Nov 7, 2024
2 parents f0a1fe4 + d367c09 commit 991a978
Show file tree
Hide file tree
Showing 13 changed files with 223 additions and 11 deletions.
3 changes: 2 additions & 1 deletion classes/SubmissionRowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function buildRow($context, $submissionWithCitations): array
$doi,
$isScieloJournal,
$submissionWithCitations->getCrossrefCitationsCount(),
$submissionWithCitations->getEuropePmcCitationsCount()
$submissionWithCitations->getEuropePmcCitationsCount(),
$submissionWithCitations->getOpenAlexCitationsCount()
];
}

Expand Down
10 changes: 10 additions & 0 deletions classes/SubmissionWithCitations.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ public function getEuropePmcCitationsCount(): int
return $this->getData('europePmcCitationsCount');
}

public function setOpenAlexCitationsCount(int $openAlexCitationsCount)
{
$this->setData('openAlexCitationsCount', $openAlexCitationsCount);
}

public function getOpenAlexCitationsCount(): int
{
return $this->getData('openAlexCitationsCount');
}

public static function __set_state($dump)
{
$obj = new SubmissionWithCitations();
Expand Down
3 changes: 2 additions & 1 deletion classes/SubmissionsCitationsReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ private function getHeaders(): array
__('metadata.property.displayName.doi'),
__('plugins.reports.submissionsCitationsReport.scieloJournal'),
__('plugins.reports.submissionsCitationsReport.crossrefCitationsCount'),
__('plugins.reports.submissionsCitationsReport.europePmcCitationsCount')
__('plugins.reports.submissionsCitationsReport.europePmcCitationsCount'),
__('plugins.reports.submissionsCitationsReport.openAlexCitationsCount')
];
}

Expand Down
12 changes: 11 additions & 1 deletion classes/SubmissionsCitationsReportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use APP\submission\Submission;
use APP\plugins\reports\submissionsCitationsReport\classes\clients\CrossrefClient;
use APP\plugins\reports\submissionsCitationsReport\classes\clients\EuropePmcClient;
use APP\plugins\reports\submissionsCitationsReport\classes\clients\OpenAlexClient;
use APP\plugins\reports\submissionsCitationsReport\classes\SubmissionsCitationsReport;
use APP\plugins\reports\submissionsCitationsReport\classes\SubmissionWithCitations;

Expand Down Expand Up @@ -63,16 +64,25 @@ public function retrieveSubmissionsWithCitations(int $contextId): array
$europePmcClient = new EuropePmcClient();
$submissionsEuropePmcCitationsCount = $europePmcClient->getSubmissionsCitationsCount($submissions);

$openAlexClient = new OpenAlexClient();
$submissionsOpenAlexCitationsCount = $openAlexClient->getSubmissionsCitationsCount($submissions);

$submissionsWithCitations = [];
foreach ($submissions as $submission) {
$crossrefCitationsCount = $submissionsCrossrefCitationsCount[$submission->getId()];
$europePmcCitationsCount = $submissionsEuropePmcCitationsCount[$submission->getId()];
$openAlexCitationsCount = $submissionsOpenAlexCitationsCount[$submission->getId()];

if ($crossrefCitationsCount > 0 || $europePmcCitationsCount > 0) {
if (
$crossrefCitationsCount > 0
|| $europePmcCitationsCount > 0
|| $openAlexCitationsCount > 0
) {
$submissionWithCitations = new SubmissionWithCitations();
$submissionWithCitations->setSubmissionId($submission->getId());
$submissionWithCitations->setCrossrefCitationsCount($crossrefCitationsCount);
$submissionWithCitations->setEuropePmcCitationsCount($europePmcCitationsCount);
$submissionWithCitations->setOpenAlexCitationsCount($openAlexCitationsCount);

$submissionsWithCitations[$submission->getId()] = $submissionWithCitations;
}
Expand Down
73 changes: 73 additions & 0 deletions classes/clients/OpenAlexClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace APP\plugins\reports\submissionsCitationsReport\classes\clients;

use APP\core\Application;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Pool;
use GuzzleHttp\Exception\ClientException;

class OpenAlexClient
{
private $guzzleClient;

public const OPEN_ALEX_API_URL = 'https://api.openalex.org/works';

public function __construct($guzzleClient = null)
{
if (!is_null($guzzleClient)) {
$this->guzzleClient = $guzzleClient;
} else {
$this->guzzleClient = Application::get()->getHttpClient();
}
}

public function getSubmissionsCitationsCount(array $submissions): array
{
$requests = [];
$citationsCount = [];

foreach ($submissions as $submission) {
$publication = $submission->getCurrentPublication();
$doiObject = $publication->getData('doiObject');

if (empty($doiObject->getDoi())) {
$citationsCount[$submission->getId()] = 0;
continue;
}

$doiResolvingUrl = $doiObject->getResolvingUrl();
$requestUrl = htmlspecialchars(self::OPEN_ALEX_API_URL . "/$doiResolvingUrl?select=doi,cited_by_count");
$requests[$submission->getId()] = new Request(
'GET',
$requestUrl,
[
'headers' => ['Accept' => 'application/json'],
]
);
}

$pool = new Pool($this->guzzleClient, $requests, [
'concurrency' => 5,
'fulfilled' => function ($response, $index) use (&$citationsCount) {
$responseJson = json_decode($response->getBody(), true);
$recordDoi = $responseJson['doi'];

if (empty($recordDoi)) {
$citationsCount[$index] = 0;
return;
}

$citationsCount[$index] = (int) $responseJson['cited_by_count'];
},
'rejected' => function ($reason, $index) use (&$citationsCount) {
$citationsCount[$index] = 0;
},
]);

$promise = $pool->promise();
$promise->wait();

return $citationsCount;
}
}
3 changes: 3 additions & 0 deletions locale/en/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ msgstr "CrossRef citations count"

msgid "plugins.reports.submissionsCitationsReport.europePmcCitationsCount"
msgstr "Europe PMC citations count"

msgid "plugins.reports.submissionsCitationsReport.openAlexCitationsCount"
msgstr "OpenAlex citations count"
5 changes: 4 additions & 1 deletion locale/es/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ msgid "plugins.reports.submissionsCitationsReport.crossrefCitationsCount"
msgstr "Número de citas en CrossRef"

msgid "plugins.reports.submissionsCitationsReport.europePmcCitationsCount"
msgstr "Número de citas en Europe PMC"
msgstr "Número de citas en Europe PMC"

msgid "plugins.reports.submissionsCitationsReport.openAlexCitationsCount"
msgstr "Número de citas en OpenAlex"
3 changes: 3 additions & 0 deletions locale/pt_BR/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ msgstr "Número de citações em CrossRef"

msgid "plugins.reports.submissionsCitationsReport.europePmcCitationsCount"
msgstr "Número de citações em Europe PMC"

msgid "plugins.reports.submissionsCitationsReport.openAlexCitationsCount"
msgstr "Número de citações em OpenAlex"
7 changes: 5 additions & 2 deletions tests/SubmissionRowBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private function createSubmissionWithCitations(): SubmissionWithCitations
$submissionWithCitations->setSubmission($this->submission);
$submissionWithCitations->setCrossrefCitationsCount(51);
$submissionWithCitations->setEuropePmcCitationsCount(16);
$submissionWithCitations->setOpenAlexCitationsCount(23);

return $submissionWithCitations;
}
Expand Down Expand Up @@ -167,7 +168,8 @@ public function testBuildSubmissionRow(): void
'10.666/949494',
__('common.no'),
51,
16
16,
23
];

$this->assertEquals($expectedRow, $rowBuilder->buildRow($context, $this->submissionWithCitations));
Expand All @@ -194,7 +196,8 @@ public function testBuildSubmissionRowWithScieloJournal(): void
'10.666/949494',
__('common.yes'),
51,
16
16,
23
];

$this->assertEquals($expectedRow, $rowBuilder->buildRow($context, $this->submissionWithCitations));
Expand Down
4 changes: 3 additions & 1 deletion tests/SubmissionWithCitationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ private function createSubmissionWithCitations()
$submissionWithCitations->setSubmissionId(7890);
$submissionWithCitations->setCrossrefCitationsCount(49);
$submissionWithCitations->setEuropePmcCitationsCount(14);
$submissionWithCitations->setOpenAlexCitationsCount(21);

return $submissionWithCitations;
}
Expand All @@ -30,7 +31,8 @@ public function testGetObjectData(): void
$expectedData = [
'submissionId' => 7890,
'crossrefCitationsCount' => 49,
'europePmcCitationsCount' => 14
'europePmcCitationsCount' => 14,
'openAlexCitationsCount' => 21
];
$this->assertEquals($expectedData, $this->submissionWithCitations->_data);
}
Expand Down
7 changes: 5 additions & 2 deletions tests/SubmissionsCitationsReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ private function createSubmissionWithCitations(): SubmissionWithCitations
$submissionWithCitations->setSubmission($this->submission);
$submissionWithCitations->setCrossrefCitationsCount(52);
$submissionWithCitations->setEuropePmcCitationsCount(17);
$submissionWithCitations->setOpenAlexCitationsCount(24);

return $submissionWithCitations;
}
Expand Down Expand Up @@ -128,7 +129,8 @@ public function testGeneratedCsvHasHeaders(): void
__('metadata.property.displayName.doi'),
__('plugins.reports.submissionsCitationsReport.scieloJournal'),
__('plugins.reports.submissionsCitationsReport.crossrefCitationsCount'),
__('plugins.reports.submissionsCitationsReport.europePmcCitationsCount')
__('plugins.reports.submissionsCitationsReport.europePmcCitationsCount'),
__('plugins.reports.submissionsCitationsReport.openAlexCitationsCount')
];

$this->assertEquals($expectedRow, $firstRow);
Expand Down Expand Up @@ -156,7 +158,8 @@ public function testGeneratedCsvHasSubmissionRow(): void
'10.666/949494',
__('common.no'),
52,
17
17,
24
];

$this->assertEquals($expectedRow, $secondRow);
Expand Down
100 changes: 100 additions & 0 deletions tests/clients/OpenAlexClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace APP\plugins\reports\submissionsCitationsReport\tests;

use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use APP\submission\Submission;
use APP\publication\Publication;
use APP\facades\Repo;
use APP\plugins\reports\submissionsCitationsReport\classes\clients\OpenAlexClient;

class OpenAlexClientTest extends TestCase
{
private $mockGuzzleClient;
private $crossrefClient;
private $contextId = 1;
private $mapDoiCitationsCount = [
'10.666/949494' => 42,
null => 0,
'10.987/131415' => 31
];
private $submissions;

public function setUp(): void
{
$this->submissions = $this->createTestSubmissions();
$this->mockGuzzleClient = $this->createMockGuzzleClient();
$this->crossrefClient = new OpenAlexClient($this->mockGuzzleClient);
}

private function createMockGuzzleClient()
{
$mockResponses = [];

foreach ($this->mapDoiCitationsCount as $doi => $citationsCount) {
if (empty($doi)) {
continue;
}

$responseBody = [
'id' => 'https://openalex.org/asdjakdfja',
'doi' => "https://doi.org/$doi",
'cited_by_count' => $citationsCount
];
$mockResponses[] = new Response(200, [], json_encode($responseBody));
}

$mockHandler = new MockHandler($mockResponses);
$guzzleClient = new Client(['handler' => $mockHandler]);

return $guzzleClient;
}

private function createTestSubmissions(): array
{
$submissions = [];
$submissionId = 10;

foreach ($this->mapDoiCitationsCount as $doi => $citationsCount) {
$submissions[] = $this->createSubmission($submissionId++, $doi);
}

return $submissions;
}

private function createSubmission(int $submissionId, ?string $doi): Submission
{
$submission = new Submission();
$submission->setData('id', $submissionId);
$publication = new Publication();
$publication->setData('id', $submissionId + 2);

if (!is_nulL($doi)) {
$doiObject = Repo::doi()->newDataObject([
'doi' => $doi,
'contextId' => $this->contextId
]);

$publication->setData('doiObject', $doiObject);
}

$submission->setData('currentPublicationId', $publication->getId());
$submission->setData('publications', [$publication]);
return $submission;
}

public function testGetSubmissionCitationsCount()
{
$submissionsCitationsCount = $this->crossrefClient->getSubmissionsCitationsCount($this->submissions);

foreach ($this->submissions as $submission) {
$doi = $submission->getCurrentPublication()->getDoi();
$submissionId = $submission->getId();

$this->assertEquals($this->mapDoiCitationsCount[$doi], $submissionsCitationsCount[$submissionId]);
}
}
}
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
<version>
<application>submissionsCitationsReport</application>
<type>plugins.reports</type>
<release>1.1.0.0</release>
<date>2024-10-23</date>
<release>1.2.0.0</release>
<date>2024-11-07</date>
</version>

0 comments on commit 991a978

Please sign in to comment.