‪TYPO3CMS  11.5
BackendUserLanguageMigration.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
7 /*
8  * This file is part of the TYPO3 CMS project.
9  *
10  * It is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU General Public License, either version 2
12  * of the License, or any later version.
13  *
14  * For the full copyright and license information, please read the
15  * LICENSE.txt file that was distributed with this source code.
16  *
17  * The TYPO3 project - inspiring people to share!
18  */
19 
21 use TYPO3\CMS\Core\Database\Query\QueryBuilder;
23 
28 {
29  private const ‪TABLE_NAME = 'be_users';
30 
31  public function ‪getIdentifier(): string
32  {
33  return 'backendUserLanguage';
34  }
35 
36  public function ‪getTitle(): string
37  {
38  return 'Migrate backend users\' selected UI languages to new format.';
39  }
40 
41  public function ‪getDescription(): string
42  {
43  return 'Backend users now keep their preferred UI language for TYPO3 Backend in its own database field. This updates all backend user records.';
44  }
45 
46  public function ‪getPrerequisites(): array
47  {
48  return [
49  DatabaseUpdatedPrerequisite::class,
50  ];
51  }
52 
53  public function ‪updateNecessary(): bool
54  {
55  return $this->‪hasRecordsToUpdate();
56  }
57 
58  public function ‪executeUpdate(): bool
59  {
60  $connection = $this->‪getConnectionPool()->getConnectionForTable(self::TABLE_NAME);
61 
62  foreach ($this->‪getRecordsToUpdate() as $record) {
63  $currentDatabaseFieldValue = (string)($record['lang'] ?? '');
64  $uc = unserialize($record['uc'] ?? '', ['allowed_classes' => false]);
65  // Check if the user has a preference set, otherwise use the default from the database field
66  // however, "default" is now explicitly set.
67  $selectedLanguage = $uc['lang'] ?? $currentDatabaseFieldValue;
68  if ($selectedLanguage === '') {
69  $selectedLanguage = 'default';
70  }
71 
72  // Everything set already in the DB field, so this can be skipped
73  if ($selectedLanguage === $currentDatabaseFieldValue) {
74  continue;
75  }
76  $connection->update(
77  self::TABLE_NAME,
78  ['lang' => $selectedLanguage],
79  ['uid' => (int)$record['uid']]
80  );
81  }
82  return true;
83  }
84 
85  protected function ‪hasRecordsToUpdate(): bool
86  {
87  $queryBuilder = $this->‪getPreparedQueryBuilder();
88  return (bool)$queryBuilder
89  ->count('uid')
90  ->executeQuery()
91  ->fetchOne();
92  }
93 
94  protected function ‪getRecordsToUpdate(): array
95  {
96  return $this->‪getPreparedQueryBuilder()->select(...['uid', 'uc', 'lang'])->executeQuery()->fetchAllAssociative();
97  }
98 
99  protected function ‪getPreparedQueryBuilder(): QueryBuilder
100  {
101  $queryBuilder = $this->‪getConnectionPool()->getQueryBuilderForTable(self::TABLE_NAME);
102  $queryBuilder->getRestrictions()->removeAll();
103  $queryBuilder->from(self::TABLE_NAME);
104  return $queryBuilder;
105  }
106 
108  {
109  return GeneralUtility::makeInstance(ConnectionPool::class);
110  }
111 }
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getTitle
‪getTitle()
Definition: BackendUserLanguageMigration.php:36
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getConnectionPool
‪getConnectionPool()
Definition: BackendUserLanguageMigration.php:107
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\TABLE_NAME
‪const TABLE_NAME
Definition: BackendUserLanguageMigration.php:29
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getIdentifier
‪getIdentifier()
Definition: BackendUserLanguageMigration.php:31
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\executeUpdate
‪executeUpdate()
Definition: BackendUserLanguageMigration.php:58
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\hasRecordsToUpdate
‪hasRecordsToUpdate()
Definition: BackendUserLanguageMigration.php:85
‪TYPO3\CMS\Install\Updates
Definition: AbstractDownloadExtensionUpdate.php:16
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getDescription
‪getDescription()
Definition: BackendUserLanguageMigration.php:41
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\updateNecessary
‪updateNecessary()
Definition: BackendUserLanguageMigration.php:53
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getRecordsToUpdate
‪getRecordsToUpdate()
Definition: BackendUserLanguageMigration.php:94
‪TYPO3\CMS\Install\Updates\UpgradeWizardInterface
Definition: UpgradeWizardInterface.php:24
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getPrerequisites
‪getPrerequisites()
Definition: BackendUserLanguageMigration.php:46
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration
Definition: BackendUserLanguageMigration.php:28
‪TYPO3\CMS\Install\Updates\BackendUserLanguageMigration\getPreparedQueryBuilder
‪getPreparedQueryBuilder()
Definition: BackendUserLanguageMigration.php:99
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50