Skip to content

Commit

Permalink
Merge pull request #23 from lepidus/stable-3_3_0
Browse files Browse the repository at this point in the history
Feature/added work exists validation (OMP 3.3.0)
  • Loading branch information
thiagolepidus authored Jan 28, 2025
2 parents 820ff65 + 0839b29 commit 4314e65
Show file tree
Hide file tree
Showing 96 changed files with 1,746 additions and 4,671 deletions.
4 changes: 4 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ include:
- rm -rf lib/APIKeyEncryption
- git submodule update --init --depth 1
- composer install

unit_test_php_7_3:
rules:
- when: never
25 changes: 2 additions & 23 deletions ThothPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
* @brief Plugin for integration with Thoth for communication and synchronization of book data between the two platforms
*/

require_once(__DIR__ . '/vendor/autoload.php');

import('lib.pkp.classes.plugins.GenericPlugin');
import('plugins.generic.thoth.classes.ThothBadgeRender');
import('plugins.generic.thoth.classes.ThothNotification');
import('plugins.generic.thoth.classes.ThothRegister');
import('plugins.generic.thoth.classes.ThothUpdater');
import('plugins.generic.thoth.lib.thothAPI.exceptions.ThothException');

class ThothPlugin extends GenericPlugin
{
Expand Down Expand Up @@ -120,26 +121,4 @@ public function manage($args, $request)
}
return parent::manage($args, $request);
}

public function getThothClient($contextId = null)
{
$contextId = $contextId ?? Application::get()->getRequest()->getContext()->getId();

$email = $this->getSetting($contextId, 'email');
$password = $this->getSetting($contextId, 'password');

if (!$email || !$password) {
throw new ThothException("Credentials not configured", 0);
}

import('plugins.generic.thoth.lib.APIKeyEncryption.APIKeyEncryption');
$password = APIKeyEncryption::decryptString($password);
$testEnvironment = $this->getSetting($contextId, 'testEnvironment');

import('plugins.generic.thoth.lib.thothAPI.ThothClient');
$thothClient = new ThothClient($testEnvironment);
$thothClient->login($email, $password);

return $thothClient;
}
}
20 changes: 13 additions & 7 deletions ThothSettingsForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
* @brief Form for managers to modify Thoth plugin settings
*/

use ThothApi\Exception\QueryException;
use ThothApi\GraphQL\Client;

import('lib.pkp.classes.form.Form');
import('plugins.generic.thoth.lib.APIKeyEncryption.APIKeyEncryption');
import('plugins.generic.thoth.lib.thothAPI.ThothClient');

class ThothSettingsForm extends Form
{
Expand Down Expand Up @@ -48,13 +50,17 @@ public function __construct($plugin, $contextId)
function ($password) use ($form) {
$email = trim($this->getData('email'));
$testEnvironment = $this->getData('testEnvironment');
$thothClient = new ThothClient($testEnvironment);

$httpConfig = [];
if ($testEnvironment) {
$httpConfig['base_uri'] = 'http://localhost:8000/';
}

$client = new Client($httpConfig);

try {
$thothClient->login(
$email,
$password
);
} catch (ThothException $e) {
$client->login($email, $password);
} catch (QueryException $e) {
return false;
}
return true;
Expand Down
29 changes: 16 additions & 13 deletions classes/ThothRegister.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @brief Manage callback functions to register works in Thoth
*/

use ThothApi\Exception\QueryException;

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

Expand Down Expand Up @@ -51,12 +53,14 @@ public function addThothField($hookName, $form)
$errors = [];

try {
$thothClient = $this->plugin->getThothClient($submission->getData('contextId'));
$publishers = $thothClient->linkedPublishers();
$thothClient = ThothContainer::getInstance()->get('client');
$thothAccountDetails = $thothClient->accountDetails();
$publishers = $thothAccountDetails['resourceAccess']['linkedPublishers'];

$imprints = $thothClient->imprints(['publishers' => array_column($publishers, 'publisherId')]);
} catch (ThothException $e) {
} catch (QueryException $e) {
$errors[] = __('plugins.generic.thoth.connectionError');
error_log($e->getMessage());
error_log('Failed to send the request to Thoth: ' . $e->getMessage());
}

if (empty($errors)) {
Expand All @@ -83,8 +87,8 @@ public function addThothField($hookName, $form)
$imprintOptions = [];
foreach ($imprints as $imprint) {
$imprintOptions[] = [
'value' => $imprint['imprintId'],
'label' => $imprint['imprintName']
'value' => $imprint->getImprintId(),
'label' => $imprint->getImprintName()
];
}

Expand All @@ -102,7 +106,7 @@ public function addThothField($hookName, $form)
'required' => true,
'showWhen' => 'registerConfirmation',
'groupId' => 'default',
'value' => $imprints[0]['imprintId'] ?? null
'value' => $imprintOptions[0]['value'] ?? null
]));

