View source
<?php
namespace Drupal\Core\Database\Driver\mysql\Install;
use Drupal\Core\Database\Install\Tasks as InstallTasks;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Driver\mysql\Connection;
use Drupal\Core\Database\DatabaseNotFoundException;
class Tasks extends InstallTasks {
const MYSQLND_MINIMUM_VERSION = '5.0.9';
const LIBMYSQLCLIENT_MINIMUM_VERSION = '5.5.3';
protected $pdoDriver = 'mysql';
public function __construct() {
$this->tasks[] = [
'arguments' => [],
'function' => 'ensureInnoDbAvailable',
];
}
public function name() {
return t('MySQL, MariaDB, Percona Server, or equivalent');
}
public function minimumVersion() {
return '5.5.3';
}
protected function connect() {
try {
Database::setActiveConnection();
try {
Database::getConnection();
} catch (\Exception $e) {
if ($e
->getCode() == Connection::UNSUPPORTED_CHARSET || $e
->getCode() == Connection::SQLSTATE_SYNTAX_ERROR && $e->errorInfo[1] == Connection::UNKNOWN_CHARSET) {
$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',
]));
$info = Database::getConnectionInfo();
$info_copy = $info;
$info_copy['default']['_dsn_utf8_fallback'] = TRUE;
Database::removeConnection('default');
Database::addConnectionInfo('default', 'default', $info_copy['default']);
Database::getConnection();
Database::removeConnection('default');
Database::addConnectionInfo('default', 'default', $info['default']);
}
else {
throw $e;
}
}
$this
->pass('Drupal can CONNECT to the database ok.');
} catch (\Exception $e) {
if ($e
->getCode() == Connection::DATABASE_NOT_FOUND) {
$connection_info = Database::getConnectionInfo();
$database = $connection_info['default']['database'];
unset($connection_info['default']['database']);
Database::removeConnection('default');
Database::addConnectionInfo('default', 'default', $connection_info['default']);
try {
Database::getConnection()
->createDatabase($database);
Database::closeConnection();
Database::removeConnection('default');
$connection_info['default']['database'] = $database;
Database::addConnectionInfo('default', 'default', $connection_info['default']);
Database::getConnection();
$this
->pass('Drupal can CONNECT to the database ok.');
} catch (DatabaseNotFoundException $e) {
$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(),
]));
}
}
else {
$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(),
]));
return FALSE;
}
}
return TRUE;
}
public function getFormOptions(array $database) {
$form = parent::getFormOptions($database);
if (empty($form['advanced_options']['port']['#default_value'])) {
$form['advanced_options']['port']['#default_value'] = '3306';
}
return $form;
}
public function ensureInnoDbAvailable() {
$engines = Database::getConnection()
->query('SHOW ENGINES')
->fetchAllKeyed();
if (isset($engines['MyISAM']) && $engines['MyISAM'] == 'DEFAULT' && !isset($engines['InnoDB'])) {
$this
->fail(t('The MyISAM storage engine is not supported.'));
}
}
protected function checkEngineVersion() {
parent::checkEngineVersion();
$version = Database::getConnection()
->clientVersion();
if (FALSE !== strpos($version, 'mysqlnd')) {
$version = preg_replace('/^\\D+([\\d.]+).*/', '$1', $version);
if (version_compare($version, self::MYSQLND_MINIMUM_VERSION, '<')) {
$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,
]));
}
}
else {
if (version_compare($version, self::LIBMYSQLCLIENT_MINIMUM_VERSION, '<')) {
$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,
]));
}
}
}
}