- * Returns parent directory.
- *
- * @param string
- * Path to start from.
- *
- * @return string
- * Parent path of given path.
- */
-function _drush_shift_path_up($path) {
- if (empty($path)) {
- return FALSE;
- }
- $path = explode(DIRECTORY_SEPARATOR, $path);
- // Move one directory up.
- array_pop($path);
- return implode(DIRECTORY_SEPARATOR, $path);
- // return dirname($path);
-}
-
-/**
- * Like Drupal conf_path, but searching from beneath.
- * Allows proper site uri detection in site sub-directories.
- *
- * Essentially looks for a settings.php file. Drush uses this
- * function to find a usable site based on the user's current
- * working directory.
- *
- * @param string
- * Search starting path. Defaults to current working directory.
- *
- * @return
- * Current site path (folder containing settings.php) or FALSE if not found.
- */
-function drush_site_path($path = NULL) {
- $site_path = FALSE;
-
- $path = empty($path) ? drush_cwd() : $path;
- // Check the current path.
- if (file_exists($path . '/settings.php')) {
- $site_path = $path;
- }
- else {
- // Move up dir by dir and check each.
- // Stop if we get to a Drupal root. We don't care
- // if it is DRUSH_SELECTED_DRUPAL_ROOT or some other root.
- while (($path = _drush_shift_path_up($path)) && !drush_valid_root($path)) {
- if (file_exists($path . '/settings.php')) {
- $site_path = $path;
- break;
- }
- }
- }
-
- $site_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
- if (file_exists($site_root . '/sites/sites.php')) {
- $sites = array();
- // This will overwrite $sites with the desired mappings.
- include($site_root . '/sites/sites.php');
- // We do a reverse lookup here to determine the URL given the site key.
- if ($match = array_search($site_path, $sites)) {
- $site_path = $match;
- }
- }
-
- // Last resort: try from site root
- if (!$site_path) {
- if ($site_root) {
- if (file_exists($site_root . '/sites/default/settings.php')) {
- $site_path = $site_root . '/sites/default';
- }
- }
- }
-
- return $site_path;
-}
-
-/**
- * Lookup a site's directory via the sites.php file given a hostname.
- *
- * @param $hostname
- * The hostname of a site. May be converted from URI.
- *
- * @return $dir
- * The directory associated with that hostname or FALSE if not found.
- */
-function drush_site_dir_lookup_from_hostname($hostname, $site_root = NULL) {
- if (!isset($site_root)) {
- $site_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
- }
- if (!empty($site_root) && file_exists($site_root . '/sites/sites.php')) {
- $sites = array();
- // This will overwrite $sites with the desired mappings.
- include($site_root . '/sites/sites.php');
- return isset($sites[$hostname]) ? $sites[$hostname] : FALSE;
- }
- else {
- return FALSE;
- }
-}
-
-/**
- * This is a copy of Drupal's conf_path function, taken from D7 and
- * adjusted slightly to search from the selected Drupal Root.
- *
- * Drush uses this routine to find a usable site based on a URI
- * passed in via a site alias record or the --uri commandline option.
- *
- * Drush uses Drupal itself (specifically, the Drupal conf_path function)
- * to bootstrap the site itself. If the implementation of conf_path
- * changes, the site should still bootstrap correctly; the only consequence
- * of this routine not working is that drush configuration files
- * (drushrc.php) stored with the site's drush folders might not be found.
- */
-function drush_conf_path($server_uri, $require_settings = TRUE) {
- $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
- if(empty($drupal_root) || empty($server_uri)) {
- return NULL;
- }
- $parsed_uri = parse_url($server_uri);
- if (is_array($parsed_uri) && !array_key_exists('scheme', $parsed_uri)) {
- $parsed_uri = parse_url('http://' . $server_uri);
- }
- if (!is_array($parsed_uri)) {
- return NULL;
- }
- $server_host = $parsed_uri['host'];
- if (array_key_exists('path', $parsed_uri)) {
- $server_uri = $parsed_uri['path'] . '/index.php';
- }
- else {
- $server_uri = "/index.php";
- }
- $confdir = 'sites';
-
- $sites = array();
- if (file_exists($drupal_root . '/' . $confdir . '/sites.php')) {
- // This will overwrite $sites with the desired mappings.
- include($drupal_root . '/' . $confdir . '/sites.php');
- }
-
- $uri = explode('/', $server_uri);
- $server = explode('.', implode('.', array_reverse(explode(':', rtrim($server_host, '.')))));
- for ($i = count($uri) - 1; $i > 0; $i--) {
- for ($j = count($server); $j > 0; $j--) {
- $dir = implode('.', array_slice($server, -$j)) . implode('.', array_slice($uri, 0, $i));
- if (isset($sites[$dir]) && file_exists($drupal_root . '/' . $confdir . '/' . $sites[$dir])) {
- $dir = $sites[$dir];
- }
- if (file_exists($drupal_root . '/' . $confdir . '/' . $dir . '/settings.php') || (!$require_settings && file_exists(DRUPAL_ROOT . '/' . $confdir . '/' . $dir))) {
- $conf = "$confdir/$dir";
- return $conf;
- }
- }
- }
- $conf = "$confdir/default";
- return $conf;
-}
-
-/**
- * Exhaustive depth-first search to try and locate the Drupal root directory.
- * This makes it possible to run Drush from a subdirectory of the drupal root.
- *
- * @param
- * Search start path. Defaults to current working directory.
- * @return
- * A path to drupal root, or FALSE if not found.
- */
-function drush_locate_root($start_path = NULL) {
- $drupal_root = FALSE;
-
- $start_path = empty($start_path) ? drush_cwd() : $start_path;
- foreach (array(TRUE, FALSE) as $follow_symlinks) {
- $path = $start_path;
- if ($follow_symlinks && is_link($path)) {
- $path = realpath($path);
- }
- // Check the start path.
- if (drush_valid_root($path)) {
- $drupal_root = $path;
- break;
- }
- else {
- // Move up dir by dir and check each.
- while ($path = _drush_shift_path_up($path)) {
- if ($follow_symlinks && is_link($path)) {
- $path = realpath($path);
- }
- if (drush_valid_root($path)) {
- $drupal_root = $path;
- break 2;
- }
- }
- }
- }
-
- return $drupal_root;
-}
-
-/**
- * Checks whether given path qualifies as a Drupal root.
- *
- * @param string
- * Path to check.
- *
- * @return string
- * The relative path to common.inc (varies by Drupal version), or FALSE if not
- * a Drupal root.
- */
-function drush_valid_root($path) {
- $bootstrap_class = drush_bootstrap_class_for_root($path);
- return $bootstrap_class != NULL;
-}
-
-/**
- * Tests the currently loaded database credentials to ensure a database connection can be made.
- *
- * @param bool $log_errors
- * (optional) If TRUE, log error conditions; otherwise be quiet.
- *
- * @return bool
- * TRUE if database credentials are valid.
- */
-function drush_valid_db_credentials() {
- try {
- $sql = drush_sql_get_class();
- if (!$sqlVersion = drush_sql_get_version()) {
- drush_log(dt('While checking DB credentials, could not instantiate SQLVersion class.'), 'debug');
- return FALSE;
- }
- if (!$sqlVersion->valid_credentials($sql->db_spec())) {
- drush_log(dt('DB credentials are invalid.'), 'debug');
- return FALSE;
- }
- return $sql->query('SELECT 1;');
- }
- catch (Exception $e) {
- drush_log(dt('Checking DB credentials yielded error: @e', array('@e' => $e->getMessage())), 'debug');
- return FALSE;
- }
-}
-
-/**
- * Determine a proper way to call drush again
- *
- * This check if we were called directly or as an argument to some
- * wrapper command (php and sudo are checked now).
- *
- * Calling ./drush.php directly yields the following environment:
- *
- * _SERVER["argv"][0] => ./drush.php
- *
- * Calling php ./drush.php also yields the following:
- *
- * _SERVER["argv"][0] => ./drush.php
- *
- * Note that the $_ global is defined only in bash and therefore cannot
- * be relied upon.
- *
- * The DRUSH_COMMAND constant is initialised to the value of this
- * function when environment.inc is loaded.
- *
- * @see DRUSH_COMMAND
- */
-function drush_find_drush() {
- if ($drush = realpath($_SERVER['argv']['0'])) {
- return Path::canonicalize($drush);
- }
- return FALSE;
-}
-
-/**
- * Verify that we are running PHP through the command line interface.
- *
- * This function is useful for making sure that code cannot be run via the web server,
- * such as a function that needs to write files to which the web server should not have
- * access to.
- *
- * @return
- * A boolean value that is true when PHP is being run through the command line,
- * and false if being run through cgi or mod_php.
- */
-function drush_verify_cli() {
- return (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0));
-}
-
-/**
- * Build a drush command suitable for use for Drush to call itself
- * e.g. in backend_invoke.