return false;
Expand Down Expand Up @@ -187,11 +191,10 @@ public function registerWork($submission, $imprint)
}

try {
$thothClient = $this->plugin->getThothClient($submissionContext->getId());
$thothBook = ThothService::work()->registerBook($thothClient, $submission, $imprint);
$thothBook = ThothService::work()->registerBook($submission, $imprint);
$submission = Services::get('submission')->edit(
$submission,
['thothWorkId' => $thothBook->getId()],
['thothWorkId' => $thothBook->getWorkId()],
$request
);

Expand All @@ -201,14 +204,14 @@ public function registerWork($submission, $imprint)
NOTIFICATION_TYPE_SUCCESS,
'plugins.generic.thoth.register.success'
);
} catch (ThothException $e) {
error_log($e->getMessage());
} catch (QueryException $e) {
error_log('Failed to send the request to Thoth: ' . $e->getMessage());
ThothNotification::notify(
$request,
$submission,
NOTIFICATION_TYPE_ERROR,
'plugins.generic.thoth.register.error',
$e->getError()
$e->getMessage()
);
}
}
Expand Down
11 changes: 6 additions & 5 deletions classes/ThothUpdater.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @brief Manage callback functions to update works in Thoth
*/

use ThothApi\Exception\QueryException;

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

class ThothUpdater
Expand All @@ -37,23 +39,22 @@ public function updateWork($hookName, $args)
}

try {
$thothClient = $this->plugin->getThothClient($submission->getData('contextId'));
ThothService::work()->updateBook($thothClient, $thothWorkId, $submission, $publication);
ThothService::work()->updateBook($thothWorkId, $submission, $publication);

ThothNotification::notify(
$request,
$submission,
NOTIFICATION_TYPE_SUCCESS,
'plugins.generic.thoth.update.success'
);
} catch (ThothException $e) {
error_log($e->getMessage());
} catch (QueryException $e) {
error_log('Failed to send the request to Thoth: ' . $e->getMessage());
ThothNotification::notify(
$request,
$submission,
NOTIFICATION_TYPE_ERROR,
'plugins.generic.thoth.update.error',
$e->getError()
$e->getMessage()
);
}

Expand Down
73 changes: 70 additions & 3 deletions classes/ThothValidator.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
* @brief Validate submission metadata to Thoth submit
*/

require_once(__DIR__ . '/../vendor/autoload.php');

use Biblys\Isbn\Isbn;
use ThothApi\GraphQL\Models\Work as ThothWork;

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

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

$publication = $submission->getCurrentPublication();
$doi = $publication->getStoredPubId('doi');
$doiUrl = ThothService::work()->getDoiResolvingUrl($doi);

if ($doiUrl !== null) {
$errors = array_merge($errors, self::validateDoiExists($doiUrl));
}

$request = Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();

