1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the UrlAlias Handler implementation. |
5
|
|
|
* |
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
namespace eZ\Publish\Core\Persistence\Cache; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException as APINotFoundException; |
12
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
13
|
|
|
use eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler as UrlAliasHandlerInterface; |
14
|
|
|
use eZ\Publish\SPI\Persistence\Content\UrlAlias; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @see \eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler |
18
|
|
|
*/ |
19
|
|
|
class UrlAliasHandler extends AbstractHandler implements UrlAliasHandlerInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Constant used for storing not found results for lookup(). |
23
|
|
|
*/ |
24
|
|
|
const NOT_FOUND = 0; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
public function publishUrlAliasForLocation( |
30
|
|
|
$locationId, |
31
|
|
|
$parentLocationId, |
32
|
|
|
$name, |
33
|
|
|
$languageCode, |
34
|
|
|
$alwaysAvailable = false, |
35
|
|
|
$updatePathIdentificationString = false |
36
|
|
|
) { |
37
|
|
|
$this->logger->logCall( |
38
|
|
|
__METHOD__, |
39
|
|
|
array( |
40
|
|
|
'location' => $locationId, |
41
|
|
|
'parent' => $parentLocationId, |
42
|
|
|
'name' => $name, |
43
|
|
|
'language' => $languageCode, |
44
|
|
|
'alwaysAvailable' => $alwaysAvailable, |
45
|
|
|
) |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
49
|
|
|
$locationId, |
50
|
|
|
$parentLocationId, |
51
|
|
|
$name, |
52
|
|
|
$languageCode, |
53
|
|
|
$alwaysAvailable, |
54
|
|
|
$updatePathIdentificationString |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
$this->cache->invalidateTags(['urlAlias-location-' . $locationId, 'urlAlias-notFound']); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
64
|
|
|
{ |
65
|
|
|
$this->logger->logCall( |
66
|
|
|
__METHOD__, |
67
|
|
|
array( |
68
|
|
|
'location' => $locationId, |
69
|
|
|
'$path' => $path, |
70
|
|
|
'$forwarding' => $forwarding, |
71
|
|
|
'language' => $languageCode, |
72
|
|
|
'alwaysAvailable' => $alwaysAvailable, |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->createCustomUrlAlias( |
77
|
|
|
$locationId, |
78
|
|
|
$path, |
79
|
|
|
$forwarding, |
80
|
|
|
$languageCode, |
81
|
|
|
$alwaysAvailable |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
$this->cache->invalidateTags(['urlAlias-location-' . $locationId, 'urlAlias-notFound']); |
85
|
|
|
|
86
|
|
|
return $urlAlias; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
View Code Duplication |
public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
93
|
|
|
{ |
94
|
|
|
$this->logger->logCall( |
95
|
|
|
__METHOD__, |
96
|
|
|
array( |
97
|
|
|
'resource' => $resource, |
98
|
|
|
'path' => $path, |
99
|
|
|
'forwarding' => $forwarding, |
100
|
|
|
'language' => $languageCode, |
101
|
|
|
'alwaysAvailable' => $alwaysAvailable, |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->createGlobalUrlAlias( |
106
|
|
|
$resource, |
107
|
|
|
$path, |
108
|
|
|
$forwarding, |
109
|
|
|
$languageCode, |
110
|
|
|
$alwaysAvailable |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
$this->cache->invalidateTags(['urlAlias-notFound']); |
114
|
|
|
|
115
|
|
|
return $urlAlias; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
122
|
|
|
{ |
123
|
|
|
$this->logger->logCall(__METHOD__, array('language' => $languageCode, 'offset' => $offset, 'limit' => $limit)); |
124
|
|
|
|
125
|
|
|
return $this->persistenceHandler->urlAliasHandler()->listGlobalURLAliases($languageCode, $offset, $limit); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
public function listURLAliasesForLocation($locationId, $custom = false) |
132
|
|
|
{ |
133
|
|
|
$cacheItem = $this->cache->getItem('ez-urlAlias-location-list-' . $locationId . ($custom ? '-custom' : '')); |
134
|
|
|
if ($cacheItem->isHit()) { |
135
|
|
|
return $cacheItem->get(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->logger->logCall(__METHOD__, array('location' => $locationId, 'custom' => $custom)); |
139
|
|
|
$urlAliases = $this->persistenceHandler->urlAliasHandler()->listURLAliasesForLocation($locationId, $custom); |
140
|
|
|
|
141
|
|
|
$cacheItem->set($urlAliases); |
142
|
|
|
$cacheTags = ['urlAlias-location-' . $locationId]; |
143
|
|
|
foreach ($urlAliases as $urlAlias) { |
144
|
|
|
$cacheTags[] = 'urlAlias-' . $urlAlias->id; |
145
|
|
|
} |
146
|
|
|
$cacheItem->tag($cacheTags); |
147
|
|
|
$this->cache->save($cacheItem); |
148
|
|
|
|
149
|
|
|
return $urlAliases; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* {@inheritdoc} |
154
|
|
|
*/ |
155
|
|
|
public function removeURLAliases(array $urlAliases) |
156
|
|
|
{ |
157
|
|
|
$this->logger->logCall(__METHOD__, array('aliases' => $urlAliases)); |
158
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->removeURLAliases($urlAliases); |
159
|
|
|
|
160
|
|
|
$cacheTags = []; |
161
|
|
|
foreach ($urlAliases as $urlAlias) { |
162
|
|
|
$cacheTags[] = 'urlAlias-' . $urlAlias->id; |
163
|
|
|
if ($urlAlias->type === UrlAlias::LOCATION) { |
164
|
|
|
$cacheTags[] = 'urlAlias-location-' . $urlAlias->destination; |
165
|
|
|
} |
166
|
|
|
if ($urlAlias->isCustom) { |
167
|
|
|
$cacheTags[] = 'urlAlias-custom-' . $urlAlias->destination; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
$this->cache->invalidateTags($cacheTags); |
171
|
|
|
|
172
|
|
|
return $return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritdoc} |
177
|
|
|
*/ |
178
|
|
|
public function lookup($url) |
179
|
|
|
{ |
180
|
|
|
$cacheItem = $this->cache->getItem('ez-urlAlias-url-' . str_replace('/', '_', $url)); |
181
|
|
|
if ($cacheItem->isHit()) { |
182
|
|
|
if (($return = $cacheItem->get()) === self::NOT_FOUND) { |
183
|
|
|
throw new NotFoundException('UrlAlias', $url); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $return; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$this->logger->logCall(__METHOD__, array('url' => $url)); |
190
|
|
|
try { |
191
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->lookup($url); |
192
|
|
|
} catch (APINotFoundException $e) { |
193
|
|
|
$cacheItem->set(self::NOT_FOUND) |
194
|
|
|
->expiresAfter(30) |
195
|
|
|
->tag(['urlAlias-notFound']); |
196
|
|
|
$this->cache->save($cacheItem); |
197
|
|
|
throw $e; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$cacheItem->set($urlAlias); |
201
|
|
|
$cachTags = ['urlAlias-' . $urlAlias->id]; |
202
|
|
|
if ($urlAlias->type === UrlAlias::LOCATION) { |
203
|
|
|
$cachTags[] = 'urlAlias-location-' . $urlAlias->destination; |
204
|
|
|
} |
205
|
|
|
$cacheItem->tag($cachTags); |
206
|
|
|
$this->cache->save($cacheItem); |
207
|
|
|
|
208
|
|
|
return $urlAlias; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function loadUrlAlias($id) |
215
|
|
|
{ |
216
|
|
|
$cacheItem = $this->cache->getItem('ez-urlAlias-' . $id); |
217
|
|
|
if ($cacheItem->isHit()) { |
218
|
|
|
return $cacheItem->get(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$this->logger->logCall(__METHOD__, array('alias' => $id)); |
222
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->loadUrlAlias($id); |
223
|
|
|
|
224
|
|
|
$cacheItem->set($urlAlias); |
225
|
|
|
if ($urlAlias->type === UrlAlias::LOCATION) { |
226
|
|
|
$cacheItem->tag(['urlAlias-' . $urlAlias->id, 'urlAlias-location-' . $urlAlias->destination]); |
227
|
|
|
} else { |
228
|
|
|
$cacheItem->tag(['urlAlias-' . $urlAlias->id]); |
229
|
|
|
} |
230
|
|
|
$this->cache->save($cacheItem); |
231
|
|
|
|
232
|
|
|
return $urlAlias; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function locationMoved($locationId, $oldParentId, $newParentId) |
239
|
|
|
{ |
240
|
|
|
$this->logger->logCall( |
241
|
|
|
__METHOD__, |
242
|
|
|
array( |
243
|
|
|
'location' => $locationId, |
244
|
|
|
'oldParent' => $oldParentId, |
245
|
|
|
'newParent' => $newParentId, |
246
|
|
|
) |
247
|
|
|
); |
248
|
|
|
|
249
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationMoved($locationId, $oldParentId, $newParentId); |
250
|
|
|
|
251
|
|
|
$this->cache->invalidateTags(['urlAlias-location-' . $locationId]); |
252
|
|
|
|
253
|
|
|
return $return; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* {@inheritdoc} |
258
|
|
|
*/ |
259
|
|
View Code Duplication |
public function locationCopied($locationId, $newLocationId, $newParentId) |
260
|
|
|
{ |
261
|
|
|
$this->logger->logCall( |
262
|
|
|
__METHOD__, |
263
|
|
|
array( |
264
|
|
|
'oldLocation' => $locationId, |
265
|
|
|
'newLocation' => $newLocationId, |
266
|
|
|
'newParent' => $newParentId, |
267
|
|
|
) |
268
|
|
|
); |
269
|
|
|
|
270
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationCopied( |
271
|
|
|
$locationId, |
272
|
|
|
$newLocationId, |
273
|
|
|
$newParentId |
274
|
|
|
); |
275
|
|
|
$this->cache->invalidateTags(['urlAlias-location-' . $locationId, 'urlAlias-location-' . $newLocationId]); |
276
|
|
|
|
277
|
|
|
return $return; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* {@inheritdoc} |
282
|
|
|
*/ |
283
|
|
|
public function locationDeleted($locationId) |
284
|
|
|
{ |
285
|
|
|
$this->logger->logCall(__METHOD__, array('location' => $locationId)); |
286
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationDeleted($locationId); |
287
|
|
|
|
288
|
|
|
$this->cache->invalidateTags(['urlAlias-location-' . $locationId]); |
289
|
|
|
|
290
|
|
|
return $return; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritdoc} |
295
|
|
|
*/ |
296
|
|
|
public function translationRemoved(array $locationIds, $languageCode) |
297
|
|
|
{ |
298
|
|
|
$this->logger->logCall( |
299
|
|
|
__METHOD__, |
300
|
|
|
['locations' => implode(',', $locationIds), 'language' => $languageCode] |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
$this->persistenceHandler->urlAliasHandler()->translationRemoved($locationIds, $languageCode); |
304
|
|
|
|
305
|
|
|
$locationTags = []; |
306
|
|
|
foreach ($locationIds as $locationId) { |
307
|
|
|
$locationTags[] = 'urlAlias-location-' . $locationId; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
$this->cache->invalidateTags($locationTags); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* {@inheritdoc} |
315
|
|
|
*/ |
316
|
|
View Code Duplication |
public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
317
|
|
|
{ |
318
|
|
|
$this->logger->logCall( |
319
|
|
|
__METHOD__, |
320
|
|
|
[ |
321
|
|
|
'location1Id' => $location1Id, |
322
|
|
|
'location1ParentId' => $location1ParentId, |
323
|
|
|
'location2Id' => $location2Id, |
324
|
|
|
'location2ParentId' => $location2ParentId, |
325
|
|
|
] |
326
|
|
|
); |
327
|
|
|
|
328
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationSwapped( |
329
|
|
|
$location1Id, |
330
|
|
|
$location1ParentId, |
331
|
|
|
$location2Id, |
332
|
|
|
$location2ParentId |
333
|
|
|
); |
334
|
|
|
|
335
|
|
|
$this->cache->invalidateTags(['urlAlias-location-' . $location1Id, 'urlAlias-location-' . $location2Id]); |
336
|
|
|
|
337
|
|
|
return $return; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|