1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\LastSearches; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2010-2017 dkd Internet Service GmbH <[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\System\Records\AbstractRepository; |
28
|
|
|
|
29
|
|
|
class LastSearchesRepository extends AbstractRepository |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $table = 'tx_solr_last_searches'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \TYPO3\CMS\Core\Database\DatabaseConnection |
38
|
|
|
*/ |
39
|
|
|
protected $database; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Finds the last searched keywords from the database |
43
|
|
|
* |
44
|
|
|
* @param int $limit |
45
|
|
|
* @return array An array containing the last searches of the current user |
46
|
|
|
*/ |
47
|
|
|
public function findAllKeywords($limit = 10) : array |
48
|
|
|
{ |
49
|
|
|
$lastSearchesResultSet = $this->getLastSearchesResultSet($limit); |
50
|
|
|
if (empty($lastSearchesResultSet)) { |
51
|
|
|
return []; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$lastSearches = []; |
55
|
|
|
foreach ($lastSearchesResultSet as $row) { |
56
|
|
|
$lastSearches[] = html_entity_decode($row['keywords'], ENT_QUOTES, 'UTF-8'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $lastSearches; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns all last searches |
64
|
|
|
* |
65
|
|
|
* @param $limit |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
|
|
protected function getLastSearchesResultSet($limit) : array |
69
|
|
|
{ |
70
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
71
|
|
|
return $queryBuilder |
72
|
|
|
->select('keywords') |
73
|
|
|
->from($this->table) |
74
|
|
|
// There is no support for DISTINCT, a ->groupBy() has to be used instead. |
75
|
|
|
->groupBy('keywords') |
76
|
|
|
->orderBy('tstamp', 'DESC') |
77
|
|
|
->setMaxResults($limit)->execute()->fetchAll(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Adds keywords to last searches or updates the oldest row by given limit. |
82
|
|
|
* |
83
|
|
|
* @param string $lastSearchesKeywords |
84
|
|
|
* @param int $lastSearchesLimit |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function add(string $lastSearchesKeywords, int $lastSearchesLimit) |
88
|
|
|
{ |
89
|
|
|
$nextSequenceId = $this->resolveNextSequenceIdForGivenLimit($lastSearchesLimit); |
90
|
|
|
$rowsCount = $this->count(); |
91
|
|
|
if ($nextSequenceId < $rowsCount) { |
92
|
|
|
$this->update([ |
93
|
|
|
'sequence_id' => $nextSequenceId, |
94
|
|
|
'keywords' => $lastSearchesKeywords |
95
|
|
|
]); |
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
100
|
|
|
$queryBuilder |
101
|
|
|
->insert($this->table) |
102
|
|
|
->values([ |
103
|
|
|
'sequence_id' => $nextSequenceId, |
104
|
|
|
'keywords' => $lastSearchesKeywords, |
105
|
|
|
'tstamp' => time() |
106
|
|
|
]) |
107
|
|
|
->execute(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Resolves next sequence id by given last searches limit. |
112
|
|
|
* |
113
|
|
|
* @param int $lastSearchesLimit |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
|
|
protected function resolveNextSequenceIdForGivenLimit(int $lastSearchesLimit) : int |
117
|
|
|
{ |
118
|
|
|
$nextSequenceId = 0; |
119
|
|
|
|
120
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
121
|
|
|
$result = $queryBuilder->select('sequence_id') |
122
|
|
|
->from($this->table) |
123
|
|
|
->orderBy('tstamp', 'DESC') |
124
|
|
|
->setMaxResults(1) |
125
|
|
|
->execute()->fetch(); |
126
|
|
|
|
127
|
|
|
if (!empty($result)) { |
128
|
|
|
$nextSequenceId = ($result['sequence_id'] + 1) % $lastSearchesLimit; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $nextSequenceId; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Updates last searches row by using sequence_id from given $lastSearchesRow array |
136
|
|
|
* |
137
|
|
|
* @param array $lastSearchesRow |
138
|
|
|
* @return void |
139
|
|
|
* @throws \InvalidArgumentException |
140
|
|
|
*/ |
141
|
|
|
protected function update(array $lastSearchesRow) |
142
|
|
|
{ |
143
|
|
|
$queryBuilder = $this->getQueryBuilder(); |
144
|
|
|
|
145
|
|
|
$affectedRows = $queryBuilder |
146
|
|
|
->update($this->table) |
147
|
|
|
->where( |
148
|
|
|
$queryBuilder->expr()->eq('sequence_id', $queryBuilder->createNamedParameter($lastSearchesRow['sequence_id'])) |
149
|
|
|
) |
150
|
|
|
->set('tstamp', time()) |
151
|
|
|
->set('keywords', $lastSearchesRow['keywords']) |
152
|
|
|
->execute(); |
153
|
|
|
|
154
|
|
|
if ($affectedRows < 1) { |
155
|
|
|
throw new \InvalidArgumentException(vsprintf('By trying to update last searches row with values "%s" nothing was updated, make sure the given "sequence_id" exists in database.', [\json_encode($lastSearchesRow)]), 1502717923); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|