Skip to content

Commit

Permalink
Merge branch 'master' into entryarray
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-meyer authored Jul 5, 2024
2 parents 9439624 + 1657409 commit f6f2915
Show file tree
Hide file tree
Showing 20 changed files with 1,294 additions and 1,474 deletions.
1 change: 1 addition & 0 deletions .github/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ parameters:
ignoreErrors:
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::countByPid\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findByIsListed\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findByIndexIndexed\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findByIsSortable\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findOneByFeUserId\(\)\.#'
- '#Call to an undefined method Kitodo\\Dlf\\Domain\\Repository\\[a-zA-Z]+Repository::findOneByIndexName\(\)\.#'
Expand Down
10 changes: 5 additions & 5 deletions Classes/Command/HarvestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
// Abort if solrCoreUid is empty or not in the array of allowed solr cores.
if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
$output_solrCores = [];
foreach ($allSolrCores as $index_name => $uid) {
$output_solrCores[] = $uid . ' : ' . $index_name;
$outputSolrCores = [];
foreach ($allSolrCores as $indexName => $uid) {
$outputSolrCores[] = $uid . ' : ' . $indexName;
}
if (empty($output_solrCores)) {
if (empty($outputSolrCores)) {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n");
return BaseCommand::FAILURE;
} else {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $output_solrCores) . "\n");
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n");
return BaseCommand::FAILURE;
}
}
Expand Down
10 changes: 5 additions & 5 deletions Classes/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Abort if solrCoreUid is empty or not in the array of allowed solr cores.
if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
$output_solrCores = [];
foreach ($allSolrCores as $index_name => $uid) {
$output_solrCores[] = $uid . ' : ' . $index_name;
$outputSolrCores = [];
foreach ($allSolrCores as $indexName => $uid) {
$outputSolrCores[] = $uid . ' : ' . $indexName;
}
if (empty($output_solrCores)) {
if (empty($outputSolrCores)) {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n");
return BaseCommand::FAILURE;
} else {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $output_solrCores) . "\n");
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n");
return BaseCommand::FAILURE;
}
}
Expand Down
10 changes: 5 additions & 5 deletions Classes/Command/ReindexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

// Abort if solrCoreUid is empty or not in the array of allowed solr cores.
if (empty($solrCoreUid) || !in_array($solrCoreUid, $allSolrCores)) {
$output_solrCores = [];
foreach ($allSolrCores as $index_name => $uid) {
$output_solrCores[] = $uid . ' : ' . $index_name;
$outputSolrCores = [];
foreach ($allSolrCores as $indexName => $uid) {
$outputSolrCores[] = $uid . ' : ' . $indexName;
}
if (empty($output_solrCores)) {
if (empty($outputSolrCores)) {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. No valid cores found on PID ' . $this->storagePid . ".\n");
return BaseCommand::FAILURE;
} else {
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $output_solrCores) . "\n");
$io->error('ERROR: No valid Solr core ("' . $input->getOption('solr') . '") given. ' . "Valid cores are (<uid>:<index_name>):\n" . implode("\n", $outputSolrCores) . "\n");
return BaseCommand::FAILURE;
}
}
Expand Down
185 changes: 52 additions & 133 deletions Classes/Common/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Kitodo\Dlf\Common;

