1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the eZ Publish Kernel package. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Search\Legacy\Content\Mapper; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\Core\Search\Common\FieldRegistry; |
12
|
|
|
use eZ\Publish\Core\Search\Legacy\Content\FullTextData; |
13
|
|
|
use eZ\Publish\SPI\Persistence\Content; |
14
|
|
|
use eZ\Publish\SPI\Persistence\Content\Type; |
15
|
|
|
use eZ\Publish\SPI\Search\FieldType; |
16
|
|
|
use eZ\Publish\SPI\Persistence\Content\Type\Handler as ContentTypeHandler; |
17
|
|
|
use eZ\Publish\Core\Search\Legacy\Content\FullTextValue; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* FullTextMapper maps Content object fields to FullTextValue objects which are searchable and |
21
|
|
|
* therefore can be indexed by the legacy search engine. |
22
|
|
|
*/ |
23
|
|
|
class FullTextMapper |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Field registry. |
27
|
|
|
* |
28
|
|
|
* @var \eZ\Publish\Core\Search\Common\FieldRegistry |
29
|
|
|
*/ |
30
|
|
|
protected $fieldRegistry; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Content type handler. |
34
|
|
|
* |
35
|
|
|
* @var \eZ\Publish\SPI\Persistence\Content\Type\Handler |
36
|
|
|
*/ |
37
|
|
|
protected $contentTypeHandler; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \eZ\Publish\Core\Search\Common\FieldRegistry $fieldRegistry |
41
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Type\Handler $contentTypeHandler |
42
|
|
|
*/ |
43
|
|
|
public function __construct( |
44
|
|
|
FieldRegistry $fieldRegistry, |
45
|
|
|
ContentTypeHandler $contentTypeHandler |
46
|
|
|
) { |
47
|
|
|
$this->fieldRegistry = $fieldRegistry; |
48
|
|
|
$this->contentTypeHandler = $contentTypeHandler; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Map given Content to a FullTextValue. |
53
|
|
|
* |
54
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content $content |
55
|
|
|
* |
56
|
|
|
* @return \eZ\Publish\Core\Search\Legacy\Content\FullTextData |
57
|
|
|
*/ |
58
|
|
|
public function mapContent(Content $content) |
59
|
|
|
{ |
60
|
|
|
return new FullTextData( |
61
|
|
|
[ |
62
|
|
|
'id' => $content->versionInfo->contentInfo->id, |
63
|
|
|
'contentTypeId' => $content->versionInfo->contentInfo->contentTypeId, |
64
|
|
|
'sectionId' => $content->versionInfo->contentInfo->sectionId, |
65
|
|
|
'published' => $content->versionInfo->contentInfo->publicationDate, |
66
|
|
|
'values' => $this->getFullTextValues($content), |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns an array of FullTextValue object containing searchable values of content object |
73
|
|
|
* fields for the given $content. |
74
|
|
|
* |
75
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content $content |
76
|
|
|
* |
77
|
|
|
* @return \eZ\Publish\Core\Search\Legacy\Content\FullTextValue[] |
78
|
|
|
*/ |
79
|
|
|
protected function getFullTextValues(Content $content) |
80
|
|
|
{ |
81
|
|
|
$fullTextValues = []; |
82
|
|
|
foreach ($content->fields as $field) { |
83
|
|
|
$fieldDefinition = $this->contentTypeHandler->getFieldDefinition( |
84
|
|
|
$field->fieldDefinitionId, Content\Type::STATUS_DEFINED |
85
|
|
|
); |
86
|
|
|
if (!$fieldDefinition->isSearchable) { |
87
|
|
|
continue; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$value = $this->getFullTextFieldValue($field, $fieldDefinition); |
91
|
|
|
if (empty($value)) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$contentInfo = $content->versionInfo->contentInfo; |
96
|
|
|
$fullTextValues[] = new FullTextValue( |
97
|
|
|
[ |
98
|
|
|
'id' => $field->id, |
99
|
|
|
'fieldDefinitionId' => $field->fieldDefinitionId, |
100
|
|
|
'fieldDefinitionIdentifier' => $fieldDefinition->identifier, |
101
|
|
|
'languageCode' => $field->languageCode, |
102
|
|
|
'value' => !is_array($value) ? $value : implode(' ', $value), |
103
|
|
|
'isMainAndAlwaysAvailable' => ( |
104
|
|
|
$field->languageCode === $contentInfo->mainLanguageCode && $contentInfo->alwaysAvailable |
105
|
|
|
), |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $fullTextValues; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get FullTextField value. |
115
|
|
|
* |
116
|
|
|
* @param Content\Field $field |
117
|
|
|
* @param Type\FieldDefinition $fieldDefinition |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
private function getFullTextFieldValue(Content\Field $field, Type\FieldDefinition $fieldDefinition) |
121
|
|
|
{ |
122
|
|
|
$fieldType = $this->fieldRegistry->getType($field->type); |
123
|
|
|
$indexFields = $fieldType->getIndexData($field, $fieldDefinition); |
124
|
|
|
|
125
|
|
|
// find value to be returned (stored in FullTextField) |
126
|
|
|
$fullTextFieldValue = ''; |
127
|
|
|
foreach ($indexFields as $field) { |
128
|
|
|
/** @var \eZ\Publish\SPI\Search\Field $field */ |
129
|
|
|
if ($field->type instanceof FieldType\FullTextField) { |
130
|
|
|
$fullTextFieldValue = $field->value; |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
// some full text fields are stored as an array of strings |
136
|
|
|
return !is_array($fullTextFieldValue) ? $fullTextFieldValue : implode(' ', $fullTextFieldValue); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|