|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\ResultDocumentModifier; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2011-2015 Ingo Renner <[email protected]> |
|
8
|
|
|
* All rights reserved |
|
9
|
|
|
* |
|
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
|
11
|
|
|
* free software; you can redistribute it and/or modify |
|
12
|
|
|
* it under the terms of the GNU General Public License as published by |
|
13
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
14
|
|
|
* (at your option) any later version. |
|
15
|
|
|
* |
|
16
|
|
|
* The GNU General Public License can be found at |
|
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
|
18
|
|
|
* |
|
19
|
|
|
* This script is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
|
25
|
|
|
***************************************************************/ |
|
26
|
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\Plugin\Results\ResultsCommand; |
|
28
|
|
|
use ApacheSolrForTypo3\Solr\Search; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\Template; |
|
31
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Highlighting result document modifier, highlights query terms in result |
|
35
|
|
|
* documents. |
|
36
|
|
|
* |
|
37
|
|
|
* @author Ingo Renner <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class DocumentHighlighter implements ResultDocumentModifier |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var Search |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $search; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var TypoScriptConfiguration |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $configuration; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $highlightFields; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $fragmentSeparator; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* |
|
64
|
|
|
*/ |
|
65
|
18 |
|
public function __construct() |
|
66
|
|
|
{ |
|
67
|
18 |
|
$this->configuration = Util::getSolrConfiguration(); |
|
68
|
18 |
|
$this->highlightFields = $this->configuration->getSearchResultsHighlightingFieldsAsArray(); |
|
69
|
18 |
|
$this->fragmentSeparator = $this->configuration->getSearchResultsHighlightingFragmentSeparator(); |
|
70
|
18 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Modifies the given document and returns the modified document as result. |
|
74
|
|
|
* |
|
75
|
|
|
* @param ResultsCommand $resultCommand The search result command |
|
76
|
|
|
* @param array $resultDocument Result document as array |
|
77
|
|
|
* @return array The document with fields as array |
|
78
|
|
|
*/ |
|
79
|
18 |
|
public function modifyResultDocument( |
|
80
|
|
|
ResultsCommand $resultCommand, |
|
81
|
|
|
array $resultDocument |
|
82
|
|
|
) { |
|
83
|
18 |
|
$this->search = $resultCommand->getParentPlugin()->getSearchResultSetService()->getSearch(); |
|
84
|
|
|
|
|
85
|
18 |
|
$highlightedContent = $this->search->getHighlightedContent(); |
|
86
|
18 |
|
foreach ($this->highlightFields as $highlightField) { |
|
87
|
18 |
|
if (!empty($highlightedContent->{$resultDocument['id']}->{$highlightField}[0])) { |
|
88
|
16 |
|
$fragments = array(); |
|
89
|
16 |
|
foreach ($highlightedContent->{$resultDocument['id']}->{$highlightField} as $fragment) { |
|
90
|
16 |
|
$fragments[] = Template::escapeMarkers($fragment); |
|
91
|
|
|
} |
|
92
|
16 |
|
$resultDocument[$highlightField] = implode( |
|
93
|
18 |
|
' ' . $this->fragmentSeparator . ' ', |
|
94
|
|
|
$fragments |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
18 |
|
return $resultDocument; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|