Passed
Pull Request — release-11.2.x (#3594)
by Markus
14:43 queued 10:47
created

StatisticsWriterProcessor::getTSFE()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Domain\Search\Statistics;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2009-2015 Ingo Renner <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Search\Query\Query;
29
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSet;
30
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetProcessor;
31
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
32
use ApacheSolrForTypo3\Solr\HtmlContentExtractor;
33
use ApacheSolrForTypo3\Solr\Util;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Core\Utility\IpAnonymizationUtility;
36
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
37
38
/**
39
 * Writes statistics after searches have been conducted.
40
 *
41
 * @author Ingo Renner <[email protected]>
42
 * @author Dimitri Ebert <[email protected]>
43
 * @author Timo Hund <[email protected]>
44
 */
45
class StatisticsWriterProcessor implements SearchResultSetProcessor
46
{
47
    /**
48
     * @var StatisticsRepository
49
     */
50
    protected $statisticsRepository;
51
52
    /**
53
     * @var SiteRepository
54
     */
55
    protected $siteRepository;
56
57
    /**
58
     * @param StatisticsRepository $statisticsRepository
59
     * @param SiteRepository $siteRepository
60 1
     */
61
    public function __construct(StatisticsRepository $statisticsRepository = null, SiteRepository $siteRepository = null)
62 1
    {
63 1
        $this->statisticsRepository = $statisticsRepository ?? GeneralUtility::makeInstance(StatisticsRepository::class);
64 1
        $this->siteRepository = $siteRepository ?? GeneralUtility::makeInstance(SiteRepository::class);
65
    }
66
67
    /**
68
     * @param SearchResultSet $resultSet
69
     * @return SearchResultSet
70 1
     */
71 1
    public function process(SearchResultSet $resultSet)
72 1
    {
73 1
        $searchRequest = $resultSet->getUsedSearchRequest();
74 1
        $response = $resultSet->getResponse();
75
        $configuration = $searchRequest->getContextTypoScriptConfiguration();
76 1
        $keywords = $this->getProcessedKeywords($resultSet->getUsedQuery(), $configuration->getSearchFrequentSearchesUseLowercaseKeywords());
77
78
        if (empty($keywords)) {
79
            // do not track empty queries
80
            return $resultSet;
81 1
        }
82 1
83 1
        $filters = $searchRequest->getActiveFacets();
84 1
        $sorting = $this->sanitizeString($searchRequest->getSorting());
85
        $page = (int)$searchRequest->getPage();
86 1
        $ipMaskLength = (int)$configuration->getStatisticsAnonymizeIP();
87 1
88
        $TSFE = $this->getTSFE();
89 1
        $root_pid = $this->siteRepository->getSiteByPageId($TSFE->id)->getRootPageId();
0 ignored issues
show
Bug introduced by
$TSFE->id of type string is incompatible with the type integer expected by parameter $pageId of ApacheSolrForTypo3\Solr\...tory::getSiteByPageId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
        $root_pid = $this->siteRepository->getSiteByPageId(/** @scrutinizer ignore-type */ $TSFE->id)->getRootPageId();
Loading history...
90 1
        $statisticData = [
91 1
            'pid' => $TSFE->id,
92 1
            'root_pid' => $root_pid,
93
            'tstamp' => $this->getTime(),
94 1
            'language' => Util::getLanguageUid(),
95 1
            // @extensionScannerIgnoreLine
96
            'num_found' => (int)$resultSet->getAllResultCount(),
97 1
            'suggestions_shown' => is_object($response->spellcheck->suggestions) ? (int)get_object_vars($response->spellcheck->suggestions) : 0,
98
            // @extensionScannerIgnoreLine
99 1
            'time_total' => isset($response->debug->timing->time) ? $response->debug->timing->time : 0,
100
            // @extensionScannerIgnoreLine
101 1
            'time_preparation' => isset($response->debug->timing->prepare->time) ? $response->debug->timing->prepare->time : 0,
102 1
            // @extensionScannerIgnoreLine
103 1
            'time_processing' => isset($response->debug->timing->process->time) ? $response->debug->timing->process->time : 0,
104 1
            'feuser_id' => (int)$TSFE->fe_user->user['uid'],
105 1
            'cookie' => $TSFE->fe_user->id ?? '',
106 1
            'ip' => IpAnonymizationUtility::anonymizeIp($this->getUserIp(), $ipMaskLength),
107 1
            'page' => (int)$page,
108 1
            'keywords' => $keywords,
109 1
            'filters' => serialize($filters),
110
            'sorting' => $sorting,
111
            'parameters' => serialize($response->responseHeader->params),
112 1
        ];
113
114 1
        $this->statisticsRepository->saveStatisticsRecord($statisticData);
115
116
        return $resultSet;
117
    }
118
119
    /**
120
     * @param Query $query
121
     * @param bool $lowerCaseQuery
122 1
     * @return string
123
     */
124 1
    protected function getProcessedKeywords(Query $query, $lowerCaseQuery = false)
125 1
    {
126
        $keywords = $query->getQuery();
127 1
        $keywords = $this->sanitizeString($keywords);
128 1
        // Ensure string does not exceed database field length
129
        $keywords = substr($keywords, 0, 128);
130
        if ($lowerCaseQuery) {
131
            $keywords = mb_strtolower($keywords);
132 1
        }
133
134
        return $keywords;
135
    }
136
137
    /**
138
     * Sanitizes a string
139
     *
140
     * @param $string String to sanitize
141 1
     * @return string Sanitized string
142
     */
143
    protected function sanitizeString($string)
144 1
    {
145 1
        // clean content
146 1
        $string = HtmlContentExtractor::cleanContent($string);
147 1
        $string = html_entity_decode($string, ENT_QUOTES, 'UTF-8');
148
        $string = filter_var(strip_tags($string), FILTER_SANITIZE_STRING); // after entity decoding we might have tags again
149 1
        $string = trim($string);
150
151
        return $string;
152
    }
153
154
    /**
155
     * @return TypoScriptFrontendController
156
     */
157
    protected function getTSFE()
158
    {
159
        return $GLOBALS['TSFE'];
160
    }
161
162
    /**
163
     * @return string
164
     */
165
    protected function getUserIp()
166
    {
167
        return GeneralUtility::getIndpEnv('REMOTE_ADDR');
168
    }
169
170
    /**
171
     * @return mixed
172
     */
173
    protected function getTime()
174
    {
175
        return $GLOBALS['EXEC_TIME'];
176
    }
177
}
178