use TYPO3\CMS\Core\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Http\Uri;
Expand Down Expand Up @@ -207,7 +208,7 @@ public static function decrypt(string $encrypted)
self::log('OpenSSL library doesn\'t support cipher and/or hash algorithm', LOG_SEVERITY_ERROR);
return false;
}
if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
if (empty(self::getEncryptionKey())) {
self::log('No encryption key set in TYPO3 configuration', LOG_SEVERITY_ERROR);
return false;
}
Expand All @@ -222,7 +223,7 @@ public static function decrypt(string $encrypted)
$binary = base64_decode($encrypted);
$iv = substr($binary, 0, openssl_cipher_iv_length(self::$cipherAlgorithm));
$data = substr($binary, openssl_cipher_iv_length(self::$cipherAlgorithm));
$key = openssl_digest($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], self::$hashAlgorithm, true);
$key = openssl_digest(self::getEncryptionKey(), self::$hashAlgorithm, true);
// Decrypt data.
return openssl_decrypt($data, self::$cipherAlgorithm, $key, OPENSSL_RAW_DATA, $iv);
}
Expand Down Expand Up @@ -342,13 +343,13 @@ public static function encrypt(string $string)
self::log('OpenSSL library doesn\'t support cipher and/or hash algorithm', LOG_SEVERITY_ERROR);
return false;
}
if (empty($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'])) {
if (empty(self::getEncryptionKey())) {
self::log('No encryption key set in TYPO3 configuration', LOG_SEVERITY_ERROR);
return false;
}
// Generate random initialization vector.
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length(self::$cipherAlgorithm));
$key = openssl_digest($GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'], self::$hashAlgorithm, true);
$key = openssl_digest(self::getEncryptionKey(), self::$hashAlgorithm, true);
// Encrypt data.
$encrypted = openssl_encrypt($string, self::$cipherAlgorithm, $key, OPENSSL_RAW_DATA, $iv);
// Merge initialization vector and encrypted data.
Expand All @@ -358,23 +359,6 @@ public static function encrypt(string $string)
return $encrypted;
}

/**
* Get the unqualified name of a class
*
* @access public
*
* @static
*
* @param string $qualifiedClassName The qualified class name from get_class()
*
* @return string The unqualified class name
*/
public static function getUnqualifiedClassName(string $qualifiedClassName): string
{
$nameParts = explode('\\', $qualifiedClassName);
return end($nameParts);
}

/**
* Clean up a string to use in an URL.
*
Expand Down Expand Up @@ -412,8 +396,8 @@ public static function getCleanString(string $string): string
public static function getHookObjects(string $scriptRelPath): array
{
$hookObjects = [];
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::$extKey . '/' . $scriptRelPath]['hookClass'])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][self::$extKey . '/' . $scriptRelPath]['hookClass'] as $classRef) {
if (is_array(self::getOptions()[self::$extKey . '/' . $scriptRelPath]['hookClass'])) {
foreach (self::getOptions()[self::$extKey . '/' . $scriptRelPath]['hookClass'] as $classRef) {
$hookObjects[] = GeneralUtility::makeInstance($classRef);
}
}
Expand Down Expand Up @@ -556,94 +540,6 @@ public static function getDocumentStructures(int $pid = -1): array
return array_column($allStructures, 'indexName', 'uid');
}

/**
* Get the URN of an object
* @see http://www.persistent-identifier.de/?link=316
*
* @access public
*
* @static
*
* @param string $base The namespace and base URN
* @param string $id The object's identifier
*
* @return string Uniform Resource Name as string
*/
public static function getURN(string $base, string $id): string
{
$concordance = [
'0' => 1,
'1' => 2,
'2' => 3,
'3' => 4,
'4' => 5,
'5' => 6,
'6' => 7,
'7' => 8,
'8' => 9,
'9' => 41,
'a' => 18,
'b' => 14,
'c' => 19,
'd' => 15,
'e' => 16,
'f' => 21,
'g' => 22,
'h' => 23,
'i' => 24,
'j' => 25,
'k' => 42,
'l' => 26,
'm' => 27,
'n' => 13,
'o' => 28,
'p' => 29,
'q' => 31,
'r' => 12,
's' => 32,
't' => 33,
'u' => 11,
'v' => 34,
'w' => 35,
'x' => 36,
'y' => 37,
'z' => 38,
'-' => 39,
':' => 17,
];
$urn = strtolower($base . $id);
if (preg_match('/[^a-z\d:-]/', $urn)) {
self::log('Invalid chars in given parameters', LOG_SEVERITY_WARNING);
return '';
}
$digits = '';
for ($i = 0, $j = strlen($urn); $i < $j; $i++) {
$digits .= $concordance[substr($urn, $i, 1)];
}
$checksum = 0;
for ($i = 0, $j = strlen($digits); $i < $j; $i++) {
$checksum += ($i + 1) * (int) substr($digits, $i, 1);
}
$checksum = substr((string) floor($checksum / (int) substr($digits, -1, 1)), -1, 1);
return $base . $id . $checksum;
}

/**
* Check if given ID is a valid Pica Production Number (PPN)
*
* @access public
*
* @static
*
* @param string $id The identifier to check
*
* @return bool Is $id a valid PPN?
*/
public static function isPPN(string $id): bool
{
return self::checkIdentifier($id, 'PPN');
}

/**
* Determine whether or not $url is a valid URL using HTTP or HTTPS scheme.
*
Expand All @@ -670,28 +566,6 @@ public static function isValidHttpUrl(string $url): bool
}
}

/**
* Merges two arrays recursively and actually returns the modified array.
* @see ArrayUtility::mergeRecursiveWithOverrule()
*
* @access public
*
* @static
*
* @param array $original Original array
* @param array $overrule Overrule array, overruling the original array
* @param bool $addKeys If set to false, keys that are not found in $original will not be set
* @param bool $includeEmptyValues If set, values from $overrule will overrule if they are empty
* @param bool $enableUnsetFeature If set, special value "__UNSET" can be used in the overrule array to unset keys in the original array
*
* @return array Merged array
*/
public static function mergeRecursiveWithOverrule(array $original, array $overrule, bool $addKeys = true, bool $includeEmptyValues = true, bool $enableUnsetFeature = true): array
{
ArrayUtility::mergeRecursiveWithOverrule($original, $overrule, $addKeys, $includeEmptyValues, $enableUnsetFeature);
return $original;
}

/**
* Process a data and/or command map with TYPO3 core engine as admin.
*
Expand Down Expand Up @@ -1018,4 +892,49 @@ public static function isValidXmlId($id): bool
{
return preg_match('/^[_a-z][_a-z0-9-.]*$/i', $id) === 1;
}

/**
* Get options from local configuration.
*
* @access private
*
* @static
*
* @return array
*/
private static function getOptions(): array
{
return self::getLocalConfigurationByPath('SC_OPTIONS');
}

/**
* Get encryption key from local configuration.
*
* @access private
*
* @static
*
* @return string|null
*/
private static function getEncryptionKey(): ?string
{
return self::getLocalConfigurationByPath('SYS/encryptionKey');
}

/**
* Get local configuration for given path.
*
* @access private
*
* @static
*
* @param string $path
*
* @return mixed
*/
private static function getLocalConfigurationByPath(string $path)
{
$configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
return $configurationManager->getLocalConfigurationValueByPath($path);
}
}
18 changes: 6 additions & 12 deletions Classes/Common/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class Indexer
'sortables' => [],
'indexed' => [],
'stored' => [],
'tokenized' => [],
'fieldboost' => []
'tokenized' => []
];

