‪TYPO3CMS  9.5
RedirectsExtensionUpdate.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
20 
27 {
31  protected ‪$confirmation;
32 
33  public function ‪__construct()
34  {
35  $this->extension = new ‪ExtensionModel(
36  'redirects',
37  'Redirects',
38  '9.2',
39  'typo3/cms-redirects',
40  'Manage redirects for your TYPO3-based website'
41  );
42 
43  $this->confirmation = new ‪Confirmation(
44  'Are you sure?',
45  'You should install the "redirects" extension only if needed. ' . $this->extension->getDescription(),
46  true
47  );
48  }
49 
55  public function ‪getConfirmation(): ‪Confirmation
56  {
58  }
59 
66  public function ‪getIdentifier(): string
67  {
68  return 'redirects';
69  }
70 
76  public function ‪getTitle(): string
77  {
78  return 'Install system extension "redirects" if a sys_domain entry with redirectTo is necessary';
79  }
80 
86  public function ‪getDescription(): string
87  {
88  return 'The extension "redirects" includes functionality to handle any kind of redirects. '
89  . 'The functionality superseds sys_domain entries with the only purpose of redirecting to a different domain or entry. '
90  . 'This upgrade wizard installs the redirect extension if necessary and migrates the sys_domain entries to standard redirects.';
91  }
92 
99  public function ‪updateNecessary(): bool
100  {
101  return $this->‪checkIfWizardIsRequired();
102  }
103 
111  public function ‪executeUpdate(): bool
112  {
113  // Install the EXT:redirects extension if not happened yet
114  $installationSuccessful = $this->‪installExtension($this->extension);
115  if ($installationSuccessful) {
116  // Migrate the database entries
118  }
119  return $installationSuccessful;
120  }
121 
128  protected function ‪checkIfWizardIsRequired(): bool
129  {
130  $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
131  $connection = $connectionPool->getConnectionByName('Default');
132  $columns = $connection->getSchemaManager()->listTableColumns('sys_domain');
133  if (isset($columns['redirectto'])) {
134  // table is available, now check if there are entries in it
135  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
136  ->getQueryBuilderForTable('sys_domain');
137  $queryBuilder->getRestrictions()->removeAll();
138  $numberOfEntries = $queryBuilder->count('*')
139  ->from('sys_domain')
140  ->where(
141  $queryBuilder->expr()->neq('redirectTo', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR))
142  )
143  ->execute()
144  ->fetchColumn();
145  return (bool)$numberOfEntries;
146  }
147 
148  return false;
149  }
150 
154  protected function ‪migrateRedirectDomainsToSysRedirect()
155  {
156  $connDomains = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_domain');
157  $connRedirects = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_redirect');
158 
159  $queryBuilder = $connDomains->createQueryBuilder();
160  $queryBuilder->getRestrictions()->removeAll();
161  $domainEntries = $queryBuilder->select('*')
162  ->from('sys_domain')
163  ->where(
164  $queryBuilder->expr()->neq('redirectTo', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR))
165  )
166  ->execute()
167  ->fetchAll();
168 
169  foreach ($domainEntries as $domainEntry) {
170  $domainName = $domainEntry['domainName'];
171  $target = $domainEntry['redirectTo'];
172  $sourceDetails = parse_url($domainName);
173  $targetDetails = parse_url($target);
174  $redirectRecord = [
175  'deleted' => (int)$domainEntry['deleted'],
176  'disabled' => (int)$domainEntry['hidden'],
177  'createdon' => (int)$domainEntry['crdate'],
178  'createdby' => (int)$domainEntry['cruser_id'],
179  'updatedon' => (int)$domainEntry['tstamp'],
180  'source_host' => $sourceDetails['host'] . ($sourceDetails['port'] ? ':' . $sourceDetails['port'] : ''),
181  'keep_query_parameters' => (int)$domainEntry['prepend_params'],
182  'target_statuscode' => (int)$domainEntry['redirectHttpStatusCode'],
183  'target' => $target
184  ];
185 
186  if (isset($targetDetails['scheme']) && $targetDetails['scheme'] === 'https') {
187  $redirectRecord['force_https'] = 1;
188  }
189 
190  if (empty($sourceDetails['path']) || $sourceDetails['path'] === '/') {
191  $redirectRecord['source_path'] = '#.*#';
192  $redirectRecord['is_regexp'] = 1;
193  } else {
194  // Remove the / and add a "/" always before, and at the very end, if path is not empty
195  $sourceDetails['path'] = trim($sourceDetails['path'], '/');
196  $redirectRecord['source_path'] = '/' . ($sourceDetails['path'] ? $sourceDetails['path'] . '/' : '');
197  }
198 
199  // Add the redirect record
200  $connRedirects->insert('sys_redirect', $redirectRecord);
201 
202  // Remove the sys_domain record (hard)
203  $connDomains->delete('sys_domain', ['uid' => (int)$domainEntry['uid']]);
204  }
205  }
206 
214  public function ‪getPrerequisites(): array
215  {
216  return [
217  DatabaseUpdatedPrerequisite::class
218  ];
219  }
220 }
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\$confirmation
‪TYPO3 CMS Install Updates Confirmation $confirmation
Definition: RedirectsExtensionUpdate.php:30
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\updateNecessary
‪bool updateNecessary()
Definition: RedirectsExtensionUpdate.php:98
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\migrateRedirectDomainsToSysRedirect
‪migrateRedirectDomainsToSysRedirect()
Definition: RedirectsExtensionUpdate.php:153
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\getTitle
‪string getTitle()
Definition: RedirectsExtensionUpdate.php:75
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\checkIfWizardIsRequired
‪bool checkIfWizardIsRequired()
Definition: RedirectsExtensionUpdate.php:127
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate
Definition: RedirectsExtensionUpdate.php:27
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:3
‪TYPO3\CMS\Install\Updates\ExtensionModel
Definition: ExtensionModel.php:25
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\getIdentifier
‪string getIdentifier()
Definition: RedirectsExtensionUpdate.php:65
‪TYPO3\CMS\Install\Updates\AbstractDownloadExtensionUpdate\installExtension
‪bool installExtension(ExtensionModel $extension)
Definition: AbstractDownloadExtensionUpdate.php:71
‪TYPO3\CMS\Install\Updates\Confirmation
Definition: Confirmation.php:20
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\executeUpdate
‪bool executeUpdate()
Definition: RedirectsExtensionUpdate.php:110
‪TYPO3\CMS\Install\Updates\AbstractDownloadExtensionUpdate
Definition: AbstractDownloadExtensionUpdate.php:31
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\getPrerequisites
‪string[] getPrerequisites()
Definition: RedirectsExtensionUpdate.php:213
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\getDescription
‪string getDescription()
Definition: RedirectsExtensionUpdate.php:85
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\__construct
‪__construct()
Definition: RedirectsExtensionUpdate.php:32
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\Updates\RedirectsExtensionUpdate\getConfirmation
‪TYPO3 CMS Install Updates Confirmation getConfirmation()
Definition: RedirectsExtensionUpdate.php:54