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
|
|
|
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
|
|
|
$this->clearLocation($locationId); |
48
|
|
|
|
49
|
|
|
$this->persistenceHandler->urlAliasHandler()->publishUrlAliasForLocation( |
50
|
|
|
$locationId, |
51
|
|
|
$parentLocationId, |
52
|
|
|
$name, |
53
|
|
|
$languageCode, |
54
|
|
|
$alwaysAvailable, |
55
|
|
|
$updatePathIdentificationString |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
63
|
|
|
{ |
64
|
|
|
$this->logger->logCall( |
65
|
|
|
__METHOD__, |
66
|
|
|
array( |
67
|
|
|
'location' => $locationId, |
68
|
|
|
'$path' => $path, |
69
|
|
|
'$forwarding' => $forwarding, |
70
|
|
|
'language' => $languageCode, |
71
|
|
|
'alwaysAvailable' => $alwaysAvailable, |
72
|
|
|
) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->createCustomUrlAlias( |
76
|
|
|
$locationId, |
77
|
|
|
$path, |
78
|
|
|
$forwarding, |
79
|
|
|
$languageCode, |
80
|
|
|
$alwaysAvailable |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
$this->cache->getItem('urlAlias', $urlAlias->id)->set($urlAlias)->save(); |
84
|
|
|
$cache = $this->cache->getItem('urlAlias', 'location', $urlAlias->destination, 'custom'); |
85
|
|
|
$urlAliasIds = $cache->get(); |
86
|
|
|
if ($cache->isMiss()) { |
87
|
|
|
$urlAliasIds = array(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$urlAliasIds[] = $urlAlias->id; |
91
|
|
|
$cache->set($urlAliasIds)->save(); |
92
|
|
|
|
93
|
|
|
return $urlAlias; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
100
|
|
|
{ |
101
|
|
|
$this->logger->logCall( |
102
|
|
|
__METHOD__, |
103
|
|
|
array( |
104
|
|
|
'resource' => $resource, |
105
|
|
|
'path' => $path, |
106
|
|
|
'forwarding' => $forwarding, |
107
|
|
|
'language' => $languageCode, |
108
|
|
|
'alwaysAvailable' => $alwaysAvailable, |
109
|
|
|
) |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->createGlobalUrlAlias( |
113
|
|
|
$resource, |
114
|
|
|
$path, |
115
|
|
|
$forwarding, |
116
|
|
|
$languageCode, |
117
|
|
|
$alwaysAvailable |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->cache->getItem('urlAlias', $urlAlias->id)->set($urlAlias)->save(); |
121
|
|
|
|
122
|
|
|
return $urlAlias; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
|
|
public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
129
|
|
|
{ |
130
|
|
|
$this->logger->logCall(__METHOD__, array('language' => $languageCode, 'offset' => $offset, 'limit' => $limit)); |
131
|
|
|
|
132
|
|
|
return $this->persistenceHandler->urlAliasHandler()->listGlobalURLAliases($languageCode, $offset, $limit); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
View Code Duplication |
public function listURLAliasesForLocation($locationId, $custom = false) |
139
|
|
|
{ |
140
|
|
|
// Look for location to list of url alias id's cache |
141
|
|
|
if ($custom) { |
142
|
|
|
$cache = $this->cache->getItem('urlAlias', 'location', $locationId, 'custom'); |
143
|
|
|
} else { |
144
|
|
|
$cache = $this->cache->getItem('urlAlias', 'location', $locationId); |
145
|
|
|
} |
146
|
|
|
$urlAliasIds = $cache->get(); |
147
|
|
|
if ($cache->isMiss()) { |
148
|
|
|
$this->logger->logCall(__METHOD__, array('location' => $locationId, 'custom' => $custom)); |
149
|
|
|
$urlAliases = $this->persistenceHandler->urlAliasHandler()->listURLAliasesForLocation($locationId, $custom); |
150
|
|
|
|
151
|
|
|
$urlAliasIds = array(); |
152
|
|
|
foreach ($urlAliases as $urlAlias) { |
153
|
|
|
$urlAliasIds[] = $urlAlias->id; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$cache->set($urlAliasIds)->save(); |
157
|
|
|
} else { |
158
|
|
|
// Reuse loadUrlAlias for the url alias object cache |
159
|
|
|
$urlAliases = array(); |
160
|
|
|
foreach ($urlAliasIds as $urlAliasId) { |
161
|
|
|
$urlAliases[] = $this->loadUrlAlias($urlAliasId); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $urlAliases; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* {@inheritdoc} |
170
|
|
|
*/ |
171
|
|
|
public function removeURLAliases(array $urlAliases) |
172
|
|
|
{ |
173
|
|
|
$this->logger->logCall(__METHOD__, array('aliases' => $urlAliases)); |
174
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->removeURLAliases($urlAliases); |
175
|
|
|
|
176
|
|
|
$this->cache->clear('urlAlias', 'url');//TIMBER! (no easy way to do reverse lookup of urls) |
177
|
|
|
foreach ($urlAliases as $urlAlias) { |
178
|
|
|
$this->cache->clear('urlAlias', $urlAlias->id); |
179
|
|
|
if ($urlAlias->type === UrlAlias::LOCATION) { |
180
|
|
|
$this->cache->clear('urlAlias', 'location', $urlAlias->destination); |
181
|
|
|
} |
182
|
|
|
if ($urlAlias->isCustom) { |
183
|
|
|
$this->cache->clear('urlAlias', 'location', $urlAlias->destination, 'custom'); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $return; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* {@inheritdoc} |
192
|
|
|
*/ |
193
|
|
|
public function lookup($url) |
194
|
|
|
{ |
195
|
|
|
// Look for url to url alias id cache |
196
|
|
|
// Replace slashes by "|" to be sure not to mix cache key combinations in underlying lib. |
197
|
|
|
$cacheKey = $url ?: '/'; |
198
|
|
|
$cache = $this->cache->getItem('urlAlias', 'url', $cacheKey); |
199
|
|
|
$urlAliasId = $cache->get(); |
200
|
|
|
if ($cache->isMiss()) { |
201
|
|
|
$urlAliasHistoryCache = $this->cache->getItem('urlAlias', 'url', 'history', $cacheKey); |
202
|
|
|
$historyUrlAlias = $urlAliasHistoryCache->get(); |
203
|
|
|
|
204
|
|
|
if (!$urlAliasHistoryCache->isMiss()) { |
205
|
|
|
return $historyUrlAlias; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// Also cache "not found" as this function is heavliy used and hance should be cached |
209
|
|
|
try { |
210
|
|
|
$this->logger->logCall(__METHOD__, array('url' => $url)); |
211
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->lookup($url); |
212
|
|
|
|
213
|
|
|
if ($urlAlias->isHistory) { |
214
|
|
|
$urlAliasHistoryCache->set($urlAlias)->save(); |
215
|
|
|
} else { |
216
|
|
|
$cache->set($urlAlias->id)->save(); |
217
|
|
|
$urlAliasCache = $this->cache->getItem('urlAlias', $urlAlias->id); |
218
|
|
|
$urlAliasCache->set($urlAlias)->save(); |
219
|
|
|
} |
220
|
|
|
} catch (APINotFoundException $e) { |
221
|
|
|
$cache->set(self::NOT_FOUND)->save(); |
222
|
|
|
throw $e; |
223
|
|
|
} |
224
|
|
|
} elseif ($urlAliasId === self::NOT_FOUND) { |
225
|
|
|
throw new NotFoundException('UrlAlias', $url); |
226
|
|
|
} else { |
227
|
|
|
$urlAlias = $this->loadUrlAlias($urlAliasId); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $urlAlias; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritdoc} |
235
|
|
|
*/ |
236
|
|
View Code Duplication |
public function loadUrlAlias($id) |
237
|
|
|
{ |
238
|
|
|
// Look for url alias cache |
239
|
|
|
$cache = $this->cache->getItem('urlAlias', $id); |
240
|
|
|
$urlAlias = $cache->get(); |
241
|
|
|
if ($cache->isMiss()) { |
242
|
|
|
$this->logger->logCall(__METHOD__, array('alias' => $id)); |
243
|
|
|
$urlAlias = $this->persistenceHandler->urlAliasHandler()->loadUrlAlias($id); |
244
|
|
|
$cache->set($urlAlias)->save(); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $urlAlias; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritdoc} |
252
|
|
|
*/ |
253
|
|
View Code Duplication |
public function locationMoved($locationId, $oldParentId, $newParentId) |
254
|
|
|
{ |
255
|
|
|
$this->logger->logCall( |
256
|
|
|
__METHOD__, |
257
|
|
|
array( |
258
|
|
|
'location' => $locationId, |
259
|
|
|
'oldParent' => $oldParentId, |
260
|
|
|
'newParent' => $newParentId, |
261
|
|
|
) |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationMoved($locationId, $oldParentId, $newParentId); |
265
|
|
|
$this->cache->clear('urlAlias', 'url');//TIMBER! (Will have to load url aliases for location to be able to clear specific entries) |
266
|
|
|
|
267
|
|
|
return $return; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* {@inheritdoc} |
272
|
|
|
*/ |
273
|
|
View Code Duplication |
public function locationCopied($locationId, $newLocationId, $newParentId) |
274
|
|
|
{ |
275
|
|
|
$this->logger->logCall( |
276
|
|
|
__METHOD__, |
277
|
|
|
array( |
278
|
|
|
'oldLocation' => $locationId, |
279
|
|
|
'newLocation' => $newLocationId, |
280
|
|
|
'newParent' => $newParentId, |
281
|
|
|
) |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationCopied( |
285
|
|
|
$locationId, |
286
|
|
|
$newLocationId, |
287
|
|
|
$newParentId |
288
|
|
|
); |
289
|
|
|
$this->cache->clear('urlAlias', 'url'); // required due to caching not found aliases |
290
|
|
|
|
291
|
|
|
return $return; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* {@inheritdoc} |
296
|
|
|
*/ |
297
|
|
|
public function locationDeleted($locationId) |
298
|
|
|
{ |
299
|
|
|
$this->logger->logCall(__METHOD__, array('location' => $locationId)); |
300
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationDeleted($locationId); |
301
|
|
|
|
302
|
|
|
$this->clearLocation($locationId); |
303
|
|
|
|
304
|
|
|
return $return; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* {@inheritdoc} |
309
|
|
|
*/ |
310
|
|
|
public function translationRemoved(array $locationIds, $languageCode) |
311
|
|
|
{ |
312
|
|
|
$this->logger->logCall( |
313
|
|
|
__METHOD__, |
314
|
|
|
['locations' => implode(',', $locationIds), 'language' => $languageCode] |
315
|
|
|
); |
316
|
|
|
|
317
|
|
|
$this->persistenceHandler->urlAliasHandler()->translationRemoved($locationIds, $languageCode); |
318
|
|
|
|
319
|
|
|
foreach ($locationIds as $locationId) { |
320
|
|
|
$this->clearLocation($locationId); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @param $locationId |
326
|
|
|
*/ |
327
|
|
|
protected function clearLocation($locationId) |
328
|
|
|
{ |
329
|
|
|
$locationCache = $this->cache->getItem('urlAlias', 'location', $locationId); |
330
|
|
|
|
331
|
|
|
if ($locationCache->isMiss()) { |
332
|
|
|
// we need to clear all if we don't have location id in cache |
333
|
|
|
$this->cache->clear('urlAlias'); |
334
|
|
|
} else { |
335
|
|
|
$urlAliasIds = $locationCache->get(); |
336
|
|
|
foreach ((array) $urlAliasIds as $urlAliasId) { |
337
|
|
|
$this->cache->clear('urlAlias', $urlAliasId); |
338
|
|
|
} |
339
|
|
|
$this->cache->clear('urlAlias', 'url'); |
340
|
|
|
$locationCache->clear(); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* {@inheritdoc} |
346
|
|
|
*/ |
347
|
|
View Code Duplication |
public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
348
|
|
|
{ |
349
|
|
|
$this->logger->logCall( |
350
|
|
|
__METHOD__, |
351
|
|
|
[ |
352
|
|
|
'location1Id' => $location1Id, |
353
|
|
|
'location1ParentId' => $location1ParentId, |
354
|
|
|
'location2Id' => $location2Id, |
355
|
|
|
'location2ParentId' => $location2ParentId, |
356
|
|
|
] |
357
|
|
|
); |
358
|
|
|
|
359
|
|
|
$return = $this->persistenceHandler->urlAliasHandler()->locationSwapped( |
360
|
|
|
$location1Id, |
361
|
|
|
$location1ParentId, |
362
|
|
|
$location2Id, |
363
|
|
|
$location2ParentId |
364
|
|
|
); |
365
|
|
|
|
366
|
|
|
$this->clearLocation($location1Id); |
367
|
|
|
$this->clearLocation($location2Id); |
368
|
|
|
|
369
|
|
|
return $return; |
370
|
|
|
} |
371
|
|
|
} |
372
|
|
|
|