/** * Modifies the given document and returns the modified document as result. * * @param ResultsCommand $resultCommand The search result command * @param array $resultDocument Result document as array * @return array The document with fields as array */ public function modifyResultDocument(ResultsCommand $resultCommand, array $resultDocument) { $this->search = $resultCommand->getParentPlugin()->getSearchResultSetService()->getSearch(); $highlightedContent = $this->search->getHighlightedContent(); foreach ($this->highlightFields as $highlightField) { if (!empty($highlightedContent->{$resultDocument['id']}->{$highlightField}[0])) { $fragments = array(); foreach ($highlightedContent->{$resultDocument['id']}->{$highlightField} as $fragment) { $fragments[] = Template::escapeMarkers($fragment); } $resultDocument[$highlightField] = implode(' ' . $this->fragmentSeparator . ' ', $fragments); } } return $resultDocument; }
/** * Modifies the given document and returns the modified document as result. * * @param ResultsCommand $resultCommand The search result command * @param array $resultDocument Result document as array * @return array The document with fields as array */ public function modifyResultDocument(ResultsCommand $resultCommand, array $resultDocument) { $this->search = $resultCommand->getParentPlugin()->getSearch(); $configuration = Util::getSolrConfiguration(); $highlightedContent = $this->search->getHighlightedContent(); $highlightFields = GeneralUtility::trimExplode(',', $configuration['search.']['results.']['resultsHighlighting.']['highlightFields'], true); foreach ($highlightFields as $highlightField) { if (!empty($highlightedContent->{$resultDocument['id']}->{$highlightField}[0])) { $fragments = array(); foreach ($highlightedContent->{$resultDocument['id']}->{$highlightField} as $fragment) { $fragments[] = Template::escapeMarkers($fragment); } $resultDocument[$highlightField] = implode(' ' . $configuration['search.']['results.']['resultsHighlighting.']['fragmentSeparator'] . ' ', $fragments); } } return $resultDocument; }
/** * Renders the complete facet. * * @return string Rendered HTML representing the facet. */ public function renderFacetOptions() { $facetOptionLinks = array(); $solrConfiguration = Util::getSolrConfiguration(); $this->template->workOnSubpart('single_facet_option'); if (!empty($this->facetConfiguration['manualSortOrder'])) { $this->sortFacetOptionsByUserDefinedOrder(); } if (!empty($this->facetConfiguration['reverseOrder'])) { $this->facetOptions = array_reverse($this->facetOptions, true); } $i = 0; foreach ($this->facetOptions as $facetOption => $facetOptionResultCount) { $facetOption = (string) $facetOption; if ($facetOption == '_empty_') { // TODO - for now we don't handle facet missing. continue; } $facetOption = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Facet\\FacetOption', $this->facetName, $facetOption, $facetOptionResultCount); /* @var $facetOption FacetOption */ $facetLinkBuilder = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Facet\\LinkBuilder', $this->query, $this->facetName, $facetOption); /* @var $facetLinkBuilder LinkBuilder */ $facetLinkBuilder->setLinkTargetPageId($this->linkTargetPageId); $optionText = $facetOption->render(); $optionLink = $facetLinkBuilder->getAddFacetOptionLink($optionText); $optionLinkUrl = $facetLinkBuilder->getAddFacetOptionUrl(); $optionHidden = ''; if (++$i > $solrConfiguration['search.']['faceting.']['limit']) { $optionHidden = 'tx-solr-facet-hidden'; } $optionSelected = $facetOption->isSelectedInFacet($this->facetName); // negating the facet option links to remove a filter if ($this->facetConfiguration['selectingSelectedFacetOptionRemovesFilter'] && $optionSelected) { $optionLink = $facetLinkBuilder->getRemoveFacetOptionLink($optionText); $optionLinkUrl = $facetLinkBuilder->getRemoveFacetOptionUrl(); } elseif ($this->facetConfiguration['singleOptionMode']) { $optionLink = $facetLinkBuilder->getReplaceFacetOptionLink($optionText); $optionLinkUrl = $facetLinkBuilder->getReplaceFacetOptionUrl(); } $facetOptionLinks[] = array('hidden' => $optionHidden, 'link' => $optionLink, 'url' => $optionLinkUrl, 'text' => $optionText, 'value' => $facetOption->getValue(), 'count' => $facetOption->getNumberOfResults(), 'selected' => $optionSelected ? '1' : '0', 'facet_name' => $this->facetName); } $this->template->addLoop('facet_links', 'facet_link', $facetOptionLinks); return $this->template->render(); }
/** * Builds the properties for the frequent search term markers. * * @param array $frequentSearchTerms Frequent search terms as array with terms as keys and hits as the value * @return array An array with content for the frequent terms markers */ protected function getSearchTermMarkerProperties(array $frequentSearchTerms) { $frequentSearches = array(); $minimumSize = $this->frequentSearchConfiguration['minSize']; $maximumSize = $this->frequentSearchConfiguration['maxSize']; if (count($frequentSearchTerms)) { $maximumHits = max(array_values($frequentSearchTerms)); $minimumHits = min(array_values($frequentSearchTerms)); $spread = $maximumHits - $minimumHits; $step = $spread == 0 ? 1 : ($maximumSize - $minimumSize) / $spread; foreach ($frequentSearchTerms as $term => $hits) { $size = round($minimumSize + ($hits - $minimumHits) * $step); $frequentSearches[] = array('term' => Template::escapeMarkers($term), 'hits' => $hits, 'style' => 'font-size: ' . $size . 'px', 'class' => 'tx-solr-frequent-term-' . $size, 'parameters' => '&q=' . html_entity_decode($term, ENT_NOQUOTES, 'UTF-8'), 'pid' => $this->parentPlugin->getLinkTargetPageId()); } } return $frequentSearches; }
/** * Helper method to escape/encode keywords for use in HTML * * @param string $keywords Keywords to prepare for use in HTML * @return string Encoded keywords */ public static function cleanKeywords($keywords) { $keywords = trim($keywords); $keywords = GeneralUtility::removeXSS($keywords); $keywords = htmlentities($keywords, ENT_QUOTES, $GLOBALS['TSFE']->metaCharset); // escape triple hashes as they are used in the template engine // TODO remove after switching to fluid templates $keywords = Template::escapeMarkers($keywords); return $keywords; }
/** * Prepares the content for the last search markers * * @return array An array with content for the last search markers */ protected function getLastSearches() { $lastSearchesKeywords = array(); switch ($this->configuration['search.']['lastSearches.']['mode']) { case 'user': $lastSearchesKeywords = $this->getLastSearchesFromSession(); break; case 'global': $lastSearchesKeywords = $this->getLastSearchesFromDatabase($this->configuration['search.']['lastSearches.']['limit']); break; } // fill array for output $i = 0; $lastSearches = array(); foreach ($lastSearchesKeywords as $keywords) { if (++$i > $this->configuration['search.']['lastSearches.']['limit']) { break; } $keywords = stripslashes($keywords); $lastSearches[] = array('q' => Template::escapeMarkers($keywords), 'parameters' => '&q=' . html_entity_decode($keywords, ENT_NOQUOTES, 'UTF-8'), 'pid' => $this->parentPlugin->getLinkTargetPageId()); } return $lastSearches; }
/** * Gets the user's query term and cleans it so that it can be used in * templates f.e. * * @return string The cleaned user query. */ public function getCleanUserQuery() { $userQuery = $this->getRawUserQuery(); if (!is_null($userQuery)) { $userQuery = Query::cleanKeywords($userQuery); } // escape triple hashes as they are used in the template engine // TODO remove after switching to fluid templates $userQuery = Template::escapeMarkers($userQuery); return $userQuery; }
/** * takes a search result document and processes its fields according to the * instructions configured in TS. Currently available instructions are * * timestamp - converts a date field into a unix timestamp * * serialize - uses serialize() to encode multivalue fields which then can be put out using the MULTIVALUE view helper * * skip - skips the whole field so that it is not available in the result, useful for the spell field f.e. * The default is to do nothing and just add the document's field to the * resulting array. * * @param \Apache_Solr_Document $document the Apache_Solr_Document result document * @return array An array with field values processed like defined in TS */ protected function processDocumentFieldsToArray(\Apache_Solr_Document $document) { $processingInstructions = $this->configuration->getSearchResultsFieldProcessingInstructionsConfiguration(); $availableFields = $document->getFieldNames(); $result = array(); foreach ($availableFields as $fieldName) { $processingInstruction = $processingInstructions[$fieldName]; // TODO switch to field processors // TODO allow to have multiple (comma-separated) instructions for each field switch ($processingInstruction) { case 'timestamp': $processedFieldValue = Util::isoToTimestamp($document->{$fieldName}); break; case 'serialize': if (!empty($document->{$fieldName})) { $processedFieldValue = serialize($document->{$fieldName}); } else { $processedFieldValue = ''; } break; case 'skip': continue 2; default: $processedFieldValue = $document->{$fieldName}; } // escape markers in document fields // TODO remove after switching to fluid templates $processedFieldValue = Template::escapeMarkers($processedFieldValue); $result[$fieldName] = $processedFieldValue; } return $result; }
/** * Post initialization of the template engine, adding some Solr variables. * * @param Template $template The template object as initialized thus far. * @return Template The modified template instance with additional variables available for rendering. */ protected function postInitializeTemplateEngine($template) { $template->addVariable('tx_solr', $this->getSolrVariables()); return $template; }
/** * Prepares the content for the last search markers * * @return array An array with content for the last search markers */ protected function getLastSearches() { /** @var $lastSearchesService \ApacheSolrForTypo3\Solr\Domain\Search\LastSearches\LastSearchesService */ $lastSearchesService = GeneralUtility::makeInstance('ApacheSolrForTypo3\\Solr\\Domain\\Search\\LastSearches\\LastSearchesService', $this->configuration, $GLOBALS['TSFE'], $GLOBALS['TYPO3_DB']); // fill array for output $lastSearches = array(); $lastSearchesKeywords = $lastSearchesService->getLastSearches(); foreach ($lastSearchesKeywords as $keywords) { $keywords = stripslashes($keywords); $lastSearches[] = array('q' => Template::escapeMarkers($keywords), 'parameters' => '&q=' . html_entity_decode($keywords, ENT_NOQUOTES, 'UTF-8'), 'pid' => $this->parentPlugin->getLinkTargetPageId()); } return $lastSearches; }
/** * @param string $viewHelperMarkerName * @param string $markup * @param array $expectedArguments * @dataProvider testExtractViewHelperArgumentsDataProvider */ public function testExtractViewHelperArguments($viewHelperMarkerName, $markup, $expectedArguments) { $result = Template::extractViewHelperArguments($viewHelperMarkerName, $markup); $this->assertSame($expectedArguments, $result); }