Tasks.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // This can not be increased above '5.5.5' without dropping support for all
  45. // MariaDB versions. MariaDB prefixes its version string with '5.5.5-'. For
  46. // more information, see
  47. // https://github.com/MariaDB/server/blob/f6633bf058802ad7da8196d01fd19d75c53f7274/include/mysql_com.h#L42.
  48. return '5.5.3';
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function connect() {
  54. try {
  55. // This doesn't actually test the connection.
  56. Database::setActiveConnection();
  57. // Now actually do a check.
  58. try {
  59. Database::getConnection();
  60. }
  61. catch (\Exception $e) {
  62. // Detect utf8mb4 incompatibility.
  63. if ($e->getCode() == Connection::UNSUPPORTED_CHARSET || ($e->getCode() == Connection::SQLSTATE_SYNTAX_ERROR && $e->errorInfo[1] == Connection::UNKNOWN_CHARSET)) {
  64. $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']));
  65. $info = Database::getConnectionInfo();
  66. $info_copy = $info;
  67. // Set a flag to fall back to utf8. Note: this flag should only be
  68. // used here and is for internal use only.
  69. $info_copy['default']['_dsn_utf8_fallback'] = TRUE;
  70. // In order to change the Database::$databaseInfo array, we need to
  71. // remove the active connection, then re-add it with the new info.
  72. Database::removeConnection('default');
  73. Database::addConnectionInfo('default', 'default', $info_copy['default']);
  74. // Connect with the new database info, using the utf8 character set so
  75. // that we can run the checkEngineVersion test.
  76. Database::getConnection();
  77. // Revert to the old settings.
  78. Database::removeConnection('default');
  79. Database::addConnectionInfo('default', 'default', $info['default']);
  80. }
  81. else {
  82. // Rethrow the exception.
  83. throw $e;
  84. }
  85. }
  86. $this->pass('Drupal can CONNECT to the database ok.');
  87. }
  88. catch (\Exception $e) {
  89. // Attempt to create the database if it is not found.
  90. if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
  91. // Remove the database string from connection info.
  92. $connection_info = Database::getConnectionInfo();
  93. $database = $connection_info['default']['database'];
  94. unset($connection_info['default']['database']);
  95. // In order to change the Database::$databaseInfo array, need to remove
  96. // the active connection, then re-add it with the new info.
  97. Database::removeConnection('default');
  98. Database::addConnectionInfo('default', 'default', $connection_info['default']);
  99. try {
  100. // Now, attempt the connection again; if it's successful, attempt to
  101. // create the database.
  102. Database::getConnection()->createDatabase($database);
  103. Database::closeConnection();
  104. // Now, restore the database config.
  105. Database::removeConnection('default');
  106. $connection_info['default']['database'] = $database;
  107. Database::addConnectionInfo('default', 'default', $connection_info['default']);
  108. // Check the database connection.
  109. Database::getConnection();
  110. $this->pass('Drupal can CONNECT to the database ok.');
  111. }
  112. catch (DatabaseNotFoundException $e) {
  113. // Still no dice; probably a permission issue. Raise the error to the
  114. // installer.
  115. $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()]));
  116. }
  117. }
  118. else {
  119. // Database connection failed for some other reason than the database
  120. // not existing.
  121. $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 and port number?</li></ul>', ['%error' => $e->getMessage()]));
  122. return FALSE;
  123. }
  124. }
  125. return TRUE;
  126. }
  127. /**
  128. * {@inheritdoc}
  129. */
  130. public function getFormOptions(array $database) {
  131. $form = parent::getFormOptions($database);
  132. if (empty($form['advanced_options']['port']['#default_value'])) {
  133. $form['advanced_options']['port']['#default_value'] = '3306';
  134. }
  135. return $form;
  136. }
  137. /**
  138. * Ensure that InnoDB is available.
  139. */
  140. public function ensureInnoDbAvailable() {
  141. $engines = Database::getConnection()->query('SHOW ENGINES')->fetchAllKeyed();
  142. if (isset($engines['MyISAM']) && $engines['MyISAM'] == 'DEFAULT' && !isset($engines['InnoDB'])) {
  143. $this->fail(t('The MyISAM storage engine is not supported.'));
  144. }
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. protected function checkEngineVersion() {
  150. parent::checkEngineVersion();
  151. // Ensure that the MySQL driver supports utf8mb4 encoding.
  152. $version = Database::getConnection()->clientVersion();
  153. if (FALSE !== strpos($version, 'mysqlnd')) {
  154. // The mysqlnd driver supports utf8mb4 starting at version 5.0.9.
  155. $version = preg_replace('/^\D+([\d.]+).*/', '$1', $version);
  156. if (version_compare($version, self::MYSQLND_MINIMUM_VERSION, '<')) {
  157. $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]));
  158. }
  159. }
  160. else {
  161. // The libmysqlclient driver supports utf8mb4 starting at version 5.5.3.
  162. if (version_compare($version, self::LIBMYSQLCLIENT_MINIMUM_VERSION, '<')) {
  163. $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]));
  164. }
  165. }
  166. }
  167. }