forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchHandler.php
345 lines (321 loc) · 14.1 KB
/
SearchHandler.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* File containing the Content Search handler class
*
* @copyright Copyright (C) 1999-2013 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\Persistence\InMemory;
use eZ\Publish\SPI\Persistence\Content;
use eZ\Publish\SPI\Persistence\Content\ContentInfo;
use eZ\Publish\SPI\Persistence\Content\VersionInfo;
use eZ\Publish\SPI\Persistence\Content\Search\Handler as SearchHandlerInterface;
use eZ\Publish\API\Repository\Values\Content\Search\SearchResult;
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
use eZ\Publish\API\Repository\Values\Content\Query;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ContentId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ContentTypeId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LocationId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\RemoteId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LocationRemoteId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\SectionId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\UserMetadata;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ParentLocationId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\ObjectStateId;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LanguageCode;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalAnd;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\LogicalOr;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Subtree;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator;
use eZ\Publish\Core\Base\Exceptions\NotFoundException as NotFound;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use Exception;
/**
* The Content Search handler retrieves sets of of Content objects, based on a
* set of criteria.
*
* The basic idea of this class is to do the following:
*
* 1) The find methods retrieve a recursive set of filters, which define which
* content objects to retrieve from the database. Those may be combined using
* boolean operators.
*
* 2) This recursive criterion definition is visited into a query, which limits
* the content retrieved from the database. We might not be able to create
* sensible queries from all criterion definitions.
*
* 3) The query might be possible to optimize (remove empty statements),
* reduce singular and and or constructs…
*
* 4) Additionally we might need a post-query filtering step, which filters
* content objects based on criteria, which could not be converted in to
* database statements.
*/
class SearchHandler implements SearchHandlerInterface
{
/**
* @var Handler
*/
protected $handler;
/**
* @var Backend
*/
protected $backend;
/**
* Setups current handler instance with reference to Handler object that created it.
*
* @param Handler $handler
* @param Backend $backend The storage engine backend
*/
public function __construct( Handler $handler, Backend $backend )
{
$this->handler = $handler;
$this->backend = $backend;
}
/**
* Finds content objects for the given query.
*
* @todo define structs for the field filters
*
* @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>.
*
* @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult
*/
public function findContent( Query $query, array $fieldFilters = array() )
{
// Only some criteria are supported as getting full support for all in InMemory engine is not a priority
$match = array();
$this->generateMatchByCriteria( array( $query->criterion ), $match );
if ( empty( $match ) )
{
throw new Exception( "Logical error: \$match is empty" );
}
else
{
$match['versionInfo']['contentInfo']['isPublished'] = true;
}
$list = $this->backend->find(
'Content',
$match,
array(
'locations' => array(
'type' => 'Content\\Location',
'match' => array( 'contentId' => 'id' ),
'skip' => true
),
'versionInfo' => array(
'type' => 'Content\\VersionInfo',
'match' => array( '_contentId' => 'id', 'versionNo' => '_currentVersionNo' ),
'single' => true,
'sub' => array(
'contentInfo' => array(
'type' => 'Content\\ContentInfo',
'match' => array( 'id' => '_contentId' ),
'single' => true
),
)
),
'objectStates' => array(
'type' => 'Content\\ObjectState',
'match' => array( '_contentId' => 'id' ),
'skip' => true
)
)
);
$resultList = array();
foreach ( $list as $item )
{
if ( !$item instanceof Content )
throw new \RuntimeException( "item is not instance of Content, it is ". var_export( $item, true ) );
else if ( !$item->versionInfo instanceof VersionInfo )
throw new \RuntimeException( "item->versionInfo is not instance of VersionInfo, it is ". var_export( $item, true ) );
else if ( !$item->versionInfo->contentInfo instanceof ContentInfo )
throw new \RuntimeException( "item->versionInfo->contentInfo is not instance of ContentInfo, it is ". var_export( $item, true ) );
else
{
$item->fields = $this->backend->find(
'Content\\Field',
array(
'_contentId' => $item->versionInfo->contentInfo->id,
'versionNo' => $item->versionInfo->contentInfo->currentVersionNo
)
);
$resultList[] = $item;
}
}
$result = new SearchResult();
$result->time = 0;
$result->totalCount = count( $resultList );
if ( empty( $resultList ) || ( $query->limit === null && $query->offset === 0 ) )
$result->searchHits = $resultList;
else if ( $query->limit === null )
$result->searchHits = array_slice( $resultList, $query->offset );
else
$result->searchHits = array_slice( $resultList, $query->offset, $query->limit );
$result->searchHits = array_map(
function ( $content )
{
return new SearchHit(
array(
'valueObject' => $content,
)
);
},
$result->searchHits
);
return $result;
}
/**
* 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 there is more than than one result matching the criterions
*
* @todo define structs for the field filters
* @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion
* @param array $fieldFilters - a map of filters for the returned fields.
* Currently supported: <code>array("languages" => array(<language1>,..))</code>.
*
* @return \eZ\Publish\SPI\Persistence\Content
*/
public function findSingle( Criterion $criterion, array $fieldFilters = array() )
{
$list = $this->findContent( new Query( array( 'criterion' => $criterion ) ) );
if ( !$list->totalCount )
throw new NotFound( 'Content', var_export( $criterion, true ) );
else if ( $list->totalCount > 1 )
throw new InvalidArgumentException( "totalCount", "findSingle() found more then one item for query" );
return $list->searchHits[0]->valueObject;
}
/**
* 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 )
{
throw new \Exception( "Not implemented yet." );
}
/**
* @see \eZ\Publish\SPI\Persistence\Content\Search\Handler
*/
public function indexContent( Content $content )
{
throw new \Exception( "Not implemented yet." );
}
/**
* Deletes a content object from the index
*
* @param int $contentId
* @param int|null $versionId
*
* @return void
*/
public function deleteContent( $contentId, $versionId = null )
{
throw new \Exception( "Not implemented yet." );
}
/**
* Deletes a location from the index
*
* @param mixed $locationId
*/
public function deleteLocation( $locationId )
{
throw new \Exception( "Not implemented yet." );
}
/**
* Generate match array for use with Backend based on criteria
*
* @param array $criteria
* @param array $match
*
* @return void
*/
protected function generateMatchByCriteria( array $criteria, array &$match )
{
foreach ( $criteria as $criterion )
{
if ( $criterion instanceof LogicalAnd )
{
$this->generateMatchByCriteria( $criterion->criteria, $match );
}
else if ( $criterion instanceof LogicalOr && !isset( $match['or'] ) )
{
foreach ( $criterion->criteria as $subCriterion )
{
$subMatch = array();
$this->generateMatchByCriteria( array( $subCriterion ), $subMatch );
$match['or'][] = $subMatch;
}
}
else if ( $criterion instanceof ContentId && !isset( $match['id'] ) )
{
$match['id'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof ContentTypeId && !isset( $match['versionInfo']['contentInfo']['typeId'] ) )
{
$match['versionInfo']['contentInfo']['contentTypeId'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof LocationId && !isset( $match['locations']['id'] ) )
{
$match['locations']['id'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof RemoteId && !isset( $match['versionInfo']['contentInfo']['remoteId'] ) )
{
$match['versionInfo']['contentInfo']['remoteId'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof LocationRemoteId && !isset( $match['locations']['remoteId'] ) )
{
$match['locations']['remoteId'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof ObjectStateId && !isset( $match['objectStates']['id'] ) )
{
$match['objectStates']['id'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof LanguageCode && !isset( $match['versionInfo']['languageIds'] ) )
{
$languageHandler = $this->handler->contentLanguageHandler();
$languageIds = array_map(
function ( $languageCode ) use ( $languageHandler )
{
return $languageHandler->loadByLanguageCode( $languageCode )->id;
},
$criterion->value
);
$match['versionInfo']['languageIds'] = $criterion->operator === Operator::IN ? $languageIds : $languageIds[0];
}
else if ( $criterion instanceof SectionId && !isset( $match['versionInfo']['contentInfo']['sectionId'] ) )
{
$match['versionInfo']['contentInfo']['sectionId'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof ParentLocationId && !isset( $match['locations']['parentId'] ) )
{
$match['locations']['parentId'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
}
else if ( $criterion instanceof Subtree && !isset( $match['locations']['pathString'] ) )
{
$match['locations']['pathString'] = $criterion->value[0] . '%';
}
else
{
if ( $criterion instanceof UserMetadata && $criterion->target !== $criterion::MODIFIER )
{
if ( $criterion->target === $criterion::OWNER && !isset( $match['versionInfo']['contentInfo']['ownerId'] ) )
$match['versionInfo']['contentInfo']['ownerId'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
else if ( $criterion->target === $criterion::MODIFIER && !isset( $match['versionInfo']['creatorId'] ) )
$match['versionInfo']['creatorId'] = $criterion->operator === Operator::IN ? $criterion->value : $criterion->value[0];
continue;
}
throw new Exception( "Support for provided criterion not supported or used more then once: " . get_class( $criterion ) );
}
}
}
}