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