‪TYPO3CMS  10.4
MigrateUrlTypesInPagesUpdate.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 
28 {
32  private ‪$databaseTables = ['pages', 'pages_language_overlay'];
33 
37  private ‪$urltypes = ['', 'http://', 'ftp://', 'mailto:', 'https://'];
38 
42  public function ‪getIdentifier(): string
43  {
44  return 'pagesUrltypeField';
45  }
46 
50  public function ‪getTitle(): string
51  {
52  return 'Migrate pages.urltype to pages.url';
53  }
54 
58  public function ‪getDescription(): string
59  {
60  return 'The page property "URL Protocol" for external URLs has been merged into the URL itself.'
61  . ' The update wizard takes care of properly populating all existing pages and page translations.';
62  }
63 
69  public function ‪updateNecessary(): bool
70  {
71  if (!$this->‪checkIfWizardIsRequired()) {
72  return false;
73  }
74  $recordsToMigrate = 0;
75  // Check if there is data to migrate
76  foreach ($this->databaseTables as $databaseTable) {
77  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
78  ->getQueryBuilderForTable($databaseTable);
79  $queryBuilder->getRestrictions()->removeAll();
80  $recordsToMigrate = $queryBuilder->count('*')
81  ->from($databaseTable)
82  ->where(
83  $queryBuilder->expr()->neq('urltype', 0),
84  $queryBuilder->expr()->neq('url', $queryBuilder->createPositionalParameter(''))
85  )
86  ->execute()
87  ->fetchColumn();
88 
89  if ($recordsToMigrate > 0) {
90  break;
91  }
92  }
93  return $recordsToMigrate > 0;
94  }
95 
99  public function ‪getPrerequisites(): array
100  {
101  return [
102  DatabaseUpdatedPrerequisite::class
103  ];
104  }
105 
111  public function ‪executeUpdate(): bool
112  {
113  foreach ($this->databaseTables as $databaseTable) {
114  $connection = GeneralUtility::makeInstance(ConnectionPool::class)
115  ->getConnectionForTable($databaseTable);
116 
117  // Process records that have entries in pages.urltype
118  $queryBuilder = $connection->createQueryBuilder();
119  $queryBuilder->getRestrictions()->removeAll();
120  $statement = $queryBuilder->select('uid', 'urltype', 'url')
121  ->from($databaseTable)
122  ->where(
123  $queryBuilder->expr()->neq('urltype', 0),
124  $queryBuilder->expr()->neq('url', $queryBuilder->createPositionalParameter(''))
125  )
126  ->execute();
127 
128  while ($row = $statement->fetch()) {
129  $url = $this->urltypes[(int)$row['urltype']] . $row['url'];
130  $updateQueryBuilder = $connection->createQueryBuilder();
131  $updateQueryBuilder
132  ->update($databaseTable)
133  ->where(
134  $updateQueryBuilder->expr()->eq(
135  'uid',
136  $updateQueryBuilder->createNamedParameter($row['uid'], \PDO::PARAM_INT)
137  )
138  )
139  ->set('url', $updateQueryBuilder->createNamedParameter($url), false)
140  ->set('urltype', 0);
141  $updateQueryBuilder->execute();
142  }
143  }
144  return true;
145  }
146 
152  protected function ‪checkIfWizardIsRequired(): bool
153  {
154  foreach ($this->databaseTables as $key => $databaseTable) {
155  $columns = GeneralUtility::makeInstance(ConnectionPool::class)
156  ->getConnectionForTable($databaseTable)
157  ->getSchemaManager()
158  ->listTableColumns($databaseTable);
159  if (!isset($columns['urltype'])) {
160  unset($this->databaseTables[$key]);
161  }
162  }
163  return count($this->databaseTables) > 0;
164  }
165 }
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\$urltypes
‪string[] $urltypes
Definition: MigrateUrlTypesInPagesUpdate.php:35
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate
Definition: MigrateUrlTypesInPagesUpdate.php:28
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\$databaseTables
‪string[] $databaseTables
Definition: MigrateUrlTypesInPagesUpdate.php:31
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\getDescription
‪string getDescription()
Definition: MigrateUrlTypesInPagesUpdate.php:56
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:16
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\executeUpdate
‪bool executeUpdate()
Definition: MigrateUrlTypesInPagesUpdate.php:109
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:24
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\getPrerequisites
‪string[] getPrerequisites()
Definition: MigrateUrlTypesInPagesUpdate.php:97
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\checkIfWizardIsRequired
‪bool checkIfWizardIsRequired()
Definition: MigrateUrlTypesInPagesUpdate.php:150
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\getTitle
‪string getTitle()
Definition: MigrateUrlTypesInPagesUpdate.php:48
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\getIdentifier
‪string getIdentifier()
Definition: MigrateUrlTypesInPagesUpdate.php:40
‪TYPO3\CMS\Install\Updates\MigrateUrlTypesInPagesUpdate\updateNecessary
‪bool updateNecessary()
Definition: MigrateUrlTypesInPagesUpdate.php:67