Skip to content

Commit

Permalink
Merge branch 'addEuropePmc-710' into 'main'
Browse files Browse the repository at this point in the history
Obtem número de citações da Europe PMC

See merge request softwares-pkp/plugins_ojs/submissionsCitationsReport!2
  • Loading branch information
JhonathanLepidus committed Oct 23, 2024
2 parents 1bab501 + 98b54f8 commit f0a1fe4
Show file tree
Hide file tree
Showing 16 changed files with 504 additions and 41 deletions.
8 changes: 6 additions & 2 deletions classes/SubmissionRowBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace APP\plugins\reports\submissionsCitationsReport\classes;

use APP\core\Application;
use APP\facades\Repo;
use APP\plugins\reports\submissionsCitationsReport\classes\CitationsReportDAO;

class SubmissionRowBuilder
{
public function buildRow($context, $submission): array
public function buildRow($context, $submissionWithCitations): array
{
$submission = $submissionWithCitations->getSubmission();
$publication = $submission->getCurrentPublication();

$submissionId = $submission->getId();
Expand All @@ -24,7 +26,9 @@ public function buildRow($context, $submission): array
$authors,
$url,
$doi,
$isScieloJournal
$isScieloJournal,
$submissionWithCitations->getCrossrefCitationsCount(),
$submissionWithCitations->getEuropePmcCitationsCount()
];
}

Expand Down
57 changes: 57 additions & 0 deletions classes/SubmissionWithCitations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace APP\plugins\reports\submissionsCitationsReport\classes;

use PKP\core\DataObject;
use APP\submission\Submission;

class SubmissionWithCitations extends DataObject
{
public function setSubmissionId(int $submissionId)
{
$this->setData('submissionId', $submissionId);
}

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

public function setSubmission(Submission $submission)
{
$this->setData('submission', $submission);
}

public function getSubmission(): Submission
{
return $this->getData('submission');
}

public function setCrossrefCitationsCount(int $crossrefCitationsCount)
{
$this->setData('crossrefCitationsCount', $crossrefCitationsCount);
}

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

public function setEuropePmcCitationsCount(int $europePmcCitationsCount)
{
$this->setData('europePmcCitationsCount', $europePmcCitationsCount);
}

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

public static function __set_state($dump)
{
$obj = new SubmissionWithCitations();
$obj->setAllData($dump['_data']);

return $obj;
}
}
18 changes: 10 additions & 8 deletions classes/SubmissionsCitationsReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
class SubmissionsCitationsReport
{
private $contextId;
private $submissions;
private $submissionsWithCitations;
private $UTF8_BOM;

public function __construct(int $contextId, array $submissions)
public function __construct(int $contextId, array $submissionsWithCitations)
{
$this->contextId = $contextId;
$this->submissions = $submissions;
$this->submissionsWithCitations = $submissionsWithCitations;
$this->UTF8_BOM = chr(0xEF) . chr(0xBB) . chr(0xBF);
}

public function getSubmissions(): array
public function getSubmissionsWithCitations(): array
{
return $this->submissions;
return $this->submissionsWithCitations;
}

private function getHeaders(): array
Expand All @@ -31,7 +31,9 @@ private function getHeaders(): array
__('submission.authors'),
__('common.url'),
__('metadata.property.displayName.doi'),
__('plugins.reports.submissionsCitationsReport.scieloJournal')
__('plugins.reports.submissionsCitationsReport.scieloJournal'),
__('plugins.reports.submissionsCitationsReport.crossrefCitationsCount'),
__('plugins.reports.submissionsCitationsReport.europePmcCitationsCount')
];
}

Expand All @@ -43,8 +45,8 @@ public function buildCSV($fileDescriptor): void
fprintf($fileDescriptor, $this->UTF8_BOM);
fputcsv($fileDescriptor, $this->getHeaders());

foreach ($this->submissions as $submission) {
fputcsv($fileDescriptor, $submissionRowBuilder->buildRow($context, $submission));
foreach ($this->submissionsWithCitations as $submissionWithCitations) {
fputcsv($fileDescriptor, $submissionRowBuilder->buildRow($context, $submissionWithCitations));
}
}
}
38 changes: 25 additions & 13 deletions classes/SubmissionsCitationsReportBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use PKP\cache\CacheManager;
use APP\facades\Repo;
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\SubmissionsCitationsReport;
use APP\plugins\reports\submissionsCitationsReport\classes\CrossrefClient;
use APP\plugins\reports\submissionsCitationsReport\classes\SubmissionWithCitations;

class SubmissionsCitationsReportBuilder
{
Expand All @@ -27,20 +29,19 @@ private function getSubmissionsWithCitations(int $contextId): array
[$this, 'cacheDismiss']
);

