-
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 'addOpenAlex-715' into 'main'
Obtém número de citações do OpenAlex See merge request softwares-pkp/plugins_ojs/submissionsCitationsReport!3
- Loading branch information
Showing
13 changed files
with
223 additions
and
11 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
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
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,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; | ||
} | ||
} |
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
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
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
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,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]); | ||
} | ||
} | ||
} |
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