Skip to content

Commit

Permalink
Merge pull request #93 from lepidus/i6700_cslOps
Browse files Browse the repository at this point in the history
pkp/pkp-lib#6700 Add support for OPS
  • Loading branch information
NateWr authored Mar 24, 2022
2 parents b2a25a7 + ddff18a commit 8f756b3
Show file tree
Hide file tree
Showing 11 changed files with 2,195 additions and 908 deletions.
103 changes: 77 additions & 26 deletions CitationStyleLanguagePlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class CitationStyleLanguagePlugin extends GenericPlugin
/** @var array List of citation download formats available */
public $_citationDownloads = [];

/** @var string Name of the application */
public $applicationName = '';

/**
* @copydoc Plugin::getDisplayName()
*/
Expand Down Expand Up @@ -64,7 +67,10 @@ public function register($category, $path, $mainContextId = null)
return $success;
}
if ($success && $this->getEnabled($mainContextId)) {
$this->applicationName = Application::get()->getName();

HookRegistry::register('ArticleHandler::view', [$this, 'getArticleTemplateData']);
HookRegistry::register('PreprintHandler::view', array($this, 'getPreprintTemplateData'));
HookRegistry::register('LoadHandler', [$this, 'setPageHandler']);
}
return $success;
Expand Down Expand Up @@ -281,7 +287,15 @@ public function getCitationStyleConfig($styleId)
return array_shift($styleConfig);
}

/**
protected function getPublicationTypeUrlPath(): string {
switch ($this->applicationName) {
case 'ojs2': return 'article';
case 'ops': return 'preprint';
}
return '';
}

/**
* Retrieve citation information for the article details template. This
* method is hooked in before a template displays.
*
Expand Down Expand Up @@ -325,7 +339,49 @@ public function getArticleTemplateData($hookName, $args)
}

/**
* Get a specified citation for a given article
* Retrieve citation information for the preprint details template. This
* method is hooked in before a template displays.
*
* @see PreprintHandler::view()
*
* @param array $args
*
* @return false
*/
public function getPreprintTemplateData($hookName, $args)
{
$request = $args[0];
$preprint = $args[1];
$publication = $args[2];
$context = $request->getContext();
$contextId = $context ? $context->getId() : 0;
$templateMgr = TemplateManager::getManager();

$citationArgs = [
'submissionId' => $preprint->getId(),
'publicationId' => $publication->getId(),
];
$citationArgsJson = $citationArgs;
$citationArgsJson['return'] = 'json';

$templateMgr->assign([
'citation' => $this->getCitation($request, $preprint, $this->getPrimaryStyleName($contextId), null, $publication),
'citationArgs' => $citationArgs,
'citationArgsJson' => $citationArgsJson,
'citationStyles' => $this->getEnabledCitationStyles($contextId),
'citationDownloads' => $this->getEnabledCitationDownloads($contextId),
]);

$templateMgr->addJavaScript(
'citationStyleLanguage',
$request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/articleCitation.js'
);

return false;
}

