public function creds($hide_password = TRUE) { if ($hide_password) { // EMPTY password is not the same as NO password, and is valid. $contents = <<<EOT #This file was written by Drush's Sqlmysql.inc. [client] user="******" password="******" EOT; $file = drush_save_data_to_temp_file($contents); $parameters['defaults-extra-file'] = $file; } else { // User is required. Drupal calls it 'username'. MySQL calls it 'user'. $parameters['user'] = $this->db_spec['username']; // EMPTY password is not the same as NO password, and is valid. if (isset($this->db_spec['password'])) { $parameters['password'] = $this->db_spec['password']; } } // Some drush commands (e.g. site-install) want to connect to the // server, but not the database. Connect to the built-in database. $parameters['database'] = empty($this->db_spec['database']) ? 'information_schema' : $this->db_spec['database']; // Default to unix socket if configured. if (!empty($this->db_spec['unix_socket'])) { $parameters['socket'] = $this->db_spec['unix_socket']; } elseif (isset($this->db_spec['host'])) { $parameters['host'] = $this->db_spec['host']; } if (!empty($this->db_spec['port'])) { $parameters['port'] = $this->db_spec['port']; } return $this->params_to_options($parameters); }
private function password_file() { if (!isset($password_file) && isset($this->db_spec['password'])) { $host = empty($this->db_spec['host']) ? 'localhost' : $this->db_spec['host']; $port = empty($this->db_spec['port']) ? '5432' : $this->db_spec['port']; $pgpass_contents = "{$host}:{$port}:*:{$this->db_spec['username']}:{$this->db_spec['password']}"; $password_file = drush_save_data_to_temp_file($pgpass_contents); chmod($password_file, 0600); } return $password_file; }
public function createFile($file, $contents) { $tmp_file = drush_save_data_to_temp_file($contents); if ($tmp_file) { if (drush_op('copy', $tmp_file, $file)) { drush_log(dt('Created @file', array('@file' => $file)), 'success'); return TRUE; } else { // @throw copy failed } } else { // @throw tmpfile failed } }
private function password_file() { if (!isset($password_file) && isset($this->db_spec['password'])) { $pgpass_parts = array(empty($this->db_spec['host']) ? 'localhost' : $this->db_spec['host'], empty($this->db_spec['port']) ? '5432' : $this->db_spec['port'], '*', $this->db_spec['username'], $this->db_spec['password']); // Escape colon and backslash characters in entries. // @see http://www.postgresql.org/docs/9.1/static/libpq-pgpass.html array_walk($pgpass_parts, function (&$part) { // The order of the replacements is important so that backslashes are // not replaced twice. $part = str_replace(array('\\', ':'), array('\\\\', '\\:'), $part); }); $pgpass_contents = implode(':', $pgpass_parts); $password_file = drush_save_data_to_temp_file($pgpass_contents); chmod($password_file, 0600); } return $password_file; }
/** * Execute a SQL query. * * Note: This is an API function. Try to avoid using drush_get_option() and instead * pass params in. If you don't want to query results to print during --debug then * provide a $result_file whose value can be drush_bit_bucket(). * * @param string $query * The SQL to be executed. Should be NULL if $input_file is provided. * @param string $input_file * A path to a file containing the SQL to be executed. * @param string $result_file * A path to save query results to. Can be drush_bit_bucket() if desired. * * @return * TRUE on success, FALSE on failure */ public function query($query, $input_file = NULL, $result_file = '') { $input_file_original = $input_file; if ($input_file && drush_file_is_tarball($input_file)) { if (drush_shell_exec('gunzip %s', $input_file)) { $input_file = trim($input_file, '.gz'); } else { return drush_set_error(dt('Failed to gunzip input file.')); } } // Save $query to a tmp file if needed. We will redirect it in. if (!$input_file) { $query = $this->query_prefix($query); $query = $this->query_format($query); $input_file = drush_save_data_to_temp_file($query); } $parts = array($this->command(), $this->creds(), $this->silent(), drush_get_option('extra', $this->query_extra), $this->query_file, drush_escapeshellarg($input_file)); $exec = implode(' ', $parts); if ($result_file) { $exec .= ' > ' . drush_escapeshellarg($result_file); } // In --verbose mode, drush_shell_exec() will show the call to mysql/psql/sqlite, // but the sql query itself is stored in a temp file and not displayed. // We show the query when --debug is used and this function created the temp file. if ((drush_get_context('DRUSH_DEBUG') || drush_get_context('DRUSH_SIMULATE')) && empty($input_file_original)) { drush_log('sql-query: ' . $query, LogLevel::NOTICE); } $success = drush_shell_exec($exec); if ($success && drush_get_option('file-delete')) { drush_op('drush_delete_dir', $input_file); } return $success; }