From 22ff4350731bab11342c46fb3fa0e6789fe39c36 Mon Sep 17 00:00:00 2001 From: nongenti Date: Thu, 26 Jan 2023 12:50:40 +0100 Subject: [PATCH] Adapt to support OMP --- CitationStyleLanguagePlugin.php | 144 ++++++----------------- CitationStyleLanguageSettingsForm.php | 6 +- composer.lock | 163 ++++++++------------------ 3 files changed, 88 insertions(+), 225 deletions(-) diff --git a/CitationStyleLanguagePlugin.php b/CitationStyleLanguagePlugin.php index 13bb53d..00374bb 100644 --- a/CitationStyleLanguagePlugin.php +++ b/CitationStyleLanguagePlugin.php @@ -56,9 +56,9 @@ class CitationStyleLanguagePlugin extends GenericPlugin /** @var string Name of the application */ public string $application; - private bool $book = false; - private bool $chapter = false; - private bool $article = false; + protected bool $isBook = false; + protected bool $isChapter = false; + protected bool $isArticle = false; /** * Constructor @@ -108,10 +108,8 @@ public function register($category, $path, $mainContextId = null) /** * Get list of citation styles available - * - * @return array */ - public function getCitationStyles() + public function getCitationStyles(): array { if (!empty($this->_citationStyles)) { return $this->_citationStyles; @@ -181,12 +179,8 @@ public function getCitationStyles() /** * Get the primary style name or default to the first available style - * - * @param $contextId integer Journal ID - * - * @return string */ - public function getPrimaryStyleName($contextId = 0) + public function getPrimaryStyleName(int $contextId): string { $primaryStyleName = $this->getSetting($contextId, 'primaryCitationStyle'); if ($primaryStyleName) { @@ -205,12 +199,8 @@ public function getPrimaryStyleName($contextId = 0) /** * Get enabled citation styles - * - * @param $contextId integer Journal ID - * - * @return array */ - public function getEnabledCitationStyles($contextId = 0) + public function getEnabledCitationStyles(int $contextId): array { $styles = $this->getCitationStyles(); $enabled = $this->getSetting($contextId, 'enabledCitationStyles'); @@ -227,10 +217,8 @@ public function getEnabledCitationStyles($contextId = 0) /** * Get list of citation download formats available - * - * @return array */ - public function getCitationDownloads() + public function getCitationDownloads(): array { if (!empty($this->_citationDownloads)) { return $this->_citationDownloads; @@ -264,12 +252,8 @@ public function getCitationDownloads() /** * Get enabled citation styles - * - * @param $contextId integer Journal ID - * - * @return array */ - public function getEnabledCitationDownloads($contextId = 0) + public function getEnabledCitationDownloads(int $contextId): array { $downloads = $this->getCitationDownloads(); $enabled = $this->getSetting($contextId, 'enabledCitationDownloads'); @@ -286,12 +270,8 @@ public function getEnabledCitationDownloads($contextId = 0) /** * Pluck citation IDs from array of citations - * - * @param $citations array See getCitationStyles() - * - * @return array */ - public function mapCitationIds($citations) + public function mapCitationIds(array $citations): array { return array_values(array_map(function ($citation) { return $citation['id']; @@ -299,13 +279,9 @@ public function mapCitationIds($citations) } /** - * Get citation config for a citation ID - * - * @param $styleId string Example: 'apa' - * - * @return array + * Get citation config for a citation ID (example: 'apa') */ - public function getCitationStyleConfig($styleId) + public function getCitationStyleConfig(string $styleId): array { $styleConfigs = array_merge($this->getCitationStyles(), $this->getCitationDownloads()); $styleConfig = array_filter($styleConfigs, function ($styleConfig) use ($styleId) { @@ -318,13 +294,9 @@ public function getCitationStyleConfig($styleId) * Retrieve citation information for the article details template. This * method is hooked in before a template displays. * - * @param $args array - * - * @return false * @throws Exception - * @see ArticleHandler::view() */ - public function getTemplateData($hookName, $args) + public function getTemplateData(string $hookName, array $args): bool { $request = $args[0]; $templateMgr = TemplateManager::getManager($request); @@ -347,7 +319,7 @@ public function getTemplateData($hookName, $args) } $context = $request->getContext(); - $contextId = $context ? $context->getId() : 0; + $contextId = $context->getId(); $citationArgs = [ 'submissionId' => $submission->getId(), @@ -385,7 +357,8 @@ public function getTemplateData($hookName, $args) return false; } - public function addCitationMarkup($hookName, $args) { + public function addCitationMarkup(string $hookName, array $args): bool + { $smarty =& $args[1]; $output =& $args[2]; $output .= $smarty->fetch($this->getTemplateResource('citationblock.tpl')); @@ -426,7 +399,7 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu $keywords = $submissionKeywordDao->getKeywords($publication->getId(), [Locale::getSupportedLocales()]); $citationData = new stdClass(); - if ($this->isArticle()) { + if ($this->isArticle) { $citationData->type = 'article-journal'; $citationData->risType = 'JOUR'; $citationData->id = $submission->getId(); @@ -456,7 +429,7 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu if ($publication->getDoi()) { $citationData->DOI = $publication->getDoi(); } - } elseif ($this->isBook()) { + } elseif ($this->isBook) { $citationData->type = 'book'; $citationData->risType = 'BOOK'; $citationData->id = $submission->getId(); @@ -474,7 +447,7 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu if ($publication->getDoi()) { $citationData->DOI = $publication->getDoi(); } - } elseif ($this->isBookChapter()) { + } elseif ($this->isChapter) { $citationData->type = 'chapter'; $citationData->risType = 'CHAP'; $citationData->id = $chapter->getSourceChapterId(); @@ -493,6 +466,8 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu if ($chapter->getDoi()) { $citationData->DOI = $chapter->getDoi(); } + } else { + throw new Exception('Unknown submission content type!'); } /* @var $submissionLanguageDao SubmissionLanguageDAO */ @@ -583,14 +558,14 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu //Clickable URL and DOI including affixes $additionalMarkup = [ 'DOI' => [ - 'function' => function ($cslItem, $renderedText) { - return '' . $renderedText . ''; + 'function' => function ($item, $renderedValue) { + return '' . $renderedValue . ''; }, 'affixes' => true ], 'URL' => [ - 'function' => function ($cslItem, $renderedText) { - return '' . $renderedText . ''; + 'function' => function ($item, $renderedValue) { + return '' . $renderedValue . ''; }, 'affixes' => true ], @@ -607,10 +582,8 @@ public function getCitation($request, $submission, $citationStyle = 'apa', $issu /** * Load a CSL style and return the contents as a string - * - * @param $styleConfig array CSL configuration to load */ - public function loadStyle($styleConfig) + public function loadStyle(array $styleConfig): false|string { if (!empty($styleConfig['useCsl'])) { return file_get_contents($styleConfig['useCsl']); @@ -645,10 +618,6 @@ public function downloadCitation($request, $submission, $citationStyle = 'ris', return false; } - if (!$publication) { - $publication = $submission->getCurrentPublication(); - } - $citation = trim(strip_tags($this->getCitation($request, $submission, $citationStyle, $issue, $publication, $chapter))); // 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 @@ -727,13 +696,8 @@ public function manage($args, $request) /** * Route requests for the citation styles to custom page handler - * - * @see PKPPageRouter::route() - * - * @param $hookName string - * @param $params array */ - public function setPageHandler($hookName, $params) + public function setPageHandler(string $hookName, array $params): bool { $page =& $params[0]; $handler =& $params[3]; @@ -744,22 +708,22 @@ public function setPageHandler($hookName, $params) return false; } - public function getEditorGroups(int $contextId = 0): array + public function getEditorGroups(int $contextId): array { return $this->getSetting($contextId, 'groupEditor') ?? []; } - public function getTranslatorGroups(int $contextId = 0): array + public function getTranslatorGroups(int $contextId): array { return $this->getSetting($contextId, 'groupTranslator') ?? []; } - public function getAuthorGroups(int $contextId = 0): array + public function getAuthorGroups(int $contextId): array { return $this->getSetting($contextId, 'groupAuthor') ?? []; } - public function getChapterAuthorGroups(int $contextId = 0): array + public function getChapterAuthorGroups(int $contextId): array { return $this->getSetting($contextId, 'groupChapterAuthor') ?? []; } @@ -787,8 +751,7 @@ protected function addSeriesInformation(stdClass $citationData, Publication $pub if ($seriesId) { /** @var SeriesDAO $seriesDao */ $seriesDao = DAORegistry::getDAO('SeriesDAO'); - if (null !== $seriesDao) { - $series = $seriesDao->getById($seriesId); + $series = $seriesDao->getById($seriesId); if (null !== $series) { $citationData->{'collection-title'} = htmlspecialchars(trim($series->getLocalizedFullTitle())); $citationData->volume = htmlspecialchars($publication->getData('seriesPosition')); @@ -802,7 +765,6 @@ protected function addSeriesInformation(stdClass $citationData, Publication $pub $citationData->serialNumber[] = htmlspecialchars($printISSN); } } - } } return $citationData; } @@ -816,26 +778,13 @@ protected function getChapter(PKPRequest $request, Publication $publication): ?C } elseif (isset($args[1], $args[3], $args[4]) && $args[1] === 'version' && $args[3] === 'chapter') { $key = 4; } else { - $key = 0; + return null; } - if ($key !== 0) { - $chapterId = (int) $args[$key]; - if ($chapterId > 0) { - $chapterDao = DAORegistry::getDAO('ChapterDAO'); - $chapters = $chapterDao->getBySourceChapterId($chapterId); - $chapters = $chapters->toAssociativeArray(); - $chaptersCount = count($chapters); - - if ($chaptersCount > 0) { - /** @var Chapter $chapter */ - foreach ($chapters as $chapter) { - if ((int) $chapter->getData('publicationId') === $publication->getId()) { - return $chapter; - } - } - } - } + $chapterId = (int) $args[$key]; + if ($chapterId > 0) { + $chapterDao = DAORegistry::getDAO('ChapterDAO'); + return $chapterDao->getBySourceChapterId($chapterId, true, $publication->getId()); } return null; } @@ -847,34 +796,19 @@ protected function setDocumentType($chapter): void { switch ($this->application) { case 'ojs2': - $this->article = true; + $this->isArticle = true; break; case 'omp': if ($chapter) { - $this->chapter = true; + $this->isChapter = true; } else { - $this->book = true; + $this->isBook = true; } break; default: throw new Exception('Unknown application!'); } } - protected function isArticle(): bool - { - return $this->article; - } - - protected function isBook(): bool - { - return $this->book; - } - - protected function isBookChapter(): bool - { - return $this->chapter; - } - protected function setArticleAuthors($citationData, $publication, $context): stdClass { $authors = $publication->getData('authors'); diff --git a/CitationStyleLanguageSettingsForm.php b/CitationStyleLanguageSettingsForm.php index 605bc36..2bfdd63 100644 --- a/CitationStyleLanguageSettingsForm.php +++ b/CitationStyleLanguageSettingsForm.php @@ -49,7 +49,7 @@ public function initData(): void { $request = Application::get()->getRequest(); $context = $request->getContext(); - $contextId = $context ? $context->getId() : 0; + $contextId = $context->getId(); $this->setData('primaryCitationStyle', $this->plugin->getSetting($contextId, 'primaryCitationStyle')); $this->setData('enabledCitationStyles', array_keys($this->plugin->getEnabledCitationStyles($contextId))); $this->setData('enabledCitationDownloads', $this->plugin->getEnabledCitationDownloads($contextId)); @@ -89,7 +89,7 @@ public function readInputData(): void public function fetch($request, $template = null, $display = false): ?string { $context = $request->getContext(); - $contextId = $context ? $context->getId() : 0; + $contextId = $context->getId(); $allStyles = []; foreach ($this->plugin->getCitationStyles() as $style) { @@ -140,7 +140,7 @@ public function execute(...$functionArgs) { $request = Application::get()->getRequest(); $context = $request->getContext(); - $contextId = $context ? $context->getId() : 0; + $contextId = $context->getId(); $this->plugin->updateSetting($contextId, 'primaryCitationStyle', $this->getData('primaryCitationStyle')); $enabledCitationStyles = $this->getData('enabledCitationStyles') ?: []; $this->plugin->updateSetting($contextId, 'enabledCitationStyles', $enabledCitationStyles); diff --git a/composer.lock b/composer.lock index 7b8be8b..70c34fb 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "05e9e05fedc92ddfc2cbdd9dd2196aa9", + "content-hash": "8682d85f7b433692cd90e45022d85525", "packages": [ { "name": "citation-style-language/locales", @@ -18,32 +18,35 @@ }, { "name": "myclabs/php-enum", - "version": "1.7.7", + "version": "1.8.4", "source": { "type": "git", "url": "https://github.com/myclabs/php-enum.git", - "reference": "d178027d1e679832db9f38248fcc7200647dc2b7" + "reference": "a867478eae49c9f59ece437ae7f9506bfaa27483" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/myclabs/php-enum/zipball/d178027d1e679832db9f38248fcc7200647dc2b7", - "reference": "d178027d1e679832db9f38248fcc7200647dc2b7", + "url": "https://api.github.com/repos/myclabs/php-enum/zipball/a867478eae49c9f59ece437ae7f9506bfaa27483", + "reference": "a867478eae49c9f59ece437ae7f9506bfaa27483", "shasum": "" }, "require": { "ext-json": "*", - "php": ">=7.1" + "php": "^7.3 || ^8.0" }, "require-dev": { - "phpunit/phpunit": "^7", + "phpunit/phpunit": "^9.5", "squizlabs/php_codesniffer": "1.*", - "vimeo/psalm": "^3.8" + "vimeo/psalm": "^4.6.2" }, "type": "library", "autoload": { "psr-4": { "MyCLabs\\Enum\\": "src/" - } + }, + "classmap": [ + "stubs/Stringable.php" + ] }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -62,7 +65,7 @@ ], "support": { "issues": "https://github.com/myclabs/php-enum/issues", - "source": "https://github.com/myclabs/php-enum/tree/1.7.7" + "source": "https://github.com/myclabs/php-enum/tree/1.8.4" }, "funding": [ { @@ -74,45 +77,48 @@ "type": "tidelift" } ], - "time": "2020-11-14T18:14:52+00:00" + "time": "2022-08-04T09:53:51+00:00" }, { "name": "seboettg/citeproc-php", - "version": "v2.4.1", + "version": "v2.5.1", "source": { "type": "git", "url": "https://github.com/seboettg/citeproc-php.git", - "reference": "53e94194a8269adf87cbf95a2d47876baf91f5d8" + "reference": "3fbaaaca2ea061fd4c51e7880e835d6a669e6fee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/seboettg/citeproc-php/zipball/53e94194a8269adf87cbf95a2d47876baf91f5d8", - "reference": "53e94194a8269adf87cbf95a2d47876baf91f5d8", + "url": "https://api.github.com/repos/seboettg/citeproc-php/zipball/3fbaaaca2ea061fd4c51e7880e835d6a669e6fee", + "reference": "3fbaaaca2ea061fd4c51e7880e835d6a669e6fee", "shasum": "" }, "require": { "ext-intl": "*", "ext-json": "*", + "ext-mbstring": "*", "ext-simplexml": "*", - "myclabs/php-enum": "^1.5", - "php": ">=7.1", - "seboettg/collection": "^2.1", - "symfony/polyfill-mbstring": "^1.10" + "myclabs/php-enum": "^1.8", + "php": ">=7.3", + "seboettg/collection": ">=v3.1.0" }, "require-dev": { "php-coveralls/php-coveralls": "^1", "phpmd/phpmd": "^2.8", - "phpunit/phpunit": "^7.5", + "phpunit/phpunit": "^8.5", "squizlabs/php_codesniffer": "^3.5" }, + "suggest": { + "symfony/polyfill-mbstring": "^1.10" + }, "type": "library", "autoload": { - "psr-4": { - "Seboettg\\CiteProc\\": "src/" - }, "files": [ "src/functions.php" - ] + ], + "psr-4": { + "Seboettg\\CiteProc\\": "src/" + } }, "notification-url": "https://packagist.org/downloads/", "license": [ @@ -122,40 +128,43 @@ { "name": "Sebastian Böttger", "email": "seboettg@gmail.com", - "homepage": "http://sebastianboettger.net", + "homepage": "https://sebastianboettger.net", "role": "Developer" } ], "description": "Full-featured CSL processor (https://citationstyles.org)", "support": { "issues": "https://github.com/seboettg/citeproc-php/issues", - "source": "https://github.com/seboettg/citeproc-php/tree/v2.2.5" + "source": "https://github.com/seboettg/citeproc-php/tree/v2.5.1" }, - "time": "2020-12-06T10:15:17+00:00" + "time": "2022-08-05T20:11:50+00:00" }, { "name": "seboettg/collection", - "version": "v2.1.1", + "version": "v3.1.0", "source": { "type": "git", "url": "https://github.com/seboettg/Collection.git", - "reference": "292a342c557e9c5b970a3736d3f6f987699a49de" + "reference": "6f753aef75923965173dcb11696e5dece0533f45" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/seboettg/Collection/zipball/292a342c557e9c5b970a3736d3f6f987699a49de", - "reference": "292a342c557e9c5b970a3736d3f6f987699a49de", + "url": "https://api.github.com/repos/seboettg/Collection/zipball/6f753aef75923965173dcb11696e5dece0533f45", + "reference": "6f753aef75923965173dcb11696e5dece0533f45", "shasum": "" }, "require": { - "php": ">=7.1" + "php": ">=7.3" }, "require-dev": { - "phpunit/phpunit": "6.5.*", - "satooshi/php-coveralls": "2.*" + "php-coveralls/php-coveralls": "^1", + "phpunit/phpunit": "8.5.*" }, "type": "library", "autoload": { + "files": [ + "src/ArrayList/Functions.php" + ], "psr-4": { "Seboettg\\Collection\\": "src/" } @@ -189,89 +198,9 @@ ], "support": { "issues": "https://github.com/seboettg/Collection/issues", - "source": "https://github.com/seboettg/Collection/tree/v2.1.1" + "source": "https://github.com/seboettg/Collection/tree/v3.1.0" }, - "time": "2021-05-10T23:27:19+00:00" - }, - { - "name": "symfony/polyfill-mbstring", - "version": "v1.23.1", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9174a3d80210dca8daa7f31fec659150bbeabfc6", - "reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6", - "shasum": "" - }, - "require": { - "php": ">=7.1" - }, - "suggest": { - "ext-mbstring": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "1.23-dev" - }, - "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for the Mbstring extension", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "mbstring", - "polyfill", - "portable", - "shim" - ], - "support": { - "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.23.1" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2021-05-27T12:26:48+00:00" + "time": "2022-07-09T19:47:27+00:00" } ], "packages-dev": [], @@ -287,5 +216,5 @@ "platform-overrides": { "php": "7.2.0" }, - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.3.0" }