Tasks.php 3.73 KB
Newer Older
1
2
<?php

Crell's avatar
Crell committed
3
4
5
6
7
/**
 * @file
 * Definition of Drupal\Core\Database\Driver\mysql\Install\Tasks
 */

8
namespace Drupal\Core\Database\Driver\mysql\Install;
9

10
use Drupal\Core\Database\Install\Tasks as InstallTasks;
11
12
13
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\Core\Database\DatabaseNotFoundException;
14
15
16
17
18
19
20
21
22
23
24
25

/**
 * Specifies installation tasks for MySQL and equivalent databases.
 */
class Tasks extends InstallTasks {
  /**
   * The PDO driver name for MySQL and equivalent databases.
   *
   * @var string
   */
  protected $pdoDriver = 'mysql';

26
27
28
29
30
31
32
33
34
35
36
37
38
  /**
   * Constructs a \Drupal\Core\Database\Driver\mysql\Install\Tasks object.
   */
  public function __construct() {
    $this->tasks[] = array(
      'arguments' => array(
        'SET NAMES utf8mb4',
        'The %name database server supports utf8mb4 character encoding.',
        'The %name database server must support utf8mb4 character encoding to work with Drupal. Make sure to use a database server that supports utf8mb4 character encoding, such as MySQL/MariaDB/Percona versions 5.5.3 and up.',
      ),
    );
  }

39
  /**
40
   * {@inheritdoc}
41
42
   */
  public function name() {
43
    return t('MySQL, MariaDB, Percona Server, or equivalent');
44
45
46
  }

  /**
47
   * {@inheritdoc}
48
49
   */
  public function minimumVersion() {
50
    return '5.5.3';
51
  }
52
53

  /**
54
   * {@inheritdoc}
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
   */
  protected function connect() {
    try {
      // This doesn't actually test the connection.
      db_set_active();
      // Now actually do a check.
      Database::getConnection();
      $this->pass('Drupal can CONNECT to the database ok.');
    }
    catch (\Exception $e) {
      // Attempt to create the database if it is not found.
      if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
        // Remove the database string from connection info.
        $connection_info = Database::getConnectionInfo();
        $database = $connection_info['default']['database'];
        unset($connection_info['default']['database']);

        // In order to change the Database::$databaseInfo array, need to remove
        // the active connection, then re-add it with the new info.
        Database::removeConnection('default');
        Database::addConnectionInfo('default', 'default', $connection_info['default']);

        try {
          // Now, attempt the connection again; if it's successful, attempt to
          // create the database.
          Database::getConnection()->createDatabase($database);
        }
        catch (DatabaseNotFoundException $e) {
          // Still no dice; probably a permission issue. Raise the error to the
          // installer.
85
          $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', array('%database' => $database, '%error' => $e->getMessage())));
86
87
88
89
90
        }
      }
      else {
        // Database connection failed for some other reason than the database
        // not existing.
91
        $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 or does the database user have sufficient privileges to create the database?</li><li>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>', array('%error' => $e->getMessage())));
92
93
94
95
96
        return FALSE;
      }
    }
    return TRUE;
  }
97
98
99
100
101
102

  /**
   * {@inheritdoc}
   */
  public function getFormOptions(array $database) {
    $form = parent::getFormOptions($database);
103
104
105
    if (empty($form['advanced_options']['port']['#default_value'])) {
      $form['advanced_options']['port']['#default_value'] = '3306';
    }
106
107
108

    return $form;
  }
109
}