Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/Repository/SearchService.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Repository;
8
9 use eZ\Publish\API\Repository\SearchService as SearchServiceInterface;
10 use eZ\Publish\API\Repository\PermissionCriterionResolver;
11 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
12 use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd;
13 use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOperator;
14 use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Location as LocationCriterion;
15 use eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location as LocationSortClause;
16 use eZ\Publish\API\Repository\Values\Content\Query;
17 use eZ\Publish\API\Repository\Values\Content\LocationQuery;
18 use eZ\Publish\API\Repository\Repository as RepositoryInterface;
19 use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
20 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
21 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
22 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
23 use eZ\Publish\SPI\Search\Capable;
24 use eZ\Publish\Core\Search\Common\BackgroundIndexer;
25 use eZ\Publish\SPI\Search\Handler;
26
30 class SearchService implements SearchServiceInterface
31 {
33 protected $repository;
34
36 protected $searchHandler;
37
39 protected $settings;
40
42 protected $domainMapper;
43
45 protected $permissionCriterionResolver;
46
48 protected $backgroundIndexer;
49
60 public function __construct(
61 RepositoryInterface $repository,
62 Handler $searchHandler,
63 Helper\DomainMapper $domainMapper,
64 PermissionCriterionResolver $permissionCriterionResolver,
65 BackgroundIndexer $backgroundIndexer,
66 array $settings = []
67 ) {
68 $this->repository = $repository;
69 $this->searchHandler = $searchHandler;
70 $this->domainMapper = $domainMapper;
71 // Union makes sure default settings are ignored if provided in argument
72 $this->settings = $settings + [
73 //'defaultSetting' => array(),
74 ];
75 $this->permissionCriterionResolver = $permissionCriterionResolver;
76 $this->backgroundIndexer = $backgroundIndexer;
77 }
78
92 public function findContent(Query $query, array $languageFilter = [], $filterOnUserPermissions = true)
93 {
94 $result = $this->internalFindContentInfo($query, $languageFilter, $filterOnUserPermissions);
95 $missingContentList = $this->domainMapper->buildContentDomainObjectsOnSearchResult($result, $languageFilter);
96 foreach ($missingContentList as $missingContent) {
97 $this->backgroundIndexer->registerContent($missingContent);
98 }
99
100 return $result;
101 }
102
119 public function findContentInfo(Query $query, array $languageFilter = [], $filterOnUserPermissions = true)
120 {
121 $result = $this->internalFindContentInfo($query, $languageFilter, $filterOnUserPermissions);
122 foreach ($result->searchHits as $hit) {
123 $hit->valueObject = $this->domainMapper->buildContentInfoDomainObject(
124 $hit->valueObject
125 );
126 }
127
128 return $result;
129 }
130
146 protected function internalFindContentInfo(Query $query, array $languageFilter = [], $filterOnUserPermissions = true)
147 {
148 if (!is_int($query->offset)) {
149 throw new InvalidArgumentType(
150 '$query->offset',
151 'integer',
152 $query->offset
153 );
154 }
155
156 if (!is_int($query->limit)) {
157 throw new InvalidArgumentType(
158 '$query->limit',
159 'integer',
160 $query->limit
161 );
162 }
163
164 $query = clone $query;
165 $query->filter = $query->filter ?: new Criterion\MatchAll();
166
167 $this->validateContentCriteria([$query->query], '$query');
168 $this->validateContentCriteria([$query->filter], '$query');
169 $this->validateContentSortClauses($query);
170
171 if ($filterOnUserPermissions && !$this->addPermissionsCriterion($query->filter)) {
172 return new SearchResult(['time' => 0, 'totalCount' => 0]);
173 }
174
175 return $this->searchHandler->findContent($query, $languageFilter);
176 }
177
186 protected function validateContentCriteria(array $criteria, $argumentName)
187 {
188 foreach ($criteria as $criterion) {
189 if ($criterion instanceof LocationCriterion) {
190 throw new InvalidArgumentException(
191 $argumentName,
192 'Location criterions cannot be used in Content search'
193 );
194 }
195 if ($criterion instanceof LogicalOperator) {
196 $this->validateContentCriteria($criterion->criteria, $argumentName);
197 }
198 }
199 }
200
208 protected function validateContentSortClauses(Query $query)
209 {
210 foreach ($query->sortClauses as $sortClause) {
211 if ($sortClause instanceof LocationSortClause) {
212 throw new InvalidArgumentException('$query', 'Location sort clauses cannot be used in Content search');
213 }
214 }
215 }
216
232 public function findSingle(Criterion $filter, array $languageFilter = [], $filterOnUserPermissions = true)
233 {
234 $this->validateContentCriteria([$filter], '$filter');
235
236 if ($filterOnUserPermissions && !$this->addPermissionsCriterion($filter)) {
237 throw new NotFoundException('Content', '*');
238 }
239
240 $contentInfo = $this->searchHandler->findSingle($filter, $languageFilter);
241
242 return $this->repository->getContentService()->internalLoadContent(
243 $contentInfo->id,
244 (!empty($languageFilter['languages']) ? $languageFilter['languages'] : null),
245 null,
246 false,
247 (isset($languageFilter['useAlwaysAvailable']) ? $languageFilter['useAlwaysAvailable'] : true)
248 );
249 }
250
259 public function suggest($prefix, $fieldPaths = [], $limit = 10, Criterion $filter = null)
260 {
261 }
262
276 public function findLocations(LocationQuery $query, array $languageFilter = [], $filterOnUserPermissions = true)
277 {
278 if (!is_int($query->offset)) {
279 throw new InvalidArgumentType(
280 '$query->offset',
281 'integer',
282 $query->offset
283 );
284 }
285
286 if (!is_int($query->limit)) {
287 throw new InvalidArgumentType(
288 '$query->limit',
289 'integer',
290 $query->limit
291 );
292 }
293
294 $query = clone $query;
295 $query->filter = $query->filter ?: new Criterion\MatchAll();
296
297 if ($filterOnUserPermissions && !$this->addPermissionsCriterion($query->filter)) {
298 return new SearchResult(['time' => 0, 'totalCount' => 0]);
299 }
300
301 $result = $this->searchHandler->findLocations($query, $languageFilter);
302
303 $missingLocations = $this->domainMapper->buildLocationDomainObjectsOnSearchResult($result, $languageFilter);
304 foreach ($missingLocations as $missingLocation) {
305 $this->backgroundIndexer->registerLocation($missingLocation);
306 }
307
308 return $result;
309 }
310
320 protected function addPermissionsCriterion(Criterion &$criterion)
321 {
322 $permissionCriterion = $this->permissionCriterionResolver->getPermissionsCriterion('content', 'read');
323 if ($permissionCriterion === true || $permissionCriterion === false) {
324 return $permissionCriterion;
325 }
326
327 // Merge with original $criterion
328 if ($criterion instanceof LogicalAnd) {
329 $criterion->criteria[] = $permissionCriterion;
330 } else {
331 $criterion = new LogicalAnd(
332 [
333 $criterion,
334 $permissionCriterion,
335 ]
336 );
337 }
338
339 return true;
340 }
341
342 public function supports($capabilityFlag)
343 {
344 if ($this->searchHandler instanceof Capable) {
345 return $this->searchHandler->supports($capabilityFlag);
346 }
347
348 return false;
349 }
350 }
eZ\Publish\API\Repository\Exceptions\NotFoundException
This Exception is thrown if an object referenced by an id or identifier could not be found in the rep...
Definition: API/Repository/Exceptions/NotFoundException.php:16
eZ\Publish\API\Repository\Values\Content\LocationQuery
This class is used to perform a Location query.
Definition: API/Repository/Values/Content/LocationQuery.php:13
eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd
This criterion implements a logical AND criterion and will only match if ALL of the given criteria ma...
Definition: API/Repository/Values/Content/Query/Criterion/LogicalAnd.php:14
eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOperator
Note that the class should ideally have been in a Logical namespace, but it would have then be named ...
Definition: API/Repository/Values/Content/Query/Criterion/LogicalOperator.php:18
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion.php:15
eZ\Publish\API\Repository\Values\Content\Query
This class is used to perform a Content query.
Definition: API/Repository/Values/Content/Query.php:15
eZ\Publish\API\Repository\Values\Content\Search\SearchResult
This class represents a search result.
Definition: Content/Search/SearchResult.php:15
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
Invalid Argument Type Exception implementation.
Definition: InvalidArgumentType.php:17
eZ\Publish\Core\Base\Exceptions\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\Repository\SearchService
Search service.
Definition: Core/Repository/SearchService.php:31
eZ\Publish\Core\Repository\SearchService\findLocations
findLocations(LocationQuery $query, array $languageFilter=[], $filterOnUserPermissions=true)
Finds Locations for the given query.
Definition: Core/Repository/SearchService.php:276
eZ\Publish\Core\Repository\SearchService\$permissionCriterionResolver
$permissionCriterionResolver
Definition: Core/Repository/SearchService.php:45
eZ\Publish\Core\Repository\SearchService\validateContentCriteria
validateContentCriteria(array $criteria, $argumentName)
Checks that $criteria does not contain Location criterions.
Definition: Core/Repository/SearchService.php:186
eZ\Publish\Core\Repository\SearchService\supports
supports($capabilityFlag)
Query for supported capability of currently configured search engine.
Definition: Core/Repository/SearchService.php:342
eZ\Publish\Core\Repository\SearchService\validateContentSortClauses
validateContentSortClauses(Query $query)
Checks that $query does not contain Location sort clauses.
Definition: Core/Repository/SearchService.php:208
eZ\Publish\Core\Repository\SearchService\$settings
$settings
Definition: Core/Repository/SearchService.php:39
eZ\Publish\Core\Repository\SearchService\findSingle
findSingle(Criterion $filter, array $languageFilter=[], $filterOnUserPermissions=true)
Performs a query for a single content object.
Definition: Core/Repository/SearchService.php:232
eZ\Publish\Core\Repository\SearchService\$repository
$repository
Definition: Core/Repository/SearchService.php:33
eZ\Publish\Core\Repository\SearchService\$searchHandler
$searchHandler
Definition: Core/Repository/SearchService.php:36
eZ\Publish\Core\Repository\SearchService\$domainMapper
$domainMapper
Definition: Core/Repository/SearchService.php:42
eZ\Publish\Core\Repository\SearchService\$backgroundIndexer
$backgroundIndexer
Definition: Core/Repository/SearchService.php:48
eZ\Publish\Core\Repository\SearchService\suggest
suggest($prefix, $fieldPaths=[], $limit=10, Criterion $filter=null)
Suggests a list of values for the given prefix.
Definition: Core/Repository/SearchService.php:259
eZ\Publish\Core\Repository\SearchService\addPermissionsCriterion
addPermissionsCriterion(Criterion &$criterion)
Adds content, read Permission criteria if needed and return false if no access at all.
Definition: Core/Repository/SearchService.php:320
eZ\Publish\Core\Repository\SearchService\findContentInfo
findContentInfo(Query $query, array $languageFilter=[], $filterOnUserPermissions=true)
Finds contentInfo objects for the given query.
Definition: Core/Repository/SearchService.php:119
eZ\Publish\Core\Repository\SearchService\__construct
__construct(RepositoryInterface $repository, Handler $searchHandler, Helper\DomainMapper $domainMapper, PermissionCriterionResolver $permissionCriterionResolver, BackgroundIndexer $backgroundIndexer, array $settings=[])
Setups service with reference to repository object that created it & corresponding handler.
Definition: Core/Repository/SearchService.php:60
eZ\Publish\Core\Repository\SearchService\findContent
findContent(Query $query, array $languageFilter=[], $filterOnUserPermissions=true)
Finds content objects for the given query.
Definition: Core/Repository/SearchService.php:92
eZ\Publish\Core\Repository\SearchService\internalFindContentInfo
internalFindContentInfo(Query $query, array $languageFilter=[], $filterOnUserPermissions=true)
Finds SPI content info objects for the given query.
Definition: Core/Repository/SearchService.php:146
eZ\Publish\API\Repository\PermissionCriterionResolver
This service provides methods for resolving criterion permissions.
Definition: API/Repository/PermissionCriterionResolver.php:15
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\API\Repository\SearchService
Search service.
Definition: API/Repository/SearchService.php:17
eZ\Publish\Core\Search\Common\BackgroundIndexer
Interface for performing indexing in background.
Definition: BackgroundIndexer.php:21
eZ\Publish\SPI\Persistence\Content\Language\Handler
Language Handler interface.
Definition: SPI/Persistence/Content/Language/Handler.php:15
eZ\Publish\SPI\Search\Capable
Capability interface for search engines needed for {.
Definition: Capable.php:15
eZ\Publish\SPI\Search\Handler
The Search handler retrieves sets of of Content objects, based on a set of criteria.
Definition: SPI/Search/Handler.php:20
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Location
Definition: API/Repository/Values/Content/Query/Criterion/Location/Depth.php:7
eZ\Publish\API\Repository\Values\Content\Query\Criterion
Definition: API/Repository/Values/Content/Query/Criterion/Ancestor.php:7
eZ\Publish\API\Repository\Values\Content\Query\SortClause\Location
Definition: API/Repository/Values/Content/Query/SortClause/Location/Depth.php:7
eZ\Publish\Core\Repository
Definition: Core/Repository/BookmarkService.php:9
eZ\Publish\Core\Search\Common\BackgroundIndexer
Definition: NullIndexer.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.