We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Conditions | 48 |
| Paths | 15172 |
| Total Lines | 280 |
| Code Lines | 109 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 436 | public function main($content, $conf) { |
||
| 437 | |||
| 438 | $this->init($conf); |
||
| 439 | |||
| 440 | // Disable caching for this plugin. |
||
| 441 | $this->setCache(FALSE); |
||
| 442 | |||
| 443 | // Quit without doing anything if required variables are not set. |
||
| 444 | if (empty($this->conf['solrcore'])) { |
||
| 445 | |||
| 446 | if (TYPO3_DLOG) { |
||
| 447 | |||
| 448 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_search->main('.$content.', [data])] Incomplete plugin configuration', $this->extKey, SYSLOG_SEVERITY_WARNING, $conf); |
||
| 449 | |||
| 450 | } |
||
| 451 | |||
| 452 | return $content; |
||
| 453 | |||
| 454 | } |
||
| 455 | |||
| 456 | if (!isset($this->piVars['query']) && empty($this->piVars['extQuery'])) { |
||
| 457 | |||
| 458 | // Extract query and filter from last search. |
||
| 459 | $list = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_dlf_list'); |
||
| 460 | |||
| 461 | if (!empty($list->metadata['searchString'])) { |
||
| 462 | |||
| 463 | if ($list->metadata['options']['source'] == 'search') { |
||
| 464 | |||
| 465 | $search['query'] = $list->metadata['searchString']; |
||
|
1 ignored issue
–
show
|
|||
| 466 | |||
| 467 | } |
||
| 468 | |||
| 469 | $search['params'] = $list->metadata['options']['params']; |
||
| 470 | |||
| 471 | } |
||
| 472 | |||
| 473 | // Add javascript for search suggestions if enabled and jQuery autocompletion is available. |
||
| 474 | if (!empty($this->conf['suggest'])) { |
||
| 475 | |||
| 476 | $this->addAutocompleteJS(); |
||
| 477 | |||
| 478 | } |
||
| 479 | |||
| 480 | // Load template file. |
||
| 481 | if (!empty($this->conf['templateFile'])) { |
||
| 482 | |||
| 483 | $this->template = $this->cObj->getSubpart($this->cObj->fileResource($this->conf['templateFile']), '###TEMPLATE###'); |
||
| 484 | |||
| 485 | } else { |
||
| 486 | |||
| 487 | $this->template = $this->cObj->getSubpart($this->cObj->fileResource('EXT:dlf/plugins/search/template.tmpl'), '###TEMPLATE###'); |
||
| 488 | |||
| 489 | } |
||
| 490 | |||
| 491 | // Configure @action URL for form. |
||
| 492 | $linkConf = array ( |
||
| 493 | 'parameter' => $GLOBALS['TSFE']->id, |
||
| 494 | 'forceAbsoluteUrl' => 1 |
||
| 495 | ); |
||
| 496 | |||
| 497 | // Fill markers. |
||
| 498 | $markerArray = array ( |
||
| 499 | '###ACTION_URL###' => $this->cObj->typoLink_URL($linkConf), |
||
| 500 | '###LABEL_QUERY###' => (!empty($search['query']) ? $search['query'] : $this->pi_getLL('label.query')), |
||
| 501 | '###LABEL_SUBMIT###' => $this->pi_getLL('label.submit'), |
||
| 502 | '###FIELD_QUERY###' => $this->prefixId.'[query]', |
||
| 503 | '###QUERY###' => (!empty($search['query']) ? $search['query'] : ''), |
||
| 504 | '###FULLTEXTSWITCH###' => $this->addFulltextSwitch($list->metadata['fulltextSearch']), |
||
| 505 | '###FIELD_DOC###' => ($this->conf['searchIn'] == 'document' || $this->conf['searchIn'] == 'all' ? $this->addCurrentDocument() : ''), |
||
| 506 | '###FIELD_COLL###' => ($this->conf['searchIn'] == 'collection' || $this->conf['searchIn'] == 'all' ? $this->addCurrentCollection() : ''), |
||
| 507 | '###ADDITIONAL_INPUTS###' => $this->addEncryptedCoreName(), |
||
| 508 | '###FACETS_MENU###' => $this->addFacetsMenu(), |
||
| 509 | '###LOGICAL_PAGE###' => $this->addLogicalPage() |
||
| 510 | ); |
||
| 511 | |||
| 512 | // Get additional fields for extended search. |
||
| 513 | $extendedSearch = $this->addExtendedSearch(); |
||
| 514 | |||
| 515 | // Display search form. |
||
| 516 | $content .= $this->cObj->substituteSubpart($this->cObj->substituteMarkerArray($this->template, $markerArray), '###EXT_SEARCH_ENTRY###', $extendedSearch); |
||
| 517 | |||
| 518 | return $this->pi_wrapInBaseClass($content); |
||
| 519 | |||
| 520 | } else { |
||
| 521 | |||
| 522 | // Instantiate search object. |
||
| 523 | $solr = tx_dlf_solr::getInstance($this->conf['solrcore']); |
||
| 524 | |||
| 525 | if (!$solr->ready) { |
||
| 526 | |||
| 527 | if (TYPO3_DLOG) { |
||
| 528 | |||
| 529 | \TYPO3\CMS\Core\Utility\GeneralUtility::devLog('[tx_dlf_search->main('.$content.', [data])] Apache Solr not available', $this->extKey, SYSLOG_SEVERITY_ERROR, $conf); |
||
| 530 | |||
| 531 | } |
||
| 532 | |||
| 533 | return $content; |
||
| 534 | |||
| 535 | } |
||
| 536 | |||
| 537 | // Build label for result list. |
||
| 538 | $label = $this->pi_getLL('search', '', TRUE); |
||
| 539 | |||
| 540 | if (!empty($this->piVars['query'])) { |
||
| 541 | |||
| 542 | $label .= htmlspecialchars(sprintf($this->pi_getLL('for', ''), $this->piVars['query'])); |
||
| 543 | |||
| 544 | } |
||
| 545 | |||
| 546 | // Prepare query parameters. |
||
| 547 | $params = array (); |
||
| 548 | |||
| 549 | $matches = array (); |
||
| 550 | |||
| 551 | // Set search query. |
||
| 552 | if ((!empty($this->conf['fulltext']) && !empty($this->piVars['fulltext'])) || preg_match('/fulltext:\((.*)\)/', $this->piVars['query'], $matches)) { |
||
| 553 | |||
| 554 | // If the query already is a fulltext query e.g using the facets |
||
| 555 | $this->piVars['query'] = empty($matches[1]) ? $this->piVars['query'] : $matches[1]; |
||
| 556 | |||
| 557 | // Search in fulltext field if applicable. query must not be empty! |
||
| 558 | if (!empty($this->piVars['query'])) { |
||
| 559 | |||
| 560 | $query = 'fulltext:('.tx_dlf_solr::escapeQuery($this->piVars['query']).')'; |
||
| 561 | |||
| 562 | } |
||
| 563 | |||
| 564 | } else { |
||
| 565 | // Retain given search field if valid. |
||
| 566 | $query = tx_dlf_solr::escapeQueryKeepField($this->piVars['query'], $this->conf['pages']); |
||
| 567 | |||
| 568 | } |
||
| 569 | |||
| 570 | // Add extended search query. |
||
| 571 | if (!empty($this->piVars['extQuery']) && is_array($this->piVars['extQuery'])) { |
||
| 572 | |||
| 573 | $allowedOperators = array ('AND', 'OR', 'NOT'); |
||
| 574 | |||
| 575 | $allowedFields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['extendedFields'], TRUE); |
||
| 576 | |||
| 577 | for ($i = 0; $i < count($this->piVars['extQuery']); $i++) { |
||
| 578 | |||
| 579 | if (!empty($this->piVars['extQuery'][$i])) { |
||
| 580 | |||
| 581 | if (in_array($this->piVars['extOperator'][$i], $allowedOperators) && in_array($this->piVars['extField'][$i], $allowedFields)) { |
||
| 582 | |||
| 583 | if (!empty($query)) { |
||
| 584 | |||
| 585 | $query .= ' '.$this->piVars['extOperator'][$i].' '; |
||
| 586 | |||
| 587 | } |
||
| 588 | |||
| 589 | $query .= tx_dlf_indexing::getIndexFieldName($this->piVars['extField'][$i], $this->conf['pages']).':('.tx_dlf_solr::escapeQuery($this->piVars['extQuery'][$i]).')'; |
||
| 590 | |||
| 591 | } |
||
| 592 | |||
| 593 | } |
||
| 594 | |||
| 595 | } |
||
| 596 | |||
| 597 | } |
||
| 598 | |||
| 599 | // Add filter query for faceting. |
||
| 600 | if (!empty($this->piVars['fq'])) { |
||
| 601 | |||
| 602 | $params['fq'] = $this->piVars['fq']; |
||
| 603 | |||
| 604 | } |
||
| 605 | |||
| 606 | // Add filter query for in-document searching. |
||
| 607 | if ($this->conf['searchIn'] == 'document' || $this->conf['searchIn'] == 'all') { |
||
| 608 | |||
| 609 | if (!empty($this->piVars['id']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['id'])) { |
||
| 610 | |||
| 611 | $params['fq'][] = 'uid:('.$this->piVars['id'].') OR partof:('.$this->piVars['id'].')'; |
||
| 612 | |||
| 613 | $label .= htmlspecialchars(sprintf($this->pi_getLL('in', ''), tx_dlf_document::getTitle($this->piVars['id']))); |
||
| 614 | |||
| 615 | } |
||
| 616 | |||
| 617 | } |
||
| 618 | |||
| 619 | // Add filter query for in-collection searching. |
||
| 620 | if ($this->conf['searchIn'] == 'collection' || $this->conf['searchIn'] == 'all') { |
||
| 621 | |||
| 622 | if (!empty($this->piVars['collection']) && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['collection'])) { |
||
| 623 | |||
| 624 | $index_name = tx_dlf_helper::getIndexName($this->piVars['collection'], 'tx_dlf_collections', $this->conf['pages']); |
||
| 625 | |||
| 626 | $params['fq'][] = 'collection_faceting:("'.tx_dlf_solr::escapeQuery($index_name).'")'; |
||
| 627 | |||
| 628 | $label .= sprintf($this->pi_getLL('in', '', TRUE), tx_dlf_helper::translate($index_name, 'tx_dlf_collections', $this->conf['pages'])); |
||
| 629 | |||
| 630 | } |
||
| 631 | |||
| 632 | } |
||
| 633 | |||
| 634 | // Add filter query for collection restrictions. |
||
| 635 | if ($this->conf['collections']) { |
||
| 636 | |||
| 637 | $collIds = explode(',', $this->conf['collections']); |
||
| 638 | |||
| 639 | $collIndexNames = array (); |
||
| 640 | |||
| 641 | foreach ($collIds as $collId) { |
||
| 642 | |||
| 643 | $collIndexNames[] = tx_dlf_solr::escapeQuery(tx_dlf_helper::getIndexName(intval($collId), 'tx_dlf_collections', $this->conf['pages'])); |
||
| 644 | |||
| 645 | } |
||
| 646 | |||
| 647 | // Last value is fake and used for distinction in $this->addCurrentCollection() |
||
| 648 | $params['fq'][] = 'collection_faceting:("'.implode('" OR "', $collIndexNames).'" OR "FakeValueForDistinction")'; |
||
| 649 | |||
| 650 | } |
||
| 651 | |||
| 652 | // Set search parameters. |
||
| 653 | $solr->limit = max(intval($this->conf['limit']), 1); |
||
| 654 | |||
| 655 | $solr->cPid = $this->conf['pages']; |
||
| 656 | |||
| 657 | $solr->params = $params; |
||
| 658 | |||
| 659 | // Perform search. |
||
| 660 | $results = $solr->search($query); |
||
| 661 | |||
| 662 | $results->metadata = array ( |
||
| 663 | 'label' => $label, |
||
| 664 | 'description' => '<p class="tx-dlf-search-numHits">'.htmlspecialchars(sprintf($this->pi_getLL('hits', ''), $solr->numberOfHits, count($results))).'</p>', |
||
| 665 | 'thumbnail' => '', |
||
| 666 | 'searchString' => $this->piVars['query'], |
||
| 667 | 'fulltextSearch' => (!empty($this->piVars['fulltext']) ? '1' : '0'), |
||
| 668 | 'options' => $results->metadata['options'] |
||
| 669 | ); |
||
| 670 | |||
| 671 | $results->save(); |
||
| 672 | |||
| 673 | // Clean output buffer. |
||
| 674 | \TYPO3\CMS\Core\Utility\GeneralUtility::cleanOutputBuffers(); |
||
| 675 | |||
| 676 | $additionalParams = array (); |
||
| 677 | |||
| 678 | if (!empty($this->piVars['logicalPage'])) { |
||
| 679 | |||
| 680 | $additionalParams['logicalPage'] = $this->piVars['logicalPage']; |
||
| 681 | |||
| 682 | } |
||
| 683 | |||
| 684 | // Jump directly to the page view, if there is only one result and it is configured |
||
| 685 | if ($results->count() == 1 && !empty($this->conf['showSingleResult'])) { |
||
| 686 | |||
| 687 | $linkConf['parameter'] = $this->conf['targetPidPageView']; |
||
|
1 ignored issue
–
show
|
|||
| 688 | |||
| 689 | $additionalParams['id'] = $results->current()['uid']; |
||
| 690 | $additionalParams['highlight_word'] = preg_replace('/\s\s+/', ';', $results->metadata['searchString']); |
||
| 691 | $additionalParams['page'] = count($results[0]['subparts']) == 1 ? $results[0]['subparts'][0]['page'] : 1; |
||
| 692 | |||
| 693 | } else { |
||
| 694 | |||
| 695 | // Keep some plugin variables. |
||
| 696 | $linkConf['parameter'] = $this->conf['targetPid']; |
||
| 697 | |||
| 698 | if (!empty($this->piVars['order'])) { |
||
| 699 | |||
| 700 | $additionalParams['order'] = $this->piVars['order']; |
||
| 701 | $additionalParams['asc'] = !empty($this->piVars['asc']) ? '1' : '0'; |
||
| 702 | |||
| 703 | } |
||
| 704 | |||
| 705 | } |
||
| 706 | |||
| 707 | $linkConf['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE); |
||
| 708 | |||
| 709 | // Send headers. |
||
| 710 | header('Location: '.\TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl($this->cObj->typoLink_URL($linkConf))); |
||
| 711 | |||
| 712 | // Flush output buffer and end script processing. |
||
| 713 | ob_end_flush(); |
||
| 714 | |||
| 715 | exit; |
||
| 716 | |||
| 847 |