|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* RealUrl. |
|
5
|
|
|
*/ |
|
6
|
|
|
declare(strict_types=1); |
|
7
|
|
|
|
|
8
|
|
|
namespace HDNET\Calendarize\Service\Url; |
|
9
|
|
|
|
|
10
|
|
|
use DmitryDulepov\Realurl\Configuration\ConfigurationReader; |
|
11
|
|
|
use DmitryDulepov\Realurl\Utility; |
|
12
|
|
|
use HDNET\Calendarize\Domain\Model\Index; |
|
13
|
|
|
use HDNET\Calendarize\Service\IndexerService; |
|
14
|
|
|
use HDNET\Calendarize\Utility\HelperUtility; |
|
15
|
|
|
use TYPO3\CMS\Core\Database\ConnectionPool; |
|
16
|
|
|
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility; |
|
17
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
18
|
|
|
use TYPO3\CMS\Core\Utility\VersionNumberUtility; |
|
19
|
|
|
use TYPO3\CMS\Extbase\Reflection\ObjectAccess; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* RealUrl. |
|
23
|
|
|
*/ |
|
24
|
|
|
class RealUrl extends AbstractUrl |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Convert the given information. |
|
28
|
|
|
* |
|
29
|
|
|
* @param $param1 |
|
30
|
|
|
* @param $param2 |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function convert($param1, $param2) |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->main($param1, $param2); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Build the realurl alias. |
|
41
|
|
|
* |
|
42
|
|
|
* @param $params |
|
43
|
|
|
* @param $ref |
|
44
|
|
|
* |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function main($params, $ref) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
if ($params['decodeAlias']) { |
|
50
|
|
|
return $this->alias2id($params['value']); |
|
51
|
|
|
} |
|
52
|
|
|
$this->cleanupOldLinks(); |
|
53
|
|
|
|
|
54
|
|
|
return $this->id2alias($params['value']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Cleanup old URL segments. |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function cleanupOldLinks() |
|
61
|
|
|
{ |
|
62
|
|
|
$removeIds = []; |
|
63
|
|
|
$q = HelperUtility::getDatabaseConnection(IndexerService::TABLE_NAME)->createQueryBuilder(); |
|
64
|
|
|
|
|
65
|
|
|
$q->select('u.uid') |
|
66
|
|
|
->from('tx_realurl_uniqalias', 'u') |
|
67
|
|
|
->leftJoin('u', IndexerService::TABLE_NAME, 'i', 'u.value_id = i.uid') |
|
68
|
|
|
->where( |
|
69
|
|
|
$q->expr()->andX( |
|
70
|
|
|
$q->expr()->isNull('i.uid'), |
|
71
|
|
|
$q->expr()->eq('u.tablename', $q->expr()->literal(IndexerService::TABLE_NAME)) |
|
72
|
|
|
) |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
foreach ($q->execute()->fetchAll() as $row) { |
|
76
|
|
|
$removeIds[] = (int)$row['uid']; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
if (empty($removeIds)) { |
|
80
|
|
|
return; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$q->resetQueryParts(); |
|
84
|
|
|
|
|
85
|
|
|
$q->delete('tx_realurl_uniqalias') |
|
86
|
|
|
->where( |
|
87
|
|
|
$q->expr()->in('uid', $removeIds) |
|
88
|
|
|
) |
|
89
|
|
|
->execute(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Handle the alias to index ID convert. |
|
94
|
|
|
* |
|
95
|
|
|
* @param $value |
|
96
|
|
|
* |
|
97
|
|
|
* @return int |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function alias2id($value): int |
|
100
|
|
|
{ |
|
101
|
|
|
$q = HelperUtility::getDatabaseConnection(IndexerService::TABLE_NAME)->createQueryBuilder(); |
|
102
|
|
|
|
|
103
|
|
|
$row = $q->select('value_id') |
|
104
|
|
|
->from('tx_realurl_uniqalias') |
|
105
|
|
|
->where( |
|
106
|
|
|
$q->expr()->andX( |
|
107
|
|
|
$q->expr()->eq('tablename', $q->expr()->literal(IndexerService::TABLE_NAME)), |
|
108
|
|
|
$q->expr()->eq('value_alias', $q->createNamedParameter($value)) |
|
109
|
|
|
) |
|
110
|
|
|
) |
|
111
|
|
|
->execute() |
|
112
|
|
|
->fetch(); |
|
113
|
|
|
|
|
114
|
|
|
if (isset($row['value_id'])) { |
|
115
|
|
|
return (int)$row['value_id']; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$matches = []; |
|
119
|
|
|
if (\preg_match('/^idx-([0-9]+)$/', $value, $matches)) { |
|
120
|
|
|
return (int)$matches[1]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return 0; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Handle the index ID to alias convert. |
|
128
|
|
|
* |
|
129
|
|
|
* @param $value |
|
130
|
|
|
* |
|
131
|
|
|
* @return string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function id2alias($value): string |
|
134
|
|
|
{ |
|
135
|
|
|
$q = HelperUtility::getDatabaseConnection('tx_realurl_uniqalias')->createQueryBuilder(); |
|
136
|
|
|
|
|
137
|
|
|
$row = $q->select('value_id') |
|
138
|
|
|
->from('tx_realurl_uniqalias') |
|
139
|
|
|
->where( |
|
140
|
|
|
$q->expr()->andX( |
|
141
|
|
|
$q->expr()->eq('tablename', $q->expr()->literal(IndexerService::TABLE_NAME)), |
|
142
|
|
|
$q->expr()->eq('value_id', $q->createNamedParameter((int)$value, \PDO::PARAM_INT)) |
|
143
|
|
|
) |
|
144
|
|
|
) |
|
145
|
|
|
->execute() |
|
146
|
|
|
->fetch(); |
|
147
|
|
|
|
|
148
|
|
|
if (isset($row['value_alias'])) { |
|
149
|
|
|
return (string)$row['value_alias']; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$alias = $this->getIndexBase((int)$value); |
|
153
|
|
|
$alias = $this->prepareBase($alias); |
|
154
|
|
|
$alias = $this->cleanUrl($alias); |
|
|
|
|
|
|
155
|
|
|
$entry = [ |
|
156
|
|
|
'tablename' => IndexerService::TABLE_NAME, |
|
157
|
|
|
'field_alias' => 'title', |
|
158
|
|
|
'field_id' => 'uid', |
|
159
|
|
|
'value_alias' => $alias, |
|
160
|
|
|
'value_id' => $value, |
|
161
|
|
|
]; |
|
162
|
|
|
if ($this->isOldRealUrlVersion()) { |
|
|
|
|
|
|
163
|
|
|
$entry['tstamp'] = (new \DateTime())->getTimestamp(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$aliasBase = $alias; |
|
167
|
|
|
for ($i = 0;; ++$i) { |
|
168
|
|
|
$alias = $i > 0 ? $aliasBase . '-' . $i : $aliasBase; |
|
169
|
|
|
if (!$this->aliasAlreadyExists($alias)) { |
|
170
|
|
|
$entry['value_alias'] = $alias; |
|
171
|
|
|
break; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$q->resetQueryParts(); |
|
176
|
|
|
$q->insert('tx_realurl_uniqalias')->values($entry)->execute(); |
|
177
|
|
|
|
|
178
|
|
|
return (string)$alias; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Check if alias already exists. |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $alias |
|
185
|
|
|
* |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function aliasAlreadyExists($alias) |
|
189
|
|
|
{ |
|
190
|
|
|
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('tx_realurl_uniqalias'); |
|
191
|
|
|
|
|
192
|
|
|
$count = $queryBuilder |
|
193
|
|
|
->count('uid') |
|
194
|
|
|
->from('tx_realurl_uniqalias') |
|
195
|
|
|
->where($queryBuilder->expr()->eq('value_alias', $queryBuilder->createNamedParameter($alias))) |
|
196
|
|
|
->execute() |
|
197
|
|
|
->fetchColumn(0); |
|
198
|
|
|
|
|
199
|
|
|
return (bool)$count; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Generate the realurl part. |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $alias |
|
206
|
|
|
* |
|
207
|
|
|
* @return string |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function cleanUrl(string $alias): string |
|
210
|
|
|
{ |
|
211
|
|
|
$configuration = GeneralUtility::makeInstance(ConfigurationReader::class, ConfigurationReader::MODE_ENCODE); |
|
212
|
|
|
// Init the internal utility by ObjectAccess because the property is |
|
213
|
|
|
// set by a protected method only. :( Perhaps this could be part of the construct (in realurl) |
|
214
|
|
|
$utility = GeneralUtility::makeInstance(Utility::class, $configuration); |
|
215
|
|
|
return (string)$utility->convertToSafeString($alias); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.