/**
* Get a specified citation for a given submission
*
* This citation format follows the csl-json schema and takes some direction
* from existing CSL mappings documented by Zotero and Mendeley.
Expand All @@ -335,24 +391,25 @@ public function getArticleTemplateData($hookName, $args)
* @see Mendeley's mappings http://support.mendeley.com/customer/portal/articles/364144-csl-type-mapping
*
* @param Request $request
* @param Submission $article
* @param Submission $submission
* @param string $citationStyle Name of the citation style to use.
* @param Issue $issue Optional. Will fetch from db if not passed.
* @param Publication $publication Optional. A particular version
*
* @return string
*/
public function getCitation($request, $article, $citationStyle = 'apa', $issue = null, $publication = null)
{
$publication ??= $article->getCurrentPublication();
$issue ??= Repo::issue()->get($publication->getData('issueId'));
public function getCitation($request, $submission, $citationStyle = 'apa', $issue = null, $publication = null) {
$publication ??= $submission->getCurrentPublication();
$context = $request->getContext();
if($this->applicationName == 'ojs2') {
$issue ??= Repo::issue()->get($publication->getData('issueId'));
}

import('lib.pkp.classes.core.PKPString');

$citationData = new stdClass();
$citationData->type = 'article-journal';
$citationData->id = $article->getId();
$citationData->type = ($this->applicationName == 'ojs2' ? 'article-journal' : 'article');
$citationData->id = $submission->getId();
$citationData->title = $publication->getLocalizedFullTitle();
$citationData->{'container-title'} = $context->getLocalizedName();
$citationData->{'publisher-place'} = $this->getSetting($context->getId(), 'publisherLocation');
Expand Down Expand Up @@ -381,9 +438,9 @@ public function getCitation($request, $article, $citationStyle = 'apa', $issue =
$request,
PKPApplication::ROUTE_PAGE,
null,
'article',
$this->getPublicationTypeUrlPath(),
'view',
$article->getBestId()
$submission->getBestId()
);
$citationData->accessed = new stdClass();
$citationData->accessed->raw = date('Y-m-d');
Expand All @@ -406,7 +463,7 @@ public function getCitation($request, $article, $citationStyle = 'apa', $issue =
if ($publication->getData('datePublished')) {
$citationData->issued = new stdClass();
$citationData->issued->raw = htmlspecialchars($publication->getData('datePublished'));
$publishedPublications = $article->getPublishedPublications();
$publishedPublications = $submission->getPublishedPublications();
if (count($publishedPublications) > 1) {
$originalPublication = array_reduce($publishedPublications, function ($a, $b) {
return $a && $a->getId() < $b->getId() ? $a : $b;
Expand All @@ -427,11 +484,11 @@ public function getCitation($request, $article, $citationStyle = 'apa', $issue =
}

// DOI
if ($issue && $issue->getPublished()) {
$citationData->DOI = $article->getStoredPubId('doi');
if ($publication->getDoi()) {
$citationData->DOI = $publication->getDoi();
}

HookRegistry::call('CitationStyleLanguage::citation', [&$citationData, &$citationStyle, $article, $issue, $context, $publication]);
HookRegistry::call('CitationStyleLanguage::citation', [&$citationData, &$citationStyle, $submission, $issue, $context, $publication]);

$citation = '';

Expand All @@ -444,7 +501,7 @@ public function getCitation($request, $article, $citationStyle = 'apa', $issue =
$templateMgr->assign([
'citationData' => $citationData,
'citationStyle' => $citationStyle,
'article' => $article,
'article' => $submission,
'publication' => $publication,
'issue' => $issue,
'journal' => $context,
Expand Down Expand Up @@ -496,33 +553,27 @@ public function loadStyle($styleConfig)
* software.
*
* @param Request $request
* @param Submission $article
* @param Submission $submission
* @param string $citationStyle Name of the citation style to use.
* @param Issue $issue Optional. Will fetch from db if not passed.
*
* @return string
*/
public function downloadCitation($request, $article, $citationStyle = 'ris', $issue = null)
public function downloadCitation($request, $submission, $citationStyle = 'ris', $issue = null)
{
$journal = $request->getContext();

if (empty($issue)) {
$issue = Repo::issue()->get($article->getCurrentPublication()->getData('issueId'));
}

$styleConfig = $this->getCitationStyleConfig($citationStyle);
if (empty($styleConfig)) {
return false;
}

$citation = trim(strip_tags($this->getCitation($request, $article, $citationStyle, $issue)));
$citation = trim(strip_tags($this->getCitation($request, $submission, $citationStyle, $issue)));
// TODO this is likely going to cause an error in a citation some day,
// but is necessary to get the .ris downloadable format working. The
// CSL language doesn't seem to offer a way to indicate a line break.
// See: https://github.com/citation-style-language/styles/issues/2831
$citation = str_replace('\n', "\n", $citation);

$encodedFilename = urlencode(substr($article->getLocalizedTitle(), 0, 60)) . '.' . $styleConfig['fileExtension'];
$encodedFilename = urlencode(substr($submission->getLocalizedTitle(), 0, 60)) . '.' . $styleConfig['fileExtension'];

header("Content-Disposition: attachment; filename*=UTF-8''\"${encodedFilename}\"");
header('Content-Type: ' . $styleConfig['contentType']);
Expand Down
Loading

0 comments on commit 8f756b3

Please sign in to comment.