|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace eZ\Publish\Core\Search\Legacy\Content\Location\Gateway; |
|
8
|
|
|
|
|
9
|
|
|
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter; |
|
10
|
|
|
use eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseConverter; |
|
11
|
|
|
use eZ\Publish\Core\Search\Legacy\Content\Location\Gateway; |
|
12
|
|
|
use eZ\Publish\Core\Persistence\Database\DatabaseHandler; |
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query; |
|
14
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
|
15
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler; |
|
16
|
|
|
use PDO; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Location gateway implementation using the Doctrine database. |
|
20
|
|
|
*/ |
|
21
|
|
|
class DoctrineDatabase extends Gateway |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* 2^30, since PHP_INT_MAX can cause overflows in DB systems, if PHP is run |
|
25
|
|
|
* on 64 bit systems. |
|
26
|
|
|
*/ |
|
27
|
|
|
const MAX_LIMIT = 1073741824; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Database handler. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $handler; |
|
35
|
|
|
|
|
36
|
|
|
/** @var \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter */ |
|
37
|
|
|
private $criteriaConverter; |
|
38
|
|
|
|
|
39
|
|
|
/** @var \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseConverter */ |
|
40
|
|
|
private $sortClauseConverter; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Language handler. |
|
44
|
|
|
* |
|
45
|
|
|
* @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $languageHandler; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Construct from database handler. |
|
51
|
|
|
* |
|
52
|
|
|
* @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $handler |
|
53
|
|
|
* @param \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter $criteriaConverter |
|
54
|
|
|
* @param \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseConverter $sortClauseConverter |
|
55
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
|
56
|
|
|
*/ |
|
57
|
|
View Code Duplication |
public function __construct( |
|
58
|
|
|
DatabaseHandler $handler, |
|
59
|
|
|
CriteriaConverter $criteriaConverter, |
|
60
|
|
|
SortClauseConverter $sortClauseConverter, |
|
61
|
|
|
LanguageHandler $languageHandler |
|
62
|
|
|
) { |
|
63
|
|
|
$this->handler = $handler; |
|
64
|
|
|
$this->criteriaConverter = $criteriaConverter; |
|
65
|
|
|
$this->sortClauseConverter = $sortClauseConverter; |
|
66
|
|
|
$this->languageHandler = $languageHandler; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns total count and data for all Locations satisfying the parameters. |
|
71
|
|
|
* |
|
72
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
|
73
|
|
|
* @param int $offset |
|
74
|
|
|
* @param int $limit |
|
75
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause[]|null $sortClauses |
|
76
|
|
|
* @param array $languageFilter |
|
77
|
|
|
* @param bool $doCount |
|
78
|
|
|
* |
|
79
|
|
|
* @return mixed[][] |
|
80
|
|
|
*/ |
|
81
|
|
|
public function find( |
|
82
|
|
|
Criterion $criterion, |
|
83
|
|
|
$offset, |
|
84
|
|
|
$limit, |
|
85
|
|
|
array $sortClauses = null, |
|
86
|
|
|
array $languageFilter = [], |
|
87
|
|
|
$doCount = true |
|
88
|
|
|
) { |
|
89
|
|
|
$count = $doCount ? $this->getTotalCount($criterion, $languageFilter) : null; |
|
90
|
|
|
|
|
91
|
|
|
if (!$doCount && $limit === 0) { |
|
92
|
|
|
throw new \RuntimeException('Invalid query. Cannot disable count and request 0 items at the same time.'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
if ($limit === 0 || ($count !== null && $count <= $offset)) { |
|
|
|
|
|
|
96
|
|
|
return ['count' => $count, 'rows' => []]; |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$selectQuery = $this->handler->createSelectQuery(); |
|
100
|
|
|
$selectQuery->select( |
|
101
|
|
|
'ezcontentobject_tree.*', |
|
102
|
|
|
$this->handler->quoteColumn('language_mask', 'ezcontentobject'), |
|
103
|
|
|
$this->handler->quoteColumn('initial_language_id', 'ezcontentobject') |
|
104
|
|
|
); |
|
105
|
|
|
|
|
106
|
|
|
if ($sortClauses !== null) { |
|
107
|
|
|
$this->sortClauseConverter->applySelect($selectQuery, $sortClauses); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$selectQuery |
|
111
|
|
|
->from($this->handler->quoteTable('ezcontentobject_tree')) |
|
112
|
|
|
->innerJoin( |
|
113
|
|
|
'ezcontentobject', |
|
114
|
|
|
'ezcontentobject_tree.contentobject_id', |
|
115
|
|
|
'ezcontentobject.id' |
|
116
|
|
|
) |
|
117
|
|
|
->innerJoin( |
|
118
|
|
|
'ezcontentobject_version', |
|
119
|
|
|
'ezcontentobject.id', |
|
120
|
|
|
'ezcontentobject_version.contentobject_id' |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
if ($sortClauses !== null) { |
|
124
|
|
|
$this->sortClauseConverter->applyJoin($selectQuery, $sortClauses, $languageFilter); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$selectQuery->where( |
|
128
|
|
|
$this->criteriaConverter->convertCriteria($selectQuery, $criterion, $languageFilter), |
|
129
|
|
|
$selectQuery->expr->eq( |
|
130
|
|
|
'ezcontentobject.status', |
|
131
|
|
|
//ContentInfo::STATUS_PUBLISHED |
|
132
|
|
|
$selectQuery->bindValue(1, null, PDO::PARAM_INT) |
|
133
|
|
|
), |
|
134
|
|
|
$selectQuery->expr->eq( |
|
135
|
|
|
'ezcontentobject_version.status', |
|
136
|
|
|
//VersionInfo::STATUS_PUBLISHED |
|
137
|
|
|
$selectQuery->bindValue(1, null, PDO::PARAM_INT) |
|
138
|
|
|
), |
|
139
|
|
|
$selectQuery->expr->neq( |
|
140
|
|
|
$this->handler->quoteColumn('depth', 'ezcontentobject_tree'), |
|
141
|
|
|
$selectQuery->bindValue(0, null, PDO::PARAM_INT) |
|
142
|
|
|
) |
|
143
|
|
|
); |
|
144
|
|
|
|
|
145
|
|
|
// If not main-languages query |
|
146
|
|
View Code Duplication |
if (!empty($languageFilter['languages'])) { |
|
|
|
|
|
|
147
|
|
|
$selectQuery->where( |
|
148
|
|
|
$selectQuery->expr->gt( |
|
149
|
|
|
$selectQuery->expr->bitAnd( |
|
150
|
|
|
$this->handler->quoteColumn('language_mask', 'ezcontentobject'), |
|
151
|
|
|
$selectQuery->bindValue( |
|
152
|
|
|
$this->getLanguageMask($languageFilter), |
|
153
|
|
|
null, |
|
154
|
|
|
PDO::PARAM_INT |
|
155
|
|
|
) |
|
156
|
|
|
), |
|
157
|
|
|
$selectQuery->bindValue(0, null, PDO::PARAM_INT) |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
if ($sortClauses !== null) { |
|
163
|
|
|
$this->sortClauseConverter->applyOrderBy($selectQuery); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$selectQuery->limit($limit, $offset); |
|
167
|
|
|
|
|
168
|
|
|
$statement = $selectQuery->prepare(); |
|
169
|
|
|
$statement->execute(); |
|
170
|
|
|
|
|
171
|
|
|
return [ |
|
|
|
|
|
|
172
|
|
|
'count' => $count, |
|
173
|
|
|
'rows' => $statement->fetchAll(PDO::FETCH_ASSOC), |
|
174
|
|
|
]; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Returns total results count for $criterion and $sortClauses. |
|
179
|
|
|
* |
|
180
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
|
181
|
|
|
* @param array $languageFilter |
|
182
|
|
|
* |
|
183
|
|
|
* @return array |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function getTotalCount(Criterion $criterion, array $languageFilter) |
|
186
|
|
|
{ |
|
187
|
|
|
$query = $this->handler->createSelectQuery(); |
|
188
|
|
|
$query |
|
189
|
|
|
->select($query->alias($query->expr->count('*'), 'count')) |
|
190
|
|
|
->from($this->handler->quoteTable('ezcontentobject_tree')) |
|
191
|
|
|
->innerJoin( |
|
192
|
|
|
'ezcontentobject', |
|
193
|
|
|
'ezcontentobject_tree.contentobject_id', |
|
194
|
|
|
'ezcontentobject.id' |
|
195
|
|
|
) |
|
196
|
|
|
->innerJoin( |
|
197
|
|
|
'ezcontentobject_version', |
|
198
|
|
|
'ezcontentobject.id', |
|
199
|
|
|
'ezcontentobject_version.contentobject_id' |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
$query->where( |
|
203
|
|
|
$this->criteriaConverter->convertCriteria($query, $criterion, $languageFilter), |
|
204
|
|
|
$query->expr->eq( |
|
205
|
|
|
'ezcontentobject.status', |
|
206
|
|
|
//ContentInfo::STATUS_PUBLISHED |
|
207
|
|
|
$query->bindValue(1, null, PDO::PARAM_INT) |
|
208
|
|
|
), |
|
209
|
|
|
$query->expr->eq( |
|
210
|
|
|
'ezcontentobject_version.status', |
|
211
|
|
|
//VersionInfo::STATUS_PUBLISHED |
|
212
|
|
|
$query->bindValue(1, null, PDO::PARAM_INT) |
|
213
|
|
|
), |
|
214
|
|
|
$query->expr->neq( |
|
215
|
|
|
$this->handler->quoteColumn('depth', 'ezcontentobject_tree'), |
|
216
|
|
|
$query->bindValue(0, null, PDO::PARAM_INT) |
|
217
|
|
|
) |
|
218
|
|
|
); |
|
219
|
|
|
|
|
220
|
|
|
// If not main-languages query |
|
221
|
|
View Code Duplication |
if (!empty($languageFilter['languages'])) { |
|
|
|
|
|
|
222
|
|
|
$query->where( |
|
223
|
|
|
$query->expr->gt( |
|
224
|
|
|
$query->expr->bitAnd( |
|
225
|
|
|
$this->handler->quoteColumn('language_mask', 'ezcontentobject'), |
|
226
|
|
|
$query->bindValue( |
|
227
|
|
|
$this->getLanguageMask($languageFilter), |
|
228
|
|
|
null, |
|
229
|
|
|
PDO::PARAM_INT |
|
230
|
|
|
) |
|
231
|
|
|
), |
|
232
|
|
|
$query->bindValue(0, null, PDO::PARAM_INT) |
|
233
|
|
|
) |
|
234
|
|
|
); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
$statement = $query->prepare(); |
|
238
|
|
|
$statement->execute(); |
|
239
|
|
|
|
|
240
|
|
|
return (int)$statement->fetchColumn(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* Generates a language mask from the given $languageFilter. |
|
245
|
|
|
* |
|
246
|
|
|
* @param array $languageFilter |
|
247
|
|
|
* |
|
248
|
|
|
* @return int |
|
249
|
|
|
*/ |
|
250
|
|
|
protected function getLanguageMask(array $languageFilter) |
|
251
|
|
|
{ |
|
252
|
|
|
if (!isset($languageFilter['languages'])) { |
|
253
|
|
|
$languageFilter['languages'] = []; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
if (!isset($languageFilter['useAlwaysAvailable'])) { |
|
257
|
|
|
$languageFilter['useAlwaysAvailable'] = true; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
$mask = 0; |
|
261
|
|
|
if ($languageFilter['useAlwaysAvailable']) { |
|
262
|
|
|
$mask |= 1; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
foreach ($languageFilter['languages'] as $languageCode) { |
|
266
|
|
|
$mask |= $this->languageHandler->loadByLanguageCode($languageCode)->id; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
return $mask; |
|
270
|
|
|
} |
|
271
|
|
|
} |
|
272
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.