|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the Mapper class. |
|
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\Persistence\Legacy\Content; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\SPI\Persistence\Content; |
|
12
|
|
|
use eZ\Publish\SPI\Persistence\Content\CreateStruct; |
|
13
|
|
|
use eZ\Publish\SPI\Persistence\Content\Field; |
|
14
|
|
|
use eZ\Publish\SPI\Persistence\Content\FieldValue; |
|
15
|
|
|
use eZ\Publish\SPI\Persistence\Content\Relation; |
|
16
|
|
|
use eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct as RelationCreateStruct; |
|
17
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry as Registry; |
|
18
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler; |
|
19
|
|
|
use eZ\Publish\SPI\Persistence\Content\ContentInfo; |
|
20
|
|
|
use eZ\Publish\SPI\Persistence\Content\VersionInfo; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Mapper for Content Handler. |
|
24
|
|
|
* |
|
25
|
|
|
* Performs mapping of Content objects. |
|
26
|
|
|
*/ |
|
27
|
|
|
class Mapper |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* FieldValue converter registry. |
|
31
|
|
|
* |
|
32
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $converterRegistry; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Caching language handler. |
|
38
|
|
|
* |
|
39
|
|
|
* @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $languageHandler; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Creates a new mapper. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry $converterRegistry |
|
47
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(Registry $converterRegistry, LanguageHandler $languageHandler) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->converterRegistry = $converterRegistry; |
|
52
|
|
|
$this->languageHandler = $languageHandler; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Creates a Content from the given $struct and $currentVersionNo. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
|
59
|
|
|
* @param mixed $currentVersionNo |
|
60
|
|
|
* |
|
61
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
|
62
|
|
|
*/ |
|
63
|
|
|
private function createContentInfoFromCreateStruct(CreateStruct $struct, $currentVersionNo = 1) |
|
64
|
|
|
{ |
|
65
|
|
|
$contentInfo = new ContentInfo(); |
|
66
|
|
|
|
|
67
|
|
|
$contentInfo->id = null; |
|
68
|
|
|
$contentInfo->contentTypeId = $struct->typeId; |
|
69
|
|
|
$contentInfo->sectionId = $struct->sectionId; |
|
70
|
|
|
$contentInfo->ownerId = $struct->ownerId; |
|
71
|
|
|
$contentInfo->alwaysAvailable = $struct->alwaysAvailable; |
|
72
|
|
|
$contentInfo->remoteId = $struct->remoteId; |
|
73
|
|
|
$contentInfo->mainLanguageCode = $this->languageHandler |
|
74
|
|
|
->load(isset($struct->mainLanguageId) ? $struct->mainLanguageId : $struct->initialLanguageId) |
|
75
|
|
|
->languageCode; |
|
76
|
|
|
$contentInfo->name = isset($struct->name[$contentInfo->mainLanguageCode]) |
|
77
|
|
|
? $struct->name[$contentInfo->mainLanguageCode] |
|
78
|
|
|
: ''; |
|
79
|
|
|
// For drafts published and modified timestamps should be 0 |
|
80
|
|
|
$contentInfo->publicationDate = 0; |
|
81
|
|
|
$contentInfo->modificationDate = 0; |
|
82
|
|
|
$contentInfo->currentVersionNo = $currentVersionNo; |
|
83
|
|
|
$contentInfo->isPublished = false; |
|
84
|
|
|
|
|
85
|
|
|
return $contentInfo; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Creates a new version for the given $struct and $versionNo. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\CreateStruct $struct |
|
92
|
|
|
* @param mixed $versionNo |
|
93
|
|
|
* |
|
94
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
|
95
|
|
|
*/ |
|
96
|
|
|
public function createVersionInfoFromCreateStruct(CreateStruct $struct, $versionNo) |
|
97
|
|
|
{ |
|
98
|
|
|
$versionInfo = new VersionInfo(); |
|
99
|
|
|
|
|
100
|
|
|
$versionInfo->id = null; |
|
101
|
|
|
$versionInfo->contentInfo = $this->createContentInfoFromCreateStruct($struct, $versionNo); |
|
102
|
|
|
$versionInfo->versionNo = $versionNo; |
|
103
|
|
|
$versionInfo->creatorId = $struct->ownerId; |
|
104
|
|
|
$versionInfo->status = VersionInfo::STATUS_DRAFT; |
|
105
|
|
|
$versionInfo->initialLanguageCode = $this->languageHandler->load($struct->initialLanguageId)->languageCode; |
|
|
|
|
|
|
106
|
|
|
$versionInfo->creationDate = $struct->modified; |
|
107
|
|
|
$versionInfo->modificationDate = $struct->modified; |
|
108
|
|
|
$versionInfo->names = $struct->name; |
|
109
|
|
|
|
|
110
|
|
|
$languageIds = array(); |
|
111
|
|
|
foreach ($struct->fields as $field) { |
|
112
|
|
|
if (!isset($languageIds[$field->languageCode])) { |
|
113
|
|
|
$languageIds[$field->languageCode] = |
|
114
|
|
|
$this->languageHandler->loadByLanguageCode($field->languageCode)->id; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
$versionInfo->languageIds = array_values($languageIds); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
return $versionInfo; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Creates a new version for the given $content. |
|
124
|
|
|
* |
|
125
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content $content |
|
126
|
|
|
* @param mixed $versionNo |
|
127
|
|
|
* @param mixed $userId |
|
128
|
|
|
* |
|
129
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
|
130
|
|
|
*/ |
|
131
|
|
|
public function createVersionInfoForContent(Content $content, $versionNo, $userId) |
|
132
|
|
|
{ |
|
133
|
|
|
$versionInfo = new VersionInfo(); |
|
134
|
|
|
|
|
135
|
|
|
$versionInfo->contentInfo = $content->versionInfo->contentInfo; |
|
136
|
|
|
$versionInfo->versionNo = $versionNo; |
|
137
|
|
|
$versionInfo->creatorId = $userId; |
|
138
|
|
|
$versionInfo->status = VersionInfo::STATUS_DRAFT; |
|
139
|
|
|
$versionInfo->initialLanguageCode = $content->versionInfo->initialLanguageCode; |
|
140
|
|
|
$versionInfo->creationDate = time(); |
|
141
|
|
|
$versionInfo->modificationDate = $versionInfo->creationDate; |
|
142
|
|
|
$versionInfo->names = is_object($content->versionInfo) ? $content->versionInfo->names : array(); |
|
143
|
|
|
$versionInfo->languageIds = $content->versionInfo->languageIds; |
|
144
|
|
|
|
|
145
|
|
|
return $versionInfo; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Converts value of $field to storage value. |
|
150
|
|
|
* |
|
151
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Field $field |
|
152
|
|
|
* |
|
153
|
|
|
* @return \eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue |
|
154
|
|
|
*/ |
|
155
|
|
|
public function convertToStorageValue(Field $field) |
|
156
|
|
|
{ |
|
157
|
|
|
$converter = $this->converterRegistry->getConverter( |
|
158
|
|
|
$field->type |
|
159
|
|
|
); |
|
160
|
|
|
$storageValue = new StorageFieldValue(); |
|
161
|
|
|
$converter->toStorageValue( |
|
162
|
|
|
$field->value, |
|
163
|
|
|
$storageValue |
|
164
|
|
|
); |
|
165
|
|
|
|
|
166
|
|
|
return $storageValue; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Extracts Content objects (and nested) from database result $rows. |
|
171
|
|
|
* |
|
172
|
|
|
* Expects database rows to be indexed by keys of the format |
|
173
|
|
|
* |
|
174
|
|
|
* "$tableName_$columnName" |
|
175
|
|
|
* |
|
176
|
|
|
* @param array $rows |
|
177
|
|
|
* @param array $nameRows |
|
178
|
|
|
* |
|
179
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content[] |
|
180
|
|
|
*/ |
|
181
|
|
|
public function extractContentFromRows(array $rows, array $nameRows) |
|
182
|
|
|
{ |
|
183
|
|
|
$versionedNameData = array(); |
|
184
|
|
|
foreach ($nameRows as $row) { |
|
185
|
|
|
$contentId = (int)$row['ezcontentobject_name_contentobject_id']; |
|
186
|
|
|
$versionNo = (int)$row['ezcontentobject_name_content_version']; |
|
187
|
|
|
$versionedNameData[$contentId][$versionNo][$row['ezcontentobject_name_content_translation']] = $row['ezcontentobject_name_name']; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$contentInfos = array(); |
|
191
|
|
|
$versionInfos = array(); |
|
192
|
|
|
$fields = array(); |
|
193
|
|
|
|
|
194
|
|
|
foreach ($rows as $row) { |
|
195
|
|
|
$contentId = (int)$row['ezcontentobject_id']; |
|
196
|
|
|
if (!isset($contentInfos[$contentId])) { |
|
197
|
|
|
$contentInfos[$contentId] = $this->extractContentInfoFromRow($row, 'ezcontentobject_'); |
|
198
|
|
|
} |
|
199
|
|
|
if (!isset($versionInfos[$contentId])) { |
|
200
|
|
|
$versionInfos[$contentId] = array(); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$versionId = (int)$row['ezcontentobject_version_id']; |
|
204
|
|
|
if (!isset($versionInfos[$contentId][$versionId])) { |
|
205
|
|
|
$versionInfos[$contentId][$versionId] = $this->extractVersionInfoFromRow($row); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$fieldId = (int)$row['ezcontentobject_attribute_id']; |
|
209
|
|
View Code Duplication |
if (!isset($fields[$contentId][$versionId][$fieldId])) { |
|
|
|
|
|
|
210
|
|
|
$fields[$contentId][$versionId][$fieldId] = $this->extractFieldFromRow($row); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
$results = array(); |
|
215
|
|
|
foreach ($contentInfos as $contentId => $contentInfo) { |
|
216
|
|
|
foreach ($versionInfos[$contentId] as $versionId => $versionInfo) { |
|
217
|
|
|
// Fallback to just main language name if versioned name data is missing |
|
218
|
|
View Code Duplication |
if (isset($versionedNameData[$contentId][$versionInfo->versionNo])) { |
|
|
|
|
|
|
219
|
|
|
$names = $versionedNameData[$contentId][$versionInfo->versionNo]; |
|
220
|
|
|
} else { |
|
221
|
|
|
$names = [$contentInfo->mainLanguageCode => $contentInfo->name]; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$content = new Content(); |
|
225
|
|
|
$content->versionInfo = $versionInfo; |
|
226
|
|
|
$content->versionInfo->names = $names; |
|
227
|
|
|
$content->versionInfo->contentInfo = $contentInfo; |
|
228
|
|
|
$content->fields = array_values($fields[$contentId][$versionId]); |
|
229
|
|
|
$results[] = $content; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $results; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Extracts a ContentInfo object from $row. |
|
238
|
|
|
* |
|
239
|
|
|
* @param array $row |
|
240
|
|
|
* @param string $prefix Prefix for row keys, which are initially mapped by ezcontentobject fields |
|
241
|
|
|
* @param string $treePrefix Prefix for tree row key, which are initially mapped by ezcontentobject_tree_ fields |
|
242
|
|
|
* |
|
243
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
|
244
|
|
|
*/ |
|
245
|
|
|
public function extractContentInfoFromRow(array $row, $prefix = '', $treePrefix = 'ezcontentobject_tree_') |
|
246
|
|
|
{ |
|
247
|
|
|
$contentInfo = new ContentInfo(); |
|
248
|
|
|
$contentInfo->id = (int)$row["{$prefix}id"]; |
|
249
|
|
|
$contentInfo->name = $row["{$prefix}name"]; |
|
250
|
|
|
$contentInfo->contentTypeId = (int)$row["{$prefix}contentclass_id"]; |
|
251
|
|
|
$contentInfo->sectionId = (int)$row["{$prefix}section_id"]; |
|
252
|
|
|
$contentInfo->currentVersionNo = (int)$row["{$prefix}current_version"]; |
|
253
|
|
|
$contentInfo->isPublished = (bool)($row["{$prefix}status"] == ContentInfo::STATUS_PUBLISHED); |
|
254
|
|
|
$contentInfo->ownerId = (int)$row["{$prefix}owner_id"]; |
|
255
|
|
|
$contentInfo->publicationDate = (int)$row["{$prefix}published"]; |
|
256
|
|
|
$contentInfo->modificationDate = (int)$row["{$prefix}modified"]; |
|
257
|
|
|
$contentInfo->alwaysAvailable = (int)$row["{$prefix}language_mask"] & 1; |
|
|
|
|
|
|
258
|
|
|
$contentInfo->mainLanguageCode = $this->languageHandler->load($row["{$prefix}initial_language_id"])->languageCode; |
|
259
|
|
|
$contentInfo->remoteId = $row["{$prefix}remote_id"]; |
|
260
|
|
|
$contentInfo->mainLocationId = ($row["{$treePrefix}main_node_id"] !== null ? (int)$row["{$treePrefix}main_node_id"] : null); |
|
261
|
|
|
|
|
262
|
|
|
return $contentInfo; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Extracts ContentInfo objects from $rows. |
|
267
|
|
|
* |
|
268
|
|
|
* @param array $rows |
|
269
|
|
|
* @param string $prefix Prefix for row keys, which are initially mapped by ezcontentobject fields |
|
270
|
|
|
* @param string $treePrefix Prefix for tree row key, which are initially mapped by ezcontentobject_tree_ fields |
|
271
|
|
|
* |
|
272
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\ContentInfo[] |
|
273
|
|
|
*/ |
|
274
|
|
|
public function extractContentInfoFromRows(array $rows, $prefix = '', $treePrefix = 'ezcontentobject_tree_') |
|
275
|
|
|
{ |
|
276
|
|
|
$contentInfoObjects = array(); |
|
277
|
|
|
foreach ($rows as $row) { |
|
278
|
|
|
$contentInfoObjects[] = $this->extractContentInfoFromRow($row, $prefix, $treePrefix); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return $contentInfoObjects; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Extracts a VersionInfo object from $row. |
|
286
|
|
|
* |
|
287
|
|
|
* This method will return VersionInfo with incomplete data. It is intended to be used only by |
|
288
|
|
|
* {@link self::extractContentFromRows} where missing data will be filled in. |
|
289
|
|
|
* |
|
290
|
|
|
* @param array $row |
|
291
|
|
|
* @param array $names |
|
292
|
|
|
* |
|
293
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\VersionInfo |
|
294
|
|
|
*/ |
|
295
|
|
|
private function extractVersionInfoFromRow(array $row, array $names = array()) |
|
296
|
|
|
{ |
|
297
|
|
|
$versionInfo = new VersionInfo(); |
|
298
|
|
|
$versionInfo->id = (int)$row['ezcontentobject_version_id']; |
|
299
|
|
|
$versionInfo->contentInfo = null; |
|
300
|
|
|
$versionInfo->versionNo = (int)$row['ezcontentobject_version_version']; |
|
301
|
|
|
$versionInfo->creatorId = (int)$row['ezcontentobject_version_creator_id']; |
|
302
|
|
|
$versionInfo->creationDate = (int)$row['ezcontentobject_version_created']; |
|
303
|
|
|
$versionInfo->modificationDate = (int)$row['ezcontentobject_version_modified']; |
|
304
|
|
|
$versionInfo->initialLanguageCode = $this->languageHandler->load($row['ezcontentobject_version_initial_language_id'])->languageCode; |
|
|
|
|
|
|
305
|
|
|
$versionInfo->languageIds = $this->extractLanguageIdsFromMask($row['ezcontentobject_version_language_mask']); |
|
306
|
|
|
$versionInfo->status = (int)$row['ezcontentobject_version_status']; |
|
307
|
|
|
$versionInfo->names = $names; |
|
308
|
|
|
|
|
309
|
|
|
return $versionInfo; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* Extracts a VersionInfo object from $row. |
|
314
|
|
|
* |
|
315
|
|
|
* @param array $rows |
|
316
|
|
|
* @param array $nameRows |
|
317
|
|
|
* |
|
318
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\VersionInfo[] |
|
319
|
|
|
*/ |
|
320
|
|
|
public function extractVersionInfoListFromRows(array $rows, array $nameRows) |
|
321
|
|
|
{ |
|
322
|
|
|
$nameData = array(); |
|
323
|
|
|
foreach ($nameRows as $row) { |
|
324
|
|
|
$versionId = $row['ezcontentobject_name_contentobject_id'] . '_' . $row['ezcontentobject_name_content_version']; |
|
325
|
|
|
$nameData[$versionId][$row['ezcontentobject_name_content_translation']] = $row['ezcontentobject_name_name']; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
$versionInfoList = array(); |
|
329
|
|
|
foreach ($rows as $row) { |
|
330
|
|
|
$versionId = $row['ezcontentobject_id'] . '_' . $row['ezcontentobject_version_version']; |
|
331
|
|
|
if (!isset($versionInfoList[$versionId])) { |
|
332
|
|
|
$versionInfo = new VersionInfo(); |
|
333
|
|
|
$versionInfo->id = (int)$row['ezcontentobject_version_id']; |
|
334
|
|
|
$versionInfo->contentInfo = $this->extractContentInfoFromRow($row, 'ezcontentobject_'); |
|
335
|
|
|
$versionInfo->versionNo = (int)$row['ezcontentobject_version_version']; |
|
336
|
|
|
$versionInfo->creatorId = (int)$row['ezcontentobject_version_creator_id']; |
|
337
|
|
|
$versionInfo->creationDate = (int)$row['ezcontentobject_version_created']; |
|
338
|
|
|
$versionInfo->modificationDate = (int)$row['ezcontentobject_version_modified']; |
|
339
|
|
|
$versionInfo->initialLanguageCode = $this->languageHandler->load($row['ezcontentobject_version_initial_language_id'])->languageCode; |
|
|
|
|
|
|
340
|
|
|
$versionInfo->languageIds = $this->extractLanguageIdsFromMask((int)$row['ezcontentobject_version_language_mask']); |
|
341
|
|
|
$versionInfo->status = (int)$row['ezcontentobject_version_status']; |
|
342
|
|
|
$versionInfo->names = $nameData[$versionId]; |
|
343
|
|
|
$versionInfoList[$versionId] = $versionInfo; |
|
344
|
|
|
} |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
return array_values($versionInfoList); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** |
|
351
|
|
|
* @todo use langmask handler for this |
|
352
|
|
|
* |
|
353
|
|
|
* @param int $languageMask |
|
354
|
|
|
* |
|
355
|
|
|
* @return array |
|
356
|
|
|
*/ |
|
357
|
|
View Code Duplication |
public function extractLanguageIdsFromMask($languageMask) |
|
358
|
|
|
{ |
|
359
|
|
|
$exp = 2; |
|
360
|
|
|
$result = array(); |
|
361
|
|
|
|
|
362
|
|
|
// Decomposition of $languageMask into its binary components. |
|
363
|
|
|
while ($exp <= $languageMask) { |
|
364
|
|
|
if ($languageMask & $exp) { |
|
365
|
|
|
$result[] = $exp; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
$exp *= 2; |
|
369
|
|
|
} |
|
370
|
|
|
|
|
371
|
|
|
return $result; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Extracts a Field from $row. |
|
376
|
|
|
* |
|
377
|
|
|
* @param array $row |
|
378
|
|
|
* |
|
379
|
|
|
* @return Field |
|
380
|
|
|
*/ |
|
381
|
|
|
protected function extractFieldFromRow(array $row) |
|
382
|
|
|
{ |
|
383
|
|
|
$field = new Field(); |
|
384
|
|
|
|
|
385
|
|
|
$field->id = (int)$row['ezcontentobject_attribute_id']; |
|
386
|
|
|
$field->fieldDefinitionId = (int)$row['ezcontentobject_attribute_contentclassattribute_id']; |
|
387
|
|
|
$field->type = $row['ezcontentobject_attribute_data_type_string']; |
|
388
|
|
|
$field->value = $this->extractFieldValueFromRow($row, $field->type); |
|
389
|
|
|
$field->languageCode = $row['ezcontentobject_attribute_language_code']; |
|
390
|
|
|
$field->versionNo = (int)$row['ezcontentobject_attribute_version']; |
|
391
|
|
|
|
|
392
|
|
|
return $field; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* Extracts a FieldValue of $type from $row. |
|
397
|
|
|
* |
|
398
|
|
|
* @param array $row |
|
399
|
|
|
* @param string $type |
|
400
|
|
|
* |
|
401
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\FieldValue |
|
402
|
|
|
* |
|
403
|
|
|
* @throws \eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter\Exception\NotFound |
|
404
|
|
|
* if the necessary converter for $type could not be found. |
|
405
|
|
|
*/ |
|
406
|
|
|
protected function extractFieldValueFromRow(array $row, $type) |
|
407
|
|
|
{ |
|
408
|
|
|
$storageValue = new StorageFieldValue(); |
|
409
|
|
|
|
|
410
|
|
|
// Nullable field |
|
411
|
|
|
$storageValue->dataFloat = isset($row['ezcontentobject_attribute_data_float']) |
|
412
|
|
|
? (float)$row['ezcontentobject_attribute_data_float'] |
|
413
|
|
|
: null; |
|
414
|
|
|
// Nullable field |
|
415
|
|
|
$storageValue->dataInt = isset($row['ezcontentobject_attribute_data_int']) |
|
416
|
|
|
? (int)$row['ezcontentobject_attribute_data_int'] |
|
417
|
|
|
: null; |
|
418
|
|
|
$storageValue->dataText = $row['ezcontentobject_attribute_data_text']; |
|
419
|
|
|
// Not nullable field |
|
420
|
|
|
$storageValue->sortKeyInt = (int)$row['ezcontentobject_attribute_sort_key_int']; |
|
421
|
|
|
$storageValue->sortKeyString = $row['ezcontentobject_attribute_sort_key_string']; |
|
422
|
|
|
|
|
423
|
|
|
$fieldValue = new FieldValue(); |
|
424
|
|
|
|
|
425
|
|
|
$converter = $this->converterRegistry->getConverter($type); |
|
426
|
|
|
$converter->toFieldValue($storageValue, $fieldValue); |
|
427
|
|
|
|
|
428
|
|
|
return $fieldValue; |
|
429
|
|
|
} |
|
430
|
|
|
|
|
431
|
|
|
/** |
|
432
|
|
|
* Creates CreateStruct from $content. |
|
433
|
|
|
* |
|
434
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content $content |
|
435
|
|
|
* |
|
436
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\CreateStruct |
|
437
|
|
|
*/ |
|
438
|
|
|
public function createCreateStructFromContent(Content $content) |
|
439
|
|
|
{ |
|
440
|
|
|
$struct = new CreateStruct(); |
|
441
|
|
|
$struct->name = $content->versionInfo->names; |
|
442
|
|
|
$struct->typeId = $content->versionInfo->contentInfo->contentTypeId; |
|
443
|
|
|
$struct->sectionId = $content->versionInfo->contentInfo->sectionId; |
|
444
|
|
|
$struct->ownerId = $content->versionInfo->contentInfo->ownerId; |
|
445
|
|
|
$struct->locations = array(); |
|
446
|
|
|
$struct->alwaysAvailable = $content->versionInfo->contentInfo->alwaysAvailable; |
|
447
|
|
|
$struct->remoteId = md5(uniqid(get_class($this), true)); |
|
448
|
|
|
$struct->initialLanguageId = $this->languageHandler->loadByLanguageCode($content->versionInfo->initialLanguageCode)->id; |
|
449
|
|
|
$struct->mainLanguageId = $this->languageHandler->loadByLanguageCode($content->versionInfo->contentInfo->mainLanguageCode)->id; |
|
450
|
|
|
$struct->modified = time(); |
|
451
|
|
|
|
|
452
|
|
|
foreach ($content->fields as $field) { |
|
453
|
|
|
$newField = clone $field; |
|
454
|
|
|
$newField->id = null; |
|
455
|
|
|
$struct->fields[] = $newField; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
return $struct; |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* Extracts relation objects from $rows. |
|
463
|
|
|
*/ |
|
464
|
|
|
public function extractRelationsFromRows(array $rows) |
|
465
|
|
|
{ |
|
466
|
|
|
$relations = array(); |
|
467
|
|
|
|
|
468
|
|
|
foreach ($rows as $row) { |
|
469
|
|
|
$id = (int)$row['ezcontentobject_link_id']; |
|
470
|
|
|
if (!isset($relations[$id])) { |
|
471
|
|
|
$relations[$id] = $this->extractRelationFromRow($row); |
|
472
|
|
|
} |
|
473
|
|
|
} |
|
474
|
|
|
|
|
475
|
|
|
return $relations; |
|
476
|
|
|
} |
|
477
|
|
|
|
|
478
|
|
|
/** |
|
479
|
|
|
* Extracts a Relation object from a $row. |
|
480
|
|
|
* |
|
481
|
|
|
* @param array $row Associative array representing a relation |
|
482
|
|
|
* |
|
483
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Relation |
|
484
|
|
|
*/ |
|
485
|
|
|
protected function extractRelationFromRow(array $row) |
|
486
|
|
|
{ |
|
487
|
|
|
$relation = new Relation(); |
|
488
|
|
|
$relation->id = (int)$row['ezcontentobject_link_id']; |
|
489
|
|
|
$relation->sourceContentId = (int)$row['ezcontentobject_link_from_contentobject_id']; |
|
490
|
|
|
$relation->sourceContentVersionNo = (int)$row['ezcontentobject_link_from_contentobject_version']; |
|
491
|
|
|
$relation->destinationContentId = (int)$row['ezcontentobject_link_to_contentobject_id']; |
|
492
|
|
|
$relation->type = (int)$row['ezcontentobject_link_relation_type']; |
|
493
|
|
|
|
|
494
|
|
|
$contentClassAttributeId = (int)$row['ezcontentobject_link_contentclassattribute_id']; |
|
495
|
|
|
if ($contentClassAttributeId > 0) { |
|
496
|
|
|
$relation->sourceFieldDefinitionId = $contentClassAttributeId; |
|
497
|
|
|
} |
|
498
|
|
|
|
|
499
|
|
|
return $relation; |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* Creates a Content from the given $struct. |
|
504
|
|
|
* |
|
505
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Relation\CreateStruct $struct |
|
506
|
|
|
* |
|
507
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\Relation |
|
508
|
|
|
*/ |
|
509
|
|
|
public function createRelationFromCreateStruct(RelationCreateStruct $struct) |
|
510
|
|
|
{ |
|
511
|
|
|
$relation = new Relation(); |
|
512
|
|
|
|
|
513
|
|
|
$relation->destinationContentId = $struct->destinationContentId; |
|
514
|
|
|
$relation->sourceContentId = $struct->sourceContentId; |
|
515
|
|
|
$relation->sourceContentVersionNo = $struct->sourceContentVersionNo; |
|
516
|
|
|
$relation->sourceFieldDefinitionId = $struct->sourceFieldDefinitionId; |
|
517
|
|
|
$relation->type = $struct->type; |
|
518
|
|
|
|
|
519
|
|
|
return $relation; |
|
520
|
|
|
} |
|
521
|
|
|
} |
|
522
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.