-
Notifications
You must be signed in to change notification settings - Fork 638
/
PopulatePageSlugs.php
265 lines (242 loc) · 10 KB
/
PopulatePageSlugs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Install\Updates;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
use TYPO3\CMS\Core\DataHandling\Model\RecordStateFactory;
use TYPO3\CMS\Core\DataHandling\SlugHelper;
use TYPO3\CMS\Core\Utility\GeneralUtility;
/**
* Fills pages.slug with a proper value for pages that do not have a slug updater.
* Does not take "deleted" pages into account, but respects workspace records.
*
* This is how it works:
* - Check if a page has pages.alias filled.
* - Check if realurl v1 (tx_realurl_pathcache) or v2 (tx_realurl_pathdata) has a page path, use that instead.
* - If not -> generate the slug.
*
* @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
*/
class PopulatePageSlugs implements UpgradeWizardInterface
{
protected $table = 'pages';
protected $fieldName = 'slug';
/**
* @return string Unique identifier of this updater
*/
public function getIdentifier(): string
{
return 'pagesSlugs';
}
/**
* @return string Title of this updater
*/
public function getTitle(): string
{
return 'Introduce URL parts ("slugs") to all existing pages';
}
/**
* @return string Longer description of this updater
*/
public function getDescription(): string
{
return 'TYPO3 includes native URL handling. Every page record has its own speaking URL path'
. ' called "slug" which can be edited in TYPO3 Backend. However, it is necessary that all pages have'
. ' a URL pre-filled. This is done by evaluating the page title / navigation title and all of its rootline.';
}
/**
* Checks whether updates are required.
*
* @return bool Whether an update is required (TRUE) or not (FALSE)
*/
public function updateNecessary(): bool
{
$updateNeeded = false;
// Check if the database table even exists
if ($this->checkIfWizardIsRequired()) {
$updateNeeded = true;
}
return $updateNeeded;
}
/**
* @return string[] All new fields and tables must exist
*/
public function getPrerequisites(): array
{
return [
DatabaseUpdatedPrerequisite::class
];
}
/**
* Performs the accordant updates.
*
* @return bool Whether everything went smoothly or not
*/
public function executeUpdate(): bool
{
$this->populateSlugs();
return true;
}
/**
* Fills the database table "pages" with slugs based on the page title and its configuration.
* But also checks "legacy" functionality.
*/
protected function populateSlugs()
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$statement = $queryBuilder
->select('*')
->from($this->table)
->where(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq($this->fieldName, $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->isNull($this->fieldName)
)
)
// Ensure that fields with alias are managed first
->orderBy('alias', 'desc')
// Ensure that live workspace records are handled first
->addOrderBy('t3ver_wsid', 'asc')
// Ensure that all pages are run through "per parent page" field, and in the correct sorting values
->addOrderBy('pid', 'asc')
->addOrderBy('sorting', 'asc')
->execute();
// Check for existing slugs from realurl
$suggestedSlugs = [];
if ($this->checkIfTableExists('tx_realurl_pathdata')) {
$suggestedSlugs = $this->getSuggestedSlugs('tx_realurl_pathdata');
} elseif ($this->checkIfTableExists('tx_realurl_pathcache')) {
$suggestedSlugs = $this->getSuggestedSlugs('tx_realurl_pathcache');
}
$fieldConfig = $GLOBALS['TCA'][$this->table]['columns'][$this->fieldName]['config'];
$fieldConfig['generatorOptions']['fields'] = ['tx_realurl_pathsegment,title'];
$evalInfo = !empty($fieldConfig['eval']) ? GeneralUtility::trimExplode(',', $fieldConfig['eval'], true) : [];
$hasToBeUniqueInSite = in_array('uniqueInSite', $evalInfo, true);
$hasToBeUniqueInPid = in_array('uniqueInPid', $evalInfo, true);
$slugHelper = GeneralUtility::makeInstance(SlugHelper::class, $this->table, $this->fieldName, $fieldConfig);
while ($record = $statement->fetch()) {
$recordId = (int)$record['uid'];
$pid = (int)$record['pid'];
$languageId = (int)$record['sys_language_uid'];
$pageIdInDefaultLanguage = $languageId > 0 ? (int)$record['l10n_parent'] : $recordId;
$slug = $suggestedSlugs[$pageIdInDefaultLanguage][$languageId] ?? '';
// see if an alias field was used, then let's build a slug out of that.
if (!empty($record['alias'])) {
$slug = $slugHelper->sanitize('/' . $record['alias']);
}
if (empty($slug)) {
if ($pid === -1) {
$queryBuilder = $connection->createQueryBuilder();
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$liveVersion = $queryBuilder
->select('pid')
->from('pages')
->where(
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($record['t3ver_oid'], \PDO::PARAM_INT))
)->execute()->fetch();
$pid = (int)$liveVersion['pid'];
}
$slug = $slugHelper->generate($record, $pid);
}
$state = RecordStateFactory::forName($this->table)
->fromArray($record, $pid, $recordId);
if ($hasToBeUniqueInSite && !$slugHelper->isUniqueInSite($slug, $state)) {
$slug = $slugHelper->buildSlugForUniqueInSite($slug, $state);
}
if ($hasToBeUniqueInPid && !$slugHelper->isUniqueInPid($slug, $state)) {
$slug = $slugHelper->buildSlugForUniqueInPid($slug, $state);
}
$connection->update(
$this->table,
[$this->fieldName => $slug],
['uid' => $recordId]
);
}
}
/**
* Check if there are record within "pages" database table with an empty "slug" field.
*
* @return bool
* @throws \InvalidArgumentException
*/
protected function checkIfWizardIsRequired(): bool
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable($this->table);
$queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class));
$numberOfEntries = $queryBuilder
->count('uid')
->from($this->table)
->where(
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq($this->fieldName, $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->isNull($this->fieldName)
)
)
->execute()
->fetchColumn();
return $numberOfEntries > 0;
}
/**
* Resolve prepared realurl "pagepath" for pages
*
* @param string $tableName
* @return array with pageID (default language) and language ID as two-dimensional array containing the page path
*/
protected function getSuggestedSlugs(string $tableName): array
{
$context = GeneralUtility::makeInstance(Context::class);
$currentTimestamp = $context->getPropertyFromAspect('date', 'timestamp');
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
$statement = $queryBuilder
->select('*')
->from($tableName)
->where(
$queryBuilder->expr()->eq('mpvar', $queryBuilder->createNamedParameter('')),
$queryBuilder->expr()->orX(
$queryBuilder->expr()->eq('expire', $queryBuilder->createNamedParameter(0)),
$queryBuilder->expr()->gt('expire', $queryBuilder->createNamedParameter($currentTimestamp))
)
)
->orderBy('uid', 'DESC')
->execute();
$suggestedSlugs = [];
while ($row = $statement->fetch()) {
// rawurldecode ensures that non-ASCII arguments are also migrated
$pagePath = rawurldecode($row['pagepath']);
if (!isset($suggestedSlugs[(int)$row['page_id']][(int)$row['language_id']])) { // keep only first result
$suggestedSlugs[(int)$row['page_id']][(int)$row['language_id']] = '/' . trim($pagePath, '/');
}
}
return $suggestedSlugs;
}
/**
* Check if given table exists
*
* @param string $table
* @return bool
*/
protected function checkIfTableExists($table)
{
$tableExists = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable($table)
->getSchemaManager()
->tablesExist([$table]);
return $tableExists;
}
}