|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Matcher; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Repository; |
|
6
|
|
|
use eZ\Publish\API\Repository\Values\Content\Content; |
|
7
|
|
|
use \eZ\Publish\API\Repository\Values\Content\VersionInfo; |
|
8
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
|
9
|
|
|
use Kaliop\eZMigrationBundle\API\MatcherInterface; |
|
10
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\VersionInfoCollection; |
|
11
|
|
|
|
|
12
|
|
|
class ContentVersionMatcher extends RepositoryMatcher implements MatcherInterface |
|
13
|
|
|
{ |
|
14
|
|
|
const MATCH_STATUS_DRAFT = 'draft'; |
|
15
|
|
|
const MATCH_STATUS_PUBLISHED = 'published'; |
|
16
|
|
|
const MATCH_STATUS_ARCHIVED = 'archived'; |
|
17
|
|
|
|
|
18
|
|
|
const MATCH_STATUS = 'version_status'; |
|
19
|
|
|
const MATCH_VERSION = 'version'; |
|
20
|
|
|
|
|
21
|
|
|
const STATUS_MAP = array( |
|
22
|
|
|
self::MATCH_STATUS_DRAFT => VersionInfo::STATUS_DRAFT, |
|
23
|
|
|
self::MATCH_STATUS_PUBLISHED => VersionInfo::STATUS_PUBLISHED, |
|
24
|
|
|
self::MATCH_STATUS_ARCHIVED => VersionInfo::STATUS_ARCHIVED |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
protected $allowedConditions = array( |
|
28
|
|
|
self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
|
29
|
|
|
self::MATCH_STATUS, self::MATCH_VERSION, |
|
30
|
|
|
// aliases |
|
31
|
|
|
'status' |
|
32
|
|
|
); |
|
33
|
|
|
protected $returns = 'VersionInfo'; |
|
34
|
|
|
|
|
35
|
|
|
protected $contentMatcher; |
|
36
|
|
|
|
|
37
|
80 |
|
public function __construct(Repository $repository, ContentMatcher $contentMatcher) |
|
38
|
|
|
{ |
|
39
|
80 |
|
$this->repository = $repository; |
|
40
|
80 |
|
$this->contentMatcher = $contentMatcher; |
|
41
|
80 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function match(array $contentConditions, array $versionConditions = array(), $sort = array(), $offset = 0, $limit = 0) |
|
44
|
|
|
{ |
|
45
|
|
|
$versions = array(); |
|
46
|
|
|
|
|
47
|
|
|
$contentCollection = $this->contentMatcher->match($contentConditions, $sort, $offset, $limit); |
|
48
|
|
|
foreach($contentCollection as $content) { |
|
|
|
|
|
|
49
|
|
|
$versions = array_merge($versions, $this->matchContentVersions($versionConditions, $content)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return new VersionInfoCollection($versions); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Like match, but will throw an exception if there are 0 or more than 1 items matching |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $conditions |
|
|
|
|
|
|
59
|
|
|
* @return mixed |
|
60
|
|
|
* @throws \Exception |
|
61
|
|
|
*/ |
|
62
|
|
|
public function matchOne(array $contentConditions, array $versionConditions = array(), $sort = array(), $offset = 0) |
|
63
|
|
|
{ |
|
64
|
|
|
$results = $this->match($contentConditions, $versionConditions, $sort, $offset, 2); |
|
65
|
|
|
$count = count($results); |
|
66
|
|
|
if ($count !== 1) { |
|
67
|
|
|
throw new \Exception("Found $count " . $this->returns . " when expected exactly only one to match the conditions"); |
|
68
|
|
|
} |
|
69
|
|
|
return reset($results); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $versionConditions |
|
74
|
|
|
* @param Content $content |
|
75
|
|
|
* @return VersionInfo[] key: obj_id/version_no |
|
76
|
|
|
*/ |
|
77
|
|
|
public function matchContentVersions(array $versionConditions, Content $content) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->validateConditions($versionConditions); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($versionConditions as $key => $values) { |
|
82
|
|
|
|
|
83
|
|
|
if (!is_array($values)) { |
|
84
|
|
|
$values = array($values); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
switch ($key) { |
|
88
|
|
|
case 'status': |
|
89
|
|
|
case self::MATCH_STATUS: |
|
90
|
|
|
return $this->findContentVersionsByStatus($content, $values); |
|
91
|
|
|
|
|
92
|
|
|
case self::MATCH_VERSION: |
|
93
|
|
|
return $this->findContentVersionsByVersionNo($content, $values); |
|
94
|
|
|
|
|
95
|
|
|
case self::MATCH_ALL: |
|
96
|
|
|
return $this->findAllContentVersions($content); |
|
97
|
|
|
|
|
98
|
|
|
case self::MATCH_AND: |
|
99
|
|
|
return $this->matchAnd($values, $content); |
|
100
|
|
|
|
|
101
|
|
|
case self::MATCH_OR: |
|
102
|
|
|
return $this->matchOr($values, $content); |
|
103
|
|
|
|
|
104
|
|
|
case self::MATCH_NOT: |
|
105
|
|
|
return array_diff_key($this->findAllContentVersions($content), $this->matchContentVersions($values, $content)); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
View Code Duplication |
protected function matchAnd(array $conditionsArray, $content) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
/// @todo introduce proper re-validation of all child conditions |
|
113
|
|
|
if (!is_array($conditionsArray) || !count($conditionsArray)) { |
|
114
|
|
|
throw new \Exception($this->returns . " can not be matched because no matching conditions found for 'and' clause."); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
foreach ($conditionsArray as $conditions) { |
|
118
|
|
|
$out = $this->matchContentVersions($conditions, $content); |
|
119
|
|
|
if (!isset($results)) { |
|
120
|
|
|
$results = $out; |
|
121
|
|
|
} else { |
|
122
|
|
|
$results = array_intersect_key($results, $out); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
return $results; |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
View Code Duplication |
protected function matchOr(array $conditionsArray, $content) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
/// @todo introduce proper re-validation of all child conditions |
|
132
|
|
|
if (!is_array($conditionsArray) || !count($conditionsArray)) { |
|
133
|
|
|
throw new \Exception($this->returns . " can not be matched because no matching conditions found for 'or' clause."); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$results = array(); |
|
137
|
|
|
foreach ($conditionsArray as $conditions) { |
|
138
|
|
|
$out = $this->matchContentVersions($conditions, $content); |
|
139
|
|
|
$results = array_replace($results, $out); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return $results; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param Content $content |
|
147
|
|
|
* @param string[] $values |
|
148
|
|
|
* @return VersionInfo[] key: obj_id/version_no, sorted in increasing version no. |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function findContentVersionsByStatus(Content $content, array $values) |
|
151
|
|
|
{ |
|
152
|
|
|
$versions = array(); |
|
153
|
|
|
foreach ($this->findAllContentVersions($content) as $versionKey => $versionInfo) { |
|
154
|
|
|
foreach($values as $acceptedStatus) { |
|
155
|
|
|
if ($versionInfo->status == self::STATUS_MAP[$acceptedStatus]) { |
|
156
|
|
|
$versions[$versionKey] = $versionInfo; |
|
157
|
|
|
break; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
return $versions; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param Content $content |
|
166
|
|
|
* @param int[] $values |
|
167
|
|
|
* @return VersionInfo[] key: obj_id/version_no, sorted in increasing version no. |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function findContentVersionsByVersionNo(Content $content, array $values) |
|
170
|
|
|
{ |
|
171
|
|
|
$versions = array(); |
|
172
|
|
|
$contentVersions = $this->findAllContentVersions($content); |
|
173
|
|
|
$contentVersionsCount = count($contentVersions); |
|
174
|
|
|
$i = 0; |
|
175
|
|
|
foreach ($contentVersions as $versionKey => $versionInfo) { |
|
176
|
|
|
foreach($values as $acceptedVersionNo) { |
|
177
|
|
|
if ($acceptedVersionNo > 0 ) { |
|
178
|
|
|
if ($acceptedVersionNr == $versionInfo->versionNo) { |
|
|
|
|
|
|
179
|
|
|
$versions[$versionKey] = $versionInfo; |
|
180
|
|
|
break; |
|
181
|
|
|
} |
|
182
|
|
|
} else { |
|
183
|
|
|
// negative $acceptedVersionNo means 'leave the last X versions', eg: -1 = leave the last version |
|
184
|
|
|
if ($i < $contentVersionsCount + $acceptedVersionNo) { |
|
185
|
|
|
$versions[$versionKey] = $versionInfo; |
|
186
|
|
|
break; |
|
187
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
$i++; |
|
192
|
|
|
} |
|
193
|
|
|
return $versions; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param Content $content |
|
198
|
|
|
* @return VersionInfo[] key: obj_id/version_no, sorted in increasing version no. |
|
199
|
|
|
*/ |
|
200
|
|
|
protected function findAllContentVersions(Content $content) |
|
201
|
|
|
{ |
|
202
|
|
|
$contentVersions = $this->repository->getContentService()->loadVersions($content->contentInfo); |
|
203
|
|
|
// different eZ kernels apparently sort versions in different order... |
|
204
|
|
|
$sortedVersions = array(); |
|
205
|
|
|
foreach($contentVersions as $versionInfo) { |
|
206
|
|
|
$sortedVersions[$content->contentInfo->id . '/' . $versionInfo->versionNo] = $versionInfo; |
|
207
|
|
|
} |
|
208
|
|
|
ksort($sortedVersions); |
|
209
|
|
|
|
|
210
|
|
|
return $sortedVersions; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.