1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\VersionInfo; |
6
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
7
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\ContentVersionMatcher; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Handles content-version migrations. |
11
|
|
|
* @todo disallow calling (throw): create, update, generateMigration |
12
|
|
|
*/ |
13
|
|
|
class ContentVersionManager extends ContentManager |
14
|
|
|
{ |
15
|
|
|
protected $supportedStepTypes = array('content_version'); |
16
|
|
|
protected $supportedActions = array('delete', 'load'); |
17
|
|
|
|
18
|
|
|
protected $versionMatcher; |
19
|
|
|
|
20
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
21
|
|
|
ContentMatcher $contentMatcher, |
22
|
|
|
SectionMatcher $sectionMatcher, |
23
|
|
|
UserMatcher $userMatcher, |
24
|
|
|
ObjectStateMatcher $objectStateMatcher, |
25
|
|
|
ObjectStateGroupMatcher $objectStateGroupMatcher, |
26
|
|
|
FieldHandlerManager $fieldHandlerManager, |
27
|
|
|
LocationManager $locationManager, |
28
|
|
|
SortConverter $sortConverter, |
29
|
|
|
ContentVersionMatcher $versionMatcher |
30
|
|
|
) |
31
|
|
|
{ |
32
|
|
|
$this->contentMatcher = $contentMatcher; |
33
|
|
|
$this->sectionMatcher = $sectionMatcher; |
34
|
|
|
$this->userMatcher = $userMatcher; |
35
|
|
|
$this->objectStateMatcher = $objectStateMatcher; |
36
|
|
|
$this->objectStateGroupMatcher = $objectStateGroupMatcher; |
37
|
|
|
$this->fieldHandlerManager = $fieldHandlerManager; |
38
|
|
|
$this->locationManager = $locationManager; |
39
|
|
|
$this->sortConverter = $sortConverter; |
40
|
|
|
$this->versionMatcher = $versionMatcher; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected function load($step) |
44
|
|
|
{ |
45
|
|
|
$versionCollection = $this->matchVersions('load', $step); |
|
|
|
|
46
|
|
|
|
47
|
|
|
$this->setReferences($versionCollection, $step); |
|
|
|
|
48
|
|
|
|
49
|
|
|
return $versionCollection; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Handles the content delete migration action type |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
protected function delete($step) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$versionCollection = $this->matchVersions('delete', $step); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$this->setReferences($versionCollection, $step); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$contentService = $this->repository->getContentService(); |
62
|
|
|
|
63
|
|
|
foreach ($versionCollection as $versionInfo) { |
64
|
|
|
try { |
65
|
|
|
$contentService->deleteVersion($versionInfo); |
66
|
|
|
} catch (NotFoundException $e) { |
67
|
|
|
// Someone else (or even us, by virtue of location tree?) removed the content which we found just a |
68
|
|
|
// second ago. We can safely ignore this |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $versionCollection; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $action |
77
|
|
|
* @return ContentCollection |
78
|
|
|
* @throws \Exception |
79
|
|
|
*/ |
80
|
|
|
protected function matchVersions($action, $step) |
81
|
|
|
{ |
82
|
|
View Code Duplication |
if (!isset($step->dsl['object_id']) && !isset($step->dsl['remote_id']) && !isset($step->dsl['match'])) { |
|
|
|
|
83
|
|
|
throw new \Exception("The id or remote id of an object or a match condition is required to $action a content version"); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (!isset($step->dsl['match_versions']) && !isset($step->dsl['versions'])) { |
87
|
|
|
throw new \Exception("A verision match condition is required to $action a content version"); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Backwards compat |
91
|
|
|
|
92
|
|
View Code Duplication |
if (isset($step->dsl['match'])) { |
|
|
|
|
93
|
|
|
$match = $step->dsl['match']; |
94
|
|
|
} else { |
95
|
|
|
if (isset($step->dsl['object_id'])) { |
96
|
|
|
$match = array('content_id' => $step->dsl['object_id']); |
97
|
|
|
} elseif (isset($step->dsl['remote_id'])) { |
98
|
|
|
$match = array('content_remote_id' => $step->dsl['remote_id']); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if (isset($step->dsl['match_versions'])) { |
103
|
|
|
$matchVersions = $step->dsl['match_versions']; |
104
|
|
|
} else { |
105
|
|
|
$matchVersions = array(ContentVersionMatcher::MATCH_VERSION => $step->dsl['versions']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// convert the references passed in the match |
109
|
|
|
$match = $this->resolveReferencesRecursively($match); |
|
|
|
|
110
|
|
|
$matchVersions = $this->resolveReferencesRecursively($matchVersions); |
111
|
|
|
|
112
|
|
|
$sort = isset($step->dsl['match_sort']) ? $this->referenceResolver->resolveReference($step->dsl['match_sort']) : array(); |
113
|
|
|
$offset = isset($step->dsl['match_offset']) ? $this->referenceResolver->resolveReference($step->dsl['match_offset']) : 0; |
114
|
|
|
$limit = isset($step->dsl['match_limit']) ? $this->referenceResolver->resolveReference($step->dsl['match_limit']) : 0; |
115
|
|
|
|
116
|
|
|
return $this->versionMatcher->match($match, $matchVersions, $sort, $offset, $limit); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param VersionInfo $versionInfo |
121
|
|
|
* @param array $references |
122
|
|
|
* @param $step |
123
|
|
|
* @return array |
124
|
|
|
* |
125
|
|
|
* @todo allow setting more refs: creation date, modification date, creator id, langauge codes |
126
|
|
|
*/ |
127
|
|
|
protected function getReferencesValues($versionInfo, array $references, $step) |
128
|
|
|
{ |
129
|
|
|
$refs = array(); |
130
|
|
|
|
131
|
|
|
foreach ($references as $reference) { |
132
|
|
|
switch ($reference['attribute']) { |
133
|
|
|
case 'version_no': |
134
|
|
|
$value = $versionInfo->versionNo; |
135
|
|
|
break; |
136
|
|
|
case 'version_status': |
137
|
|
|
$value = $this->versionStatusToHash($versionInfo->status); |
138
|
|
|
break; |
139
|
|
|
default: |
140
|
|
|
// NB: this will generate an error if the user tries to seta ref to a field value |
141
|
|
|
$value = reset(parent::getReferencesValues($versionInfo, array($references), $step)); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$refs[$reference['identifier']] = $value; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $refs; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
protected function versionStatusToHash($status) |
151
|
|
|
{ |
152
|
|
|
foreach(ContentVersionMatcher::STATUS_MAP as $own => $ez) { |
153
|
|
|
if ($status == $ez) { |
154
|
|
|
return $own; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/// @todo log warning? |
159
|
|
|
return $status; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.