1 | <?php |
||
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) |
|
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) |
|
177 | } |
||
178 |