first commit
This commit is contained in:
110
Classes/ViewHelpers/Document/HighlightResultViewHelper.php
Normal file
110
Classes/ViewHelpers/Document/HighlightResultViewHelper.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\ViewHelpers\Document;
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Result\SearchResult;
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSet;
|
||||
use WapplerSystems\Meilisearch\ViewHelpers\AbstractSolrFrontendViewHelper;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
|
||||
|
||||
/**
|
||||
* Class HighlightResultViewHelper
|
||||
*
|
||||
* @author Frans Saris <frans@beech.it>
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class HighlightResultViewHelper extends AbstractSolrFrontendViewHelper
|
||||
{
|
||||
use CompileWithRenderStatic;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $escapeOutput = false;
|
||||
|
||||
/**
|
||||
* Initializes the arguments
|
||||
*/
|
||||
public function initializeArguments()
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
|
||||
$this->registerArgument('document', SearchResult::class, 'The document to highlight', true);
|
||||
$this->registerArgument('fieldName', 'string', 'The fieldName', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $arguments
|
||||
* @param \Closure $renderChildrenClosure
|
||||
* @param RenderingContextInterface $renderingContext
|
||||
* @return string
|
||||
*/
|
||||
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
|
||||
{
|
||||
/** @var $resultSet SearchResultSet */
|
||||
$resultSet = $arguments['resultSet'];
|
||||
$fieldName = $arguments['fieldName'];
|
||||
$document = $arguments['document'];
|
||||
$content = self::getHighlightedContent($resultSet, $document, $fieldName);
|
||||
return self::escapeEverythingExceptAllowedTags($resultSet, $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SearchResultSet $resultSet
|
||||
* @param $document
|
||||
* @param $fieldName
|
||||
* @return mixed|string
|
||||
*/
|
||||
protected static function getHighlightedContent(SearchResultSet $resultSet, $document, $fieldName)
|
||||
{
|
||||
$fragmentSeparator = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchResultsHighlightingFragmentSeparator();
|
||||
|
||||
$content = call_user_func([$document, 'get' . $fieldName]);
|
||||
$highlightedContent = $resultSet->getUsedSearch()->getHighlightedContent();
|
||||
if (!empty($highlightedContent->{$document->getId()}->{$fieldName}[0])) {
|
||||
$content = implode(' ' . $fragmentSeparator . ' ', $highlightedContent->{$document->getId()}->{$fieldName});
|
||||
return $content;
|
||||
}
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SearchResultSet $resultSet
|
||||
* @param $content
|
||||
* @return string
|
||||
*/
|
||||
protected static function escapeEverythingExceptAllowedTags(SearchResultSet $resultSet, $content)
|
||||
{
|
||||
$wrap = $resultSet->getUsedSearchRequest()->getContextTypoScriptConfiguration()->getSearchResultsHighlightingWrap();
|
||||
if ($wrap === '') {
|
||||
return htmlspecialchars($content);
|
||||
}
|
||||
|
||||
$wrapParts = GeneralUtility::trimExplode("|", $wrap);
|
||||
if (count($wrapParts) !== 2) {
|
||||
return htmlspecialchars($content);
|
||||
}
|
||||
|
||||
$substitutedContent = str_replace($wrapParts[0], '___highlight_begin___', $content);
|
||||
$substitutedContent = str_replace($wrapParts[1], '___highlight_end___', $substitutedContent);
|
||||
$output = htmlspecialchars($substitutedContent);
|
||||
$output = str_replace('___highlight_begin___', $wrapParts[0], $output);
|
||||
$output = str_replace('___highlight_end___', $wrapParts[1], $output);
|
||||
|
||||
return $output;
|
||||
}
|
||||
}
|
72
Classes/ViewHelpers/Document/RelevanceViewHelper.php
Normal file
72
Classes/ViewHelpers/Document/RelevanceViewHelper.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace WapplerSystems\Meilisearch\ViewHelpers\Document;
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\Result\SearchResult;
|
||||
use WapplerSystems\Meilisearch\Domain\Search\ResultSet\SearchResultSet;
|
||||
use WapplerSystems\Meilisearch\ViewHelpers\AbstractSolrFrontendViewHelper;
|
||||
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
|
||||
use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
|
||||
|
||||
/**
|
||||
* Class RelevanceViewHelper
|
||||
*
|
||||
* @author Frans Saris <frans@beech.it>
|
||||
* @author Timo Hund <timo.hund@dkd.de>
|
||||
*/
|
||||
class RelevanceViewHelper extends AbstractSolrFrontendViewHelper
|
||||
{
|
||||
use CompileWithRenderStatic;
|
||||
|
||||
/**
|
||||
* Initializes the arguments
|
||||
*/
|
||||
public function initializeArguments()
|
||||
{
|
||||
parent::initializeArguments();
|
||||
$this->registerArgument('resultSet', SearchResultSet::class, 'The context searchResultSet', true);
|
||||
$this->registerArgument('document', SearchResult::class, 'The document to highlight', true);
|
||||
$this->registerArgument('maximumScore', 'float', 'The maximum score that should be used for percentage calculation, if nothing is passed the maximum from the resultSet is used', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $arguments
|
||||
* @param \Closure $renderChildrenClosure
|
||||
* @param RenderingContextInterface $renderingContext
|
||||
* @return string
|
||||
*/
|
||||
public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
|
||||
{
|
||||
/** @var $document SearchResult */
|
||||
$document = $arguments['document'];
|
||||
|
||||
/** @var $resultSet SearchResultSet */
|
||||
$resultSet = $arguments['resultSet'];
|
||||
|
||||
$maximumScore = $arguments['maximumScore'] ?? $resultSet->getMaximumScore();
|
||||
$content = 0;
|
||||
|
||||
if ($maximumScore <= 0) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$documentScore = $document->getScore();
|
||||
$score = floatval($documentScore);
|
||||
$multiplier = 100 / $maximumScore;
|
||||
$scorePercentage = round($score * $multiplier);
|
||||
$content = $scorePercentage;
|
||||
return $content;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user