$landingPage = $dispatcher->url(
$request,
ROUTE_PAGE,
$context->getPath(),
'catalog',
'book',
$submission->getBestId()
);

$errors = array_merge($errors, self::validateLandingPageExists($landingPage));

$publicationFormats = Application::getRepresentationDao()
->getApprovedByPublicationId($submission->getData('currentPublicationId'))
->toArray();
Expand All @@ -44,7 +66,10 @@ public static function validateIsbn($publicationFormats)
foreach ($publicationFormats as $publicationFormat) {
try {
$isbn = ThothService::publication()->getIsbnByPublicationFormat($publicationFormat);
Isbn::validateAsIsbn13($isbn);
if ($isbn !== null) {
Isbn::validateAsIsbn13($isbn);
}
$errors = array_merge($errors, self::validateIsbnExists($isbn));
} catch (Exception $e) {
$errors[] = __('plugins.generic.thoth.validation.isbn', [
'isbn' => $isbn,
Expand All @@ -55,4 +80,46 @@ public static function validateIsbn($publicationFormats)

return $errors;
}

public static function validateDoiExists($doi)
{
$errors = [];

try {
$work = ThothService::work()->getByDoi($doi);
if ($work instanceof ThothWork) {
$errors[] = __('plugins.generic.thoth.validation.doiExists', ['doi' => $doi]);
}
} catch (Exception $e) {
return $errors;
}

return $errors;
}

public static function validateLandingPageExists($landingPage)
{
$errors = [];

$works = ThothService::work()->search($landingPage);

if (!empty($works)) {
$errors[] = __('plugins.generic.thoth.validation.landingPageExists', ['landingPage' => $landingPage]);
}

return $errors;
}

public static function validateIsbnExists($isbn)
{
$errors = [];

$publications = ThothService::publication()->search($isbn);

if (!empty($publications)) {
$errors[] = __('plugins.generic.thoth.validation.isbnExists', ['isbn' => $isbn]);
}

return $errors;
}
}
6 changes: 3 additions & 3 deletions classes/components/forms/RegisterForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function __construct($action, $imprints, $errors)
$imprintOptions = [];
foreach ($imprints as $imprint) {
$imprintOptions[] = [
'value' => $imprint['imprintId'],
'label' => $imprint['imprintName']
'value' => $imprint->getImprintId(),
'label' => $imprint->getImprintName()
];
}

Expand All @@ -85,7 +85,7 @@ public function __construct($action, $imprints, $errors)
'options' => $imprintOptions,
'required' => true,
'groupId' => 'default',
'value' => $imprints[0]['imprintId'] ?? null
'value' => $imprintOptions[0]['value'] ?? null
]));
}

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

/**
* @file plugins/generic/thoth/tests/classes/container/Container.inc.php
*
* Copyright (c) 2025 Lepidus Tecnologia
* Copyright (c) 2025 Thoth
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Container
* @ingroup plugins_generic_thoth
*
* @brief Simple dependency injection container implementation
*/

class Container
{
private $bindings = [];

public function set($id, $factory)
{
$this->bindings[$id] = $factory;
}

public function get($id)
{
if (!isset($this->bindings[$id])) {
throw new Exception("Target binding \"$id\" does not exist.");
}

$factory = $this->bindings[$id];

return $factory($this);
}

public function backup($id)
{
return $this->bindings[$id];
}

public function register($containerProvider)
{
$containerProvider->register($this);
}
}
19 changes: 19 additions & 0 deletions classes/container/ContainerProvider.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @file plugins/generic/thoth/tests/classes/container/ContainerProvider.inc.php
*
* Copyright (c) 2025 Lepidus Tecnologia
* Copyright (c) 2025 Thoth
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class ContainerProvider
* @ingroup plugins_generic_thoth
*
* @brief Interface to package container bindings
*/

interface ContainerProvider
{
public function register($container);
}
Loading

0 comments on commit 4314e65

Please sign in to comment.