Passed
Push — master ( 5db2a9...650d5d )
by
unknown
19:20
created

BackendUserLanguageMigration::updateNecessary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TYPO3\CMS\Install\Updates;
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
20
use TYPO3\CMS\Core\Database\ConnectionPool;
21
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24
/**
25
 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
26
 */
27
class BackendUserLanguageMigration implements UpgradeWizardInterface
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($user['uc'] ?? '', ['allowed_classes' => false]);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user seems to never exist and therefore isset should always be false.
Loading history...
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
            ->execute()
91
            ->fetchColumn();
92
    }
93
94
    protected function getRecordsToUpdate(): array
95
    {
96
        return $this->getPreparedQueryBuilder()->select(...['uid', 'uc', 'lang'])->execute()->fetchAll();
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
107
    protected function getConnectionPool(): ConnectionPool
108
    {
109
        return GeneralUtility::makeInstance(ConnectionPool::class);
110
    }
111
}
112