1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\ContentType; |
6
|
|
|
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition; |
7
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location; |
8
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
9
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentCreateStruct; |
10
|
|
|
use eZ\Publish\API\Repository\Values\Content\ContentUpdateStruct; |
11
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
12
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\ContentCollection; |
13
|
|
|
use Kaliop\eZMigrationBundle\API\MigrationGeneratorInterface; |
14
|
|
|
use Kaliop\eZMigrationBundle\Core\FieldHandlerManager; |
15
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\ContentMatcher; |
16
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\SectionMatcher; |
17
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\UserMatcher; |
18
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateMatcher; |
19
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\ObjectStateGroupMatcher; |
20
|
|
|
use Kaliop\eZMigrationBundle\Core\Helper\SortConverter; |
21
|
|
|
use JmesPath\Env as JmesPath; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Handles content migrations. |
25
|
|
|
* |
26
|
|
|
* @todo add support for updating of content metadata |
27
|
|
|
*/ |
28
|
|
|
class ContentManager extends RepositoryExecutor implements MigrationGeneratorInterface |
29
|
|
|
{ |
30
|
|
|
protected $supportedStepTypes = array('content'); |
31
|
|
|
protected $supportedActions = array('create', 'load', 'update', 'delete'); |
32
|
|
|
|
33
|
|
|
protected $contentMatcher; |
34
|
|
|
protected $sectionMatcher; |
35
|
|
|
protected $userMatcher; |
36
|
|
|
protected $objectStateMatcher; |
37
|
|
|
protected $objectStateGroupMatcher; |
38
|
|
|
protected $fieldHandlerManager; |
39
|
|
|
protected $locationManager; |
40
|
|
|
protected $sortConverter; |
41
|
|
|
|
42
|
|
|
public function __construct( |
43
|
|
|
ContentMatcher $contentMatcher, |
44
|
|
|
SectionMatcher $sectionMatcher, |
45
|
|
|
UserMatcher $userMatcher, |
46
|
|
|
ObjectStateMatcher $objectStateMatcher, |
47
|
|
|
ObjectStateGroupMatcher $objectStateGroupMatcher, |
48
|
|
|
FieldHandlerManager $fieldHandlerManager, |
49
|
|
|
LocationManager $locationManager, |
50
|
|
|
SortConverter $sortConverter |
51
|
|
|
) { |
52
|
|
|
$this->contentMatcher = $contentMatcher; |
53
|
|
|
$this->sectionMatcher = $sectionMatcher; |
54
|
|
|
$this->userMatcher = $userMatcher; |
55
|
|
|
$this->objectStateMatcher = $objectStateMatcher; |
56
|
|
|
$this->objectStateGroupMatcher = $objectStateGroupMatcher; |
57
|
|
|
$this->fieldHandlerManager = $fieldHandlerManager; |
58
|
|
|
$this->locationManager = $locationManager; |
59
|
|
|
$this->sortConverter = $sortConverter; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Handles the content create migration action type |
64
|
|
|
*/ |
65
|
|
|
protected function create($step) |
66
|
|
|
{ |
67
|
|
|
$contentService = $this->repository->getContentService(); |
68
|
|
|
$locationService = $this->repository->getLocationService(); |
69
|
|
|
$contentTypeService = $this->repository->getContentTypeService(); |
70
|
|
|
|
71
|
|
|
$contentTypeIdentifier = $step->dsl['content_type']; |
72
|
|
|
$contentTypeIdentifier = $this->referenceResolver->resolveReference($contentTypeIdentifier); |
73
|
|
|
/// @todo use a contenttypematcher |
74
|
|
|
$contentType = $contentTypeService->loadContentTypeByIdentifier($contentTypeIdentifier); |
75
|
|
|
|
76
|
|
|
$contentCreateStruct = $contentService->newContentCreateStruct($contentType, $this->getLanguageCode($step)); |
77
|
|
|
|
78
|
|
|
$this->setFields($contentCreateStruct, $step->dsl['attributes'], $contentType, $step); |
79
|
|
|
|
80
|
|
|
if (isset($step->dsl['always_available'])) { |
81
|
|
|
$contentCreateStruct->alwaysAvailable = $step->dsl['always_available']; |
82
|
|
|
} else { |
83
|
|
|
// Could be removed when https://github.com/ezsystems/ezpublish-kernel/pull/1874 is merged, |
84
|
|
|
// but we strive to support old eZ kernel versions as well... |
85
|
|
|
$contentCreateStruct->alwaysAvailable = $contentType->defaultAlwaysAvailable; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (isset($step->dsl['remote_id'])) { |
89
|
|
|
$contentCreateStruct->remoteId = $step->dsl['remote_id']; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
View Code Duplication |
if (isset($step->dsl['section'])) { |
|
|
|
|
93
|
|
|
$sectionKey = $this->referenceResolver->resolveReference($step->dsl['section']); |
94
|
|
|
$section = $this->sectionMatcher->matchOneByKey($sectionKey); |
95
|
|
|
$contentCreateStruct->sectionId = $section->id; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
View Code Duplication |
if (isset($step->dsl['owner'])) { |
|
|
|
|
99
|
|
|
$owner = $this->getUser($step->dsl['owner']); |
100
|
|
|
$contentCreateStruct->ownerId = $owner->id; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// This is a bit tricky, as the eZPublish API does not support having a different creator and owner with only 1 version. |
104
|
|
|
// We allow it, hoping that nothing gets broken because of it |
105
|
|
|
if (isset($step->dsl['version_creator'])) { |
106
|
|
|
$realContentOwnerId = $contentCreateStruct->ownerId; |
107
|
|
|
if ($realContentOwnerId == null) { |
108
|
|
|
$realContentOwnerId = $this->repository->getCurrentUser()->id; |
109
|
|
|
} |
110
|
|
|
$versionCreator = $this->getUser($step->dsl['version_creator']); |
111
|
|
|
$contentCreateStruct->ownerId = $versionCreator->id; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (isset($step->dsl['modification_date'])) { |
115
|
|
|
$contentCreateStruct->modificationDate = $this->toDateTime($step->dsl['modification_date']); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// instantiate a location create struct from the parent location: |
119
|
|
|
// BC |
120
|
|
|
$locationId = isset($step->dsl['parent_location']) ? $step->dsl['parent_location'] : ( |
121
|
|
|
isset($step->dsl['main_location']) ? $step->dsl['main_location'] : null |
122
|
|
|
); |
123
|
|
|
// 1st resolve references |
124
|
|
|
$locationId = $this->referenceResolver->resolveReference($locationId); |
125
|
|
|
// 2nd allow to specify the location via remote_id |
126
|
|
|
$locationId = $this->locationManager->matchLocationByKey($locationId)->id; |
127
|
|
|
$locationCreateStruct = $locationService->newLocationCreateStruct($locationId); |
128
|
|
|
|
129
|
|
|
if (isset($step->dsl['location_remote_id'])) { |
130
|
|
|
$locationCreateStruct->remoteId = $step->dsl['location_remote_id']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if (isset($step->dsl['priority'])) { |
134
|
|
|
$locationCreateStruct->priority = $step->dsl['priority']; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if (isset($step->dsl['is_hidden'])) { |
138
|
|
|
$locationCreateStruct->hidden = $step->dsl['is_hidden']; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
if (isset($step->dsl['sort_field'])) { |
142
|
|
|
$locationCreateStruct->sortField = $this->sortConverter->hash2SortField($step->dsl['sort_field']); |
143
|
|
|
} else { |
144
|
|
|
$locationCreateStruct->sortField = $contentType->defaultSortField; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (isset($step->dsl['sort_order'])) { |
148
|
|
|
$locationCreateStruct->sortOrder = $this->sortConverter->hash2SortOrder($step->dsl['sort_order']); |
149
|
|
|
} else { |
150
|
|
|
$locationCreateStruct->sortOrder = $contentType->defaultSortOrder; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$locations = array($locationCreateStruct); |
154
|
|
|
|
155
|
|
|
// BC |
156
|
|
|
$other_locations = isset($step->dsl['other_parent_locations']) ? $step->dsl['other_parent_locations'] : ( |
157
|
|
|
isset($step->dsl['other_locations']) ? $step->dsl['other_locations'] : null |
158
|
|
|
); |
159
|
|
|
if (isset($other_locations)) { |
160
|
|
|
foreach ($other_locations as $locationId) { |
161
|
|
|
$locationId = $this->referenceResolver->resolveReference($locationId); |
162
|
|
|
$locationId = $this->locationManager->matchLocationByKey($locationId)->id; |
163
|
|
|
$secondaryLocationCreateStruct = $locationService->newLocationCreateStruct($locationId); |
164
|
|
|
array_push($locations, $secondaryLocationCreateStruct); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// create a draft using the content and location create struct and publish it |
169
|
|
|
$draft = $contentService->createContent($contentCreateStruct, $locations); |
170
|
|
|
$content = $contentService->publishVersion($draft->versionInfo); |
171
|
|
|
|
172
|
|
|
if (isset($step->dsl['object_states'])) { |
173
|
|
|
$this->setObjectStates($content, $step->dsl['object_states']); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// 2nd part of the hack: re-set the content owner to its intended value |
177
|
|
|
if (isset($step->dsl['version_creator']) || isset($step->dsl['publication_date'])) { |
178
|
|
|
$contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct(); |
179
|
|
|
|
180
|
|
|
if (isset($step->dsl['version_creator'])) { |
181
|
|
|
$contentMetaDataUpdateStruct->ownerId = $realContentOwnerId; |
|
|
|
|
182
|
|
|
} |
183
|
|
|
if (isset($step->dsl['publication_date'])) { |
184
|
|
|
$contentMetaDataUpdateStruct->publishedDate = $this->toDateTime($step->dsl['publication_date']); |
185
|
|
|
} |
186
|
|
|
// we have to do this to make sure we preserve the custom modification date |
187
|
|
|
if (isset($this->dsl['modification_date'])) { |
188
|
|
|
$contentMetaDataUpdateStruct->modificationDate = $this->toDateTime($this->dsl['modification_date']); |
|
|
|
|
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
$contentService->updateContentMetadata($content->contentInfo, $contentMetaDataUpdateStruct); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->setReferences($content, $step); |
|
|
|
|
195
|
|
|
|
196
|
|
|
return $content; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
protected function load($step) |
200
|
|
|
{ |
201
|
|
|
$contentCollection = $this->matchContents('load', $step); |
202
|
|
|
|
203
|
|
|
$this->setReferences($contentCollection, $step); |
|
|
|
|
204
|
|
|
|
205
|
|
|
return $contentCollection; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Handles the content update migration action type |
210
|
|
|
* |
211
|
|
|
* @todo handle updating of more metadata fields |
212
|
|
|
*/ |
213
|
|
|
protected function update($step) |
214
|
|
|
{ |
215
|
|
|
$contentService = $this->repository->getContentService(); |
216
|
|
|
$contentTypeService = $this->repository->getContentTypeService(); |
217
|
|
|
|
218
|
|
|
$contentCollection = $this->matchContents('update', $step); |
219
|
|
|
|
220
|
|
|
if (count($contentCollection) > 1 && isset($step->dsl['references'])) { |
221
|
|
|
throw new \Exception("Can not execute Content update because multiple contents match, and a references section is specified in the dsl. References can be set when only 1 content matches"); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if (count($contentCollection) > 1 && isset($step->dsl['main_location'])) { |
225
|
|
|
throw new \Exception("Can not execute Content update because multiple contents match, and a main_location section is specified in the dsl. References can be set when only 1 content matches"); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$contentType = array(); |
229
|
|
|
|
230
|
|
|
foreach ($contentCollection as $key => $content) { |
|
|
|
|
231
|
|
|
$contentInfo = $content->contentInfo; |
232
|
|
|
|
233
|
|
|
if (!isset($contentType[$contentInfo->contentTypeId])) { |
234
|
|
|
$contentType[$contentInfo->contentTypeId] = $contentTypeService->loadContentType($contentInfo->contentTypeId); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
if (isset($step->dsl['attributes']) || isset($step->dsl['version_creator'])) { |
238
|
|
|
$contentUpdateStruct = $contentService->newContentUpdateStruct(); |
239
|
|
|
|
240
|
|
|
if (isset($step->dsl['attributes'])) { |
241
|
|
|
$this->setFields($contentUpdateStruct, $step->dsl['attributes'], $contentType[$contentInfo->contentTypeId], $step); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$versionCreator = null; |
245
|
|
|
if (isset($step->dsl['version_creator'])) { |
246
|
|
|
$versionCreator = $this->getUser($step->dsl['version_creator']); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
$draft = $contentService->createContentDraft($contentInfo, null, $versionCreator); |
250
|
|
|
$contentService->updateContent($draft->versionInfo, $contentUpdateStruct); |
251
|
|
|
$content = $contentService->publishVersion($draft->versionInfo); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
if (isset($step->dsl['always_available']) || |
255
|
|
|
isset($step->dsl['new_remote_id']) || |
256
|
|
|
isset($step->dsl['owner']) || |
257
|
|
|
isset($step->dsl['modification_date']) || |
258
|
|
|
isset($step->dsl['publication_date'])) { |
259
|
|
|
|
260
|
|
|
$contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct(); |
261
|
|
|
|
262
|
|
|
if (isset($step->dsl['always_available'])) { |
263
|
|
|
$contentMetaDataUpdateStruct->alwaysAvailable = $step->dsl['always_available']; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
if (isset($step->dsl['new_remote_id'])) { |
267
|
|
|
$contentMetaDataUpdateStruct->remoteId = $step->dsl['new_remote_id']; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
View Code Duplication |
if (isset($step->dsl['owner'])) { |
|
|
|
|
271
|
|
|
$owner = $this->getUser($step->dsl['owner']); |
272
|
|
|
$contentMetaDataUpdateStruct->ownerId = $owner->id; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
if (isset($step->dsl['modification_date'])) { |
276
|
|
|
$contentMetaDataUpdateStruct->modificationDate = $this->toDateTime($step->dsl['modification_date']); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
if (isset($step->dsl['publication_date'])) { |
280
|
|
|
$contentMetaDataUpdateStruct->publishedDate = $this->toDateTime($step->dsl['publication_date']); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
$content = $contentService->updateContentMetadata($content->contentInfo, $contentMetaDataUpdateStruct); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if (isset($step->dsl['section'])) { |
287
|
|
|
$this->setSection($content, $step->dsl['section']); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
if (isset($step->dsl['object_states'])) { |
291
|
|
|
$this->setObjectStates($content, $step->dsl['object_states']); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
if (isset($step->dsl['main_location'])) { |
295
|
|
|
$this->setMainLocation($content, $step->dsl['main_location']); |
296
|
|
|
|
297
|
|
|
} |
298
|
|
|
$contentCollection[$key] = $content; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$this->setReferences($contentCollection, $step); |
|
|
|
|
302
|
|
|
|
303
|
|
|
return $contentCollection; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Handles the content delete migration action type |
308
|
|
|
*/ |
309
|
|
|
protected function delete($step) |
310
|
|
|
{ |
311
|
|
|
$contentCollection = $this->matchContents('delete', $step); |
312
|
|
|
|
313
|
|
|
$this->setReferences($contentCollection, $step); |
|
|
|
|
314
|
|
|
|
315
|
|
|
$contentService = $this->repository->getContentService(); |
316
|
|
|
|
317
|
|
|
foreach ($contentCollection as $content) { |
|
|
|
|
318
|
|
|
try { |
319
|
|
|
$contentService->deleteContent($content->contentInfo); |
320
|
|
|
} catch (NotFoundException $e) { |
321
|
|
|
// Someone else (or even us, by virtue of location tree?) removed the content which we found just a |
322
|
|
|
// second ago. We can safely ignore this |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
return $contentCollection; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @param string $action |
331
|
|
|
* @return ContentCollection |
332
|
|
|
* @throws \Exception |
333
|
|
|
*/ |
334
|
|
View Code Duplication |
protected function matchContents($action, $step) |
|
|
|
|
335
|
|
|
{ |
336
|
|
|
if (!isset($step->dsl['object_id']) && !isset($step->dsl['remote_id']) && !isset($step->dsl['match'])) { |
337
|
|
|
throw new \Exception("The id or remote id of an object or a match condition is required to $action a content"); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
// Backwards compat |
341
|
|
|
|
342
|
|
|
if (isset($step->dsl['match'])) { |
343
|
|
|
$match = $step->dsl['match']; |
344
|
|
|
} else { |
345
|
|
|
if (isset($step->dsl['object_id'])) { |
346
|
|
|
$match = array('content_id' => $step->dsl['object_id']); |
347
|
|
|
} elseif (isset($step->dsl['remote_id'])) { |
348
|
|
|
$match = array('content_remote_id' => $step->dsl['remote_id']); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
// convert the references passed in the match |
353
|
|
|
$match = $this->resolveReferencesRecursively($match); |
|
|
|
|
354
|
|
|
|
355
|
|
|
return $this->contentMatcher->match($match); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* @param Content $content |
360
|
|
|
* @param array $references the definitions of the references to set |
361
|
|
|
* @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute |
362
|
|
|
* @return array key: the reference names, values: the reference values |
363
|
|
|
*/ |
364
|
|
|
protected function getReferencesValues($content, array $references, $step) |
365
|
|
|
{ |
366
|
|
|
$refs = array(); |
367
|
|
|
|
368
|
|
|
foreach ($references as $reference) { |
369
|
|
|
|
370
|
|
|
switch ($reference['attribute']) { |
371
|
|
|
case 'object_id': |
372
|
|
|
case 'content_id': |
373
|
|
|
case 'id': |
374
|
|
|
$value = $content->id; |
375
|
|
|
break; |
376
|
|
|
case 'remote_id': |
377
|
|
|
case 'content_remote_id': |
378
|
|
|
$value = $content->contentInfo->remoteId; |
379
|
|
|
break; |
380
|
|
|
case 'always_available': |
381
|
|
|
$value = $content->contentInfo->alwaysAvailable; |
382
|
|
|
break; |
383
|
|
|
case 'content_type_id': |
384
|
|
|
$value = $content->contentInfo->contentTypeId; |
385
|
|
|
break; |
386
|
|
|
case 'content_type_identifier': |
387
|
|
|
$contentTypeService = $this->repository->getContentTypeService(); |
388
|
|
|
$value = $contentTypeService->loadContentType($content->contentInfo->contentTypeId)->identifier; |
389
|
|
|
break; |
390
|
|
|
case 'current_version': |
391
|
|
|
case 'current_version_no': |
392
|
|
|
$value = $content->contentInfo->currentVersionNo; |
393
|
|
|
break; |
394
|
|
|
case 'location_id': |
395
|
|
|
case 'main_location_id': |
396
|
|
|
$value = $content->contentInfo->mainLocationId; |
397
|
|
|
break; |
398
|
|
|
case 'main_language_code': |
399
|
|
|
$value = $content->contentInfo->mainLanguageCode; |
400
|
|
|
break; |
401
|
|
|
case 'modification_date': |
402
|
|
|
$value = $content->contentInfo->modificationDate->getTimestamp(); |
403
|
|
|
break; |
404
|
|
|
case 'name': |
405
|
|
|
$value = $content->contentInfo->name; |
406
|
|
|
break; |
407
|
|
|
case 'owner_id': |
408
|
|
|
$value = $content->contentInfo->ownerId; |
409
|
|
|
break; |
410
|
|
|
case 'path': |
411
|
|
|
$locationService = $this->repository->getLocationService(); |
412
|
|
|
$value = $locationService->loadLocation($content->contentInfo->mainLocationId)->pathString; |
413
|
|
|
break; |
414
|
|
|
case 'publication_date': |
415
|
|
|
$value = $content->contentInfo->publishedDate->getTimestamp(); |
416
|
|
|
break; |
417
|
|
|
case 'section_id': |
418
|
|
|
$value = $content->contentInfo->sectionId; |
419
|
|
|
break; |
420
|
|
|
case 'section_identifier': |
421
|
|
|
$sectionService = $this->repository->getSectionService(); |
422
|
|
|
$value = $sectionService->loadSection($content->contentInfo->sectionId)->identifier; |
423
|
|
|
break; |
424
|
|
|
case 'version_count': |
425
|
|
|
$contentService = $this->repository->getContentService(); |
426
|
|
|
$value = count($contentService->loadVersions($content->contentInfo)); |
427
|
|
|
break; |
428
|
|
|
default: |
429
|
|
|
if (strpos($reference['attribute'], 'object_state.') === 0) { |
430
|
|
|
$stateGroupKey = substr($reference['attribute'], 13); |
431
|
|
|
$stateGroup = $this->objectStateGroupMatcher->matchOneByKey($stateGroupKey); |
432
|
|
|
$value = $stateGroupKey . '/' . $this->repository->getObjectStateService()-> |
433
|
|
|
getContentState($content->contentInfo, $stateGroup)->identifier; |
434
|
|
|
break; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
// allow to get the value of fields as well as their sub-parts |
438
|
|
|
if (strpos($reference['attribute'], 'attributes.') === 0) { |
439
|
|
|
$contentType = $this->repository->getContentTypeService()->loadContentType( |
440
|
|
|
$content->contentInfo->contentTypeId |
441
|
|
|
); |
442
|
|
|
$parts = explode('.', $reference['attribute']); |
443
|
|
|
// totally not sure if this list of special chars is correct for what could follow a jmespath identifier... |
444
|
|
|
// also what about quoted strings? |
445
|
|
|
$fieldIdentifier = preg_replace('/[[(|&!{].*$/', '', $parts[1]); |
446
|
|
|
$field = $content->getField($fieldIdentifier); |
|
|
|
|
447
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldIdentifier); |
448
|
|
|
$hashValue = $this->fieldHandlerManager->fieldValueToHash( |
449
|
|
|
$fieldDefinition->fieldTypeIdentifier, $contentType->identifier, $field->value |
450
|
|
|
); |
451
|
|
|
if (is_array($hashValue)) { |
452
|
|
View Code Duplication |
if (count($parts) == 2 && $fieldIdentifier === $parts[1]) { |
|
|
|
|
453
|
|
|
throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute'] . ': the given attribute has an array value'); |
454
|
|
|
} |
455
|
|
|
$value = JmesPath::search(implode('.', array_slice($parts, 1)), array($fieldIdentifier => $hashValue)); |
456
|
|
|
} else { |
457
|
|
|
if (count($parts) > 2) { |
458
|
|
|
throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute'] . ': the given attribute has a scalar value'); |
459
|
|
|
} |
460
|
|
|
$value = $hashValue; |
461
|
|
|
} |
462
|
|
|
break; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
throw new \InvalidArgumentException('Content Manager does not support setting references for attribute ' . $reference['attribute']); |
466
|
|
|
} |
467
|
|
|
|
468
|
|
|
$refs[$reference['identifier']] = $value; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
return $refs; |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* @param array $matchCondition |
476
|
|
|
* @param string $mode |
477
|
|
|
* @param array $context |
478
|
|
|
* @throws \Exception |
479
|
|
|
* @return array |
480
|
|
|
* |
481
|
|
|
* @todo add support for dumping all object languages |
482
|
|
|
* @todo add 2ndary locations when in 'update' mode |
483
|
|
|
* @todo add dumping of sort_field and sort_order for 2ndary locations |
484
|
|
|
*/ |
485
|
|
|
public function generateMigration(array $matchCondition, $mode, array $context = array()) |
486
|
|
|
{ |
487
|
|
|
$previousUserId = $this->loginUser($this->getAdminUserIdentifierFromContext($context)); |
488
|
|
|
$contentCollection = $this->contentMatcher->match($matchCondition); |
489
|
|
|
$data = array(); |
490
|
|
|
|
491
|
|
|
/** @var \eZ\Publish\API\Repository\Values\Content\Content $content */ |
492
|
|
|
foreach ($contentCollection as $content) { |
|
|
|
|
493
|
|
|
|
494
|
|
|
$location = $this->repository->getLocationService()->loadLocation($content->contentInfo->mainLocationId); |
495
|
|
|
$contentType = $this->repository->getContentTypeService()->loadContentType( |
496
|
|
|
$content->contentInfo->contentTypeId |
497
|
|
|
); |
498
|
|
|
|
499
|
|
|
$contentData = array( |
500
|
|
|
'type' => reset($this->supportedStepTypes), |
501
|
|
|
'mode' => $mode |
502
|
|
|
); |
503
|
|
|
|
504
|
|
|
switch ($mode) { |
505
|
|
|
case 'create': |
506
|
|
|
$contentData = array_merge( |
507
|
|
|
$contentData, |
508
|
|
|
array( |
509
|
|
|
'content_type' => $contentType->identifier, |
510
|
|
|
'parent_location' => $location->parentLocationId, |
511
|
|
|
'priority' => $location->priority, |
512
|
|
|
'is_hidden' => $location->invisible, |
513
|
|
|
'sort_field' => $this->sortConverter->sortField2Hash($location->sortField), |
514
|
|
|
'sort_order' => $this->sortConverter->sortOrder2Hash($location->sortOrder), |
515
|
|
|
'remote_id' => $content->contentInfo->remoteId, |
516
|
|
|
'location_remote_id' => $location->remoteId |
517
|
|
|
) |
518
|
|
|
); |
519
|
|
|
$locationService = $this->repository->getLocationService(); |
520
|
|
|
$locations = $locationService->loadLocations($content->contentInfo); |
521
|
|
|
if (count($locations) > 1) { |
522
|
|
|
$otherParentLocations = array(); |
523
|
|
|
foreach($locations as $otherLocation) { |
524
|
|
|
if ($otherLocation->id != $location->id) { |
525
|
|
|
$otherParentLocations[] = $otherLocation->parentLocationId; |
526
|
|
|
} |
527
|
|
|
} |
528
|
|
|
$contentData['other_parent_locations'] = $otherParentLocations; |
529
|
|
|
} |
530
|
|
|
break; |
531
|
|
View Code Duplication |
case 'update': |
|
|
|
|
532
|
|
|
$contentData = array_merge( |
533
|
|
|
$contentData, |
534
|
|
|
array( |
535
|
|
|
'match' => array( |
536
|
|
|
ContentMatcher::MATCH_CONTENT_REMOTE_ID => $content->contentInfo->remoteId |
537
|
|
|
), |
538
|
|
|
'new_remote_id' => $content->contentInfo->remoteId, |
539
|
|
|
) |
540
|
|
|
); |
541
|
|
|
break; |
542
|
|
View Code Duplication |
case 'delete': |
|
|
|
|
543
|
|
|
$contentData = array_merge( |
544
|
|
|
$contentData, |
545
|
|
|
array( |
546
|
|
|
'match' => array( |
547
|
|
|
ContentMatcher::MATCH_CONTENT_REMOTE_ID => $content->contentInfo->remoteId |
548
|
|
|
) |
549
|
|
|
) |
550
|
|
|
); |
551
|
|
|
break; |
552
|
|
|
default: |
553
|
|
|
throw new \Exception("Executor 'content' doesn't support mode '$mode'"); |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
if ($mode != 'delete') { |
557
|
|
|
|
558
|
|
|
$attributes = array(); |
559
|
|
|
foreach ($content->getFieldsByLanguage($this->getLanguageCodeFromContext($context)) as $fieldIdentifier => $field) { |
560
|
|
|
$fieldDefinition = $contentType->getFieldDefinition($fieldIdentifier); |
561
|
|
|
$attributes[$field->fieldDefIdentifier] = $this->fieldHandlerManager->fieldValueToHash( |
562
|
|
|
$fieldDefinition->fieldTypeIdentifier, $contentType->identifier, $field->value |
563
|
|
|
); |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
$contentData = array_merge( |
567
|
|
|
$contentData, |
568
|
|
|
array( |
569
|
|
|
'lang' => $this->getLanguageCodeFromContext($context), |
570
|
|
|
'section' => $content->contentInfo->sectionId, |
571
|
|
|
'owner' => $content->contentInfo->ownerId, |
572
|
|
|
'modification_date' => $content->contentInfo->modificationDate->getTimestamp(), |
573
|
|
|
'publication_date' => $content->contentInfo->publishedDate->getTimestamp(), |
574
|
|
|
'always_available' => (bool)$content->contentInfo->alwaysAvailable, |
575
|
|
|
'attributes' => $attributes |
576
|
|
|
) |
577
|
|
|
); |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
$data[] = $contentData; |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
$this->loginUser($previousUserId); |
584
|
|
|
return $data; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Helper function to set the fields of a ContentCreateStruct based on the DSL attribute settings. |
589
|
|
|
* |
590
|
|
|
* @param ContentCreateStruct|ContentUpdateStruct $createOrUpdateStruct |
591
|
|
|
* @param array $fields see description of expected format in code below |
592
|
|
|
* @param ContentType $contentType |
593
|
|
|
* @param $step |
594
|
|
|
* @throws \Exception |
595
|
|
|
*/ |
596
|
|
|
protected function setFields($createOrUpdateStruct, array $fields, ContentType $contentType, $step) |
597
|
|
|
{ |
598
|
|
|
$i = 0; |
599
|
|
|
// the 'easy' yml: key = field name, value = value |
600
|
|
|
// deprecated: the 'legacy' yml: key = numerical index, value = array ( field name => value ) |
601
|
|
|
foreach ($fields as $key => $field) { |
602
|
|
|
$fieldsList = []; |
603
|
|
|
|
604
|
|
|
if ($key === $i && is_array($field) && count($field) == 1) { |
605
|
|
|
// each $field is one key value pair |
606
|
|
|
// eg.: $field = array($fieldIdentifier => $fieldValue) |
|
|
|
|
607
|
|
|
reset($field); |
608
|
|
|
$fieldIdentifier = key($field); |
609
|
|
|
$fieldValue = $field[$fieldIdentifier]; |
610
|
|
View Code Duplication |
if (is_array($fieldValue) && $this->hasLanguageCodesAsKeys($fieldValue)) { |
|
|
|
|
611
|
|
|
foreach($fieldValue as $languageCode => $value) { |
612
|
|
|
$fieldsList[] = [$fieldIdentifier, $value, $languageCode]; |
613
|
|
|
} |
614
|
|
|
} else { |
615
|
|
|
$fieldsList[] = [$fieldIdentifier, $fieldValue, $this->getLanguageCode($step)]; |
616
|
|
|
} |
617
|
|
View Code Duplication |
} else if (is_array($field) && $this->hasLanguageCodesAsKeys($field)) { |
|
|
|
|
618
|
|
|
foreach($field as $languageCode => $value) { |
619
|
|
|
$fieldsList[] = [$key, $value, $languageCode]; |
620
|
|
|
} |
621
|
|
|
} else { |
622
|
|
|
$fieldsList[] = [$key, $field, $this->getLanguageCode($step)]; |
623
|
|
|
} |
624
|
|
|
|
625
|
|
|
foreach($fieldsList as list($fieldIdentifier, $fieldValue, $language)) { |
626
|
|
|
if (!isset($contentType->fieldDefinitionsByIdentifier[$fieldIdentifier])) { |
|
|
|
|
627
|
|
|
throw new \Exception("Field '$fieldIdentifier' is not present in content type '{$contentType->identifier}'"); |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
$fieldDefinition = $contentType->fieldDefinitionsByIdentifier[$fieldIdentifier]; |
|
|
|
|
631
|
|
|
$fieldValue = $this->getFieldValue($fieldValue, $fieldDefinition, $contentType->identifier, $step->context); |
632
|
|
|
$createOrUpdateStruct->setField($fieldIdentifier, $fieldValue, $language); |
633
|
|
|
} |
634
|
|
|
$i++; |
635
|
|
|
} |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
protected function hasLanguageCodesAsKeys(array &$array) |
639
|
|
|
{ |
640
|
|
|
$languages = $this->getContentLanguages(); |
641
|
|
|
$languageCodes = array_combine($languages, $languages); |
642
|
|
|
$hasLanguageCodesAsKeys = true; |
643
|
|
|
|
644
|
|
|
foreach ($array as $key => $value) { |
645
|
|
|
if (!array_key_exists($key, $languageCodes)) { |
646
|
|
|
$hasLanguageCodesAsKeys = false; |
647
|
|
|
} |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
return $hasLanguageCodesAsKeys; |
651
|
|
|
} |
652
|
|
|
|
653
|
|
|
protected function getContentLanguages() |
654
|
|
|
{ |
655
|
|
|
return array_map( |
656
|
|
|
function($language) { |
657
|
|
|
return $language->languageCode; |
658
|
|
|
}, |
659
|
|
|
array_filter( |
660
|
|
|
$this->repository->getContentLanguageService()->loadLanguages(), |
661
|
|
|
function ($language) { |
662
|
|
|
return $language->enabled; |
663
|
|
|
} |
664
|
|
|
) |
665
|
|
|
); |
666
|
|
|
} |
667
|
|
|
|
668
|
|
View Code Duplication |
protected function setSection(Content $content, $sectionKey) |
|
|
|
|
669
|
|
|
{ |
670
|
|
|
$sectionKey = $this->referenceResolver->resolveReference($sectionKey); |
671
|
|
|
$section = $this->sectionMatcher->matchOneByKey($sectionKey); |
672
|
|
|
|
673
|
|
|
$sectionService = $this->repository->getSectionService(); |
674
|
|
|
$sectionService->assignSection($content->contentInfo, $section); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
protected function setObjectStates(Content $content, array $stateKeys) |
678
|
|
|
{ |
679
|
|
|
foreach ($stateKeys as $stateKey) { |
680
|
|
|
$stateKey = $this->referenceResolver->resolveReference($stateKey); |
681
|
|
|
/** @var \eZ\Publish\API\Repository\Values\ObjectState\ObjectState $state */ |
682
|
|
|
$state = $this->objectStateMatcher->matchOneByKey($stateKey); |
683
|
|
|
|
684
|
|
|
$stateService = $this->repository->getObjectStateService(); |
685
|
|
|
$stateService->setContentState($content->contentInfo, $state->getObjectStateGroup(), $state); |
686
|
|
|
} |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
protected function setMainLocation(Content $content, $locationId) |
690
|
|
|
{ |
691
|
|
|
$locationId = $this->referenceResolver->resolveReference($locationId); |
692
|
|
|
if (is_int($locationId) || ctype_digit($locationId)) { |
693
|
|
|
$location = $this->repository->getLocationService()->loadLocation($locationId); |
694
|
|
|
} else { |
695
|
|
|
$location = $this->repository->getLocationService()->loadLocationByRemoteId($locationId); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
if ($location->contentInfo->id != $content->id) { |
699
|
|
|
throw new \Exception("Can not set main location {$location->id} to content {$content->id} as it belongs to another object"); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
$contentService = $this->repository->getContentService(); |
703
|
|
|
$contentMetaDataUpdateStruct = $contentService->newContentMetadataUpdateStruct(); |
704
|
|
|
$contentMetaDataUpdateStruct->mainLocationId = $location->id; |
705
|
|
|
$contentService->updateContentMetadata($location->contentInfo, $contentMetaDataUpdateStruct); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
/** |
709
|
|
|
* Create the field value from the migration definition hash |
710
|
|
|
* |
711
|
|
|
* @param mixed $value |
712
|
|
|
* @param FieldDefinition $fieldDefinition |
713
|
|
|
* @param string $contentTypeIdentifier |
714
|
|
|
* @param array $context |
715
|
|
|
* @throws \InvalidArgumentException |
716
|
|
|
* @return mixed |
717
|
|
|
*/ |
718
|
|
|
protected function getFieldValue($value, FieldDefinition $fieldDefinition, $contentTypeIdentifier, array $context = array()) |
719
|
|
|
{ |
720
|
|
|
$fieldTypeIdentifier = $fieldDefinition->fieldTypeIdentifier; |
721
|
|
|
|
722
|
|
|
if (is_array($value) || $this->fieldHandlerManager->managesField($fieldTypeIdentifier, $contentTypeIdentifier)) { |
723
|
|
|
// since we now allow refs to be arrays, let's attempt a 1st pass at resolving them here instead of every single fieldHandler... |
724
|
|
|
if (is_string($value) && $this->fieldHandlerManager->doPreResolveStringReferences($fieldTypeIdentifier, $contentTypeIdentifier)) { |
725
|
|
|
$value = $this->referenceResolver->resolveReference($value); |
726
|
|
|
} |
727
|
|
|
// inject info about the current content type and field into the context |
728
|
|
|
$context['contentTypeIdentifier'] = $contentTypeIdentifier; |
729
|
|
|
$context['fieldIdentifier'] = $fieldDefinition->identifier; |
730
|
|
|
return $this->fieldHandlerManager->hashToFieldValue($fieldTypeIdentifier, $contentTypeIdentifier, $value, $context); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
return $this->getSingleFieldValue($value, $fieldDefinition, $contentTypeIdentifier, $context); |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
/** |
737
|
|
|
* Create the field value for a primitive field from the migration definition hash |
738
|
|
|
* |
739
|
|
|
* @param mixed $value |
740
|
|
|
* @param FieldDefinition $fieldDefinition |
741
|
|
|
* @param string $contentTypeIdentifier |
742
|
|
|
* @param array $context |
743
|
|
|
* @throws \InvalidArgumentException |
744
|
|
|
* @return mixed |
745
|
|
|
*/ |
746
|
|
|
protected function getSingleFieldValue($value, FieldDefinition $fieldDefinition, $contentTypeIdentifier, array $context = array()) |
|
|
|
|
747
|
|
|
{ |
748
|
|
|
// booleans were handled here. They are now handled as complextypes |
749
|
|
|
|
750
|
|
|
// q: do we really want this to happen by default on all scalar field values? |
751
|
|
|
// Note: if you want this *not* to happen, register a complex field for your scalar field... |
752
|
|
|
$value = $this->referenceResolver->resolveReference($value); |
753
|
|
|
|
754
|
|
|
return $value; |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
/** |
758
|
|
|
* Load user using either login, email, id - resolving eventual references |
759
|
|
|
* @param int|string $userKey |
760
|
|
|
* @return \eZ\Publish\API\Repository\Values\User\User |
761
|
|
|
*/ |
762
|
|
|
protected function getUser($userKey) |
763
|
|
|
{ |
764
|
|
|
$userKey = $this->referenceResolver->resolveReference($userKey); |
765
|
|
|
return $this->userMatcher->matchOneByKey($userKey); |
766
|
|
|
} |
767
|
|
|
|
768
|
|
|
/** |
769
|
|
|
* @param int|string $date if integer, we assume a timestamp |
770
|
|
|
* @return \DateTime |
771
|
|
|
*/ |
772
|
|
|
protected function toDateTime($date) |
773
|
|
|
{ |
774
|
|
|
if (is_int($date)) { |
775
|
|
|
return new \DateTime("@" . $date); |
776
|
|
|
} else { |
777
|
|
|
return new \DateTime($date); |
778
|
|
|
} |
779
|
|
|
} |
780
|
|
|
} |
781
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.