3 namespace Drupal\Core\Database\Install;
5 use Drupal\Core\Database\Database;
8 * Database installer structure.
10 * Defines basic Drupal requirements for databases.
12 abstract class Tasks {
15 * The name of the PDO driver this database type requires.
22 * Structure that describes each task to run.
26 * Each value of the tasks array is an associative array defining the function
27 * to call (optional) and any arguments to be passed to the function.
31 'function' => 'checkEngineVersion',
36 'CREATE TABLE {drupal_install_test} (id int NULL)',
37 'Drupal can use CREATE TABLE database commands.',
38 'Failed to <strong>CREATE</strong> a test table on your database server with the command %query. The server reports the following message: %error.<p>Are you sure the configured username has the necessary permissions to create tables in the database?</p>',
44 'INSERT INTO {drupal_install_test} (id) VALUES (1)',
45 'Drupal can use INSERT database commands.',
46 'Failed to <strong>INSERT</strong> a value into a test table on your database server. We tried inserting a value with the command %query and the server reported the following error: %error.',
51 'UPDATE {drupal_install_test} SET id = 2',
52 'Drupal can use UPDATE database commands.',
53 'Failed to <strong>UPDATE</strong> a value in a test table on your database server. We tried updating a value with the command %query and the server reported the following error: %error.',
58 'DELETE FROM {drupal_install_test}',
59 'Drupal can use DELETE database commands.',
60 'Failed to <strong>DELETE</strong> a value from a test table on your database server. We tried deleting a value with the command %query and the server reported the following error: %error.',
65 'DROP TABLE {drupal_install_test}',
66 'Drupal can use DROP TABLE database commands.',
67 'Failed to <strong>DROP</strong> a test table from your database server. We tried dropping a table with the command %query and the server reported the following error %error.',
77 protected $results = [
83 * Ensure the PDO driver is supported by the version of PHP in use.
85 protected function hasPdoDriver() {
86 return in_array($this->pdoDriver, \PDO::getAvailableDrivers());
90 * Assert test as failed.
92 protected function fail($message) {
93 $this->results['fail'][] = $message;
97 * Assert test as a pass.
99 protected function pass($message) {
100 $this->results['pass'][] = $message;
104 * Check whether Drupal is installable on the database.
106 public function installable() {
107 return $this->hasPdoDriver() && empty($this->error);
111 * Return the human-readable name of the driver.
113 abstract public function name();
116 * Return the minimum required version of the engine.
119 * A version string. If not NULL, it will be checked against the version
120 * reported by the Database engine using version_compare().
122 public function minimumVersion() {
127 * Run database tasks and tests to see if Drupal can run on the database.
130 * A list of error messages.
132 public function runTasks() {
133 // We need to establish a connection before we can run tests.
134 if ($this->connect()) {
135 foreach ($this->tasks as $task) {
136 if (!isset($task['function'])) {
137 $task['function'] = 'runTestQuery';
139 if (method_exists($this, $task['function'])) {
140 // Returning false is fatal. No other tasks can run.
141 if (FALSE === call_user_func_array([$this, $task['function']], $task['arguments'])) {
146 $this->fail(t("Failed to run all tasks against the database server. The task %task wasn't found.", ['%task' => $task['function']]));
150 return $this->results['fail'];
154 * Check if we can connect to the database.
156 protected function connect() {
158 // This doesn't actually test the connection.
160 // Now actually do a check.
161 Database::getConnection();
162 $this->pass('Drupal can CONNECT to the database ok.');
164 catch (\Exception $e) {
165 $this->fail(t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname?</li></ul>', ['%error' => $e->getMessage()]));
172 * Run SQL tests to ensure the database can execute commands with the current user.
174 protected function runTestQuery($query, $pass, $fail, $fatal = FALSE) {
176 Database::getConnection()->query($query);
177 $this->pass(t($pass));
179 catch (\Exception $e) {
180 $this->fail(t($fail, ['%query' => $query, '%error' => $e->getMessage(), '%name' => $this->name()]));
186 * Check the engine version.
188 protected function checkEngineVersion() {
189 // Ensure that the database server has the right version.
190 if ($this->minimumVersion() && version_compare(Database::getConnection()->version(), $this->minimumVersion(), '<')) {
191 $this->fail(t("The database server version %version is less than the minimum required version %minimum_version.", ['%version' => Database::getConnection()->version(), '%minimum_version' => $this->minimumVersion()]));
196 * Return driver specific configuration options.
199 * An array of driver specific configuration options.
202 * The options form array.
204 public function getFormOptions(array $database) {
205 $form['database'] = [
206 '#type' => 'textfield',
207 '#title' => t('Database name'),
208 '#default_value' => empty($database['database']) ? '' : $database['database'],
213 ':input[name=driver]' => ['value' => $this->pdoDriver],
218 $form['username'] = [
219 '#type' => 'textfield',
220 '#title' => t('Database username'),
221 '#default_value' => empty($database['username']) ? '' : $database['username'],
226 ':input[name=driver]' => ['value' => $this->pdoDriver],
231 $form['password'] = [
232 '#type' => 'password',
233 '#title' => t('Database password'),
234 '#default_value' => empty($database['password']) ? '' : $database['password'],
235 '#required' => FALSE,
239 $form['advanced_options'] = [
240 '#type' => 'details',
241 '#title' => t('Advanced options'),
245 $profile = drupal_get_profile();
246 $db_prefix = ($profile == 'standard') ? 'drupal_' : $profile . '_';
247 $form['advanced_options']['prefix'] = [
248 '#type' => 'textfield',
249 '#title' => t('Table name prefix'),
250 '#default_value' => empty($database['prefix']) ? '' : $database['prefix'],
252 '#description' => t('If more than one application will be sharing this database, a unique table name prefix – such as %prefix – will prevent collisions.', ['%prefix' => $db_prefix]),
256 $form['advanced_options']['host'] = [
257 '#type' => 'textfield',
258 '#title' => t('Host'),
259 '#default_value' => empty($database['host']) ? 'localhost' : $database['host'],
261 // Hostnames can be 255 characters long.
266 $form['advanced_options']['port'] = [
268 '#title' => t('Port number'),
269 '#default_value' => empty($database['port']) ? '' : $database['port'],
278 * Validates driver specific configuration settings.
280 * Checks to ensure correct basic database settings and that a proper
281 * connection to the database can be established.
284 * An array of driver specific configuration options.
287 * An array of driver configuration errors, keyed by form element name.
289 public function validateDatabaseSettings($database) {
292 // Verify the table prefix.
293 if (!empty($database['prefix']) && is_string($database['prefix']) && !preg_match('/^[A-Za-z0-9_.]+$/', $database['prefix'])) {
294 $errors[$database['driver'] . '][prefix'] = t('The database table prefix you have entered, %prefix, is invalid. The table prefix can only contain alphanumeric characters, periods, or underscores.', ['%prefix' => $database['prefix']]);