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