Skip to content

Commit

Permalink
WIP: Add OPS compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ewhanson committed May 8, 2024
1 parent b10c388 commit ec2bf81
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 443 deletions.
311 changes: 60 additions & 251 deletions classes/orcid/OrcidWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,294 +14,103 @@

namespace APP\orcid;

use APP\author\Author;
use APP\core\Application;
use APP\facades\Repo;
use APP\issue\Issue;
use APP\journal\Journal;
use APP\plugins\generic\citationStyleLanguage\CitationStyleLanguagePlugin;
use APP\plugins\PubIdPlugin;
use APP\publication\Publication;
use APP\submission\Submission;
use Carbon\Carbon;
use PKP\context\Context;
use PKP\i18n\LocaleConversion;
use PKP\orcid\PKPOrcidWork;
use PKP\plugins\PluginRegistry;

class OrcidWork
class OrcidWork extends PKPOrcidWork
{
public const PUBID_TO_ORCID_EXT_ID = ['doi' => 'doi', 'other::urn' => 'urn'];
public const USER_GROUP_TO_ORCID_ROLE = ['Author' => 'AUTHOR', 'Translator' => 'CHAIR_OR_TRANSLATOR', 'Journal manager' => 'AUTHOR'];

private array $data = [];

public function __construct(
private Publication $publication,
private Context $context,
private array $authors,
private ?Issue $issue = null
protected Publication $publication,
protected Context $context,
protected array $authors,
protected ?Issue $issue = null
) {
$this->data = $this->build();
}

public function toArray(): array
{
return $this->data;
parent::__construct($this->publication, $this->context, $this->authors);
}

private function build(): array
/**
* @inheritdoc
*/
protected function getAppPubIdExternalIds(PubIdPlugin $plugin): array
{
$submission = Repo::submission()->get($this->publication->getData('submissionId'));

$applicationName = Application::get()->getName();
$bibtexCitation = '';

$publicationLocale = $this->publication->getData('locale');

$publicationUrl = Application::get()->getDispatcher()->url(
Application::get()->getRequest(),
Application::ROUTE_PAGE,
$this->context->getPath(),
'article',
'view',
$submission->getId(),
urlLocaleForPage: '',
);

$orcidWork = [
'title' => [
'title' => [
'value' => trim(strip_tags($this->publication->getLocalizedTitle($publicationLocale))) ?? ''
$ids = [];

$pubIdType = $plugin->getPubIdType();
$pubId = $this->issue?->getStoredPubId($pubIdType);
if ($pubId) {
$ids[] = [
'external-id-type' => self::PUBID_TO_ORCID_EXT_ID[$pubIdType],
'external-id-value' => $pubId,
'external-id-url' => [
'value' => $plugin->getResolvingURL($this->context->getId(), $pubId)
],
'subtitle' => [
'value' => trim(strip_tags($this->publication->getLocalizedData('subtitle', $publicationLocale))) ?? ''
]
],
'journal-title' => [
'value' => $this->context->getName($publicationLocale) ?? $this->context->getName($this->context->getPrimaryLocale()),
],
'short-description' => trim(strip_tags($this->publication->getLocalizedData('abstract', $publicationLocale))) ?? '',

'external-ids' => [
'external-id' => $this->buildOrcidExternalIds($submission, $this->publication, $this->context, $this->issue, $publicationUrl)
],
'publication-date' => $this->buildOrcidPublicationDate($this->publication, $this->issue),
'url' => $publicationUrl,
'language-code' => LocaleConversion::getIso1FromLocale($publicationLocale),
'contributors' => [
'contributor' => $this->buildOrcidContributors($this->authors, $this->context, $this->publication)
]
];

if ($applicationName == 'ojs2') {
PluginRegistry::loadCategory('generic');
$citationPlugin = PluginRegistry::getPlugin('generic', 'citationstylelanguageplugin');
/** @var CitationStyleLanguagePlugin $citationPlugin */
$bibtexCitation = trim(strip_tags($citationPlugin->getCitation($this->request, $submission, 'bibtex', $this->issue, $this->publication)));
$orcidWork['citation'] = [
'citation-type' => 'bibtex',
'citation-value' => $bibtexCitation,
'external-id-relationship' => 'part-of'
];
$orcidWork['type'] = 'journal-article';
} elseif ($applicationName == 'ops') {
$orcidWork['type'] = 'preprint';
}

foreach ($this->publication->getData('title') as $locale => $title) {
if ($locale !== $publicationLocale) {
$orcidWork['title']['translated-title'] = ['value' => $title, 'language-code' => LocaleConversion::getIso1FromLocale($locale)];
}
}

return $orcidWork;
return $ids;
}

/**
* Build the external identifiers ORCID JSON structure from article, journal and issue meta data.
*
* @see https://pub.orcid.org/v2.0/identifiers Table of valid ORCID identifier types.
*
* @param Submission $submission The Article object for which the external identifiers should be build.
* @param Publication $publication The Article object for which the external identifiers should be build.
* @param Journal $context Context the Submission is part of.
* @param Issue $issue The Issue object the Article object belongs to.
*
* @return array An associative array corresponding to ORCID external-id JSON.
* @inheritdoc
*/
private function buildOrcidExternalIds(Submission $submission, Publication $publication, Context $context, Issue $issue, string $articleUrl): array
protected function getAppDoiExternalIds(): array
{
$contextId = $context->getId();

$externalIds = [];
$pubIdPlugins = PluginRegistry::loadCategory('pubIds', true, $contextId);
// Add doi, urn, etc. for article
$articleHasStoredPubId = false;

// Handle non-DOI pubIds
if (!empty($pubIdPlugins)) {
foreach ($pubIdPlugins as $plugin) {
if (!$plugin->getEnabled()) {
continue;
}

$pubIdType = $plugin->getPubIdType();

# Add article ids
$pubId = $publication->getStoredPubId($pubIdType);

if ($pubId) {
$externalIds[] = [
'external-id-type' => self::PUBID_TO_ORCID_EXT_ID[$pubIdType],
'external-id-value' => $pubId,
'external-id-url' => [
'value' => $plugin->getResolvingURL($contextId, $pubId)
],
'external-id-relationship' => 'self'
];

$articleHasStoredPubId = true;
}

# Add issue ids if they exist
$pubId = $issue->getStoredPubId($pubIdType);
if ($pubId) {
$externalIds[] = [
'external-id-type' => self::PUBID_TO_ORCID_EXT_ID[$pubIdType],
'external-id-value' => $pubId,
'external-id-url' => [
'value' => $plugin->getResolvingURL($contextId, $pubId)
],
'external-id-relationship' => 'part-of'
];
}
}
}

// Handle DOIs
if ($context->areDoisEnabled()) {
# Add article ids
$publicationDoiObject = $publication->getData('doiObject');

if ($publicationDoiObject) {
$externalIds[] = [
'external-id-type' => self::PUBID_TO_ORCID_EXT_ID['doi'],
'external-id-value' => $publicationDoiObject->getData('doi'),
'external-id-url' => [
'value' => $publicationDoiObject->getResolvingUrl()
],
'external-id-relationship' => 'self'
];

$articleHasStoredPubId = true;
}

// Add issue ids if they exist
$issueDoiObject = $issue->getData('doiObject');
if ($issueDoiObject) {
$externalIds[] = [
'external-id-type' => self::PUBID_TO_ORCID_EXT_ID['doi'],
'external-id-value' => $issueDoiObject->getData('doi'),
'external-id-url' => [
'value' => $issueDoiObject->getResolvingUrl()
],
'external-id-relationship' => 'part-of'
];
}
}

if (!$articleHasStoredPubId) {
// No pubidplugins available or article does not have any stored pubid
// Use URL as an external-id
$externalIds[] = [
'external-id-type' => 'uri',
'external-id-value' => $articleUrl,
'external-id-relationship' => 'self'
];
}

// Add journal online ISSN
// TODO What about print ISSN?
if ($context->getData('onlineIssn')) {
$externalIds[] = [
'external-id-type' => 'issn',
'external-id-value' => $context->getData('onlineIssn'),
$ids = [];

$issueDoiObject = $this->issue->getData('doiObject');
if ($issueDoiObject) {
$ids[] = [
'external-id-type' => self::PUBID_TO_ORCID_EXT_ID['doi'],
'external-id-value' => $issueDoiObject->getData('doi'),
'external-id-url' => [
'value' => $issueDoiObject->getResolvingUrl()
],
'external-id-relationship' => 'part-of'
];
}

return $externalIds;
return $ids;
}

/**
* Parse issue year and publication date and use the older one of the two as
* the publication date of the ORCID work.
*
* @param null|mixed $issue
*
* @return array Associative array with year, month and day or only year
* @inheritDoc
*/
private function buildOrcidPublicationDate(Publication $publication, ?Issue $issue = null): array
protected function getOrcidPublicationType(): string
{
$publicationPublishDate = Carbon::parse($publication->getData('datePublished'));

return [
'year' => ['value' => $publicationPublishDate->format('Y')],
'month' => ['value' => $publicationPublishDate->format('m')],
'day' => ['value' => $publicationPublishDate->format('d')]
];
return 'journal-article';
}

/**
* Build associative array fitting for ORCID contributor mentions in an
* ORCID work from the supplied Authors array.
*
* @param Author[] $authors Array of Author objects
*
* @return array[] Array of associative arrays,
* one for each contributor
* @inheritdoc
*/
private function buildOrcidContributors(array $authors, Context $context, Publication $publication): array
protected function getBibtexCitation(Submission $submission): string
{
$contributors = [];
$first = true;

foreach ($authors as $author) {
$contributor = [
'credit-name' => $author->getFullName(),
'contributor-attributes' => [
'contributor-sequence' => $first ? 'first' : 'additional'
]
];

$userGroup = $author->getUserGroup();
$role = self::USER_GROUP_TO_ORCID_ROLE[$userGroup->getName('en')];

if ($role) {
$contributor['contributor-attributes']['contributor-role'] = $role;
}

if ($author->getOrcid()) {
$orcid = basename(parse_url($author->getOrcid(), PHP_URL_PATH));

if ($author->getData('orcidSandbox')) {
$uri = ORCID_URL_SANDBOX . $orcid;
$host = 'sandbox.orcid.org';
} else {
$uri = $author->getOrcid();
$host = 'orcid.org';
}

$contributor['contributor-orcid'] = [
'uri' => $uri,
'path' => $orcid,
'host' => $host
];
}

$first = false;

$contributors[] = $contributor;
$request = Application::get()->getRequest();
PluginRegistry::loadCategory('generic');
/** @var CitationStyleLanguagePlugin $citationPlugin */
$citationPlugin = PluginRegistry::getPlugin('generic', 'citationstylelanguageplugin');
try {
return trim(
strip_tags(
$citationPlugin->getCitation(
$request,
$submission,
'bibtex',
$this->issue,
$this->publication
)
)
);
} catch (\Exception $exception) {
return '';
}

return $contributors;
}
}
Loading

0 comments on commit ec2bf81

Please sign in to comment.