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