[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / Action / Step / DatabaseSelect.php
1 <?php
2 namespace TYPO3\CMS\Install\Controller\Action\Step;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17
18 /**
19 * Database select step.
20 * This step is only rendered if database is mysql. With dbal,
21 * database name is submitted by previous step already.
22 */
23 class DatabaseSelect extends AbstractStepAction {
24
25 /**
26 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
27 */
28 protected $databaseConnection = NULL;
29
30 /**
31 * Create database if needed, save selected db name in configuration
32 *
33 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
34 */
35 public function execute() {
36 $result = array();
37 $this->initializeDatabaseConnection();
38 $postValues = $this->postValues['values'];
39 $localConfigurationPathValuePairs = array();
40 /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
41 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
42 if ($postValues['type'] === 'new') {
43 $newDatabaseName = $postValues['new'];
44 if (strlen($newDatabaseName) <= 50) {
45 $createDatabaseResult = $this->databaseConnection->admin_query('CREATE DATABASE ' . $newDatabaseName . ' CHARACTER SET utf8');
46 if ($createDatabaseResult) {
47 $localConfigurationPathValuePairs['DB/database'] = $newDatabaseName;
48 } else {
49 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
50 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
51 $errorStatus->setTitle('Unable to create database');
52 $errorStatus->setMessage(
53 'Database with name ' . $newDatabaseName . ' could not be created.' .
54 ' Either your database name contains special chars (only alphanumeric characters are allowed)' .
55 ' or your database user probably does not have sufficient permissions to create it.' .
56 ' Please choose an existing (empty) database or contact administration.'
57 );
58 $result[] = $errorStatus;
59 }
60 } else {
61 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
62 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
63 $errorStatus->setTitle('Database name not valid');
64 $errorStatus->setMessage('Given database name must be shorter than fifty characters.');
65 $result[] = $errorStatus;
66 }
67 } elseif ($postValues['type'] === 'existing' && !empty($postValues['existing'])) {
68 // Only store database information when it's empty
69 $this->databaseConnection->setDatabaseName($postValues['existing']);
70 $this->databaseConnection->sql_select_db();
71 $existingTables = $this->databaseConnection->admin_get_tables();
72 $isInitialInstallation = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
73 if (!$isInitialInstallation || count($existingTables) === 0) {
74 $localConfigurationPathValuePairs['DB/database'] = $postValues['existing'];
75 }
76 } else {
77 /** @var $errorStatus \TYPO3\CMS\Install\Status\ErrorStatus */
78 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
79 $errorStatus->setTitle('No Database selected');
80 $errorStatus->setMessage('You must select a database.');
81 $result[] = $errorStatus;
82 }
83
84 if (!empty($localConfigurationPathValuePairs)) {
85 $configurationManager->setLocalConfigurationValuesByPathValuePairs($localConfigurationPathValuePairs);
86 }
87
88 return $result;
89 }
90
91 /**
92 * Step needs to be executed if database is not set or can
93 * not be selected.
94 *
95 * @return boolean
96 */
97 public function needsExecution() {
98 $this->initializeDatabaseConnection();
99 $result = TRUE;
100 if (strlen($GLOBALS['TYPO3_CONF_VARS']['DB']['database']) > 0) {
101 $this->databaseConnection->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
102 try {
103 $selectResult = $this->databaseConnection->sql_select_db();
104 if ($selectResult === TRUE) {
105 $result = FALSE;
106 }
107 } catch (\RuntimeException $e) {
108 }
109 }
110 return $result;
111 }
112
113 /**
114 * Executes the step
115 *
116 * @return string Rendered content
117 */
118 protected function executeAction() {
119 /** @var $configurationManager \TYPO3\CMS\Core\Configuration\ConfigurationManager */
120 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
121 $isInitialInstallationInProgress = $configurationManager->getConfigurationValueByPath('SYS/isInitialInstallationInProgress');
122 $this->view->assign('databaseList', $this->getDatabaseList($isInitialInstallationInProgress));
123 $this->view->assign('isInitialInstallationInProgress', $isInitialInstallationInProgress);
124 $this->assignSteps();
125 return $this->view->render();
126 }
127
128 /**
129 * Returns list of available databases (with access-check based on username/password)
130 *
131 * @param boolean $initialInstallation TRUE if first installation is in progress, FALSE if upgrading or usual access
132 * @return array List of available databases
133 */
134 protected function getDatabaseList($initialInstallation) {
135 $this->initializeDatabaseConnection();
136 $databaseArray = $this->databaseConnection->admin_get_dbs();
137 // Remove mysql organizational tables from database list
138 $reservedDatabaseNames = array('mysql', 'information_schema', 'performance_schema');
139 $allPossibleDatabases = array_diff($databaseArray, $reservedDatabaseNames);
140
141 // If we are upgrading we show *all* databases the user has access to
142 if ($initialInstallation === FALSE) {
143 return $allPossibleDatabases;
144 } else {
145 // In first installation we show all databases but disable not empty ones (with tables)
146 $databases = array();
147 foreach ($allPossibleDatabases as $database) {
148 $this->databaseConnection->setDatabaseName($database);
149 $this->databaseConnection->sql_select_db();
150 $existingTables = $this->databaseConnection->admin_get_tables();
151 $databases[] = array(
152 'name' => $database,
153 'tables' => count($existingTables),
154 );
155 }
156 return $databases;
157 }
158 }
159
160 /**
161 * Initialize database connection
162 *
163 * @return void
164 */
165 protected function initializeDatabaseConnection() {
166 $this->databaseConnection = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
167 $this->databaseConnection->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
168 $this->databaseConnection->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
169 $this->databaseConnection->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
170 $this->databaseConnection->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
171 $this->databaseConnection->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
172 $this->databaseConnection->sql_pconnect();
173 }
174 }