dkd-kaehm /
ext-solr
| 1 | <?php |
||
| 2 | namespace ApacheSolrForTypo3\Solr\Query\Modifier; |
||
| 3 | |||
| 4 | /*************************************************************** |
||
| 5 | * Copyright notice |
||
| 6 | * |
||
| 7 | * (c) 2009-2015 Ingo Renner <[email protected]> |
||
| 8 | * All rights reserved |
||
| 9 | * |
||
| 10 | * This script is part of the TYPO3 project. The TYPO3 project is |
||
| 11 | * free software; you can redistribute it and/or modify |
||
| 12 | * it under the terms of the GNU General Public License as published by |
||
| 13 | * the Free Software Foundation; either version 3 of the License, or |
||
| 14 | * (at your option) any later version. |
||
| 15 | * |
||
| 16 | * The GNU General Public License can be found at |
||
| 17 | * http://www.gnu.org/copyleft/gpl.html. |
||
| 18 | * |
||
| 19 | * This script is distributed in the hope that it will be useful, |
||
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 22 | * GNU General Public License for more details. |
||
| 23 | * |
||
| 24 | * This copyright notice MUST APPEAR in all copies of the script! |
||
| 25 | ***************************************************************/ |
||
| 26 | |||
| 27 | use ApacheSolrForTypo3\Solr\Domain\Search\Query\ParameterBuilder\Faceting as FacetingBuilder; |
||
| 28 | use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder; |
||
| 29 | use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query; |
||
| 30 | use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\FacetRegistry; |
||
| 31 | use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest; |
||
| 32 | use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequestAware; |
||
| 33 | use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
||
| 34 | use TYPO3\CMS\Core\Utility\GeneralUtility; |
||
| 35 | use TYPO3\CMS\Extbase\Object\ObjectManager; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Modifies a query to add faceting parameters |
||
| 39 | * |
||
| 40 | * @author Ingo Renner <[email protected]> |
||
| 41 | * @author Daniel Poetzinger <[email protected]> |
||
| 42 | * @author Sebastian Kurfuerst <[email protected]> |
||
| 43 | */ |
||
| 44 | class Faceting implements Modifier, SearchRequestAware |
||
| 45 | { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var FacetRegistry |
||
| 49 | */ |
||
| 50 | protected $facetRegistry = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var SearchRequest |
||
| 54 | */ |
||
| 55 | protected $searchRequest; |
||
| 56 | |||
| 57 | /** |
||
| 58 | 41 | * @param FacetRegistry $facetRegistry |
|
| 59 | */ |
||
| 60 | 41 | public function __construct(FacetRegistry $facetRegistry) |
|
| 61 | 41 | { |
|
| 62 | 41 | $this->facetRegistry = $facetRegistry; |
|
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param SearchRequest $searchRequest |
||
| 67 | 41 | */ |
|
| 68 | public function setSearchRequest(SearchRequest $searchRequest) |
||
| 69 | 41 | { |
|
| 70 | 41 | $this->searchRequest = $searchRequest; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Modifies the given query and adds the parameters necessary for faceted |
||
| 75 | * search. |
||
| 76 | * |
||
| 77 | * @param Query $query The query to modify |
||
| 78 | * @return Query The modified query with faceting parameters |
||
| 79 | 41 | */ |
|
| 80 | public function modifyQuery(Query $query) |
||
| 81 | 41 | { |
|
| 82 | 41 | $typoScriptConfiguration = $this->searchRequest->getContextTypoScriptConfiguration(); |
|
| 83 | 41 | $faceting = FacetingBuilder::fromTypoScriptConfiguration($typoScriptConfiguration); |
|
| 84 | |||
| 85 | 41 | $allFacets = $typoScriptConfiguration->getSearchFacetingFacets(); |
|
| 86 | 41 | $facetParameters = $this->buildFacetingParameters($allFacets, $typoScriptConfiguration); |
|
| 87 | 41 | foreach ($facetParameters as $facetParameter => $value) { |
|
| 88 | 32 | if(strtolower($facetParameter) === 'facet.field') { |
|
| 89 | $faceting->setFields($value); |
||
| 90 | 41 | } else { |
|
| 91 | $faceting->addAdditionalParameter($facetParameter, $value); |
||
| 92 | } |
||
| 93 | } |
||
| 94 | 41 | ||
| 95 | 41 | $searchArguments = $this->searchRequest->getArguments(); |
|
| 96 | if (!is_array($searchArguments)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 97 | return $query; |
||
| 98 | } |
||
| 99 | 41 | ||
| 100 | 41 | $keepAllFacetsOnSelection = $typoScriptConfiguration->getSearchFacetingKeepAllFacetsOnSelection(); |
|
| 101 | $facetFilters = $this->addFacetQueryFilters($searchArguments, $allFacets, $keepAllFacetsOnSelection); |
||
| 102 | 41 | ||
| 103 | 7 | $queryBuilder = new QueryBuilder($typoScriptConfiguration); |
|
| 104 | $queryBuilder->startFrom($query)->useFaceting($faceting)->useFilterArray($facetFilters); |
||
| 105 | return $query; |
||
| 106 | 41 | } |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Delegates the parameter building to specialized functions depending on |
||
| 110 | * the type of facet to add. |
||
| 111 | * |
||
| 112 | */ |
||
| 113 | protected function buildFacetingParameters($allFacets, TypoScriptConfiguration $typoScriptConfiguration) |
||
| 114 | 41 | { |
|
| 115 | $facetParameters = []; |
||
| 116 | 41 | ||
| 117 | foreach ($allFacets as $facetName => $facetConfiguration) { |
||
| 118 | 41 | $facetName = substr($facetName, 0, -1); |
|
| 119 | 41 | $type = isset($facetConfiguration['type']) ? $facetConfiguration['type'] : 'options'; |
|
| 120 | 41 | $facetParameterBuilder = $this->facetRegistry->getPackage($type)->getQueryBuilder(); |
|
| 121 | 41 | ||
| 122 | if (is_null($facetParameterBuilder)) { |
||
| 123 | 41 | throw new \InvalidArgumentException('No query build configured for facet ' . htmlspecialchars($facetName)); |
|
| 124 | } |
||
| 125 | |||
| 126 | $facetParameters = array_merge_recursive($facetParameters, $facetParameterBuilder->build($facetName, $typoScriptConfiguration)); |
||
| 127 | 41 | } |
|
| 128 | |||
| 129 | return $facetParameters; |
||
| 130 | 41 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Adds filters specified through HTTP GET as filter query parameters to |
||
| 134 | * the Solr query. |
||
| 135 | * |
||
| 136 | * @param array $resultParameters |
||
| 137 | * @param array $allFacets |
||
| 138 | * @param bool $keepAllFacetsOnSelection |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | protected function addFacetQueryFilters($resultParameters, $allFacets, $keepAllFacetsOnSelection) |
||
| 142 | 41 | { |
|
| 143 | $facetFilters = []; |
||
| 144 | 41 | ||
| 145 | if (!is_array($resultParameters['filter'])) { |
||
| 146 | 41 | return $facetFilters; |
|
| 147 | 34 | } |
|
| 148 | |||
| 149 | $filtersByFacetName = $this->getFiltersByFacetName($resultParameters, $allFacets); |
||
| 150 | 7 | ||
| 151 | foreach ($filtersByFacetName as $facetName => $filterValues) { |
||
| 152 | 7 | $facetConfiguration = $allFacets[$facetName . '.']; |
|
| 153 | 7 | $tag = $this->getFilterTag($facetConfiguration, $keepAllFacetsOnSelection); |
|
| 154 | 7 | $filterParts = $this->getFilterParts($facetConfiguration, $facetName, $filterValues); |
|
| 155 | 7 | $operator = ($facetConfiguration['operator'] === 'OR') ? ' OR ' : ' AND '; |
|
| 156 | 7 | $facetFilters[$facetName] = $tag . '(' . implode($operator, $filterParts) . ')'; |
|
| 157 | 7 | } |
|
| 158 | |||
| 159 | return $facetFilters; |
||
| 160 | 7 | } |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Builds the tag part of the query depending on the keepAllOptionsOnSelection configuration or the global configuration |
||
| 164 | * keepAllFacetsOnSelection. |
||
| 165 | * |
||
| 166 | * @param array $facetConfiguration |
||
| 167 | * @param boolean $keepAllFacetsOnSelection |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | protected function getFilterTag($facetConfiguration, $keepAllFacetsOnSelection) |
||
| 171 | 7 | { |
|
| 172 | $tag = ''; |
||
| 173 | 7 | if ($facetConfiguration['keepAllOptionsOnSelection'] == 1 || $facetConfiguration['addFieldAsTag'] == 1 || $keepAllFacetsOnSelection) { |
|
| 174 | 7 | $tag = '{!tag=' . addslashes($facetConfiguration['field']) . '}'; |
|
| 175 | 2 | } |
|
| 176 | |||
| 177 | return $tag; |
||
| 178 | 7 | } |
|
| 179 | |||
| 180 | /** |
||
| 181 | * This method is used to build the filter parts of the query. |
||
| 182 | * |
||
| 183 | * @param array $facetConfiguration |
||
| 184 | * @param string $facetName |
||
| 185 | * @param array $filterValues |
||
| 186 | * @return array |
||
| 187 | */ |
||
| 188 | protected function getFilterParts($facetConfiguration, $facetName, $filterValues) |
||
| 189 | 7 | { |
|
| 190 | $filterParts = []; |
||
| 191 | 7 | ||
| 192 | $type = isset($facetConfiguration['type']) ? $facetConfiguration['type'] : 'options'; |
||
| 193 | 7 | $filterEncoder = $this->facetRegistry->getPackage($type)->getUrlDecoder(); |
|
| 194 | 7 | ||
| 195 | if (is_null($filterEncoder)) { |
||
| 196 | 7 | throw new \InvalidArgumentException('No encoder configured for facet ' . htmlspecialchars($facetName)); |
|
| 197 | } |
||
| 198 | |||
| 199 | foreach ($filterValues as $filterValue) { |
||
| 200 | 7 | $filterOptions = $facetConfiguration[$facetConfiguration['type'] . '.']; |
|
| 201 | 7 | if (empty($filterOptions)) { |
|
| 202 | 7 | $filterOptions = []; |
|
| 203 | 6 | } |
|
| 204 | |||
| 205 | $filterValue = $filterEncoder->decode($filterValue, $filterOptions); |
||
| 206 | 7 | $filterParts[] = $facetConfiguration['field'] . ':' . $filterValue; |
|
| 207 | 7 | } |
|
| 208 | |||
| 209 | return $filterParts; |
||
| 210 | 7 | } |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Groups facet values by facet name. |
||
| 214 | * |
||
| 215 | * @param array $resultParameters |
||
| 216 | * @param array $allFacets |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | protected function getFiltersByFacetName($resultParameters, $allFacets) |
||
| 220 | 7 | { |
|
| 221 | // format for filter URL parameter: |
||
| 222 | // tx_solr[filter]=$facetName0:$facetValue0,$facetName1:$facetValue1,$facetName2:$facetValue2 |
||
| 223 | $filters = array_map('urldecode', $resultParameters['filter']); |
||
| 224 | 7 | // $filters look like ['name:value1','name:value2','fieldname2:foo'] |
|
| 225 | $configuredFacets = $this->getFacetNamesWithConfiguredField($allFacets); |
||
| 226 | 7 | // first group the filters by facetName - so that we can |
|
| 227 | // decide later whether we need to do AND or OR for multiple |
||
| 228 | // filters for a certain facet/field |
||
| 229 | // $filtersByFacetName look like ['name' => ['value1', 'value2'], 'fieldname2' => ['foo']] |
||
| 230 | $filtersByFacetName = []; |
||
| 231 | 7 | foreach ($filters as $filter) { |
|
| 232 | 7 | // only split by the first colon to allow using colons in the filter value itself |
|
| 233 | list($filterFacetName, $filterValue) = explode(':', $filter, 2); |
||
| 234 | 7 | if (in_array($filterFacetName, $configuredFacets)) { |
|
| 235 | 7 | $filtersByFacetName[$filterFacetName][] = $filterValue; |
|
| 236 | 7 | } |
|
| 237 | } |
||
| 238 | |||
| 239 | return $filtersByFacetName; |
||
| 240 | 7 | } |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Gets the facets as configured through TypoScript |
||
| 244 | * |
||
| 245 | * @param array $allFacets |
||
| 246 | * @return array An array of facet names as specified in TypoScript |
||
| 247 | */ |
||
| 248 | protected function getFacetNamesWithConfiguredField(array $allFacets) |
||
| 249 | 7 | { |
|
| 250 | $facets = []; |
||
| 251 | 7 | ||
| 252 | foreach ($allFacets as $facetName => $facetConfiguration) { |
||
| 253 | 7 | $facetName = substr($facetName, 0, -1); |
|
| 254 | 7 | ||
| 255 | if (empty($facetConfiguration['field'])) { |
||
| 256 | 7 | // TODO later check for query and date, too |
|
| 257 | continue; |
||
| 258 | } |
||
| 259 | |||
| 260 | $facets[] = $facetName; |
||
| 261 | 7 | } |
|
| 262 | |||
| 263 | return $facets; |
||
| 264 | 7 | } |
|
| 265 | } |
||
| 266 |