$submissionsIds = $cache->getContents();
if (is_null($submissionsIds)) {
$submissionsWithCitations = $cache->getContents();
if (is_null($submissionsWithCitations)) {
$cache->flush();

$submissionsWithCitations = $this->retrieveSubmissionsWithCitations($contextId);
$submissionsIds = array_keys($submissionsWithCitations);
$cache->setEntireCache($submissionsIds);

return $submissionsWithCitations;
$cache->setEntireCache($submissionsWithCitations);
}

$submissionsWithCitations = [];
foreach ($submissionsIds as $submissionId) {
$submissionsWithCitations[] = Repo::submission()->get($submissionId);
foreach ($submissionsWithCitations as $submissionId => $submissionWithCitations) {
$submission = Repo::submission()->get($submissionId);
$submissionWithCitations->setSubmission($submission);

$submissionsWithCitations[$submissionId] = $submissionWithCitations;
}

return $submissionsWithCitations;
Expand All @@ -57,12 +58,23 @@ public function retrieveSubmissionsWithCitations(int $contextId): array
->toArray();

$crossrefClient = new CrossrefClient();
$submissionsCitationsCount = $crossrefClient->getSubmissionsCitationsCount($submissions);
$submissionsCrossrefCitationsCount = $crossrefClient->getSubmissionsCitationsCount($submissions);

$europePmcClient = new EuropePmcClient();
$submissionsEuropePmcCitationsCount = $europePmcClient->getSubmissionsCitationsCount($submissions);

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

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

$submissionsWithCitations[$submission->getId()] = $submissionWithCitations;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

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

use APP\core\Application;
use GuzzleHttp\Psr7\Request;
Expand Down Expand Up @@ -31,7 +31,7 @@ public function getSubmissionsCitationsCount(array $submissions): array
$publication = $submission->getCurrentPublication();
$doi = $publication->getDoi();

if (is_null($doi)) {
if (empty($doi)) {
$citationsCount[$submission->getId()] = 0;
continue;
}
Expand Down
132 changes: 132 additions & 0 deletions classes/clients/EuropePmcClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

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

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

class EuropePmcClient
{
private $guzzleClient;

public const EUROPE_PMC_API_URL = 'https://www.ebi.ac.uk/europepmc/webservices/rest';

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

public function getSubmissionsCitationsCount(array $submissions): array
{
$idsAndSources = $this->getSubmissionsIdAndSource($submissions);
$submissionsCitationsCount = $this->getCitationsCountByIdAndSource($idsAndSources);

return $submissionsCitationsCount;
}

public function getSubmissionsIdAndSource(array $submissions): array
{
$requests = [];
$idsAndSources = [];

foreach ($submissions as $submission) {
$publication = $submission->getCurrentPublication();
$doi = $publication->getDoi();

if (empty($doi)) {
$idsAndSources[$submission->getId()] = [];
continue;
}

$requestUrl = htmlspecialchars(self::EUROPE_PMC_API_URL . "/search?query=DOI:$doi") . '&format=json';
$requests[$submission->getId()] = new Request(
'GET',
$requestUrl
);
}

$pool = new Pool($this->guzzleClient, $requests, [
'concurrency' => 5,
'fulfilled' => function ($response, $index) use (&$idsAndSources, $submissions) {
$responseBody = json_decode($response->getBody(), true);
$idAndSource = [];

if (isset($responseBody['resultList'])) {
$results = $responseBody['resultList']['result'];
$submission = $submissions[$index];
$submissionDoi = strtolower($submission->getCurrentPublication()->getDoi());

foreach ($results as $result) {
if (strtolower($result['doi']) == $submissionDoi) {
$idAndSource = [
'id' => $result['id'],
'source' => $result['source']
];
break;
}
}
}

$idsAndSources[$index] = $idAndSource;
},
'rejected' => function ($reason, $index) use (&$idsAndSources) {
$idsAndSources[$index] = [];
},
]);

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

return $idsAndSources;
}

public function getCitationsCountByIdAndSource(array $submissionsIdsAndSources): array
{
$requests = [];
$submissionsCitationsCount = [];

foreach ($submissionsIdsAndSources as $submissionId => $idAndSource) {
if (empty($idAndSource)) {
$submissionsCitationsCount[$submissionId] = 0;
continue;
}

$id = $idAndSource['id'];
$source = $idAndSource['source'];

$requestUrl = htmlspecialchars(self::EUROPE_PMC_API_URL . "/$source/$id/citations?format=json");
$requests[$submissionId] = new Request(
'GET',
$requestUrl
);
}

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

if (isset($responseJson['hitCount'])) {
$citationsCount = $responseJson['hitCount'];
}

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

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

return $submissionsCitationsCount;
}
}
7 changes: 3 additions & 4 deletions classes/tasks/UpdateCitationsCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ public function executeActions()
);

$cache->flush();
$reporBuilder = new SubmissionsCitationsReportBuilder();
$submissionsWithCitations = $reporBuilder->retrieveSubmissionsWithCitations($contextId);
$submissionsIds = array_keys($submissionsWithCitations);
$cache->setEntireCache($submissionsIds);
$reportBuilder = new SubmissionsCitationsReportBuilder();
$submissionsWithCitations = $reportBuilder->retrieveSubmissionsWithCitations($contextId);
$cache->setEntireCache($submissionsWithCitations);
}

return true;
Expand Down
6 changes: 6 additions & 0 deletions locale/en/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ msgstr "Implements a report containing all the submissions which have citations

msgid "plugins.reports.submissionsCitationsReport.scieloJournal"
msgstr "SciELO Journal"

msgid "plugins.reports.submissionsCitationsReport.crossrefCitationsCount"
msgstr "CrossRef citations count"

msgid "plugins.reports.submissionsCitationsReport.europePmcCitationsCount"
msgstr "Europe PMC citations count"
6 changes: 6 additions & 0 deletions locale/es/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ msgstr "Implementa un informe que contiene todas las presentaciones que tienen c

msgid "plugins.reports.submissionsCitationsReport.scieloJournal"
msgstr "Revista SciELO"

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"
8 changes: 7 additions & 1 deletion locale/pt_BR/locale.po
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ msgid "plugins.reports.submissionsCitationsReport.description"
msgstr "Implementa um relatório contendo todos as submissões que têm citações listadas publicamente"

msgid "plugins.reports.submissionsCitationsReport.scieloJournal"
msgstr "Periódico SciELO"
msgstr "Periódico SciELO"

msgid "plugins.reports.submissionsCitationsReport.crossrefCitationsCount"
msgstr "Número de citações em CrossRef"

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

0 comments on commit f0a1fe4

Please sign in to comment.