1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Matcher; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
6
|
|
|
use eZ\Publish\API\Repository\Values\Content\UrlAlias; |
7
|
|
|
use Kaliop\eZMigrationBundle\API\Collection\UrlAliasCollection; |
8
|
|
|
use Kaliop\eZMigrationBundle\API\Exception\InvalidMatchConditionsException; |
9
|
|
|
use Kaliop\eZMigrationBundle\API\KeyMatcherInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @todo allow matching non-custom location aliases via matchUrlAlias() |
13
|
|
|
*/ |
14
|
|
|
class UrlAliasMatcher extends RepositoryMatcher implements KeyMatcherInterface |
15
|
|
|
{ |
16
|
|
|
use FlexibleKeyMatcherTrait; |
17
|
|
|
|
18
|
|
|
const MATCH_URL_ID = 'url_id'; // NB: this is used to match a composite string $parentid-$md5, not the id column in the DB |
19
|
|
|
const MATCH_URL = 'url'; |
20
|
|
|
const MATCH_LOCATION_ID = 'location_id'; |
21
|
|
|
const MATCH_LOCATION_REMOTE_ID = 'location_remote_id'; |
22
|
|
|
|
23
|
|
|
protected $allowedConditions = array( |
24
|
|
|
self::MATCH_ALL, self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
25
|
|
|
self::MATCH_URL_ID, self::MATCH_URL, self::MATCH_LOCATION_ID, self::MATCH_LOCATION_REMOTE_ID, |
26
|
|
|
// aliases |
27
|
|
|
'id' |
28
|
|
|
); |
29
|
|
|
protected $returns = 'UrlALias'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
33
|
|
|
* @param bool $tolerateMisses |
34
|
|
|
* @return UrlAliasCollection |
35
|
|
|
* @throws InvalidMatchConditionsException |
36
|
|
|
* @throws NotFoundException |
37
|
|
|
*/ |
38
|
|
|
public function match(array $conditions, $tolerateMisses = false) |
39
|
|
|
{ |
40
|
|
|
return $this->matchUrlAlias($conditions, $tolerateMisses); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* NB: by default, only custom aliases will be matched for locations |
45
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
46
|
|
|
* @param bool $tolerateMisses |
47
|
|
|
* @return UrlAliasCollection |
48
|
|
|
* @throws InvalidMatchConditionsException |
49
|
|
|
* @throws NotFoundException |
50
|
|
|
*/ |
51
|
|
|
public function matchUrlAlias(array $conditions, $tolerateMisses = false) |
52
|
|
|
{ |
53
|
|
|
$this->validateConditions($conditions); |
54
|
|
|
|
55
|
|
|
foreach ($conditions as $key => $values) { |
56
|
|
|
|
57
|
|
|
if (!is_array($values)) { |
58
|
|
|
$values = array($values); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
switch ($key) { |
62
|
|
|
case 'id': |
63
|
|
|
case self::MATCH_URL_ID: |
64
|
|
|
return new UrlAliasCollection($this->findUrlAliasesById($values, $tolerateMisses)); |
65
|
|
|
|
66
|
|
|
case self::MATCH_URL: |
67
|
|
|
return new UrlAliasCollection($this->findUrlAliasesByUrl($values, $tolerateMisses)); |
68
|
|
|
|
69
|
|
|
case self::MATCH_LOCATION_ID: |
70
|
|
|
case self::MATCH_LOCATION_REMOTE_ID: |
71
|
|
|
return new UrlAliasCollection($this->findUrlAliasesByLocation($values, $tolerateMisses)); |
72
|
|
|
|
73
|
|
|
case self::MATCH_ALL: |
74
|
|
|
/// @todo this will most likely not surface custom location aliases |
75
|
|
|
return new UrlAliasCollection($this->findAllUrlAliases()); |
76
|
|
|
|
77
|
|
|
case self::MATCH_AND: |
78
|
|
|
return $this->matchAnd($values, $tolerateMisses = false); |
|
|
|
|
79
|
|
|
|
80
|
|
|
case self::MATCH_OR: |
81
|
|
|
return $this->matchOr($values, $tolerateMisses = false); |
|
|
|
|
82
|
|
|
|
83
|
|
|
case self::MATCH_NOT: |
84
|
|
|
/// @todo this will most likely not surface custom location aliases |
85
|
|
|
return new UrlAliasCollection(array_diff_key($this->findAllUrlAliases(), $this->matchUrlAlias($values, true)->getArrayCopy())); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function getConditionsFromKey($key) |
91
|
|
|
{ |
92
|
|
|
// The value for url_id matching is a string, so hard to tell it apart from matching eg. by url |
93
|
|
|
return array(self::MATCH_URL_ID => $key); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string[] $urlIds |
98
|
|
|
* @param bool $tolerateMisses |
99
|
|
|
* @return UrlAlias[] |
100
|
|
|
* @throws NotFoundException |
101
|
|
|
*/ |
102
|
|
|
protected function findUrlAliasesById(array $urlIds, $tolerateMisses = false) |
103
|
|
|
{ |
104
|
|
|
$urls = []; |
105
|
|
|
|
106
|
|
|
foreach ($urlIds as $urlId) { |
107
|
|
|
try { |
108
|
|
|
// return unique items |
109
|
|
|
$UrlAlias = $this->repository->getUrlAliasService()->load($urlId); |
110
|
|
|
$urls[$UrlAlias->id] = $UrlAlias; |
111
|
|
|
} catch (NotFoundException $e) { |
112
|
|
|
if (!$tolerateMisses) { |
113
|
|
|
throw $e; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $urls; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string[] $urls |
123
|
|
|
* @param bool $tolerateMisses |
124
|
|
|
* @return UrlAlias[] |
125
|
|
|
* @throws NotFoundException |
126
|
|
|
*/ |
127
|
|
|
protected function findUrlAliasesByUrl(array $urls, $tolerateMisses = false) |
|
|
|
|
128
|
|
|
{ |
129
|
|
|
$urls = []; |
130
|
|
|
|
131
|
|
|
foreach ($urls as $url) { |
132
|
|
|
try { |
133
|
|
|
// return unique contents |
134
|
|
|
$UrlAlias = $this->repository->getUrlAliasService()->lookup($url); |
135
|
|
|
$urls[$UrlAlias->id] = $UrlAlias; |
136
|
|
|
} catch (NotFoundException $e) { |
137
|
|
|
if (!$tolerateMisses) { |
138
|
|
|
throw $e; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $urls; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param int[]|string[] $locationIds |
148
|
|
|
* @param bool $tolerateMisses |
149
|
|
|
* @param bool|null $custom when null, return both custom and non-custom aliases. When false, only non-custom |
150
|
|
|
* @return UrlAlias[] |
151
|
|
|
* @throws NotFoundException |
152
|
|
|
*/ |
153
|
|
|
protected function findUrlAliasesByLocation(array $locationIds, $tolerateMisses = false, $custom = true) |
154
|
|
|
{ |
155
|
|
|
$urls = []; |
156
|
|
|
|
157
|
|
|
foreach ($locationIds as $locationId) { |
158
|
|
|
try { |
159
|
|
|
if (!is_int($locationId) && !ctype_digit($locationId)) { |
160
|
|
|
// presume it is a remote_id |
161
|
|
|
$location = $this->repository->getLocationService()->loadLocationByRemoteId($locationId); |
162
|
|
|
} else { |
163
|
|
|
$location = $this->repository->getLocationService()->loadLocation($locationId); |
164
|
|
|
} |
165
|
|
|
// return unique items |
166
|
|
|
if ($custom === null || !$custom) { |
167
|
|
|
foreach ($this->repository->getUrlAliasService()->listLocationAliases($location, false) as $UrlAlias) { |
168
|
|
|
$urls[$UrlAlias->id] = $UrlAlias; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
if ($custom === null || $custom) { |
172
|
|
|
foreach ($this->repository->getUrlAliasService()->listLocationAliases($location) as $UrlAlias) { |
173
|
|
|
$urls[$UrlAlias->id] = $UrlAlias; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} catch (NotFoundException $e) { |
177
|
|
|
if (!$tolerateMisses) { |
178
|
|
|
throw $e; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $urls; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @todo check: does this include all custom location aliases? If not, we should not allow matching with NOT |
188
|
|
|
* @return UrlAlias[] |
189
|
|
|
*/ |
190
|
|
|
protected function findAllUrlAliases() |
191
|
|
|
{ |
192
|
|
|
$urls = []; |
193
|
|
|
|
194
|
|
|
foreach ($this->repository->getUrlAliasService()->listGlobalAliases() as $UrlAlias) { |
195
|
|
|
// return unique contents |
196
|
|
|
$urls[$UrlAlias->id] = $UrlAlias; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $urls; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|