1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the UrlAlias Handler. |
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\Legacy\Content\UrlAlias; |
10
|
|
|
|
11
|
|
|
use eZ\Publish\SPI\Persistence\Content\UrlAlias\Handler as UrlAliasHandlerInterface; |
12
|
|
|
use eZ\Publish\SPI\Persistence\Content\Language\Handler as LanguageHandler; |
13
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway as LocationGateway; |
14
|
|
|
use eZ\Publish\Core\Base\Exceptions\NotFoundException; |
15
|
|
|
use eZ\Publish\Core\Base\Exceptions\ForbiddenException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The UrlAlias Handler provides nice urls management. |
19
|
|
|
* |
20
|
|
|
* Its methods operate on a representation of the url alias data structure held |
21
|
|
|
* inside a storage engine. |
22
|
|
|
*/ |
23
|
|
|
class Handler implements UrlAliasHandlerInterface |
24
|
|
|
{ |
25
|
|
|
const ROOT_LOCATION_ID = 1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This is intentionally hardcoded for now as: |
29
|
|
|
* 1. We don't implement this configuration option. |
30
|
|
|
* 2. Such option should not be in this layer, should be handled higher up. |
31
|
|
|
* |
32
|
|
|
* @deprecated |
33
|
|
|
*/ |
34
|
|
|
const CONTENT_REPOSITORY_ROOT_LOCATION_ID = 2; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* UrlAlias Gateway. |
38
|
|
|
* |
39
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway |
40
|
|
|
*/ |
41
|
|
|
protected $gateway; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gateway for handling location data. |
45
|
|
|
* |
46
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway |
47
|
|
|
*/ |
48
|
|
|
protected $locationGateway; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* UrlAlias Mapper. |
52
|
|
|
* |
53
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper |
54
|
|
|
*/ |
55
|
|
|
protected $mapper; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Caching language handler. |
59
|
|
|
* |
60
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\CachingHandler |
61
|
|
|
*/ |
62
|
|
|
protected $languageHandler; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* URL slug converter. |
66
|
|
|
* |
67
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter |
68
|
|
|
*/ |
69
|
|
|
protected $slugConverter; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Creates a new UrlAlias Handler. |
73
|
|
|
* |
74
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Gateway $gateway |
75
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Mapper $mapper |
76
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Gateway $locationGateway |
77
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
78
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\SlugConverter $slugConverter |
79
|
|
|
*/ |
80
|
|
|
public function __construct( |
81
|
|
|
Gateway $gateway, |
82
|
|
|
Mapper $mapper, |
83
|
|
|
LocationGateway $locationGateway, |
84
|
|
|
LanguageHandler $languageHandler, |
85
|
|
|
SlugConverter $slugConverter |
86
|
|
|
) { |
87
|
|
|
$this->gateway = $gateway; |
88
|
|
|
$this->mapper = $mapper; |
89
|
|
|
$this->locationGateway = $locationGateway; |
90
|
|
|
$this->languageHandler = $languageHandler; |
|
|
|
|
91
|
|
|
$this->slugConverter = $slugConverter; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function publishUrlAliasForLocation( |
95
|
|
|
$locationId, |
96
|
|
|
$parentLocationId, |
97
|
|
|
$name, |
98
|
|
|
$languageCode, |
99
|
|
|
$alwaysAvailable = false, |
100
|
|
|
$updatePathIdentificationString = false |
101
|
|
|
) { |
102
|
|
|
$languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id; |
103
|
|
|
|
104
|
|
|
$this->internalPublishUrlAliasForLocation( |
105
|
|
|
$locationId, |
106
|
|
|
$parentLocationId, |
107
|
|
|
$name, |
108
|
|
|
$languageId, |
109
|
|
|
$alwaysAvailable, |
110
|
|
|
$updatePathIdentificationString |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Internal publish method, accepting language ID instead of language code and optionally |
116
|
|
|
* new alias ID (used when swapping Locations). |
117
|
|
|
* |
118
|
|
|
* @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
119
|
|
|
* |
120
|
|
|
* @param int $locationId |
121
|
|
|
* @param int $parentLocationId |
122
|
|
|
* @param string $name |
123
|
|
|
* @param int $languageId |
124
|
|
|
* @param bool $alwaysAvailable |
125
|
|
|
* @param bool $updatePathIdentificationString legacy storage specific for updating ezcontentobject_tree.path_identification_string |
126
|
|
|
* @param int $newId |
127
|
|
|
*/ |
128
|
|
|
private function internalPublishUrlAliasForLocation( |
129
|
|
|
$locationId, |
130
|
|
|
$parentLocationId, |
131
|
|
|
$name, |
132
|
|
|
$languageId, |
133
|
|
|
$alwaysAvailable = false, |
134
|
|
|
$updatePathIdentificationString = false, |
135
|
|
|
$newId = null |
136
|
|
|
) { |
137
|
|
|
$parentId = $this->getRealAliasId($parentLocationId); |
138
|
|
|
$name = $this->slugConverter->convert($name, 'location_' . $locationId); |
139
|
|
|
$uniqueCounter = $this->slugConverter->getUniqueCounterValue($name, $parentId == 0); |
140
|
|
|
$languageMask = $languageId | (int)$alwaysAvailable; |
141
|
|
|
$action = 'eznode:' . $locationId; |
142
|
|
|
$cleanup = false; |
143
|
|
|
|
144
|
|
|
// Exiting the loop with break; |
145
|
|
|
while (true) { |
146
|
|
|
$newText = ''; |
147
|
|
|
if ($locationId != self::CONTENT_REPOSITORY_ROOT_LOCATION_ID) { |
|
|
|
|
148
|
|
|
$newText = $name . ($uniqueCounter > 1 ? $uniqueCounter : ''); |
149
|
|
|
} |
150
|
|
|
$newTextMD5 = $this->getHash($newText); |
151
|
|
|
|
152
|
|
|
// Try to load existing entry |
153
|
|
|
$row = $this->gateway->loadRow($parentId, $newTextMD5); |
154
|
|
|
|
155
|
|
|
// If nothing was returned insert new entry |
156
|
|
|
if (empty($row)) { |
157
|
|
|
// Check for existing active location entry on this level and reuse it's id |
158
|
|
|
$existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
159
|
|
|
if (!empty($existingLocationEntry)) { |
160
|
|
|
$cleanup = true; |
161
|
|
|
$newId = $existingLocationEntry['id']; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$newId = $this->gateway->insertRow( |
165
|
|
|
array( |
166
|
|
|
'id' => $newId, |
167
|
|
|
'link' => $newId, |
168
|
|
|
'parent' => $parentId, |
169
|
|
|
'action' => $action, |
170
|
|
|
'lang_mask' => $languageMask, |
171
|
|
|
'text' => $newText, |
172
|
|
|
'text_md5' => $newTextMD5, |
173
|
|
|
) |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
break; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Row exists, check if it is reusable. There are 3 cases when this is possible: |
180
|
|
|
// 1. NOP entry |
181
|
|
|
// 2. existing location or custom alias entry |
182
|
|
|
// 3. history entry |
183
|
|
|
if ($row['action'] == 'nop:' || $row['action'] == $action || $row['is_original'] == 0) { |
184
|
|
|
// Check for existing location entry on this level, if it exists and it's id differs from reusable |
185
|
|
|
// entry id then reusable entry should be updated with the existing location entry id. |
186
|
|
|
// Note: existing location entry may be downgraded and relinked later, depending on its language. |
187
|
|
|
$existingLocationEntry = $this->gateway->loadAutogeneratedEntry($action, $parentId); |
188
|
|
|
|
189
|
|
|
if (!empty($existingLocationEntry)) { |
190
|
|
|
// Always cleanup when active autogenerated entry exists on the same level |
191
|
|
|
$cleanup = true; |
192
|
|
|
$newId = $existingLocationEntry['id']; |
193
|
|
|
if ($existingLocationEntry['id'] == $row['id']) { |
194
|
|
|
// If we are reusing existing location entry merge existing language mask |
195
|
|
|
$languageMask |= ($row['lang_mask'] & ~1); |
196
|
|
|
} |
197
|
|
|
} elseif ($newId === null) { |
198
|
|
|
// Use reused row ID only if publishing normally, else use given $newId |
199
|
|
|
$newId = $row['id']; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
$this->gateway->updateRow( |
203
|
|
|
$parentId, |
204
|
|
|
$newTextMD5, |
205
|
|
|
array( |
206
|
|
|
'action' => $action, |
207
|
|
|
// In case when NOP row was reused |
208
|
|
|
'action_type' => 'eznode', |
209
|
|
|
'lang_mask' => $languageMask, |
210
|
|
|
// Updating text ensures that letter case changes are stored |
211
|
|
|
'text' => $newText, |
212
|
|
|
// Set "id" and "link" for case when reusable entry is history |
213
|
|
|
'id' => $newId, |
214
|
|
|
'link' => $newId, |
215
|
|
|
// Entry should be active location entry (original and not alias). |
216
|
|
|
// Note: this takes care of taking over custom alias entry for the location on the same level |
217
|
|
|
// and with same name and action. |
218
|
|
|
'alias_redirects' => 1, |
219
|
|
|
'is_original' => 1, |
220
|
|
|
'is_alias' => 0, |
221
|
|
|
) |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
break; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// If existing row is not reusable, increment $uniqueCounter and try again |
228
|
|
|
$uniqueCounter += 1; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/* @var $newText */ |
232
|
|
|
if ($updatePathIdentificationString) { |
233
|
|
|
$this->locationGateway->updatePathIdentificationString( |
234
|
|
|
$locationId, |
235
|
|
|
$parentLocationId, |
236
|
|
|
$this->slugConverter->convert($newText, 'node_' . $locationId, 'urlalias_compat') |
|
|
|
|
237
|
|
|
); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/* @var $newId */ |
241
|
|
|
/* @var $newTextMD5 */ |
242
|
|
|
// Note: cleanup does not touch custom and global entries |
243
|
|
|
if ($cleanup) { |
244
|
|
|
$this->gateway->cleanupAfterPublish($action, $languageId, $newId, $parentId, $newTextMD5); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Create a user chosen $alias pointing to $locationId in $languageCode. |
250
|
|
|
* |
251
|
|
|
* If $languageCode is null the $alias is created in the system's default |
252
|
|
|
* language. $alwaysAvailable makes the alias available in all languages. |
253
|
|
|
* |
254
|
|
|
* @param mixed $locationId |
255
|
|
|
* @param string $path |
256
|
|
|
* @param bool $forwarding |
257
|
|
|
* @param string $languageCode |
258
|
|
|
* @param bool $alwaysAvailable |
259
|
|
|
* |
260
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
261
|
|
|
*/ |
262
|
|
|
public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
263
|
|
|
{ |
264
|
|
|
return $this->createUrlAlias( |
265
|
|
|
'eznode:' . $locationId, |
266
|
|
|
$path, |
267
|
|
|
$forwarding, |
268
|
|
|
$languageCode, |
269
|
|
|
$alwaysAvailable |
270
|
|
|
); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Create a user chosen $alias pointing to a resource in $languageCode. |
275
|
|
|
* This method does not handle location resources - if a user enters a location target |
276
|
|
|
* the createCustomUrlAlias method has to be used. |
277
|
|
|
* |
278
|
|
|
* If $languageCode is null the $alias is created in the system's default |
279
|
|
|
* language. $alwaysAvailable makes the alias available in all languages. |
280
|
|
|
* |
281
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\ForbiddenException if the path already exists for the given language |
282
|
|
|
* |
283
|
|
|
* @param string $resource |
284
|
|
|
* @param string $path |
285
|
|
|
* @param bool $forwarding |
286
|
|
|
* @param string $languageCode |
287
|
|
|
* @param bool $alwaysAvailable |
288
|
|
|
* |
289
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
290
|
|
|
*/ |
291
|
|
|
public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
292
|
|
|
{ |
293
|
|
|
return $this->createUrlAlias( |
294
|
|
|
$resource, |
295
|
|
|
$path, |
296
|
|
|
$forwarding, |
297
|
|
|
$languageCode, |
298
|
|
|
$alwaysAvailable |
299
|
|
|
); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Internal method for creating global or custom URL alias (these are handled in the same way). |
304
|
|
|
* |
305
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\ForbiddenException if the path already exists for the given language |
306
|
|
|
* |
307
|
|
|
* @param string $action |
308
|
|
|
* @param string $path |
309
|
|
|
* @param bool $forward |
310
|
|
|
* @param string|null $languageCode |
311
|
|
|
* @param bool $alwaysAvailable |
312
|
|
|
* |
313
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
314
|
|
|
*/ |
315
|
|
|
protected function createUrlAlias($action, $path, $forward, $languageCode, $alwaysAvailable) |
316
|
|
|
{ |
317
|
|
|
$pathElements = explode('/', $path); |
318
|
|
|
$topElement = array_pop($pathElements); |
319
|
|
|
$languageId = $this->languageHandler->loadByLanguageCode($languageCode)->id; |
320
|
|
|
$parentId = 0; |
321
|
|
|
|
322
|
|
|
// Handle all path elements except topmost one |
323
|
|
|
$isPathNew = false; |
324
|
|
|
foreach ($pathElements as $level => $pathElement) { |
325
|
|
|
$pathElement = $this->slugConverter->convert($pathElement, 'noname' . ($level + 1)); |
326
|
|
|
$pathElementMD5 = $this->getHash($pathElement); |
327
|
|
|
if (!$isPathNew) { |
328
|
|
|
$row = $this->gateway->loadRow($parentId, $pathElementMD5); |
329
|
|
|
if (empty($row)) { |
330
|
|
|
$isPathNew = true; |
331
|
|
|
} else { |
332
|
|
|
$parentId = $row['link']; |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
if ($isPathNew) { |
337
|
|
|
$parentId = $this->insertNopEntry($parentId, $pathElement, $pathElementMD5); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
// Handle topmost path element |
342
|
|
|
$topElement = $this->slugConverter->convert($topElement, 'noname' . (count($pathElements) + 1)); |
343
|
|
|
|
344
|
|
|
// If last (next to topmost) entry parent is special root entry we handle topmost entry as first level entry |
345
|
|
|
// That is why we need to reset $parentId to 0 |
346
|
|
|
if ($parentId != 0 && $this->gateway->isRootEntry($parentId)) { |
347
|
|
|
$parentId = 0; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$topElementMD5 = $this->getHash($topElement); |
351
|
|
|
// Set common values for two cases below |
352
|
|
|
$data = array( |
353
|
|
|
'action' => $action, |
354
|
|
|
'is_alias' => 1, |
355
|
|
|
'alias_redirects' => $forward ? 1 : 0, |
356
|
|
|
'parent' => $parentId, |
357
|
|
|
'text' => $topElement, |
358
|
|
|
'text_md5' => $topElementMD5, |
359
|
|
|
'is_original' => 1, |
360
|
|
|
); |
361
|
|
|
// Try to load topmost element |
362
|
|
|
if (!$isPathNew) { |
363
|
|
|
$row = $this->gateway->loadRow($parentId, $topElementMD5); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
// If nothing was returned perform insert |
367
|
|
|
if ($isPathNew || empty($row)) { |
368
|
|
|
$data['lang_mask'] = $languageId | (int)$alwaysAvailable; |
369
|
|
|
$id = $this->gateway->insertRow($data); |
370
|
|
|
} elseif ($row['action'] == 'nop:' || $row['is_original'] == 0) { |
371
|
|
|
// Row exists, check if it is reusable. There are 2 cases when this is possible: |
372
|
|
|
// 1. NOP entry |
373
|
|
|
// 2. history entry |
374
|
|
|
$data['lang_mask'] = $languageId | (int)$alwaysAvailable; |
375
|
|
|
// If history is reused move link to id |
376
|
|
|
$data['link'] = $id = $row['id']; |
377
|
|
|
$this->gateway->updateRow( |
378
|
|
|
$parentId, |
379
|
|
|
$topElementMD5, |
380
|
|
|
$data |
381
|
|
|
); |
382
|
|
|
} else { |
383
|
|
|
throw new ForbiddenException("Path '%path%' already exists for the given language", ['%path%' => $path]); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
$data['raw_path_data'] = $this->gateway->loadPathData($id); |
387
|
|
|
|
388
|
|
|
return $this->mapper->extractUrlAliasFromData($data); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Convenience method for inserting nop type row. |
393
|
|
|
* |
394
|
|
|
* @param mixed $parentId |
395
|
|
|
* @param string $text |
396
|
|
|
* @param string $textMD5 |
397
|
|
|
* |
398
|
|
|
* @return mixed |
399
|
|
|
*/ |
400
|
|
|
protected function insertNopEntry($parentId, $text, $textMD5) |
401
|
|
|
{ |
402
|
|
|
return $this->gateway->insertRow( |
403
|
|
|
array( |
404
|
|
|
'lang_mask' => 1, |
405
|
|
|
'action' => 'nop:', |
406
|
|
|
'parent' => $parentId, |
407
|
|
|
'text' => $text, |
408
|
|
|
'text_md5' => $textMD5, |
409
|
|
|
) |
410
|
|
|
); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* List of user generated or autogenerated url entries, pointing to $locationId. |
415
|
|
|
* |
416
|
|
|
* @param mixed $locationId |
417
|
|
|
* @param bool $custom if true the user generated aliases are listed otherwise the autogenerated |
418
|
|
|
* |
419
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
420
|
|
|
*/ |
421
|
|
View Code Duplication |
public function listURLAliasesForLocation($locationId, $custom = false) |
422
|
|
|
{ |
423
|
|
|
$data = $this->gateway->loadLocationEntries($locationId, $custom); |
424
|
|
|
foreach ($data as &$entry) { |
425
|
|
|
$entry['raw_path_data'] = $this->gateway->loadPathData($entry['id']); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
return $this->mapper->extractUrlAliasListFromData($data); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* List global aliases. |
433
|
|
|
* |
434
|
|
|
* @param string|null $languageCode |
435
|
|
|
* @param int $offset |
436
|
|
|
* @param int $limit |
437
|
|
|
* |
438
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
439
|
|
|
*/ |
440
|
|
View Code Duplication |
public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
441
|
|
|
{ |
442
|
|
|
$data = $this->gateway->listGlobalEntries($languageCode, $offset, $limit); |
443
|
|
|
foreach ($data as &$entry) { |
444
|
|
|
$entry['raw_path_data'] = $this->gateway->loadPathData($entry['id']); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
return $this->mapper->extractUrlAliasListFromData($data); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Removes url aliases. |
452
|
|
|
* |
453
|
|
|
* Autogenerated aliases are not removed by this method. |
454
|
|
|
* |
455
|
|
|
* @param \eZ\Publish\SPI\Persistence\Content\UrlAlias[] $urlAliases |
456
|
|
|
* |
457
|
|
|
* @return bool |
458
|
|
|
*/ |
459
|
|
|
public function removeURLAliases(array $urlAliases) |
460
|
|
|
{ |
461
|
|
|
foreach ($urlAliases as $urlAlias) { |
462
|
|
|
if ($urlAlias->isCustom) { |
463
|
|
|
list($parentId, $textMD5) = explode('-', $urlAlias->id); |
464
|
|
|
if (!$this->gateway->removeCustomAlias($parentId, $textMD5)) { |
465
|
|
|
return false; |
466
|
|
|
} |
467
|
|
|
} |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
return true; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* Looks up a url alias for the given url. |
475
|
|
|
* |
476
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
477
|
|
|
* @throws \RuntimeException |
478
|
|
|
* @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException |
479
|
|
|
* |
480
|
|
|
* @param string $url |
481
|
|
|
* |
482
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
483
|
|
|
*/ |
484
|
|
|
public function lookup($url) |
485
|
|
|
{ |
486
|
|
|
$urlHashes = array(); |
487
|
|
|
foreach (explode('/', $url) as $level => $text) { |
488
|
|
|
$urlHashes[$level] = $this->getHash($text); |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
$data = $this->gateway->loadUrlAliasData($urlHashes); |
492
|
|
|
if (empty($data)) { |
493
|
|
|
throw new NotFoundException('URLAlias', $url); |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
$pathDepth = count($urlHashes); |
497
|
|
|
$hierarchyData = array(); |
498
|
|
|
$isPathHistory = false; |
499
|
|
|
for ($level = 0; $level < $pathDepth; ++$level) { |
500
|
|
|
$prefix = $level === $pathDepth - 1 ? '' : 'ezurlalias_ml' . $level . '_'; |
501
|
|
|
$isPathHistory = $isPathHistory ?: ($data[$prefix . 'link'] != $data[$prefix . 'id']); |
502
|
|
|
$hierarchyData[$level] = array( |
503
|
|
|
'id' => $data[$prefix . 'id'], |
504
|
|
|
'parent' => $data[$prefix . 'parent'], |
505
|
|
|
'action' => $data[$prefix . 'action'], |
506
|
|
|
); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
$data['is_path_history'] = $isPathHistory; |
510
|
|
|
$data['raw_path_data'] = ($data['action_type'] == 'eznode' && !$data['is_alias']) |
511
|
|
|
? $this->gateway->loadPathDataByHierarchy($hierarchyData) |
512
|
|
|
: $this->gateway->loadPathData($data['id']); |
513
|
|
|
|
514
|
|
|
return $this->mapper->extractUrlAliasFromData($data); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
/** |
518
|
|
|
* Loads URL alias by given $id. |
519
|
|
|
* |
520
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException |
521
|
|
|
* |
522
|
|
|
* @param string $id |
523
|
|
|
* |
524
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
525
|
|
|
*/ |
526
|
|
|
public function loadUrlAlias($id) |
527
|
|
|
{ |
528
|
|
|
list($parentId, $textMD5) = explode('-', $id); |
529
|
|
|
$data = $this->gateway->loadRow($parentId, $textMD5); |
530
|
|
|
|
531
|
|
|
if (empty($data)) { |
532
|
|
|
throw new NotFoundException('URLAlias', $id); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
$data['raw_path_data'] = $this->gateway->loadPathData($data['id']); |
536
|
|
|
|
537
|
|
|
return $this->mapper->extractUrlAliasFromData($data); |
538
|
|
|
} |
539
|
|
|
|
540
|
|
|
/** |
541
|
|
|
* Notifies the underlying engine that a location has moved. |
542
|
|
|
* |
543
|
|
|
* This method triggers the change of the autogenerated aliases. |
544
|
|
|
* |
545
|
|
|
* @param mixed $locationId |
546
|
|
|
* @param mixed $oldParentId |
547
|
|
|
* @param mixed $newParentId |
548
|
|
|
*/ |
549
|
|
|
public function locationMoved($locationId, $oldParentId, $newParentId) |
550
|
|
|
{ |
551
|
|
|
// @todo optimize: $newLocationAliasId is already available in self::publishUrlAliasForLocation() as $newId |
552
|
|
|
$newParentLocationAliasId = $this->getRealAliasId($newParentId); |
553
|
|
|
$newLocationAlias = $this->gateway->loadAutogeneratedEntry( |
554
|
|
|
'eznode:' . $locationId, |
555
|
|
|
$newParentLocationAliasId |
556
|
|
|
); |
557
|
|
|
|
558
|
|
|
$oldParentLocationAliasId = $this->getRealAliasId($oldParentId); |
559
|
|
|
$oldLocationAlias = $this->gateway->loadAutogeneratedEntry( |
560
|
|
|
'eznode:' . $locationId, |
561
|
|
|
$oldParentLocationAliasId |
562
|
|
|
); |
563
|
|
|
|
564
|
|
|
// Historize alias for old location |
565
|
|
|
$this->gateway->historizeId($oldLocationAlias['id'], $newLocationAlias['id']); |
566
|
|
|
// Reparent subtree of old location to new location |
567
|
|
|
$this->gateway->reparent($oldLocationAlias['id'], $newLocationAlias['id']); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
/** |
571
|
|
|
* Notifies the underlying engine that a location was copied. |
572
|
|
|
* |
573
|
|
|
* This method triggers the creation of the autogenerated aliases for the copied locations |
574
|
|
|
* |
575
|
|
|
* @param mixed $locationId |
576
|
|
|
* @param mixed $newLocationId |
577
|
|
|
* @param mixed $newParentId |
578
|
|
|
*/ |
579
|
|
|
public function locationCopied($locationId, $newLocationId, $newParentId) |
580
|
|
|
{ |
581
|
|
|
$newParentAliasId = $this->getRealAliasId($newLocationId); |
582
|
|
|
$oldParentAliasId = $this->getRealAliasId($locationId); |
583
|
|
|
|
584
|
|
|
$actionMap = $this->getCopiedLocationsMap($locationId, $newLocationId); |
585
|
|
|
|
586
|
|
|
$this->copySubtree( |
587
|
|
|
$actionMap, |
588
|
|
|
$oldParentAliasId, |
589
|
|
|
$newParentAliasId |
590
|
|
|
); |
591
|
|
|
} |
592
|
|
|
|
593
|
|
|
public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
594
|
|
|
{ |
595
|
|
|
$location1Entries = $this->gateway->loadLocationEntries($location1Id); |
596
|
|
|
$location2Entries = $this->gateway->loadLocationEntries($location2Id); |
597
|
|
|
|
598
|
|
|
$location1MainLanguageId = $this->gateway->getLocationContentMainLanguageId($location1Id); |
599
|
|
|
$location2MainLanguageId = $this->gateway->getLocationContentMainLanguageId($location2Id); |
600
|
|
|
|
601
|
|
|
// Load autogenerated entries to find alias ID |
602
|
|
|
$autoLocation1 = $this->gateway->loadAutogeneratedEntry("eznode:{$location1Id}"); |
603
|
|
|
$autoLocation2 = $this->gateway->loadAutogeneratedEntry("eznode:{$location2Id}"); |
604
|
|
|
|
605
|
|
|
// Historize everything first to avoid name conflicts in case swapped Locations are siblings |
606
|
|
|
$this->historizeBeforeSwap($location1Entries, $location2Entries); |
607
|
|
|
|
608
|
|
View Code Duplication |
foreach ($location2Entries as $row) { |
|
|
|
|
609
|
|
|
$alwaysAvailable = (bool)($row['lang_mask'] & 1); |
610
|
|
|
$languageIds = $this->extractLanguageIdsFromMask($row['lang_mask']); |
611
|
|
|
|
612
|
|
|
foreach ($languageIds as $languageId) { |
613
|
|
|
$isMainLanguage = $languageId == $location2MainLanguageId; |
614
|
|
|
$this->internalPublishUrlAliasForLocation( |
615
|
|
|
$location1Id, |
616
|
|
|
$location1ParentId, |
617
|
|
|
$row['text'], |
618
|
|
|
$languageId, |
619
|
|
|
$isMainLanguage && $alwaysAvailable, |
620
|
|
|
$isMainLanguage, |
621
|
|
|
$autoLocation1['id'] |
622
|
|
|
); |
623
|
|
|
} |
624
|
|
|
} |
625
|
|
|
|
626
|
|
View Code Duplication |
foreach ($location1Entries as $row) { |
|
|
|
|
627
|
|
|
$alwaysAvailable = (bool)($row['lang_mask'] & 1); |
628
|
|
|
$languageIds = $this->extractLanguageIdsFromMask($row['lang_mask']); |
629
|
|
|
|
630
|
|
|
foreach ($languageIds as $languageId) { |
631
|
|
|
$isMainLanguage = $languageId == $location1MainLanguageId; |
632
|
|
|
$this->internalPublishUrlAliasForLocation( |
633
|
|
|
$location2Id, |
634
|
|
|
$location2ParentId, |
635
|
|
|
$row['text'], |
636
|
|
|
$languageId, |
637
|
|
|
$isMainLanguage && $alwaysAvailable, |
638
|
|
|
$isMainLanguage, |
639
|
|
|
$autoLocation2['id'] |
640
|
|
|
); |
641
|
|
|
} |
642
|
|
|
} |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
/** |
646
|
|
|
* Historizes given existing active entries for two swapped Locations. |
647
|
|
|
* |
648
|
|
|
* This should be done before republishing URL aliases, in order to avoid unnecessary |
649
|
|
|
* conflicts when swapped Locations are siblings. |
650
|
|
|
* |
651
|
|
|
* We need to historize everything separately per language (mask), in case the entries |
652
|
|
|
* remain history future publishing reusages need to be able to take them over cleanly. |
653
|
|
|
* |
654
|
|
|
* @see \eZ\Publish\Core\Persistence\Legacy\Content\UrlAlias\Handler::locationSwapped() |
655
|
|
|
* |
656
|
|
|
* @param array $location1Entries |
657
|
|
|
* @param array $location2Entries |
658
|
|
|
*/ |
659
|
|
|
private function historizeBeforeSwap($location1Entries, $location2Entries) |
660
|
|
|
{ |
661
|
|
|
foreach ($location1Entries as $row) { |
662
|
|
|
$this->gateway->historizeBeforeSwap($row['action'], $row['lang_mask']); |
663
|
|
|
} |
664
|
|
|
|
665
|
|
|
foreach ($location2Entries as $row) { |
666
|
|
|
$this->gateway->historizeBeforeSwap($row['action'], $row['lang_mask']); |
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
/** |
671
|
|
|
* Extracts every language Ids contained in $languageMask. |
672
|
|
|
* |
673
|
|
|
* @param int $languageMask |
674
|
|
|
* |
675
|
|
|
* @return int[] An array of language IDs |
676
|
|
|
*/ |
677
|
|
|
private function extractLanguageIdsFromMask($languageMask) |
678
|
|
|
{ |
679
|
|
|
$exp = 2; |
680
|
|
|
$languageIds = []; |
681
|
|
|
|
682
|
|
|
// Decomposition of $languageMask into its binary components. |
683
|
|
|
while ($exp <= $languageMask) { |
684
|
|
|
if ($languageMask & $exp) { |
685
|
|
|
$languageIds[] = $exp; |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
$exp *= 2; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
return $languageIds; |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
/** |
695
|
|
|
* Returns possibly corrected alias id for given $locationId !! For use as parent id in logic. |
696
|
|
|
* |
697
|
|
|
* First level entries must have parent id set to 0 instead of their parent location alias id. |
698
|
|
|
* There are two cases when alias id needs to be corrected: |
699
|
|
|
* 1) location is special location without URL alias (location with id=1 in standard installation) |
700
|
|
|
* 2) location is site root location, having special root entry in the ezurlalias_ml table (location with id=2 |
701
|
|
|
* in standard installation) |
702
|
|
|
* |
703
|
|
|
* @param mixed $locationId |
704
|
|
|
* |
705
|
|
|
* @return mixed |
706
|
|
|
*/ |
707
|
|
|
protected function getRealAliasId($locationId) |
708
|
|
|
{ |
709
|
|
|
// Absolute root location does have a url alias entry so we can skip lookup |
710
|
|
|
if ($locationId == self::ROOT_LOCATION_ID) { |
711
|
|
|
return 0; |
712
|
|
|
} |
713
|
|
|
|
714
|
|
|
$data = $this->gateway->loadAutogeneratedEntry('eznode:' . $locationId); |
715
|
|
|
|
716
|
|
|
// Root entries (URL wise) can return 0 as the returned value is used as parent (parent is 0 for root entries) |
717
|
|
|
if (empty($data) || $data['id'] != 0 && $data['parent'] == 0 && strlen($data['text']) == 0) { |
718
|
|
|
$id = 0; |
719
|
|
|
} else { |
720
|
|
|
$id = $data['id']; |
721
|
|
|
} |
722
|
|
|
|
723
|
|
|
return $id; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Recursively copies aliases from old parent under new parent. |
728
|
|
|
* |
729
|
|
|
* @param array $actionMap |
730
|
|
|
* @param mixed $oldParentAliasId |
731
|
|
|
* @param mixed $newParentAliasId |
732
|
|
|
*/ |
733
|
|
|
protected function copySubtree($actionMap, $oldParentAliasId, $newParentAliasId) |
734
|
|
|
{ |
735
|
|
|
$rows = $this->gateway->loadAutogeneratedEntries($oldParentAliasId); |
736
|
|
|
$newIdsMap = array(); |
737
|
|
|
foreach ($rows as $row) { |
738
|
|
|
$oldParentAliasId = $row['id']; |
739
|
|
|
|
740
|
|
|
// Ensure that same action entries remain grouped by the same id |
741
|
|
|
if (!isset($newIdsMap[$oldParentAliasId])) { |
742
|
|
|
$newIdsMap[$oldParentAliasId] = $this->gateway->getNextId(); |
743
|
|
|
} |
744
|
|
|
|
745
|
|
|
$row['action'] = $actionMap[$row['action']]; |
746
|
|
|
$row['parent'] = $newParentAliasId; |
747
|
|
|
$row['id'] = $row['link'] = $newIdsMap[$oldParentAliasId]; |
748
|
|
|
$this->gateway->insertRow($row); |
749
|
|
|
|
750
|
|
|
$this->copySubtree( |
751
|
|
|
$actionMap, |
752
|
|
|
$oldParentAliasId, |
753
|
|
|
$row['id'] |
754
|
|
|
); |
755
|
|
|
} |
756
|
|
|
} |
757
|
|
|
|
758
|
|
|
/** |
759
|
|
|
* @param mixed $oldParentId |
760
|
|
|
* @param mixed $newParentId |
761
|
|
|
* |
762
|
|
|
* @return array |
763
|
|
|
*/ |
764
|
|
|
protected function getCopiedLocationsMap($oldParentId, $newParentId) |
765
|
|
|
{ |
766
|
|
|
$originalLocations = $this->locationGateway->getSubtreeContent($oldParentId); |
767
|
|
|
$copiedLocations = $this->locationGateway->getSubtreeContent($newParentId); |
768
|
|
|
|
769
|
|
|
$map = array(); |
770
|
|
|
foreach ($originalLocations as $index => $originalLocation) { |
771
|
|
|
$map['eznode:' . $originalLocation['node_id']] = 'eznode:' . $copiedLocations[$index]['node_id']; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
return $map; |
775
|
|
|
} |
776
|
|
|
|
777
|
|
|
/** |
778
|
|
|
* Notifies the underlying engine that a location was deleted or moved to trash. |
779
|
|
|
* |
780
|
|
|
* @param mixed $locationId |
781
|
|
|
*/ |
782
|
|
|
public function locationDeleted($locationId) |
783
|
|
|
{ |
784
|
|
|
$action = 'eznode:' . $locationId; |
785
|
|
|
$entry = $this->gateway->loadAutogeneratedEntry($action); |
786
|
|
|
|
787
|
|
|
$this->removeSubtree($entry['id'], $action, $entry['is_original']); |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
/** |
791
|
|
|
* Recursively removes aliases by given $id and $action. |
792
|
|
|
* |
793
|
|
|
* $original parameter is used to limit removal of moved Location aliases to history entries only. |
794
|
|
|
* |
795
|
|
|
* @param mixed $id |
796
|
|
|
* @param string $action |
797
|
|
|
* @param mixed $original |
798
|
|
|
*/ |
799
|
|
|
protected function removeSubtree($id, $action, $original) |
800
|
|
|
{ |
801
|
|
|
// Remove first to avoid unnecessary recursion. |
802
|
|
|
if ($original) { |
803
|
|
|
// If entry is original remove all for action (history and custom entries included). |
804
|
|
|
$this->gateway->remove($action); |
805
|
|
|
} else { |
806
|
|
|
// Else entry is history, so remove only for action with the id. |
807
|
|
|
// This means $id grouped history entries are removed, other history, active autogenerated |
808
|
|
|
// and custom are left alone. |
809
|
|
|
$this->gateway->remove($action, $id); |
810
|
|
|
} |
811
|
|
|
|
812
|
|
|
// Load all autogenerated for parent $id, including history. |
813
|
|
|
$entries = $this->gateway->loadAutogeneratedEntries($id, true); |
814
|
|
|
|
815
|
|
|
foreach ($entries as $entry) { |
816
|
|
|
$this->removeSubtree($entry['id'], $entry['action'], $entry['is_original']); |
817
|
|
|
} |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* @param string $text |
822
|
|
|
* |
823
|
|
|
* @return string |
824
|
|
|
*/ |
825
|
|
|
protected function getHash($text) |
826
|
|
|
{ |
827
|
|
|
return md5(mb_strtolower($text, 'UTF-8')); |
828
|
|
|
} |
829
|
|
|
} |
830
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.