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

Core/Search/Legacy/Content/Handler.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Search\Legacy\Content;
8
9 use eZ\Publish\SPI\Persistence\Content;
10 use eZ\Publish\SPI\Persistence\Content\Location;
11 use eZ\Publish\SPI\Search\Handler as SearchHandlerInterface;
12 use eZ\Publish\Core\Persistence\Legacy\Content\Mapper as ContentMapper;
13 use eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper;
14 use eZ\Publish\Core\Search\Legacy\Content\Location\Gateway as LocationGateway;
15 use eZ\Publish\Core\Search\Legacy\Content\WordIndexer\Gateway as WordIndexerGateway;
16 use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
17 use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
18 use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
19 use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
20 use eZ\Publish\API\Repository\Values\Content\Query;
21 use eZ\Publish\API\Repository\Values\Content\LocationQuery;
22 use eZ\Publish\Core\Base\Exceptions\NotFoundException;
23 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
24 use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler;
25 use eZ\Publish\Core\Search\Legacy\Content\Mapper\FullTextMapper;
26
48 class Handler implements SearchHandlerInterface
49 {
55 protected $gateway;
56
62 protected $locationGateway;
63
69 protected $indexerGateway;
70
76 protected $contentMapper;
77
83 protected $locationMapper;
84
90 protected $languageHandler;
91
97 protected $mapper;
98
110 public function __construct(
111 Gateway $gateway,
112 LocationGateway $locationGateway,
113 WordIndexerGateway $indexerGateway,
114 ContentMapper $contentMapper,
115 LocationMapper $locationMapper,
116 LanguageHandler $languageHandler,
117 FullTextMapper $mapper
118 ) {
119 $this->gateway = $gateway;
120 $this->locationGateway = $locationGateway;
121 $this->indexerGateway = $indexerGateway;
122 $this->contentMapper = $contentMapper;
123 $this->locationMapper = $locationMapper;
124 $this->languageHandler = $languageHandler;
125 $this->mapper = $mapper;
126 }
127
141 public function findContent(Query $query, array $languageFilter = [])
142 {
143 if (!isset($languageFilter['languages'])) {
144 $languageFilter['languages'] = [];
145 }
146
147 if (!isset($languageFilter['useAlwaysAvailable'])) {
148 $languageFilter['useAlwaysAvailable'] = true;
149 }
150
151 $start = microtime(true);
152 $query->filter = $query->filter ?: new Criterion\MatchAll();
153 $query->query = $query->query ?: new Criterion\MatchAll();
154
155 // The legacy search does not know about scores, so that we just
156 // combine the query with the filter
157 $filter = new Criterion\LogicalAnd([$query->query, $query->filter]);
158
159 $data = $this->gateway->find(
160 $filter,
161 $query->offset,
162 $query->limit,
163 $query->sortClauses,
164 $languageFilter,
165 $query->performCount
166 );
167
168 $result = new SearchResult();
169 $result->time = microtime(true) - $start;
170 $result->totalCount = $data['count'];
171 $contentInfoList = $this->contentMapper->extractContentInfoFromRows(
172 $data['rows'],
173 '',
174 'main_tree_'
175 );
176
177 foreach ($contentInfoList as $index => $contentInfo) {
178 $searchHit = new SearchHit();
179 $searchHit->valueObject = $contentInfo;
180 $searchHit->matchedTranslation = $this->extractMatchedLanguage(
181 $data['rows'][$index]['language_mask'],
182 $data['rows'][$index]['initial_language_id'],
183 $languageFilter
184 );
185
186 $result->searchHits[] = $searchHit;
187 }
188
189 return $result;
190 }
191
192 protected function extractMatchedLanguage($languageMask, $mainLanguageId, $languageSettings)
193 {
194 $languageList = !empty($languageSettings['languages']) ?
195 $this->languageHandler->loadListByLanguageCodes($languageSettings['languages']) :
196 [];
197
198 foreach ($languageList as $language) {
199 if ($languageMask & $language->id) {
200 return $language->languageCode;
201 }
202 }
203
204 if ($languageMask & 1 || empty($languageSettings['languages'])) {
205 return $this->languageHandler->load($mainLanguageId)->languageCode;
206 }
207
208 return null;
209 }
210
226 public function findSingle(Criterion $filter, array $languageFilter = [])
227 {
228 if (!isset($languageFilter['languages'])) {
229 $languageFilter['languages'] = [];
230 }
231
232 if (!isset($languageFilter['useAlwaysAvailable'])) {
233 $languageFilter['useAlwaysAvailable'] = true;
234 }
235
236 $searchQuery = new Query();
237 $searchQuery->filter = $filter;
238 $searchQuery->query = new Criterion\MatchAll();
239 $searchQuery->offset = 0;
240 $searchQuery->limit = 2; // Because we optimize away the count query below
241 $searchQuery->performCount = true;
242 $searchQuery->sortClauses = null;
243 $result = $this->findContent($searchQuery, $languageFilter);
244
245 if (empty($result->searchHits)) {
246 throw new NotFoundException('Content', 'findSingle() found no content for given $criterion');
247 } elseif (isset($result->searchHits[1])) {
248 throw new InvalidArgumentException('totalCount', 'findSingle() found more then one item for given $criterion');
249 }
250
251 $first = reset($result->searchHits);
252
253 return $first->valueObject;
254 }
255
259 public function findLocations(LocationQuery $query, array $languageFilter = [])
260 {
261 if (!isset($languageFilter['languages'])) {
262 $languageFilter['languages'] = [];
263 }
264
265 if (!isset($languageFilter['useAlwaysAvailable'])) {
266 $languageFilter['useAlwaysAvailable'] = true;
267 }
268
269 $start = microtime(true);
270 $query->filter = $query->filter ?: new Criterion\MatchAll();
271 $query->query = $query->query ?: new Criterion\MatchAll();
272
273 // The legacy search does not know about scores, so we just
274 // combine the query with the filter
275 $data = $this->locationGateway->find(
276 new Criterion\LogicalAnd([$query->query, $query->filter]),
277 $query->offset,
278 $query->limit,
279 $query->sortClauses,
280 $languageFilter,
281 $query->performCount
282 );
283
284 $result = new SearchResult();
285 $result->time = microtime(true) - $start;
286 $result->totalCount = $data['count'];
287 $locationList = $this->locationMapper->createLocationsFromRows($data['rows']);
288
289 foreach ($locationList as $index => $location) {
290 $searchHit = new SearchHit();
291 $searchHit->valueObject = $location;
292 $searchHit->matchedTranslation = $this->extractMatchedLanguage(
293 $data['rows'][$index]['language_mask'],
294 $data['rows'][$index]['initial_language_id'],
295 $languageFilter
296 );
297
298 $result->searchHits[] = $searchHit;
299 }
300
301 return $result;
302 }
303
314 public function suggest($prefix, $fieldPaths = [], $limit = 10, Criterion $filter = null)
315 {
316 throw new NotImplementedException('Suggestions are not supported by legacy search engine.');
317 }
318
324 public function indexContent(Content $content)
325 {
326 $fullTextValue = $this->mapper->mapContent($content);
327
328 $this->indexerGateway->index($fullTextValue);
329 }
330
337 public function bulkIndex(array $contentList, callable $errorCallback)
338 {
339 $fullTextBulkData = [];
340 foreach ($contentList as $content) {
341 try {
342 $fullTextBulkData[] = $this->mapper->mapContent($content);
343 } catch (NotFoundException $e) {
344 $errorCallback($content, $e);
345 }
346 }
347
348 $this->indexerGateway->bulkIndex($fullTextBulkData);
349 }
350
354 public function indexLocation(Location $location)
355 {
356 // Not needed with Legacy Storage/Search Engine
357 }
358
365 public function deleteContent($contentId, $versionId = null)
366 {
367 $this->indexerGateway->remove($contentId, $versionId);
368 }
369
376 public function deleteLocation($locationId, $contentId)
377 {
378 // Not needed with Legacy Storage/Search Engine
379 }
380
384 public function purgeIndex()
385 {
386 $this->indexerGateway->purgeIndex();
387 }
388
394 public function commit($flush = false)
395 {
396 // Not needed with Legacy Storage/Search Engine
397 }
398 }
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\Exceptions\NotImplementedException
This Exception is thrown if a feature has not been implemented intentionally.
Definition: API/Repository/Exceptions/NotImplementedException.php:15
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\Location
This is the base class for Location criterions.
Definition: API/Repository/Values/Content/Query/Criterion/Location.php:15
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
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\SearchHit
This class represents a SearchHit matching the query.
Definition: API/Repository/Values/Content/Search/SearchHit.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\NotFoundException
Not Found Exception implementation.
Definition: Core/Base/Exceptions/NotFoundException.php:21
eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper
Mapper for Location objects.
Definition: Publish/Core/Persistence/Legacy/Content/Location/Mapper.php:16
eZ\Publish\Core\Persistence\Legacy\Content\Mapper
Mapper for Content Handler.
Definition: Publish/Core/Persistence/Legacy/Content/Mapper.php:27
eZ\Publish\Core\Search\Legacy\Content\Gateway
The Content Search Gateway provides the implementation for one database to retrieve the desired conte...
Definition: Search/Legacy/Content/Gateway.php:16
eZ\Publish\Core\Search\Legacy\Content\Handler
The Content Search handler retrieves sets of of Content objects, based on a set of criteria.
Definition: Core/Search/Legacy/Content/Handler.php:49
eZ\Publish\Core\Search\Legacy\Content\Handler\commit
commit($flush=false)
Commits the data to the index, making it available for search.
Definition: Core/Search/Legacy/Content/Handler.php:394
eZ\Publish\Core\Search\Legacy\Content\Handler\deleteContent
deleteContent($contentId, $versionId=null)
Deletes a content object from the index.
Definition: Core/Search/Legacy/Content/Handler.php:365
eZ\Publish\Core\Search\Legacy\Content\Handler\$gateway
$gateway
Definition: Core/Search/Legacy/Content/Handler.php:55
eZ\Publish\Core\Search\Legacy\Content\Handler\extractMatchedLanguage
extractMatchedLanguage($languageMask, $mainLanguageId, $languageSettings)
Definition: Core/Search/Legacy/Content/Handler.php:192
eZ\Publish\Core\Search\Legacy\Content\Handler\$indexerGateway
$indexerGateway
Definition: Core/Search/Legacy/Content/Handler.php:69
eZ\Publish\Core\Search\Legacy\Content\Handler\indexLocation
indexLocation(Location $location)
Definition: Core/Search/Legacy/Content/Handler.php:354
eZ\Publish\Core\Search\Legacy\Content\Handler\$mapper
$mapper
Definition: Core/Search/Legacy/Content/Handler.php:97
eZ\Publish\Core\Search\Legacy\Content\Handler\suggest
suggest($prefix, $fieldPaths=[], $limit=10, Criterion $filter=null)
Suggests a list of values for the given prefix.
Definition: Core/Search/Legacy/Content/Handler.php:314
eZ\Publish\Core\Search\Legacy\Content\Handler\deleteLocation
deleteLocation($locationId, $contentId)
Deletes a location from the index.
Definition: Core/Search/Legacy/Content/Handler.php:376
eZ\Publish\Core\Search\Legacy\Content\Handler\indexContent
indexContent(Content $content)
Indexes a content object.
Definition: Core/Search/Legacy/Content/Handler.php:324
eZ\Publish\Core\Search\Legacy\Content\Handler\__construct
__construct(Gateway $gateway, LocationGateway $locationGateway, WordIndexerGateway $indexerGateway, ContentMapper $contentMapper, LocationMapper $locationMapper, LanguageHandler $languageHandler, FullTextMapper $mapper)
Creates a new content handler.
Definition: Core/Search/Legacy/Content/Handler.php:110
eZ\Publish\Core\Search\Legacy\Content\Handler\findContent
findContent(Query $query, array $languageFilter=[])
Finds content objects for the given query.
Definition: Core/Search/Legacy/Content/Handler.php:141
eZ\Publish\Core\Search\Legacy\Content\Handler\$locationGateway
$locationGateway
Definition: Core/Search/Legacy/Content/Handler.php:62
eZ\Publish\Core\Search\Legacy\Content\Handler\purgeIndex
purgeIndex()
Purges all contents from the index.
Definition: Core/Search/Legacy/Content/Handler.php:384
eZ\Publish\Core\Search\Legacy\Content\Handler\$contentMapper
$contentMapper
Definition: Core/Search/Legacy/Content/Handler.php:76
eZ\Publish\Core\Search\Legacy\Content\Handler\findSingle
findSingle(Criterion $filter, array $languageFilter=[])
Performs a query for a single content object.
Definition: Core/Search/Legacy/Content/Handler.php:226
eZ\Publish\Core\Search\Legacy\Content\Handler\$locationMapper
$locationMapper
Definition: Core/Search/Legacy/Content/Handler.php:83
eZ\Publish\Core\Search\Legacy\Content\Handler\$languageHandler
$languageHandler
Definition: Core/Search/Legacy/Content/Handler.php:90
eZ\Publish\Core\Search\Legacy\Content\Handler\findLocations
findLocations(LocationQuery $query, array $languageFilter=[])
Definition: Core/Search/Legacy/Content/Handler.php:259
eZ\Publish\Core\Search\Legacy\Content\Handler\bulkIndex
bulkIndex(array $contentList, callable $errorCallback)
Bulk index list of content objects.
Definition: Core/Search/Legacy/Content/Handler.php:337
eZ\Publish\Core\Search\Legacy\Content\Location\Gateway
Base class for location search gateways.
Definition: Search/Legacy/Content/Location/Gateway.php:15
eZ\Publish\Core\Search\Legacy\Content\Mapper\FullTextMapper
FullTextMapper maps Content object fields to FullTextValue objects which are searchable and therefore...
Definition: FullTextMapper.php:23
eZ\Publish\SPI\Persistence\Content
Content value object, bound to a version.
Definition: Publish/SPI/Persistence/Content.php:17
eZ\Publish\SPI\Persistence\Content\Language\Handler
Language Handler interface.
Definition: SPI/Persistence/Content/Language/Handler.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
Definition: API/Repository/Values/Content/Query/Criterion/Ancestor.php:7
eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway
Definition: Persistence/Legacy/Content/Location/Gateway/DoctrineDatabase.php:7
eZ\Publish\Core\Search\Legacy\Content\WordIndexer\Gateway
Definition: Search/Legacy/Content/WordIndexer/Gateway/DoctrineDatabase.php:7
eZ\Publish\SPI\Persistence\Content\Location
Definition: SPI/Persistence/Content/Location/CreateStruct.php:7
eZ\Publish\SPI\Persistence\Content
Definition: SPI/Persistence/Content/ContentInfo.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.