Skip to content

Commit

Permalink
[MAINTENANCE] Sanitize settings from FlexForm (#1324)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
  • Loading branch information
beatrycze-volk and sebastian-meyer committed Sep 20, 2024
1 parent a156360 commit 6307228
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 29 deletions.
51 changes: 50 additions & 1 deletion Classes/Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,55 @@ protected function sanitizeRequestData(): void
$this->requestData['double'] = MathUtility::forceIntegerInRange($this->requestData['double'] ?? 0, 0, 1);
}

/**
* Sanitize settings from FlexForm.
*
* @access protected
*
* @return void
*/
protected function sanitizeSettings(): void
{
$this->setDefaultIntSetting('storagePid', 0);

if ($this instanceof MetadataController) {
$this->setDefaultIntSetting('rootline', 0);
$this->setDefaultIntSetting('originalIiifMetadata', 0);
$this->setDefaultIntSetting('displayIiifDescription', 1);
$this->setDefaultIntSetting('displayIiifRights', 1);
$this->setDefaultIntSetting('displayIiifLinks', 1);
}

if ($this instanceof OaiPmhController) {
$this->setDefaultIntSetting('limit', 5);
$this->setDefaultIntSetting('solr_limit', 50000);
}

if ($this instanceof PageViewController) {
$this->setDefaultIntSetting('useInternalProxy', 0);
}
}

/**
* Sets default value for setting if not yet set.
*
* @access protected
*
* @param string $setting name of setting
* @param int $value for being set if empty
*
* @return void
*/
protected function setDefaultIntSetting(string $setting, int $value): void
{
if (empty($this->settings[$setting])) {
$this->settings[$setting] = $value;
$this->logger->warning('Setting "' . $setting . '" not set, using default value "' . $value . '". Probably FlexForm for controller "' . get_class($this) . '" is not read.');
} else {
$this->settings[$setting] = (int) $this->settings[$setting];
}
}

/**
* Sets page value.
*
Expand Down Expand Up @@ -492,7 +541,7 @@ private function getDocumentByUrl(string $documentId)

// Make sure configuration PID is set when applicable
if ($doc->cPid == 0) {
$doc->cPid = max((int) $this->settings['storagePid'], 0);
$doc->cPid = max($this->settings['storagePid'], 0);
}

$this->document->setLocation($documentId);
Expand Down
27 changes: 2 additions & 25 deletions Classes/Controller/MetadataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,10 @@ public function mainAction(): void
if ($this->isDocMissing()) {
// Quit without doing anything if required variables are not set.
return;
} else {
// Set default values if not set.
$this->setDefault('rootline', 0);
$this->setDefault('originalIiifMetadata', 0);
$this->setDefault('displayIiifDescription', 1);
$this->setDefault('displayIiifRights', 1);
$this->setDefault('displayIiifLinks', 1);
$this->setPage();
}

$this->setPage();

$this->currentDocument = $this->document->getCurrentDocument();
$this->useOriginalIiifManifestMetadata = $this->settings['originalIiifMetadata'] == 1 && $this->currentDocument instanceof IiifManifest;

Expand Down Expand Up @@ -518,21 +512,4 @@ private function getMetadataForIds(array $id, array $metadata): array
}
return $metadata;
}

/**
* Sets default value for setting if not yet set.
*
* @access private
*
* @param string $setting name of setting
* @param int $value 0 or 1
*
* @return void
*/
private function setDefault(string $setting, int $value): void
{
if (!isset($this->settings[$setting])) {
$this->settings[$setting] = $value;
}
}
}
6 changes: 3 additions & 3 deletions Classes/Controller/OaiPmhController.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,8 +645,8 @@ protected function fetchDocumentSet(): array
$this->logger->error('Apache Solr not available');
return $documentSet;
}
if ((int) $this->settings['solr_limit'] > 0) {
$solr->limit = (int) $this->settings['solr_limit'];
if ($this->settings['solr_limit'] > 0) {
$solr->limit = $this->settings['solr_limit'];
}
// We only care about the UID in the results and want them sorted
$parameters = [
Expand Down Expand Up @@ -785,7 +785,7 @@ private function checkGranularity(): void
*/
protected function generateOutputForDocumentList(array $documentListSet)
{
$documentsToProcess = array_splice($documentListSet['elements'], 0, (int) $this->settings['limit']);
$documentsToProcess = array_splice($documentListSet['elements'], 0, $this->settings['limit']);
if (empty($documentsToProcess)) {
$this->error = 'noRecordsMatch';
return [];
Expand Down

0 comments on commit 6307228

Please sign in to comment.