|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the UrlAlias Mapper class. |
|
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; |
|
12
|
|
|
use eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator as LanguageMaskGenerator; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* UrlAlias Mapper. |
|
16
|
|
|
*/ |
|
17
|
|
|
class Mapper |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Language mask generator. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $languageMaskGenerator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates a new UrlWildcard Handler. |
|
28
|
|
|
* |
|
29
|
|
|
* @param \eZ\Publish\Core\Persistence\Legacy\Content\Language\MaskGenerator $languageMaskGenerator |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(LanguageMaskGenerator $languageMaskGenerator) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->languageMaskGenerator = $languageMaskGenerator; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates a UrlAlias object from database row data. |
|
38
|
|
|
* |
|
39
|
|
|
* @param mixed[] $data |
|
40
|
|
|
* |
|
41
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias |
|
42
|
|
|
*/ |
|
43
|
|
|
public function extractUrlAliasFromData($data) |
|
44
|
|
|
{ |
|
45
|
|
|
$urlAlias = new UrlAlias(); |
|
46
|
|
|
|
|
47
|
|
|
list($type, $destination) = $this->matchTypeAndDestination($data['action']); |
|
48
|
|
|
$urlAlias->id = $data['parent'] . '-' . $data['text_md5']; |
|
49
|
|
|
$urlAlias->pathData = $this->normalizePathData($data['raw_path_data']); |
|
50
|
|
|
$urlAlias->languageCodes = $this->languageMaskGenerator->extractLanguageCodesFromMask($data['lang_mask']); |
|
51
|
|
|
$urlAlias->alwaysAvailable = $this->languageMaskGenerator->isAlwaysAvailable($data['lang_mask']); |
|
52
|
|
|
$urlAlias->isHistory = isset($data['is_path_history']) ? $data['is_path_history'] : !$data['is_original']; |
|
53
|
|
|
$urlAlias->isCustom = (bool)$data['is_alias']; |
|
54
|
|
|
$urlAlias->forward = $data['is_alias'] && $data['alias_redirects']; |
|
55
|
|
|
$urlAlias->destination = $destination; |
|
56
|
|
|
$urlAlias->type = $type; |
|
57
|
|
|
|
|
58
|
|
|
return $urlAlias; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Extracts UrlAlias objects from database $rows. |
|
63
|
|
|
* |
|
64
|
|
|
* @param array $rows |
|
65
|
|
|
* |
|
66
|
|
|
* @return \eZ\Publish\SPI\Persistence\Content\UrlAlias[] |
|
67
|
|
|
*/ |
|
68
|
|
|
public function extractUrlAliasListFromData(array $rows) |
|
69
|
|
|
{ |
|
70
|
|
|
$urlAliases = array(); |
|
71
|
|
|
foreach ($rows as $row) { |
|
72
|
|
|
$urlAliases[] = $this->extractUrlAliasFromData($row); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $urlAliases; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Extracts language codes from database $rows. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $rows |
|
82
|
|
|
* |
|
83
|
|
|
* @return string[] |
|
84
|
|
|
*/ |
|
85
|
|
|
public function extractLanguageCodesFromData(array $rows): array |
|
86
|
|
|
{ |
|
87
|
|
|
$languageMask = 0; |
|
88
|
|
|
foreach ($rows as $row) { |
|
89
|
|
|
$languageMask |= $row['lang_mask']; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $this->languageMaskGenerator->extractLanguageCodesFromMask($languageMask); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @throws \RuntimeException |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $action |
|
99
|
|
|
* |
|
100
|
|
|
* @return array |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function matchTypeAndDestination($action) |
|
103
|
|
|
{ |
|
104
|
|
|
if (preg_match('#^([a-zA-Z0-9_]+):(.+)?$#', $action, $matches)) { |
|
105
|
|
|
$actionType = $matches[1]; |
|
106
|
|
|
$actionValue = isset($matches[2]) ? $matches[2] : false; |
|
107
|
|
|
|
|
108
|
|
|
switch ($actionType) { |
|
109
|
|
|
case 'eznode': |
|
110
|
|
|
$type = UrlAlias::LOCATION; |
|
111
|
|
|
$destination = $actionValue; |
|
112
|
|
|
break; |
|
113
|
|
|
|
|
114
|
|
|
case 'module': |
|
115
|
|
|
$type = UrlAlias::RESOURCE; |
|
116
|
|
|
$destination = $actionValue; |
|
117
|
|
|
break; |
|
118
|
|
|
|
|
119
|
|
|
case 'nop': |
|
120
|
|
|
$type = UrlAlias::VIRTUAL; |
|
121
|
|
|
$destination = null; |
|
122
|
|
|
break; |
|
123
|
|
|
|
|
124
|
|
|
default: |
|
125
|
|
|
// @todo log message |
|
126
|
|
|
throw new \RuntimeException("Action type '{$actionType}' is unknown"); |
|
127
|
|
|
} |
|
128
|
|
|
} else { |
|
129
|
|
|
// @todo log message |
|
130
|
|
|
throw new \RuntimeException("Action '{$action}' is not valid"); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return array($type, $destination); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param array $pathData |
|
138
|
|
|
* |
|
139
|
|
|
* @return array |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function normalizePathData(array $pathData) |
|
142
|
|
|
{ |
|
143
|
|
|
$normalizedPathData = array(); |
|
144
|
|
|
foreach ($pathData as $level => $rows) { |
|
145
|
|
|
$pathElementData = array(); |
|
146
|
|
|
foreach ($rows as $row) { |
|
147
|
|
|
$this->normalizePathDataRow($pathElementData, $row); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$normalizedPathData[$level] = $pathElementData; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $normalizedPathData; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param array $pathElementData |
|
158
|
|
|
* @param array $row |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function normalizePathDataRow(array &$pathElementData, array $row) |
|
161
|
|
|
{ |
|
162
|
|
|
$languageCodes = $this->languageMaskGenerator->extractLanguageCodesFromMask($row['lang_mask']); |
|
163
|
|
|
$pathElementData['always-available'] = $this->languageMaskGenerator->isAlwaysAvailable($row['lang_mask']); |
|
164
|
|
|
if (!empty($languageCodes)) { |
|
165
|
|
|
foreach ($languageCodes as $languageCode) { |
|
166
|
|
|
$pathElementData['translations'][$languageCode] = $row['text']; |
|
167
|
|
|
} |
|
168
|
|
|
} elseif ($pathElementData['always-available']) { |
|
169
|
|
|
// NOP entry, lang_mask == 1 |
|
170
|
|
|
$pathElementData['translations']['always-available'] = $row['text']; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|