/**
Expand Down Expand Up @@ -305,11 +304,6 @@ protected static function loadIndexConf(int $pid): void
if ($indexing['index_autocomplete']) {
self::$fields['autocomplete'][] = $indexing['index_name'];
}
if ($indexing['index_boost'] > 0.0) {
self::$fields['fieldboost'][$indexing['index_name']] = floatval($indexing['index_boost']);
} else {
self::$fields['fieldboost'][$indexing['index_name']] = false;
}
}
self::$fieldsLoaded = true;
}
Expand Down Expand Up @@ -357,8 +351,8 @@ protected static function processLogical(Document $document, array $logicalUnit)
}
// There can be only one toplevel unit per UID, independently of backend configuration
$solrDoc->setField('toplevel', $logicalUnit['id'] == $doc->toplevelId ? true : false);
$solrDoc->setField('title', $metadata['title'][0], self::$fields['fieldboost']['title']);
$solrDoc->setField('volume', $metadata['volume'][0], self::$fields['fieldboost']['volume']);
$solrDoc->setField('title', $metadata['title'][0]);
$solrDoc->setField('volume', $metadata['volume'][0]);
// verify date formatting
if(strtotime($metadata['date'][0])) {
$solrDoc->setField('date', self::getFormattedDate($metadata['date'][0]));
Expand Down Expand Up @@ -445,7 +439,7 @@ protected static function processPhysical(Document $document, int $page, array $
}
}
$solrDoc->setField('toplevel', false);
$solrDoc->setField('type', $physicalUnit['type'], self::$fields['fieldboost']['type']);
$solrDoc->setField('type', $physicalUnit['type']);
$solrDoc->setField('collection', $doc->metadataArray[$doc->toplevelId]['collection']);
$solrDoc->setField('location', $document->getLocation());

Expand Down Expand Up @@ -520,7 +514,7 @@ private static function processMetadata($document, $metadata, &$solrDoc): array
!empty($data)
&& substr($indexName, -8) !== '_sorting'
) {
$solrDoc->setField(self::getIndexFieldName($indexName, $document->getPid()), $data, self::$fields['fieldboost'][$indexName]);
$solrDoc->setField(self::getIndexFieldName($indexName, $document->getPid()), $data);
if (in_array($indexName, self::$fields['sortables'])) {
// Add sortable fields to index.
$solrDoc->setField($indexName . '_sorting', $metadata[$indexName . '_sorting'][0]);
Expand Down Expand Up @@ -626,7 +620,7 @@ private static function getSolrDocument(Query $updateQuery, Document $document,
$solrDoc->setField('partof', $document->getPartof());
$solrDoc->setField('root', $document->getCurrentDocument()->rootId);
$solrDoc->setField('sid', $unit['id']);
$solrDoc->setField('type', $unit['type'], self::$fields['fieldboost']['type']);
$solrDoc->setField('type', $unit['type']);
$solrDoc->setField('collection', $document->getCurrentDocument()->metadataArray[$document->getCurrentDocument()->toplevelId]['collection']);
$solrDoc->setField('fulltext', $fullText);
return $solrDoc;
Expand Down
Loading

0 comments on commit f6f2915

Please sign in to comment.