Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Sql / SqlMysql.php
1 <?php
2
3 namespace Drush\Sql;
4
5 use Drush\Drush;
6 use Drush\Preflight\PreflightArgs;
7 use PDO;
8
9 class SqlMysql extends SqlBase
10 {
11
12     public $queryExtra = '-A';
13
14     public function command()
15     {
16         return 'mysql';
17     }
18
19     public function creds($hide_password = true)
20     {
21         $dbSpec = $this->getDbSpec();
22         if ($hide_password) {
23             // EMPTY password is not the same as NO password, and is valid.
24             $contents = <<<EOT
25 #This file was written by Drush's Sqlmysql.php.
26 [client]
27 user="{$dbSpec['username']}"
28 password="{$dbSpec['password']}"
29 EOT;
30
31             $file = drush_save_data_to_temp_file($contents);
32             $parameters['defaults-file'] = $file;
33         } else {
34             // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
35             $parameters['user'] = $dbSpec['username'];
36             // EMPTY password is not the same as NO password, and is valid.
37             if (isset($dbSpec['password'])) {
38                 $parameters['password'] = $dbSpec['password'];
39             }
40         }
41
42         // Some Drush commands (e.g. site-install) want to connect to the
43         // server, but not the database.  Connect to the built-in database.
44         $parameters['database'] = empty($dbSpec['database']) ? 'information_schema' : $dbSpec['database'];
45
46         // Default to unix socket if configured.
47         if (!empty($dbSpec['unix_socket'])) {
48             $parameters['socket'] = $dbSpec['unix_socket'];
49         } // EMPTY host is not the same as NO host, and is valid (see unix_socket).
50         elseif (isset($dbSpec['host'])) {
51             $parameters['host'] = $dbSpec['host'];
52         }
53
54         if (!empty($dbSpec['port'])) {
55             $parameters['port'] = $dbSpec['port'];
56         }
57
58         if (!empty($dbSpec['pdo']['unix_socket'])) {
59             $parameters['socket'] = $dbSpec['pdo']['unix_socket'];
60         }
61
62         if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CA])) {
63             $parameters['ssl-ca'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CA];
64         }
65
66         if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH])) {
67             $parameters['ssl-capath'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH];
68         }
69
70         if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CERT])) {
71             $parameters['ssl-cert'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CERT];
72         }
73
74         if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER])) {
75             $parameters['ssl-cipher'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER];
76         }
77
78         if (!empty($dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_KEY])) {
79             $parameters['ssl-key'] = $dbSpec['pdo'][PDO::MYSQL_ATTR_SSL_KEY];
80         }
81
82         return $this->paramsToOptions($parameters);
83     }
84
85     public function silent()
86     {
87         return '--silent';
88     }
89
90     public function createdbSql($dbname, $quoted = false)
91     {
92         $dbSpec = $this->getDbSpec();
93         if ($quoted) {
94             $dbname = '`' . $dbname . '`';
95         }
96         $sql[] = sprintf('DROP DATABASE IF EXISTS %s;', $dbname);
97         $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
98         $db_superuser = $this->getConfig()->get('sql.db-su');
99         if (isset($db_superuser)) {
100             // - For a localhost database, create a localhost user.  This is important for security.
101             //   localhost is special and only allows local Unix socket file connections.
102             // - If the database is on a remote server, create a wildcard user with %.
103             //   We can't easily know what IP address or hostname would represent our server.
104             $domain = ($dbSpec['host'] == 'localhost') ? 'localhost' : '%';
105             $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $dbSpec['username'], $domain);
106             $sql[] = sprintf("IDENTIFIED BY '%s';", $dbSpec['password']);
107             $sql[] = 'FLUSH PRIVILEGES;';
108         }
109         return implode(' ', $sql);
110     }
111
112     /**
113      * @inheritdoc
114      */
115     public function dbExists()
116     {
117         // Suppress output. We only care about return value.
118         return $this->alwaysQuery("SELECT 1;", null, drush_bit_bucket());
119     }
120
121     public function listTables()
122     {
123         $this->alwaysQuery('SHOW TABLES;');
124         return drush_shell_exec_output();
125     }
126
127     public function dumpCmd($table_selection)
128     {
129         $dbSpec = $this->getDbSpec();
130         $parens = false;
131         $skip_tables = $table_selection['skip'];
132         $structure_tables = $table_selection['structure'];
133         $tables = $table_selection['tables'];
134
135         $ignores = [];
136         $skip_tables  = array_merge($structure_tables, $skip_tables);
137         $data_only = $this->getOption('data-only');
138         // The ordered-dump option is only supported by MySQL for now.
139         $ordered_dump = $this->getOption('ordered-dump');
140
141         $exec = 'mysqldump ';
142         // mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
143         $only_db_name = str_replace('--database=', ' ', $this->creds());
144         $exec .= $only_db_name;
145
146         // We had --skip-add-locks here for a while to help people with insufficient permissions,
147         // but removed it because it slows down the import a lot.  See http://drupal.org/node/1283978
148         $extra = ' --no-autocommit --single-transaction --opt -Q';
149         if ($data_only) {
150             $extra .= ' --no-create-info';
151         }
152         if ($ordered_dump) {
153             $extra .= ' --skip-extended-insert --order-by-primary';
154         }
155         if ($option = $this->getOption('extra-dump')) {
156             $extra .= " $option";
157         }
158         $exec .= $extra;
159
160         if (!empty($tables)) {
161             $exec .= ' ' . implode(' ', $tables);
162         } else {
163             // Append the ignore-table options.
164             foreach ($skip_tables as $table) {
165                 $ignores[] = '--ignore-table=' . $dbSpec['database'] . '.' . $table;
166                 $parens = true;
167             }
168             $exec .= ' '. implode(' ', $ignores);
169
170             // Run mysqldump again and append output if we need some structure only tables.
171             if (!empty($structure_tables)) {
172                 $exec .= " && mysqldump " . $only_db_name . " --no-data $extra " . implode(' ', $structure_tables);
173                 $parens = true;
174             }
175         }
176         return $parens ? "($exec)" : $exec;
177     }
178 }