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