From 01087ddedcb9b54f87d63391e1cf8b9091c776f9 Mon Sep 17 00:00:00 2001 From: Dimitris Efstathiou Date: Thu, 7 Dec 2023 13:53:36 +0200 Subject: [PATCH] pkp/pkp-lib#7505 Support for JATS files/content added --- JatsTemplatePlugin.php | 11 ++------- classes/Article.php | 38 ++++++++++++++++++----------- classes/ArticleFront.php | 42 +++++++++++++++++++++++--------- tests/functional/ArticleTest.php | 11 +++++---- 4 files changed, 62 insertions(+), 40 deletions(-) diff --git a/JatsTemplatePlugin.php b/JatsTemplatePlugin.php index 40cad2d..b613609 100755 --- a/JatsTemplatePlugin.php +++ b/JatsTemplatePlugin.php @@ -12,19 +12,11 @@ namespace APP\plugins\generic\jatsTemplate; -use APP\facades\Repo; -use APP\core\Services; use APP\core\Application; use APP\plugins\generic\jatsTemplate\classes\Article; use APP\template\TemplateManager; -use PKP\core\PKPString; -use PKP\db\DAORegistry; use PKP\plugins\GenericPlugin; use PKP\plugins\Hook; -use PKP\plugins\PluginRegistry; -use PKP\search\SearchFileParser; -use PKP\submissionFile\SubmissionFile; -use PKP\config\Config; class JatsTemplatePlugin extends GenericPlugin { @@ -68,7 +60,8 @@ public function callbackFindJats($hookName, $args) { $doc =& $args[3]; if (!$doc && empty($candidateFiles)) { - $doc = new Article($record); + $doc = new Article(); + $doc->convertOAIToXml($record); } return false; diff --git a/classes/Article.php b/classes/Article.php index d1247af..9f3afc3 100644 --- a/classes/Article.php +++ b/classes/Article.php @@ -14,21 +14,25 @@ namespace APP\plugins\generic\jatsTemplate\classes; use APP\core\Application; -use DOMException; +use APP\issue\Issue; +use APP\publication\Publication; +use APP\section\Section; +use APP\submission\Submission; +use PKP\context\Context; +use PKP\core\PKPRequest; +use PKP\oai\OAIRecord; class Article extends \DOMDocument { - function __construct($record) + function __construct() { parent::__construct('1.0', 'UTF-8'); - $this->convertToXml($record); } /** - * @param $record - * @return bool|\DOMDocument + * */ - public function convertToXml($record) :bool|\DOMDocument + public function convertOAIToXml(OAIRecord $record): void { $submission = $record->getData('article'); $journal = $record->getData('journal'); @@ -37,25 +41,30 @@ public function convertToXml($record) :bool|\DOMDocument $publication = $submission->getCurrentPublication(); $request = Application::get()->getRequest(); + $this->convertSubmission($submission, $journal, $section, $issue, $publication, $request); + } + + /** + * Convert submission metadata to JATS XML + */ + public function convertSubmission(Submission $submission, Context $context, Section $section, ?Issue $issue = null, ?Publication $publication = null, PKPRequest $request): void + { $articleElement = $this->appendChild($this->createElement('article')) - ->setAttribute('xmlns:xlink','http://www.w3.org/1999/xlink')->parentNode - ->setAttribute('xml:lang', substr($submission->getLocale(), 0, 2))->parentNode - ->setAttribute('xmlns:mml','http://www.w3.org/1998/Math/MathML')->parentNode - ->setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')->parentNode; + ->setAttribute('xmlns:xlink','http://www.w3.org/1999/xlink')->parentNode + ->setAttribute('xml:lang', substr($submission->getLocale(), 0, 2))->parentNode + ->setAttribute('xmlns:mml','http://www.w3.org/1998/Math/MathML')->parentNode + ->setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance')->parentNode; $articleFront = new ArticleFront(); - $articleElement->appendChild($this->importNode($articleFront->create($journal, $submission, $section, $issue, $request, $this), true)); + $articleElement->appendChild($this->importNode($articleFront->create($context, $submission, $section, $issue, $request, $this, $publication), true)); $articleBody = new ArticleBody(); $articleElement->appendChild($this->importNode($articleBody->create($submission), true)); $articleBack = new ArticleBack(); $articleElement->appendChild($this->importNode($articleBack->create($publication), true)); - - return $this->loadXml($this->saveXML($articleElement)); } - /** * Map the specific HTML tags in title/ sub title for JATS schema compability * @see https://jats.nlm.nih.gov/publishing/0.4/xsd/JATS-journalpublishing0.xsd @@ -76,4 +85,5 @@ public function mapHtmlTagsForTitle(string $htmlTitle): string return str_replace(array_keys($mappings), array_values($mappings), $htmlTitle); } + } diff --git a/classes/ArticleFront.php b/classes/ArticleFront.php index bf0aded..2aa8e09 100644 --- a/classes/ArticleFront.php +++ b/classes/ArticleFront.php @@ -19,6 +19,7 @@ use APP\section\Section; use APP\submission\Submission; use APP\publication\Publication; +use PKP\core\PKPApplication; use PKP\core\PKPString; use PKP\db\DAORegistry; use PKP\plugins\PluginRegistry; @@ -27,16 +28,15 @@ class ArticleFront extends \DOMDocument { - /** * Create article front element */ - public function create(Journal $journal, Submission $submission, Section $section,Issue $issue, PKPRequest $request, Article $article): \DOMNode + public function create(Journal $journal, Submission $submission, Section $section, ?Issue $issue, PKPRequest $request, Article $article, ?Publication $workingPublication = null): \DOMNode { return $this->appendChild($this->createElement('front')) ->appendChild($this->createJournalMeta($journal, $request)) ->parentNode - ->appendChild($this->createArticleMeta($submission, $journal, $section, $issue, $request, $article)) + ->appendChild($this->createArticleMeta($submission, $journal, $section, $issue, $request, $article, $workingPublication)) ->parentNode; } @@ -87,9 +87,14 @@ public function createJournalMeta(Journal $journal, PKPRequest $request): \DOMNo ->appendChild($this->createTextNode($journal->getSetting('printIssn')))->parentNode ->setAttribute('pub-type','ppub'); } + + $router = $request->getRouter(); + $dispatcher = $router->getDispatcher(); + + $journalUrl = $dispatcher->url($request, PKPApplication::ROUTE_PAGE, $journal->getPath()); $journalMetaElement ->appendChild($this->createElement('self-uri')) - ->setAttribute('xlink:href', $request->url($journal->getPath())); + ->setAttribute('xlink:href', $journalUrl); return $journalMetaElement; @@ -124,9 +129,12 @@ public function createJournalMetaJournalTitleGroup(Journal $journal): \DOMNode /** * Create xml article-meta DOMNode */ - function createArticleMeta(Submission $submission, Journal $journal, Section $section, Issue $issue, $request, Article $article) + function createArticleMeta(Submission $submission, Journal $journal, Section $section, ?Issue $issue, $request, Article $article, ?Publication $workingPublication = null) { $publication = $submission->getCurrentPublication(); + if ($workingPublication) { + $publication = $workingPublication; + } $articleMetaElement = $this->appendChild($this->createElement('article-meta')); @@ -186,11 +194,16 @@ function createArticleMeta(Submission $submission, Journal $journal, Section $se } $datePublished = $submission->getDatePublished(); - if (!$datePublished) $datePublished = $issue->getDatePublished(); - if ($datePublished) $datePublished = strtotime($datePublished); + if ($datePublished) { + $datePublished = strtotime($datePublished); + } else { + if ($issue) { + $datePublished = $issue->getDatePublished(); + } + } // Include pub dates - if ($submission->getDatePublished()){ + if ($datePublished) { $pubDateElement = $articleMetaElement->appendChild($this->createElement('pub-date')) ->setAttribute('date-type', 'pub')->parentNode ->setAttribute('publication-format','epub')->parentNode; @@ -237,9 +250,9 @@ function createArticleMeta(Submission $submission, Journal $journal, Section $se $pageCount = $matchedPageTo - $matchedPageFrom + 1; } - $copyrightYear = $submission->getCopyrightYear(); - $copyrightHolder = $submission->getLocalizedCopyrightHolder(); - $licenseUrl = $submission->getLicenseURL(); + $copyrightYear = $publication->getData('copyrightYear'); + $copyrightHolder = $publication->getLocalizedData('copyrightHolder'); + $licenseUrl = $publication->getData('licenseUrl'); $ccBadge = Application::get()->getCCLicenseBadge($licenseUrl, $submission->getLocale())=== null?'':Application::get()->getCCLicenseBadge($licenseUrl, $submission->getLocale()); if ($copyrightYear || $copyrightHolder || $licenseUrl || $ccBadge) { $permissionsElement = $articleMetaElement->appendChild($this->createElement('permissions')); @@ -264,10 +277,15 @@ function createArticleMeta(Submission $submission, Journal $journal, Section $se } } } + + $router = $request->getRouter(); + $dispatcher = $router->getDispatcher(); + + $url = $dispatcher->url($request, PKPApplication::ROUTE_PAGE, $journal->getPath(), 'article', 'view', $submission->getBestId(), null, null, true); $articleMetaElement ->appendChild($this->createElement('self-uri')) - ->setAttribute('xlink:href', $request->url($journal->getPath(), 'article', 'view', $submission->getBestArticleId())); + ->setAttribute('xlink:href', $url); $submissionKeywordDao = DAORegistry::getDAO('SubmissionKeywordDAO'); foreach ($submissionKeywordDao->getKeywords($publication->getId(), $journal->getSupportedLocales()) as $locale => $keywords) { diff --git a/tests/functional/ArticleTest.php b/tests/functional/ArticleTest.php index 7ffe277..f5acd09 100644 --- a/tests/functional/ArticleTest.php +++ b/tests/functional/ArticleTest.php @@ -208,10 +208,10 @@ private function createOAIRecordMockObject(): OAIRecord public function testConvertToXml() { $record = $this->createOAIRecordMockObject(); - $article = new Article($record); - $xml = $article->convertToXml($record); + $article = new Article(); + $article->convertOAIToXml($record); self::assertXmlStringEqualsXmlFile($this->xmlFilePath.'ie1.xml', $article->saveXml()); - self::assertTrue($xml); + self::assertTrue($article); } /** @@ -221,8 +221,9 @@ public function testMapHtmlTagsForTitle(){ $expected = 'test'; $htmlString = 'test'; $record = $this->createOAIRecordMockObject(); - $article = new Article($record); + $article = new Article(); + $article->convertOAIToXml($record); $actual = $article->mapHtmlTagsForTitle($htmlString); - self::assertEquals($expected,$actual); + self::assertEquals($expected, $actual); } }