|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\Statistics; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2009-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 3 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\Domain\Search\Query\Query; |
|
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetProcessor; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\HtmlContentExtractor; |
|
31
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository; |
|
32
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
|
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
34
|
|
|
use TYPO3\CMS\Core\Utility\IpAnonymizationUtility; |
|
35
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Writes statistics after searches have been conducted. |
|
39
|
|
|
* |
|
40
|
|
|
* @author Ingo Renner <[email protected]> |
|
41
|
|
|
* @author Dimitri Ebert <[email protected]> |
|
42
|
|
|
* @author Timo Hund <[email protected]> |
|
43
|
|
|
*/ |
|
44
|
|
|
class StatisticsWriterProcessor implements SearchResultSetProcessor |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* @var StatisticsRepository |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $statisticsRepository; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var SiteRepository |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $siteRepository; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param StatisticsRepository $statisticsRepository |
|
58
|
|
|
* @param SiteRepository $siteRepository |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function __construct(StatisticsRepository $statisticsRepository = null, SiteRepository $siteRepository = null) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$this->statisticsRepository = $statisticsRepository ?? GeneralUtility::makeInstance(StatisticsRepository::class); |
|
63
|
1 |
|
$this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param SearchResultSet $resultSet |
|
68
|
|
|
* @return SearchResultSet |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function process(SearchResultSet $resultSet) { |
|
71
|
1 |
|
$searchRequest = $resultSet->getUsedSearchRequest(); |
|
72
|
1 |
|
$response = $resultSet->getResponse(); |
|
73
|
1 |
|
$configuration = $searchRequest->getContextTypoScriptConfiguration(); |
|
74
|
1 |
|
$keywords = $this->getProcessedKeywords($resultSet->getUsedQuery(), $configuration->getSearchFrequentSearchesUseLowercaseKeywords()); |
|
75
|
|
|
|
|
76
|
1 |
|
if (empty($keywords)) { |
|
77
|
|
|
// do not track empty queries |
|
78
|
|
|
return $resultSet; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$filters = $searchRequest->getActiveFacets(); |
|
82
|
1 |
|
$sorting = $this->sanitizeString($searchRequest->getSorting()); |
|
83
|
1 |
|
$page = (int)$searchRequest->getPage(); |
|
84
|
1 |
|
$ipMaskLength = (int)$configuration->getStatisticsAnonymizeIP(); |
|
85
|
|
|
|
|
86
|
1 |
|
$TSFE = $this->getTSFE(); |
|
87
|
1 |
|
$root_pid = $this->siteRepository->getSiteByPageId($TSFE->id)->getRootPageId(); |
|
|
|
|
|
|
88
|
|
|
$statisticData = [ |
|
89
|
1 |
|
'pid' => $TSFE->id, |
|
90
|
1 |
|
'root_pid' => $root_pid, |
|
91
|
1 |
|
'tstamp' => $this->getTime(), |
|
92
|
1 |
|
'language' => Util::getLanguageUid(), |
|
93
|
|
|
// @extensionScannerIgnoreLine |
|
94
|
1 |
|
'num_found' => (int)$resultSet->getAllResultCount(), |
|
95
|
1 |
|
'suggestions_shown' => is_object($response->spellcheck->suggestions) ? (int)get_object_vars($response->spellcheck->suggestions) : 0, |
|
96
|
|
|
// @extensionScannerIgnoreLine |
|
97
|
1 |
|
'time_total' => isset($response->debug->timing->time) ? $response->debug->timing->time : 0, |
|
98
|
|
|
// @extensionScannerIgnoreLine |
|
99
|
1 |
|
'time_preparation' => isset($response->debug->timing->prepare->time) ? $response->debug->timing->prepare->time : 0, |
|
100
|
|
|
// @extensionScannerIgnoreLine |
|
101
|
1 |
|
'time_processing' => isset($response->debug->timing->process->time) ? $response->debug->timing->process->time : 0, |
|
102
|
1 |
|
'feuser_id' => (int)$TSFE->fe_user->user['uid'], |
|
103
|
1 |
|
'cookie' => $TSFE->fe_user->id ?? '', |
|
104
|
1 |
|
'ip' => IpAnonymizationUtility::anonymizeIp($this->getUserIp(), $ipMaskLength), |
|
105
|
1 |
|
'page' => (int)$page, |
|
106
|
1 |
|
'keywords' => $keywords, |
|
107
|
1 |
|
'filters' => serialize($filters), |
|
108
|
1 |
|
'sorting' => $sorting, |
|
109
|
1 |
|
'parameters' => serialize($response->responseHeader->params) |
|
110
|
|
|
]; |
|
111
|
|
|
|
|
112
|
1 |
|
$this->statisticsRepository->saveStatisticsRecord($statisticData); |
|
113
|
|
|
|
|
114
|
1 |
|
return $resultSet; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param Query $query |
|
119
|
|
|
* @param boolean $lowerCaseQuery |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
1 |
|
protected function getProcessedKeywords(Query $query, $lowerCaseQuery = false) |
|
123
|
|
|
{ |
|
124
|
1 |
|
$keywords = $query->getQuery(); |
|
125
|
1 |
|
$keywords = $this->sanitizeString($keywords); |
|
126
|
|
|
// Ensure string does not exceed database field length |
|
127
|
1 |
|
$keywords = substr($keywords, 0, 128); |
|
128
|
1 |
|
if ($lowerCaseQuery) { |
|
129
|
|
|
$keywords = mb_strtolower($keywords); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
return $keywords; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Sanitizes a string |
|
137
|
|
|
* |
|
138
|
|
|
* @param $string String to sanitize |
|
139
|
|
|
* @return string Sanitized string |
|
140
|
|
|
*/ |
|
141
|
1 |
|
protected function sanitizeString($string) |
|
142
|
|
|
{ |
|
143
|
|
|
// clean content |
|
144
|
1 |
|
$string = HtmlContentExtractor::cleanContent($string); |
|
145
|
1 |
|
$string = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); |
|
146
|
1 |
|
$string = filter_var(strip_tags($string), FILTER_SANITIZE_STRING); // after entity decoding we might have tags again |
|
147
|
1 |
|
$string = trim($string); |
|
148
|
|
|
|
|
149
|
1 |
|
return $string; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return TypoScriptFrontendController |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function getTSFE() |
|
156
|
|
|
{ |
|
157
|
|
|
return $GLOBALS['TSFE']; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getUserIp() |
|
164
|
|
|
{ |
|
165
|
|
|
return GeneralUtility::getIndpEnv('REMOTE_ADDR'); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return mixed |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getTime() |
|
172
|
|
|
{ |
|
173
|
|
|
return $GLOBALS['EXEC_TIME']; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|