Tasks.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Drupal\Core\Database\Driver\mysql\Install;
  3. use Drupal\Core\Database\Install\Tasks as InstallTasks;
  4. use Drupal\Core\Database\Database;
  5. use Drupal\Core\Database\Driver\mysql\Connection;
  6. use Drupal\Core\Database\DatabaseNotFoundException;
  7. /**
  8. * Specifies installation tasks for MySQL and equivalent databases.
  9. */
  10. class Tasks extends InstallTasks {
  11. /**
  12. * Minimum required MySQLnd version.
  13. */
  14. const MYSQLND_MINIMUM_VERSION = '5.0.9';
  15. /**
  16. * Minimum required libmysqlclient version.
  17. */
  18. const LIBMYSQLCLIENT_MINIMUM_VERSION = '5.5.3';
  19. /**
  20. * The PDO driver name for MySQL and equivalent databases.
  21. *
  22. * @var string
  23. */
  24. protected $pdoDriver = 'mysql';
  25. /**
  26. * Constructs a \Drupal\Core\Database\Driver\mysql\Install\Tasks object.
  27. */
  28. public function __construct() {
  29. $this->tasks[] = [
  30. 'arguments' => [],
  31. 'function' => 'ensureInnoDbAvailable',
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function name() {
  38. return t('MySQL, MariaDB, Percona Server, or equivalent');
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function minimumVersion() {
  44. return '5.5.3';
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function connect() {
  50. try {
  51. // This doesn't actually test the connection.
  52. Database::setActiveConnection();
  53. // Now actually do a check.
  54. try {
  55. Database::getConnection();
  56. }
  57. catch (\Exception $e) {
  58. // Detect utf8mb4 incompatibility.
  59. if ($e->getCode() == Connection::UNSUPPORTED_CHARSET || ($e->getCode() == Connection::SQLSTATE_SYNTAX_ERROR && $e->errorInfo[1] == Connection::UNKNOWN_CHARSET)) {
  60. $this->fail(t('Your MySQL server and PHP MySQL driver must support utf8mb4 character encoding. Make sure to use a database system that supports this (such as MySQL/MariaDB/Percona 5.5.3 and up), and that the utf8mb4 character set is compiled in. See the <a href=":documentation" target="_blank">MySQL documentation</a> for more information.', [':documentation' => 'https://dev.mysql.com/doc/refman/5.0/en/cannot-initialize-character-set.html']));
  61. $info = Database::getConnectionInfo();
  62. $info_copy = $info;
  63. // Set a flag to fall back to utf8. Note: this flag should only be
  64. // used here and is for internal use only.
  65. $info_copy['default']['_dsn_utf8_fallback'] = TRUE;
  66. // In order to change the Database::$databaseInfo array, we need to
  67. // remove the active connection, then re-add it with the new info.
  68. Database::removeConnection('default');
  69. Database::addConnectionInfo('default', 'default', $info_copy['default']);
  70. // Connect with the new database info, using the utf8 character set so
  71. // that we can run the checkEngineVersion test.
  72. Database::getConnection();
  73. // Revert to the old settings.
  74. Database::removeConnection('default');
  75. Database::addConnectionInfo('default', 'default', $info['default']);
  76. }
  77. else {
  78. // Rethrow the exception.
  79. throw $e;
  80. }
  81. }
  82. $this->pass('Drupal can CONNECT to the database ok.');
  83. }
  84. catch (\Exception $e) {
  85. // Attempt to create the database if it is not found.
  86. if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
  87. // Remove the database string from connection info.
  88. $connection_info = Database::getConnectionInfo();
  89. $database = $connection_info['default']['database'];
  90. unset($connection_info['default']['database']);
  91. // In order to change the Database::$databaseInfo array, need to remove
  92. // the active connection, then re-add it with the new info.
  93. Database::removeConnection('default');
  94. Database::addConnectionInfo('default', 'default', $connection_info['default']);
  95. try {
  96. // Now, attempt the connection again; if it's successful, attempt to
  97. // create the database.
  98. Database::getConnection()->createDatabase($database);
  99. Database::closeConnection();
  100. // Now, restore the database config.
  101. Database::removeConnection('default');
  102. $connection_info['default']['database'] = $database;
  103. Database::addConnectionInfo('default', 'default', $connection_info['default']);
  104. // Check the database connection.
  105. Database::getConnection();
  106. $this->pass('Drupal can CONNECT to the database ok.');
  107. }
  108. catch (DatabaseNotFoundException $e) {
  109. // Still no dice; probably a permission issue. Raise the error to the
  110. // installer.
  111. $this->fail(t('Database %database not found. The server reports the following message when attempting to create the database: %error.', ['%database' => $database, '%error' => $e->getMessage()]));
  112. }
  113. }
  114. else {
  115. // Database connection failed for some other reason than the database
  116. // not existing.
  117. $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>', ['%error' => $e->getMessage()]));
  118. return FALSE;
  119. }
  120. }
  121. return TRUE;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getFormOptions(array $database) {
  127. $form = parent::getFormOptions($database);
  128. if (empty($form['advanced_options']['port']['#default_value'])) {
  129. $form['advanced_options']['port']['#default_value'] = '3306';
  130. }
  131. return $form;
  132. }
  133. /**
  134. * Ensure that InnoDB is available.
  135. */
  136. public function ensureInnoDbAvailable() {
  137. $engines = Database::getConnection()->query('SHOW ENGINES')->fetchAllKeyed();
  138. if (isset($engines['MyISAM']) && $engines['MyISAM'] == 'DEFAULT' && !isset($engines['InnoDB'])) {
  139. $this->fail(t('The MyISAM storage engine is not supported.'));
  140. }
  141. }
  142. /**
  143. * {@inheritdoc}
  144. */
  145. protected function checkEngineVersion() {
  146. parent::checkEngineVersion();
  147. // Ensure that the MySQL driver supports utf8mb4 encoding.
  148. $version = Database::getConnection()->clientVersion();
  149. if (FALSE !== strpos($version, 'mysqlnd')) {
  150. // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
  151. $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
  152. if (version_compare($version, self::MYSQLND_MINIMUM_VERSION, '<')) {
  153. $this->fail(t("The MySQLnd driver version %version is less than the minimum required version. Upgrade to MySQLnd version %mysqlnd_minimum_version or up, or alternatively switch mysql drivers to libmysqlclient version %libmysqlclient_minimum_version or up.", ['%version' => $version, '%mysqlnd_minimum_version' => self::MYSQLND_MINIMUM_VERSION, '%libmysqlclient_minimum_version' => self::LIBMYSQLCLIENT_MINIMUM_VERSION]));
  154. }
  155. }
  156. else {
  157. // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
  158. if (version_compare($version, self::LIBMYSQLCLIENT_MINIMUM_VERSION, '<')) {
  159. $this->fail(t("The libmysqlclient driver version %version is less than the minimum required version. Upgrade to libmysqlclient version %libmysqlclient_minimum_version or up, or alternatively switch mysql drivers to MySQLnd version %mysqlnd_minimum_version or up.", ['%version' => $version, '%libmysqlclient_minimum_version' => self::LIBMYSQLCLIENT_MINIMUM_VERSION, '%mysqlnd_minimum_version' => self::MYSQLND_MINIMUM_VERSION]));
  160. }
  161. }
  162. }
  163. }