forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchService.php
317 lines (283 loc) · 11.7 KB
/
SearchService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* File containing the eZ\Publish\Core\Repository\SearchService class.
*
* @copyright Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\SearchService as SearchServiceInterface;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOperator;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Location as LocationCriterion;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
use eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location as LocationSortClause;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\LocationQuery;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\SPI\Persistence\Content\Search\Handler;
use eZ\Publish\SPI\Persistence\Content\Location\Search\Handler as LocationSearchHandler;
/**
* Search service
*
* @package eZ\Publish\Core\Repository
*/
class SearchService implements SearchServiceInterface
{
/**
* 2^30, since PHP_INT_MAX can cause overflows in DB systems, if PHP is run
* on 64 bit systems
*/
const MAX_LIMIT = 1073741824;
/**
* @var \eZ\Publish\Core\Repository\Repository
*/
protected $repository;
/**
* @var \eZ\Publish\SPI\Persistence\Content\Search\Handler
*/
protected $searchHandler;
/**
* @var \eZ\Publish\SPI\Persistence\Content\Location\Search\Handler
*/
protected $locationSearchHandler;
/**
* @var array
*/
protected $settings;
/**
* @var \eZ\Publish\Core\Repository\DomainMapper
*/
protected $domainMapper;
/**
* @var \eZ\Publish\Core\Repository\PermissionsCriterionHandler
*/
protected $permissionsCriterionHandler;
/**
* Setups service with reference to repository object that created it & corresponding handler
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\SPI\Persistence\Content\Search\Handler $searchHandler
* @param \eZ\Publish\SPI\Persistence\Content\Location\Search\Handler $locationSearchHandler
* @param \eZ\Publish\Core\Repository\DomainMapper $domainMapper
* @param \eZ\Publish\Core\Repository\PermissionsCriterionHandler $permissionsCriterionHandler
* @param array $settings
*/
public function __construct(
RepositoryInterface $repository,
Handler $searchHandler,
LocationSearchHandler $locationSearchHandler,
DomainMapper $domainMapper,
PermissionsCriterionHandler $permissionsCriterionHandler,
array $settings = array()
)
{
$this->repository = $repository;
$this->searchHandler = $searchHandler;
$this->locationSearchHandler = $locationSearchHandler;
$this->domainMapper = $domainMapper;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + array(
//'defaultSetting' => array(),
);
$this->permissionsCriterionHandler = $permissionsCriterionHandler;
}
/**
* Finds content objects for the given query.
*
* @todo define structs for the field filters
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid
*
* @param \eZ\Publish\API\Repository\Values\Content\Query $query
* @param array $fieldFilters - a map of filters for the returned fields.
* Currently supported: <code>array("languages" => array(<language1>,..))</code>.
* @param boolean $filterOnUserPermissions if true only the objects which is the user allowed to read are returned.
*
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult
*/
public function findContent( Query $query, array $fieldFilters = array(), $filterOnUserPermissions = true )
{
$query = clone $query;
$query->filter = $query->filter ?: new Criterion\MatchAll();
$this->validateContentCriteria( array( $query->query ), "\$query" );
$this->validateContentCriteria( array( $query->filter ), "\$query" );
$this->validateContentSortClauses( $query );
$this->validateSortClauses( $query );
if ( $filterOnUserPermissions && !$this->permissionsCriterionHandler->addPermissionsCriterion( $query->filter ) )
{
return new SearchResult( array( 'time' => 0, 'totalCount' => 0 ) );
}
if ( $query->limit === null )
{
$query->limit = self::MAX_LIMIT;
}
$result = $this->searchHandler->findContent( $query, $fieldFilters );
foreach ( $result->searchHits as $hit )
{
$hit->valueObject = $this->domainMapper->buildContentDomainObject(
$hit->valueObject
);
}
return $result;
}
/**
* Checks that $criteria does not contain Location criterions.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion[] $criteria
* @param string $argumentName
*/
protected function validateContentCriteria( array $criteria, $argumentName )
{
foreach ( $criteria as $criterion )
{
if ( $criterion instanceof LocationCriterion )
{
throw new InvalidArgumentException(
$argumentName, "Location criterions cannot be used in Content search"
);
}
if ( $criterion instanceof LogicalOperator )
{
$this->validateContentCriteria( $criterion->criteria, $argumentName );
}
}
}
/**
* Checks that $query does not contain Location sort clauses.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param \eZ\Publish\API\Repository\Values\Content\Query $query
*/
protected function validateContentSortClauses( Query $query )
{
foreach ( $query->sortClauses as $sortClause )
{
if ( $sortClause instanceof LocationSortClause )
{
throw new InvalidArgumentException(
"\$query", "Location sort clauses cannot be used in Content search"
);
}
}
}
/**
* Validates sort clauses of a given $query.
*
* For the moment this validates only Field sort clauses.
* Valid Field sort clause provides $languageCode if targeted field is translatable,
* and the same in reverse - it does not provide $languageCode for non-translatable field.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If sort clauses are not valid
*
* @param \eZ\Publish\API\Repository\Values\Content\Query $query
*
* @return void
*/
protected function validateSortClauses( Query $query )
{
foreach ( $query->sortClauses as $key => $sortClause )
{
if ( !$sortClause instanceof SortClause\Field && !$sortClause instanceof SortClause\MapLocationDistance )
{
continue;
}
/** @var \eZ\Publish\API\Repository\Values\Content\Query\SortClause\Target\FieldTarget|\eZ\Publish\API\Repository\Values\Content\Query\SortClause\Target\MapLocationTarget $fieldTarget */
$fieldTarget = $sortClause->targetData;
$contentType = $this->repository->getContentTypeService()->loadContentTypeByIdentifier(
$fieldTarget->typeIdentifier
);
if ( $contentType->getFieldDefinition( $fieldTarget->fieldIdentifier )->isTranslatable )
{
if ( $fieldTarget->languageCode === null )
{
throw new InvalidArgumentException(
"\$query->sortClauses[{$key}]", "No language is specified for translatable field"
);
}
}
else if ( $fieldTarget->languageCode !== null )
{
throw new InvalidArgumentException(
"\$query->sortClauses[{$key}]", "Language is specified for non-translatable field, null should be used instead"
);
}
}
}
/**
* Performs a query for a single content object
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than one result matching the criterions
*
* @todo define structs for the field filters
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter
* @param array $fieldFilters - a map of filters for the returned fields.
* Currently supported: <code>array("languages" => array(<language1>,..))</code>.
* @param boolean $filterOnUserPermissions if true only the objects which is the user allowed to read are returned.
*
* @return \eZ\Publish\API\Repository\Values\Content\Content
*/
public function findSingle( Criterion $filter, array $fieldFilters = array(), $filterOnUserPermissions = true )
{
$this->validateContentCriteria( array( $filter ), "\$filter" );
if ( $filterOnUserPermissions && !$this->permissionsCriterionHandler->addPermissionsCriterion( $filter ) )
{
throw new NotFoundException( 'Content', '*' );
}
return $this->domainMapper->buildContentDomainObject(
$this->searchHandler->findSingle( $filter, $fieldFilters )
);
}
/**
* Suggests a list of values for the given prefix
*
* @param string $prefix
* @param string[] $fieldPaths
* @param int $limit
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter
*/
public function suggest( $prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null )
{
}
/**
* Finds Locations for the given query.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid
*
* @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query
* @param boolean $filterOnUserPermissions if true only the objects which is the user allowed to read are returned.
*
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult
*/
public function findLocations( LocationQuery $query, $filterOnUserPermissions = true )
{
$query = clone $query;
$query->filter = $query->filter ?: new Criterion\MatchAll();
$this->validateSortClauses( $query );
if ( $filterOnUserPermissions && !$this->permissionsCriterionHandler->addPermissionsCriterion( $query->filter ) )
{
return new SearchResult( array( 'time' => 0, 'totalCount' => 0 ) );
}
if ( $query->limit === null )
{
$query->limit = self::MAX_LIMIT;
}
$result = $this->locationSearchHandler->findLocations( $query );
foreach ( $result->searchHits as $hit )
{
$hit->valueObject = $this->domainMapper->buildLocationDomainObject(
$hit->valueObject
);
}
return $result;
}
}