function drush_sitealias_convert_db_spec_to_db_url

8.0.x sitealias.inc drush_sitealias_convert_db_spec_to_db_url($db_spec)
7.x sitealias.inc drush_sitealias_convert_db_spec_to_db_url($db_spec)
master sitealias.inc drush_sitealias_convert_db_spec_to_db_url($db_spec)

Drush still accepts --db-url format database specifications as cli parameters; it is therefore useful to be able to convert from a database record back to a db-url sometimes.

1 call to drush_sitealias_convert_db_spec_to_db_url()
drush_sitealias_convert_databases_to_db_url in includes/sitealias.inc
Create a db-url from the databases record.

File

includes/sitealias.inc, line 880
The site alias API.

Code

function drush_sitealias_convert_db_spec_to_db_url($db_spec) {
  $result = urlencode($db_spec["driver"]) . "://";
  if (isset($db_spec["username"])) {
    $result .= urlencode($db_spec["username"]);
    if (isset($db_spec["password"])) {
      $result .= ":" . urlencode($db_spec["password"]);
    }
    $result .= "@";
  }
  // Host is required, unless this is an sqlite db.
  if (isset($db_spec["host"])) {
    $result .= urlencode($db_spec["host"]);
    if (isset($db_spec["port"])) {
      $result .= ":" . urlencode($db_spec["port"]);
    }
    $result .= '/' . urlencode($db_spec["database"]);
  }
  else {
    // URL-encode the database, but convert slashes
    // back to their original form for readability.
    // This portion is the "path" of the URL, so it may
    // contain slashes.  This is important for sqlite.
    $result .= str_replace("%2F", "/", urlencode(ltrim($db_spec["database"], '/')));
  }
  return $result;
}