|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\TrashedItemCollection; |
|
6
|
|
|
use Kaliop\eZMigrationBundle\Core\Matcher\TrashMatcher; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Handles trash migrations. |
|
10
|
|
|
*/ |
|
11
|
|
|
class TrashManager extends RepositoryExecutor |
|
12
|
|
|
{ |
|
13
|
|
|
protected $supportedActions = array('purge', 'recover', 'delete'); |
|
14
|
|
|
protected $supportedStepTypes = array('trash'); |
|
15
|
|
|
|
|
16
|
|
|
/** @var TrashMatcher $trashMatcher */ |
|
17
|
|
|
protected $trashMatcher; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param TrashMatcher $trashMatcher |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct(TrashMatcher $trashMatcher) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->trashMatcher = $trashMatcher; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Handles emptying the trash |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function purge($step) |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$trashService = $this->repository->getTrashService(); |
|
33
|
|
|
|
|
34
|
|
|
$trashService->emptyTrash(); |
|
35
|
|
|
|
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handles the trash-restore migration action |
|
41
|
|
|
*/ |
|
42
|
|
View Code Duplication |
protected function restore($step) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
|
|
$itemsCollection = $this->matchItems('restore', $step); |
|
45
|
|
|
|
|
46
|
|
|
if (count($itemsCollection) > 1 && array_key_exists('references', $step->dsl)) { |
|
47
|
|
|
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"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$trashService = $this->repository->getTrashService(); |
|
51
|
|
|
foreach ($itemsCollection as $key => $item) { |
|
|
|
|
|
|
52
|
|
|
/// @todo support handling of custom restoration locations |
|
53
|
|
|
$trashService->recover($item); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$this->setReferences($itemsCollection, $step); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
return $itemsCollection; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Handles the trash-restore migration action |
|
63
|
|
|
*/ |
|
64
|
|
View Code Duplication |
protected function delete($step) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$itemsCollection = $this->matchItems('delete', $step); |
|
67
|
|
|
|
|
68
|
|
|
if (count($itemsCollection) > 1 && array_key_exists('references', $step->dsl)) { |
|
69
|
|
|
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"); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$this->setReferences($itemsCollection, $step); |
|
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
$trashService = $this->repository->getTrashService(); |
|
75
|
|
|
foreach ($itemsCollection as $key => $item) { |
|
|
|
|
|
|
76
|
|
|
$trashService->deleteTrashItem($item); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->setReferences($itemsCollection, $step); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
return $itemsCollection; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $action |
|
86
|
|
|
* @return TrashedItemCollection |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
|
View Code Duplication |
protected function matchItems($action, $step) |
|
|
|
|
|
|
90
|
|
|
{ |
|
91
|
|
|
if (!isset($step->dsl['match'])) { |
|
92
|
|
|
throw new \Exception("A match condition is required to $action trash items"); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// convert the references passed in the match |
|
96
|
|
|
$match = $this->resolveReferencesRecursively($step->dsl['match']); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
return $this->trashMatcher->match($match); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Sets references to certain trashed-item attributes. |
|
103
|
|
|
* |
|
104
|
|
|
* @param \eZ\Publish\API\Repository\Values\Content\TrashItem|TrashedItemCollection $item |
|
105
|
|
|
* @param $step |
|
106
|
|
|
* @throws \InvalidArgumentException When trying to set a reference to an unsupported attribute |
|
107
|
|
|
* @return boolean |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function setReferences($item, $step) |
|
110
|
|
|
{ |
|
111
|
|
|
if (!array_key_exists('references', $step->dsl)) { |
|
112
|
|
|
return false; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$references = $this->setReferencesCommon($item, $step->dsl['references']); |
|
116
|
|
|
$item = $this->insureSingleEntity($item, $references); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
foreach ($references as $reference) { |
|
119
|
|
|
switch ($reference['attribute']) { |
|
120
|
|
|
/// @todo a trashed item extends a location, so in theory everything 'location' here should work |
|
121
|
|
|
/*case 'section_id': |
|
|
|
|
|
|
122
|
|
|
case 'id': |
|
123
|
|
|
$value = $section->id; |
|
124
|
|
|
break; |
|
125
|
|
|
case 'section_identifier': |
|
126
|
|
|
case 'identifier': |
|
127
|
|
|
$value = $section->identifier; |
|
128
|
|
|
break; |
|
129
|
|
|
case 'section_name': |
|
130
|
|
|
case 'name': |
|
131
|
|
|
$value = $section->name; |
|
132
|
|
|
break;*/ |
|
133
|
|
|
default: |
|
|
|
|
|
|
134
|
|
|
throw new \InvalidArgumentException('Trash Manager does not support setting references for attribute ' . $reference['attribute']); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$overwrite = false; |
|
138
|
|
|
if (isset($reference['overwrite'])) { |
|
139
|
|
|
$overwrite = $reference['overwrite']; |
|
140
|
|
|
} |
|
141
|
|
|
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.