Skip to content

Commit 0839b29

Browse files
committed
Merge branch 'validate_work_exists_3_3_0' into 'stable-3_3_0'
Validate Work exists See merge request softwares-pkp/plugins_ojs/thoth-omp-plugin!48
2 parents b5ba5f6 + 348dac2 commit 0839b29

13 files changed

Lines changed: 299 additions & 9 deletions

ThothPlugin.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* @brief Plugin for integration with Thoth for communication and synchronization of book data between the two platforms
1616
*/
1717

18+
require_once(__DIR__ . '/vendor/autoload.php');
19+
1820
import('lib.pkp.classes.plugins.GenericPlugin');
1921
import('plugins.generic.thoth.classes.ThothBadgeRender');
2022
import('plugins.generic.thoth.classes.ThothNotification');

classes/ThothValidator.inc.php

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
* @brief Validate submission metadata to Thoth submit
1414
*/
1515

16-
require_once(__DIR__ . '/../vendor/autoload.php');
17-
1816
use Biblys\Isbn\Isbn;
17+
use ThothApi\GraphQL\Models\Work as ThothWork;
1918

2019
import('plugins.generic.thoth.classes.facades.ThothService');
2120

@@ -25,6 +24,29 @@ public static function validate($submission)
2524
{
2625
$errors = [];
2726

27+
$publication = $submission->getCurrentPublication();
28+
$doi = $publication->getStoredPubId('doi');
29+
$doiUrl = ThothService::work()->getDoiResolvingUrl($doi);
30+
31+
if ($doiUrl !== null) {
32+
$errors = array_merge($errors, self::validateDoiExists($doiUrl));
33+
}
34+
35+
$request = Application::get()->getRequest();
36+
$context = $request->getContext();
37+
$dispatcher = $request->getDispatcher();
38+
39+
$landingPage = $dispatcher->url(
40+
$request,
41+
ROUTE_PAGE,
42+
$context->getPath(),
43+
'catalog',
44+
'book',
45+
$submission->getBestId()
46+
);
47+
48+
$errors = array_merge($errors, self::validateLandingPageExists($landingPage));
49+
2850
$publicationFormats = Application::getRepresentationDao()
2951
->getApprovedByPublicationId($submission->getData('currentPublicationId'))
3052
->toArray();
@@ -44,7 +66,10 @@ public static function validateIsbn($publicationFormats)
4466
foreach ($publicationFormats as $publicationFormat) {
4567
try {
4668
$isbn = ThothService::publication()->getIsbnByPublicationFormat($publicationFormat);
47-
Isbn::validateAsIsbn13($isbn);
69+
if ($isbn !== null) {
70+
Isbn::validateAsIsbn13($isbn);
71+
}
72+
$errors = array_merge($errors, self::validateIsbnExists($isbn));
4873
} catch (Exception $e) {
4974
$errors[] = __('plugins.generic.thoth.validation.isbn', [
5075
'isbn' => $isbn,
@@ -55,4 +80,46 @@ public static function validateIsbn($publicationFormats)
5580

5681
return $errors;
5782
}
83+
84+
public static function validateDoiExists($doi)
85+
{
86+
$errors = [];
87+
88+
try {
89+
$work = ThothService::work()->getByDoi($doi);
90+
if ($work instanceof ThothWork) {
91+
$errors[] = __('plugins.generic.thoth.validation.doiExists', ['doi' => $doi]);
92+
}
93+
} catch (Exception $e) {
94+
return $errors;
95+
}
96+
97+
return $errors;
98+
}
99+
100+
public static function validateLandingPageExists($landingPage)
101+
{
102+
$errors = [];
103+
104+
$works = ThothService::work()->search($landingPage);
105+
106+
if (!empty($works)) {
107+
$errors[] = __('plugins.generic.thoth.validation.landingPageExists', ['landingPage' => $landingPage]);
108+
}
109+
110+
return $errors;
111+
}
112+
113+
public static function validateIsbnExists($isbn)
114+
{
115+
$errors = [];
116+
117+
$publications = ThothService::publication()->search($isbn);
118+
119+
if (!empty($publications)) {
120+
$errors[] = __('plugins.generic.thoth.validation.isbnExists', ['isbn' => $isbn]);
121+
}
122+
123+
return $errors;
124+
}
58125
}

classes/container/ThothContainerProvider.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* @brief Utility class to package all plugin container bindings
1414
*/
1515

16+
require_once(__DIR__ . '/../../vendor/autoload.php');
17+
1618
use ThothApi\GraphQL\Client;
1719

1820
import('plugins.generic.thoth.classes.container.ContainerProvider');

classes/services/ThothPublicationService.inc.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ public function new($params)
3939
return $publication;
4040
}
4141

42+
public function search($filter)
43+
{
44+
$thothClient = ThothContainer::getInstance()->get('client');
45+
return $thothClient->publications(['filter' => $filter]);
46+
}
47+
4248
public function register($publicationFormat, $workId, $chapterId = null)
4349
{
4450
$thothPublication = $this->newByPublicationFormat($publicationFormat);

classes/services/ThothWorkService.inc.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
class ThothWorkService
2222
{
23-
private function getDoiResolvingUrl($doi)
23+
public function getDoiResolvingUrl($doi)
2424
{
2525
if (empty($doi)) {
2626
return $doi;
@@ -119,6 +119,18 @@ public function get($thothWorkId)
119119
return $thothClient->work($thothWorkId);
120120
}
121121

122+
public function search($filter)
123+
{
124+
$thothClient = ThothContainer::getInstance()->get('client');
125+
return $thothClient->works(['filter' => $filter]);
126+
}
127+
128+
public function getByDoi($doi)
129+
{
130+
$thothClient = ThothContainer::getInstance()->get('client');
131+
return $thothClient->workByDoi($doi);
132+
}
133+
122134
public function registerBook($submission, $thothImprintId)
123135
{
124136
$thothBook = $this->newBySubmission($submission);

locale/en_US/locale.po

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ msgstr "The submission metadata can not be submitted to Thoth because of the fol
5454
msgid "plugins.generic.thoth.validation.isbn"
5555
msgstr "\"{$isbn}\" of \"{$formatName}\" publication format is not a valid ISBN-13. A valid ISBN-13 must be exactly 17 characters (numbers and hyphens)."
5656

57+
msgid "plugins.generic.thoth.validation.doiExists"
58+
msgstr "A work with DOI \"{$doi}\" already exists."
59+
60+
msgid "plugins.generic.thoth.validation.landingPageExists"
61+
msgstr "A work with the landing page \"{$landingPage}\" already exists."
62+
63+
msgid "plugins.generic.thoth.validation.isbnExists"
64+
msgstr "A publication with the ISBN \"{$isbn}\" already exists."
65+
5766
msgid "plugins.generic.thoth.register.confirmation"
5867
msgstr "Do you want to register in Thoth the metadata related to this submission?"
5968

locale/es_ES/locale.po

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ msgstr "Los metadatos de envio no pueden ser enviados a Thoth debido a los sigui
5454
msgid "plugins.generic.thoth.validation.isbn"
5555
msgstr "\"{$isbn}\" del formato de publicación \"{$formatName}\" no es un ISBN-13 válido. Un ISBN-13 válido debe tener exactamente 17 caracteres (números y guiones)."
5656

57+
msgid "plugins.generic.thoth.validation.doiExists"
58+
msgstr "Un trabajo con el DOI \"{$doi}\" ya existe."
59+
60+
msgid "plugins.generic.thoth.validation.landingPageExists"
61+
msgstr "Un trabajo con la página de destino \"{$landingPage}\" ya existe."
62+
63+
msgid "plugins.generic.thoth.validation.isbnExists"
64+
msgstr "Una publicación con el ISBN \"{$isbn}\" ya existe."
65+
5766
msgid "plugins.generic.thoth.register.confirmation"
5867
msgstr "¿Desea registrar en Thoth los metadatos relacionados con este envío?"
5968

locale/pt_BR/locale.po

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ msgstr "Os metadados da submissão não podem ser enviados para Thoth devido aos
5454
msgid "plugins.generic.thoth.validation.isbn"
5555
msgstr "\"{$isbn}\" do formato de publicação \"{$formatName}\" não é um ISBN-13 válido. Um ISBN-13 válido deve ter exatamente 17 caracteres (números e hífens)."
5656

57+
msgid "plugins.generic.thoth.validation.doiExists"
58+
msgstr "Um trabalho com o DOI \"{$doi}\" já existe."
59+
60+
msgid "plugins.generic.thoth.validation.landingPageExists"
61+
msgstr "Um trabalho com a página de destino \"{$landingPage}\" já existe."
62+
63+
msgid "plugins.generic.thoth.validation.isbnExists"
64+
msgstr "Uma publicação com o ISBN \"{$isbn}\" já existe."
65+
5766
msgid "plugins.generic.thoth.register.confirmation"
5867
msgstr "Deseja realmente registrar na Thoth os metadados relacionados a essa submissão?"
5968

tests/classes/ThothValidatorTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* @brief Test class for the ThothValidator class
1515
*/
1616

17+
use ThothApi\GraphQL\Client as ThothClient;
18+
use ThothApi\GraphQL\Models\Publication as ThothPublication;
19+
use ThothApi\GraphQL\Models\Work as ThothWork;
20+
1721
import('lib.pkp.tests.PKPTestCase');
1822
import('plugins.generic.thoth.classes.ThothValidator');
1923
import('classes.publicationFormat.PublicationFormat');
@@ -73,4 +77,86 @@ public function testISBNValidationFails()
7377
'##plugins.generic.thoth.validation.isbn##'
7478
], $errors);
7579
}
80+
81+
public function testDOIExistsValidationFails()
82+
{
83+
$doi = 'https://doi.org/10.12345/12345678';
84+
85+
$mockThothClient = $this->getMockBuilder(ThothClient::class)
86+
->setMethods([
87+
'workByDoi',
88+
])
89+
->getMock();
90+
$mockThothClient->expects($this->any())
91+
->method('workByDoi')
92+
->will($this->returnValue(new ThothWork([
93+
'doi' => $doi
94+
])));
95+
96+
ThothContainer::getInstance()->set('client', function () use ($mockThothClient) {
97+
return $mockThothClient;
98+
});
99+
100+
$errors = ThothValidator::validateDoiExists($doi);
101+
102+
$this->assertEquals([
103+
'##plugins.generic.thoth.validation.doiExists##',
104+
], $errors);
105+
}
106+
107+
public function testLandingPageExistsValidationFails()
108+
{
109+
$landingPage = 'http://www.publicknowledge.omp/index.php/publicknowledge/catalog/book/14';
110+
111+
$mockThothClient = $this->getMockBuilder(ThothClient::class)
112+
->setMethods([
113+
'works',
114+
])
115+
->getMock();
116+
$mockThothClient->expects($this->any())
117+
->method('works')
118+
->will($this->returnValue([
119+
new ThothWork([
120+
'landingPage' => $landingPage
121+
])
122+
]));
123+
124+
ThothContainer::getInstance()->set('client', function () use ($mockThothClient) {
125+
return $mockThothClient;
126+
});
127+
128+
$errors = ThothValidator::validateLandingPageExists($landingPage);
129+
130+
$this->assertEquals([
131+
'##plugins.generic.thoth.validation.landingPageExists##',
132+
], $errors);
133+
}
134+
135+
public function testISBNExistsValidationFails()
136+
{
137+
$isbn = '978-65-89999-01-3';
138+
139+
$mockThothClient = $this->getMockBuilder(ThothClient::class)
140+
->setMethods([
141+
'publications',
142+
])
143+
->getMock();
144+
$mockThothClient->expects($this->any())
145+
->method('publications')
146+
->will($this->returnValue([
147+
new ThothPublication([
148+
'isbn' => $isbn
149+
])
150+
]));
151+
152+
ThothContainer::getInstance()->set('client', function () use ($mockThothClient) {
153+
return $mockThothClient;
154+
});
155+
156+
$errors = ThothValidator::validateIsbnExists($isbn);
157+
158+
$this->assertEquals([
159+
'##plugins.generic.thoth.validation.isbnExists##',
160+
], $errors);
161+
}
76162
}

tests/classes/services/ThothContributorServiceTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,13 @@ public function testGetManyContributors()
137137
'firstName' => 'Brenna Clarke',
138138
'lastName' => 'Gray',
139139
'fullName' => 'Brenna Clarke Gray',
140-
// 'orcid' => 'https://orcid.org/0000-0002-6079-0484',
141140
'website' => 'http://brennaclarkegray.ca'
142141
]),
143142
new ThothContributor([
144143
'contributorId' => '5b0d32d4-bfd9-4db1-88fb-4cb91bdaf246',
145144
'firstName' => 'Dilton Oliveira de',
146145
'lastName' => 'Araújo',
147146
'fullName' => 'Dilton Oliveira de Araújo',
148-
// 'orcid' => null,
149147
'website' => 'http://buscatextual.cnpq.br/buscatextual/visualizacv.do?id=B00408'
150148
])
151149
]));

0 commit comments

Comments
 (0)