‪TYPO3CMS  ‪main
BackendUserLanguageMigration.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 
21 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
24 
28 #[UpgradeWizard('backendUserLanguage')]
30 {
31  private const ‪TABLE_NAME = 'be_users';
32 
33  public function ‪getTitle(): string
34  {
35  return 'Migrate backend users\' selected UI languages to new format.';
36  }
37 
38  public function ‪getDescription(): string
39  {
40  return 'Backend users now keep their preferred UI language for TYPO3 Backend in its own database field. This updates all backend user records.';
41  }
42 
43  public function ‪getPrerequisites(): array
44  {
45  return [
46  DatabaseUpdatedPrerequisite::class,
47  ];
48  }
49 
50  public function ‪updateNecessary(): bool
51  {
52  return $this->‪hasRecordsToUpdate();
53  }
54 
55  public function ‪executeUpdate(): bool
56  {
57  $connection = $this->‪getConnectionPool()->getConnectionForTable(self::TABLE_NAME);
58 
59  foreach ($this->‪getRecordsToUpdate() as ‪$record) {
60  $currentDatabaseFieldValue = (string)(‪$record['lang'] ?? '');
61  $uc = unserialize(‪$record['uc'] ?? '', ['allowed_classes' => false]);
62  // Check if the user has a preference set, otherwise use the default from the database field
63  // however, "default" is now explicitly set.
64  $selectedLanguage = $uc['lang'] ?? $currentDatabaseFieldValue;
65  if ($selectedLanguage === '') {
66  $selectedLanguage = 'default';
67  }
68 
69  // Everything set already in the DB field, so this can be skipped
70  if ($selectedLanguage === $currentDatabaseFieldValue) {
71  continue;
72  }
73  $connection->update(
74  self::TABLE_NAME,
75  ['lang' => $selectedLanguage],
76  ['uid' => (int)‪$record['uid']]
77  );
78  }
79  return true;
80  }
81 
82  protected function ‪hasRecordsToUpdate(): bool
83  {
84  $queryBuilder = $this->‪getPreparedQueryBuilder();
85  return (bool)$queryBuilder
86  ->count('uid')
87  ->executeQuery()
88  ->fetchOne();
89  }
90 
91  protected function ‪getRecordsToUpdate(): array
92  {
93  return $this->‪getPreparedQueryBuilder()->select(...['uid', 'uc', 'lang'])->executeQuery()->fetchAllAssociative();
94  }
95 
96  protected function ‪getPreparedQueryBuilder(): QueryBuilder
97  {
98  $queryBuilder = $this->‪getConnectionPool()->getQueryBuilderForTable(self::TABLE_NAME);
99  $queryBuilder->getRestrictions()->removeAll();
100  $queryBuilder->from(self::TABLE_NAME);
101  return $queryBuilder;
102  }
103 
105  {
106  return GeneralUtility::makeInstance(ConnectionPool::class);
107  }
108 }
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getTitle
‪getTitle()
Definition: BackendUserLanguageMigration.php:33
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getConnectionPool
‪getConnectionPool()
Definition: BackendUserLanguageMigration.php:104
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\TABLE_NAME
‪const TABLE_NAME
Definition: BackendUserLanguageMigration.php:31
‪TYPO3\CMS\Install\Attribute\UpgradeWizard
Definition: UpgradeWizard.php:27
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\executeUpdate
‪executeUpdate()
Definition: BackendUserLanguageMigration.php:55
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\hasRecordsToUpdate
‪hasRecordsToUpdate()
Definition: BackendUserLanguageMigration.php:82
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:16
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getDescription
‪getDescription()
Definition: BackendUserLanguageMigration.php:38
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\updateNecessary
‪updateNecessary()
Definition: BackendUserLanguageMigration.php:50
‪TYPO3\CMS\Webhooks\Message\$record
‪identifier readonly int readonly array $record
Definition: PageModificationMessage.php:36
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getRecordsToUpdate
‪getRecordsToUpdate()
Definition: BackendUserLanguageMigration.php:91
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:24
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getPrerequisites
‪getPrerequisites()
Definition: BackendUserLanguageMigration.php:43
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration
Definition: BackendUserLanguageMigration.php:30
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getPreparedQueryBuilder
‪getPreparedQueryBuilder()
Definition: BackendUserLanguageMigration.php:96
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:51
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51