function _drush_backend_generate_command_sitealias

4.x backend.inc _drush_backend_generate_command_sitealias($site_record, $command, $args, &$data, $method = 'GET')

Generate a command to execute.

Parameters

site_record: An array containing information used to generate the command. 'remote-host' Optional. A remote host to execute the drush command on. 'remote-user' Optional. Defaults to the current user. If you specify this, you can choose which module to send. 'ssh-options' Optional. Defaults to "-o PasswordAuthentication=no" 'path-aliases' Optional; contains paths to folders and executables useful to the command. '%drush-script' Optional. Defaults to the current drush.php file on the local machine, and to simply 'drush' (the drush script in the current PATH) on remote servers. You may also specify a different drush.php script explicitly. You will need to set this when calling drush on a remote server if 'drush' is not in the PATH on that machine.

command: A defined drush command such as 'cron', 'status' or any of the available ones such as 'drush pm'.

args: An array of arguments for the command.

data: Optional. An array containing options to pass to the remote script. Array items with a numeric key are treated as optional arguments to the command. This parameter is a reference, as any options that have been represented as either an option, or an argument will be removed. This allows you to pass the left over options as a JSON encoded string, without duplicating data.

method: Optional. Defaults to 'GET'. If this parameter is set to 'POST', the $data array will be passed to the script being called as a JSON encoded string over the STDIN pipe of that process. This is preferable if you have to pass sensitive data such as passwords and the like. For any other value, the $data array will be collapsed down into a set of command line options to the script.

Return value

A text string representing a fully escaped command.

2 calls to _drush_backend_generate_command_sitealias()
drush_backend_invoke_sitealias in includes/backend.inc
Execute a new local or remote command in a new process.
_drush_backend_generate_command in includes/backend.inc
Generate a command to execute.

File

includes/backend.inc, line 502
Drush backend API

Code

function _drush_backend_generate_command_sitealias($site_record, $command, $args, &$data, $method = 'GET') {
  $drush_path = null;

  $hostname = array_key_exists('remote-host', $site_record) ? $site_record['remote-host'] : null;
  $username = array_key_exists('remote-user', $site_record) ? $site_record['remote-user'] : null;
  $ssh_options = array_key_exists('ssh-options', $site_record) ? $site_record['ssh-options'] : null;
  $os = drush_os($site_record);

  $drush_path = NULL;
  if (array_key_exists('path-aliases', $site_record)) {
    if (array_key_exists('%drush-script', $site_record['path-aliases'])) {
      $drush_path = $site_record['path-aliases']['%drush-script'];
    }
  }

  if (drush_is_local_host($hostname)) {
    $hostname = null;
  }

  $drush_path = !is_null($drush_path) ? $drush_path : (is_null($hostname) ? DRUSH_COMMAND : 'drush'); // Call own drush.php file on local machines, or 'drush' on remote machines.
  $data['root'] = array_key_exists('root', $data) ? $data['root'] : drush_get_context('DRUSH_DRUPAL_ROOT');
  $data['uri'] = array_key_exists('uri', $data) ? $data['uri'] : drush_get_context('DRUSH_URI');

  $option_str = _drush_backend_argument_string($data, $method);
  foreach ($data as $key => $arg) {
    if (is_numeric($key)) {
      $args[] = $arg;
      unset($data[$key]);
    }
  }
  foreach ($args as $arg) {
    $command .= ' ' . drush_escapeshellarg($arg);
  }
  $interactive = ' ' . (empty($data['#interactive']) ? '' : ' > `tty`') . ' 2>&1';
  // @TODO: Implement proper multi platform / multi server support.
  $cmd = escapeshellcmd($drush_path) . " " . $option_str . " " . $command . (empty($data['#interactive']) ? " --backend" : "");

  if (!is_null($hostname)) {
    $username = (!is_null($username)) ? drush_escapeshellarg($username) . "@" : '';
    $ssh_options = (!is_null($ssh_options)) ? $ssh_options : drush_get_option('ssh-options', "-o PasswordAuthentication=no");
    $cmd = "ssh " . $ssh_options . " " . $username . drush_escapeshellarg($hostname) . " " . drush_escapeshellarg($cmd . ' 2>&1', $os) . $interactive;
  }
  else {
    $cmd .= $interactive;
  }

  return $cmd;
}