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