1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\Install\Updates;
17
18use TYPO3\CMS\Core\Database\ConnectionPool;
19use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21/**
22 * Update backend user setting startModule if set to "help_aboutmodules" or "help_CshmanualCshmanual"
23 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
24 */
25class BackendUserStartModuleUpdate implements UpgradeWizardInterface
26{
27    /**
28     * @return string Unique identifier of this updater
29     */
30    public function getIdentifier(): string
31    {
32        return 'cshmanualBackendUsers';
33    }
34
35    /**
36     * @return string Title of this updater
37     */
38    public function getTitle(): string
39    {
40        return 'Update backend user setting "startModule"';
41    }
42
43    /**
44     * @return string Longer description of this updater
45     */
46    public function getDescription(): string
47    {
48        return 'The backend user setting startModule is changed for the extensions about/aboutmodules'
49            . ' and help/cshmanual. Update all backend users that use EXT:aboutmodules and'
50            . ' EXT:cshmanual as startModule.';
51    }
52
53    /**
54     * Checks if an update is needed
55     *
56     * @return bool Whether an update is needed (TRUE) or not (FALSE)
57     */
58    public function updateNecessary(): bool
59    {
60        $statement = GeneralUtility::makeInstance(ConnectionPool::class)
61            ->getConnectionForTable('be_users')
62            ->select(['uid', 'uc'], 'be_users', []);
63        $needsExecution = false;
64        while ($backendUser = $statement->fetch()) {
65            if ($backendUser['uc'] !== null) {
66                $userConfig = unserialize($backendUser['uc'], ['allowed_classes' => false]);
67                if ($userConfig['startModule'] === 'help_aboutmodules'
68                    || $userConfig['startModule'] === 'help_AboutmodulesAboutmodules'
69                    || $userConfig['startModule'] === 'help_AboutAboutmodules'
70                    || $userConfig['startModule'] === 'help_CshmanualCshmanual'
71                    || $userConfig['startModule'] === 'help_DocumentationCshmanual'
72                ) {
73                    $needsExecution = true;
74                    break;
75                }
76            }
77        }
78        return $needsExecution;
79    }
80
81    /**
82     * @return string[] All new fields and tables must exist
83     */
84    public function getPrerequisites(): array
85    {
86        return [
87            DatabaseUpdatedPrerequisite::class
88        ];
89    }
90
91    /**
92     * Performs the database update if backend user's startmodule is
93     * "help_aboutmodules" or "help_AboutmodulesAboutmodules" or "help_CshmanualCshmanual"
94     *
95     * @return bool
96     */
97    public function executeUpdate(): bool
98    {
99        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
100        $statement = $queryBuilder->select('uid', 'uc')->from('be_users')->execute();
101        while ($backendUser = $statement->fetch()) {
102            if ($backendUser['uc'] !== null) {
103                $userConfig = unserialize($backendUser['uc'], ['allowed_classes' => false]);
104                if ($userConfig['startModule'] === 'help_aboutmodules'
105                    || $userConfig['startModule'] === 'help_AboutmodulesAboutmodules'
106                    || $userConfig['startModule'] === 'help_AboutAboutmodules'
107                    || $userConfig['startModule'] === 'help_CshmanualCshmanual'
108                    || $userConfig['startModule'] === 'help_DocumentationCshmanual'
109                ) {
110                    $userConfig['startModule'] = 'help_AboutAbout';
111                    if ($userConfig['startModule'] === 'help_CshmanualCshmanual' || $userConfig['startModule'] === 'help_DocumentationCshmanual') {
112                        $userConfig['startModule'] = 'help_BackendCshmanual';
113                    }
114                    $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('be_users');
115                    $queryBuilder->update('be_users')
116                        ->where(
117                            $queryBuilder->expr()->eq(
118                                'uid',
119                                $queryBuilder->createNamedParameter($backendUser['uid'], \PDO::PARAM_INT)
120                            )
121                        )
122                        ->set('uc', $queryBuilder->createNamedParameter(serialize($userConfig), \PDO::PARAM_LOB))
123                        ->execute();
124                }
125            }
126        }
127        return true;
128    }
129}
130