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