Passed
Push — master ( 29ec4f...169eb0 )
by
unknown
17:16
created

SearchTermRestriction   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 55
c 0
b 0
f 0
dl 0
loc 121
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildExpression() 0 11 3
A extractSearchableFieldsFromTable() 0 21 6
B makeQuerySearchByTable() 0 54 11
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Core\Resource\Search\QueryRestrictions;
19
20
use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
21
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
22
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
23
use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionInterface;
24
use TYPO3\CMS\Core\Resource\Search\FileSearchDemand;
25
use TYPO3\CMS\Core\Utility\GeneralUtility;
26
27
/**
28
 * Filters result by a given search term, respecting search fields defined in search demand or in TCA.
29
 */
30
class SearchTermRestriction implements QueryRestrictionInterface
31
{
32
    /**
33
     * @var FileSearchDemand
34
     */
35
    private $searchDemand;
36
37
    /**
38
     * @var QueryBuilder
39
     */
40
    private $queryBuilder;
41
42
    public function __construct(FileSearchDemand $searchDemand, QueryBuilder $queryBuilder)
43
    {
44
        $this->searchDemand = $searchDemand;
45
        $this->queryBuilder = $queryBuilder;
46
    }
47
48
    public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression
49
    {
50
        $constraints = [];
51
        foreach ($queriedTables as $tableAlias => $tableName) {
52
            if (!in_array($tableName, ['sys_file', 'sys_file_metadata'])) {
53
                continue;
54
            }
55
            $constraints[] = $this->makeQuerySearchByTable($tableName, $tableAlias);
56
        }
57
58
        return $expressionBuilder->orX(...$constraints);
59
    }
60
61
    /**
62
     * Build the MySql where clause by table.
63
     *
64
     * @param string $tableName Record table name
65
     * @param string $tableAlias
66
     * @return CompositeExpression
67
     */
68
    private function makeQuerySearchByTable(string $tableName, string $tableAlias): CompositeExpression
69
    {
70
        $fieldsToSearchWithin = $this->extractSearchableFieldsFromTable($tableName);
71
        $searchTerm = (string)$this->searchDemand->getSearchTerm();
72
        $constraints = [];
73
74
        $searchTermParts = str_getcsv($searchTerm, ' ');
75
        foreach ($searchTermParts as $searchTermPart) {
76
            $searchTermPart = trim($searchTermPart);
77
            if ($searchTermPart === '') {
78
                continue;
79
            }
80
            $constraintsForParts = [];
81
            $like = '%' . $this->queryBuilder->escapeLikeWildcards($searchTermPart) . '%';
82
            foreach ($fieldsToSearchWithin as $fieldName) {
83
                if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName])) {
84
                    continue;
85
                }
86
                $fieldConfig = $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'];
87
                $fieldType = $fieldConfig['type'];
88
                $evalRules = $fieldConfig['eval'] ?? '';
89
90
                // Check whether search should be case-sensitive or not
91
                if (in_array('case', (array)($fieldConfig['search'] ?? []), true)) {
92
                    // case sensitive
93
                    $searchConstraint = $this->queryBuilder->expr()->andX(
94
                        $this->queryBuilder->expr()->like(
95
                            $tableAlias . '.' . $fieldName,
96
                            $this->queryBuilder->createNamedParameter($like, \PDO::PARAM_STR)
97
                        )
98
                    );
99
                } else {
100
                    $searchConstraint = $this->queryBuilder->expr()->andX(
101
                    // case insensitive
102
                        $this->queryBuilder->expr()->comparison(
103
                            'LOWER(' . $this->queryBuilder->quoteIdentifier($tableAlias . '.' . $fieldName) . ')',
104
                            'LIKE',
105
                            $this->queryBuilder->createNamedParameter(mb_strtolower($like), \PDO::PARAM_STR)
106
                        )
107
                    );
108
                }
109
110
                // Assemble the search condition only if the field makes sense to be searched
111
                if ($fieldType === 'text'
112
                    || $fieldType === 'flex'
113
                    || ($fieldType === 'input' && (!$evalRules || !preg_match('/\b(?:date|time|int)\b/', $evalRules)))
114
                ) {
115
                    $constraintsForParts[] = $searchConstraint;
116
                }
117
            }
118
            $constraints[] = $this->queryBuilder->expr()->orX(...$constraintsForParts);
119
        }
120
121
        return $this->queryBuilder->expr()->andX(...$constraints);
122
    }
123
124
    /**
125
     * Get all fields from given table where we can search for.
126
     *
127
     * @param string $tableName Name of the table for which to get the searchable fields
128
     * @return array
129
     */
130
    private function extractSearchableFieldsFromTable(string $tableName): array
131
    {
132
        if ($searchFields = $this->searchDemand->getSearchFields()) {
133
            if (empty($searchFields[$tableName])) {
134
                return [];
135
            }
136
            foreach ($searchFields[$tableName] as $searchField) {
137
                if (!isset($GLOBALS['TCA'][$tableName]['columns'][$searchField])) {
138
                    throw new \RuntimeException(sprintf('Cannot use search field "%s" because it is not defined in TCA.', $searchField), 1556367071);
139
                }
140
            }
141
142
            return $searchFields;
143
        }
144
        $fieldListArray = [];
145
        // Get the list of fields to search in from the TCA, if any
146
        if (isset($GLOBALS['TCA'][$tableName]['ctrl']['searchFields'])) {
147
            $fieldListArray = GeneralUtility::trimExplode(',', $GLOBALS['TCA'][$tableName]['ctrl']['searchFields'], true);
148
        }
149
150
        return $fieldListArray;
151
    }
152
}
153