Skip to content

Commit

Permalink
[MAINTENANCE] Filter array for empty search parameters (#1411)
Browse files Browse the repository at this point in the history
Co-authored-by: Sebastian Meyer <[email protected]>
  • Loading branch information
beatrycze-volk and sebastian-meyer authored Feb 1, 2025
1 parent 70567e6 commit d4590f3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ parameters:
- '#Constant LOG_SEVERITY_ERROR not found\.#'
- '#Constant LOG_SEVERITY_NOTICE not found\.#'
- '#Constant LOG_SEVERITY_WARNING not found\.#'
- '#^Parameter \#2 \$callback of function array_filter expects \(callable\(mixed\)\: bool\)\|null, ''strlen'' given\.$#'
level: 5
paths:
- ../Classes/
Expand Down
9 changes: 4 additions & 5 deletions Classes/Controller/ListViewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ public function injectMetadataRepository(MetadataRepository $metadataRepository)
public function mainAction(): ResponseInterface
{
$this->searchParams = $this->getParametersSafely('searchParameter');
$this->searchParams = is_array($this->searchParams) ? array_filter($this->searchParams, 'strlen') : [];

// extract collection(s) from collection parameter
$collections = [];
if (is_array($this->searchParams) && array_key_exists('collection', $this->searchParams)) {
if (array_key_exists('collection', $this->searchParams)) {
foreach(explode(',', $this->searchParams['collection']) as $collectionEntry) {
if (!empty($collectionEntry)) {
$collections[] = $this->collectionRepository->findByUid((int) $collectionEntry);
}
$collections[] = $this->collectionRepository->findByUid((int) $collectionEntry);
}
}

Expand All @@ -104,7 +103,7 @@ public function mainAction(): ResponseInterface

$solrResults = null;
$numResults = 0;
if (is_array($this->searchParams) && !empty($this->searchParams)) {
if (!empty($this->searchParams)) {
$solrResults = $this->documentRepository->findSolrByCollections($collections, $this->settings, $this->searchParams, $listedMetadata, $indexedMetadata);
$numResults = $solrResults->getNumFound();

Expand Down
17 changes: 10 additions & 7 deletions Classes/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public function mainAction(): ResponseInterface

// if search was triggered, get search parameters from POST variables
$this->searchParams = $this->getParametersSafely('searchParameter');

// if search was triggered by the ListView plugin, get the parameters from GET variables
// replace with $this->request->getQueryParams() when dropping support for Typo3 v11, see Deprecation-100596
$listRequestData = GeneralUtility::_GPmerged('tx_dlf_listview');
Expand All @@ -130,13 +131,15 @@ public function mainAction(): ResponseInterface
$GLOBALS['TSFE']->fe_user->setKey('ses', 'search', $this->searchParams);
}

$this->searchParams = is_array($this->searchParams) ? array_filter($this->searchParams, 'strlen') : [];

// sanitize date search input
if (!empty($this->searchParams['dateFrom']) || !empty($this->searchParams['dateTo'])) {
if (empty($this->searchParams['dateFrom']) && !empty($this->searchParams['dateTo'])) {
if (array_key_exists('dateFrom', $this->searchParams) || array_key_exists('dateTo', $this->searchParams)) {
if (!array_key_exists('dateFrom', $this->searchParams) && array_key_exists('dateTo', $this->searchParams)) {
$this->searchParams['dateFrom'] = '*';
}

if (empty($this->searchParams['dateTo']) && !empty($this->searchParams['dateFrom'])) {
if (!array_key_exists('dateTo', $this->searchParams) && array_key_exists('dateFrom', $this->searchParams)) {
$this->searchParams['dateTo'] = 'NOW';
}

Expand Down Expand Up @@ -168,7 +171,7 @@ public function mainAction(): ResponseInterface

// If no search has been executed, no variables have to be prepared.
// An empty form will be shown.
if (is_array($this->searchParams) && !empty($this->searchParams)) {
if (!empty($this->searchParams)) {
// get all sortable metadata records
$sortableMetadata = $this->metadataRepository->findByIsSortable(true);

Expand Down Expand Up @@ -276,7 +279,7 @@ public function makeFacetsMenuArray(array $facets): array
// Set search query.
$searchParams = $this->searchParams;
if (
(!empty($searchParams['fulltext']))
(array_key_exists('fulltext', $searchParams))
|| preg_match('/' . $fields['fulltext'] . ':\((.*)\)/', trim($searchParams['query']), $matches)
) {
// If the query already is a fulltext query e.g using the facets
Expand All @@ -298,7 +301,7 @@ public function makeFacetsMenuArray(array $facets): array
}

// add filter query for date search
if (!empty($this->searchParams['dateFrom']) && !empty($this->searchParams['dateTo'])) {
if (array_key_exists('dateFrom', $this->searchParams) && array_key_exists('dateTo', $this->searchParams)) {
// combine dateFrom and dateTo into filterquery as range search
$search['params']['filterquery'][]['query'] = '{!join from=' . $fields['uid'] . ' to=' . $fields['uid'] . '}' . $fields['date'] . ':[' . $this->searchParams['dateFrom'] . ' TO ' . $this->searchParams['dateTo'] . ']';
}
Expand Down Expand Up @@ -326,7 +329,7 @@ public function makeFacetsMenuArray(array $facets): array
}
}

if (isset($this->searchParams['fq']) && is_array($this->searchParams['fq'])) {
if (array_key_exists('fq', $this->searchParams) && is_array($this->searchParams['fq'])) {
foreach ($this->searchParams['fq'] as $fq) {
$search['params']['filterquery'][]['query'] = $fq;
}
Expand Down

0 comments on commit d4590f3

Please sign in to comment.