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