1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
4
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\LocationCollection; |
6
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\TrashedItemCollection; |
7
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\TrashMatcher; |
8
|
|
|
use Kaliop\eZMigrationBundle\Core\Helper\SortConverter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Handles trash migrations. |
12
|
|
|
*/ |
13
|
|
|
class TrashManager extends RepositoryExecutor |
14
|
|
|
{ |
15
|
|
|
protected $supportedActions = array('purge', 'recover', 'delete'); |
16
|
|
|
protected $supportedStepTypes = array('trash'); |
17
|
|
|
|
18
|
|
|
/** @var TrashMatcher $trashMatcher */ |
19
|
|
|
protected $trashMatcher; |
20
|
|
|
|
21
|
|
|
protected $sortConverter; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param TrashMatcher $trashMatcher |
25
|
|
|
*/ |
26
|
|
|
public function __construct(TrashMatcher $trashMatcher, SortConverter $sortConverter) |
27
|
|
|
{ |
28
|
|
|
$this->trashMatcher = $trashMatcher; |
29
|
|
|
$this->sortConverter = $sortConverter; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Handles emptying the trash |
34
|
|
|
*/ |
35
|
|
|
protected function purge($step) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$trashService = $this->repository->getTrashService(); |
38
|
|
|
|
39
|
|
|
$trashService->emptyTrash(); |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Handles the trash-restore migration action |
46
|
|
|
* |
47
|
|
|
* @todo support handling of restoration to custom locations |
48
|
|
|
*/ |
49
|
|
|
protected function recover($step) |
50
|
|
|
{ |
51
|
|
|
$itemsCollection = $this->matchItems('restore', $step); |
52
|
|
|
|
53
|
|
View Code Duplication |
if (count($itemsCollection) > 1 && array_key_exists('references', $step->dsl)) { |
|
|
|
|
54
|
|
|
throw new \Exception("Can not execute Trash restore because multiple types match, and a references section is specified in the dsl. References can be set when only 1 section matches"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$locations = array(); |
58
|
|
|
$trashService = $this->repository->getTrashService(); |
59
|
|
|
foreach ($itemsCollection as $key => $item) { |
|
|
|
|
60
|
|
|
$locations[] = $trashService->recover($item); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->setReferences(new LocationCollection($locations), $step); |
|
|
|
|
64
|
|
|
|
65
|
|
|
return $itemsCollection; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handles the trash-delete migration action |
70
|
|
|
*/ |
71
|
|
|
protected function delete($step) |
72
|
|
|
{ |
73
|
|
|
$itemsCollection = $this->matchItems('delete', $step); |
74
|
|
|
|
75
|
|
View Code Duplication |
if (count($itemsCollection) > 1 && array_key_exists('references', $step->dsl)) { |
|
|
|
|
76
|
|
|
throw new \Exception("Can not execute Trash restore because multiple types match, and a references section is specified in the dsl. References can be set when only 1 section matches"); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->setReferences($itemsCollection, $step); |
|
|
|
|
80
|
|
|
|
81
|
|
|
$trashService = $this->repository->getTrashService(); |
82
|
|
|
foreach ($itemsCollection as $key => $item) { |
|
|
|
|
83
|
|
|
$trashService->deleteTrashItem($item); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->setReferences($itemsCollection, $step); |
|
|
|
|
87
|
|
|
|
88
|
|
|
return $itemsCollection; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $action |
93
|
|
|
* @return TrashedItemCollection |
94
|
|
|
* @throws \Exception |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
protected function matchItems($action, $step) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if (!isset($step->dsl['match'])) { |
99
|
|
|
throw new \Exception("A match condition is required to $action trash items"); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// convert the references passed in the match |
103
|
|
|
$match = $this->resolveReferencesRecursively($step->dsl['match']); |
104
|
|
|
|
105
|
|
|
return $this->trashMatcher->match($match); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem|\eZ\Publish\API\Repository\Values\Content\Location $item |
110
|
|
|
* @param array $references the definitions of the references to set |
111
|
|
|
* @throws \InvalidArgumentException When trying to assign a reference to an unsupported attribute |
112
|
|
|
* @return array key: the reference names, values: the reference values |
113
|
|
|
*/ |
114
|
|
View Code Duplication |
protected function getReferencesValues($item, array $references) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
$refs = array(); |
117
|
|
|
|
118
|
|
|
foreach ($references as $reference) { |
119
|
|
|
switch ($reference['attribute']) { |
120
|
|
|
// a trashed item extends a location, so in theory everything 'location' here should work |
121
|
|
|
case 'location_id': |
122
|
|
|
case 'id': |
123
|
|
|
$value = $item->id; |
124
|
|
|
break; |
125
|
|
|
case 'remote_id': |
126
|
|
|
case 'location_remote_id': |
127
|
|
|
$value = $item->remoteId; |
128
|
|
|
break; |
129
|
|
|
case 'always_available': |
130
|
|
|
$value = $item->contentInfo->alwaysAvailable; |
131
|
|
|
break; |
132
|
|
|
case 'content_id': |
133
|
|
|
$value = $item->contentId; |
134
|
|
|
break; |
135
|
|
|
case 'content_type_id': |
136
|
|
|
$value = $item->contentInfo->contentTypeId; |
137
|
|
|
break; |
138
|
|
|
case 'content_type_identifier': |
139
|
|
|
$contentTypeService = $this->repository->getContentTypeService(); |
140
|
|
|
$value = $contentTypeService->loadContentType($item->contentInfo->contentTypeId)->identifier; |
141
|
|
|
break; |
142
|
|
|
case 'current_version': |
143
|
|
|
case 'current_version_no': |
144
|
|
|
$value = $item->contentInfo->currentVersionNo; |
145
|
|
|
break; |
146
|
|
|
case 'depth': |
147
|
|
|
$value = $item->depth; |
148
|
|
|
break; |
149
|
|
|
case 'is_hidden': |
150
|
|
|
$value = $item->hidden; |
151
|
|
|
break; |
152
|
|
|
case 'main_location_id': |
153
|
|
|
$value = $item->contentInfo->mainLocationId; |
154
|
|
|
break; |
155
|
|
|
case 'main_language_code': |
156
|
|
|
$value = $item->contentInfo->mainLanguageCode; |
157
|
|
|
break; |
158
|
|
|
case 'modification_date': |
159
|
|
|
$value = $item->contentInfo->modificationDate->getTimestamp(); |
160
|
|
|
break; |
161
|
|
|
case 'name': |
162
|
|
|
$value = $item->contentInfo->name; |
163
|
|
|
break; |
164
|
|
|
case 'owner_id': |
165
|
|
|
$value = $item->contentInfo->ownerId; |
166
|
|
|
break; |
167
|
|
|
case 'parent_location_id': |
168
|
|
|
$value = $item->parentLocationId; |
169
|
|
|
break; |
170
|
|
|
case 'path': |
171
|
|
|
$value = $item->pathString; |
172
|
|
|
break; |
173
|
|
|
case 'priority': |
174
|
|
|
$value = $item->priority; |
175
|
|
|
break; |
176
|
|
|
case 'publication_date': |
177
|
|
|
$value = $item->contentInfo->publishedDate->getTimestamp(); |
178
|
|
|
break; |
179
|
|
|
case 'section_id': |
180
|
|
|
$value = $item->contentInfo->sectionId; |
181
|
|
|
break; |
182
|
|
|
case 'section_identifier': |
183
|
|
|
$sectionService = $this->repository->getSectionService(); |
184
|
|
|
$value = $sectionService->loadSection($item->contentInfo->sectionId)->identifier; |
185
|
|
|
break; |
186
|
|
|
case 'sort_field': |
187
|
|
|
$value = $this->sortConverter->sortField2Hash($item->sortField); |
188
|
|
|
break; |
189
|
|
|
case 'sort_order': |
190
|
|
|
$value = $this->sortConverter->sortOrder2Hash($item->sortOrder); |
191
|
|
|
break; |
192
|
|
|
default: |
193
|
|
|
throw new \InvalidArgumentException('Trash Manager does not support setting references for attribute ' . $reference['attribute']); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$refs[$reference['identifier']] = $value; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $refs; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.