From 068bdd02262880cef92b1caa6198cf71e9f5e2cf Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 17 Aug 2023 17:12:48 +0200 Subject: [PATCH 01/59] Separation of the formats regarding the format xslt files and made the formats configurable via ini file. --- application/configs/application.ini | 14 +++ modules/oai/controllers/IndexController.php | 11 ++- modules/oai/models/Configuration.php | 41 ++++++++ modules/oai/models/Server.php | 12 ++- modules/oai/models/ServerFactory.php | 95 +++++++++++++++++++ modules/oai/views/scripts/index/oai-pmh.xslt | 7 -- .../scripts/index/prefixes/XMetaDissPlus.xslt | 2 + .../scripts/index/prefixes/copy_xml.xslt | 2 + .../views/scripts/index/prefixes/epicur.xslt | 1 + .../views/scripts/index/prefixes/marc21.xslt | 2 + .../views/scripts/index/prefixes/oai_dc.xslt | 2 + .../views/scripts/index/prefixes/oai_pp.xslt | 1 + 12 files changed, 177 insertions(+), 13 deletions(-) create mode 100644 modules/oai/models/ServerFactory.php diff --git a/application/configs/application.ini b/application/configs/application.ini index b657b6d02..86858bbe1 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -102,6 +102,20 @@ oai.description.eprints.submissionPolicy.text = oai.description.eprints.comment.url = oai.description.eprints.comment.text = +; Configuration of the oai formats +; Optional custom oai server class name +; oai.format.[lower cased format identifier].class = [oai server class name] +; The xslt file +; oai.format.[lower cased format identifier ].xsltFile = [xslt file name] +; The format prefix +; oai.format.[lower cased format identifier ].prefix = [prefix] +oai.format.copy_xml.xsltFile = copy_xml.xslt +oai.format.epicur.xsltFile = epicur.xslt +oai.format.marc21.xsltFile = marc21.xslt +oai.format.oai_dc.xsltFile = oai_dc.xslt +oai.format.oai_pp.xsltFile = oai_pp.xslt +oai.format.xmetadissplus.xsltFile = XMetaDissPlus.xslt + ; ERROR CONTROLLER SETTINGS errorController.showException = 0 errorController.showRequest = 0 diff --git a/modules/oai/controllers/IndexController.php b/modules/oai/controllers/IndexController.php index 5c2705a78..ce1865ad8 100644 --- a/modules/oai/controllers/IndexController.php +++ b/modules/oai/controllers/IndexController.php @@ -77,7 +77,16 @@ public function indexAction() unset($parameters[$name]); } - $server = new Oai_Model_Server(); // TODO needs factory + $serverFactory = new Oai_Model_ServerFactory(); + + if (isset($parameters['metadataPrefix'])) { + $server = $serverFactory->create($parameters['metadataPrefix']); + } elseif (isset($parameters['resumptionToken'])) { + $server = $serverFactory->createByResumptionToken($parameters['resumptionToken']); + } else { + $server = $serverFactory->create(); + } + $server->setScriptPath($this->view->getScriptPath('index')); $server->setBaseUrl($this->view->fullUrl()); $server->setBaseUri($request->getBaseUrl()); diff --git a/modules/oai/models/Configuration.php b/modules/oai/models/Configuration.php index 25ebdfa9a..5edba215c 100644 --- a/modules/oai/models/Configuration.php +++ b/modules/oai/models/Configuration.php @@ -92,6 +92,13 @@ class Oai_Model_Configuration */ private $oaiBaseUrl = ''; + /** + * Holds the configuration for the formats + * + * @var mixed + */ + private $oaiFormat; + /** * Collect configuration information from Zend_Config instance. * @@ -131,6 +138,10 @@ public function __construct(Zend_Config $config) if (true === isset($config->mail->opus->address)) { $this->emailContact = $config->mail->opus->address; } + + if (true === isset($config->oai->format)) { + $this->oaiFormat = $config->oai->format; + } } /** @@ -212,4 +223,34 @@ public function getMaxListRecords() { return $this->maxListRecs; } + + /** + * Reads the custom oai server class from the configuration if exists. + * + * @param string $metadataPrefix + * @return string + */ + public function getCustomServerClassName($metadataPrefix) + { + if (isset($this->oaiFormat->$metadataPrefix->class)) { + return $this->oaiFormat->$metadataPrefix->class; + } + + return ''; + } + + /** + * Gets the name of the format style sheet file for the given metadata prefix + * + * @param string $metadataPrefix + * @return string + */ + public function getPrefixStyleSheetName($metadataPrefix) + { + if (isset($this->oaiFormat->$metadataPrefix->xsltFile)) { + return $this->oaiFormat->$metadataPrefix->xsltFile; + } + + return ''; + } } diff --git a/modules/oai/models/Server.php b/modules/oai/models/Server.php index 61dcab4ec..896a3960e 100644 --- a/modules/oai/models/Server.php +++ b/modules/oai/models/Server.php @@ -163,9 +163,6 @@ protected function handleRequestIntern($oaiRequest, $requestUri) { $this->init(); - // Setup stylesheet - $this->loadStyleSheet($this->getScriptPath() . '/oai-pmh.xslt'); - $this->setupProcessor(); $metadataPrefixPath = $this->getScriptPath() . DIRECTORY_SEPARATOR . 'prefixes'; @@ -231,13 +228,18 @@ protected function handleRequestIntern($oaiRequest, $requestUri) throw new Exception('The verb provided in the request is illegal.', Oai_Model_Error::BADVERB); } - $doc = $this->proc->transformToDoc($this->xml); - // Requests with resumptionToken do not provide metadataPrefix in the URL if ($metadataPrefix === null && isset($oaiRequest['metadataPrefixMode'])) { $metadataPrefix = $oaiRequest['metadataPrefixMode']; } + // Setup stylesheet + $this->loadStyleSheet( + $this->getScriptPath() . '/prefixes/' . $this->configuration->getPrefixStyleSheetName($metadataPrefix) + ); + + $doc = $this->proc->transformToDoc($this->xml); + // TODO is this something that should happen for all metadataPrefixes (OPUSVIER-4531) $metadataPrefixTags = [ 'oai_dc' => 'dc', diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php new file mode 100644 index 000000000..96ee8847b --- /dev/null +++ b/modules/oai/models/ServerFactory.php @@ -0,0 +1,95 @@ +getConfig()); + $serverClass = $configuration->getCustomServerClassName(strtolower($metaDataPrefix)); + + if (empty($serverClass)) { + return new Oai_Model_Server(); + } + + $classExists = ClassLoaderHelper::classExists($serverClass); + + if (! $classExists) { + return new Oai_Model_Server(); + } + + return new $serverClass(); + } + + /** + * Creates an oai server model by resumption token + * + * @param string $resumptionToken + * @return Oai_Model_Server + */ + public function createByResumptionToken($resumptionToken) + { + $config = $this->getConfig(); + + if (true === isset($config->workspacePath)) { + $tempPath = $config->workspacePath + . DIRECTORY_SEPARATOR . 'tmp' + . DIRECTORY_SEPARATOR . 'resumption'; + } + $tokenWorker = new Oai_Model_Resumptiontokens(); + $tokenWorker->setResumptionPath($tempPath); + $token = $tokenWorker->getResumptionToken($resumptionToken); + + if ($token === null) { + throw new Oai_Model_Exception("file could not be read.", Oai_Model_Error::BADRESUMPTIONTOKEN); + } + + return $this->create($token->getMetadataPrefix()); + } +} diff --git a/modules/oai/views/scripts/index/oai-pmh.xslt b/modules/oai/views/scripts/index/oai-pmh.xslt index 9da7963ca..c298b6a85 100644 --- a/modules/oai/views/scripts/index/oai-pmh.xslt +++ b/modules/oai/views/scripts/index/oai-pmh.xslt @@ -43,13 +43,6 @@ - - - - - - - diff --git a/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt b/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt index f041f1cbe..2c2a8d7d7 100644 --- a/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt +++ b/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt @@ -59,6 +59,8 @@ + + + + diff --git a/modules/oai/views/scripts/index/prefixes/epicur.xslt b/modules/oai/views/scripts/index/prefixes/epicur.xslt index 6222960d0..36c9c9be6 100644 --- a/modules/oai/views/scripts/index/prefixes/epicur.xslt +++ b/modules/oai/views/scripts/index/prefixes/epicur.xslt @@ -45,6 +45,7 @@ + + + diff --git a/modules/oai/views/scripts/index/prefixes/oai_dc.xslt b/modules/oai/views/scripts/index/prefixes/oai_dc.xslt index c684b255a..0d37de7d5 100644 --- a/modules/oai/views/scripts/index/prefixes/oai_dc.xslt +++ b/modules/oai/views/scripts/index/prefixes/oai_dc.xslt @@ -46,6 +46,8 @@ + + diff --git a/modules/oai/views/scripts/index/prefixes/oai_pp.xslt b/modules/oai/views/scripts/index/prefixes/oai_pp.xslt index 91ff37111..a10f596b1 100644 --- a/modules/oai/views/scripts/index/prefixes/oai_pp.xslt +++ b/modules/oai/views/scripts/index/prefixes/oai_pp.xslt @@ -44,6 +44,7 @@ + Date: Sun, 20 Aug 2023 16:30:20 +0200 Subject: [PATCH 02/59] #766 Refactor configuration, added options (currently only xslt file) for the oai server, added some tests. --- application/configs/application.ini | 1 + modules/oai/models/Configuration.php | 30 ++++-- modules/oai/models/Server.php | 48 ++++++++-- modules/oai/models/ServerEpicur.php | 33 +++++++ modules/oai/models/ServerFactory.php | 21 ++--- .../modules/oai/models/ConfigurationTest.php | 93 +++++++++++++++++++ tests/support/OaiDcServer.php | 33 +++++++ 7 files changed, 231 insertions(+), 28 deletions(-) create mode 100644 modules/oai/models/ServerEpicur.php create mode 100644 tests/modules/oai/models/ConfigurationTest.php create mode 100644 tests/support/OaiDcServer.php diff --git a/application/configs/application.ini b/application/configs/application.ini index 86858bbe1..3411a6faa 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -109,6 +109,7 @@ oai.description.eprints.comment.text = ; oai.format.[lower cased format identifier ].xsltFile = [xslt file name] ; The format prefix ; oai.format.[lower cased format identifier ].prefix = [prefix] +oai.format.default.class = Oai_Model_Server oai.format.copy_xml.xsltFile = copy_xml.xslt oai.format.epicur.xsltFile = epicur.xslt oai.format.marc21.xsltFile = marc21.xslt diff --git a/modules/oai/models/Configuration.php b/modules/oai/models/Configuration.php index 5edba215c..91b76c6ce 100644 --- a/modules/oai/models/Configuration.php +++ b/modules/oai/models/Configuration.php @@ -230,27 +230,41 @@ public function getMaxListRecords() * @param string $metadataPrefix * @return string */ - public function getCustomServerClassName($metadataPrefix) + public function getFormatClassName($metadataPrefix) { - if (isset($this->oaiFormat->$metadataPrefix->class)) { + $metadataPrefix = strtolower($metadataPrefix); + + if ($metadataPrefix && isset($this->oaiFormat->$metadataPrefix->class)) { return $this->oaiFormat->$metadataPrefix->class; } + if (isset($this->oaiFormat->default->class)) { + return $this->oaiFormat->default->class; + } + return ''; } /** - * Gets the name of the format style sheet file for the given metadata prefix + * Gets the options for the oai format. * * @param string $metadataPrefix - * @return string + * @return array */ - public function getPrefixStyleSheetName($metadataPrefix) + public function getFormatOptions($metadataPrefix) { - if (isset($this->oaiFormat->$metadataPrefix->xsltFile)) { - return $this->oaiFormat->$metadataPrefix->xsltFile; + $metadataPrefix = strtolower($metadataPrefix); + + $options = []; + + if (isset($this->oaiFormat->$metadataPrefix)) { + foreach ($this->oaiFormat->$metadataPrefix as $key => $value) { + if (strtolower($key) !== 'class') { + $options[$key] = $value; + } + } } - return ''; + return $options; } } diff --git a/modules/oai/models/Server.php b/modules/oai/models/Server.php index 896a3960e..d6637439a 100644 --- a/modules/oai/models/Server.php +++ b/modules/oai/models/Server.php @@ -97,6 +97,13 @@ class Oai_Model_Server extends Application_Model_Abstract /** @var Zend_Controller_Response_Http */ private $response; // TODO temporary hack + /** + * Hold oai format options. + * + * @var array + */ + protected $options; + /** * Gather configuration before action handling. */ @@ -163,6 +170,19 @@ protected function handleRequestIntern($oaiRequest, $requestUri) { $this->init(); + // Setup stylesheet + $sheetFile = $this->getOption('xsltFile'); + if ($sheetFile) { + $this->loadStyleSheet( + $this->getScriptPath() . '/prefixes/' . $sheetFile + ); + } else { + // Setup stylesheet + $this->loadStyleSheet( + $this->getScriptPath() . '/oai-pmh.xslt' + ); + } + $this->setupProcessor(); $metadataPrefixPath = $this->getScriptPath() . DIRECTORY_SEPARATOR . 'prefixes'; @@ -228,18 +248,13 @@ protected function handleRequestIntern($oaiRequest, $requestUri) throw new Exception('The verb provided in the request is illegal.', Oai_Model_Error::BADVERB); } + $doc = $this->proc->transformToDoc($this->xml); + // Requests with resumptionToken do not provide metadataPrefix in the URL if ($metadataPrefix === null && isset($oaiRequest['metadataPrefixMode'])) { $metadataPrefix = $oaiRequest['metadataPrefixMode']; } - // Setup stylesheet - $this->loadStyleSheet( - $this->getScriptPath() . '/prefixes/' . $this->configuration->getPrefixStyleSheetName($metadataPrefix) - ); - - $doc = $this->proc->transformToDoc($this->xml); - // TODO is this something that should happen for all metadataPrefixes (OPUSVIER-4531) $metadataPrefixTags = [ 'oai_dc' => 'dc', @@ -877,4 +892,23 @@ public function getResponse() { return $this->response; } + + /** + * @param array $options + */ + public function setOptions($options) + { + $this->options = $options; + } + + /** + * Gets a single format option by its key + * + * @param string $key + * @return bool|string + */ + protected function getOption($key) + { + return $this->options[$key] ?? ''; + } } diff --git a/modules/oai/models/ServerEpicur.php b/modules/oai/models/ServerEpicur.php new file mode 100644 index 000000000..ac31fd554 --- /dev/null +++ b/modules/oai/models/ServerEpicur.php @@ -0,0 +1,33 @@ +getConfig()); - $serverClass = $configuration->getCustomServerClassName(strtolower($metaDataPrefix)); + $serverClass = $configuration->getFormatClassName($metaDataPrefix); + $formatOptions = $configuration->getFormatOptions($metaDataPrefix); - if (empty($serverClass)) { - return new Oai_Model_Server(); + if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { + $server = new Oai_Model_Server(); + } else { + $server = new $serverClass(); } - $classExists = ClassLoaderHelper::classExists($serverClass); - - if (! $classExists) { - return new Oai_Model_Server(); - } + $server->setOptions($formatOptions); - return new $serverClass(); + return $server; } /** diff --git a/tests/modules/oai/models/ConfigurationTest.php b/tests/modules/oai/models/ConfigurationTest.php new file mode 100644 index 000000000..92a4c4852 --- /dev/null +++ b/tests/modules/oai/models/ConfigurationTest.php @@ -0,0 +1,93 @@ +adjustConfiguration( + [ + 'oai' => [ + 'format' => [ + 'default' => [ + 'class' => 'DefaultServer', + ], + 'epicur' => [ + 'class' => 'Oai_Model_EpicurServer', + 'xsltFile' => 'epicurFile.xslt', + ], + 'oai_dc' => null, + ], + ], + ] + ); + } + + public function testGetServerClass() + { + $configuration = new Oai_Model_Configuration($this->getConfig()); + + $metadDataPrefix = 'epicur'; + $serverClass = $configuration->getFormatClassName($metadDataPrefix); + $this->assertEquals('Oai_Model_EpicurServer', $serverClass); + + $metadDataPrefix = 'EpiCur'; + $serverClass = $configuration->getFormatClassName($metadDataPrefix); + $this->assertEquals('Oai_Model_EpicurServer', $serverClass); + + $metadDataPrefix = 'unknown'; + $serverClass = $configuration->getFormatClassName($metadDataPrefix); + $this->assertEquals('DefaultServer', $serverClass); + } + + public function testGetOptions() + { + $configuration = new Oai_Model_Configuration($this->getConfig()); + + $expectedOptions = [ + 'xsltFile' => 'epicurFile.xslt', + ]; + + $metadDataPrefix = 'epicur'; + $options = $configuration->getFormatOptions($metadDataPrefix); + $this->assertEquals($expectedOptions, $options); + + $metadDataPrefix = 'EpiCur'; + $options = $configuration->getFormatOptions($metadDataPrefix); + $this->assertEquals($expectedOptions, $options); + + $metadDataPrefix = 'unknown'; + $options = $configuration->getFormatOptions($metadDataPrefix); + $this->assertEquals([], $options); + } +} diff --git a/tests/support/OaiDcServer.php b/tests/support/OaiDcServer.php new file mode 100644 index 000000000..d3c89cde2 --- /dev/null +++ b/tests/support/OaiDcServer.php @@ -0,0 +1,33 @@ + Date: Wed, 23 Aug 2023 17:25:22 +0200 Subject: [PATCH 03/59] #766 Including the prefix xslt in the base xslt instead of the other way around. --- modules/oai/models/Server.php | 35 +++++++++---------- .../scripts/index/prefixes/XMetaDissPlus.xslt | 2 -- .../scripts/index/prefixes/copy_xml.xslt | 2 -- .../views/scripts/index/prefixes/epicur.xslt | 2 -- .../views/scripts/index/prefixes/marc21.xslt | 2 -- .../views/scripts/index/prefixes/oai_dc.xslt | 2 -- .../views/scripts/index/prefixes/oai_pp.xslt | 2 -- 7 files changed, 17 insertions(+), 30 deletions(-) diff --git a/modules/oai/models/Server.php b/modules/oai/models/Server.php index d6637439a..97532ff7c 100644 --- a/modules/oai/models/Server.php +++ b/modules/oai/models/Server.php @@ -169,20 +169,7 @@ public function handleRequest($parameters, $requestUri) protected function handleRequestIntern($oaiRequest, $requestUri) { $this->init(); - - // Setup stylesheet - $sheetFile = $this->getOption('xsltFile'); - if ($sheetFile) { - $this->loadStyleSheet( - $this->getScriptPath() . '/prefixes/' . $sheetFile - ); - } else { - // Setup stylesheet - $this->loadStyleSheet( - $this->getScriptPath() . '/oai-pmh.xslt' - ); - } - + $this->loadStyleSheet(); $this->setupProcessor(); $metadataPrefixPath = $this->getScriptPath() . DIRECTORY_SEPARATOR . 'prefixes'; @@ -815,13 +802,25 @@ private function getOaiBaseUrl() /** * Load an xslt stylesheet. - * - * @param string $stylesheet */ - protected function loadStyleSheet($stylesheet) + protected function loadStyleSheet() { $this->xslt = new DOMDocument(); - $this->xslt->load($stylesheet); + $this->xslt->load($this->getScriptPath() . '/oai-pmh.xslt'); + + // Replace import comment with prefix import + $prefixXsltFile = $this->getOption('xsltFile'); + if ($prefixXsltFile) { + $xsltXml = $this->xslt->saveXML(); + $xsltXml = preg_replace( + '//u', + '', + $xsltXml + ); + + $this->xslt->loadXML($xsltXml); + } + $this->proc->importStyleSheet($this->xslt); if (isset($_SERVER['HTTP_HOST'])) { $this->proc->setParameter('', 'host', $_SERVER['HTTP_HOST']); diff --git a/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt b/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt index 2c2a8d7d7..f041f1cbe 100644 --- a/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt +++ b/modules/oai/views/scripts/index/prefixes/XMetaDissPlus.xslt @@ -59,8 +59,6 @@ - - - - diff --git a/modules/oai/views/scripts/index/prefixes/epicur.xslt b/modules/oai/views/scripts/index/prefixes/epicur.xslt index 36c9c9be6..6c8e44267 100644 --- a/modules/oai/views/scripts/index/prefixes/epicur.xslt +++ b/modules/oai/views/scripts/index/prefixes/epicur.xslt @@ -45,8 +45,6 @@ - - - - diff --git a/modules/oai/views/scripts/index/prefixes/oai_dc.xslt b/modules/oai/views/scripts/index/prefixes/oai_dc.xslt index 0d37de7d5..c684b255a 100644 --- a/modules/oai/views/scripts/index/prefixes/oai_dc.xslt +++ b/modules/oai/views/scripts/index/prefixes/oai_dc.xslt @@ -46,8 +46,6 @@ - - diff --git a/modules/oai/views/scripts/index/prefixes/oai_pp.xslt b/modules/oai/views/scripts/index/prefixes/oai_pp.xslt index a10f596b1..1d7e18098 100644 --- a/modules/oai/views/scripts/index/prefixes/oai_pp.xslt +++ b/modules/oai/views/scripts/index/prefixes/oai_pp.xslt @@ -44,8 +44,6 @@ - - From 83a0a4eaef533522eb1add8ae8ee38d2d6cc2fb7 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 25 Aug 2023 16:00:11 +0200 Subject: [PATCH 04/59] #766 ServerFactory directly reads oai options from configuration and injects them into the oai server. XsltFilePath can be absolute and overwritten by a derived server class. --- application/configs/application.ini | 2 +- .../oai/models/{Server.php => BaseServer.php} | 249 ++++++++++++---- modules/oai/models/Configuration.php | 270 ------------------ modules/oai/models/OptionsTrait.php | 58 ++++ modules/oai/models/ServerFactory.php | 101 ++++++- .../marcxml/MarcXmlServer.php} | 8 +- tests/modules/oai/format/EpicurTest.php | 21 ++ .../modules/oai/models/ConfigurationTest.php | 93 ------ .../modules/oai/models/ServerFactoryTest.php | 154 ++++++++++ tests/support/OaiDcServer.php | 2 +- 10 files changed, 530 insertions(+), 428 deletions(-) rename modules/oai/models/{Server.php => BaseServer.php} (86%) delete mode 100644 modules/oai/models/Configuration.php create mode 100644 modules/oai/models/OptionsTrait.php rename modules/oai/models/{ServerEpicur.php => prefix/marcxml/MarcXmlServer.php} (88%) delete mode 100644 tests/modules/oai/models/ConfigurationTest.php create mode 100644 tests/modules/oai/models/ServerFactoryTest.php diff --git a/application/configs/application.ini b/application/configs/application.ini index 3411a6faa..cb7769a2f 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -109,7 +109,7 @@ oai.description.eprints.comment.text = ; oai.format.[lower cased format identifier ].xsltFile = [xslt file name] ; The format prefix ; oai.format.[lower cased format identifier ].prefix = [prefix] -oai.format.default.class = Oai_Model_Server +oai.format.default.class = Oai_Model_BaseServer oai.format.copy_xml.xsltFile = copy_xml.xslt oai.format.epicur.xsltFile = epicur.xslt oai.format.marc21.xsltFile = marc21.xslt diff --git a/modules/oai/models/Server.php b/modules/oai/models/BaseServer.php similarity index 86% rename from modules/oai/models/Server.php rename to modules/oai/models/BaseServer.php index 97532ff7c..458c7509f 100644 --- a/modules/oai/models/Server.php +++ b/modules/oai/models/BaseServer.php @@ -37,8 +37,10 @@ use Opus\Model\Xml; use Opus\Model\Xml\Version1; -class Oai_Model_Server extends Application_Model_Abstract +class Oai_Model_BaseServer extends Application_Model_Abstract { + use Oai_Model_OptionsTrait; + /** * Holds xml representation of document information to be processed. * @@ -75,13 +77,6 @@ class Oai_Model_Server extends Application_Model_Abstract */ private $xMetaDissRestriction = ['doctoralthesis', 'habilitation']; - /** - * Hold oai module configuration model. - * - * @var Oai_Model_Configuration - */ - protected $configuration; - /** @var Oai_Model_XmlFactory */ private $xmlFactory; @@ -97,13 +92,6 @@ class Oai_Model_Server extends Application_Model_Abstract /** @var Zend_Controller_Response_Http */ private $response; // TODO temporary hack - /** - * Hold oai format options. - * - * @var array - */ - protected $options; - /** * Gather configuration before action handling. */ @@ -113,7 +101,6 @@ public function init() $this->xml = new DOMDocument(); $this->proc = new XSLTProcessor(); - $this->configuration = new Oai_Model_Configuration($config); $this->xmlFactory = new Oai_Model_XmlFactory(); } @@ -127,7 +114,7 @@ public function init() */ public function handleRequest($parameters, $requestUri) { - // TODO move error handling into Oai_Model_Server + // TODO move error handling into Oai_Model_BaseServer try { // handle request return $this->handleRequestIntern($parameters, $requestUri); @@ -173,7 +160,7 @@ protected function handleRequestIntern($oaiRequest, $requestUri) $this->setupProcessor(); $metadataPrefixPath = $this->getScriptPath() . DIRECTORY_SEPARATOR . 'prefixes'; - $resumptionPath = $this->configuration->getResumptionTokenPath(); + $resumptionPath = $this->getResumptionTokenPath(); $request = new Oai_Model_Request(); $request->setPathToMetadataPrefixFiles($metadataPrefixPath); @@ -354,10 +341,10 @@ protected function handleGetRecord(array &$oaiRequest) */ protected function handleIdentify() { - $email = $this->configuration->getEmailContact(); - $repName = $this->configuration->getRepositoryName(); - $repIdentifier = $this->configuration->getRepositoryIdentifier(); - $sampleIdentifier = $this->configuration->getSampleIdentifier(); + $email = $this->getEmailContact(); + $repName = $this->getRepositoryName(); + $repIdentifier = $this->getRepositoryIdentifier(); + $sampleIdentifier = $this->getSampleIdentifier(); // Set backup date if database query does not return a date. $earliestDate = DateTime::createFromFormat("Y-m-d", '1970-01-01'); @@ -389,7 +376,7 @@ protected function handleIdentify() */ protected function handleListIdentifiers(array &$oaiRequest) { - $maxIdentifier = $this->configuration->getMaxListIdentifiers(); + $maxIdentifier = $this->getMaxListIdentifiers(); $this->handlingOfLists($oaiRequest, $maxIdentifier); } @@ -428,7 +415,7 @@ protected function handleListMetadataFormats($oaiRequest) */ protected function handleListRecords(array &$oaiRequest) { - $maxRecords = $this->configuration->getMaxListRecords(); + $maxRecords = $this->getMaxListRecords(); $this->handlingOfLists($oaiRequest, $maxRecords); } @@ -437,7 +424,7 @@ protected function handleListRecords(array &$oaiRequest) */ protected function handleListSets() { - $repIdentifier = $this->configuration->getRepositoryIdentifier(); + $repIdentifier = $this->getRepositoryIdentifier(); $this->proc->setParameter('', 'repIdentifier', $repIdentifier); $this->xml->appendChild($this->xml->createElement('Documents')); @@ -471,8 +458,8 @@ private function handlingOfLists(array &$oaiRequest, $maxRecords) $maxRecords = 100; } - $repIdentifier = $this->configuration->getRepositoryIdentifier(); - $tempPath = $this->configuration->getResumptionTokenPath(); + $repIdentifier = $this->getRepositoryIdentifier(); + $tempPath = $this->getResumptionTokenPath(); $this->proc->setParameter('', 'repIdentifier', $repIdentifier); $this->xml->appendChild($this->xml->createElement('Documents')); @@ -785,21 +772,6 @@ private function getDocumentXmlDomNode($document) return $xmlModel->getDomDocument()->getElementsByTagName('Opus_Document')->item(0); } - /** - * @return string - */ - private function getOaiBaseUrl() - { - $oaiBaseUrl = $this->configuration->getOaiBaseUrl(); - - // if no OAI base url is set, use local information as base url - if (true === empty($oaiBaseUrl)) { - $oaiBaseUrl = $this->getBaseUrl() . '/oai'; // TODO . $module; - } - - return $oaiBaseUrl; - } - /** * Load an xslt stylesheet. */ @@ -809,12 +781,16 @@ protected function loadStyleSheet() $this->xslt->load($this->getScriptPath() . '/oai-pmh.xslt'); // Replace import comment with prefix import - $prefixXsltFile = $this->getOption('xsltFile'); + $prefixXsltFile = $this->getXsltFile(); + if ($prefixXsltFile) { + if (! file_exists($prefixXsltFile)) { + $prefixXsltFile = $this->getScriptPath() . '/prefixes/' . $prefixXsltFile; + } $xsltXml = $this->xslt->saveXML(); $xsltXml = preg_replace( '//u', - '', + '', $xsltXml ); @@ -893,21 +869,190 @@ public function getResponse() } /** - * @param array $options + * Return temporary path for resumption tokens. + * + * @return string Path. + */ + public function getResumptionTokenPath() + { + return $this->options['resumptionTokenPath'] ?? ''; + } + + /** + * Sets the temporary path for resumption tokens. + * + * @param string $path + */ + public function setResumptionTokenPath($path) + { + $this->options['resumptionTokenPath'] = $path; + } + + /** + * Return contact email address. + * + * @return string Email address. + */ + public function getEmailContact() + { + return $this->options['emailContact'] ?? ''; + } + + /** + * Sets the contact email address. + * + * @param string $email + */ + public function setEmailContact($email) + { + $this->options['emailContact'] = $email; + } + + /** + * Return OAI base url. + * + * @return string Oai base url. + */ + private function getOaiBaseUrl() + { + $oaiBaseUrl = $this->options['oaiBaseUrl'] ?? ''; + + // if no OAI base url is set, use local information as base url + if (true === empty($oaiBaseUrl)) { + $oaiBaseUrl = $this->getBaseUrl() . '/oai'; // TODO . $module; + } + + return $oaiBaseUrl; + } + + /** + * Sets the OAI base url. + * + * @param string $url + */ + public function setOaiBaseUrl($url) + { + $this->options['oaiBaseUrl'] = $url; + } + + /** + * Return repository name. + * + * @return string Repository name. + */ + private function getRepositoryName() + { + return $this->options['repositoryName'] ?? ''; + } + + /** + * Sets the repository name. + * + * @param string $repoName + */ + public function setRepositoryName($repoName) + { + $this->options['repositoryName'] = $repoName; + } + + /** + * Return repository identifier. + * + * @return string Repository identifier. + */ + private function getRepositoryIdentifier() + { + return $this->options['repositoryIdentifier'] ?? ''; + } + + /** + * Sets the repository identifier. + * + * @param string $repoId + * @return void + */ + public function setRepositoryIdentifier($repoId) + { + $this->options['repositoryIdentifier'] = $repoId; + } + + /** + * Return sample identifier. + * + * @return string Sample identifier. + */ + private function getSampleIdentifier() + { + return $this->options['sampleIdentifier'] ?? ''; + } + + /** + * Sets the sample identifier. + * + * @param string $sampleId + */ + public function setSampleIdentifier($sampleId) + { + $this->options['sampleIdentifier'] = $sampleId; + } + + /** + * Return maximum number of listable identifiers per request. + * + * @return int Maximum number of listable identifiers per request. + */ + private function getMaxListIdentifiers() + { + return $this->options['maxListIdentifiers'] ?? ''; + } + + /** + * Sets the maximum number of listable identifiers per request. + * + * @param int $maxListIds + */ + public function setMaxListIdentifiers($maxListIds) + { + $this->options['maxListIdentifiers'] = $maxListIds; + } + + /** + * Return maximum number of listable records per request. + * + * @return int Maximum number of listable records per request. + */ + private function getMaxListRecords() + { + return $this->options['maxListRecords'] ?? ''; + } + + /** + * Sets the maximum number of listable records per request. + * + * @param int $maxListRecs + */ + public function setMaxListRecords($maxListRecs) + { + $this->options['maxListRecords'] = $maxListRecs; + } + + /** + * Return xlst file name / file path. + * + * @return string */ - public function setOptions($options) + protected function getXsltFile() { - $this->options = $options; + return $this->options['xsltFile'] ?? ''; } /** - * Gets a single format option by its key + * Sets the xlst file name / file path. * - * @param string $key - * @return bool|string + * @param string */ - protected function getOption($key) + public function setXsltFile($file) { - return $this->options[$key] ?? ''; + $this->options['xsltFile'] = $file; } } diff --git a/modules/oai/models/Configuration.php b/modules/oai/models/Configuration.php deleted file mode 100644 index 91b76c6ce..000000000 --- a/modules/oai/models/Configuration.php +++ /dev/null @@ -1,270 +0,0 @@ -oai)) { - throw new Exception('No configuration for module oai.'); - } - - if (true === isset($config->oai->repository->name)) { - $this->repoName = $config->oai->repository->name; - } - if (true === isset($config->oai->repository->identifier)) { - $this->repoId = $config->oai->repository->identifier; - } - if (true === isset($config->oai->sample->identifier)) { - $this->sampleId = $config->oai->sample->identifier; - } - if (true === isset($config->oai->max->listidentifiers)) { - $this->maxListIds = $config->oai->max->listidentifiers; - } - if (true === isset($config->oai->max->listrecords)) { - $this->maxListRecs = $config->oai->max->listrecords; - } - if (true === isset($config->oai->baseurl)) { - $this->oaiBaseUrl = $config->oai->baseurl; - } - - if (true === isset($config->workspacePath)) { - $this->pathTokens = $config->workspacePath - . DIRECTORY_SEPARATOR . 'tmp' - . DIRECTORY_SEPARATOR . 'resumption'; - } - - if (true === isset($config->mail->opus->address)) { - $this->emailContact = $config->mail->opus->address; - } - - if (true === isset($config->oai->format)) { - $this->oaiFormat = $config->oai->format; - } - } - - /** - * Return temporary path for resumption tokens. - * - * @return string Path. - */ - public function getResumptionTokenPath() - { - return $this->pathTokens; - } - - /** - * Return contact email address. - * - * @return string Email address. - */ - public function getEmailContact() - { - return $this->emailContact; - } - - /** - * Return OAI base url. - * - * @return string Oai base url. - */ - public function getOaiBaseUrl() - { - return $this->oaiBaseUrl; - } - - /** - * Return repository name. - * - * @return string Repository name. - */ - public function getRepositoryName() - { - return $this->repoName; - } - - /** - * Return repository identifier. - * - * @return string Repository identifier. - */ - public function getRepositoryIdentifier() - { - return $this->repoId; - } - - /** - * Return sample identifier. - * - * @return string Sample identifier. - */ - public function getSampleIdentifier() - { - return $this->sampleId; - } - - /** - * Return maximum number of listable identifiers per request. - * - * @return int Maximum number of listable identifiers per request. - */ - public function getMaxListIdentifiers() - { - return $this->maxListIds; - } - - /** - * Return maximum number of listable records per request. - * - * @return int Maximum number of listable records per request. - */ - public function getMaxListRecords() - { - return $this->maxListRecs; - } - - /** - * Reads the custom oai server class from the configuration if exists. - * - * @param string $metadataPrefix - * @return string - */ - public function getFormatClassName($metadataPrefix) - { - $metadataPrefix = strtolower($metadataPrefix); - - if ($metadataPrefix && isset($this->oaiFormat->$metadataPrefix->class)) { - return $this->oaiFormat->$metadataPrefix->class; - } - - if (isset($this->oaiFormat->default->class)) { - return $this->oaiFormat->default->class; - } - - return ''; - } - - /** - * Gets the options for the oai format. - * - * @param string $metadataPrefix - * @return array - */ - public function getFormatOptions($metadataPrefix) - { - $metadataPrefix = strtolower($metadataPrefix); - - $options = []; - - if (isset($this->oaiFormat->$metadataPrefix)) { - foreach ($this->oaiFormat->$metadataPrefix as $key => $value) { - if (strtolower($key) !== 'class') { - $options[$key] = $value; - } - } - } - - return $options; - } -} diff --git a/modules/oai/models/OptionsTrait.php b/modules/oai/models/OptionsTrait.php new file mode 100644 index 000000000..63c434839 --- /dev/null +++ b/modules/oai/models/OptionsTrait.php @@ -0,0 +1,58 @@ +options) ? $this->options : []; + } + + /** + * @param array $options + */ + public function setOptions($options) + { + // TODO How to react if a setter does not exist? + foreach ($options as $key => $value) { + $method = 'set' . ucfirst($key); + if (method_exists($this, $method) && $value) { + $this->$method($value); + } + } + } +} diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index cbb9b3a41..c99986dbf 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -43,21 +43,20 @@ class Oai_Model_ServerFactory * Creates an oai server model by metaDataPrefix * * @param string $metaDataPrefix - * @return Oai_Model_Server + * @return Oai_Model_BaseServer */ public function create($metaDataPrefix = '') { - $configuration = new Oai_Model_Configuration($this->getConfig()); - $serverClass = $configuration->getFormatClassName($metaDataPrefix); - $formatOptions = $configuration->getFormatOptions($metaDataPrefix); + $serverClass = $this->getFormatClassName($metaDataPrefix); - if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { - $server = new Oai_Model_Server(); + if (empty($serverClass) || !ClassLoaderHelper::classExists($serverClass)) { + $server = new Oai_Model_BaseServer(); } else { $server = new $serverClass(); } - $server->setOptions($formatOptions); + $options = $this->getFormatOptions($metaDataPrefix); + $server->setOptions($options); return $server; } @@ -66,13 +65,13 @@ public function create($metaDataPrefix = '') * Creates an oai server model by resumption token * * @param string $resumptionToken - * @return Oai_Model_Server + * @return Oai_Model_BaseServer */ public function createByResumptionToken($resumptionToken) { $config = $this->getConfig(); - if (true === isset($config->workspacePath)) { + if (isset($config->workspacePath)) { $tempPath = $config->workspacePath . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'resumption'; @@ -87,4 +86,88 @@ public function createByResumptionToken($resumptionToken) return $this->create($token->getMetadataPrefix()); } + + /** + * Reads the custom oai server class from the configuration if exists. + * + * @param string $metadataPrefix + * @return string + */ + public function getFormatClassName($metadataPrefix) + { + $config = $this->getConfig(); + + if (! isset($config->oai)) { + throw new Exception('No configuration for module oai.'); + } + + $metadataPrefix = strtolower($metadataPrefix); + + if ($metadataPrefix && isset($config->oai->format->$metadataPrefix->class)) { + return $config->oai->format->$metadataPrefix->class; + } + + if (isset($config->oai->format->default->class)) { + return $config->oai->format->default->class; + } + + return ''; + } + + /** + * Gets all options for the oai server + * + * @param string $metadataPrefix + * @return array + */ + public function getFormatOptions($metadataPrefix = '') + { + $metadataPrefix = strtolower($metadataPrefix); + + $config = $this->getConfig(); + + if (! isset($config->oai)) { + throw new Exception('No configuration for module oai.'); + } + + $options = []; + + if (isset($config->oai->repository->name)) { + $options['repositoryName'] = $config->oai->repository->name; + } + if (isset($config->oai->repository->identifier)) { + $options['repositoryIdentifier'] = $config->oai->repository->identifier; + } + if (true === isset($config->oai->sample->identifier)) { + $options['sampleIdentifier'] = $config->oai->sample->identifier; + } + if (true === isset($config->oai->max->listidentifiers)) { + $options['maxListIdentifiers'] = (int)$config->oai->max->listidentifiers; + } + + if (true === isset($config->oai->max->listrecords)) { + $options['maxListRecords'] = (int)$config->oai->max->listrecords; + } + if (true === isset($config->oai->baseurl)) { + $options['oaiBaseUrl'] = $config->oai->baseurl; + } + + if (true === isset($config->workspacePath)) { + $options['resumptionTokenPath'] = $config->workspacePath + . DIRECTORY_SEPARATOR . 'tmp' + . DIRECTORY_SEPARATOR . 'resumption'; + } + + if (isset($config->mail->opus->address)) { + $options['emailContact'] = $config->mail->opus->address; + } + + $formatOptions = []; + if (isset($config->oai->format->$metadataPrefix)) { + $formatOptions = $config->oai->format->$metadataPrefix->toArray(); + unset($options['class']); + } + + return $formatOptions ? array_merge($options, $formatOptions) : $options; + } } diff --git a/modules/oai/models/ServerEpicur.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php similarity index 88% rename from modules/oai/models/ServerEpicur.php rename to modules/oai/models/prefix/marcxml/MarcXmlServer.php index ac31fd554..322375aa3 100644 --- a/modules/oai/models/ServerEpicur.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -25,9 +25,13 @@ * along with OPUS; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - * @copyright Copyright (c) 2017, OPUS 4 development team + * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class Oai_Model_ServerEpicur extends Oai_Model_Server +class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_BaseServer { + protected function getXsltFile() + { + return "marc21.xslt"; + } } diff --git a/tests/modules/oai/format/EpicurTest.php b/tests/modules/oai/format/EpicurTest.php index ecc24db81..cdc503463 100644 --- a/tests/modules/oai/format/EpicurTest.php +++ b/tests/modules/oai/format/EpicurTest.php @@ -80,4 +80,25 @@ public function testXmlXsiSchemaDeclarationPresentForEpicurMetadata() $this->fail('element \'epicur\' not found'); } } + + public function testConfigurationWithAbsolutXsltFilePath() + { + $this->adjustConfiguration( + [ + 'oai' => [ + 'format' => [ + 'epicur' => [ + 'xsltFile' => '/vagrant/modules/oai/views/scripts/index/prefixes/epicur.xslt', + ], + ], + ], + ] + ); + + $this->dispatch('/oai?verb=GetRecord&metadataPrefix=epicur&identifier=oai::146'); + + $this->registerXpathNamespaces($this->xpathNamespaces); + + $this->assertXpath('//oai:metadata'); + } } diff --git a/tests/modules/oai/models/ConfigurationTest.php b/tests/modules/oai/models/ConfigurationTest.php deleted file mode 100644 index 92a4c4852..000000000 --- a/tests/modules/oai/models/ConfigurationTest.php +++ /dev/null @@ -1,93 +0,0 @@ -adjustConfiguration( - [ - 'oai' => [ - 'format' => [ - 'default' => [ - 'class' => 'DefaultServer', - ], - 'epicur' => [ - 'class' => 'Oai_Model_EpicurServer', - 'xsltFile' => 'epicurFile.xslt', - ], - 'oai_dc' => null, - ], - ], - ] - ); - } - - public function testGetServerClass() - { - $configuration = new Oai_Model_Configuration($this->getConfig()); - - $metadDataPrefix = 'epicur'; - $serverClass = $configuration->getFormatClassName($metadDataPrefix); - $this->assertEquals('Oai_Model_EpicurServer', $serverClass); - - $metadDataPrefix = 'EpiCur'; - $serverClass = $configuration->getFormatClassName($metadDataPrefix); - $this->assertEquals('Oai_Model_EpicurServer', $serverClass); - - $metadDataPrefix = 'unknown'; - $serverClass = $configuration->getFormatClassName($metadDataPrefix); - $this->assertEquals('DefaultServer', $serverClass); - } - - public function testGetOptions() - { - $configuration = new Oai_Model_Configuration($this->getConfig()); - - $expectedOptions = [ - 'xsltFile' => 'epicurFile.xslt', - ]; - - $metadDataPrefix = 'epicur'; - $options = $configuration->getFormatOptions($metadDataPrefix); - $this->assertEquals($expectedOptions, $options); - - $metadDataPrefix = 'EpiCur'; - $options = $configuration->getFormatOptions($metadDataPrefix); - $this->assertEquals($expectedOptions, $options); - - $metadDataPrefix = 'unknown'; - $options = $configuration->getFormatOptions($metadDataPrefix); - $this->assertEquals([], $options); - } -} diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php new file mode 100644 index 000000000..481c57ce4 --- /dev/null +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -0,0 +1,154 @@ + '/vagrant/tests/workspace', + 'mail' => [ + 'opus' => [ + 'address' => 'opus4ci@example.org' + ], + ], + 'oai' => [ + 'max' => [ + 'listrecords' => 10, + 'listidentifiers' => 10, + ], + 'format' => [ + 'default' => [ + 'class' => 'DefaultServerClass' + ], + 'epicur' => [ + 'class' => 'UnknownEpicurClass', + 'xsltFile' => 'epicurFile.xslt', + ], + 'oai_dc' => [ + 'class' => 'OaiDcServer', + ], + 'oai_pp' => null, + ], + ] + ] + ); + + $this->serverFactory = new Oai_Model_ServerFactory(); + $this->serverFactory->setConfig($config); + } + + public function testCreate() + { + $server = $this->serverFactory->create(); + $expectedServer = new Oai_Model_BaseServer(); + $expectedServer->setMaxListIdentifiers(10); + $expectedServer->setMaxListRecords(10); + $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); + $expectedServer->setEmailContact('opus4ci@example.org'); + $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); + $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + + $server = $this->serverFactory->create('epicur'); + $expectedServer = new Oai_Model_BaseServer(); + $expectedServer->setXsltFile('epicurFile.xslt'); + $expectedServer->setMaxListIdentifiers(10); + $expectedServer->setMaxListRecords(10); + $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); + $expectedServer->setEmailContact('opus4ci@example.org'); + $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); + $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + + $server = $this->serverFactory->create('oai_dc'); + $expectedServer = new OaiDcServer(); + $expectedServer->setMaxListIdentifiers(10); + $expectedServer->setMaxListRecords(10); + $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); + $expectedServer->setEmailContact('opus4ci@example.org'); + $this->assertEquals(OaiDcServer::class, get_class($server)); + $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + + $server = $this->serverFactory->create('oai_pp'); + $expectedServer = new Oai_Model_BaseServer(); + $expectedServer->setMaxListIdentifiers(10); + $expectedServer->setMaxListRecords(10); + $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); + $expectedServer->setEmailContact('opus4ci@example.org'); + $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); + $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + } + + public function testGetFormatClassName() + { + $metadDataPrefix = 'epicur'; + $serverClass = $this->serverFactory->getFormatClassName($metadDataPrefix); + $this->assertEquals('UnknownEpicurClass', $serverClass); + + $metadDataPrefix = 'EpiCur'; + $serverClass = $this->serverFactory->getFormatClassName($metadDataPrefix); + $this->assertEquals('UnknownEpicurClass', $serverClass); + + $metadDataPrefix = 'unknown'; + $serverClass = $this->serverFactory->getFormatClassName($metadDataPrefix); + $this->assertEquals('DefaultServerClass', $serverClass); + } + + public function testGetFormatOptions() + { + $expectedOptions = [ + 'xsltFile' => 'epicurFile.xslt', + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'class' => 'UnknownEpicurClass', + ]; + + $metadDataPrefix = 'epicur'; + $options = $this->serverFactory->getFormatOptions($metadDataPrefix); + $this->assertEquals($expectedOptions, $options); + + $metadDataPrefix = 'EpiCur'; + $options = $this->serverFactory->getFormatOptions($metadDataPrefix); + $this->assertEquals($expectedOptions, $options); + + $metadDataPrefix = 'unknown'; + $options = $this->serverFactory->getFormatOptions($metadDataPrefix); + unset($expectedOptions['xsltFile']); + unset($expectedOptions['class']); + $this->assertEquals($expectedOptions, $options); + } +} diff --git a/tests/support/OaiDcServer.php b/tests/support/OaiDcServer.php index d3c89cde2..723cbfe72 100644 --- a/tests/support/OaiDcServer.php +++ b/tests/support/OaiDcServer.php @@ -28,6 +28,6 @@ * @copyright Copyright (c) 2008, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class OaiDcServer extends Oai_Model_Server +class OaiDcServer extends Oai_Model_BaseServer { } From 84e700eeca35893393a5c53269bf504cd5a4b262 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 25 Aug 2023 16:58:30 +0200 Subject: [PATCH 05/59] #766 Fix absolut path for xsltFile --- tests/modules/oai/format/EpicurTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modules/oai/format/EpicurTest.php b/tests/modules/oai/format/EpicurTest.php index cdc503463..76cab5e76 100644 --- a/tests/modules/oai/format/EpicurTest.php +++ b/tests/modules/oai/format/EpicurTest.php @@ -88,7 +88,7 @@ public function testConfigurationWithAbsolutXsltFilePath() 'oai' => [ 'format' => [ 'epicur' => [ - 'xsltFile' => '/vagrant/modules/oai/views/scripts/index/prefixes/epicur.xslt', + 'xsltFile' => getcwd() . '/modules/oai/views/scripts/index/prefixes/epicur.xslt', ], ], ], From d0fcdcda7a524a4213bd7f8b41761ff261430b32 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 25 Aug 2023 17:36:07 +0200 Subject: [PATCH 06/59] #766 Fix some coding style. --- modules/oai/models/BaseServer.php | 8 ++--- modules/oai/models/ServerFactory.php | 6 ++-- .../models/prefix/marcxml/MarcXmlServer.php | 5 +++ .../modules/oai/models/ServerFactoryTest.php | 31 ++++++++++--------- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index 458c7509f..5b4091d4d 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -99,9 +99,9 @@ public function init() { $config = $this->getConfig(); - $this->xml = new DOMDocument(); - $this->proc = new XSLTProcessor(); - $this->xmlFactory = new Oai_Model_XmlFactory(); + $this->xml = new DOMDocument(); + $this->proc = new XSLTProcessor(); + $this->xmlFactory = new Oai_Model_XmlFactory(); } /** @@ -1049,7 +1049,7 @@ protected function getXsltFile() /** * Sets the xlst file name / file path. * - * @param string + * @param string $file */ public function setXsltFile($file) { diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index c99986dbf..1abc8cab1 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -49,7 +49,7 @@ public function create($metaDataPrefix = '') { $serverClass = $this->getFormatClassName($metaDataPrefix); - if (empty($serverClass) || !ClassLoaderHelper::classExists($serverClass)) { + if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { $server = new Oai_Model_BaseServer(); } else { $server = new $serverClass(); @@ -142,11 +142,11 @@ public function getFormatOptions($metadataPrefix = '') $options['sampleIdentifier'] = $config->oai->sample->identifier; } if (true === isset($config->oai->max->listidentifiers)) { - $options['maxListIdentifiers'] = (int)$config->oai->max->listidentifiers; + $options['maxListIdentifiers'] = (int) $config->oai->max->listidentifiers; } if (true === isset($config->oai->max->listrecords)) { - $options['maxListRecords'] = (int)$config->oai->max->listrecords; + $options['maxListRecords'] = (int) $config->oai->max->listrecords; } if (true === isset($config->oai->baseurl)) { $options['oaiBaseUrl'] = $config->oai->baseurl; diff --git a/modules/oai/models/prefix/marcxml/MarcXmlServer.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php index 322375aa3..0c447191f 100644 --- a/modules/oai/models/prefix/marcxml/MarcXmlServer.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -30,6 +30,11 @@ */ class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_BaseServer { + /** + * Return xlst file name / file path. + * + * @return string + */ protected function getXsltFile() { return "marc21.xslt"; diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 481c57ce4..8037cdc0d 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -31,6 +31,7 @@ class Oai_Model_ServerFactoryTest extends ControllerTestCase { + /** @var Oai_Model_ServerFactory */ private $serverFactory; public function setUp(): void @@ -40,30 +41,30 @@ public function setUp(): void $config = new Zend_Config( [ 'workspacePath' => '/vagrant/tests/workspace', - 'mail' => [ + 'mail' => [ 'opus' => [ - 'address' => 'opus4ci@example.org' + 'address' => 'opus4ci@example.org', ], ], - 'oai' => [ - 'max' => [ - 'listrecords' => 10, + 'oai' => [ + 'max' => [ + 'listrecords' => 10, 'listidentifiers' => 10, ], 'format' => [ 'default' => [ - 'class' => 'DefaultServerClass' + 'class' => 'DefaultServerClass', ], - 'epicur' => [ + 'epicur' => [ 'class' => 'UnknownEpicurClass', 'xsltFile' => 'epicurFile.xslt', ], - 'oai_dc' => [ + 'oai_dc' => [ 'class' => 'OaiDcServer', ], - 'oai_pp' => null, + 'oai_pp' => null, ], - ] + ], ] ); @@ -129,12 +130,12 @@ public function testGetFormatClassName() public function testGetFormatOptions() { $expectedOptions = [ - 'xsltFile' => 'epicurFile.xslt', - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, + 'xsltFile' => 'epicurFile.xslt', + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - 'class' => 'UnknownEpicurClass', + 'emailContact' => 'opus4ci@example.org', + 'class' => 'UnknownEpicurClass', ]; $metadDataPrefix = 'epicur'; From 5fa63f7d20274bdf1e6bcb6ee708009c8e16da53 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Mon, 28 Aug 2023 08:14:47 +0200 Subject: [PATCH 07/59] Fix coding style. --- modules/oai/models/BaseServer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index 5b4091d4d..ebf254491 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -969,7 +969,6 @@ private function getRepositoryIdentifier() * Sets the repository identifier. * * @param string $repoId - * @return void */ public function setRepositoryIdentifier($repoId) { From 32f82691a65c6cd01240f454d2a719142f692ba6 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 29 Aug 2023 14:22:18 +0200 Subject: [PATCH 08/59] Adjust visibility of some option getter methods. --- modules/oai/models/BaseServer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index ebf254491..66e3f14e7 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -913,7 +913,7 @@ public function setEmailContact($email) * * @return string Oai base url. */ - private function getOaiBaseUrl() + public function getOaiBaseUrl() { $oaiBaseUrl = $this->options['oaiBaseUrl'] ?? ''; @@ -940,7 +940,7 @@ public function setOaiBaseUrl($url) * * @return string Repository name. */ - private function getRepositoryName() + public function getRepositoryName() { return $this->options['repositoryName'] ?? ''; } @@ -960,7 +960,7 @@ public function setRepositoryName($repoName) * * @return string Repository identifier. */ - private function getRepositoryIdentifier() + public function getRepositoryIdentifier() { return $this->options['repositoryIdentifier'] ?? ''; } @@ -980,7 +980,7 @@ public function setRepositoryIdentifier($repoId) * * @return string Sample identifier. */ - private function getSampleIdentifier() + public function getSampleIdentifier() { return $this->options['sampleIdentifier'] ?? ''; } @@ -1000,7 +1000,7 @@ public function setSampleIdentifier($sampleId) * * @return int Maximum number of listable identifiers per request. */ - private function getMaxListIdentifiers() + public function getMaxListIdentifiers() { return $this->options['maxListIdentifiers'] ?? ''; } @@ -1020,7 +1020,7 @@ public function setMaxListIdentifiers($maxListIds) * * @return int Maximum number of listable records per request. */ - private function getMaxListRecords() + public function getMaxListRecords() { return $this->options['maxListRecords'] ?? ''; } @@ -1040,7 +1040,7 @@ public function setMaxListRecords($maxListRecs) * * @return string */ - protected function getXsltFile() + public function getXsltFile() { return $this->options['xsltFile'] ?? ''; } From b78a4590d868236fba1b9b4c5737ae922ef8a5dd Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 29 Aug 2023 15:20:12 +0200 Subject: [PATCH 09/59] #766 Add basic option getter tests for the BaseServer. --- modules/oai/models/BaseServer.php | 2 +- tests/modules/oai/models/BaseServerTest.php | 66 +++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/modules/oai/models/BaseServerTest.php diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index 66e3f14e7..eb24a5e89 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -918,7 +918,7 @@ public function getOaiBaseUrl() $oaiBaseUrl = $this->options['oaiBaseUrl'] ?? ''; // if no OAI base url is set, use local information as base url - if (true === empty($oaiBaseUrl)) { + if (empty($oaiBaseUrl)) { $oaiBaseUrl = $this->getBaseUrl() . '/oai'; // TODO . $module; } diff --git a/tests/modules/oai/models/BaseServerTest.php b/tests/modules/oai/models/BaseServerTest.php new file mode 100644 index 000000000..4570a5303 --- /dev/null +++ b/tests/modules/oai/models/BaseServerTest.php @@ -0,0 +1,66 @@ +setBaseUrl('http://baseUrlPath'); + $baseServer->setOaiBaseUrl('http://oaiBaseUrlPath'); + + $this->assertEquals('http://oaiBaseUrlPath', $baseServer->getOaiBaseUrl()); + } + + public function testGetOaiBaseUrlNotSet() + { + $baseServer = new Oai_Model_BaseServer(); + + $baseServer->setBaseUrl('http://baseUrlPath'); + + $this->assertEquals('http://baseUrlPath/oai', $baseServer->getOaiBaseUrl()); + } + + public function testGetOptionNotInOptionsArray() + { + $baseServer = new Oai_Model_BaseServer(); + + $baseServer->setOptions([]); + $this->assertEquals('', $baseServer->getRepositoryIdentifier()); + + $baseServer->setOptions(['repositoryIdentifier' => null]); + $this->assertEquals('', $baseServer->getRepositoryIdentifier()); + + $baseServer->setOptions(['repositoryIdentifier' => '']); + $this->assertEquals('', $baseServer->getRepositoryIdentifier()); + } +} From e92fc9bd9e53c11df1e4de02a7ace2689bb1188d Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 29 Aug 2023 15:23:20 +0200 Subject: [PATCH 10/59] #766 Removed usage of getcwd(). --- tests/modules/oai/format/EpicurTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modules/oai/format/EpicurTest.php b/tests/modules/oai/format/EpicurTest.php index 76cab5e76..e225966f3 100644 --- a/tests/modules/oai/format/EpicurTest.php +++ b/tests/modules/oai/format/EpicurTest.php @@ -88,7 +88,7 @@ public function testConfigurationWithAbsolutXsltFilePath() 'oai' => [ 'format' => [ 'epicur' => [ - 'xsltFile' => getcwd() . '/modules/oai/views/scripts/index/prefixes/epicur.xslt', + 'xsltFile' => APPLICATION_PATH . '/modules/oai/views/scripts/index/prefixes/epicur.xslt', ], ], ], From ce5004d9062a67df0baac3ebd401e0ef421ee4ae Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 29 Aug 2023 15:26:10 +0200 Subject: [PATCH 11/59] #766 Fix method access level --- modules/oai/models/prefix/marcxml/MarcXmlServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/oai/models/prefix/marcxml/MarcXmlServer.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php index 0c447191f..132e54522 100644 --- a/modules/oai/models/prefix/marcxml/MarcXmlServer.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -35,7 +35,7 @@ class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_BaseServer * * @return string */ - protected function getXsltFile() + public function getXsltFile() { return "marc21.xslt"; } From 4039c6e531f791ecbb4059cac4497246d0300484 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 29 Aug 2023 17:59:05 +0200 Subject: [PATCH 12/59] #766 Split ServerFactory test methods. --- .../modules/oai/models/ServerFactoryTest.php | 190 ++++++++++++------ tests/support/DefaultOaiServer.php | 33 +++ 2 files changed, 163 insertions(+), 60 deletions(-) create mode 100644 tests/support/DefaultOaiServer.php diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 8037cdc0d..6da45d68a 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -31,123 +31,193 @@ class Oai_Model_ServerFactoryTest extends ControllerTestCase { - /** @var Oai_Model_ServerFactory */ - private $serverFactory; - - public function setUp(): void + /** + * @return array + */ + protected function getConfigurationArray() { - parent::setUp(); - - $config = new Zend_Config( - [ - 'workspacePath' => '/vagrant/tests/workspace', - 'mail' => [ - 'opus' => [ - 'address' => 'opus4ci@example.org', - ], + return [ + 'workspacePath' => '/vagrant/tests/workspace', + 'mail' => [ + 'opus' => [ + 'address' => 'opus4ci@example.org', + ], + ], + 'oai' => [ + 'max' => [ + 'listrecords' => 10, + 'listidentifiers' => 10, ], - 'oai' => [ - 'max' => [ - 'listrecords' => 10, - 'listidentifiers' => 10, + 'format' => [ + 'default' => [ + 'class' => DefaultOaiServer::class, ], - 'format' => [ - 'default' => [ - 'class' => 'DefaultServerClass', - ], - 'epicur' => [ - 'class' => 'UnknownEpicurClass', - 'xsltFile' => 'epicurFile.xslt', - ], - 'oai_dc' => [ - 'class' => 'OaiDcServer', - ], - 'oai_pp' => null, + 'oai_dc' => [ + 'class' => OaiDcServer::class, + 'xsltFile' => 'oaiFile.xslt', ], + 'oai_pp' => null, ], - ] - ); + ], + ]; + } + + /** + * @param array|null $configurationArray + * @return Oai_Model_ServerFactory + */ + protected function createServerFactory($configurationArray = null) + { + if ($configurationArray === null) { + $config = new Zend_Config($this->getConfigurationArray()); + } else { + $config = new Zend_Config($configurationArray); + } + + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setConfig($config); + return $serverFactory; + } - $this->serverFactory = new Oai_Model_ServerFactory(); - $this->serverFactory->setConfig($config); + public function testCreateWithValidMetadataPrefix() + { + $serverFactory = $this->createServerFactory(); + $server = $serverFactory->create('oai_dc'); + $expectedServer = new OaiDcServer(); + $expectedServer->setMaxListIdentifiers(10); + $expectedServer->setMaxListRecords(10); + $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); + $expectedServer->setEmailContact('opus4ci@example.org'); + $expectedServer->setXsltFile('oaiFile.xslt'); + $this->assertEquals(OaiDcServer::class, get_class($server)); + $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); } - public function testCreate() + public function testCreateWithNoneExistingFormatServerClass() { - $server = $this->serverFactory->create(); + $configArray = $this->getConfigurationArray(); + + $configArray['oai']['format']['oai_dc']['class'] = 'UnknownOaiDcServerClass'; + + $serverFactory = $this->createServerFactory($configArray); + + $server = $serverFactory->create('oai_dc'); $expectedServer = new Oai_Model_BaseServer(); + $expectedServer->setXsltFile('oaiFile.xslt'); $expectedServer->setMaxListIdentifiers(10); $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + } + + public function testCreateWithNoneExistingDefaultServerClass() + { + $configArray = $this->getConfigurationArray(); + unset($configArray['oai']['format']['oai_dc']['class']); + $configArray['oai']['format']['default']['class'] = 'UnknownDefaultServerClass'; + + $serverFactory = $this->createServerFactory($configArray); - $server = $this->serverFactory->create('epicur'); + $server = $serverFactory->create('oai_dc'); $expectedServer = new Oai_Model_BaseServer(); - $expectedServer->setXsltFile('epicurFile.xslt'); + $expectedServer->setXsltFile('oaiFile.xslt'); $expectedServer->setMaxListIdentifiers(10); $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + } - $server = $this->serverFactory->create('oai_dc'); - $expectedServer = new OaiDcServer(); + public function testCreateWithNoPrefix() + { + $serverFactory = $this->createServerFactory(); + $server = $serverFactory->create(); + $expectedServer = new DefaultOaiServer(); $expectedServer->setMaxListIdentifiers(10); $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); - $this->assertEquals(OaiDcServer::class, get_class($server)); + $this->assertEquals(DefaultOaiServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + } - $server = $this->serverFactory->create('oai_pp'); - $expectedServer = new Oai_Model_BaseServer(); + public function testCreateWithPrefixNotConfigured() + { + $serverFactory = $this->createServerFactory(); + $server = $serverFactory->create('oai_pp'); + $expectedServer = new DefaultOaiServer(); $expectedServer->setMaxListIdentifiers(10); $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); - $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); + $this->assertEquals(DefaultOaiServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); } public function testGetFormatClassName() { - $metadDataPrefix = 'epicur'; - $serverClass = $this->serverFactory->getFormatClassName($metadDataPrefix); - $this->assertEquals('UnknownEpicurClass', $serverClass); + $serverFactory = $this->createServerFactory(); + + $metaDataPrefix = 'oai_dc'; + $serverClass = $serverFactory->getFormatClassName($metaDataPrefix); + $this->assertEquals(OaiDcServer::class, $serverClass); + + $metaDataPrefix = 'oai_Dc'; + $serverClass = $serverFactory->getFormatClassName($metaDataPrefix); + $this->assertEquals(OaiDcServer::class, $serverClass); + } + + public function testGetFormatClassNameWithUnknownPrefix() + { + $configArray = $this->getConfigurationArray(); - $metadDataPrefix = 'EpiCur'; - $serverClass = $this->serverFactory->getFormatClassName($metadDataPrefix); - $this->assertEquals('UnknownEpicurClass', $serverClass); + $serverFactory = $this->createServerFactory($configArray); - $metadDataPrefix = 'unknown'; - $serverClass = $this->serverFactory->getFormatClassName($metadDataPrefix); - $this->assertEquals('DefaultServerClass', $serverClass); + $metaDataPrefix = 'unknown'; + $serverClass = $serverFactory->getFormatClassName($metaDataPrefix); + $this->assertEquals(DefaultOaiServer::class, $serverClass); } public function testGetFormatOptions() { + $serverFactory = $this->createServerFactory(); + $expectedOptions = [ - 'xsltFile' => 'epicurFile.xslt', + 'xsltFile' => 'oaiFile.xslt', 'maxListIdentifiers' => 10, 'maxListRecords' => 10, 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', 'emailContact' => 'opus4ci@example.org', - 'class' => 'UnknownEpicurClass', + 'class' => OaiDcServer::class, ]; - $metadDataPrefix = 'epicur'; - $options = $this->serverFactory->getFormatOptions($metadDataPrefix); + $metaDataPrefix = 'oai_dc'; + $options = $serverFactory->getFormatOptions($metaDataPrefix); $this->assertEquals($expectedOptions, $options); - $metadDataPrefix = 'EpiCur'; - $options = $this->serverFactory->getFormatOptions($metadDataPrefix); + $metaDataPrefix = 'oai_Dc'; + $options = $serverFactory->getFormatOptions($metaDataPrefix); $this->assertEquals($expectedOptions, $options); + } + + public function testGetFormatOptionsWithUnknownMetaDataPrefix() + { + $serverFactory = $this->createServerFactory(); + + $expectedOptions = [ + 'xsltFile' => 'oaiFile.xslt', + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'class' => OaiDcServer::class, + ]; - $metadDataPrefix = 'unknown'; - $options = $this->serverFactory->getFormatOptions($metadDataPrefix); + $metaDataPrefix = 'unknown'; + $options = $serverFactory->getFormatOptions($metaDataPrefix); unset($expectedOptions['xsltFile']); unset($expectedOptions['class']); $this->assertEquals($expectedOptions, $options); diff --git a/tests/support/DefaultOaiServer.php b/tests/support/DefaultOaiServer.php new file mode 100644 index 000000000..ed842a336 --- /dev/null +++ b/tests/support/DefaultOaiServer.php @@ -0,0 +1,33 @@ + Date: Thu, 31 Aug 2023 09:33:40 +0200 Subject: [PATCH 13/59] #766 Merge default and format specific options. --- modules/oai/models/ServerFactory.php | 42 ++++++++----------- .../modules/oai/models/ServerFactoryTest.php | 18 ++++---- 2 files changed, 25 insertions(+), 35 deletions(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 1abc8cab1..b56b2571b 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -47,7 +47,9 @@ class Oai_Model_ServerFactory */ public function create($metaDataPrefix = '') { - $serverClass = $this->getFormatClassName($metaDataPrefix); + $options = $this->getFormatOptions($metaDataPrefix); + + $serverClass = $options['class'] ?? Oai_Model_BaseServer::class; if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { $server = new Oai_Model_BaseServer(); @@ -55,7 +57,6 @@ public function create($metaDataPrefix = '') $server = new $serverClass(); } - $options = $this->getFormatOptions($metaDataPrefix); $server->setOptions($options); return $server; @@ -88,42 +89,39 @@ public function createByResumptionToken($resumptionToken) } /** - * Reads the custom oai server class from the configuration if exists. + * Gets all options for the oai server * * @param string $metadataPrefix - * @return string + * @return array */ - public function getFormatClassName($metadataPrefix) + public function getFormatOptions($metadataPrefix = '') { + $metadataPrefix = strtolower($metadataPrefix); + $config = $this->getConfig(); if (! isset($config->oai)) { throw new Exception('No configuration for module oai.'); } - $metadataPrefix = strtolower($metadataPrefix); - - if ($metadataPrefix && isset($config->oai->format->$metadataPrefix->class)) { - return $config->oai->format->$metadataPrefix->class; - } + $generalOptions = $this->getGeneralOaiOptions(); + $defaultOptions = isset($config->oai->format->default) ? $config->oai->format->default->toArray() : []; - if (isset($config->oai->format->default->class)) { - return $config->oai->format->default->class; + $formatOptions = []; + if (isset($config->oai->format->$metadataPrefix)) { + $formatOptions = $config->oai->format->$metadataPrefix->toArray(); } - return ''; + return array_merge($generalOptions, $defaultOptions, $formatOptions); } /** - * Gets all options for the oai server + * Gets the general oai options from the configuration * - * @param string $metadataPrefix * @return array */ - public function getFormatOptions($metadataPrefix = '') + public function getGeneralOaiOptions() { - $metadataPrefix = strtolower($metadataPrefix); - $config = $this->getConfig(); if (! isset($config->oai)) { @@ -162,12 +160,6 @@ public function getFormatOptions($metadataPrefix = '') $options['emailContact'] = $config->mail->opus->address; } - $formatOptions = []; - if (isset($config->oai->format->$metadataPrefix)) { - $formatOptions = $config->oai->format->$metadataPrefix->toArray(); - unset($options['class']); - } - - return $formatOptions ? array_merge($options, $formatOptions) : $options; + return $options; } } diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 6da45d68a..455d119d5 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -162,12 +162,12 @@ public function testGetFormatClassName() $serverFactory = $this->createServerFactory(); $metaDataPrefix = 'oai_dc'; - $serverClass = $serverFactory->getFormatClassName($metaDataPrefix); - $this->assertEquals(OaiDcServer::class, $serverClass); + $options = $serverFactory->getFormatOptions($metaDataPrefix); + $this->assertEquals(OaiDcServer::class, $options['class']); $metaDataPrefix = 'oai_Dc'; - $serverClass = $serverFactory->getFormatClassName($metaDataPrefix); - $this->assertEquals(OaiDcServer::class, $serverClass); + $options = $serverFactory->getFormatOptions($metaDataPrefix); + $this->assertEquals(OaiDcServer::class, $options['class']); } public function testGetFormatClassNameWithUnknownPrefix() @@ -177,8 +177,8 @@ public function testGetFormatClassNameWithUnknownPrefix() $serverFactory = $this->createServerFactory($configArray); $metaDataPrefix = 'unknown'; - $serverClass = $serverFactory->getFormatClassName($metaDataPrefix); - $this->assertEquals(DefaultOaiServer::class, $serverClass); + $options = $serverFactory->getFormatOptions($metaDataPrefix); + $this->assertEquals(DefaultOaiServer::class, $options['class']); } public function testGetFormatOptions() @@ -208,18 +208,16 @@ public function testGetFormatOptionsWithUnknownMetaDataPrefix() $serverFactory = $this->createServerFactory(); $expectedOptions = [ - 'xsltFile' => 'oaiFile.xslt', 'maxListIdentifiers' => 10, 'maxListRecords' => 10, 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', 'emailContact' => 'opus4ci@example.org', - 'class' => OaiDcServer::class, + 'class' => DefaultOaiServer::class, ]; $metaDataPrefix = 'unknown'; $options = $serverFactory->getFormatOptions($metaDataPrefix); - unset($expectedOptions['xsltFile']); - unset($expectedOptions['class']); + $this->assertEquals($expectedOptions, $options); } } From aa07706eb857301cfa3eab77e7c21bbef69119af Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 31 Aug 2023 09:46:56 +0200 Subject: [PATCH 14/59] #766 Remove unnecessary check on "true". --- modules/oai/models/ServerFactory.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index b56b2571b..5ea8ba8fe 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -136,21 +136,21 @@ public function getGeneralOaiOptions() if (isset($config->oai->repository->identifier)) { $options['repositoryIdentifier'] = $config->oai->repository->identifier; } - if (true === isset($config->oai->sample->identifier)) { + if (isset($config->oai->sample->identifier)) { $options['sampleIdentifier'] = $config->oai->sample->identifier; } - if (true === isset($config->oai->max->listidentifiers)) { + if (isset($config->oai->max->listidentifiers)) { $options['maxListIdentifiers'] = (int) $config->oai->max->listidentifiers; } - if (true === isset($config->oai->max->listrecords)) { + if (isset($config->oai->max->listrecords)) { $options['maxListRecords'] = (int) $config->oai->max->listrecords; } - if (true === isset($config->oai->baseurl)) { + if (isset($config->oai->baseurl)) { $options['oaiBaseUrl'] = $config->oai->baseurl; } - if (true === isset($config->workspacePath)) { + if (isset($config->workspacePath)) { $options['resumptionTokenPath'] = $config->workspacePath . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'resumption'; From b9585c94c677a1f4f992e1f2c2ac936f9383952e Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 31 Aug 2023 10:46:26 +0200 Subject: [PATCH 15/59] #766 Removed throwing the exception, as this is already happening at the appropriate place and coding style. --- modules/oai/models/ServerFactory.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 5ea8ba8fe..bf7cf45b5 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -100,12 +100,11 @@ public function getFormatOptions($metadataPrefix = '') $config = $this->getConfig(); - if (! isset($config->oai)) { - throw new Exception('No configuration for module oai.'); - } - $generalOptions = $this->getGeneralOaiOptions(); - $defaultOptions = isset($config->oai->format->default) ? $config->oai->format->default->toArray() : []; + $defaultOptions = []; + if (isset($config->oai->format->default)) { + $defaultOptions = $config->oai->format->default->toArray(); + } $formatOptions = []; if (isset($config->oai->format->$metadataPrefix)) { From 97311ccf45e0ca9d49f8b1568a1afda6d1790a82 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 31 Aug 2023 14:00:47 +0200 Subject: [PATCH 16/59] #766 Set the value for xsltFile so that it can be overwritten again using configuration. --- .../models/prefix/marcxml/MarcXmlServer.php | 10 ++---- .../modules/oai/models/ServerFactoryTest.php | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/modules/oai/models/prefix/marcxml/MarcXmlServer.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php index 132e54522..54f663e50 100644 --- a/modules/oai/models/prefix/marcxml/MarcXmlServer.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -30,13 +30,9 @@ */ class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_BaseServer { - /** - * Return xlst file name / file path. - * - * @return string - */ - public function getXsltFile() + public function __construct() { - return "marc21.xslt"; + // Default value for the xsltFile, may be overwritten later by the configuration (using the setOptions method). + $this->setXsltFile('marc21.xslt'); } } diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 455d119d5..4b469fcd8 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -220,4 +220,35 @@ public function testGetFormatOptionsWithUnknownMetaDataPrefix() $this->assertEquals($expectedOptions, $options); } + + public function testConfigurationOverwritesXsltFileValueFromPrefixClass() + { + $configArray = $this->getConfigurationArray(); + + $configArray['oai']['format']['xmetadissplus'] = [ + 'class' => Oai_Model_Prefix_MarcXml_MarcXmlServer::class, + 'xsltFile' => 'XMetaDissPlus.xslt' + ]; + + $serverFactory = $this->createServerFactory($configArray); + + $server = $serverFactory->create('xmetadissplus'); + + $this->assertEquals('XMetaDissPlus.xslt', $server->getXsltFile()); + } + + public function testXsltFileNotConfiguredButSetInPrefixClass() + { + $configArray = $this->getConfigurationArray(); + + $configArray['oai']['format']['xmetadissplus'] = [ + 'class' => Oai_Model_Prefix_MarcXml_MarcXmlServer::class, + ]; + + $serverFactory = $this->createServerFactory($configArray); + + $server = $serverFactory->create('xmetadissplus'); + + $this->assertEquals('marc21.xslt', $server->getXsltFile()); + } } From 2d244488a0d5b82f1f6bed8b3f07e7c3b195b9ed Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 31 Aug 2023 17:15:48 +0200 Subject: [PATCH 17/59] #1097 Made ViewHelper configurable. --- application/configs/application.ini | 1 + modules/oai/models/BaseServer.php | 41 ++++-- modules/oai/models/OptionsTrait.php | 1 - modules/oai/models/ServerFactory.php | 12 +- .../modules/oai/models/ServerFactoryTest.php | 132 +++++++++++++++++- 5 files changed, 171 insertions(+), 16 deletions(-) diff --git a/application/configs/application.ini b/application/configs/application.ini index cb7769a2f..fbd4f5b19 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -110,6 +110,7 @@ oai.description.eprints.comment.text = ; The format prefix ; oai.format.[lower cased format identifier ].prefix = [prefix] oai.format.default.class = Oai_Model_BaseServer +oai.format.default.viewHelper = optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType oai.format.copy_xml.xsltFile = copy_xml.xslt oai.format.epicur.xsltFile = epicur.xslt oai.format.marc21.xsltFile = marc21.xslt diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index eb24a5e89..134ae8158 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -92,6 +92,14 @@ class Oai_Model_BaseServer extends Application_Model_Abstract /** @var Zend_Controller_Response_Http */ private $response; // TODO temporary hack + public function __construct() + { + // Set default values for some options + $this->setViewHelper( + ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] + ); + } + /** * Gather configuration before action handling. */ @@ -259,18 +267,7 @@ protected function handleRequestIntern($oaiRequest, $requestUri) protected function setupProcessor() { $this->proc->registerPHPFunctions('Opus\Common\Language::getLanguageCode'); - Application_Xslt::registerViewHelper( - $this->proc, - [ - 'optionValue', - 'fileUrl', - 'frontdoorUrl', - 'transferUrl', - 'dcmiType', - 'dcType', - 'openAireType', - ] - ); + Application_Xslt::registerViewHelper($this->proc, $this->getViewHelper()); $this->proc->setParameter('', 'urnResolverUrl', $this->getConfig()->urn->resolverUrl); $this->proc->setParameter('', 'doiResolverUrl', $this->getConfig()->doi->resolverUrl); @@ -1054,4 +1051,24 @@ public function setXsltFile($file) { $this->options['xsltFile'] = $file; } + + /** + * Gets the viewHelper + * + * @return array + */ + public function getViewHelper() + { + return $this->options['viewHelper'] ?? []; + } + + /** + * Sets the viewHelper + * + * @param array $viewHelper + */ + public function setViewHelper($viewHelper) + { + $this->options['viewHelper'] = $viewHelper; + } } diff --git a/modules/oai/models/OptionsTrait.php b/modules/oai/models/OptionsTrait.php index 63c434839..9074fbad9 100644 --- a/modules/oai/models/OptionsTrait.php +++ b/modules/oai/models/OptionsTrait.php @@ -47,7 +47,6 @@ public function getOptions() */ public function setOptions($options) { - // TODO How to react if a setter does not exist? foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); if (method_exists($this, $method) && $value) { diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index bf7cf45b5..419aa54e3 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -101,6 +101,7 @@ public function getFormatOptions($metadataPrefix = '') $config = $this->getConfig(); $generalOptions = $this->getGeneralOaiOptions(); + $defaultOptions = []; if (isset($config->oai->format->default)) { $defaultOptions = $config->oai->format->default->toArray(); @@ -111,7 +112,16 @@ public function getFormatOptions($metadataPrefix = '') $formatOptions = $config->oai->format->$metadataPrefix->toArray(); } - return array_merge($generalOptions, $defaultOptions, $formatOptions); + $options = array_merge($generalOptions, $defaultOptions, $formatOptions); + + if (isset($options['viewHelper'])) { + $viewHelper = $options['viewHelper']; + if (is_string($viewHelper)) { + $options['viewHelper'] = array_map('trim', explode(',', $viewHelper)); + } + } + + return $options; } /** diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 4b469fcd8..292bed787 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -89,6 +89,9 @@ public function testCreateWithValidMetadataPrefix() $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); $expectedServer->setXsltFile('oaiFile.xslt'); + $expectedServer->setViewHelper( + ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] + ); $this->assertEquals(OaiDcServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); } @@ -108,6 +111,9 @@ public function testCreateWithNoneExistingFormatServerClass() $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); + $expectedServer->setViewHelper( + ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] + ); $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); } @@ -127,6 +133,9 @@ public function testCreateWithNoneExistingDefaultServerClass() $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); + $expectedServer->setViewHelper( + ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] + ); $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); } @@ -140,6 +149,9 @@ public function testCreateWithNoPrefix() $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); + $expectedServer->setViewHelper( + ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] + ); $this->assertEquals(DefaultOaiServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); } @@ -153,6 +165,9 @@ public function testCreateWithPrefixNotConfigured() $expectedServer->setMaxListRecords(10); $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); $expectedServer->setEmailContact('opus4ci@example.org'); + $expectedServer->setViewHelper( + ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] + ); $this->assertEquals(DefaultOaiServer::class, get_class($server)); $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); } @@ -226,8 +241,8 @@ public function testConfigurationOverwritesXsltFileValueFromPrefixClass() $configArray = $this->getConfigurationArray(); $configArray['oai']['format']['xmetadissplus'] = [ - 'class' => Oai_Model_Prefix_MarcXml_MarcXmlServer::class, - 'xsltFile' => 'XMetaDissPlus.xslt' + 'class' => Oai_Model_Prefix_MarcXml_MarcXmlServer::class, + 'xsltFile' => 'XMetaDissPlus.xslt', ]; $serverFactory = $this->createServerFactory($configArray); @@ -251,4 +266,117 @@ public function testXsltFileNotConfiguredButSetInPrefixClass() $this->assertEquals('marc21.xslt', $server->getXsltFile()); } + + public function testViewHelperNotConfigured() + { + $configArray = $this->getConfigurationArray(); + + unset($configArray['oai']['format']['default']['viewHelper']); + + $serverFactory = $this->createServerFactory($configArray); + + $expectedOptions = [ + 'xsltFile' => 'oaiFile.xslt', + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'viewHelper' => [ + 'optionValue', + 'fileUrl', + 'frontdoorUrl', + 'transferUrl', + 'dcmiType', + 'dcType', + 'openAireType', + ], + ]; + + $server = $serverFactory->create('oai_dc'); + + $this->assertEquals($expectedOptions, $server->getOptions()); + } + + public function testViewHelperConfiguredAsArray() + { + $configArray = $this->getConfigurationArray(); + + $configArray['oai']['format']['default'] = [ + 'viewHelper' => [ + 'optionValue', + 'fileUrl', + 'frontdoorUrl', + 'transferUrl', + 'dcmiType', + 'dcType', + 'openAireType', + ], + ]; + + $serverFactory = $this->createServerFactory($configArray); + + $expectedViewHelpers = [ + 'optionValue', + 'fileUrl', + 'frontdoorUrl', + 'transferUrl', + 'dcmiType', + 'dcType', + 'openAireType', + ]; + + $options = $serverFactory->getFormatOptions(); + + $this->assertEquals($expectedViewHelpers, $options['viewHelper']); + } + + public function testViewHelperConfiguredAsCommaSeparatedList() + { + $configArray = $this->getConfigurationArray(); + + $configArray['oai']['format']['default'] = [ + 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + ]; + + $serverFactory = $this->createServerFactory($configArray); + + $expectedViewHelpers = [ + 'optionValue', + 'fileUrl', + 'frontdoorUrl', + 'transferUrl', + 'dcmiType', + 'dcType', + 'openAireType', + ]; + + $options = $serverFactory->getFormatOptions(); + + $this->assertEquals($expectedViewHelpers, $options['viewHelper']); + } + + public function testViewHelpersConfiguredForMetadaPrefix() + { + $configArray = $this->getConfigurationArray(); + + $configArray['oai']['format']['default'] = [ + 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + ]; + + $configArray['oai']['format']['oai_dc'] = [ + 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl', + ]; + + $serverFactory = $this->createServerFactory($configArray); + + $expectedProcessors = [ + 'optionValue', + 'fileUrl', + 'frontdoorUrl', + ]; + + $options = $serverFactory->getFormatOptions('oai_dc'); + + $this->assertEquals($expectedProcessors, $options['viewHelper']); + } } From a7110374e857530ee087db1d7f365589ee13d072 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 1 Sep 2023 11:19:05 +0200 Subject: [PATCH 18/59] #766 Replaced options array with single properties. --- modules/oai/models/BaseServer.php | 76 ++++++---- modules/oai/models/OptionsTrait.php | 14 +- .../modules/oai/models/ServerFactoryTest.php | 142 +++++++----------- 3 files changed, 117 insertions(+), 115 deletions(-) diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index 134ae8158..981badb88 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -92,13 +92,35 @@ class Oai_Model_BaseServer extends Application_Model_Abstract /** @var Zend_Controller_Response_Http */ private $response; // TODO temporary hack - public function __construct() - { - // Set default values for some options - $this->setViewHelper( - ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] - ); - } + /** @var string */ + private $oaiBaseUrl; + + /** @var string */ + private $resumptionTokenPath; + + /** @var string */ + private $emailContact; + + /** @var string */ + private $repositoryName; + + /** @var string */ + private $repositoryIdentifier; + + /** @var string */ + private $sampleIdentifier; + + /** @var string */ + private $maxListIdentifiers; + + /** @var string */ + private $maxListRecords; + + /** @var string */ + private $xsltFile; + + /** @var string */ + private $viewHelper; /** * Gather configuration before action handling. @@ -872,7 +894,7 @@ public function getResponse() */ public function getResumptionTokenPath() { - return $this->options['resumptionTokenPath'] ?? ''; + return $this->resumptionTokenPath ?? ''; } /** @@ -882,7 +904,7 @@ public function getResumptionTokenPath() */ public function setResumptionTokenPath($path) { - $this->options['resumptionTokenPath'] = $path; + $this->resumptionTokenPath = $path; } /** @@ -892,7 +914,7 @@ public function setResumptionTokenPath($path) */ public function getEmailContact() { - return $this->options['emailContact'] ?? ''; + return $this->emailContact ?? ''; } /** @@ -902,7 +924,7 @@ public function getEmailContact() */ public function setEmailContact($email) { - $this->options['emailContact'] = $email; + $this->emailContact = $email; } /** @@ -912,7 +934,7 @@ public function setEmailContact($email) */ public function getOaiBaseUrl() { - $oaiBaseUrl = $this->options['oaiBaseUrl'] ?? ''; + $oaiBaseUrl = $this->oaiBaseUrl ?? ''; // if no OAI base url is set, use local information as base url if (empty($oaiBaseUrl)) { @@ -929,7 +951,7 @@ public function getOaiBaseUrl() */ public function setOaiBaseUrl($url) { - $this->options['oaiBaseUrl'] = $url; + $this->oaiBaseUrl = $url; } /** @@ -939,7 +961,7 @@ public function setOaiBaseUrl($url) */ public function getRepositoryName() { - return $this->options['repositoryName'] ?? ''; + return $this->repositoryName ?? ''; } /** @@ -949,7 +971,7 @@ public function getRepositoryName() */ public function setRepositoryName($repoName) { - $this->options['repositoryName'] = $repoName; + $this->repositoryName = $repoName; } /** @@ -959,7 +981,7 @@ public function setRepositoryName($repoName) */ public function getRepositoryIdentifier() { - return $this->options['repositoryIdentifier'] ?? ''; + return $this->repositoryIdentifier ?? ''; } /** @@ -969,7 +991,7 @@ public function getRepositoryIdentifier() */ public function setRepositoryIdentifier($repoId) { - $this->options['repositoryIdentifier'] = $repoId; + $this->repositoryIdentifier = $repoId; } /** @@ -979,7 +1001,7 @@ public function setRepositoryIdentifier($repoId) */ public function getSampleIdentifier() { - return $this->options['sampleIdentifier'] ?? ''; + return $this->sampleIdentifier ?? ''; } /** @@ -989,7 +1011,7 @@ public function getSampleIdentifier() */ public function setSampleIdentifier($sampleId) { - $this->options['sampleIdentifier'] = $sampleId; + $this->sampleIdentifier = $sampleId; } /** @@ -999,7 +1021,7 @@ public function setSampleIdentifier($sampleId) */ public function getMaxListIdentifiers() { - return $this->options['maxListIdentifiers'] ?? ''; + return $this->maxListIdentifiers ?? ''; } /** @@ -1009,7 +1031,7 @@ public function getMaxListIdentifiers() */ public function setMaxListIdentifiers($maxListIds) { - $this->options['maxListIdentifiers'] = $maxListIds; + $this->maxListIdentifiers = $maxListIds; } /** @@ -1019,7 +1041,7 @@ public function setMaxListIdentifiers($maxListIds) */ public function getMaxListRecords() { - return $this->options['maxListRecords'] ?? ''; + return $this->maxListRecords ?? ''; } /** @@ -1029,7 +1051,7 @@ public function getMaxListRecords() */ public function setMaxListRecords($maxListRecs) { - $this->options['maxListRecords'] = $maxListRecs; + $this->maxListRecords = $maxListRecs; } /** @@ -1039,7 +1061,7 @@ public function setMaxListRecords($maxListRecs) */ public function getXsltFile() { - return $this->options['xsltFile'] ?? ''; + return $this->xsltFile ?? ''; } /** @@ -1049,7 +1071,7 @@ public function getXsltFile() */ public function setXsltFile($file) { - $this->options['xsltFile'] = $file; + $this->xsltFile = $file; } /** @@ -1059,7 +1081,7 @@ public function setXsltFile($file) */ public function getViewHelper() { - return $this->options['viewHelper'] ?? []; + return $this->viewHelper ?? []; } /** @@ -1069,6 +1091,6 @@ public function getViewHelper() */ public function setViewHelper($viewHelper) { - $this->options['viewHelper'] = $viewHelper; + $this->viewHelper = $viewHelper; } } diff --git a/modules/oai/models/OptionsTrait.php b/modules/oai/models/OptionsTrait.php index 9074fbad9..8ed75f1fb 100644 --- a/modules/oai/models/OptionsTrait.php +++ b/modules/oai/models/OptionsTrait.php @@ -35,11 +35,21 @@ trait Oai_Model_OptionsTrait private $options; /** + * @param array $optionNames * @return array */ - public function getOptions() + public function getOptions($optionNames) { - return is_array($this->options) ? $this->options : []; + $options = []; + + foreach ($optionNames as $optionName) { + $method = 'get' . ucfirst($optionName); + if (method_exists($this, $method)) { + $options[$optionName] = $this->$method(); + } + } + + return $options; } /** diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 292bed787..10348ad58 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -81,19 +81,19 @@ protected function createServerFactory($configurationArray = null) public function testCreateWithValidMetadataPrefix() { - $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create('oai_dc'); - $expectedServer = new OaiDcServer(); - $expectedServer->setMaxListIdentifiers(10); - $expectedServer->setMaxListRecords(10); - $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); - $expectedServer->setEmailContact('opus4ci@example.org'); - $expectedServer->setXsltFile('oaiFile.xslt'); - $expectedServer->setViewHelper( - ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] - ); + $serverFactory = $this->createServerFactory(); + $server = $serverFactory->create('oai_dc'); + + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => 'oaiFile.xslt', + ]; + $this->assertEquals(OaiDcServer::class, get_class($server)); - $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } public function testCreateWithNoneExistingFormatServerClass() @@ -104,18 +104,18 @@ public function testCreateWithNoneExistingFormatServerClass() $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create('oai_dc'); - $expectedServer = new Oai_Model_BaseServer(); - $expectedServer->setXsltFile('oaiFile.xslt'); - $expectedServer->setMaxListIdentifiers(10); - $expectedServer->setMaxListRecords(10); - $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); - $expectedServer->setEmailContact('opus4ci@example.org'); - $expectedServer->setViewHelper( - ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] - ); + $server = $serverFactory->create('oai_dc'); + + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => 'oaiFile.xslt', + ]; + $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); - $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } public function testCreateWithNoneExistingDefaultServerClass() @@ -126,50 +126,50 @@ public function testCreateWithNoneExistingDefaultServerClass() $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create('oai_dc'); - $expectedServer = new Oai_Model_BaseServer(); - $expectedServer->setXsltFile('oaiFile.xslt'); - $expectedServer->setMaxListIdentifiers(10); - $expectedServer->setMaxListRecords(10); - $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); - $expectedServer->setEmailContact('opus4ci@example.org'); - $expectedServer->setViewHelper( - ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] - ); + $server = $serverFactory->create('oai_dc'); + + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => 'oaiFile.xslt', + ]; + $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); - $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } public function testCreateWithNoPrefix() { - $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create(); - $expectedServer = new DefaultOaiServer(); - $expectedServer->setMaxListIdentifiers(10); - $expectedServer->setMaxListRecords(10); - $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); - $expectedServer->setEmailContact('opus4ci@example.org'); - $expectedServer->setViewHelper( - ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] - ); + $serverFactory = $this->createServerFactory(); + $server = $serverFactory->create(); + + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + ]; + $this->assertEquals(DefaultOaiServer::class, get_class($server)); - $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } public function testCreateWithPrefixNotConfigured() { - $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create('oai_pp'); - $expectedServer = new DefaultOaiServer(); - $expectedServer->setMaxListIdentifiers(10); - $expectedServer->setMaxListRecords(10); - $expectedServer->setResumptionTokenPath('/vagrant/tests/workspace/tmp/resumption'); - $expectedServer->setEmailContact('opus4ci@example.org'); - $expectedServer->setViewHelper( - ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'] - ); + $serverFactory = $this->createServerFactory(); + $server = $serverFactory->create('oai_pp'); + + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + ]; + $this->assertEquals(DefaultOaiServer::class, get_class($server)); - $this->assertEquals($expectedServer->getOptions(), $server->getOptions()); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } public function testGetFormatClassName() @@ -267,36 +267,6 @@ public function testXsltFileNotConfiguredButSetInPrefixClass() $this->assertEquals('marc21.xslt', $server->getXsltFile()); } - public function testViewHelperNotConfigured() - { - $configArray = $this->getConfigurationArray(); - - unset($configArray['oai']['format']['default']['viewHelper']); - - $serverFactory = $this->createServerFactory($configArray); - - $expectedOptions = [ - 'xsltFile' => 'oaiFile.xslt', - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - 'viewHelper' => [ - 'optionValue', - 'fileUrl', - 'frontdoorUrl', - 'transferUrl', - 'dcmiType', - 'dcType', - 'openAireType', - ], - ]; - - $server = $serverFactory->create('oai_dc'); - - $this->assertEquals($expectedOptions, $server->getOptions()); - } - public function testViewHelperConfiguredAsArray() { $configArray = $this->getConfigurationArray(); From 9a5da62ce4117d03c818362ca26023f16fec7dcd Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 1 Sep 2023 14:34:22 +0200 Subject: [PATCH 19/59] #1097 Default and format-specific ViewHelper options are combined additively. --- modules/oai/models/ServerFactory.php | 14 +++++-- .../modules/oai/models/ServerFactoryTest.php | 42 +++++++++++++++++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 419aa54e3..2127511a5 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -115,10 +115,18 @@ public function getFormatOptions($metadataPrefix = '') $options = array_merge($generalOptions, $defaultOptions, $formatOptions); if (isset($options['viewHelper'])) { - $viewHelper = $options['viewHelper']; - if (is_string($viewHelper)) { - $options['viewHelper'] = array_map('trim', explode(',', $viewHelper)); + $defaultViewHelper = $defaultOptions['viewHelper'] ?? []; + $formatViewHelper = $formatOptions['viewHelper'] ?? []; + + if (is_string($defaultViewHelper)) { + $defaultViewHelper = array_map('trim', explode(',', $defaultViewHelper)); + } + + if (is_string($formatViewHelper)) { + $formatViewHelper = array_map('trim', explode(',', $formatViewHelper)); } + + $options['viewHelper'] = array_unique(array_merge($defaultViewHelper, $formatViewHelper)); } return $options; diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 10348ad58..6722019c7 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -267,7 +267,7 @@ public function testXsltFileNotConfiguredButSetInPrefixClass() $this->assertEquals('marc21.xslt', $server->getXsltFile()); } - public function testViewHelperConfiguredAsArray() + public function testViewHelpersConfiguredAsArray() { $configArray = $this->getConfigurationArray(); @@ -300,7 +300,37 @@ public function testViewHelperConfiguredAsArray() $this->assertEquals($expectedViewHelpers, $options['viewHelper']); } - public function testViewHelperConfiguredAsCommaSeparatedList() + public function testViewHelpersDefaultConfiguredAsArrayAndFormatAsString() + { + $configArray = $this->getConfigurationArray(); + + $configArray['oai']['format']['default'] = [ + 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'], + ]; + + $configArray['oai']['format']['oai_dc'] = [ + 'viewHelper' => 'firstNewViewHelper, secondNewViewHelper', + ]; + + $serverFactory = $this->createServerFactory($configArray); + + $expectedProcessors = [ + 'optionValue', + 'fileUrl', + 'frontdoorUrl', + 'transferUrl', + 'dcmiType', + 'dcType', + 'openAireType', + 'firstNewViewHelper', + 'secondNewViewHelper', + ]; + + $options = $serverFactory->getFormatOptions('oai_dc'); + $this->assertEquals($expectedProcessors, $options['viewHelper']); + } + + public function testViewHelpersConfiguredAsCommaSeparatedList() { $configArray = $this->getConfigurationArray(); @@ -334,7 +364,7 @@ public function testViewHelpersConfiguredForMetadaPrefix() ]; $configArray['oai']['format']['oai_dc'] = [ - 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl', + 'viewHelper' => 'newViewHelper', ]; $serverFactory = $this->createServerFactory($configArray); @@ -343,10 +373,14 @@ public function testViewHelpersConfiguredForMetadaPrefix() 'optionValue', 'fileUrl', 'frontdoorUrl', + 'transferUrl', + 'dcmiType', + 'dcType', + 'openAireType', + 'newViewHelper', ]; $options = $serverFactory->getFormatOptions('oai_dc'); - $this->assertEquals($expectedProcessors, $options['viewHelper']); } } From 098856f926468c5fcfedf8a507598831fc45f3dc Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 1 Sep 2023 15:34:10 +0200 Subject: [PATCH 20/59] #1097 Check if viewhelpers exist. --- .../modules/oai/models/ServerFactoryTest.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 6722019c7..f4c08b523 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -31,6 +31,9 @@ class Oai_Model_ServerFactoryTest extends ControllerTestCase { + /** @var string[] */ + protected $additionalResources = ['database', 'view']; + /** * @return array */ @@ -218,7 +221,7 @@ public function testGetFormatOptions() $this->assertEquals($expectedOptions, $options); } - public function testGetFormatOptionsWithUnknownMetaDataPrefix() + public function testGetFormatOptionsWithUnknownMetadataPrefix() { $serverFactory = $this->createServerFactory(); @@ -383,4 +386,20 @@ public function testViewHelpersConfiguredForMetadaPrefix() $options = $serverFactory->getFormatOptions('oai_dc'); $this->assertEquals($expectedProcessors, $options['viewHelper']); } + + public function testDoViewHelpersExist() + { + $viewHelpers = ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType']; + $viewHelpersNotFound = []; + + foreach ($viewHelpers as $viewHelper) { + try { + Zend_Registry::get('Opus_View')->getHelper($viewHelper); + } catch (Zend_Loader_PluginLoader_Exception $e) { + $viewHelpersNotFound[] = $viewHelper; + } + } + + $this->assertEquals([], $viewHelpersNotFound); + } } From 88b9275559f008c2587bc72c113bedadf93c16c5 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 1 Sep 2023 16:41:29 +0200 Subject: [PATCH 21/59] #766 Replace marker comment with a more technical marker. --- modules/oai/models/BaseServer.php | 2 +- modules/oai/views/scripts/index/oai-pmh.xslt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index 981badb88..3ee38f4cf 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -808,7 +808,7 @@ protected function loadStyleSheet() } $xsltXml = $this->xslt->saveXML(); $xsltXml = preg_replace( - '//u', + '//u', '', $xsltXml ); diff --git a/modules/oai/views/scripts/index/oai-pmh.xslt b/modules/oai/views/scripts/index/oai-pmh.xslt index c298b6a85..6366cb3b3 100644 --- a/modules/oai/views/scripts/index/oai-pmh.xslt +++ b/modules/oai/views/scripts/index/oai-pmh.xslt @@ -41,7 +41,7 @@ - + From 2388d8b77a4d7437d9cf028f346e899a4058f6de Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 1 Sep 2023 17:34:16 +0200 Subject: [PATCH 22/59] #766 Removed some double spaces. --- modules/oai/models/BaseServer.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index 3ee38f4cf..c8a32aacf 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -92,34 +92,34 @@ class Oai_Model_BaseServer extends Application_Model_Abstract /** @var Zend_Controller_Response_Http */ private $response; // TODO temporary hack - /** @var string */ + /** @var string */ private $oaiBaseUrl; - /** @var string */ + /** @var string */ private $resumptionTokenPath; - /** @var string */ + /** @var string */ private $emailContact; - /** @var string */ + /** @var string */ private $repositoryName; - /** @var string */ + /** @var string */ private $repositoryIdentifier; - /** @var string */ + /** @var string */ private $sampleIdentifier; - /** @var string */ + /** @var string */ private $maxListIdentifiers; - /** @var string */ + /** @var string */ private $maxListRecords; - /** @var string */ + /** @var string */ private $xsltFile; - /** @var string */ + /** @var string */ private $viewHelper; /** From 90b25f2fd56bc933af3473c9be3f11425ff1a6d2 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 1 Sep 2023 17:36:04 +0200 Subject: [PATCH 23/59] #766 Removed some double blank lines. --- modules/oai/views/scripts/index/oai-pmh.xslt | 3 --- 1 file changed, 3 deletions(-) diff --git a/modules/oai/views/scripts/index/oai-pmh.xslt b/modules/oai/views/scripts/index/oai-pmh.xslt index 6366cb3b3..27e434530 100644 --- a/modules/oai/views/scripts/index/oai-pmh.xslt +++ b/modules/oai/views/scripts/index/oai-pmh.xslt @@ -148,7 +148,6 @@ - @@ -234,7 +233,6 @@ - @@ -342,7 +340,6 @@ -
From a84057cba2e30140bac2532da32ba01070a39d7d Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 5 Sep 2023 12:04:16 +0200 Subject: [PATCH 24/59] #766 Remove some hard coded format specific code and reduced oai format configuration to class definition only. --- application/configs/application.ini | 17 +- modules/oai/models/BaseServer.php | 252 +++++++++++++++--- modules/oai/models/DocumentList.php | 45 +--- modules/oai/models/Request.php | 21 +- modules/oai/models/ServerFactory.php | 91 ++++++- .../models/prefix/CopyXml/CopyXmlServer.php | 37 +++ .../oai/models/prefix/epicur/EpicurServer.php | 38 +++ modules/oai/models/prefix/oai/OaiDcServer.php | 37 +++ modules/oai/models/prefix/oai/OaiPpServer.php | 37 +++ .../xmetadissplus/XMetaDissPlusServer.php | 40 +++ tests/modules/oai/models/DocumentListTest.php | 42 +-- 11 files changed, 524 insertions(+), 133 deletions(-) create mode 100644 modules/oai/models/prefix/CopyXml/CopyXmlServer.php create mode 100644 modules/oai/models/prefix/epicur/EpicurServer.php create mode 100644 modules/oai/models/prefix/oai/OaiDcServer.php create mode 100644 modules/oai/models/prefix/oai/OaiPpServer.php create mode 100644 modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php diff --git a/application/configs/application.ini b/application/configs/application.ini index fbd4f5b19..7fef1597e 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -111,12 +111,17 @@ oai.description.eprints.comment.text = ; oai.format.[lower cased format identifier ].prefix = [prefix] oai.format.default.class = Oai_Model_BaseServer oai.format.default.viewHelper = optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType -oai.format.copy_xml.xsltFile = copy_xml.xslt -oai.format.epicur.xsltFile = epicur.xslt -oai.format.marc21.xsltFile = marc21.xslt -oai.format.oai_dc.xsltFile = oai_dc.xslt -oai.format.oai_pp.xsltFile = oai_pp.xslt -oai.format.xmetadissplus.xsltFile = XMetaDissPlus.xslt +oai.format.default.checkEmbargo = 0 +oai.format.default.hasFilesVisibleInOai = 0 + +; OPUS 4 XML (only for admins) +oai.format.copy_xml.class = Oai_Model_Prefix_CopyXml_CopyXmlServer + +oai.format.epicur.class = Oai_Model_Prefix_Epicur_EpicurServer +oai.format.marc21.class = Oai_Model_Prefix_MarcXml_MarcXmlServer +oai.format.oai_dc.class = Oai_Model_Prefix_Oai_OaiDcServer +oai.format.oai_pp.class = Oai_Model_Prefix_Oai_OaiPpServer +oai.format.xmetadissplus.class = Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer ; ERROR CONTROLLER SETTINGS errorController.showException = 0 diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index c8a32aacf..9d2b061c1 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -29,7 +29,9 @@ * @license http://www.gnu.org/licenses/gpl.html General Public License */ +use Opus\Common\Config\ConfigException; use Opus\Common\Document; +use Opus\Common\DocumentFinderInterface; use Opus\Common\DocumentInterface; use Opus\Common\Log; use Opus\Common\Model\NotFoundException; @@ -62,21 +64,6 @@ class Oai_Model_BaseServer extends Application_Model_Abstract */ protected $proc; - /** - * Holds information about which document state aka server_state - * are delivered out - * - * @var array - */ - private $deliveringDocumentStates = ['published', 'deleted']; // maybe deleted documents too - - /** - * Holds restriction types for xMetaDiss - * - * @var array - */ - private $xMetaDissRestriction = ['doctoralthesis', 'habilitation']; - /** @var Oai_Model_XmlFactory */ private $xmlFactory; @@ -122,6 +109,24 @@ class Oai_Model_BaseServer extends Application_Model_Abstract /** @var string */ private $viewHelper; + /** @var bool */ + private $checkEmbargo = false; + + /** @var array */ + private $documentTypesAllowed; + + /** @var array Holds information about which document state aka server_state are delivered out. */ + private $documentStatesAllowed = ['published', 'deleted']; // maybe deleted documents too + + /** @var bool */ + private $notEmbargoedOn = false; + + /** @var string */ + private $identifierExists = ''; + + /** @var bool */ + private $hasFilesVisibleInOai = false; + /** * Gather configuration before action handling. */ @@ -329,27 +334,8 @@ protected function handleGetRecord(array &$oaiRequest) $metadataPrefix = $oaiRequest['metadataPrefix']; - // do not deliver documents which are restricted by document state - if ( - $document === null - || (false === in_array($document->getServerState(), $this->deliveringDocumentStates)) - || (false === $document->hasEmbargoPassed() && stripos($metadataPrefix, 'xmetadiss') === 0) - ) { - throw new Oai_Model_Exception('Document is not available for OAI export!', Oai_Model_Error::NORECORDSMATCH); - } + $this->checkExportAllowed($document, $metadataPrefix); - // for xMetaDiss it must be habilitation-thesis or doctoral-thesis - if ('xMetaDiss' === $metadataPrefix) { - $type = $document->getType(); - $isHabOrDoc = in_array($type, $this->xMetaDissRestriction); - if (false === $isHabOrDoc) { - throw new Oai_Model_Exception( - "The combination of the given values results in an empty list (xMetaDiss only for habilitation" - . " and doctoralthesis).", - Oai_Model_Error::NORECORDSMATCH - ); - } - } $this->xml->appendChild($this->xml->createElement('Documents')); $this->createXmlRecord($document); @@ -521,11 +507,9 @@ private function handlingOfLists(array &$oaiRequest, $maxRecords) $resumed = true; } else { // no resumptionToken is given - $docListModel = new Oai_Model_DocumentList(); - $docListModel->deliveringDocumentStates = $this->deliveringDocumentStates; - $docListModel->xMetaDissRestriction = $this->xMetaDissRestriction; - $restIds = $docListModel->query($oaiRequest); - $totalIds = count($restIds); + $docListModel = new Oai_Model_DocumentList($this); + $restIds = $docListModel->query($oaiRequest); + $totalIds = count($restIds); } // handling of document ids @@ -743,7 +727,7 @@ private function getDocumentIdByIdentifier($oaiIdentifier) case 'urn': $finder = Repository::getInstance()->getDocumentFinder(); $finder->setIdentifierValue('urn', $oaiIdentifier); - $finder->setServerState($this->deliveringDocumentStates); + $finder->setServerState($this->getDocumentStatesAllowed()); $docIds = $finder->getIds(); $docId = $docIds[0]; break; @@ -777,7 +761,7 @@ private function getDocumentIdByIdentifier($oaiIdentifier) */ private function getDocumentXmlDomNode($document) { - if (! in_array($document->getServerState(), $this->deliveringDocumentStates)) { + if (! in_array($document->getServerState(), $this->getDocumentStatesAllowed())) { $message = 'Trying to get a document in server state "' . $document->getServerState() . '"'; Log::get()->err($message); throw new Exception($message); @@ -1093,4 +1077,188 @@ public function setViewHelper($viewHelper) { $this->viewHelper = $viewHelper; } + + /** + * Gets the checkEmbargo option. + * + * @return bool + */ + public function isCheckEmbargo() + { + return $this->checkEmbargo; + } + + /** + * Sets the checkEmbargo option. + * + * @param mixed $checkEmbargo + */ + public function setCheckEmbargo($checkEmbargo) + { + $this->checkEmbargo = filter_var($checkEmbargo, FILTER_VALIDATE_BOOLEAN); + } + + /** + * Gets the allowed document types. + * + * @return array + */ + public function getDocumentTypesAllowed() + { + return $this->documentTypesAllowed; + } + + /** + * Sets the allowed document types. + * + * @param array|string $documentTypesAllowed + */ + public function setdocumentTypesAllowed($documentTypesAllowed) + { + if (is_string($documentTypesAllowed)) { + $this->documentTypesAllowed = [$documentTypesAllowed]; + } else { + $this->documentTypesAllowed = $documentTypesAllowed; + } + } + + /** + * Gets the allowed document states. + * + * @return array + */ + public function getDocumentStatesAllowed() + { + return $this->documentStatesAllowed; + } + + /** + * Sets the allowed document states + * + * @param array|string $documentStatesAllowed + */ + public function setDocumentStatesAllowed($documentStatesAllowed) + { + if (is_string($documentStatesAllowed)) { + $this->documentStatesAllowed = [$documentStatesAllowed]; + } else { + $this->documentStatesAllowed = $documentStatesAllowed; + } + } + + /** + * @return bool + */ + public function isNotEmbargoedOn() + { + return $this->notEmbargoedOn; + } + + /** + * @param bool|string $notEmbargoedOn + */ + public function setNotEmbargoedOn($notEmbargoedOn) + { + $this->notEmbargoedOn = filter_var($notEmbargoedOn, FILTER_VALIDATE_BOOLEAN); + } + + /** + * @return string + */ + public function getIdentifierExists() + { + return $this->identifierExists; + } + + /** + * @param string $identifierExists + */ + public function setIdentifierExists($identifierExists) + { + $this->identifierExists = $identifierExists; + } + + /** + * @return bool + */ + public function isHasFilesVisibleInOai() + { + return $this->hasFilesVisibleInOai; + } + + /** + * @param bool|string $hasFilesVisibleInOai + */ + public function setHasFilesVisibleInOai($hasFilesVisibleInOai) + { + $this->hasFilesVisibleInOai = filter_var($hasFilesVisibleInOai, FILTER_VALIDATE_BOOLEAN); + } + + /** + * Checks if exporting a document is allowed, if not an exception will be thrown. + * + * @param Document $document + * @param string $metadataPrefix + * @return void + * @throws Oai_Model_Exception + */ + protected function checkExportAllowed($document, $metadataPrefix) + { + // do not deliver documents which are restricted by document state + if ( + $document === null + || (! in_array($document->getServerState(), $this->getDocumentStatesAllowed())) + || (! $document->hasEmbargoPassed() && $this->isCheckEmbargo()) + ) { + throw new Oai_Model_Exception('Document is not available for OAI export!', Oai_Model_Error::NORECORDSMATCH); + } + + $documentTypeRestriction = $this->getDocumentTypesAllowed(); + if ($documentTypeRestriction) { + $type = $document->getType(); + if (! in_array($type, $documentTypeRestriction)) { + throw new Oai_Model_Exception( + 'The combination of the given values results in an empty list (' . $metadataPrefix + . ' only for' . implode(', ', $documentTypeRestriction) . ')', + Oai_Model_Error::NORECORDSMATCH + ); + } + } + } + + /** + * Get the initialized finder for the given metadata prefix + * + * @param string $metadataPrefix + * @return DocumentFinderInterface + * @throws ConfigException + */ + public function getFinder($metadataPrefix) + { + $today = date('Y-m-d', time()); + + $finder = Repository::getInstance()->getDocumentFinder(); + + // add server state restrictions + $finder->setServerState($this->getDocumentStatesAllowed()); + + if ($this->isHasFilesVisibleInOai()) { + $finder->setHasFilesVisibleInOai(); + } + + $documentTypesAllowed = $this->getDocumentTypesAllowed(); + if ($documentTypesAllowed) { + $finder->setDocumentType($documentTypesAllowed); + } + + if ($this->isNotEmbargoedOn()) { + $finder->setNotEmbargoedOn($today); + } + + if ($this->getIdentifierExists()) { + $finder->setIdentifierExists($this->getIdentifierExists()); + } + + return $finder; + } } diff --git a/modules/oai/models/DocumentList.php b/modules/oai/models/DocumentList.php index 80a112632..5450fa10c 100644 --- a/modules/oai/models/DocumentList.php +++ b/modules/oai/models/DocumentList.php @@ -31,28 +31,19 @@ */ use Opus\Common\CollectionRole; -use Opus\Common\Repository; class Oai_Model_DocumentList { - /** - * Holds information about which document state aka server_state - * are delivered out - * - * @var array - * - * TODO should be private - */ - public $deliveringDocumentStates; + /** @var Oai_Model_BaseServer */ + protected $server; /** - * Holds restriction types for xMetaDiss - * - * @var array - * - * TODO should be private + * @param Oai_Model_BaseServer $server */ - public $xMetaDissRestriction; + public function __construct($server) + { + $this->server = $server; + } /** * Retrieve all document ids for a valid oai request. @@ -64,29 +55,9 @@ class Oai_Model_DocumentList */ public function query(array $oaiRequest) { - $today = date('Y-m-d', time()); - - $finder = Repository::getInstance()->getDocumentFinder(); - - // add server state restrictions - $finder->setServerState($this->deliveringDocumentStates); - $metadataPrefix = strtolower($oaiRequest['metadataPrefix']); - if ( - strcmp('xmetadissplus', $metadataPrefix) === 0 - || 'xmetadiss' === $metadataPrefix - ) { - $finder->setHasFilesVisibleInOai(); - $finder->setNotEmbargoedOn($today); - } - if ('xmetadiss' === $metadataPrefix) { - $finder->setDocumentType($this->xMetaDissRestriction); - $finder->setNotEmbargoedOn($today); - } - if ('epicur' === $metadataPrefix) { - $finder->setIdentifierExists('urn'); - } + $finder = $this->server->getFinder($metadataPrefix); if (array_key_exists('set', $oaiRequest)) { $setarray = explode(':', $oaiRequest['set']); diff --git a/modules/oai/models/Request.php b/modules/oai/models/Request.php index da1efbf32..a6b5226ee 100644 --- a/modules/oai/models/Request.php +++ b/modules/oai/models/Request.php @@ -31,7 +31,6 @@ */ use Opus\Common\Log; -use Opus\Common\Security\Realm; /** * TODO BUG documentation is not existent - especially the fact that 'validate' functions are called dynamically @@ -139,23 +138,11 @@ public function checkDate($datestr) */ public function validateMetadataPrefix($oaiMetadataPrefix) { - // we assuming that a metadata prefix file ends with xslt - $possibleFiles = glob($this->pathToMetadataPrefixFiles . DIRECTORY_SEPARATOR . '*.xslt'); + $serverFactory = new Oai_Model_ServerFactory(); + $availableMetadataPrefixes = array_map('strtolower', $serverFactory->getFormats()); + $result = in_array(strtolower($oaiMetadataPrefix), $availableMetadataPrefixes); - // we support both spellings, xMetaDissPlus and XMetaDissPlus TODO really? - $availableMetadataPrefixes = ['xMetaDissPlus']; - foreach ($possibleFiles as $prefixFile) { - $availableMetadataPrefixes[] = strtolower(basename($prefixFile, '.xslt')); - } - - // only administrators can request copy_xml format - if (! Realm::getInstance()->checkModule('admin')) { - $availableMetadataPrefixes = array_diff($availableMetadataPrefixes, ['copy_xml']); - } - - $result = in_array(strtolower($oaiMetadataPrefix), $availableMetadataPrefixes); - - if (false === $result) { + if (! $result) { // MetadataPrefix not available. $this->setErrorCode(Oai_Model_Error::CANNOTDISSEMINATEFORMAT); $this->setErrorMessage( diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 2127511a5..6c2bdfbe1 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -30,6 +30,7 @@ */ use Opus\Common\ConfigTrait; +use Opus\Common\Security\Realm; use Opus\Common\Util\ClassLoaderHelper; /** @@ -115,18 +116,19 @@ public function getFormatOptions($metadataPrefix = '') $options = array_merge($generalOptions, $defaultOptions, $formatOptions); if (isset($options['viewHelper'])) { - $defaultViewHelper = $defaultOptions['viewHelper'] ?? []; - $formatViewHelper = $formatOptions['viewHelper'] ?? []; - - if (is_string($defaultViewHelper)) { - $defaultViewHelper = array_map('trim', explode(',', $defaultViewHelper)); - } - - if (is_string($formatViewHelper)) { - $formatViewHelper = array_map('trim', explode(',', $formatViewHelper)); - } + $options['viewHelper'] = $this->mergeMultiValueOption( + 'viewHelper', + $defaultOptions, + $formatOptions + ); + } - $options['viewHelper'] = array_unique(array_merge($defaultViewHelper, $formatViewHelper)); + if (isset($options['documentTypeRestriction'])) { + $options['documentTypeRestriction'] = $this->mergeMultiValueOption( + 'documentTypeRestriction', + $defaultOptions, + $formatOptions + ); } return $options; @@ -179,4 +181,71 @@ public function getGeneralOaiOptions() return $options; } + + /** + * Merges the default and format specific configuration for an option + * containing multiple values as an array or comma separated list + * + * @param string $optionName + * @param array $defaultOptions + * @param array $formatOptions + * @return array + */ + protected function mergeMultiValueOption($optionName, $defaultOptions, $formatOptions) + { + $default = $defaultOptions[$optionName] ?? []; + $format = $formatOptions[$optionName] ?? []; + + if (is_string($default)) { + $default = array_map('trim', explode(',', $default)); + } + + if (is_string($format)) { + $format = array_map('trim', explode(',', $format)); + } + + return array_unique(array_merge($default, $format)); + } + + /** + * Gets all configured format prefixes + * + * @return array + */ + public function getFormats() + { + $config = $this->getConfig(); + + $prefixes = []; + + if (isset($config->oai->format)) { + $formats = $config->oai->format->toArray(); + + foreach ($formats as $formatIdentifier => $format) { + if (isset($format['prefixLabel'])) { + $prefixLabels = $format['prefixLabel']; + if (is_string($prefixLabels)) { + $prefixLabels = array_map('trim', explode(',', $prefixLabels)); + } + + foreach ($prefixLabels as $prefixLabel) { + if ($prefixLabel) { + $prefixes[] = $prefixLabel; + } + } + } else { + $prefixes[] = $formatIdentifier; + } + } + } + + $prefixes = array_diff($prefixes, ['default']); + + // only administrators can request copy_xml format + if (! Realm::getInstance()->checkModule('admin')) { + $prefixes = array_diff($prefixes, ['copy_xml']); + } + + return $prefixes; + } } diff --git a/modules/oai/models/prefix/CopyXml/CopyXmlServer.php b/modules/oai/models/prefix/CopyXml/CopyXmlServer.php new file mode 100644 index 000000000..2a5060f82 --- /dev/null +++ b/modules/oai/models/prefix/CopyXml/CopyXmlServer.php @@ -0,0 +1,37 @@ +setXsltFile('copy_xml.xslt'); + } +} diff --git a/modules/oai/models/prefix/epicur/EpicurServer.php b/modules/oai/models/prefix/epicur/EpicurServer.php new file mode 100644 index 000000000..773d37669 --- /dev/null +++ b/modules/oai/models/prefix/epicur/EpicurServer.php @@ -0,0 +1,38 @@ +setIdentifierExists('urn'); + $this->setXsltFile('epicur.xslt'); + } +} diff --git a/modules/oai/models/prefix/oai/OaiDcServer.php b/modules/oai/models/prefix/oai/OaiDcServer.php new file mode 100644 index 000000000..549ffd661 --- /dev/null +++ b/modules/oai/models/prefix/oai/OaiDcServer.php @@ -0,0 +1,37 @@ +setXsltFile('oai_dc.xslt'); + } +} diff --git a/modules/oai/models/prefix/oai/OaiPpServer.php b/modules/oai/models/prefix/oai/OaiPpServer.php new file mode 100644 index 000000000..7343efe78 --- /dev/null +++ b/modules/oai/models/prefix/oai/OaiPpServer.php @@ -0,0 +1,37 @@ +setXsltFile('oai_pp.xslt'); + } +} diff --git a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php new file mode 100644 index 000000000..b42cf38a2 --- /dev/null +++ b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php @@ -0,0 +1,40 @@ +setHasFilesVisibleInOai(true); + $this->setCheckEmbargo(true); + $this->setNotEmbargoedOn(true); + $this->setXsltFile('XMetaDissPlus.xslt'); + } +} diff --git a/tests/modules/oai/models/DocumentListTest.php b/tests/modules/oai/models/DocumentListTest.php index 0a133274a..29e6a7656 100644 --- a/tests/modules/oai/models/DocumentListTest.php +++ b/tests/modules/oai/models/DocumentListTest.php @@ -54,10 +54,12 @@ public function testDocumentOutputUrn() $docWoUrn->setServerState('published'); $docWoUrnId = $docWoUrn->store(); - $oaiRequest = ['metadataPrefix' => 'epicur']; - $docListModel = new Oai_Model_DocumentList(); - $docListModel->deliveringDocumentStates = ['published']; - $docIds = $docListModel->query($oaiRequest); + $oaiRequest = ['metadataPrefix' => 'epicur']; + $serverFactory = new Oai_Model_ServerFactory(); + $server = $serverFactory->create('epicur'); + $server->setDocumentStatesAllowed(['published']); + $docListModel = new Oai_Model_DocumentList($server); + $docIds = $docListModel->query($oaiRequest); $this->assertTrue(in_array($docWithUrnId, $docIds), 'Document with URN is not returned.'); $this->assertFalse(in_array($docWoUrnId, $docIds), 'Document without URN is returned.'); @@ -112,10 +114,10 @@ public function testIntervalOAIPMHQueries() $oaiRequest = ['verb' => 'ListRecords', 'metadataPrefix' => 'XMetaDissPlus']; $oaiRequest = array_merge($interval, $oaiRequest); - $docListModel = new Oai_Model_DocumentList(); - $docListModel->deliveringDocumentStates = ['published', 'deleted']; - $docListModel->xMetaDissRestriction = []; - $docIds = $docListModel->query($oaiRequest); + $serverFactory = new Oai_Model_ServerFactory(); + $server = $serverFactory->create('XMetaDissPlus'); + $docListModel = new Oai_Model_DocumentList($server); + $docIds = $docListModel->query($oaiRequest); $this->assertTrue( in_array($this->docId, $docIds), @@ -172,10 +174,10 @@ public function testIntervalOAIPMHQueryWithoutTestDoc() $oaiRequest = ['verb' => 'ListRecords', 'metadataPrefix' => 'XMetaDissPlus']; $oaiRequest = array_merge($interval, $oaiRequest); - $docListModel = new Oai_Model_DocumentList(); - $docListModel->deliveringDocumentStates = ['published', 'deleted']; - $docListModel->xMetaDissRestriction = []; - $docIds = $docListModel->query($oaiRequest); + $serverFactory = new Oai_Model_ServerFactory(); + $server = $serverFactory->create('XMetaDissPlus'); + $docListModel = new Oai_Model_DocumentList($server); + $docIds = $docListModel->query($oaiRequest); $this->assertFalse(in_array($this->docId, $docIds), "Response must NOT contain document id $this->docId: " . var_export($interval, true)); } @@ -199,10 +201,10 @@ public function testOnlyFilesVisibleInOaiIncludedForXMetaDissPlus() $oaiRequest = ['verb' => 'ListRecords', 'metadataPrefix' => 'XMetaDissPlus']; - $docListModel = new Oai_Model_DocumentList(); - $docListModel->deliveringDocumentStates = ['published', 'deleted']; - $docListModel->xMetaDissRestriction = []; - $docIds = $docListModel->query($oaiRequest); + $serverFactory = new Oai_Model_ServerFactory(); + $server = $serverFactory->create('XMetaDissPlus'); + $docListModel = new Oai_Model_DocumentList($server); + $docIds = $docListModel->query($oaiRequest); $this->assertTrue( in_array($docIdIncluded, $docIds), @@ -217,10 +219,10 @@ public function testOnlyFilesVisibleInOaiIncludedForXMetaDissPlus() $oaiRequest = ['verb' => 'ListRecords', 'metadataPrefix' => 'xMetaDissPlus']; - $docListModel = new Oai_Model_DocumentList(); - $docListModel->deliveringDocumentStates = ['published', 'deleted']; - $docListModel->xMetaDissRestriction = []; - $docIds = $docListModel->query($oaiRequest); + $serverFactory = new Oai_Model_ServerFactory(); + $server = $serverFactory->create('XMetaDissPlus'); + $docListModel = new Oai_Model_DocumentList($server); + $docIds = $docListModel->query($oaiRequest); $this->assertTrue( in_array($docIdIncluded, $docIds), From 23621bcc94835d9d8ba3083893a2679f59576a91 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 5 Sep 2023 16:58:35 +0200 Subject: [PATCH 25/59] #1101 Generate OAI ListMetadataFormats from configuration. --- application/configs/application.ini | 12 +- modules/oai/models/BaseServer.php | 105 +++++++++++++++++- modules/oai/models/OptionsTrait.php | 7 +- modules/oai/models/Request.php | 6 +- modules/oai/models/ServerFactory.php | 29 ++--- .../models/prefix/CopyXml/CopyXmlServer.php | 37 ------ .../oai/models/prefix/epicur/EpicurServer.php | 2 + .../models/prefix/marcxml/MarcXmlServer.php | 4 +- modules/oai/models/prefix/oai/OaiDcServer.php | 2 + modules/oai/models/prefix/oai/OaiPpServer.php | 1 + .../xmetadissplus/XMetaDissPlusServer.php | 3 + modules/oai/views/scripts/index/oai-pmh.xslt | 26 +---- .../oai/controllers/IndexControllerTest.php | 3 + 13 files changed, 146 insertions(+), 91 deletions(-) delete mode 100644 modules/oai/models/prefix/CopyXml/CopyXmlServer.php diff --git a/application/configs/application.ini b/application/configs/application.ini index 7fef1597e..eb2c0f22b 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -111,17 +111,19 @@ oai.description.eprints.comment.text = ; oai.format.[lower cased format identifier ].prefix = [prefix] oai.format.default.class = Oai_Model_BaseServer oai.format.default.viewHelper = optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType -oai.format.default.checkEmbargo = 0 -oai.format.default.hasFilesVisibleInOai = 0 +oai.format.default.adminOnly = 0 +oai.format.default.visible = 1 ; OPUS 4 XML (only for admins) -oai.format.copy_xml.class = Oai_Model_Prefix_CopyXml_CopyXmlServer +oai.format.copy_xml.xsltFile = copy_xml.xslt +oai.format.copy_xml.adminOnly = 1 +oai.format.copy_xml.visible = 0 -oai.format.epicur.class = Oai_Model_Prefix_Epicur_EpicurServer -oai.format.marc21.class = Oai_Model_Prefix_MarcXml_MarcXmlServer oai.format.oai_dc.class = Oai_Model_Prefix_Oai_OaiDcServer oai.format.oai_pp.class = Oai_Model_Prefix_Oai_OaiPpServer +oai.format.epicur.class = Oai_Model_Prefix_Epicur_EpicurServer oai.format.xmetadissplus.class = Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer +oai.format.marc21.class = Oai_Model_Prefix_MarcXml_MarcXmlServer ; ERROR CONTROLLER SETTINGS errorController.showException = 0 diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/BaseServer.php index 9d2b061c1..e1f60f2fe 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/BaseServer.php @@ -127,6 +127,21 @@ class Oai_Model_BaseServer extends Application_Model_Abstract /** @var bool */ private $hasFilesVisibleInOai = false; + /** @var array */ + private $prefixLabel; + + /** @var string */ + private $schemaUrl; + + /** @var string */ + private $metadataNamespaceUrl; + + /** @var bool */ + private $adminOnly = false; + + /** @var bool */ + private $visible = true; + /** * Gather configuration before action handling. */ @@ -1065,7 +1080,11 @@ public function setXsltFile($file) */ public function getViewHelper() { - return $this->viewHelper ?? []; + $viewHelper = $this->viewHelper ?? []; + + $viewHelper[] = 'listMetadataFormats'; + + return $viewHelper; } /** @@ -1194,6 +1213,90 @@ public function setHasFilesVisibleInOai($hasFilesVisibleInOai) $this->hasFilesVisibleInOai = filter_var($hasFilesVisibleInOai, FILTER_VALIDATE_BOOLEAN); } + /** + * @return array + */ + public function getPrefixLabel() + { + return $this->prefixLabel; + } + + /** + * @param array|string $prefixLabel + */ + public function setPrefixLabel($prefixLabel) + { + if (is_string($prefixLabel)) { + $this->prefixLabel = [$prefixLabel]; + } else { + $this->prefixLabel = $prefixLabel; + } + } + + /** + * @return string + */ + public function getSchemaUrl() + { + return $this->schemaUrl; + } + + /** + * @param string $schemaUrl + */ + public function setSchemaUrl($schemaUrl) + { + $this->schemaUrl = $schemaUrl; + } + + /** + * @return string + */ + public function getMetadataNamespaceUrl() + { + return $this->metadataNamespaceUrl; + } + + /** + * @param string $metadataNamespaceUrl + */ + public function setMetadataNamespaceUrl($metadataNamespaceUrl) + { + $this->metadataNamespaceUrl = $metadataNamespaceUrl; + } + + /** + * @return bool + */ + public function isAdminOnly() + { + return $this->adminOnly; + } + + /** + * @param mixed $adminOnly + */ + public function setAdminOnly($adminOnly) + { + $this->adminOnly = filter_var($adminOnly, FILTER_VALIDATE_BOOLEAN); + } + + /** + * @return bool + */ + public function isVisible() + { + return $this->visible; + } + + /** + * @param bool|string $visible + */ + public function setVisible($visible) + { + $this->visible = filter_var($visible, FILTER_VALIDATE_BOOLEAN); + } + /** * Checks if exporting a document is allowed, if not an exception will be thrown. * diff --git a/modules/oai/models/OptionsTrait.php b/modules/oai/models/OptionsTrait.php index 8ed75f1fb..53227b564 100644 --- a/modules/oai/models/OptionsTrait.php +++ b/modules/oai/models/OptionsTrait.php @@ -47,6 +47,11 @@ public function getOptions($optionNames) if (method_exists($this, $method)) { $options[$optionName] = $this->$method(); } + + $method = 'is' . ucfirst($optionName); + if (method_exists($this, $method)) { + $options[$optionName] = $this->$method(); + } } return $options; @@ -59,7 +64,7 @@ public function setOptions($options) { foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); - if (method_exists($this, $method) && $value) { + if (method_exists($this, $method)) { $this->$method($value); } } diff --git a/modules/oai/models/Request.php b/modules/oai/models/Request.php index a6b5226ee..a854d45f9 100644 --- a/modules/oai/models/Request.php +++ b/modules/oai/models/Request.php @@ -31,6 +31,7 @@ */ use Opus\Common\Log; +use Opus\Common\Security\Realm; /** * TODO BUG documentation is not existent - especially the fact that 'validate' functions are called dynamically @@ -139,8 +140,11 @@ public function checkDate($datestr) public function validateMetadataPrefix($oaiMetadataPrefix) { $serverFactory = new Oai_Model_ServerFactory(); + $server = $serverFactory->create(strtolower($oaiMetadataPrefix)); $availableMetadataPrefixes = array_map('strtolower', $serverFactory->getFormats()); - $result = in_array(strtolower($oaiMetadataPrefix), $availableMetadataPrefixes); + + $result = in_array(strtolower($oaiMetadataPrefix), $availableMetadataPrefixes) + && (! $server->isAdminOnly() || Realm::getInstance()->checkModule('admin')); if (! $result) { // MetadataPrefix not available. diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 6c2bdfbe1..441cd1c77 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -30,7 +30,6 @@ */ use Opus\Common\ConfigTrait; -use Opus\Common\Security\Realm; use Opus\Common\Util\ClassLoaderHelper; /** @@ -131,6 +130,14 @@ public function getFormatOptions($metadataPrefix = '') ); } + if (isset($options['prefixLabel'])) { + $options['prefixLabel'] = $this->mergeMultiValueOption( + 'prefixLabel', + $defaultOptions, + $formatOptions + ); + } + return $options; } @@ -222,30 +229,12 @@ public function getFormats() $formats = $config->oai->format->toArray(); foreach ($formats as $formatIdentifier => $format) { - if (isset($format['prefixLabel'])) { - $prefixLabels = $format['prefixLabel']; - if (is_string($prefixLabels)) { - $prefixLabels = array_map('trim', explode(',', $prefixLabels)); - } - - foreach ($prefixLabels as $prefixLabel) { - if ($prefixLabel) { - $prefixes[] = $prefixLabel; - } - } - } else { - $prefixes[] = $formatIdentifier; - } + $prefixes[] = $formatIdentifier; } } $prefixes = array_diff($prefixes, ['default']); - // only administrators can request copy_xml format - if (! Realm::getInstance()->checkModule('admin')) { - $prefixes = array_diff($prefixes, ['copy_xml']); - } - return $prefixes; } } diff --git a/modules/oai/models/prefix/CopyXml/CopyXmlServer.php b/modules/oai/models/prefix/CopyXml/CopyXmlServer.php deleted file mode 100644 index 2a5060f82..000000000 --- a/modules/oai/models/prefix/CopyXml/CopyXmlServer.php +++ /dev/null @@ -1,37 +0,0 @@ -setXsltFile('copy_xml.xslt'); - } -} diff --git a/modules/oai/models/prefix/epicur/EpicurServer.php b/modules/oai/models/prefix/epicur/EpicurServer.php index 773d37669..060410af5 100644 --- a/modules/oai/models/prefix/epicur/EpicurServer.php +++ b/modules/oai/models/prefix/epicur/EpicurServer.php @@ -34,5 +34,7 @@ public function __construct() { $this->setIdentifierExists('urn'); $this->setXsltFile('epicur.xslt'); + $this->setSchemaUrl('http://www.persistent-identifier.de/xepicur/version1.0/xepicur.xsd'); + $this->setMetadataNamespaceUrl('urn:nbn:de:1111-2004033116'); } } diff --git a/modules/oai/models/prefix/marcxml/MarcXmlServer.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php index 54f663e50..fcb322249 100644 --- a/modules/oai/models/prefix/marcxml/MarcXmlServer.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -32,7 +32,9 @@ class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_BaseServer { public function __construct() { - // Default value for the xsltFile, may be overwritten later by the configuration (using the setOptions method). $this->setXsltFile('marc21.xslt'); + $this->setPrefixLabel('MARC21'); + $this->setSchemaUrl('https://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd'); + $this->setMetadataNamespaceUrl('http://www.loc.gov/MARC21/slim'); } } diff --git a/modules/oai/models/prefix/oai/OaiDcServer.php b/modules/oai/models/prefix/oai/OaiDcServer.php index 549ffd661..92eb759a2 100644 --- a/modules/oai/models/prefix/oai/OaiDcServer.php +++ b/modules/oai/models/prefix/oai/OaiDcServer.php @@ -33,5 +33,7 @@ class Oai_Model_Prefix_Oai_OaiDcServer extends Oai_Model_BaseServer public function __construct() { $this->setXsltFile('oai_dc.xslt'); + $this->setSchemaUrl('http://www.openarchives.org/OAI/2.0/oai_dc.xsd'); + $this->setMetadataNamespaceUrl('http://www.openarchives.org/OAI/2.0/oai_dc/'); } } diff --git a/modules/oai/models/prefix/oai/OaiPpServer.php b/modules/oai/models/prefix/oai/OaiPpServer.php index 7343efe78..10b0c674a 100644 --- a/modules/oai/models/prefix/oai/OaiPpServer.php +++ b/modules/oai/models/prefix/oai/OaiPpServer.php @@ -33,5 +33,6 @@ class Oai_Model_Prefix_Oai_OaiPpServer extends Oai_Model_BaseServer public function __construct() { $this->setXsltFile('oai_pp.xslt'); + $this->setVisible(false); } } diff --git a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php index b42cf38a2..0ae789b37 100644 --- a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php +++ b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php @@ -36,5 +36,8 @@ public function __construct() $this->setCheckEmbargo(true); $this->setNotEmbargoedOn(true); $this->setXsltFile('XMetaDissPlus.xslt'); + $this->setPrefixLabel(['xMetaDissPlus']); + $this->setSchemaUrl('http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd'); + $this->setMetadataNamespaceUrl('http://www.d-nb.de/standards/xmetadissplus/'); } } diff --git a/modules/oai/views/scripts/index/oai-pmh.xslt b/modules/oai/views/scripts/index/oai-pmh.xslt index 27e434530..efc38796f 100644 --- a/modules/oai/views/scripts/index/oai-pmh.xslt +++ b/modules/oai/views/scripts/index/oai-pmh.xslt @@ -236,31 +236,7 @@ - - oai_dc - http://www.openarchives.org/OAI/2.0/oai_dc.xsd - http://www.openarchives.org/OAI/2.0/oai_dc/ - - - epicur - http://www.persistent-identifier.de/xepicur/version1.0/xepicur.xsd - urn:nbn:de:1111-2004033116 - - - XMetaDissPlus - http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd - http://www.d-nb.de/standards/xmetadissplus/ - - - xMetaDissPlus - http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd - http://www.d-nb.de/standards/xmetadissplus/ - - - MARC21 - https://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd - http://www.loc.gov/MARC21/slim - + diff --git a/tests/modules/oai/controllers/IndexControllerTest.php b/tests/modules/oai/controllers/IndexControllerTest.php index acf2651e4..f6c0a41b3 100644 --- a/tests/modules/oai/controllers/IndexControllerTest.php +++ b/tests/modules/oai/controllers/IndexControllerTest.php @@ -280,6 +280,9 @@ public function testListMetadataFormats() $this->assertResponseCode(200); $response = $this->getResponse(); + + $this->assertEquals('', $response->getBody()); + $this->checkForBadStringsInHtml($response->getBody()); } From 1b27ea937f1c45e56464a602f9e7f89117021fd3 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 5 Sep 2023 16:59:35 +0200 Subject: [PATCH 26/59] #1101 Add xslt viewhelper for generating the OAI ListMetadataFormats. --- .../oai/views/helpers/ListMetaDataFormats.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 modules/oai/views/helpers/ListMetaDataFormats.php diff --git a/modules/oai/views/helpers/ListMetaDataFormats.php b/modules/oai/views/helpers/ListMetaDataFormats.php new file mode 100644 index 000000000..da54cf8e3 --- /dev/null +++ b/modules/oai/views/helpers/ListMetaDataFormats.php @@ -0,0 +1,75 @@ +getFormats(); + + if ($formats) { + foreach ($formats as $formatPrefix) { + $server = $serverFactory->create($formatPrefix); + $prefixLabel = $server->getPrefixLabel(); + $prefixes = $prefixLabel ?? [$formatPrefix]; + $schemaUrl = $server->getSchemaUrl(); + $metadataNamespaceUrl = $server->getMetadataNamespaceUrl(); + + if ($server->isVisible() && (! $server->isAdminOnly() || Realm::getInstance()->checkModule('admin'))) { + foreach ($prefixes as $prefix) { + $output .= '' + . '' . $prefix . '' + . '' . $schemaUrl . '' + . '' . $metadataNamespaceUrl . '' + . ''; + } + } + } + } + + return $output; + } +} From 99c919ee41421236fd42d08024254c9754e246e0 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 6 Sep 2023 09:49:12 +0200 Subject: [PATCH 27/59] #1101 Made string composition more readable. --- modules/oai/views/helpers/ListMetaDataFormats.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/oai/views/helpers/ListMetaDataFormats.php b/modules/oai/views/helpers/ListMetaDataFormats.php index da54cf8e3..8aaccc287 100644 --- a/modules/oai/views/helpers/ListMetaDataFormats.php +++ b/modules/oai/views/helpers/ListMetaDataFormats.php @@ -61,9 +61,9 @@ public function listMetadataFormats() if ($server->isVisible() && (! $server->isAdminOnly() || Realm::getInstance()->checkModule('admin'))) { foreach ($prefixes as $prefix) { $output .= '' - . '' . $prefix . '' - . '' . $schemaUrl . '' - . '' . $metadataNamespaceUrl . '' + . "$prefix" + . "$schemaUrl" + . "$metadataNamespaceUrl" . ''; } } From a24cf6172c1ca160b2ba539fe47c28534be46a97 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Wed, 6 Sep 2023 17:03:54 +0200 Subject: [PATCH 28/59] #1101 Reworked the behavior of the incremental option merging so that the derived classes can overwrite default values, but the format configuration can overwrite them again. --- application/configs/application.ini | 4 +- .../{BaseServer.php => DefaultServer.php} | 53 ++- modules/oai/models/DocumentList.php | 4 +- modules/oai/models/OAIConfig.php | 143 +++++++ modules/oai/models/ServerFactory.php | 193 +++------ .../oai/models/prefix/epicur/EpicurServer.php | 4 +- .../models/prefix/marcxml/MarcXmlServer.php | 4 +- modules/oai/models/prefix/oai/OaiDcServer.php | 4 +- modules/oai/models/prefix/oai/OaiPpServer.php | 4 +- .../xmetadissplus/XMetaDissPlusServer.php | 6 +- .../oai/views/helpers/ListMetaDataFormats.php | 5 +- .../oai/controllers/IndexControllerTest.php | 8 +- tests/modules/oai/models/BaseServerTest.php | 8 +- tests/modules/oai/models/OAIConfigTest.php | 195 +++++++++ .../modules/oai/models/ServerFactoryTest.php | 397 +++++++----------- tests/support/DefaultOaiServer.php | 2 +- tests/support/OaiDcServer.php | 2 +- tests/support/XMetaDissPlusServer.php | 44 ++ 18 files changed, 661 insertions(+), 419 deletions(-) rename modules/oai/models/{BaseServer.php => DefaultServer.php} (96%) create mode 100644 modules/oai/models/OAIConfig.php create mode 100644 tests/modules/oai/models/OAIConfigTest.php create mode 100644 tests/support/XMetaDissPlusServer.php diff --git a/application/configs/application.ini b/application/configs/application.ini index eb2c0f22b..5dc5ca2ad 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -109,10 +109,8 @@ oai.description.eprints.comment.text = ; oai.format.[lower cased format identifier ].xsltFile = [xslt file name] ; The format prefix ; oai.format.[lower cased format identifier ].prefix = [prefix] -oai.format.default.class = Oai_Model_BaseServer +oai.format.default.class = Oai_Model_DefaultServer oai.format.default.viewHelper = optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType -oai.format.default.adminOnly = 0 -oai.format.default.visible = 1 ; OPUS 4 XML (only for admins) oai.format.copy_xml.xsltFile = copy_xml.xslt diff --git a/modules/oai/models/BaseServer.php b/modules/oai/models/DefaultServer.php similarity index 96% rename from modules/oai/models/BaseServer.php rename to modules/oai/models/DefaultServer.php index e1f60f2fe..ac83f16bb 100644 --- a/modules/oai/models/BaseServer.php +++ b/modules/oai/models/DefaultServer.php @@ -39,7 +39,7 @@ use Opus\Model\Xml; use Opus\Model\Xml\Version1; -class Oai_Model_BaseServer extends Application_Model_Abstract +class Oai_Model_DefaultServer extends Application_Model_Abstract { use Oai_Model_OptionsTrait; @@ -142,6 +142,35 @@ class Oai_Model_BaseServer extends Application_Model_Abstract /** @var bool */ private $visible = true; + /** + * @param Zend_Config $config + */ + public function __construct($config = null) + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + + if ($config) { + $oaiConfig->setConfig($config); + } + + $defaults = $oaiConfig->getDefaults(); + + $this->setOptions($defaults); + + $this->initDefaults(); + } + + /** + * Initializes server options with default values + */ + protected function initDefaults() + { + /* + This function is used by derived classes to set their own values for options by overwriting the method + and using setters, in order to overwrite default values. + */ + } + /** * Gather configuration before action handling. */ @@ -164,7 +193,7 @@ public function init() */ public function handleRequest($parameters, $requestUri) { - // TODO move error handling into Oai_Model_BaseServer + // TODO move error handling into Oai_Model_DefaultServer try { // handle request return $this->handleRequestIntern($parameters, $requestUri); @@ -1082,6 +1111,7 @@ public function getViewHelper() { $viewHelper = $this->viewHelper ?? []; + // listMetadataFormats ist part of basic OAI functionality. $viewHelper[] = 'listMetadataFormats'; return $viewHelper; @@ -1090,10 +1120,17 @@ public function getViewHelper() /** * Sets the viewHelper * - * @param array $viewHelper + * @param array|string $viewHelper */ public function setViewHelper($viewHelper) { + if (is_string($viewHelper)) { + $viewHelper = array_map('trim', explode(',', $viewHelper)); + } + + // listMetadataFormats ist part of basic OAI functionality. + $viewHelper = array_values(array_diff($viewHelper, ['listMetadataFormats'])); + $this->viewHelper = $viewHelper; } @@ -1214,7 +1251,7 @@ public function setHasFilesVisibleInOai($hasFilesVisibleInOai) } /** - * @return array + * @return string */ public function getPrefixLabel() { @@ -1222,15 +1259,11 @@ public function getPrefixLabel() } /** - * @param array|string $prefixLabel + * @param string $prefixLabel */ public function setPrefixLabel($prefixLabel) { - if (is_string($prefixLabel)) { - $this->prefixLabel = [$prefixLabel]; - } else { - $this->prefixLabel = $prefixLabel; - } + $this->prefixLabel = $prefixLabel; } /** diff --git a/modules/oai/models/DocumentList.php b/modules/oai/models/DocumentList.php index 5450fa10c..ed65e3ebb 100644 --- a/modules/oai/models/DocumentList.php +++ b/modules/oai/models/DocumentList.php @@ -34,11 +34,11 @@ class Oai_Model_DocumentList { - /** @var Oai_Model_BaseServer */ + /** @var Oai_Model_DefaultServer */ protected $server; /** - * @param Oai_Model_BaseServer $server + * @param Oai_Model_DefaultServer $server */ public function __construct($server) { diff --git a/modules/oai/models/OAIConfig.php b/modules/oai/models/OAIConfig.php new file mode 100644 index 000000000..f82dadd59 --- /dev/null +++ b/modules/oai/models/OAIConfig.php @@ -0,0 +1,143 @@ +getConfig(); + + $generalOptions = $this->getGeneralOaiOptions(); + + $defaultOptions = []; + + if (isset($config->oai->format->default)) { + $defaultOptions = $config->oai->format->default->toArray(); + } + + return array_merge($generalOptions, $defaultOptions); + } + + /** + * Gets the format specific options. + * + * @param string $metadataPrefix + * @return array + */ + public function getFormatOptions($metadataPrefix) + { + $metadataPrefix = strtolower($metadataPrefix); + + $config = $this->getConfig(); + + $formatOptions = []; + + if (isset($config->oai->format)) { + $formats = array_change_key_case($config->oai->format->toArray()); + + if (isset($formats[$metadataPrefix])) { + $formatOptions = $formats[$metadataPrefix]; + } + } + + return $formatOptions; + } + + /** + * Gets the general oai options from the configuration + * + * @return array + */ + protected function getGeneralOaiOptions() + { + $config = $this->getConfig(); + + if (! isset($config->oai)) { + throw new Exception('No configuration for module oai.'); + } + + $options = []; + + if (isset($config->oai->repository->name)) { + $options['repositoryName'] = $config->oai->repository->name; + } + if (isset($config->oai->repository->identifier)) { + $options['repositoryIdentifier'] = $config->oai->repository->identifier; + } + if (isset($config->oai->sample->identifier)) { + $options['sampleIdentifier'] = $config->oai->sample->identifier; + } + if (isset($config->oai->max->listidentifiers)) { + $options['maxListIdentifiers'] = (int) $config->oai->max->listidentifiers; + } + + if (isset($config->oai->max->listrecords)) { + $options['maxListRecords'] = (int) $config->oai->max->listrecords; + } + if (isset($config->oai->baseurl)) { + $options['oaiBaseUrl'] = $config->oai->baseurl; + } + + if (isset($config->workspacePath)) { + $options['resumptionTokenPath'] = $config->workspacePath + . DIRECTORY_SEPARATOR . 'tmp' + . DIRECTORY_SEPARATOR . 'resumption'; + } + + if (isset($config->mail->opus->address)) { + $options['emailContact'] = $config->mail->opus->address; + } + + return $options; + } +} diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 441cd1c77..a99b3998c 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -39,179 +39,99 @@ class Oai_Model_ServerFactory { use ConfigTrait; + /** @var Oai_Model_OAIConfig */ + private $oaiConfig; + + public function __constructor() + { + $this->oaiConfig = Oai_Model_OAIConfig::getInstance(); + } + /** - * Creates an oai server model by metaDataPrefix + * Gets the oai configuration * - * @param string $metaDataPrefix - * @return Oai_Model_BaseServer + * @return Oai_Model_OAIConfig */ - public function create($metaDataPrefix = '') + public function getOaiConfig() { - $options = $this->getFormatOptions($metaDataPrefix); - - $serverClass = $options['class'] ?? Oai_Model_BaseServer::class; - - if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { - $server = new Oai_Model_BaseServer(); - } else { - $server = new $serverClass(); + if ($this->oaiConfig === null) { + $this->oaiConfig = Oai_Model_OAIConfig::getInstance(); + $this->oaiConfig->setConfig($this->getConfig()); } - $server->setOptions($options); - - return $server; + return $this->oaiConfig; } /** - * Creates an oai server model by resumption token + * Sets the oai configuration * - * @param string $resumptionToken - * @return Oai_Model_BaseServer + * @param Oai_Model_OAIConfig $oaiConfig */ - public function createByResumptionToken($resumptionToken) + public function setOaiConfig($oaiConfig) { - $config = $this->getConfig(); - - if (isset($config->workspacePath)) { - $tempPath = $config->workspacePath - . DIRECTORY_SEPARATOR . 'tmp' - . DIRECTORY_SEPARATOR . 'resumption'; - } - $tokenWorker = new Oai_Model_Resumptiontokens(); - $tokenWorker->setResumptionPath($tempPath); - $token = $tokenWorker->getResumptionToken($resumptionToken); - - if ($token === null) { - throw new Oai_Model_Exception("file could not be read.", Oai_Model_Error::BADRESUMPTIONTOKEN); - } - - return $this->create($token->getMetadataPrefix()); + $this->oaiConfig = $oaiConfig; } /** - * Gets all options for the oai server + * Creates an oai server model by metaDataPrefix * - * @param string $metadataPrefix - * @return array + * @param string $metaDataPrefix + * @return Oai_Model_DefaultServer */ - public function getFormatOptions($metadataPrefix = '') + public function create($metaDataPrefix = '') { - $metadataPrefix = strtolower($metadataPrefix); - - $config = $this->getConfig(); + $options = $this->getOaiConfig()->getFormatOptions($metaDataPrefix); - $generalOptions = $this->getGeneralOaiOptions(); + $serverClass = $options['class'] ?? Oai_Model_DefaultServer::class; - $defaultOptions = []; - if (isset($config->oai->format->default)) { - $defaultOptions = $config->oai->format->default->toArray(); + if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { + $server = new Oai_Model_DefaultServer($this->getConfig()); + } else { + $server = new $serverClass($this->getConfig()); } - $formatOptions = []; - if (isset($config->oai->format->$metadataPrefix)) { - $formatOptions = $config->oai->format->$metadataPrefix->toArray(); - } + if ($options) { + if (isset($options['viewHelper'])) { + $previousViewHelper = $server->getViewHelper() ?: []; + $viewHelper = $options['viewHelper']; - $options = array_merge($generalOptions, $defaultOptions, $formatOptions); + if (is_string($viewHelper)) { + $viewHelper = array_map('trim', explode(',', $viewHelper)); + } - if (isset($options['viewHelper'])) { - $options['viewHelper'] = $this->mergeMultiValueOption( - 'viewHelper', - $defaultOptions, - $formatOptions - ); - } - - if (isset($options['documentTypeRestriction'])) { - $options['documentTypeRestriction'] = $this->mergeMultiValueOption( - 'documentTypeRestriction', - $defaultOptions, - $formatOptions - ); - } + $options['viewHelper'] = array_unique(array_merge($previousViewHelper, $viewHelper)); + } - if (isset($options['prefixLabel'])) { - $options['prefixLabel'] = $this->mergeMultiValueOption( - 'prefixLabel', - $defaultOptions, - $formatOptions - ); + $server->setOptions($options); } - return $options; + return $server; } /** - * Gets the general oai options from the configuration + * Creates an oai server model by resumption token * - * @return array + * @param string $resumptionToken + * @return Oai_Model_DefaultServer */ - public function getGeneralOaiOptions() + public function createByResumptionToken($resumptionToken) { $config = $this->getConfig(); - if (! isset($config->oai)) { - throw new Exception('No configuration for module oai.'); - } - - $options = []; - - if (isset($config->oai->repository->name)) { - $options['repositoryName'] = $config->oai->repository->name; - } - if (isset($config->oai->repository->identifier)) { - $options['repositoryIdentifier'] = $config->oai->repository->identifier; - } - if (isset($config->oai->sample->identifier)) { - $options['sampleIdentifier'] = $config->oai->sample->identifier; - } - if (isset($config->oai->max->listidentifiers)) { - $options['maxListIdentifiers'] = (int) $config->oai->max->listidentifiers; - } - - if (isset($config->oai->max->listrecords)) { - $options['maxListRecords'] = (int) $config->oai->max->listrecords; - } - if (isset($config->oai->baseurl)) { - $options['oaiBaseUrl'] = $config->oai->baseurl; - } - if (isset($config->workspacePath)) { - $options['resumptionTokenPath'] = $config->workspacePath + $tempPath = $config->workspacePath . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . 'resumption'; } + $tokenWorker = new Oai_Model_Resumptiontokens(); + $tokenWorker->setResumptionPath($tempPath); + $token = $tokenWorker->getResumptionToken($resumptionToken); - if (isset($config->mail->opus->address)) { - $options['emailContact'] = $config->mail->opus->address; - } - - return $options; - } - - /** - * Merges the default and format specific configuration for an option - * containing multiple values as an array or comma separated list - * - * @param string $optionName - * @param array $defaultOptions - * @param array $formatOptions - * @return array - */ - protected function mergeMultiValueOption($optionName, $defaultOptions, $formatOptions) - { - $default = $defaultOptions[$optionName] ?? []; - $format = $formatOptions[$optionName] ?? []; - - if (is_string($default)) { - $default = array_map('trim', explode(',', $default)); - } - - if (is_string($format)) { - $format = array_map('trim', explode(',', $format)); + if ($token === null) { + throw new Oai_Model_Exception("file could not be read.", Oai_Model_Error::BADRESUMPTIONTOKEN); } - return array_unique(array_merge($default, $format)); + return $this->create($token->getMetadataPrefix()); } /** @@ -226,15 +146,12 @@ public function getFormats() $prefixes = []; if (isset($config->oai->format)) { - $formats = $config->oai->format->toArray(); - - foreach ($formats as $formatIdentifier => $format) { - $prefixes[] = $formatIdentifier; - } + $formats = $config->oai->format->toArray(); + $prefixes = array_keys($formats); + $prefixes = array_map('strtolower', $prefixes); + $prefixes = array_values(array_diff($prefixes, ['default'])); } - $prefixes = array_diff($prefixes, ['default']); - return $prefixes; } } diff --git a/modules/oai/models/prefix/epicur/EpicurServer.php b/modules/oai/models/prefix/epicur/EpicurServer.php index 060410af5..774ef91c4 100644 --- a/modules/oai/models/prefix/epicur/EpicurServer.php +++ b/modules/oai/models/prefix/epicur/EpicurServer.php @@ -28,9 +28,9 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class Oai_Model_Prefix_Epicur_EpicurServer extends Oai_Model_BaseServer +class Oai_Model_Prefix_Epicur_EpicurServer extends Oai_Model_DefaultServer { - public function __construct() + protected function initDefaults() { $this->setIdentifierExists('urn'); $this->setXsltFile('epicur.xslt'); diff --git a/modules/oai/models/prefix/marcxml/MarcXmlServer.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php index fcb322249..534970b92 100644 --- a/modules/oai/models/prefix/marcxml/MarcXmlServer.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -28,9 +28,9 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_BaseServer +class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_DefaultServer { - public function __construct() + protected function initDefaults() { $this->setXsltFile('marc21.xslt'); $this->setPrefixLabel('MARC21'); diff --git a/modules/oai/models/prefix/oai/OaiDcServer.php b/modules/oai/models/prefix/oai/OaiDcServer.php index 92eb759a2..286db89f0 100644 --- a/modules/oai/models/prefix/oai/OaiDcServer.php +++ b/modules/oai/models/prefix/oai/OaiDcServer.php @@ -28,9 +28,9 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class Oai_Model_Prefix_Oai_OaiDcServer extends Oai_Model_BaseServer +class Oai_Model_Prefix_Oai_OaiDcServer extends Oai_Model_DefaultServer { - public function __construct() + protected function initDefaults() { $this->setXsltFile('oai_dc.xslt'); $this->setSchemaUrl('http://www.openarchives.org/OAI/2.0/oai_dc.xsd'); diff --git a/modules/oai/models/prefix/oai/OaiPpServer.php b/modules/oai/models/prefix/oai/OaiPpServer.php index 10b0c674a..db8919ebb 100644 --- a/modules/oai/models/prefix/oai/OaiPpServer.php +++ b/modules/oai/models/prefix/oai/OaiPpServer.php @@ -28,9 +28,9 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class Oai_Model_Prefix_Oai_OaiPpServer extends Oai_Model_BaseServer +class Oai_Model_Prefix_Oai_OaiPpServer extends Oai_Model_DefaultServer { - public function __construct() + protected function initDefaults() { $this->setXsltFile('oai_pp.xslt'); $this->setVisible(false); diff --git a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php index 0ae789b37..ccc22c7e3 100644 --- a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php +++ b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php @@ -28,15 +28,15 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer extends Oai_Model_BaseServer +class Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer extends Oai_Model_DefaultServer { - public function __construct() + protected function initDefaults() { $this->setHasFilesVisibleInOai(true); $this->setCheckEmbargo(true); $this->setNotEmbargoedOn(true); $this->setXsltFile('XMetaDissPlus.xslt'); - $this->setPrefixLabel(['xMetaDissPlus']); + $this->setPrefixLabel('xMetaDissPlus'); $this->setSchemaUrl('http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd'); $this->setMetadataNamespaceUrl('http://www.d-nb.de/standards/xmetadissplus/'); } diff --git a/modules/oai/views/helpers/ListMetaDataFormats.php b/modules/oai/views/helpers/ListMetaDataFormats.php index 8aaccc287..8c9c25837 100644 --- a/modules/oai/views/helpers/ListMetaDataFormats.php +++ b/modules/oai/views/helpers/ListMetaDataFormats.php @@ -53,13 +53,12 @@ public function listMetadataFormats() if ($formats) { foreach ($formats as $formatPrefix) { $server = $serverFactory->create($formatPrefix); - $prefixLabel = $server->getPrefixLabel(); - $prefixes = $prefixLabel ?? [$formatPrefix]; + $prefix = $server->getPrefixLabel() ?: $formatPrefix; $schemaUrl = $server->getSchemaUrl(); $metadataNamespaceUrl = $server->getMetadataNamespaceUrl(); if ($server->isVisible() && (! $server->isAdminOnly() || Realm::getInstance()->checkModule('admin'))) { - foreach ($prefixes as $prefix) { + if ($prefix) { $output .= '' . "$prefix" . "$schemaUrl" diff --git a/tests/modules/oai/controllers/IndexControllerTest.php b/tests/modules/oai/controllers/IndexControllerTest.php index f6c0a41b3..e38458973 100644 --- a/tests/modules/oai/controllers/IndexControllerTest.php +++ b/tests/modules/oai/controllers/IndexControllerTest.php @@ -281,9 +281,13 @@ public function testListMetadataFormats() $response = $this->getResponse(); - $this->assertEquals('', $response->getBody()); - $this->checkForBadStringsInHtml($response->getBody()); + + $this->assertNotContains( + 'copy_xml', + $response->getBody(), + "Response must not contain format 'copy_xml'" + ); } /** diff --git a/tests/modules/oai/models/BaseServerTest.php b/tests/modules/oai/models/BaseServerTest.php index 4570a5303..daa959a0e 100644 --- a/tests/modules/oai/models/BaseServerTest.php +++ b/tests/modules/oai/models/BaseServerTest.php @@ -29,11 +29,11 @@ * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class Oai_Model_BaseServerTest extends ControllerTestCase +class Oai_Model_DefaultServerTest extends ControllerTestCase { public function testGetOaiBaseUrl() { - $baseServer = new Oai_Model_BaseServer(); + $baseServer = new Oai_Model_DefaultServer(); $baseServer->setBaseUrl('http://baseUrlPath'); $baseServer->setOaiBaseUrl('http://oaiBaseUrlPath'); @@ -43,7 +43,7 @@ public function testGetOaiBaseUrl() public function testGetOaiBaseUrlNotSet() { - $baseServer = new Oai_Model_BaseServer(); + $baseServer = new Oai_Model_DefaultServer(); $baseServer->setBaseUrl('http://baseUrlPath'); @@ -52,7 +52,7 @@ public function testGetOaiBaseUrlNotSet() public function testGetOptionNotInOptionsArray() { - $baseServer = new Oai_Model_BaseServer(); + $baseServer = new Oai_Model_DefaultServer(); $baseServer->setOptions([]); $this->assertEquals('', $baseServer->getRepositoryIdentifier()); diff --git a/tests/modules/oai/models/OAIConfigTest.php b/tests/modules/oai/models/OAIConfigTest.php new file mode 100644 index 000000000..fb7dee99e --- /dev/null +++ b/tests/modules/oai/models/OAIConfigTest.php @@ -0,0 +1,195 @@ + '/vagrant/tests/workspace', + 'mail' => [ + 'opus' => [ + 'address' => 'opus4ci@example.org', + ], + ], + 'oai' => [ + 'max' => [ + 'listrecords' => 10, + 'listidentifiers' => 10, + ], + 'format' => [ + 'default' => [ + 'class' => DefaultOaiServer::class, + 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + 'xsltFile' => 'oaiFile.xslt', + ], + 'copy_xml' => [ + 'xsltFile' => 'copy_xml.xslt', + 'adminOnly' => 1, + 'visible' => 0, + ], + 'xmetadissplus' => [ + 'class' => Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer::class, + 'xsltFile' => 'XMetaDissPlus.xslt', + 'prefixLabel' => 'xMetaDissPlus', + 'hasFilesVisibleInOai' => 1, + 'checkEmbargo' => 1, + 'notEmbargoedOn' => 1, + 'schemaUrl' => 'http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd', + 'setMetadataNamespaceUrl' => 'http://www.d-nb.de/standards/xmetadissplus/', + ], + 'oai_pp' => null, + ], + ], + ]; + } + + public function testGetDefaults() + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + $oaiConfig->setConfig(new Zend_Config($this->getConfigurationArray())); + + $defaults = $oaiConfig->getDefaults(); + + $expectedDefaults = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => 'oaiFile.xslt', + 'class' => DefaultOaiServer::class, + 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + ]; + + $this->assertEquals($expectedDefaults, $defaults); + } + + public function testGetDefaultsNoFormatDefaults() + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + + $configArray = $this->getConfigurationArray(); + + unset($configArray['oai']['format']['default']); + + $oaiConfig->setConfig(new Zend_Config($configArray)); + + $expectedDefaults = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + ]; + + $defaults = $oaiConfig->getDefaults(); + $this->assertEquals($expectedDefaults, $defaults); + } + + public function testGetDefaultsNoOaiConfiguration() + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + + $configArray = $this->getConfigurationArray(); + + unset($configArray['oai']); + + $oaiConfig->setConfig(new Zend_Config($configArray)); + + $this->expectException(Exception::class); + $this->expectExceptionMessage('No configuration for module oai.'); + + $oaiConfig->getDefaults(); + } + + public function testGetFormatOptions() + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + $oaiConfig->setConfig(new Zend_Config($this->getConfigurationArray())); + + $expectedFormatOptions = [ + 'class' => Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer::class, + 'xsltFile' => 'XMetaDissPlus.xslt', + 'prefixLabel' => 'xMetaDissPlus', + 'hasFilesVisibleInOai' => 1, + 'checkEmbargo' => 1, + 'notEmbargoedOn' => 1, + 'schemaUrl' => 'http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd', + 'setMetadataNamespaceUrl' => 'http://www.d-nb.de/standards/xmetadissplus/', + ]; + + $formatOptions = $oaiConfig->getFormatOptions('xMetaDissPlus'); + $this->assertEquals($expectedFormatOptions, $formatOptions); + } + + public function testGetFormatOptionsUnknownPrefix() + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + $oaiConfig->setConfig(new Zend_Config($this->getConfigurationArray())); + + $formatOptions = $oaiConfig->getFormatOptions('unknownPrefix'); + $this->assertEquals([], $formatOptions); + } + + public function testGetFormatOptionsNoFormatConfiguration() + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + + $configArray = $this->getConfigurationArray(); + unset($configArray['oai']['format']); + + $oaiConfig->setConfig(new Zend_Config($configArray)); + + $formatOptions = $oaiConfig->getFormatOptions('xMetaDissPlus'); + $this->assertEquals([], $formatOptions); + } + + public function testGetFormatOptionsWithWrongPrefixCaseInConfiguration() + { + $oaiConfig = Oai_Model_OAIConfig::getInstance(); + + $configArray = $this->getConfigurationArray(); + $configArray['oai']['format']['EPICUR'] = [ + 'class' => Oai_Model_Prefix_Epicur_EpicurServer::class, + ]; + + $oaiConfig->setConfig(new Zend_Config($configArray)); + + $expectedFormatOptions = [ + 'class' => Oai_Model_Prefix_Epicur_EpicurServer::class, + ]; + + $formatOptions = $oaiConfig->getFormatOptions('epicur'); + $this->assertEquals($expectedFormatOptions, $formatOptions); + } +} diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index f4c08b523..fcf7d7344 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -34,6 +34,11 @@ class Oai_Model_ServerFactoryTest extends ControllerTestCase /** @var string[] */ protected $additionalResources = ['database', 'view']; + public function setUp(): void + { + parent::setUp(); + } + /** * @return array */ @@ -52,14 +57,28 @@ protected function getConfigurationArray() 'listidentifiers' => 10, ], 'format' => [ - 'default' => [ - 'class' => DefaultOaiServer::class, + 'default' => [ + 'class' => DefaultOaiServer::class, + 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + 'xsltFile' => 'oaiFile.xslt', + 'hasFilesVisibleInOai' => 1, + ], + 'copy_xml' => [ + 'xsltFile' => 'copy_xml.xslt', + 'adminOnly' => 1, + 'visible' => 0, ], - 'oai_dc' => [ - 'class' => OaiDcServer::class, - 'xsltFile' => 'oaiFile.xslt', + 'xmetadissplus' => [ + 'class' => Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer::class, + 'xsltFile' => 'XMetaDissPlus.xslt', + 'prefixLabel' => 'xMetaDissPlus', + 'hasFilesVisibleInOai' => 1, + 'checkEmbargo' => 1, + 'notEmbargoedOn' => 1, + 'schemaUrl' => 'http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd', + 'setMetadataNamespaceUrl' => 'http://www.d-nb.de/standards/xmetadissplus/', ], - 'oai_pp' => null, + 'oai_pp' => null, ], ], ]; @@ -71,320 +90,210 @@ protected function getConfigurationArray() */ protected function createServerFactory($configurationArray = null) { + $serverFactory = new Oai_Model_ServerFactory(); + if ($configurationArray === null) { $config = new Zend_Config($this->getConfigurationArray()); } else { $config = new Zend_Config($configurationArray); } - $serverFactory = new Oai_Model_ServerFactory(); $serverFactory->setConfig($config); - return $serverFactory; - } - - public function testCreateWithValidMetadataPrefix() - { - $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create('oai_dc'); - - $expectedOptions = [ - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - 'xsltFile' => 'oaiFile.xslt', - ]; - $this->assertEquals(OaiDcServer::class, get_class($server)); - $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); + return $serverFactory; } - public function testCreateWithNoneExistingFormatServerClass() + public function testGetFormats() { - $configArray = $this->getConfigurationArray(); - - $configArray['oai']['format']['oai_dc']['class'] = 'UnknownOaiDcServerClass'; - - $serverFactory = $this->createServerFactory($configArray); - - $server = $serverFactory->create('oai_dc'); - - $expectedOptions = [ - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - 'xsltFile' => 'oaiFile.xslt', + $configArray = [ + 'oai' => [ + 'format' => [ + 'Default' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + 'copy_xml' => [ + 'xsltFile' => 'copy_xml.xslt', + ], + 'oai_dc' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + 'epicur' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + 'xMetaDissPluss' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + ], + ], ]; - $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); - $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); - } - - public function testCreateWithNoneExistingDefaultServerClass() - { - $configArray = $this->getConfigurationArray(); - unset($configArray['oai']['format']['oai_dc']['class']); - $configArray['oai']['format']['default']['class'] = 'UnknownDefaultServerClass'; - $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create('oai_dc'); + $formats = $serverFactory->getFormats(); - $expectedOptions = [ - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - 'xsltFile' => 'oaiFile.xslt', - ]; - - $this->assertEquals(Oai_Model_BaseServer::class, get_class($server)); - $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); + $expectedFormats = ['copy_xml', 'oai_dc', 'epicur', 'xmetadisspluss']; + $this->assertEquals($expectedFormats, $formats); } - public function testCreateWithNoPrefix() + public function testCreate() { $serverFactory = $this->createServerFactory(); $server = $serverFactory->create(); - - $expectedOptions = [ - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - ]; - - $this->assertEquals(DefaultOaiServer::class, get_class($server)); - $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); + $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); } - public function testCreateWithPrefixNotConfigured() + public function testCreateWithMetadataPrefix() { $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create('oai_pp'); - - $expectedOptions = [ - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - ]; - - $this->assertEquals(DefaultOaiServer::class, get_class($server)); - $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); + $server = $serverFactory->create('xMetaDissPlus'); + $this->assertEquals(Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer::class, get_class($server)); } - public function testGetFormatClassName() + public function testCreateWithUnknownMetadataPrefix() { $serverFactory = $this->createServerFactory(); - - $metaDataPrefix = 'oai_dc'; - $options = $serverFactory->getFormatOptions($metaDataPrefix); - $this->assertEquals(OaiDcServer::class, $options['class']); - - $metaDataPrefix = 'oai_Dc'; - $options = $serverFactory->getFormatOptions($metaDataPrefix); - $this->assertEquals(OaiDcServer::class, $options['class']); + $server = $serverFactory->create('unknownPrefix'); + $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); } - public function testGetFormatClassNameWithUnknownPrefix() + public function testCreateWithNoFormatConfiguration() { $configArray = $this->getConfigurationArray(); - + unset($configArray['oai']['format']); $serverFactory = $this->createServerFactory($configArray); - - $metaDataPrefix = 'unknown'; - $options = $serverFactory->getFormatOptions($metaDataPrefix); - $this->assertEquals(DefaultOaiServer::class, $options['class']); + $server = $serverFactory->create('xMetaDissPlus'); + $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); } - public function testGetFormatOptions() + public function testCreateWithUnkownFormatClass() { - $serverFactory = $this->createServerFactory(); - - $expectedOptions = [ - 'xsltFile' => 'oaiFile.xslt', - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - 'class' => OaiDcServer::class, - ]; - - $metaDataPrefix = 'oai_dc'; - $options = $serverFactory->getFormatOptions($metaDataPrefix); - $this->assertEquals($expectedOptions, $options); - - $metaDataPrefix = 'oai_Dc'; - $options = $serverFactory->getFormatOptions($metaDataPrefix); - $this->assertEquals($expectedOptions, $options); - } - - public function testGetFormatOptionsWithUnknownMetadataPrefix() - { - $serverFactory = $this->createServerFactory(); - - $expectedOptions = [ - 'maxListIdentifiers' => 10, - 'maxListRecords' => 10, - 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', - 'emailContact' => 'opus4ci@example.org', - 'class' => DefaultOaiServer::class, - ]; - - $metaDataPrefix = 'unknown'; - $options = $serverFactory->getFormatOptions($metaDataPrefix); - - $this->assertEquals($expectedOptions, $options); - } - - public function testConfigurationOverwritesXsltFileValueFromPrefixClass() - { - $configArray = $this->getConfigurationArray(); - - $configArray['oai']['format']['xmetadissplus'] = [ - 'class' => Oai_Model_Prefix_MarcXml_MarcXmlServer::class, - 'xsltFile' => 'XMetaDissPlus.xslt', - ]; - - $serverFactory = $this->createServerFactory($configArray); - - $server = $serverFactory->create('xmetadissplus'); - - $this->assertEquals('XMetaDissPlus.xslt', $server->getXsltFile()); - } - - public function testXsltFileNotConfiguredButSetInPrefixClass() - { - $configArray = $this->getConfigurationArray(); - - $configArray['oai']['format']['xmetadissplus'] = [ - 'class' => Oai_Model_Prefix_MarcXml_MarcXmlServer::class, - ]; - - $serverFactory = $this->createServerFactory($configArray); - - $server = $serverFactory->create('xmetadissplus'); - - $this->assertEquals('marc21.xslt', $server->getXsltFile()); + $configArray = $this->getConfigurationArray(); + $configArray['oai']['format']['xmetadissplus']['class'] = 'UnknownClass'; + $serverFactory = $this->createServerFactory($configArray); + $server = $serverFactory->create('xMetaDissPlus'); + $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); } - public function testViewHelpersConfiguredAsArray() + public function testDefaultServerOptionsNoDefaultConfiguration() { $configArray = $this->getConfigurationArray(); - $configArray['oai']['format']['default'] = [ - 'viewHelper' => [ - 'optionValue', - 'fileUrl', - 'frontdoorUrl', - 'transferUrl', - 'dcmiType', - 'dcType', - 'openAireType', - ], - ]; + unset($configArray['oai']['format']['default']); $serverFactory = $this->createServerFactory($configArray); + $server = $serverFactory->create(); - $expectedViewHelpers = [ - 'optionValue', - 'fileUrl', - 'frontdoorUrl', - 'transferUrl', - 'dcmiType', - 'dcType', - 'openAireType', + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => '', + 'documentStatesAllowed' => ['published', 'deleted'], + 'viewHelper' => ['listMetadataFormats'], + 'notEmbargoedOn' => false, + 'hasFilesVisibleInOai' => false, + 'adminOnly' => false, + 'visible' => true, + 'checkEmbargo' => false, ]; - $options = $serverFactory->getFormatOptions(); - - $this->assertEquals($expectedViewHelpers, $options['viewHelper']); + $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } - public function testViewHelpersDefaultConfiguredAsArrayAndFormatAsString() + public function testDefaultServerOptionsWithDefaultConfiguration() { - $configArray = $this->getConfigurationArray(); - - $configArray['oai']['format']['default'] = [ - 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType'], - ]; - - $configArray['oai']['format']['oai_dc'] = [ - 'viewHelper' => 'firstNewViewHelper, secondNewViewHelper', - ]; - + $configArray = $this->getConfigurationArray(); $serverFactory = $this->createServerFactory($configArray); + $server = $serverFactory->create(); - $expectedProcessors = [ - 'optionValue', - 'fileUrl', - 'frontdoorUrl', - 'transferUrl', - 'dcmiType', - 'dcType', - 'openAireType', - 'firstNewViewHelper', - 'secondNewViewHelper', + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => 'oaiFile.xslt', + 'documentStatesAllowed' => ['published', 'deleted'], + 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType', 'listMetadataFormats'], + 'notEmbargoedOn' => false, + 'hasFilesVisibleInOai' => true, + 'adminOnly' => false, + 'visible' => true, + 'checkEmbargo' => false, ]; - $options = $serverFactory->getFormatOptions('oai_dc'); - $this->assertEquals($expectedProcessors, $options['viewHelper']); + $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } - public function testViewHelpersConfiguredAsCommaSeparatedList() + public function testFormatServerClassOverwritesDefaults() { $configArray = $this->getConfigurationArray(); - $configArray['oai']['format']['default'] = [ - 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + $configArray['oai']['format']['xmetadissplus'] = [ + 'class' => XMetaDissPlusServer::class, ]; $serverFactory = $this->createServerFactory($configArray); + $server = $serverFactory->create('xmetadissplus'); - $expectedViewHelpers = [ - 'optionValue', - 'fileUrl', - 'frontdoorUrl', - 'transferUrl', - 'dcmiType', - 'dcType', - 'openAireType', + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => 'XMetaDissPlus.xslt', + 'documentStatesAllowed' => ['published', 'deleted'], + 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'listMetadataFormats'], + 'notEmbargoedOn' => true, + 'hasFilesVisibleInOai' => true, + 'adminOnly' => false, + 'visible' => true, + 'checkEmbargo' => true, + 'prefixLabel' => 'xMetaDissPlus', + 'schemaUrl' => 'http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd', + 'metadataNamespaceUrl' => 'http://www.d-nb.de/standards/xmetadissplus/', ]; - $options = $serverFactory->getFormatOptions(); - - $this->assertEquals($expectedViewHelpers, $options['viewHelper']); + $this->assertEquals(XMetaDissPlusServer::class, get_class($server)); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } - public function testViewHelpersConfiguredForMetadaPrefix() + public function testFormatServerClassOptionsOverwrittenByFormatConfiguration() { $configArray = $this->getConfigurationArray(); - $configArray['oai']['format']['default'] = [ - 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', - ]; - - $configArray['oai']['format']['oai_dc'] = [ - 'viewHelper' => 'newViewHelper', + $configArray['oai']['format']['xmetadissplus'] = [ + 'class' => XMetaDissPlusServer::class, + 'viewHelper' => 'additionalViewHelper1, additionalViewHelper2', + 'xsltFile' => 'configuredXMetaDissPlus.xslt', + 'checkEmbargo' => 0, ]; $serverFactory = $this->createServerFactory($configArray); + $server = $serverFactory->create('xmetadissplus'); - $expectedProcessors = [ - 'optionValue', - 'fileUrl', - 'frontdoorUrl', - 'transferUrl', - 'dcmiType', - 'dcType', - 'openAireType', - 'newViewHelper', + $expectedOptions = [ + 'maxListIdentifiers' => 10, + 'maxListRecords' => 10, + 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', + 'emailContact' => 'opus4ci@example.org', + 'xsltFile' => 'configuredXMetaDissPlus.xslt', + 'documentStatesAllowed' => ['published', 'deleted'], + 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'additionalViewHelper1', 'additionalViewHelper2', 'listMetadataFormats'], + 'notEmbargoedOn' => true, + 'hasFilesVisibleInOai' => true, + 'adminOnly' => false, + 'visible' => true, + 'checkEmbargo' => false, + 'prefixLabel' => 'xMetaDissPlus', + 'schemaUrl' => 'http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd', + 'metadataNamespaceUrl' => 'http://www.d-nb.de/standards/xmetadissplus/', ]; - $options = $serverFactory->getFormatOptions('oai_dc'); - $this->assertEquals($expectedProcessors, $options['viewHelper']); + $this->assertEquals(XMetaDissPlusServer::class, get_class($server)); + $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } public function testDoViewHelpersExist() diff --git a/tests/support/DefaultOaiServer.php b/tests/support/DefaultOaiServer.php index ed842a336..e399c0e35 100644 --- a/tests/support/DefaultOaiServer.php +++ b/tests/support/DefaultOaiServer.php @@ -28,6 +28,6 @@ * @copyright Copyright (c) 2008, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class DefaultOaiServer extends Oai_Model_BaseServer +class DefaultOaiServer extends Oai_Model_DefaultServer { } diff --git a/tests/support/OaiDcServer.php b/tests/support/OaiDcServer.php index 723cbfe72..1ded6e1c6 100644 --- a/tests/support/OaiDcServer.php +++ b/tests/support/OaiDcServer.php @@ -28,6 +28,6 @@ * @copyright Copyright (c) 2008, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class OaiDcServer extends Oai_Model_BaseServer +class OaiDcServer extends Oai_Model_DefaultServer { } diff --git a/tests/support/XMetaDissPlusServer.php b/tests/support/XMetaDissPlusServer.php new file mode 100644 index 000000000..d5d7ebafc --- /dev/null +++ b/tests/support/XMetaDissPlusServer.php @@ -0,0 +1,44 @@ +setHasFilesVisibleInOai(true); + $this->setCheckEmbargo(true); + $this->setNotEmbargoedOn(true); + $this->setXsltFile('XMetaDissPlus.xslt'); + $this->setPrefixLabel('xMetaDissPlus'); + $this->setSchemaUrl('http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd'); + $this->setMetadataNamespaceUrl('http://www.d-nb.de/standards/xmetadissplus/'); + $this->setViewHelper('optionValue, fileUrl, frontdoorUrl'); + } +} From f551e1368c08c797331df4fef56b6a4ccefd0425 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 7 Sep 2023 12:06:21 +0200 Subject: [PATCH 29/59] #1101 Fix option merging. --- modules/oai/models/DefaultServer.php | 52 ++++--- modules/oai/models/OAIConfig.php | 53 ++++++- modules/oai/models/Request.php | 8 +- modules/oai/models/ServerFactory.php | 65 ++++---- .../oai/models/prefix/epicur/EpicurServer.php | 2 +- .../models/prefix/marcxml/MarcXmlServer.php | 2 +- modules/oai/models/prefix/oai/OaiDcServer.php | 2 +- modules/oai/models/prefix/oai/OaiPpServer.php | 2 +- .../xmetadissplus/XMetaDissPlusServer.php | 2 +- .../oai/views/helpers/ListMetaDataFormats.php | 3 +- .../{OAIConfigTest.php => OaiConfigTest.php} | 91 ++++++++--- .../modules/oai/models/ServerFactoryTest.php | 145 +++++++----------- tests/support/XMetaDissPlusServer.php | 12 +- 13 files changed, 247 insertions(+), 192 deletions(-) rename tests/modules/oai/models/{OAIConfigTest.php => OaiConfigTest.php} (68%) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index ac83f16bb..0bc845189 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -79,6 +79,9 @@ class Oai_Model_DefaultServer extends Application_Model_Abstract /** @var Zend_Controller_Response_Http */ private $response; // TODO temporary hack + /** @var Oai_Model_OaiConfig */ + private $oaiConfig; + /** @var string */ private $oaiBaseUrl; @@ -143,32 +146,22 @@ class Oai_Model_DefaultServer extends Application_Model_Abstract private $visible = true; /** - * @param Zend_Config $config + * Initializes the server options with default values. */ - public function __construct($config = null) + public function initDefaults() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); - - if ($config) { - $oaiConfig->setConfig($config); - } - - $defaults = $oaiConfig->getDefaults(); - + $defaults = $this->getOaiConfig()->getDefaults(); $this->setOptions($defaults); - - $this->initDefaults(); + $this->initFormatDefaults(); } /** * Initializes server options with default values + * This function is used by derived classes to set their own values for options by overwriting the method + * and using setters, in order to overwrite default values. */ - protected function initDefaults() + protected function initFormatDefaults() { - /* - This function is used by derived classes to set their own values for options by overwriting the method - and using setters, in order to overwrite default values. - */ } /** @@ -851,6 +844,30 @@ protected function loadStyleSheet() $this->proc->setParameter('', 'server', $this->getBaseUri()); } + /** + * Gets the oai configuration + * + * @return Oai_Model_OaiConfig + */ + public function getOaiConfig() + { + if ($this->oaiConfig === null) { + $this->oaiConfig = Oai_Model_OaiConfig::getInstance(); + } + + return $this->oaiConfig; + } + + /** + * Sets the oai configuration + * + * @param Oai_Model_OaiConfig $oaiConfig + */ + public function setOaiConfig($oaiConfig) + { + $this->oaiConfig = $oaiConfig; + } + /** * @return string */ @@ -1335,7 +1352,6 @@ public function setVisible($visible) * * @param Document $document * @param string $metadataPrefix - * @return void * @throws Oai_Model_Exception */ protected function checkExportAllowed($document, $metadataPrefix) diff --git a/modules/oai/models/OAIConfig.php b/modules/oai/models/OAIConfig.php index f82dadd59..e76d77289 100644 --- a/modules/oai/models/OAIConfig.php +++ b/modules/oai/models/OAIConfig.php @@ -34,18 +34,18 @@ /** * Class to read configuration options for the oai server. */ -class Oai_Model_OAIConfig +class Oai_Model_OaiConfig { use ConfigTrait; /** * Factory method * - * @return Oai_Model_OAIConfig + * @return self */ public static function getInstance() { - return new Oai_Model_OAIConfig(); + return new self(); } /** @@ -128,11 +128,7 @@ protected function getGeneralOaiOptions() $options['oaiBaseUrl'] = $config->oai->baseurl; } - if (isset($config->workspacePath)) { - $options['resumptionTokenPath'] = $config->workspacePath - . DIRECTORY_SEPARATOR . 'tmp' - . DIRECTORY_SEPARATOR . 'resumption'; - } + $options['resumptionTokenPath'] = $this->getResumptionTokenPath(); if (isset($config->mail->opus->address)) { $options['emailContact'] = $config->mail->opus->address; @@ -140,4 +136,45 @@ protected function getGeneralOaiOptions() return $options; } + + /** + * Gets all configured format prefixes + * + * @return string[] + */ + public function getFormats() + { + $config = $this->getConfig(); + + $prefixes = []; + + if (isset($config->oai->format)) { + $formats = $config->oai->format->toArray(); + $prefixes = array_keys($formats); + $prefixes = array_map('strtolower', $prefixes); + $prefixes = array_values(array_diff($prefixes, ['default'])); + } + + return $prefixes; + } + + /** + * Gets the path for resumption tokens + * + * @return string + */ + public function getResumptionTokenPath() + { + $config = $this->getConfig(); + + $resumptionTokenPath = ''; + + if (isset($config->workspacePath)) { + $resumptionTokenPath = $config->workspacePath + . DIRECTORY_SEPARATOR . 'tmp' + . DIRECTORY_SEPARATOR . 'resumption'; + } + + return $resumptionTokenPath; + } } diff --git a/modules/oai/models/Request.php b/modules/oai/models/Request.php index a854d45f9..08a3b7356 100644 --- a/modules/oai/models/Request.php +++ b/modules/oai/models/Request.php @@ -139,9 +139,11 @@ public function checkDate($datestr) */ public function validateMetadataPrefix($oaiMetadataPrefix) { - $serverFactory = new Oai_Model_ServerFactory(); - $server = $serverFactory->create(strtolower($oaiMetadataPrefix)); - $availableMetadataPrefixes = array_map('strtolower', $serverFactory->getFormats()); + $serverFactory = new Oai_Model_ServerFactory(); + $server = $serverFactory->create(strtolower($oaiMetadataPrefix)); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); + + $availableMetadataPrefixes = array_map('strtolower', $oaiConfig->getFormats()); $result = in_array(strtolower($oaiMetadataPrefix), $availableMetadataPrefixes) && (! $server->isAdminOnly() || Realm::getInstance()->checkModule('admin')); diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index a99b3998c..8e475d8ca 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -39,24 +39,18 @@ class Oai_Model_ServerFactory { use ConfigTrait; - /** @var Oai_Model_OAIConfig */ + /** @var Oai_Model_OaiConfig */ private $oaiConfig; - public function __constructor() - { - $this->oaiConfig = Oai_Model_OAIConfig::getInstance(); - } - /** * Gets the oai configuration * - * @return Oai_Model_OAIConfig + * @return Oai_Model_OaiConfig */ public function getOaiConfig() { if ($this->oaiConfig === null) { - $this->oaiConfig = Oai_Model_OAIConfig::getInstance(); - $this->oaiConfig->setConfig($this->getConfig()); + $this->oaiConfig = Oai_Model_OaiConfig::getInstance(); } return $this->oaiConfig; @@ -65,7 +59,7 @@ public function getOaiConfig() /** * Sets the oai configuration * - * @param Oai_Model_OAIConfig $oaiConfig + * @param Oai_Model_OaiConfig $oaiConfig */ public function setOaiConfig($oaiConfig) { @@ -75,21 +69,33 @@ public function setOaiConfig($oaiConfig) /** * Creates an oai server model by metaDataPrefix * - * @param string $metaDataPrefix + * @param string|null $metaDataPrefix * @return Oai_Model_DefaultServer */ - public function create($metaDataPrefix = '') + public function create($metaDataPrefix = null) { - $options = $this->getOaiConfig()->getFormatOptions($metaDataPrefix); + $oaiConfig = $this->getOaiConfig(); - $serverClass = $options['class'] ?? Oai_Model_DefaultServer::class; + $defaults = $oaiConfig->getDefaults(); + $options = $metaDataPrefix ? $oaiConfig->getFormatOptions($metaDataPrefix) : null; + + if (isset($options['class'])) { + $serverClass = $options['class']; + } elseif (isset($defaults['class'])) { + $serverClass = $defaults['class']; + } else { + $serverClass = Oai_Model_DefaultServer::class; + } if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { - $server = new Oai_Model_DefaultServer($this->getConfig()); + $server = new Oai_Model_DefaultServer(); } else { - $server = new $serverClass($this->getConfig()); + $server = new $serverClass(); } + $server->setOaiConfig($this->getOaiConfig()); + $server->initDefaults(); + if ($options) { if (isset($options['viewHelper'])) { $previousViewHelper = $server->getViewHelper() ?: []; @@ -116,15 +122,9 @@ public function create($metaDataPrefix = '') */ public function createByResumptionToken($resumptionToken) { - $config = $this->getConfig(); - - if (isset($config->workspacePath)) { - $tempPath = $config->workspacePath - . DIRECTORY_SEPARATOR . 'tmp' - . DIRECTORY_SEPARATOR . 'resumption'; - } - $tokenWorker = new Oai_Model_Resumptiontokens(); - $tokenWorker->setResumptionPath($tempPath); + $resumptionTokenPath = $this->getOaiConfig()->getResumptionTokenPath(); + $tokenWorker = new Oai_Model_Resumptiontokens(); + $tokenWorker->setResumptionPath($resumptionTokenPath); $token = $tokenWorker->getResumptionToken($resumptionToken); if ($token === null) { @@ -137,21 +137,10 @@ public function createByResumptionToken($resumptionToken) /** * Gets all configured format prefixes * - * @return array + * @return string[] */ public function getFormats() { - $config = $this->getConfig(); - - $prefixes = []; - - if (isset($config->oai->format)) { - $formats = $config->oai->format->toArray(); - $prefixes = array_keys($formats); - $prefixes = array_map('strtolower', $prefixes); - $prefixes = array_values(array_diff($prefixes, ['default'])); - } - - return $prefixes; + return $this->getOaiConfig()->getFormats(); } } diff --git a/modules/oai/models/prefix/epicur/EpicurServer.php b/modules/oai/models/prefix/epicur/EpicurServer.php index 774ef91c4..1e78fd5c7 100644 --- a/modules/oai/models/prefix/epicur/EpicurServer.php +++ b/modules/oai/models/prefix/epicur/EpicurServer.php @@ -30,7 +30,7 @@ */ class Oai_Model_Prefix_Epicur_EpicurServer extends Oai_Model_DefaultServer { - protected function initDefaults() + protected function initFormatDefaults() { $this->setIdentifierExists('urn'); $this->setXsltFile('epicur.xslt'); diff --git a/modules/oai/models/prefix/marcxml/MarcXmlServer.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php index 534970b92..9ab7984a0 100644 --- a/modules/oai/models/prefix/marcxml/MarcXmlServer.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -30,7 +30,7 @@ */ class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_DefaultServer { - protected function initDefaults() + protected function initFormatDefaults() { $this->setXsltFile('marc21.xslt'); $this->setPrefixLabel('MARC21'); diff --git a/modules/oai/models/prefix/oai/OaiDcServer.php b/modules/oai/models/prefix/oai/OaiDcServer.php index 286db89f0..0afb61b26 100644 --- a/modules/oai/models/prefix/oai/OaiDcServer.php +++ b/modules/oai/models/prefix/oai/OaiDcServer.php @@ -30,7 +30,7 @@ */ class Oai_Model_Prefix_Oai_OaiDcServer extends Oai_Model_DefaultServer { - protected function initDefaults() + protected function initFormatDefaults() { $this->setXsltFile('oai_dc.xslt'); $this->setSchemaUrl('http://www.openarchives.org/OAI/2.0/oai_dc.xsd'); diff --git a/modules/oai/models/prefix/oai/OaiPpServer.php b/modules/oai/models/prefix/oai/OaiPpServer.php index db8919ebb..cad0f5063 100644 --- a/modules/oai/models/prefix/oai/OaiPpServer.php +++ b/modules/oai/models/prefix/oai/OaiPpServer.php @@ -30,7 +30,7 @@ */ class Oai_Model_Prefix_Oai_OaiPpServer extends Oai_Model_DefaultServer { - protected function initDefaults() + protected function initFormatDefaults() { $this->setXsltFile('oai_pp.xslt'); $this->setVisible(false); diff --git a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php index ccc22c7e3..15da0d3b6 100644 --- a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php +++ b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php @@ -30,7 +30,7 @@ */ class Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer extends Oai_Model_DefaultServer { - protected function initDefaults() + protected function initFormatDefaults() { $this->setHasFilesVisibleInOai(true); $this->setCheckEmbargo(true); diff --git a/modules/oai/views/helpers/ListMetaDataFormats.php b/modules/oai/views/helpers/ListMetaDataFormats.php index 8c9c25837..ab4d21726 100644 --- a/modules/oai/views/helpers/ListMetaDataFormats.php +++ b/modules/oai/views/helpers/ListMetaDataFormats.php @@ -48,7 +48,8 @@ public function listMetadataFormats() $output = ''; $serverFactory = new Oai_Model_ServerFactory(); - $formats = $serverFactory->getFormats(); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); + $formats = $oaiConfig->getFormats(); if ($formats) { foreach ($formats as $formatPrefix) { diff --git a/tests/modules/oai/models/OAIConfigTest.php b/tests/modules/oai/models/OaiConfigTest.php similarity index 68% rename from tests/modules/oai/models/OAIConfigTest.php rename to tests/modules/oai/models/OaiConfigTest.php index fb7dee99e..63cb5e8b7 100644 --- a/tests/modules/oai/models/OAIConfigTest.php +++ b/tests/modules/oai/models/OaiConfigTest.php @@ -34,7 +34,7 @@ class Oai_Model_OaiConfigTest extends ControllerTestCase /** * @return array */ - protected function getConfigurationArray() + protected function getTestConfiguration() { return [ 'workspacePath' => '/vagrant/tests/workspace', @@ -77,8 +77,8 @@ protected function getConfigurationArray() public function testGetDefaults() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); - $oaiConfig->setConfig(new Zend_Config($this->getConfigurationArray())); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); + $oaiConfig->setConfig(new Zend_Config($this->getTestConfiguration())); $defaults = $oaiConfig->getDefaults(); @@ -97,13 +97,13 @@ public function testGetDefaults() public function testGetDefaultsNoFormatDefaults() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); - $configArray = $this->getConfigurationArray(); + $testConfiguration = $this->getTestConfiguration(); - unset($configArray['oai']['format']['default']); + unset($testConfiguration['oai']['format']['default']); - $oaiConfig->setConfig(new Zend_Config($configArray)); + $oaiConfig->setConfig(new Zend_Config($testConfiguration)); $expectedDefaults = [ 'maxListIdentifiers' => 10, @@ -118,13 +118,13 @@ public function testGetDefaultsNoFormatDefaults() public function testGetDefaultsNoOaiConfiguration() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); - $configArray = $this->getConfigurationArray(); + $testConfiguration = $this->getTestConfiguration(); - unset($configArray['oai']); + unset($testConfiguration['oai']); - $oaiConfig->setConfig(new Zend_Config($configArray)); + $oaiConfig->setConfig(new Zend_Config($testConfiguration)); $this->expectException(Exception::class); $this->expectExceptionMessage('No configuration for module oai.'); @@ -134,8 +134,8 @@ public function testGetDefaultsNoOaiConfiguration() public function testGetFormatOptions() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); - $oaiConfig->setConfig(new Zend_Config($this->getConfigurationArray())); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); + $oaiConfig->setConfig(new Zend_Config($this->getTestConfiguration())); $expectedFormatOptions = [ 'class' => Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer::class, @@ -154,8 +154,8 @@ public function testGetFormatOptions() public function testGetFormatOptionsUnknownPrefix() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); - $oaiConfig->setConfig(new Zend_Config($this->getConfigurationArray())); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); + $oaiConfig->setConfig(new Zend_Config($this->getTestConfiguration())); $formatOptions = $oaiConfig->getFormatOptions('unknownPrefix'); $this->assertEquals([], $formatOptions); @@ -163,12 +163,12 @@ public function testGetFormatOptionsUnknownPrefix() public function testGetFormatOptionsNoFormatConfiguration() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); - $configArray = $this->getConfigurationArray(); - unset($configArray['oai']['format']); + $testConfiguration = $this->getTestConfiguration(); + unset($testConfiguration['oai']['format']); - $oaiConfig->setConfig(new Zend_Config($configArray)); + $oaiConfig->setConfig(new Zend_Config($testConfiguration)); $formatOptions = $oaiConfig->getFormatOptions('xMetaDissPlus'); $this->assertEquals([], $formatOptions); @@ -176,14 +176,14 @@ public function testGetFormatOptionsNoFormatConfiguration() public function testGetFormatOptionsWithWrongPrefixCaseInConfiguration() { - $oaiConfig = Oai_Model_OAIConfig::getInstance(); + $oaiConfig = Oai_Model_OaiConfig::getInstance(); - $configArray = $this->getConfigurationArray(); - $configArray['oai']['format']['EPICUR'] = [ + $testConfiguration = $this->getTestConfiguration(); + $testConfiguration['oai']['format']['EPICUR'] = [ 'class' => Oai_Model_Prefix_Epicur_EpicurServer::class, ]; - $oaiConfig->setConfig(new Zend_Config($configArray)); + $oaiConfig->setConfig(new Zend_Config($testConfiguration)); $expectedFormatOptions = [ 'class' => Oai_Model_Prefix_Epicur_EpicurServer::class, @@ -192,4 +192,49 @@ public function testGetFormatOptionsWithWrongPrefixCaseInConfiguration() $formatOptions = $oaiConfig->getFormatOptions('epicur'); $this->assertEquals($expectedFormatOptions, $formatOptions); } + + public function testGetFormats() + { + $oaiConfig = Oai_Model_OaiConfig::getInstance(); + + $testConfiguration = [ + 'oai' => [ + 'format' => [ + 'Default' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + 'copy_xml' => [ + 'xsltFile' => 'copy_xml.xslt', + ], + 'oai_dc' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + 'epicur' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + 'xMetaDissPluss' => [ + 'class' => Oai_Model_DefaultServer::class, + ], + ], + ], + ]; + + $oaiConfig->setConfig(new Zend_Config($testConfiguration)); + + $formats = $oaiConfig->getFormats(); + + $expectedFormats = ['copy_xml', 'oai_dc', 'epicur', 'xmetadisspluss']; + $this->assertEquals($expectedFormats, $formats); + } + + public function testGetResumptionTokenPath() + { + $oaiConfig = Oai_Model_OaiConfig::getInstance(); + $oaiConfig->setConfig(new Zend_Config($this->getTestConfiguration())); + + $resumptionTokenPath = $oaiConfig->getResumptionTokenPath(); + + $expectedResumptionTokenPath = '/vagrant/tests/workspace/tmp/resumption'; + $this->assertEquals($expectedResumptionTokenPath, $resumptionTokenPath); + } } diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index fcf7d7344..025386235 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -34,15 +34,10 @@ class Oai_Model_ServerFactoryTest extends ControllerTestCase /** @var string[] */ protected $additionalResources = ['database', 'view']; - public function setUp(): void - { - parent::setUp(); - } - /** * @return array */ - protected function getConfigurationArray() + protected function getTestConfiguration() { return [ 'workspacePath' => '/vagrant/tests/workspace', @@ -85,103 +80,79 @@ protected function getConfigurationArray() } /** - * @param array|null $configurationArray - * @return Oai_Model_ServerFactory + * @param array|null $configuration + * @return Oai_Model_OaiConfig */ - protected function createServerFactory($configurationArray = null) + protected function getOaiConfig($configuration = null) { - $serverFactory = new Oai_Model_ServerFactory(); - - if ($configurationArray === null) { - $config = new Zend_Config($this->getConfigurationArray()); + if ($configuration === null) { + $config = new Zend_Config($this->getTestConfiguration()); } else { - $config = new Zend_Config($configurationArray); + $config = new Zend_Config($configuration); } - $serverFactory->setConfig($config); - - return $serverFactory; - } - - public function testGetFormats() - { - $configArray = [ - 'oai' => [ - 'format' => [ - 'Default' => [ - 'class' => Oai_Model_DefaultServer::class, - ], - 'copy_xml' => [ - 'xsltFile' => 'copy_xml.xslt', - ], - 'oai_dc' => [ - 'class' => Oai_Model_DefaultServer::class, - ], - 'epicur' => [ - 'class' => Oai_Model_DefaultServer::class, - ], - 'xMetaDissPluss' => [ - 'class' => Oai_Model_DefaultServer::class, - ], - ], - ], - ]; - - $serverFactory = $this->createServerFactory($configArray); + $oaiCongig = Oai_Model_OaiConfig::getInstance(); + $oaiCongig->setConfig($config); - $formats = $serverFactory->getFormats(); - - $expectedFormats = ['copy_xml', 'oai_dc', 'epicur', 'xmetadisspluss']; - $this->assertEquals($expectedFormats, $formats); + return $oaiCongig; } public function testCreate() { - $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create(); - $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($this->getOaiConfig()); + $server = $serverFactory->create(); + $this->assertEquals(DefaultOaiServer::class, get_class($server)); } public function testCreateWithMetadataPrefix() { - $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create('xMetaDissPlus'); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($this->getOaiConfig()); + $server = $serverFactory->create('xMetaDissPlus'); $this->assertEquals(Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer::class, get_class($server)); } public function testCreateWithUnknownMetadataPrefix() { - $serverFactory = $this->createServerFactory(); - $server = $serverFactory->create('unknownPrefix'); - $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($this->getOaiConfig()); + $server = $serverFactory->create('unknownPrefix'); + $this->assertEquals(DefaultOaiServer::class, get_class($server)); } public function testCreateWithNoFormatConfiguration() { - $configArray = $this->getConfigurationArray(); - unset($configArray['oai']['format']); - $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create('xMetaDissPlus'); + $testConfiguration = $this->getTestConfiguration(); + unset($testConfiguration['oai']['format']); + $oaiConfig = $this->getOaiConfig($testConfiguration); + + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($oaiConfig); + $server = $serverFactory->create('xMetaDissPlus'); + $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); } - public function testCreateWithUnkownFormatClass() + public function testCreateWithUnknownFormatClass() { - $configArray = $this->getConfigurationArray(); - $configArray['oai']['format']['xmetadissplus']['class'] = 'UnknownClass'; - $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create('xMetaDissPlus'); + $testConfiguration = $this->getTestConfiguration(); + $testConfiguration['oai']['format']['xmetadissplus']['class'] = 'UnknownClass'; + $oaiConfig = $this->getOaiConfig($testConfiguration); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($oaiConfig); + $server = $serverFactory->create('xMetaDissPlus'); $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); } public function testDefaultServerOptionsNoDefaultConfiguration() { - $configArray = $this->getConfigurationArray(); - - unset($configArray['oai']['format']['default']); - - $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create(); + $testConfiguration = $this->getTestConfiguration(); + unset($testConfiguration['oai']['format']['default']); + $oaiConfig = $this->getOaiConfig($testConfiguration); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($oaiConfig); + $server = $serverFactory->create(); $expectedOptions = [ 'maxListIdentifiers' => 10, @@ -204,9 +175,9 @@ public function testDefaultServerOptionsNoDefaultConfiguration() public function testDefaultServerOptionsWithDefaultConfiguration() { - $configArray = $this->getConfigurationArray(); - $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create(); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($this->getOaiConfig()); + $server = $serverFactory->create(); $expectedOptions = [ 'maxListIdentifiers' => 10, @@ -223,20 +194,20 @@ public function testDefaultServerOptionsWithDefaultConfiguration() 'checkEmbargo' => false, ]; - $this->assertEquals(Oai_Model_DefaultServer::class, get_class($server)); + $this->assertEquals(DefaultOaiServer::class, get_class($server)); $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } public function testFormatServerClassOverwritesDefaults() { - $configArray = $this->getConfigurationArray(); - - $configArray['oai']['format']['xmetadissplus'] = [ + $testConfiguration = $this->getTestConfiguration(); + $testConfiguration['oai']['format']['xmetadissplus'] = [ 'class' => XMetaDissPlusServer::class, ]; - - $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create('xmetadissplus'); + $oaiConfig = $this->getOaiConfig($testConfiguration); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($oaiConfig); + $server = $serverFactory->create('xmetadissplus'); $expectedOptions = [ 'maxListIdentifiers' => 10, @@ -262,17 +233,17 @@ public function testFormatServerClassOverwritesDefaults() public function testFormatServerClassOptionsOverwrittenByFormatConfiguration() { - $configArray = $this->getConfigurationArray(); - - $configArray['oai']['format']['xmetadissplus'] = [ + $testConfiguration = $this->getTestConfiguration(); + $testConfiguration['oai']['format']['xmetadissplus'] = [ 'class' => XMetaDissPlusServer::class, 'viewHelper' => 'additionalViewHelper1, additionalViewHelper2', 'xsltFile' => 'configuredXMetaDissPlus.xslt', 'checkEmbargo' => 0, ]; - - $serverFactory = $this->createServerFactory($configArray); - $server = $serverFactory->create('xmetadissplus'); + $oaiConfig = $this->getOaiConfig($testConfiguration); + $serverFactory = new Oai_Model_ServerFactory(); + $serverFactory->setOaiConfig($oaiConfig); + $server = $serverFactory->create('xmetadissplus'); $expectedOptions = [ 'maxListIdentifiers' => 10, diff --git a/tests/support/XMetaDissPlusServer.php b/tests/support/XMetaDissPlusServer.php index d5d7ebafc..6c16c18b6 100644 --- a/tests/support/XMetaDissPlusServer.php +++ b/tests/support/XMetaDissPlusServer.php @@ -28,17 +28,11 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class XMetaDissPlusServer extends Oai_Model_DefaultServer +class XMetaDissPlusServer extends Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer { - protected function initDefaults() + protected function initFormatDefaults() { - $this->setHasFilesVisibleInOai(true); - $this->setCheckEmbargo(true); - $this->setNotEmbargoedOn(true); - $this->setXsltFile('XMetaDissPlus.xslt'); - $this->setPrefixLabel('xMetaDissPlus'); - $this->setSchemaUrl('http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd'); - $this->setMetadataNamespaceUrl('http://www.d-nb.de/standards/xmetadissplus/'); + parent::initFormatDefaults(); $this->setViewHelper('optionValue, fileUrl, frontdoorUrl'); } } From 4963cf7cf471babb544d001e4d44c39684f679d4 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 7 Sep 2023 14:17:58 +0200 Subject: [PATCH 30/59] #1101 Fix coding style. --- modules/oai/models/DefaultServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 0bc845189..7719228c2 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1351,7 +1351,7 @@ public function setVisible($visible) * Checks if exporting a document is allowed, if not an exception will be thrown. * * @param Document $document - * @param string $metadataPrefix + * @param string $metadataPrefix * @throws Oai_Model_Exception */ protected function checkExportAllowed($document, $metadataPrefix) From f8cd37956ad21a36a1ed641a7c73db686ede3e7d Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 7 Sep 2023 15:28:09 +0200 Subject: [PATCH 31/59] #1101 Added tests for ListMetadataFormats viewhelper. --- .../views/helpers/ListMetaDataFormatsTest.php | 242 ++++++++++++++++++ 1 file changed, 242 insertions(+) create mode 100644 tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php diff --git a/tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php b/tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php new file mode 100644 index 000000000..69a51c68a --- /dev/null +++ b/tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php @@ -0,0 +1,242 @@ +' + . 'oai_dc' + . 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd' + . 'http://www.openarchives.org/OAI/2.0/oai_dc/' + . '' + . '' + . 'epicur' + . 'http://www.persistent-identifier.de/xepicur/version1.0/xepicur.xsd' + . 'urn:nbn:de:1111-2004033116' + . '' + . '' + . 'xMetaDissPlus' + . 'http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd' + . 'http://www.d-nb.de/standards/xmetadissplus/' + . '' + . '' + . 'MARC21' + . 'https://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd' + . 'http://www.loc.gov/MARC21/slim' + . ''; + } + + protected function getExpectedMetadaFormatsWithoutOaiDc() + { + return '' + . 'epicur' + . 'http://www.persistent-identifier.de/xepicur/version1.0/xepicur.xsd' + . 'urn:nbn:de:1111-2004033116' + . '' + . '' + . 'xMetaDissPlus' + . 'http://files.dnb.de/standards/xmetadissplus/xmetadissplus.xsd' + . 'http://www.d-nb.de/standards/xmetadissplus/' + . '' + . '' + . 'MARC21' + . 'https://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd' + . 'http://www.loc.gov/MARC21/slim' + . ''; + } + + public function testListMetadataFormats() + { + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormats(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsFormatNotVisible() + { + $this->enableSecurity(); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 0, + 'adminOnly' => 0, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormatsWithoutOaiDc(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsFormatNotVisibleAndAdminOnly() + { + $this->enableSecurity(); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 0, + 'adminOnly' => 1, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormatsWithoutOaiDc(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsFormatVisible() + { + $this->enableSecurity(); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 1, + 'adminOnly' => 0, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormats(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsFormatVisibleAndAdminOnly() + { + $this->enableSecurity(); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 1, + 'adminOnly' => 1, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormatsWithoutOaiDc(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsAsAdminWithFormatNotVisible() + { + $this->enableSecurity(); + $this->loginUser('admin', 'adminadmin'); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 0, + 'adminOnly' => 0, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $formatList = $listMetaDataFormats->listMetadataFormats(); + $this->assertEquals($this->getExpectedMetadaFormatsWithoutOaiDc(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsAsAdminWithFormatNotVisibleAndAdminOnly() + { + $this->enableSecurity(); + $this->loginUser('admin', 'adminadmin'); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 0, + 'adminOnly' => 1, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormatsWithoutOaiDc(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsAsAdminWithFormatVisible() + { + $this->enableSecurity(); + $this->loginUser('admin', 'adminadmin'); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 1, + 'adminOnly' => 0, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormats(), $listMetaDataFormats->listMetadataFormats()); + } + + public function testListMetadataFormatsAsAdminWithFormatVisibleAndAdminOnly() + { + $this->enableSecurity(); + $this->loginUser('admin', 'adminadmin'); + + $this->adjustConfiguration([ + 'oai' => [ + 'format' => [ + 'oai_dc' => [ + 'visible' => 1, + 'adminOnly' => 1, + ], + ], + ], + ]); + + $listMetaDataFormats = new Oai_View_Helper_ListMetaDataFormats(); + $this->assertEquals($this->getExpectedMetadaFormats(), $listMetaDataFormats->listMetadataFormats()); + } +} From 52593fc6208b3e2f2e7900151626c2391bb3f1df Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 7 Sep 2023 16:50:01 +0200 Subject: [PATCH 32/59] #1101 Removed deprecated property. --- modules/oai/models/DefaultServer.php | 2 +- modules/oai/models/Request.php | 24 ------------------------ tests/modules/oai/models/RequestTest.php | 1 - 3 files changed, 1 insertion(+), 26 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 7719228c2..75fbfeb47 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -235,7 +235,7 @@ protected function handleRequestIntern($oaiRequest, $requestUri) $resumptionPath = $this->getResumptionTokenPath(); $request = new Oai_Model_Request(); - $request->setPathToMetadataPrefixFiles($metadataPrefixPath); + $request->setResumptionPath($resumptionPath); // check for duplicate parameters diff --git a/modules/oai/models/Request.php b/modules/oai/models/Request.php index 08a3b7356..2d139b72b 100644 --- a/modules/oai/models/Request.php +++ b/modules/oai/models/Request.php @@ -46,9 +46,6 @@ class Oai_Model_Request /** @var string */ private $errorMessage; - /** @var string */ - private $pathToMetadataPrefixFiles; - /** @var string */ private $resumptionPath; @@ -296,27 +293,6 @@ protected function setErrorMessage($message) $this->errorMessage = $message; } - /** - * Set path to meta data prefix files. - * Returns false if given path is not a directory. - * There is no check if files are inside given directory! - * - * @param mixed $path - * @return bool - */ - public function setPathToMetadataPrefixFiles($path) - { - $realpathToFiles = realpath($path); - - $result = is_dir($realpathToFiles); - - if (true === $result) { - $this->pathToMetadataPrefixFiles = $realpathToFiles; - } - - return $result; - } - /** * Set path to directory where resumption tokens read / stored. * Checks only if given path is a directory. diff --git a/tests/modules/oai/models/RequestTest.php b/tests/modules/oai/models/RequestTest.php index 152d61ef0..b0ccaa342 100644 --- a/tests/modules/oai/models/RequestTest.php +++ b/tests/modules/oai/models/RequestTest.php @@ -39,7 +39,6 @@ public function setUp(): void parent::setUp(); $this->requestObj = new Oai_Model_Request(); - $this->requestObj->setPathToMetadataPrefixFiles(APPLICATION_PATH . '/modules/oai/views/scripts/index/prefixes'); } /** From 79cac6db0c689156a83135245ad463149c1a9a55 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 7 Sep 2023 16:50:20 +0200 Subject: [PATCH 33/59] #1101 Fix some coding style. --- tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php b/tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php index 69a51c68a..f1f39e4da 100644 --- a/tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php +++ b/tests/modules/oai/views/helpers/ListMetaDataFormatsTest.php @@ -34,6 +34,9 @@ class Oai_View_Helper_ListMetaDataFormatsTest extends ControllerTestCase /** @var string */ protected $additionalResources = 'database'; + /** + * @return string + */ protected function getExpectedMetadaFormats() { return '' @@ -58,6 +61,9 @@ protected function getExpectedMetadaFormats() . ''; } + /** + * @return string + */ protected function getExpectedMetadaFormatsWithoutOaiDc() { return '' From c4a8ba62475c1e4e951a05b479ba87ebdb7b73ff Mon Sep 17 00:00:00 2001 From: j3nsch Date: Mon, 25 Sep 2023 17:59:36 +0200 Subject: [PATCH 34/59] #766 Added comments to OAI config --- application/configs/application.ini | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/application/configs/application.ini b/application/configs/application.ini index 5dc5ca2ad..a8524ce95 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -102,14 +102,22 @@ oai.description.eprints.submissionPolicy.text = oai.description.eprints.comment.url = oai.description.eprints.comment.text = -; Configuration of the oai formats -; Optional custom oai server class name -; oai.format.[lower cased format identifier].class = [oai server class name] -; The xslt file -; oai.format.[lower cased format identifier ].xsltFile = [xslt file name] -; The format prefix -; oai.format.[lower cased format identifier ].prefix = [prefix] +; OAI FORMAT OPTIONS +; +; FORMAT_ID should be a lowercase string identifying the format, like `marc21`. The default configuration options +; are: +; +; oai.format.[FORMAT_ID]. +; class = [class implementing format for OAI] +; xsltFile = [xslt file name or location] +; prefix = [metadata prefix used for format] +; visible = 0 or 1 (visibility in ListMetadataFormats) +; adminOnly = 0 or 1 (restrict access to format to administrators) +; viewHelper = [list of view helpers available in XSLT] + +; Default OAI server class implementing all the format independent responses oai.format.default.class = Oai_Model_DefaultServer +; ViewHelpers available in OAI XSLT (additional helpers can be added using this option for a specific format) oai.format.default.viewHelper = optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType ; OPUS 4 XML (only for admins) @@ -117,6 +125,7 @@ oai.format.copy_xml.xsltFile = copy_xml.xslt oai.format.copy_xml.adminOnly = 1 oai.format.copy_xml.visible = 0 +; OAI supported formats (default configuration is part of implementing class) oai.format.oai_dc.class = Oai_Model_Prefix_Oai_OaiDcServer oai.format.oai_pp.class = Oai_Model_Prefix_Oai_OaiPpServer oai.format.epicur.class = Oai_Model_Prefix_Epicur_EpicurServer From a8d230c47f52632cfbf47ee60e61cfdb3e81ce15 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 5 Oct 2023 18:20:57 +0200 Subject: [PATCH 35/59] #766 Rename option "viewHelper" --- application/configs/application.ini | 2 +- modules/oai/models/DefaultServer.php | 30 +++++++++---------- modules/oai/models/ServerFactory.php | 12 ++++---- tests/modules/oai/models/OaiConfigTest.php | 8 ++--- .../modules/oai/models/ServerFactoryTest.php | 12 ++++---- tests/support/XMetaDissPlusServer.php | 2 +- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/application/configs/application.ini b/application/configs/application.ini index a8524ce95..d308ae6d6 100644 --- a/application/configs/application.ini +++ b/application/configs/application.ini @@ -118,7 +118,7 @@ oai.description.eprints.comment.text = ; Default OAI server class implementing all the format independent responses oai.format.default.class = Oai_Model_DefaultServer ; ViewHelpers available in OAI XSLT (additional helpers can be added using this option for a specific format) -oai.format.default.viewHelper = optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType +oai.format.default.viewHelpers = optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType ; OPUS 4 XML (only for admins) oai.format.copy_xml.xsltFile = copy_xml.xslt diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 75fbfeb47..8419335a0 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -109,8 +109,8 @@ class Oai_Model_DefaultServer extends Application_Model_Abstract /** @var string */ private $xsltFile; - /** @var string */ - private $viewHelper; + /** @var array */ + private $viewHelpers; /** @var bool */ private $checkEmbargo = false; @@ -331,7 +331,7 @@ protected function handleRequestIntern($oaiRequest, $requestUri) protected function setupProcessor() { $this->proc->registerPHPFunctions('Opus\Common\Language::getLanguageCode'); - Application_Xslt::registerViewHelper($this->proc, $this->getViewHelper()); + Application_Xslt::registerViewHelper($this->proc, $this->getViewHelpers()); $this->proc->setParameter('', 'urnResolverUrl', $this->getConfig()->urn->resolverUrl); $this->proc->setParameter('', 'doiResolverUrl', $this->getConfig()->doi->resolverUrl); @@ -1120,35 +1120,35 @@ public function setXsltFile($file) } /** - * Gets the viewHelper + * Gets the viewHelpers * * @return array */ - public function getViewHelper() + public function getViewHelpers() { - $viewHelper = $this->viewHelper ?? []; + $viewHelpers = $this->viewHelpers ?? []; // listMetadataFormats ist part of basic OAI functionality. - $viewHelper[] = 'listMetadataFormats'; + $viewHelpers[] = 'listMetadataFormats'; - return $viewHelper; + return $viewHelpers; } /** - * Sets the viewHelper + * Sets the viewHelpers * - * @param array|string $viewHelper + * @param array|string $viewHelpers */ - public function setViewHelper($viewHelper) + public function setViewHelpers($viewHelpers) { - if (is_string($viewHelper)) { - $viewHelper = array_map('trim', explode(',', $viewHelper)); + if (is_string($viewHelpers)) { + $viewHelpers = array_map('trim', explode(',', $viewHelpers)); } // listMetadataFormats ist part of basic OAI functionality. - $viewHelper = array_values(array_diff($viewHelper, ['listMetadataFormats'])); + $viewHelpers = array_values(array_diff($viewHelpers, ['listMetadataFormats'])); - $this->viewHelper = $viewHelper; + $this->viewHelpers = $viewHelpers; } /** diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 8e475d8ca..54abc8e99 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -97,15 +97,15 @@ public function create($metaDataPrefix = null) $server->initDefaults(); if ($options) { - if (isset($options['viewHelper'])) { - $previousViewHelper = $server->getViewHelper() ?: []; - $viewHelper = $options['viewHelper']; + if (isset($options['viewHelpers'])) { + $previousViewHelpers = $server->getViewHelpers() ?: []; + $viewHelpers = $options['viewHelpers']; - if (is_string($viewHelper)) { - $viewHelper = array_map('trim', explode(',', $viewHelper)); + if (is_string($viewHelpers)) { + $viewHelpers = array_map('trim', explode(',', $viewHelpers)); } - $options['viewHelper'] = array_unique(array_merge($previousViewHelper, $viewHelper)); + $options['viewHelpers'] = array_unique(array_merge($previousViewHelpers, $viewHelpers)); } $server->setOptions($options); diff --git a/tests/modules/oai/models/OaiConfigTest.php b/tests/modules/oai/models/OaiConfigTest.php index 63cb5e8b7..75d3ce840 100644 --- a/tests/modules/oai/models/OaiConfigTest.php +++ b/tests/modules/oai/models/OaiConfigTest.php @@ -50,9 +50,9 @@ protected function getTestConfiguration() ], 'format' => [ 'default' => [ - 'class' => DefaultOaiServer::class, - 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', - 'xsltFile' => 'oaiFile.xslt', + 'class' => DefaultOaiServer::class, + 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + 'xsltFile' => 'oaiFile.xslt', ], 'copy_xml' => [ 'xsltFile' => 'copy_xml.xslt', @@ -89,7 +89,7 @@ public function testGetDefaults() 'emailContact' => 'opus4ci@example.org', 'xsltFile' => 'oaiFile.xslt', 'class' => DefaultOaiServer::class, - 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', ]; $this->assertEquals($expectedDefaults, $defaults); diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 025386235..396b39fde 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -54,7 +54,7 @@ protected function getTestConfiguration() 'format' => [ 'default' => [ 'class' => DefaultOaiServer::class, - 'viewHelper' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', + 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', 'xsltFile' => 'oaiFile.xslt', 'hasFilesVisibleInOai' => 1, ], @@ -161,7 +161,7 @@ public function testDefaultServerOptionsNoDefaultConfiguration() 'emailContact' => 'opus4ci@example.org', 'xsltFile' => '', 'documentStatesAllowed' => ['published', 'deleted'], - 'viewHelper' => ['listMetadataFormats'], + 'viewHelpers' => ['listMetadataFormats'], 'notEmbargoedOn' => false, 'hasFilesVisibleInOai' => false, 'adminOnly' => false, @@ -186,7 +186,7 @@ public function testDefaultServerOptionsWithDefaultConfiguration() 'emailContact' => 'opus4ci@example.org', 'xsltFile' => 'oaiFile.xslt', 'documentStatesAllowed' => ['published', 'deleted'], - 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType', 'listMetadataFormats'], + 'viewHelpers' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType', 'listMetadataFormats'], 'notEmbargoedOn' => false, 'hasFilesVisibleInOai' => true, 'adminOnly' => false, @@ -216,7 +216,7 @@ public function testFormatServerClassOverwritesDefaults() 'emailContact' => 'opus4ci@example.org', 'xsltFile' => 'XMetaDissPlus.xslt', 'documentStatesAllowed' => ['published', 'deleted'], - 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'listMetadataFormats'], + 'viewHelpers' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'listMetadataFormats'], 'notEmbargoedOn' => true, 'hasFilesVisibleInOai' => true, 'adminOnly' => false, @@ -236,7 +236,7 @@ public function testFormatServerClassOptionsOverwrittenByFormatConfiguration() $testConfiguration = $this->getTestConfiguration(); $testConfiguration['oai']['format']['xmetadissplus'] = [ 'class' => XMetaDissPlusServer::class, - 'viewHelper' => 'additionalViewHelper1, additionalViewHelper2', + 'viewHelpers' => 'additionalViewHelper1, additionalViewHelper2', 'xsltFile' => 'configuredXMetaDissPlus.xslt', 'checkEmbargo' => 0, ]; @@ -252,7 +252,7 @@ public function testFormatServerClassOptionsOverwrittenByFormatConfiguration() 'emailContact' => 'opus4ci@example.org', 'xsltFile' => 'configuredXMetaDissPlus.xslt', 'documentStatesAllowed' => ['published', 'deleted'], - 'viewHelper' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'additionalViewHelper1', 'additionalViewHelper2', 'listMetadataFormats'], + 'viewHelpers' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'additionalViewHelper1', 'additionalViewHelper2', 'listMetadataFormats'], 'notEmbargoedOn' => true, 'hasFilesVisibleInOai' => true, 'adminOnly' => false, diff --git a/tests/support/XMetaDissPlusServer.php b/tests/support/XMetaDissPlusServer.php index 6c16c18b6..ab17cb6f2 100644 --- a/tests/support/XMetaDissPlusServer.php +++ b/tests/support/XMetaDissPlusServer.php @@ -33,6 +33,6 @@ class XMetaDissPlusServer extends Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusSe protected function initFormatDefaults() { parent::initFormatDefaults(); - $this->setViewHelper('optionValue, fileUrl, frontdoorUrl'); + $this->setViewHelpers('optionValue, fileUrl, frontdoorUrl'); } } From 7967b50b6c114608146535adb20f276a7fbff471 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 5 Oct 2023 18:34:20 +0200 Subject: [PATCH 36/59] #766 Fixed some typos. --- modules/oai/models/DefaultServer.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 8419335a0..606ade958 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1100,7 +1100,7 @@ public function setMaxListRecords($maxListRecs) } /** - * Return xlst file name / file path. + * Return xslt file name / file path. * * @return string */ @@ -1110,7 +1110,7 @@ public function getXsltFile() } /** - * Sets the xlst file name / file path. + * Sets the xslt file name / file path. * * @param string $file */ @@ -1186,7 +1186,7 @@ public function getDocumentTypesAllowed() * * @param array|string $documentTypesAllowed */ - public function setdocumentTypesAllowed($documentTypesAllowed) + public function setDocumentTypesAllowed($documentTypesAllowed) { if (is_string($documentTypesAllowed)) { $this->documentTypesAllowed = [$documentTypesAllowed]; From 42c37f6e0454992149321fdf8228a84557e19176 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 5 Oct 2023 18:51:59 +0200 Subject: [PATCH 37/59] #766 Fix return empty string for integer variables. Renaming maxRecords parameter. --- modules/oai/models/DefaultServer.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 606ade958..f90296d56 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -100,10 +100,10 @@ class Oai_Model_DefaultServer extends Application_Model_Abstract /** @var string */ private $sampleIdentifier; - /** @var string */ + /** @var int */ private $maxListIdentifiers; - /** @var string */ + /** @var int */ private $maxListRecords; /** @var string */ @@ -492,12 +492,12 @@ protected function handleListSets() /** * Helper method for handling lists. * - * @param mixed $maxRecords + * @param int $maxItems */ - private function handlingOfLists(array &$oaiRequest, $maxRecords) + private function handlingOfLists(array &$oaiRequest, $maxItems) { - if (true === empty($maxRecords)) { - $maxRecords = 100; + if (empty($maxItems)) { + $maxItems = 100; } $repIdentifier = $this->getRepositoryIdentifier(); @@ -509,7 +509,7 @@ private function handlingOfLists(array &$oaiRequest, $maxRecords) // do some initialisation $cursor = 0; $totalIds = 0; - $start = $maxRecords + 1; + $start = $maxItems + 1; $restIds = []; $metadataPrefix = null; @@ -532,7 +532,7 @@ private function handlingOfLists(array &$oaiRequest, $maxRecords) } $cursor = $token->getStartPosition() - 1; - $start = $token->getStartPosition() + $maxRecords; + $start = $token->getStartPosition() + $maxItems; $totalIds = $token->getTotalIds(); $restIds = $token->getDocumentIds(); $metadataPrefix = $token->getMetadataPrefix(); @@ -550,7 +550,7 @@ private function handlingOfLists(array &$oaiRequest, $maxRecords) } // handling of document ids - $workIds = array_splice($restIds, 0, $maxRecords); + $workIds = array_splice($restIds, 0, $maxItems); foreach ($workIds as $docId) { $document = Document::get($docId); @@ -1066,7 +1066,7 @@ public function setSampleIdentifier($sampleId) */ public function getMaxListIdentifiers() { - return $this->maxListIdentifiers ?? ''; + return $this->maxListIdentifiers ?? 0; } /** @@ -1086,7 +1086,7 @@ public function setMaxListIdentifiers($maxListIds) */ public function getMaxListRecords() { - return $this->maxListRecords ?? ''; + return $this->maxListRecords ?? 0; } /** From 882c038a22d4e28ac1d5b1e8a7beadd59b14d622 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 5 Oct 2023 18:59:18 +0200 Subject: [PATCH 38/59] #766 Now also checking for array. --- modules/oai/models/DefaultServer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index f90296d56..25ade7d8b 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1190,7 +1190,7 @@ public function setDocumentTypesAllowed($documentTypesAllowed) { if (is_string($documentTypesAllowed)) { $this->documentTypesAllowed = [$documentTypesAllowed]; - } else { + } elseif (is_array($documentTypesAllowed)) { $this->documentTypesAllowed = $documentTypesAllowed; } } @@ -1214,7 +1214,7 @@ public function setDocumentStatesAllowed($documentStatesAllowed) { if (is_string($documentStatesAllowed)) { $this->documentStatesAllowed = [$documentStatesAllowed]; - } else { + } elseif (is_array($documentStatesAllowed)) { $this->documentStatesAllowed = $documentStatesAllowed; } } From 8c5cec9c3a2ff9dd957b9d986f6d975247e84a6a Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 5 Oct 2023 19:06:26 +0200 Subject: [PATCH 39/59] #766 Using DocumentInterface instead of Document. --- modules/oai/models/DefaultServer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 25ade7d8b..dbc816a18 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1350,8 +1350,8 @@ public function setVisible($visible) /** * Checks if exporting a document is allowed, if not an exception will be thrown. * - * @param Document $document - * @param string $metadataPrefix + * @param DocumentInterface $document + * @param string $metadataPrefix * @throws Oai_Model_Exception */ protected function checkExportAllowed($document, $metadataPrefix) From 70b750492507b4c5ff793877f568455e296f14a6 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Thu, 5 Oct 2023 19:25:27 +0200 Subject: [PATCH 40/59] #766 Added class comment. --- modules/oai/models/DefaultServer.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index dbc816a18..7be626652 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -39,6 +39,12 @@ use Opus\Model\Xml; use Opus\Model\Xml\Version1; +/** + * Implements the default behaviour neede for all metadata formats. + * For each concrete metadata format the behaviour can be configured + * via application.ini or instead an own format class be used + * to extend this default class (like in oai/models/prefix). + */ class Oai_Model_DefaultServer extends Application_Model_Abstract { use Oai_Model_OptionsTrait; From e979a6a103162273ddb172c509dd698fae41bbe9 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 08:05:01 +0200 Subject: [PATCH 41/59] #766 Fix class comment. --- modules/oai/models/DefaultServer.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 7be626652..7e005f1be 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -40,10 +40,11 @@ use Opus\Model\Xml\Version1; /** - * Implements the default behaviour neede for all metadata formats. - * For each concrete metadata format the behaviour can be configured - * via application.ini or instead an own format class be used - * to extend this default class (like in oai/models/prefix). + * Implements the default behaviour used for all metadata formats. + * + * For each concrete metadata format the behaviour can be configured via application.ini + * or instead a custom format class can be derived from the default server to extend or change + * the behaviour for a certain format (like in oai/models/prefix). */ class Oai_Model_DefaultServer extends Application_Model_Abstract { From 6f70e085ae13879e00b15cb36200eab568d3b6b4 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 15:17:09 +0200 Subject: [PATCH 42/59] #766 Adjust option getters and setters for NULL values. --- modules/oai/models/DefaultServer.php | 104 +++++++++++------- .../oai/views/helpers/ListMetaDataFormats.php | 13 +++ tests/modules/oai/models/BaseServerTest.php | 42 +++++++ 3 files changed, 119 insertions(+), 40 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 7e005f1be..73762aee2 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -238,8 +238,7 @@ protected function handleRequestIntern($oaiRequest, $requestUri) $this->loadStyleSheet(); $this->setupProcessor(); - $metadataPrefixPath = $this->getScriptPath() . DIRECTORY_SEPARATOR . 'prefixes'; - $resumptionPath = $this->getResumptionTokenPath(); + $resumptionPath = $this->getResumptionTokenPath(); $request = new Oai_Model_Request(); @@ -703,6 +702,8 @@ private function addSpecInformation(DOMNode $document, $information) */ private function addFrontdoorUrlAttribute(DOMNode $document, $docid) { + $this->checkBaseUrl(); + $url = $this->getBaseUrl() . '/frontdoor/index/index/docId/' . $docid; $owner = $document->ownerDocument; @@ -720,6 +721,8 @@ private function addFrontdoorUrlAttribute(DOMNode $document, $docid) */ private function addFileUrlAttribute($file, $docid, $filename) { + $this->checkBaseUrl(); + $url = $this->getBaseUrl() . '/files/' . $docid . '/' . rawurlencode($filename); $owner = $file->ownerDocument; @@ -736,6 +739,8 @@ private function addFileUrlAttribute($file, $docid, $filename) */ private function addDdbTransferElement(DOMNode $document, $docid) { + $this->checkBaseUrl(); + $url = $this->getBaseUrl() . '/oai/container/index/docId/' . $docid; $fileElement = $document->ownerDocument->createElement('TransferUrl'); @@ -876,7 +881,7 @@ public function setOaiConfig($oaiConfig) } /** - * @return string + * @return string|false */ public function getScriptPath() { @@ -884,7 +889,7 @@ public function getScriptPath() } /** - * @param string $scriptPath + * @param string|false $scriptPath */ public function setScriptPath($scriptPath) { @@ -892,7 +897,7 @@ public function setScriptPath($scriptPath) } /** - * @return string + * @return string|null */ public function getBaseUrl() { @@ -900,7 +905,7 @@ public function getBaseUrl() } /** - * @param string $baseUrl + * @param string|null $baseUrl */ public function setBaseUrl($baseUrl) { @@ -908,7 +913,7 @@ public function setBaseUrl($baseUrl) } /** - * @return string + * @return string|null */ public function getBaseUri() { @@ -916,7 +921,7 @@ public function getBaseUri() } /** - * @param string $baseUri + * @param string|null $baseUri */ public function setBaseUri($baseUri) { @@ -952,7 +957,7 @@ public function getResumptionTokenPath() /** * Sets the temporary path for resumption tokens. * - * @param string $path + * @param string|null $path */ public function setResumptionTokenPath($path) { @@ -972,7 +977,7 @@ public function getEmailContact() /** * Sets the contact email address. * - * @param string $email + * @param string|null $email */ public function setEmailContact($email) { @@ -999,7 +1004,7 @@ public function getOaiBaseUrl() /** * Sets the OAI base url. * - * @param string $url + * @param string|null $url */ public function setOaiBaseUrl($url) { @@ -1019,7 +1024,7 @@ public function getRepositoryName() /** * Sets the repository name. * - * @param string $repoName + * @param string|null $repoName */ public function setRepositoryName($repoName) { @@ -1039,7 +1044,7 @@ public function getRepositoryIdentifier() /** * Sets the repository identifier. * - * @param string $repoId + * @param string|null $repoId */ public function setRepositoryIdentifier($repoId) { @@ -1059,7 +1064,7 @@ public function getSampleIdentifier() /** * Sets the sample identifier. * - * @param string $sampleId + * @param string|null $sampleId */ public function setSampleIdentifier($sampleId) { @@ -1073,7 +1078,7 @@ public function setSampleIdentifier($sampleId) */ public function getMaxListIdentifiers() { - return $this->maxListIdentifiers ?? 0; + return $this->maxListIdentifiers; } /** @@ -1093,7 +1098,7 @@ public function setMaxListIdentifiers($maxListIds) */ public function getMaxListRecords() { - return $this->maxListRecords ?? 0; + return $this->maxListRecords; } /** @@ -1109,17 +1114,17 @@ public function setMaxListRecords($maxListRecs) /** * Return xslt file name / file path. * - * @return string + * @return string|null */ public function getXsltFile() { - return $this->xsltFile ?? ''; + return $this->xsltFile; } /** * Sets the xslt file name / file path. * - * @param string $file + * @param string|null $file */ public function setXsltFile($file) { @@ -1144,16 +1149,18 @@ public function getViewHelpers() /** * Sets the viewHelpers * - * @param array|string $viewHelpers + * @param array|string|null $viewHelpers */ public function setViewHelpers($viewHelpers) { if (is_string($viewHelpers)) { - $viewHelpers = array_map('trim', explode(',', $viewHelpers)); + $viewHelpers = $viewHelpers !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; } - // listMetadataFormats ist part of basic OAI functionality. - $viewHelpers = array_values(array_diff($viewHelpers, ['listMetadataFormats'])); + if (is_array($viewHelpers)) { + // listMetadataFormats ist part of basic OAI functionality. + $viewHelpers = array_values(array_diff($viewHelpers, ['listMetadataFormats'])); + } $this->viewHelpers = $viewHelpers; } @@ -1185,19 +1192,19 @@ public function setCheckEmbargo($checkEmbargo) */ public function getDocumentTypesAllowed() { - return $this->documentTypesAllowed; + return $this->documentTypesAllowed ?? []; } /** * Sets the allowed document types. * - * @param array|string $documentTypesAllowed + * @param array|string|null $documentTypesAllowed */ public function setDocumentTypesAllowed($documentTypesAllowed) { if (is_string($documentTypesAllowed)) { - $this->documentTypesAllowed = [$documentTypesAllowed]; - } elseif (is_array($documentTypesAllowed)) { + $this->documentTypesAllowed = $documentTypesAllowed !== '' ? [$documentTypesAllowed] : []; + } elseif (is_array($documentTypesAllowed) || $documentTypesAllowed === null) { $this->documentTypesAllowed = $documentTypesAllowed; } } @@ -1209,19 +1216,19 @@ public function setDocumentTypesAllowed($documentTypesAllowed) */ public function getDocumentStatesAllowed() { - return $this->documentStatesAllowed; + return $this->documentStatesAllowed ?? []; } /** * Sets the allowed document states * - * @param array|string $documentStatesAllowed + * @param array|string|null $documentStatesAllowed */ public function setDocumentStatesAllowed($documentStatesAllowed) { if (is_string($documentStatesAllowed)) { - $this->documentStatesAllowed = [$documentStatesAllowed]; - } elseif (is_array($documentStatesAllowed)) { + $this->documentStatesAllowed = $documentStatesAllowed !== '' ? [$documentStatesAllowed] : []; + } elseif (is_array($documentStatesAllowed) || $documentStatesAllowed === null) { $this->documentStatesAllowed = $documentStatesAllowed; } } @@ -1243,7 +1250,7 @@ public function setNotEmbargoedOn($notEmbargoedOn) } /** - * @return string + * @return string|null */ public function getIdentifierExists() { @@ -1251,7 +1258,7 @@ public function getIdentifierExists() } /** - * @param string $identifierExists + * @param string|null $identifierExists */ public function setIdentifierExists($identifierExists) { @@ -1275,7 +1282,7 @@ public function setHasFilesVisibleInOai($hasFilesVisibleInOai) } /** - * @return string + * @return string|null */ public function getPrefixLabel() { @@ -1283,7 +1290,7 @@ public function getPrefixLabel() } /** - * @param string $prefixLabel + * @param string|null $prefixLabel */ public function setPrefixLabel($prefixLabel) { @@ -1291,7 +1298,7 @@ public function setPrefixLabel($prefixLabel) } /** - * @return string + * @return string|null */ public function getSchemaUrl() { @@ -1299,7 +1306,7 @@ public function getSchemaUrl() } /** - * @param string $schemaUrl + * @param string|null $schemaUrl */ public function setSchemaUrl($schemaUrl) { @@ -1307,7 +1314,7 @@ public function setSchemaUrl($schemaUrl) } /** - * @return string + * @return string|null */ public function getMetadataNamespaceUrl() { @@ -1315,7 +1322,7 @@ public function getMetadataNamespaceUrl() } /** - * @param string $metadataNamespaceUrl + * @param string|null $metadataNamespaceUrl */ public function setMetadataNamespaceUrl($metadataNamespaceUrl) { @@ -1331,7 +1338,7 @@ public function isAdminOnly() } /** - * @param mixed $adminOnly + * @param bool|string $adminOnly */ public function setAdminOnly($adminOnly) { @@ -1420,4 +1427,21 @@ public function getFinder($metadataPrefix) return $finder; } + + /** + * Checks if a base url has been set. + * + * @return void + * @throws Zend_Exception + */ + private function checkBaseUrl() + { + $baseUrl = $this->getBaseUrl(); + + if (empty($baseUrl)) { + $message = 'No base url set.'; + Log::get()->err($message); + throw new Exception($message); + } + } } diff --git a/modules/oai/views/helpers/ListMetaDataFormats.php b/modules/oai/views/helpers/ListMetaDataFormats.php index ab4d21726..0e8a2196d 100644 --- a/modules/oai/views/helpers/ListMetaDataFormats.php +++ b/modules/oai/views/helpers/ListMetaDataFormats.php @@ -29,6 +29,7 @@ * @license http://www.gnu.org/licenses/gpl.html General Public License */ +use Opus\Common\Log; use Opus\Common\Security\Realm; /** @@ -60,6 +61,18 @@ public function listMetadataFormats() if ($server->isVisible() && (! $server->isAdminOnly() || Realm::getInstance()->checkModule('admin'))) { if ($prefix) { + if (empty($schemaUrl)) { + $message = 'No schema url set.'; + Log::get()->err($message); + throw new Exception($message); + } + + if (empty($metadataNamespaceUrl)) { + $message = 'No metadata namespace url set.'; + Log::get()->err($message); + throw new Exception($message); + } + $output .= '' . "$prefix" . "$schemaUrl" diff --git a/tests/modules/oai/models/BaseServerTest.php b/tests/modules/oai/models/BaseServerTest.php index daa959a0e..7298b8f1c 100644 --- a/tests/modules/oai/models/BaseServerTest.php +++ b/tests/modules/oai/models/BaseServerTest.php @@ -63,4 +63,46 @@ public function testGetOptionNotInOptionsArray() $baseServer->setOptions(['repositoryIdentifier' => '']); $this->assertEquals('', $baseServer->getRepositoryIdentifier()); } + + public function testSetViewHelpersWithNull() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers(null); + $this->assertEquals(['listMetadataFormats'], $baseServer->getViewHelpers()); + } + + public function testSetViewHelpersWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers(''); + $this->assertEquals(['listMetadataFormats'], $baseServer->getViewHelpers()); + } + + public function testSetDocumentStatesAllowedWithNull() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentStatesAllowed(null); + $this->assertEquals([], $baseServer->getDocumentStatesAllowed()); + } + + public function testSetDocumentStatesAllowedWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentStatesAllowed(''); + $this->assertEquals([], $baseServer->getDocumentStatesAllowed()); + } + + public function testSetDocumentTypesAllowedWithNull() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentTypesAllowed(null); + $this->assertEquals([], $baseServer->getDocumentTypesAllowed()); + } + + public function testSetDocumentTypesAllowedWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setDocumentTypesAllowed(''); + $this->assertEquals([], $baseServer->getDocumentTypesAllowed()); + } } From b773cf15f89a446cb9d1a65eeb202a717b494edb Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 15:28:57 +0200 Subject: [PATCH 43/59] #766 Using shorter parameter names in setter methods. --- modules/oai/models/DefaultServer.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 73762aee2..64b3eddfc 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1198,14 +1198,14 @@ public function getDocumentTypesAllowed() /** * Sets the allowed document types. * - * @param array|string|null $documentTypesAllowed + * @param array|string|null $documentTypes */ - public function setDocumentTypesAllowed($documentTypesAllowed) + public function setDocumentTypesAllowed($documentTypes) { - if (is_string($documentTypesAllowed)) { - $this->documentTypesAllowed = $documentTypesAllowed !== '' ? [$documentTypesAllowed] : []; - } elseif (is_array($documentTypesAllowed) || $documentTypesAllowed === null) { - $this->documentTypesAllowed = $documentTypesAllowed; + if (is_string($documentTypes)) { + $this->documentTypesAllowed = $documentTypes !== '' ? [$documentTypes] : []; + } elseif (is_array($documentTypes) || $documentTypes === null) { + $this->documentTypesAllowed = $documentTypes; } } @@ -1222,14 +1222,14 @@ public function getDocumentStatesAllowed() /** * Sets the allowed document states * - * @param array|string|null $documentStatesAllowed + * @param array|string|null $documentStates */ - public function setDocumentStatesAllowed($documentStatesAllowed) + public function setDocumentStatesAllowed($documentStates) { - if (is_string($documentStatesAllowed)) { - $this->documentStatesAllowed = $documentStatesAllowed !== '' ? [$documentStatesAllowed] : []; - } elseif (is_array($documentStatesAllowed) || $documentStatesAllowed === null) { - $this->documentStatesAllowed = $documentStatesAllowed; + if (is_string($documentStates)) { + $this->documentStatesAllowed = $documentStates !== '' ? [$documentStates] : []; + } elseif (is_array($documentStates) || $documentStates === null) { + $this->documentStatesAllowed = $documentStates; } } From 111b4421d8d3c58b9b5e7d2db3c1ba45937afc99 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 15:35:35 +0200 Subject: [PATCH 44/59] #766 Fix some comments. --- modules/oai/models/DefaultServer.php | 3 ++- modules/oai/models/prefix/epicur/EpicurServer.php | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 64b3eddfc..5700f35c9 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -163,7 +163,8 @@ public function initDefaults() } /** - * Initializes server options with default values + * Initializes server options with default values. + * * This function is used by derived classes to set their own values for options by overwriting the method * and using setters, in order to overwrite default values. */ diff --git a/modules/oai/models/prefix/epicur/EpicurServer.php b/modules/oai/models/prefix/epicur/EpicurServer.php index 1e78fd5c7..577f0cb13 100644 --- a/modules/oai/models/prefix/epicur/EpicurServer.php +++ b/modules/oai/models/prefix/epicur/EpicurServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class Oai_Model_Prefix_Epicur_EpicurServer extends Oai_Model_DefaultServer { protected function initFormatDefaults() From c28ecb29ab1219cc1b28540e45dba26b342320de Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 15:36:52 +0200 Subject: [PATCH 45/59] #766 Removed unnecessary ternary operator. --- modules/oai/models/ServerFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 54abc8e99..c9b6e71dd 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -98,7 +98,7 @@ public function create($metaDataPrefix = null) if ($options) { if (isset($options['viewHelpers'])) { - $previousViewHelpers = $server->getViewHelpers() ?: []; + $previousViewHelpers = $server->getViewHelpers(); $viewHelpers = $options['viewHelpers']; if (is_string($viewHelpers)) { From 403318ea47fdc6ff8a5c40e7494cc7866a0c7952 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 15:45:09 +0200 Subject: [PATCH 46/59] #766 Removed outdated todo comment. --- modules/oai/views/helpers/ListMetaDataFormats.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/modules/oai/views/helpers/ListMetaDataFormats.php b/modules/oai/views/helpers/ListMetaDataFormats.php index 0e8a2196d..9d793c6e6 100644 --- a/modules/oai/views/helpers/ListMetaDataFormats.php +++ b/modules/oai/views/helpers/ListMetaDataFormats.php @@ -34,8 +34,6 @@ /** * View helper for rendering metadata formats list. - * - * TODO render link only if user has access to module */ class Oai_View_Helper_ListMetaDataFormats extends Application_View_Helper_Abstract { From f25c07a1829803df21428ff2c8334aa00da34b03 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 16:02:48 +0200 Subject: [PATCH 47/59] #766 Renaming the marker for metadata format replacement. --- modules/oai/models/DefaultServer.php | 2 +- modules/oai/views/scripts/index/oai-pmh.xslt | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 5700f35c9..e4c15a6f0 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -842,7 +842,7 @@ protected function loadStyleSheet() } $xsltXml = $this->xslt->saveXML(); $xsltXml = preg_replace( - '//u', + '//u', '', $xsltXml ); diff --git a/modules/oai/views/scripts/index/oai-pmh.xslt b/modules/oai/views/scripts/index/oai-pmh.xslt index efc38796f..68cf619e9 100644 --- a/modules/oai/views/scripts/index/oai-pmh.xslt +++ b/modules/oai/views/scripts/index/oai-pmh.xslt @@ -41,7 +41,8 @@ - + + From d83ab74d31f823881c6ae1fe40c572afade50440 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 16:12:41 +0200 Subject: [PATCH 48/59] #766 Adjust error message. --- modules/oai/models/DefaultServer.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index e4c15a6f0..c7fec7cfc 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1384,11 +1384,8 @@ protected function checkExportAllowed($document, $metadataPrefix) if ($documentTypeRestriction) { $type = $document->getType(); if (! in_array($type, $documentTypeRestriction)) { - throw new Oai_Model_Exception( - 'The combination of the given values results in an empty list (' . $metadataPrefix - . ' only for' . implode(', ', $documentTypeRestriction) . ')', - Oai_Model_Error::NORECORDSMATCH - ); + throw new Oai_Model_Exception('Document is not available for OAI export, ' + . "Illegal document type ($type)!" , Oai_Model_Error::NORECORDSMATCH); } } } From bc3f3acebfdea55daa820e3cdc58bb9cc6d1b5c4 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Fri, 6 Oct 2023 16:20:45 +0200 Subject: [PATCH 49/59] #766 Renamed OaiConfig to match the class name. --- modules/oai/models/{OAIConfig.php => OaiConfig.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/oai/models/{OAIConfig.php => OaiConfig.php} (100%) diff --git a/modules/oai/models/OAIConfig.php b/modules/oai/models/OaiConfig.php similarity index 100% rename from modules/oai/models/OAIConfig.php rename to modules/oai/models/OaiConfig.php From e61c90c76a2ca1555f94f8fafe2f281063c16f3f Mon Sep 17 00:00:00 2001 From: j3nsch Date: Mon, 9 Oct 2023 22:19:59 +0200 Subject: [PATCH 50/59] #766 Fixed coding style --- modules/oai/models/DefaultServer.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index c7fec7cfc..fad58a196 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -882,7 +882,7 @@ public function setOaiConfig($oaiConfig) } /** - * @return string|false + * @return string|null */ public function getScriptPath() { @@ -890,7 +890,7 @@ public function getScriptPath() } /** - * @param string|false $scriptPath + * @param string|null $scriptPath */ public function setScriptPath($scriptPath) { @@ -1385,7 +1385,7 @@ protected function checkExportAllowed($document, $metadataPrefix) $type = $document->getType(); if (! in_array($type, $documentTypeRestriction)) { throw new Oai_Model_Exception('Document is not available for OAI export, ' - . "Illegal document type ($type)!" , Oai_Model_Error::NORECORDSMATCH); + . "Illegal document type ($type)!", Oai_Model_Error::NORECORDSMATCH); } } } @@ -1429,7 +1429,6 @@ public function getFinder($metadataPrefix) /** * Checks if a base url has been set. * - * @return void * @throws Zend_Exception */ private function checkBaseUrl() From f27d9da14afa0c50d3392b9a4770bf8cfa972af7 Mon Sep 17 00:00:00 2001 From: j3nsch Date: Mon, 9 Oct 2023 22:42:40 +0200 Subject: [PATCH 51/59] #766 Empty line after header (coding style) --- modules/oai/models/prefix/marcxml/MarcXmlServer.php | 1 + modules/oai/models/prefix/oai/OaiDcServer.php | 1 + modules/oai/models/prefix/oai/OaiPpServer.php | 1 + modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php | 1 + tests/support/DefaultOaiServer.php | 1 + tests/support/OaiDcServer.php | 1 + tests/support/XMetaDissPlusServer.php | 1 + 7 files changed, 7 insertions(+) diff --git a/modules/oai/models/prefix/marcxml/MarcXmlServer.php b/modules/oai/models/prefix/marcxml/MarcXmlServer.php index 9ab7984a0..4c36cac02 100644 --- a/modules/oai/models/prefix/marcxml/MarcXmlServer.php +++ b/modules/oai/models/prefix/marcxml/MarcXmlServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class Oai_Model_Prefix_MarcXml_MarcXmlServer extends Oai_Model_DefaultServer { protected function initFormatDefaults() diff --git a/modules/oai/models/prefix/oai/OaiDcServer.php b/modules/oai/models/prefix/oai/OaiDcServer.php index 0afb61b26..7230a7256 100644 --- a/modules/oai/models/prefix/oai/OaiDcServer.php +++ b/modules/oai/models/prefix/oai/OaiDcServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class Oai_Model_Prefix_Oai_OaiDcServer extends Oai_Model_DefaultServer { protected function initFormatDefaults() diff --git a/modules/oai/models/prefix/oai/OaiPpServer.php b/modules/oai/models/prefix/oai/OaiPpServer.php index cad0f5063..767bd1bad 100644 --- a/modules/oai/models/prefix/oai/OaiPpServer.php +++ b/modules/oai/models/prefix/oai/OaiPpServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class Oai_Model_Prefix_Oai_OaiPpServer extends Oai_Model_DefaultServer { protected function initFormatDefaults() diff --git a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php index 15da0d3b6..ef2dc5762 100644 --- a/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php +++ b/modules/oai/models/prefix/xmetadissplus/XMetaDissPlusServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer extends Oai_Model_DefaultServer { protected function initFormatDefaults() diff --git a/tests/support/DefaultOaiServer.php b/tests/support/DefaultOaiServer.php index e399c0e35..130dbf173 100644 --- a/tests/support/DefaultOaiServer.php +++ b/tests/support/DefaultOaiServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2008, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class DefaultOaiServer extends Oai_Model_DefaultServer { } diff --git a/tests/support/OaiDcServer.php b/tests/support/OaiDcServer.php index 1ded6e1c6..c9b54b5c2 100644 --- a/tests/support/OaiDcServer.php +++ b/tests/support/OaiDcServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2008, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class OaiDcServer extends Oai_Model_DefaultServer { } diff --git a/tests/support/XMetaDissPlusServer.php b/tests/support/XMetaDissPlusServer.php index ab17cb6f2..6f89b7f46 100644 --- a/tests/support/XMetaDissPlusServer.php +++ b/tests/support/XMetaDissPlusServer.php @@ -28,6 +28,7 @@ * @copyright Copyright (c) 2023, OPUS 4 development team * @license http://www.gnu.org/licenses/gpl.html General Public License */ + class XMetaDissPlusServer extends Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer { protected function initFormatDefaults() From 719d66b9ecc6dbe98381c8c5c23fcabc919542aa Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 14:22:56 +0200 Subject: [PATCH 52/59] #766 Shorten class variable comments. --- modules/oai/models/DefaultServer.php | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index fad58a196..50c6eb12e 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -50,25 +50,13 @@ class Oai_Model_DefaultServer extends Application_Model_Abstract { use Oai_Model_OptionsTrait; - /** - * Holds xml representation of document information to be processed. - * - * @var DOMDocument Defaults to null. - */ + /** @var DOMDocument Document data. */ protected $xml; - /** - * Holds the stylesheet for the transformation. - * - * @var DOMDocument Defaults to null. - */ + /** @var DOMDocument Transformation stylesheet. */ protected $xslt; - /** - * Holds the xslt processor. - * - * @var XSLTProcessor Defaults to null. - */ + /** @var XSLTProcessor */ protected $proc; /** @var Oai_Model_XmlFactory */ @@ -125,7 +113,7 @@ class Oai_Model_DefaultServer extends Application_Model_Abstract /** @var array */ private $documentTypesAllowed; - /** @var array Holds information about which document state aka server_state are delivered out. */ + /** @var array Document states aka server_states to be delivered out. */ private $documentStatesAllowed = ['published', 'deleted']; // maybe deleted documents too /** @var bool */ From bb48b41c07a553a308e7d0ec335ebf07f7480c2d Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 14:24:52 +0200 Subject: [PATCH 53/59] #766 Added a comment to make things hopefully more clear. --- modules/oai/models/ServerFactory.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index c9b6e71dd..d749c7e83 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -98,6 +98,12 @@ public function create($metaDataPrefix = null) if ($options) { if (isset($options['viewHelpers'])) { + /* + In order to prevent required view helpers (configured in the default configuration or directly + in a derived server class) from being unintentionally removed by configuring a specific format, + they will be always just appended to the existing ones. + */ + $previousViewHelpers = $server->getViewHelpers(); $viewHelpers = $options['viewHelpers']; From 43072dfeb9892ec979efc2d879998384036cd01d Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 14:25:35 +0200 Subject: [PATCH 54/59] #766 Removed unused method. --- modules/oai/models/ServerFactory.php | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index d749c7e83..56672d515 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -139,14 +139,4 @@ public function createByResumptionToken($resumptionToken) return $this->create($token->getMetadataPrefix()); } - - /** - * Gets all configured format prefixes - * - * @return string[] - */ - public function getFormats() - { - return $this->getOaiConfig()->getFormats(); - } } From 1ca259fe37c239424f99b6b45c93fd2a7029d9c8 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 14:26:39 +0200 Subject: [PATCH 55/59] #766 Use class name only ones in the function. --- modules/oai/models/ServerFactory.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 56672d515..31040a2c5 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -83,16 +83,14 @@ public function create($metaDataPrefix = null) $serverClass = $options['class']; } elseif (isset($defaults['class'])) { $serverClass = $defaults['class']; - } else { - $serverClass = Oai_Model_DefaultServer::class; } if (empty($serverClass) || ! ClassLoaderHelper::classExists($serverClass)) { - $server = new Oai_Model_DefaultServer(); - } else { - $server = new $serverClass(); + $serverClass = Oai_Model_DefaultServer::class; } + $server = new $serverClass(); + $server->setOaiConfig($this->getOaiConfig()); $server->initDefaults(); From 01de758baec28bc1365c34e03db46e309c4831f1 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 16:08:44 +0200 Subject: [PATCH 56/59] #766 Moved oai server test classes to oai test. --- tests/Bootstrap.php | 1 + .../oai/models/MockDefaultOaiServer.php} | 2 +- .../oai/models/MockXMetaDissPlusServer.php} | 2 +- tests/modules/oai/models/OaiConfigTest.php | 4 +-- .../modules/oai/models/ServerFactoryTest.php | 16 ++++----- tests/support/OaiDcServer.php | 34 ------------------- 6 files changed, 13 insertions(+), 46 deletions(-) rename tests/{support/DefaultOaiServer.php => modules/oai/models/MockDefaultOaiServer.php} (96%) rename tests/{support/XMetaDissPlusServer.php => modules/oai/models/MockXMetaDissPlusServer.php} (95%) delete mode 100644 tests/support/OaiDcServer.php diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 8a50c34e2..78d7fd340 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -42,6 +42,7 @@ // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, [ + APPLICATION_PATH . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'oai'. DIRECTORY_SEPARATOR . 'models', APPLICATION_PATH . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'support', // Support-Klassen fuer Tests APPLICATION_PATH . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'library', // tests/library APPLICATION_PATH . DIRECTORY_SEPARATOR . 'library', // Server library diff --git a/tests/support/DefaultOaiServer.php b/tests/modules/oai/models/MockDefaultOaiServer.php similarity index 96% rename from tests/support/DefaultOaiServer.php rename to tests/modules/oai/models/MockDefaultOaiServer.php index 130dbf173..c54c6642d 100644 --- a/tests/support/DefaultOaiServer.php +++ b/tests/modules/oai/models/MockDefaultOaiServer.php @@ -29,6 +29,6 @@ * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class DefaultOaiServer extends Oai_Model_DefaultServer +class MockDefaultOaiServer extends Oai_Model_DefaultServer { } diff --git a/tests/support/XMetaDissPlusServer.php b/tests/modules/oai/models/MockXMetaDissPlusServer.php similarity index 95% rename from tests/support/XMetaDissPlusServer.php rename to tests/modules/oai/models/MockXMetaDissPlusServer.php index 6f89b7f46..315c8d562 100644 --- a/tests/support/XMetaDissPlusServer.php +++ b/tests/modules/oai/models/MockXMetaDissPlusServer.php @@ -29,7 +29,7 @@ * @license http://www.gnu.org/licenses/gpl.html General Public License */ -class XMetaDissPlusServer extends Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer +class MockXMetaDissPlusServer extends Oai_Model_Prefix_XMetaDissPlus_XMetaDissPlusServer { protected function initFormatDefaults() { diff --git a/tests/modules/oai/models/OaiConfigTest.php b/tests/modules/oai/models/OaiConfigTest.php index 75d3ce840..978c4830e 100644 --- a/tests/modules/oai/models/OaiConfigTest.php +++ b/tests/modules/oai/models/OaiConfigTest.php @@ -50,7 +50,7 @@ protected function getTestConfiguration() ], 'format' => [ 'default' => [ - 'class' => DefaultOaiServer::class, + 'class' => MockDefaultOaiServer::class, 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', 'xsltFile' => 'oaiFile.xslt', ], @@ -88,7 +88,7 @@ public function testGetDefaults() 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', 'emailContact' => 'opus4ci@example.org', 'xsltFile' => 'oaiFile.xslt', - 'class' => DefaultOaiServer::class, + 'class' => MockDefaultOaiServer::class, 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', ]; diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 396b39fde..9b9eac980 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -53,7 +53,7 @@ protected function getTestConfiguration() ], 'format' => [ 'default' => [ - 'class' => DefaultOaiServer::class, + 'class' => MockDefaultOaiServer::class, 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', 'xsltFile' => 'oaiFile.xslt', 'hasFilesVisibleInOai' => 1, @@ -102,7 +102,7 @@ public function testCreate() $serverFactory = new Oai_Model_ServerFactory(); $serverFactory->setOaiConfig($this->getOaiConfig()); $server = $serverFactory->create(); - $this->assertEquals(DefaultOaiServer::class, get_class($server)); + $this->assertEquals(MockDefaultOaiServer::class, get_class($server)); } public function testCreateWithMetadataPrefix() @@ -118,7 +118,7 @@ public function testCreateWithUnknownMetadataPrefix() $serverFactory = new Oai_Model_ServerFactory(); $serverFactory->setOaiConfig($this->getOaiConfig()); $server = $serverFactory->create('unknownPrefix'); - $this->assertEquals(DefaultOaiServer::class, get_class($server)); + $this->assertEquals(MockDefaultOaiServer::class, get_class($server)); } public function testCreateWithNoFormatConfiguration() @@ -194,7 +194,7 @@ public function testDefaultServerOptionsWithDefaultConfiguration() 'checkEmbargo' => false, ]; - $this->assertEquals(DefaultOaiServer::class, get_class($server)); + $this->assertEquals(MockDefaultOaiServer::class, get_class($server)); $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } @@ -202,7 +202,7 @@ public function testFormatServerClassOverwritesDefaults() { $testConfiguration = $this->getTestConfiguration(); $testConfiguration['oai']['format']['xmetadissplus'] = [ - 'class' => XMetaDissPlusServer::class, + 'class' => MockXMetaDissPlusServer::class, ]; $oaiConfig = $this->getOaiConfig($testConfiguration); $serverFactory = new Oai_Model_ServerFactory(); @@ -227,7 +227,7 @@ public function testFormatServerClassOverwritesDefaults() 'metadataNamespaceUrl' => 'http://www.d-nb.de/standards/xmetadissplus/', ]; - $this->assertEquals(XMetaDissPlusServer::class, get_class($server)); + $this->assertEquals(MockXMetaDissPlusServer::class, get_class($server)); $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } @@ -235,7 +235,7 @@ public function testFormatServerClassOptionsOverwrittenByFormatConfiguration() { $testConfiguration = $this->getTestConfiguration(); $testConfiguration['oai']['format']['xmetadissplus'] = [ - 'class' => XMetaDissPlusServer::class, + 'class' => MockXMetaDissPlusServer::class, 'viewHelpers' => 'additionalViewHelper1, additionalViewHelper2', 'xsltFile' => 'configuredXMetaDissPlus.xslt', 'checkEmbargo' => 0, @@ -263,7 +263,7 @@ public function testFormatServerClassOptionsOverwrittenByFormatConfiguration() 'metadataNamespaceUrl' => 'http://www.d-nb.de/standards/xmetadissplus/', ]; - $this->assertEquals(XMetaDissPlusServer::class, get_class($server)); + $this->assertEquals(MockXMetaDissPlusServer::class, get_class($server)); $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } diff --git a/tests/support/OaiDcServer.php b/tests/support/OaiDcServer.php deleted file mode 100644 index c9b54b5c2..000000000 --- a/tests/support/OaiDcServer.php +++ /dev/null @@ -1,34 +0,0 @@ - Date: Tue, 10 Oct 2023 17:16:08 +0200 Subject: [PATCH 57/59] #766 Updated comment --- modules/oai/models/ServerFactory.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/oai/models/ServerFactory.php b/modules/oai/models/ServerFactory.php index 31040a2c5..776baa712 100644 --- a/modules/oai/models/ServerFactory.php +++ b/modules/oai/models/ServerFactory.php @@ -97,11 +97,10 @@ public function create($metaDataPrefix = null) if ($options) { if (isset($options['viewHelpers'])) { /* - In order to prevent required view helpers (configured in the default configuration or directly - in a derived server class) from being unintentionally removed by configuring a specific format, - they will be always just appended to the existing ones. - */ - + * View helpers configured for a specific format are added to the list of default view helpers used by + * the main OAI-PMH XSLT. The default view helpers cannot be replaced or removed for a specific OAI + * format. + */ $previousViewHelpers = $server->getViewHelpers(); $viewHelpers = $options['viewHelpers']; From 59b25c3021838196d1839a165863dbdd50bf89fc Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 18:24:53 +0200 Subject: [PATCH 58/59] #766 Removed unnecessary mock test class and moved remaining mock test class back to the support folder. --- tests/Bootstrap.php | 1 - .../oai/models/MockDefaultOaiServer.php | 34 ------------------- tests/modules/oai/models/OaiConfigTest.php | 4 +-- .../modules/oai/models/ServerFactoryTest.php | 11 +++--- .../MockXMetaDissPlusServer.php | 0 5 files changed, 7 insertions(+), 43 deletions(-) delete mode 100644 tests/modules/oai/models/MockDefaultOaiServer.php rename tests/{modules/oai/models => support}/MockXMetaDissPlusServer.php (100%) diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index 78d7fd340..8a50c34e2 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -42,7 +42,6 @@ // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, [ - APPLICATION_PATH . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'oai'. DIRECTORY_SEPARATOR . 'models', APPLICATION_PATH . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'support', // Support-Klassen fuer Tests APPLICATION_PATH . DIRECTORY_SEPARATOR . 'tests' . DIRECTORY_SEPARATOR . 'library', // tests/library APPLICATION_PATH . DIRECTORY_SEPARATOR . 'library', // Server library diff --git a/tests/modules/oai/models/MockDefaultOaiServer.php b/tests/modules/oai/models/MockDefaultOaiServer.php deleted file mode 100644 index c54c6642d..000000000 --- a/tests/modules/oai/models/MockDefaultOaiServer.php +++ /dev/null @@ -1,34 +0,0 @@ - [ 'default' => [ - 'class' => MockDefaultOaiServer::class, + 'class' => Oai_Model_Prefix_Oai_OaiDcServer::class, 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', 'xsltFile' => 'oaiFile.xslt', ], @@ -88,7 +88,7 @@ public function testGetDefaults() 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', 'emailContact' => 'opus4ci@example.org', 'xsltFile' => 'oaiFile.xslt', - 'class' => MockDefaultOaiServer::class, + 'class' => Oai_Model_Prefix_Oai_OaiDcServer::class, 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', ]; diff --git a/tests/modules/oai/models/ServerFactoryTest.php b/tests/modules/oai/models/ServerFactoryTest.php index 9b9eac980..c54888598 100644 --- a/tests/modules/oai/models/ServerFactoryTest.php +++ b/tests/modules/oai/models/ServerFactoryTest.php @@ -53,9 +53,8 @@ protected function getTestConfiguration() ], 'format' => [ 'default' => [ - 'class' => MockDefaultOaiServer::class, + 'class' => Oai_Model_Prefix_Oai_OaiDcServer::class, 'viewHelpers' => 'optionValue, fileUrl, frontdoorUrl, transferUrl, dcmiType, dcType, openAireType', - 'xsltFile' => 'oaiFile.xslt', 'hasFilesVisibleInOai' => 1, ], 'copy_xml' => [ @@ -102,7 +101,7 @@ public function testCreate() $serverFactory = new Oai_Model_ServerFactory(); $serverFactory->setOaiConfig($this->getOaiConfig()); $server = $serverFactory->create(); - $this->assertEquals(MockDefaultOaiServer::class, get_class($server)); + $this->assertEquals(Oai_Model_Prefix_Oai_OaiDcServer::class, get_class($server)); } public function testCreateWithMetadataPrefix() @@ -118,7 +117,7 @@ public function testCreateWithUnknownMetadataPrefix() $serverFactory = new Oai_Model_ServerFactory(); $serverFactory->setOaiConfig($this->getOaiConfig()); $server = $serverFactory->create('unknownPrefix'); - $this->assertEquals(MockDefaultOaiServer::class, get_class($server)); + $this->assertEquals(Oai_Model_Prefix_Oai_OaiDcServer::class, get_class($server)); } public function testCreateWithNoFormatConfiguration() @@ -184,7 +183,7 @@ public function testDefaultServerOptionsWithDefaultConfiguration() 'maxListRecords' => 10, 'resumptionTokenPath' => '/vagrant/tests/workspace/tmp/resumption', 'emailContact' => 'opus4ci@example.org', - 'xsltFile' => 'oaiFile.xslt', + 'xsltFile' => 'oai_dc.xslt', 'documentStatesAllowed' => ['published', 'deleted'], 'viewHelpers' => ['optionValue', 'fileUrl', 'frontdoorUrl', 'transferUrl', 'dcmiType', 'dcType', 'openAireType', 'listMetadataFormats'], 'notEmbargoedOn' => false, @@ -194,7 +193,7 @@ public function testDefaultServerOptionsWithDefaultConfiguration() 'checkEmbargo' => false, ]; - $this->assertEquals(MockDefaultOaiServer::class, get_class($server)); + $this->assertEquals(Oai_Model_Prefix_Oai_OaiDcServer::class, get_class($server)); $this->assertEquals($expectedOptions, $server->getOptions(array_keys($expectedOptions))); } diff --git a/tests/modules/oai/models/MockXMetaDissPlusServer.php b/tests/support/MockXMetaDissPlusServer.php similarity index 100% rename from tests/modules/oai/models/MockXMetaDissPlusServer.php rename to tests/support/MockXMetaDissPlusServer.php From 6527e7b4287a13579605fadad95596c558471e79 Mon Sep 17 00:00:00 2001 From: haogatyp Date: Tue, 10 Oct 2023 19:09:21 +0200 Subject: [PATCH 59/59] #766 Added a function to add view helpers to a oai server. --- modules/oai/models/DefaultServer.php | 20 ++++++++++++++++- tests/modules/oai/models/BaseServerTest.php | 24 +++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/modules/oai/models/DefaultServer.php b/modules/oai/models/DefaultServer.php index 50c6eb12e..d534673bc 100644 --- a/modules/oai/models/DefaultServer.php +++ b/modules/oai/models/DefaultServer.php @@ -1143,7 +1143,7 @@ public function getViewHelpers() public function setViewHelpers($viewHelpers) { if (is_string($viewHelpers)) { - $viewHelpers = $viewHelpers !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; + $viewHelpers = trim($viewHelpers) !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; } if (is_array($viewHelpers)) { @@ -1154,6 +1154,24 @@ public function setViewHelpers($viewHelpers) $this->viewHelpers = $viewHelpers; } + /** + * Adds viewHelpers + * + * @param array|string $viewHelpers + */ + public function addViewHelpers($viewHelpers) + { + if (is_string($viewHelpers)) { + $viewHelpers = trim($viewHelpers) !== '' ? array_map('trim', explode(',', $viewHelpers)) : []; + } + + if (is_array($viewHelpers)) { + $previousViewHelpers = $this->viewHelpers ?? []; + $viewHelpers = array_unique(array_merge($previousViewHelpers, $viewHelpers)); + $this->viewHelpers = $viewHelpers; + } + } + /** * Gets the checkEmbargo option. * diff --git a/tests/modules/oai/models/BaseServerTest.php b/tests/modules/oai/models/BaseServerTest.php index 7298b8f1c..988ae6e12 100644 --- a/tests/modules/oai/models/BaseServerTest.php +++ b/tests/modules/oai/models/BaseServerTest.php @@ -105,4 +105,28 @@ public function testSetDocumentTypesAllowedWithEmptyString() $baseServer->setDocumentTypesAllowed(''); $this->assertEquals([], $baseServer->getDocumentTypesAllowed()); } + + public function testAddViewHelpers() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers('viewhelper1, viewhelper2'); + $this->assertEquals(['viewhelper1', 'viewhelper2', 'listMetadataFormats'], $baseServer->getViewHelpers()); + + $baseServer->addViewHelpers('viewhelper3, viewhelper4'); + $this->assertEquals( + ['viewhelper1', 'viewhelper2', 'viewhelper3', 'viewhelper4', 'listMetadataFormats'], + $baseServer->getViewHelpers() + ); + } + + public function testAddViewHelpersWithEmptyString() + { + $baseServer = new Oai_Model_DefaultServer(); + $baseServer->setViewHelpers('viewhelper1, viewhelper2'); + $baseServer->addViewHelpers(''); + $this->assertEquals( + ['viewhelper1', 'viewhelper2', 'listMetadataFormats'], + $baseServer->getViewHelpers() + ); + } }