We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 106 |
| Total Lines | 557 |
| Duplicated Lines | 0 % |
| Changes | 23 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SearchController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SearchController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class SearchController extends AbstractController |
||
| 26 | { |
||
| 27 | public $prefixId = 'tx_dlf'; |
||
| 28 | public $extKey = 'dlf'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var CollectionRepository |
||
| 32 | */ |
||
| 33 | protected $collectionRepository; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param CollectionRepository $collectionRepository |
||
| 37 | */ |
||
| 38 | public function injectCollectionRepository(CollectionRepository $collectionRepository) |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Search action |
||
| 45 | */ |
||
| 46 | public function searchAction() |
||
| 47 | { |
||
| 48 | $this->extConf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get($this->extKey); |
||
| 49 | |||
| 50 | // Build label for result list. |
||
| 51 | $label = htmlspecialchars(LocalizationUtility::translate('search.search', 'dlf')); |
||
|
|
|||
| 52 | if (!empty($this->requestData['query'])) { |
||
| 53 | $label .= ' ' . htmlspecialchars(sprintf(LocalizationUtility::translate('search.for', 'dlf'), trim($this->requestData['query']))); |
||
| 54 | } |
||
| 55 | // Prepare query parameters. |
||
| 56 | $params = []; |
||
| 57 | $matches = []; |
||
| 58 | // Set search query. |
||
| 59 | if ( |
||
| 60 | (!empty($this->settings['fulltext']) && !empty($this->requestData['fulltext'])) |
||
| 61 | || preg_match('/fulltext:\((.*)\)/', trim($this->requestData['query']), $matches) |
||
| 62 | ) { |
||
| 63 | // If the query already is a fulltext query e.g using the facets |
||
| 64 | $this->requestData['query'] = empty($matches[1]) ? $this->requestData['query'] : $matches[1]; |
||
| 65 | // Search in fulltext field if applicable. Query must not be empty! |
||
| 66 | if (!empty($this->requestData['query'])) { |
||
| 67 | $query = 'fulltext:(' . Solr::escapeQuery(trim($this->requestData['query'])) . ')'; |
||
| 68 | } |
||
| 69 | } else { |
||
| 70 | // Retain given search field if valid. |
||
| 71 | $query = Solr::escapeQueryKeepField(trim($this->requestData['query']), $this->settings['storagePid']); |
||
| 72 | } |
||
| 73 | // Add extended search query. |
||
| 74 | if ( |
||
| 75 | !empty($this->requestData['extQuery']) |
||
| 76 | && is_array($this->requestData['extQuery']) |
||
| 77 | ) { |
||
| 78 | $allowedOperators = ['AND', 'OR', 'NOT']; |
||
| 79 | $allowedFields = GeneralUtility::trimExplode(',', $this->settings['extendedFields'], true); |
||
| 80 | $numberOfExtQueries = count($this->requestData['extQuery']); |
||
| 81 | for ($i = 0; $i < $numberOfExtQueries; $i++) { |
||
| 82 | if (!empty($this->requestData['extQuery'][$i])) { |
||
| 83 | if ( |
||
| 84 | in_array($this->requestData['extOperator'][$i], $allowedOperators) |
||
| 85 | && in_array($this->requestData['extField'][$i], $allowedFields) |
||
| 86 | ) { |
||
| 87 | if (!empty($query)) { |
||
| 88 | $query .= ' ' . $this->requestData['extOperator'][$i] . ' '; |
||
| 89 | } |
||
| 90 | $query .= Indexer::getIndexFieldName($this->requestData['extField'][$i], $this->settings['storagePid']) . ':(' . Solr::escapeQuery($this->requestData['extQuery'][$i]) . ')'; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | } |
||
| 95 | // Add filter query for faceting. |
||
| 96 | if (!empty($this->requestData['fq'])) { |
||
| 97 | foreach ($this->requestData['fq'] as $filterQuery) { |
||
| 98 | $params['filterquery'][]['query'] = $filterQuery; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | // Add filter query for in-document searching. |
||
| 103 | if ( |
||
| 104 | $this->settings['searchIn'] == 'document' |
||
| 105 | || $this->settings['searchIn'] == 'all' |
||
| 106 | ) { |
||
| 107 | if ( |
||
| 108 | !empty($this->requestData['id']) |
||
| 109 | && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->requestData['id']) |
||
| 110 | ) { |
||
| 111 | // Search in document and all subordinates (valid for up to three levels of hierarchy). |
||
| 112 | $params['filterquery'][]['query'] = '_query_:"{!join from=uid to=partof}uid:{!join from=uid to=partof}uid:' . $this->requestData['id'] . '"' . |
||
| 113 | ' OR {!join from=uid to=partof}uid:' . $this->requestData['id'] . |
||
| 114 | ' OR uid:' . $this->requestData['id']; |
||
| 115 | $label .= ' ' . htmlspecialchars(sprintf(LocalizationUtility::translate('search.in', 'dlf'), Doc::getTitle($this->requestData['id']))); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | // Add filter query for in-collection searching. |
||
| 119 | if ( |
||
| 120 | $this->settings['searchIn'] == 'collection' |
||
| 121 | || $this->settings['searchIn'] == 'all' |
||
| 122 | ) { |
||
| 123 | if ( |
||
| 124 | !empty($this->requestData['collection']) |
||
| 125 | && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->requestData['collection']) |
||
| 126 | ) { |
||
| 127 | $index_name = Helper::getIndexNameFromUid($this->requestData['collection'], 'tx_dlf_collections', $this->settings['storagePid']); |
||
| 128 | $params['filterquery'][]['query'] = 'collection_faceting:("' . Solr::escapeQuery($index_name) . '")'; |
||
| 129 | $label .= ' ' . sprintf(LocalizationUtility::translate('search.in', 'dlf'), Helper::translate($index_name, 'tx_dlf_collections', $this->settings['storagePid'])); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | // Add filter query for collection restrictions. |
||
| 133 | if ($this->settings['collections']) { |
||
| 134 | $collIds = explode(',', $this->settings['collections']); |
||
| 135 | $collIndexNames = []; |
||
| 136 | foreach ($collIds as $collId) { |
||
| 137 | $collIndexNames[] = Solr::escapeQuery(Helper::getIndexNameFromUid(intval($collId), 'tx_dlf_collections', $this->settings['storagePid'])); |
||
| 138 | } |
||
| 139 | // Last value is fake and used for distinction in $this->addCurrentCollection() |
||
| 140 | $params['filterquery'][]['query'] = 'collection_faceting:("' . implode('" OR "', $collIndexNames) . '" OR "FakeValueForDistinction")'; |
||
| 141 | } |
||
| 142 | // Set some query parameters. |
||
| 143 | $params['query'] = !empty($query) ? $query : '*'; |
||
| 144 | $params['start'] = 0; |
||
| 145 | $params['rows'] = 0; |
||
| 146 | $params['sort'] = ['score' => 'desc']; |
||
| 147 | // Instantiate search object. |
||
| 148 | $solr = Solr::getInstance($this->settings['solrcore']); |
||
| 149 | if (!$solr->ready) { |
||
| 150 | $this->logger->error('Apache Solr not available'); |
||
| 151 | $this->redirect('main', 'Search', null); |
||
| 152 | //return $this->responseFactory->createHtmlResponse($this->view->render()); |
||
| 153 | } |
||
| 154 | // Set search parameters. |
||
| 155 | $solr->cPid = $this->settings['storagePid']; |
||
| 156 | $solr->params = $params; |
||
| 157 | // Perform search. |
||
| 158 | $list = $solr->search(); |
||
| 159 | $list->metadata = [ |
||
| 160 | 'label' => $label, |
||
| 161 | 'thumbnail' => '', |
||
| 162 | 'searchString' => $this->requestData['query'], |
||
| 163 | 'fulltextSearch' => (!empty($this->requestData['fulltext']) ? '1' : '0'), |
||
| 164 | 'options' => $list->metadata['options'] |
||
| 165 | ]; |
||
| 166 | $list->save(); |
||
| 167 | // Clean output buffer. |
||
| 168 | ob_end_clean(); |
||
| 169 | $additionalParams = []; |
||
| 170 | if (!empty($this->requestData['logicalPage'])) { |
||
| 171 | $additionalParams['logicalPage'] = $this->requestData['logicalPage']; |
||
| 172 | } |
||
| 173 | // Jump directly to the page view, if there is only one result and it is configured |
||
| 174 | if ($list->metadata['options']['numberOfHits'] == 1 && !empty($this->settings['showSingleResult'])) { |
||
| 175 | $linkConf['parameter'] = $this->settings['targetPidPageView']; |
||
| 176 | $additionalParams['id'] = $list->current()['uid']; |
||
| 177 | $additionalParams['highlight_word'] = preg_replace('/\s\s+/', ';', $list->metadata['searchString']); |
||
| 178 | $additionalParams['page'] = count($list[0]['subparts']) == 1 ? $list[0]['subparts'][0]['page'] : 1; |
||
| 179 | } else { |
||
| 180 | // Keep some plugin variables. |
||
| 181 | $linkConf['parameter'] = $this->settings['targetPid']; |
||
| 182 | if (!empty($this->requestData['order'])) { |
||
| 183 | $additionalParams['order'] = $this->requestData['order']; |
||
| 184 | $additionalParams['asc'] = !empty($this->requestData['asc']) ? '1' : '0'; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | $linkConf['forceAbsoluteUrl'] = !empty($this->settings['forceAbsoluteUrl']) ? 1 : 0; |
||
| 188 | $linkConf['forceAbsoluteUrl.']['scheme'] = !empty($this->settings['forceAbsoluteUrl']) && !empty($this->settings['forceAbsoluteUrlHttps']) ? 'https' : 'http'; |
||
| 189 | $linkConf['additionalParams'] = GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', true, false); |
||
| 190 | |||
| 191 | $this->redirect('main', 'Search', null, null); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * |
||
| 196 | * @return mixed |
||
| 197 | * @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException |
||
| 198 | */ |
||
| 199 | public function mainAction() |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Adds the current document's UID or parent ID to the search form |
||
| 242 | * |
||
| 243 | * @access protected |
||
| 244 | * |
||
| 245 | * @return string HTML input fields with current document's UID |
||
| 246 | */ |
||
| 247 | protected function addCurrentDocument() |
||
| 248 | { |
||
| 249 | // Load current list object. |
||
| 250 | $list = GeneralUtility::makeInstance(DocumentList::class); |
||
| 251 | // Load current document. |
||
| 252 | if ( |
||
| 253 | !empty($this->requestData['id']) |
||
| 254 | && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->requestData['id']) |
||
| 255 | ) { |
||
| 256 | $this->loadDocument($this->requestData); |
||
| 257 | // Get document's UID |
||
| 258 | if ($this->document) { |
||
| 259 | $this->view->assign('DOCUMENT_ID', $this->document->getUid()); |
||
| 260 | } |
||
| 261 | } elseif (!empty($list->metadata['options']['params']['filterquery'])) { |
||
| 262 | // Get document's UID from search metadata. |
||
| 263 | // The string may be e.g. "{!join from=uid to=partof}uid:{!join from=uid to=partof}uid:2" OR {!join from=uid to=partof}uid:2 OR uid:2" |
||
| 264 | // or "collection_faceting:("Some Collection Title")" |
||
| 265 | foreach ($list->metadata['options']['params']['filterquery'] as $facet) { |
||
| 266 | if (($lastUidPos = strrpos($facet['query'], 'uid:')) !== false) { |
||
| 267 | $facetKeyVal = explode(':', substr($facet['query'], $lastUidPos)); |
||
| 268 | if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($facetKeyVal[1])) { |
||
| 269 | $documentId = (int) $facetKeyVal[1]; |
||
| 270 | } |
||
| 271 | } |
||
| 272 | } |
||
| 273 | if (!empty($documentId)) { |
||
| 274 | $this->view->assign('DOCUMENT_ID', $documentId); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | |||
| 280 | /** |
||
| 281 | * Adds the current collection's UID to the search form |
||
| 282 | * |
||
| 283 | * @access protected |
||
| 284 | * |
||
| 285 | * @return string HTML input fields with current document's UID and parent ID |
||
| 286 | */ |
||
| 287 | protected function addCurrentCollection() |
||
| 288 | { |
||
| 289 | // Load current collection. |
||
| 290 | $list = GeneralUtility::makeInstance(DocumentList::class); |
||
| 291 | if ( |
||
| 292 | !empty($list->metadata['options']['source']) |
||
| 293 | && $list->metadata['options']['source'] == 'collection' |
||
| 294 | ) { |
||
| 295 | $this->view->assign('COLLECTION_ID', $list->metadata['options']['select']); |
||
| 296 | // Get collection's UID. |
||
| 297 | } elseif (!empty($list->metadata['options']['params']['filterquery'])) { |
||
| 298 | // Get collection's UID from search metadata. |
||
| 299 | foreach ($list->metadata['options']['params']['filterquery'] as $facet) { |
||
| 300 | $facetKeyVal = explode(':', $facet['query'], 2); |
||
| 301 | if ( |
||
| 302 | $facetKeyVal[0] == 'collection_faceting' |
||
| 303 | && !strpos($facetKeyVal[1], '" OR "') |
||
| 304 | ) { |
||
| 305 | $collectionId = Helper::getUidFromIndexName(trim($facetKeyVal[1], '(")'), 'tx_dlf_collections'); |
||
| 306 | } |
||
| 307 | } |
||
| 308 | $this->view->assign('COLLECTION_ID', $collectionId); |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Adds the encrypted Solr core name to the search form |
||
| 314 | * |
||
| 315 | * @access protected |
||
| 316 | * |
||
| 317 | * @return string HTML input fields with encrypted core name and hash |
||
| 318 | */ |
||
| 319 | protected function addEncryptedCoreName() |
||
| 320 | { |
||
| 321 | // Get core name. |
||
| 322 | $name = Helper::getIndexNameFromUid($this->settings['solrcore'], 'tx_dlf_solrcores'); |
||
| 323 | // Encrypt core name. |
||
| 324 | if (!empty($name)) { |
||
| 325 | $name = Helper::encrypt($name); |
||
| 326 | } |
||
| 327 | // Add encrypted fields to search form. |
||
| 328 | if ($name !== false) { |
||
| 329 | $this->view->assign('ENCRYPTED_CORE_NAME', $name); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Adds the facets menu to the search form |
||
| 335 | * |
||
| 336 | * @access protected |
||
| 337 | * |
||
| 338 | * @return string HTML output of facets menu |
||
| 339 | */ |
||
| 340 | protected function addFacetsMenu() |
||
| 341 | { |
||
| 342 | // Check for typoscript configuration to prevent fatal error. |
||
| 343 | if (empty($this->settings['facetsConf'])) { |
||
| 344 | $this->logger->warning('Incomplete plugin configuration'); |
||
| 345 | return ''; |
||
| 346 | } |
||
| 347 | // Quit without doing anything if no facets are selected. |
||
| 348 | if (empty($this->settings['facets']) && empty($this->settings['facetCollections'])) { |
||
| 349 | return ''; |
||
| 350 | } |
||
| 351 | |||
| 352 | // Get facets from plugin configuration. |
||
| 353 | $facets = []; |
||
| 354 | foreach (GeneralUtility::trimExplode(',', $this->settings['facets'], true) as $facet) { |
||
| 355 | $facets[$facet . '_faceting'] = Helper::translate($facet, 'tx_dlf_metadata', $this->settings['storagePid']); |
||
| 356 | } |
||
| 357 | |||
| 358 | $this->view->assign('facetsMenu', $this->makeFacetsMenuArray($facets)); |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * This builds a menu array for HMENU |
||
| 363 | * |
||
| 364 | * @access public |
||
| 365 | * |
||
| 366 | * @param string $content: The PlugIn content |
||
| 367 | * @param array $conf: The PlugIn configuration |
||
| 368 | * |
||
| 369 | * @return array HMENU array |
||
| 370 | */ |
||
| 371 | public function makeFacetsMenuArray($facets) |
||
| 372 | { |
||
| 373 | $menuArray = []; |
||
| 374 | // Set default value for facet search. |
||
| 375 | $search = [ |
||
| 376 | 'query' => '*', |
||
| 377 | 'params' => [ |
||
| 378 | 'component' => [ |
||
| 379 | 'facetset' => [ |
||
| 380 | 'facet' => [] |
||
| 381 | ] |
||
| 382 | ] |
||
| 383 | ] |
||
| 384 | ]; |
||
| 385 | // Extract query and filter from last search. |
||
| 386 | $list = GeneralUtility::makeInstance(DocumentList::class); |
||
| 387 | if (!empty($list->metadata['options']['source'])) { |
||
| 388 | if ($list->metadata['options']['source'] == 'search') { |
||
| 389 | $search['query'] = $list->metadata['options']['select']; |
||
| 390 | } |
||
| 391 | $search['params'] = $list->metadata['options']['params']; |
||
| 392 | } |
||
| 393 | // Get applicable facets. |
||
| 394 | $solr = Solr::getInstance($this->settings['solrcore']); |
||
| 395 | if (!$solr->ready) { |
||
| 396 | $this->logger->error('Apache Solr not available'); |
||
| 397 | return []; |
||
| 398 | } |
||
| 399 | // Set needed parameters for facet search. |
||
| 400 | if (empty($search['params']['filterquery'])) { |
||
| 401 | $search['params']['filterquery'] = []; |
||
| 402 | } |
||
| 403 | |||
| 404 | foreach (array_keys($facets) as $field) { |
||
| 405 | $search['params']['component']['facetset']['facet'][] = [ |
||
| 406 | 'type' => 'field', |
||
| 407 | 'key' => $field, |
||
| 408 | 'field' => $field, |
||
| 409 | 'limit' => $this->settings['limitFacets'], |
||
| 410 | 'sort' => isset($this->settings['sortingFacets']) ? $this->settings['sortingFacets'] : 'count' |
||
| 411 | ]; |
||
| 412 | } |
||
| 413 | |||
| 414 | // Set additional query parameters. |
||
| 415 | $search['params']['start'] = 0; |
||
| 416 | $search['params']['rows'] = 0; |
||
| 417 | // Set query. |
||
| 418 | $search['params']['query'] = $search['query']; |
||
| 419 | // Perform search. |
||
| 420 | $selectQuery = $solr->service->createSelect($search['params']); |
||
| 421 | $results = $solr->service->select($selectQuery); |
||
| 422 | $facet = $results->getFacetSet(); |
||
| 423 | |||
| 424 | $facetCollectionArray = []; |
||
| 425 | |||
| 426 | // replace everything expect numbers and comma |
||
| 427 | $facetCollections = preg_replace('/[^0-9,]/', '', $this->settings['facetCollections']); |
||
| 428 | |||
| 429 | if (!empty($facetCollections)) { |
||
| 430 | $collections = $this->collectionRepository->findCollectionsBySettings(['collections' => $facetCollections]); |
||
| 431 | |||
| 432 | /** @var Collection $collection */ |
||
| 433 | foreach ($collections as $collection) { |
||
| 434 | $facetCollectionArray[] = $collection->getIndexName(); |
||
| 435 | } |
||
| 436 | } |
||
| 437 | |||
| 438 | // Process results. |
||
| 439 | if ($facet) { |
||
| 440 | foreach ($facet as $field => $values) { |
||
| 441 | $entryArray = []; |
||
| 442 | $entryArray['title'] = htmlspecialchars($facets[$field]); |
||
| 443 | $entryArray['count'] = 0; |
||
| 444 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
| 445 | $entryArray['doNotLinkIt'] = 1; |
||
| 446 | $entryArray['ITEM_STATE'] = 'NO'; |
||
| 447 | // Count number of facet values. |
||
| 448 | $i = 0; |
||
| 449 | foreach ($values as $value => $count) { |
||
| 450 | if ($count > 0) { |
||
| 451 | // check if facet collection configuration exists |
||
| 452 | if (!empty($this->settings['facetCollections'])) { |
||
| 453 | if ($field == "collection_faceting" && !in_array($value, $facetCollectionArray)) { |
||
| 454 | continue; |
||
| 455 | } |
||
| 456 | } |
||
| 457 | $entryArray['count']++; |
||
| 458 | if ($entryArray['ITEM_STATE'] == 'NO') { |
||
| 459 | $entryArray['ITEM_STATE'] = 'IFSUB'; |
||
| 460 | } |
||
| 461 | $entryArray['_SUB_MENU'][] = $this->getFacetsMenuEntry($field, $value, $count, $search, $entryArray['ITEM_STATE']); |
||
| 462 | if (++$i == $this->settings['limit']) { |
||
| 463 | break; |
||
| 464 | } |
||
| 465 | } else { |
||
| 466 | break; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | $menuArray[] = $entryArray; |
||
| 470 | } |
||
| 471 | } |
||
| 472 | return $menuArray; |
||
| 473 | } |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Creates an array for a HMENU entry of a facet value. |
||
| 477 | * |
||
| 478 | * @access protected |
||
| 479 | * |
||
| 480 | * @param string $field: The facet's index_name |
||
| 481 | * @param string $value: The facet's value |
||
| 482 | * @param int $count: Number of hits for this facet |
||
| 483 | * @param array $search: The parameters of the current search query |
||
| 484 | * @param string &$state: The state of the parent item |
||
| 485 | * |
||
| 486 | * @return array The array for the facet's menu entry |
||
| 487 | */ |
||
| 488 | protected function getFacetsMenuEntry($field, $value, $count, $search, &$state) |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns the extended search form and adds the JS files necessary for extended search. |
||
| 548 | * |
||
| 549 | * @access protected |
||
| 550 | * |
||
| 551 | * @return string The extended search form or an empty string |
||
| 552 | */ |
||
| 553 | protected function addExtendedSearch() |
||
| 582 | } |
||
| 583 | } |
||
| 584 |