|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\FrequentSearches; |
|
4
|
|
|
|
|
5
|
|
|
/*************************************************************** |
|
6
|
|
|
* Copyright notice |
|
7
|
|
|
* |
|
8
|
|
|
* (c) 2015-2016 Timo Schmidt <[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 2 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\System\Configuration\TypoScriptConfiguration; |
|
29
|
|
|
use TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend; |
|
30
|
|
|
use TYPO3\CMS\Core\Database\DatabaseConnection; |
|
31
|
|
|
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The FrequentSearchesService is used to retrieve the frequent searches from the database or cache. |
|
35
|
|
|
* |
|
36
|
|
|
* @author Dimitri Ebert <[email protected]> |
|
37
|
|
|
* @author Timo Schmidt <[email protected]> |
|
38
|
|
|
*/ |
|
39
|
|
|
class FrequentSearchesService |
|
40
|
|
|
{ |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Instance of the caching frontend used to cache this command's output. |
|
44
|
|
|
* |
|
45
|
|
|
* @var AbstractFrontend |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $cache; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var TypoScriptFrontendController |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $tsfe; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var DatabaseConnection |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $database; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @var TypoScriptConfiguration |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $configuration; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param TypoScriptConfiguration $typoscriptConfiguration |
|
66
|
|
|
* @param AbstractFrontend $cache |
|
67
|
|
|
* @param TypoScriptFrontendController $tsfe |
|
68
|
|
|
* @param DatabaseConnection $database |
|
69
|
|
|
*/ |
|
70
|
25 |
|
public function __construct(TypoScriptConfiguration $typoscriptConfiguration, AbstractFrontend $cache, TypoScriptFrontendController $tsfe, DatabaseConnection $database) |
|
71
|
|
|
{ |
|
72
|
25 |
|
$this->configuration = $typoscriptConfiguration; |
|
73
|
25 |
|
$this->cache = $cache; |
|
74
|
25 |
|
$this->tsfe = $tsfe; |
|
75
|
25 |
|
$this->database = $database; |
|
76
|
25 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Generates an array with terms and hits |
|
80
|
|
|
* |
|
81
|
|
|
* @return array Tags as array with terms and hits |
|
82
|
|
|
*/ |
|
83
|
25 |
|
public function getFrequentSearchTerms() |
|
84
|
|
|
{ |
|
85
|
25 |
|
$frequentSearchConfiguration = $this->configuration->getSearchFrequentSearchesConfiguration(); |
|
86
|
|
|
|
|
87
|
25 |
|
$identifier = $this->getCacheIdentifier($frequentSearchConfiguration); |
|
88
|
|
|
|
|
89
|
25 |
|
if ($this->cache->has($identifier)) { |
|
90
|
4 |
|
$terms = $this->cache->get($identifier); |
|
91
|
|
|
} else { |
|
92
|
24 |
|
$terms = $this->getFrequentSearchTermsFromStatistics($frequentSearchConfiguration); |
|
93
|
|
|
|
|
94
|
24 |
|
if ($frequentSearchConfiguration['sortBy'] == 'hits') { |
|
95
|
|
|
arsort($terms); |
|
96
|
|
|
} else { |
|
97
|
24 |
|
ksort($terms); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
24 |
|
$lifetime = null; |
|
101
|
24 |
|
if (isset($frequentSearchConfiguration['cacheLifetime'])) { |
|
102
|
23 |
|
$lifetime = intval($frequentSearchConfiguration['cacheLifetime']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
24 |
|
$this->cache->set($identifier, $terms, [], $lifetime); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
25 |
|
return $terms; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Gets frequent search terms from the statistics tracking table. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $frequentSearchConfiguration |
|
115
|
|
|
* @return array Array of frequent search terms, keys are the terms, values are hits |
|
116
|
|
|
*/ |
|
117
|
24 |
|
protected function getFrequentSearchTermsFromStatistics($frequentSearchConfiguration) |
|
118
|
|
|
{ |
|
119
|
24 |
|
$terms = []; |
|
120
|
|
|
|
|
121
|
24 |
|
if ($frequentSearchConfiguration['select.']['checkRootPageId']) { |
|
122
|
1 |
|
$checkRootPidWhere = 'root_pid = ' . $this->tsfe->tmpl->rootLine[0]['uid']; |
|
123
|
|
|
} else { |
|
124
|
23 |
|
$checkRootPidWhere = '1'; |
|
125
|
|
|
} |
|
126
|
24 |
|
if ($frequentSearchConfiguration['select.']['checkLanguage']) { |
|
127
|
1 |
|
$checkLanguageWhere = ' AND language =' . $this->tsfe->sys_language_uid; |
|
128
|
|
|
} else { |
|
129
|
23 |
|
$checkLanguageWhere = ''; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
24 |
|
$frequentSearchConfiguration['select.']['ADD_WHERE'] = $checkRootPidWhere . |
|
133
|
24 |
|
$checkLanguageWhere . ' ' . |
|
134
|
24 |
|
$frequentSearchConfiguration['select.']['ADD_WHERE']; |
|
135
|
|
|
|
|
136
|
|
|
/** @noinspection PhpUndefinedMethodInspection */ |
|
137
|
24 |
|
$frequentSearchTerms = $this->database->exec_SELECTgetRows( |
|
138
|
24 |
|
$frequentSearchConfiguration['select.']['SELECT'], |
|
139
|
24 |
|
$frequentSearchConfiguration['select.']['FROM'], |
|
140
|
24 |
|
$frequentSearchConfiguration['select.']['ADD_WHERE'], |
|
141
|
24 |
|
$frequentSearchConfiguration['select.']['GROUP_BY'], |
|
142
|
24 |
|
$frequentSearchConfiguration['select.']['ORDER_BY'], |
|
143
|
24 |
|
$frequentSearchConfiguration['limit'] |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
24 |
|
if (!is_array($frequentSearchTerms)) { |
|
147
|
|
|
return $terms; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
24 |
|
foreach ($frequentSearchTerms as $term) { |
|
151
|
24 |
|
$cleanedTerm = html_entity_decode($term['search_term'], ENT_QUOTES, 'UTF-8'); |
|
152
|
24 |
|
$terms[$cleanedTerm] = $term['hits']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
24 |
|
return $terms; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param array $frequentSearchConfiguration |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
25 |
|
protected function getCacheIdentifier(array $frequentSearchConfiguration) |
|
163
|
|
|
{ |
|
164
|
|
|
// Use configuration as cache identifier |
|
165
|
25 |
|
$identifier = 'frequentSearchesTags'; |
|
166
|
|
|
|
|
167
|
25 |
|
if ($frequentSearchConfiguration['select.']['checkRootPageId']) { |
|
168
|
1 |
|
$identifier .= '_RP' . (int)$this->tsfe->tmpl->rootLine[0]['uid']; |
|
169
|
|
|
} |
|
170
|
25 |
|
if ($frequentSearchConfiguration['select.']['checkLanguage']) { |
|
171
|
1 |
|
$identifier .= '_L' . (int)$this->tsfe->sys_language_uid; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
25 |
|
$identifier .= '_' . md5(serialize($frequentSearchConfiguration)); |
|
175
|
25 |
|
return $identifier; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|