-function drush_sitealias_cache_db_settings(&$alias_record, $databases) {
- if (!empty($databases)) {
- $alias_record['databases'] = $databases;
- }
-
- // If the name is set, then re-cache the record after we fetch the databases
- if (array_key_exists('#name', $alias_record)) {
- $all_site_aliases =& drush_get_context('site-aliases');
- $all_site_aliases['@' . $alias_record['#name']] = $alias_record;
- // Check and see if this record is a copy of 'self'
- if (($alias_record['#name'] != 'self') && array_key_exists('@self', $all_site_aliases) && array_key_exists('#name', $all_site_aliases['@self']) && ($all_site_aliases['@self']['#name'] == $alias_record['#name'])) {
- $all_site_aliases['@self'] = $alias_record;
- }
- }
-}
-
-/**
- * Check to see if we have already bootstrapped to a site.
- */
-function drush_sitealias_is_bootstrapped_site($alias_record) {
- if (!isset($alias_record['remote-host']) && array_key_exists('root', $alias_record)) {
- $self_record = drush_sitealias_get_record("@self");
- if (empty($self_record) || !array_key_exists('root', $self_record)) {
- // TODO: If we have not bootstrapped to a site yet, we could
- // perhaps bootstrap to $alias_record here.
- return FALSE;
- }
- elseif(($alias_record['root'] == $self_record['root']) && ($alias_record['uri'] == $self_record['uri'])) {
- return TRUE;
- }
- }
- return FALSE;
-}
-
-/**
- * Determines whether a given site alias is for a remote site.
- *
- * @param string $alias
- * An alias name or site specification.
- *
- * @return bool
- * Returns TRUE if the alias refers to a remote site, FALSE if it does not, or NULL is unsure.
- */
-function drush_sitealias_is_remote_site($alias) {
- if (is_array($alias) && !empty($alias['remote-host'])) {
- return TRUE;
- }
- if (!is_string($alias) || !strlen($alias)) {
- return NULL;
- }
-
- $site_record = drush_sitealias_get_record($alias);
- if ($site_record) {
- if (!empty($site_record['remote-host'])) {
- return TRUE;
- }
- else {
- return FALSE;
- }
- }
- else {
- drush_set_error('Unrecognized site alias.');
- }
-}
-
-/**
- * Get the name of the current bootstrapped site
- */
-function drush_sitealias_bootstrapped_site_name() {
- $site_name = NULL;
- $self_record = drush_sitealias_get_record('@self');
- if (array_key_exists('#name', $self_record)) {
- $site_name = $self_record['#name'];
- }
- if (!isset($site_name) || ($site_name == '@self')) {
- $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
- if (isset($drupal_root)) {
- $drupal_uri = drush_get_context('DRUSH_SELECTED_URI', 'default');
- $drupal_uri = str_replace('http://', '', $drupal_uri);
- // TODO: Maybe use _drush_sitealias_find_local_alias_name?
- $site_name = $drupal_root . '#' . $drupal_uri;
- }
- }
- return $site_name;
-}
-
-/**
- * If there are any path aliases (items beginning with "%") in the test
- * string, then resolve them as path aliases and add them to the provided
- * alias record.
- *
- * @param $alias_record
- * The full alias record to use in path alias expansion
- * @param $test_string
- * A slash-separated list of path aliases to resolve
- * e.g. "%files/%special".
- */
-function drush_sitealias_resolve_path_references(&$alias_record, $test_string = '') {
- $path_aliases = array_key_exists('path-aliases', $alias_record) ? $alias_record['path-aliases'] : array();
- // Convert the test string into an array of items, and
- // from this make a comma-separated list of projects
- // that we can pass to 'drush status'.
- $test_array = explode('/', $test_string);
- $project_array = array();
- foreach($test_array as $one_item) {
- if (!empty($one_item) && ($one_item[0] == '%') && (!array_key_exists($one_item,$path_aliases))) {
- $project_array[] = substr($one_item,1);
- }
- }
- $project_list = implode(',', $project_array);
-
- if (!empty($project_array)) {
- // Optimization: if we're already bootstrapped to the
- // site specified by $alias_record, then we can just
- // call _core_site_status_table() rather than use backend invoke.
- if (drush_sitealias_is_bootstrapped_site($alias_record) && drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
- $status_values = _core_site_status_table($project_list);
- }
- else {
- $values = drush_invoke_process($alias_record, "core-status", array(), empty($project_list) ? array() : array('project' => $project_list), array('integrate' => FALSE, 'override-simulated' => TRUE));
- $status_values = $values['object'];
- }
- if (isset($status_values['%paths'])) {
- foreach ($status_values['%paths'] as $key => $path) {
- $alias_record['path-aliases'][$key] = $path;
- }
- }
- // If 'root' is not set in the alias, then fill it in from the status values.
- if (!isset($alias_record['root']) && isset($status_values['root'])) {
- $alias_record['root'] = $status_values['root'];
- }
- }
-}
-
-/**
- * Given an alias record that is a site list (contains a 'site-list' entry),
- * resolve all of the members of the site list and return them
- * is an array of alias records.
- *
- * @param $alias_record
- * The site list alias record array
- * @return
- * An array of individual site alias records
- */
-function drush_sitealias_resolve_sitelist($alias_record) {
- $result_list = array();
- if (isset($alias_record)) {
- if (array_key_exists('site-list', $alias_record)) {
- foreach ($alias_record['site-list'] as $sitespec) {
- $one_result = drush_sitealias_get_record($sitespec);
- $result_list = array_merge($result_list, drush_sitealias_resolve_sitelist($one_result));
- }
- }
- elseif (array_key_exists('#name', $alias_record)) {
- $result_list[$alias_record['#name']] = $alias_record;
- }
- }
-
- return $result_list;
-}
-
-function _drush_sitelist_find_in_list($one_source, &$target) {
- $result = FALSE;
-
- foreach ($target as $key => $one_target) {
- if(_drush_sitelist_check_site_records($one_source, $one_target)) {
- $result = $one_target;
- unset($target[$key]);
- }
- }
-
- return $result;
-}
-
-function _drush_sitelist_check_site_records($source, $target) {
- if ((array_key_exists('uri', $source)) && (array_key_exists('uri', $target)) && ($source['uri'] == $target['uri'])) {
- return TRUE;
- }
- return FALSE;
-}
-
-/**
- * Initialize an alias record; called as soon as the alias
- * record is loaded from its alias file, before it is stored
- * in the cache.
- *
- * @param alias_record
- * The alias record to be initialized; parameter is modified in place.
- */
-function _drush_sitealias_initialize_alias_record(&$alias_record) {
- // If there is a 'from-list' entry, then build a derived
- // list based on the site list with the given name.
- if (array_key_exists('from-list', $alias_record)) {
- // danger of infinite loops... move to transient defaults?
- $from_record = drush_sitealias_get_record($alias_record['from-list']);
- $from_list = drush_sitealias_resolve_sitelist($from_record);
- $derived_list = array();
- foreach ($from_list as $one_record) {
- $derived_record = _drush_sitealias_derive_record($one_record, $alias_record);
- $derived_list[] = drush_sitealias_alias_record_to_spec($derived_record);
- }
-
- $alias_record = array();
- if (!empty($derived_list)) {
- $alias_record['site-list'] = $derived_list;
- }
- }
- // If there is a 'site-search-path' entry, then build
- // a 'site-list' entry from all of the sites that can be
- // found in the search path.
- if (array_key_exists('site-search-path', $alias_record)) {
- // TODO: Is there any point in merging the sites from
- // the search path with any sites already listed in the
- // 'site-list' entry? For now we'll just overwrite.
- $search_path = $alias_record['site-search-path'];
- if (!is_array($search_path)) {
- $search_path = explode(',', $search_path);
- }
- $found_sites = _drush_sitealias_find_local_sites($search_path);
- $alias_record['site-list'] = $found_sites;
- // The 'unordered-list' flag indicates that the order of the items in the site list is not stable.
- $alias_record['unordered-list'] = '1';
- // DEBUG: var_export($alias_record, FALSE);
- }
- if (array_key_exists('site-list', $alias_record)) {
- if (!is_array($alias_record['site-list'])) {
- $alias_record['site-list'] = explode(',', $alias_record['site-list']);
- }
- }
- else {
- if (isset($alias_record['root']) && !isset($alias_recort['uri'])) {
- $alias_recort['uri'] = 'default';
- }
- }
-}
-
-/**
- * Add "static" default values to the given alias record. The
- * difference between a static default and a transient default is
- * that static defaults -always- exist in the alias record, and
- * they are cached, whereas transient defaults are only added
- * if the given drush command explicitly adds them.
- *
- * @param alias_record
- * An alias record with most values already filled in
- */
-function _drush_sitealias_add_static_defaults(&$alias_record) {
- // If there is a 'db-url' entry but not 'databases' entry, then we will
- // build 'databases' from 'db-url' so that drush commands that use aliases
- // can always count on using a uniform 'databases' array.
- if (isset($alias_record['db-url']) && !isset($alias_record['databases'])) {
- $alias_record['databases'] = drush_sitealias_convert_db_from_db_url($alias_record['db-url']);
- }
-
- // Canonicalize paths.
- if (!empty($alias_record['root'])) {
- $alias_record['root'] = Path::canonicalize($alias_record['root']);
- }
-
- // Adjustments for aliases to drupal instances (as opposed to aliases that are site lists)
- if (array_key_exists('uri', $alias_record)) {
- // Make sure that there is always a 'path-aliases' array
- if (!array_key_exists('path-aliases', $alias_record)) {
- $alias_record['path-aliases'] = array();
- }
- // If there is a 'root' entry, then copy it to the '%root' path alias
- if (isset($alias_record['root'])) {
- $alias_record['path-aliases']['%root'] = $alias_record['root'];
- }
- }
-}
-
-function _drush_sitealias_derive_record($from_record, $modifying_record) {
- $result = $from_record;
-
- // If there is a 'remote-user' in the modifying record, copy it.
- if (array_key_exists('remote-user', $modifying_record)) {
- $result['remote-user'] = $from_record['remote_user'];
- }
- // If there is a 'remote-host', then:
- // If it is empty, clear the remote host in the result record
- // If it ends in '.', then prepend it to the remote host in the result record
- // Otherwise, copy it to the result record
- if (array_key_exists('remote-host', $modifying_record)) {
- $remote_host_modifier = $modifying_record['remote-host'];
- if(empty($remote_host_modifier)) {
- unset($result['remote-host']);
- unset($result['remote-user']);
- }
- elseif ($remote_host_modifier[strlen($remote_host_modifier)-1] == '.') {
- $result['remote-host'] = $remote_host_modifier . $result['remote-host'];
- }
- else {
- $result['remote-host'] = $remote_host_modifier;
- }
- }
- // If there is a 'root', then:
- // If it begins with '/', copy it to the result record
- // Otherwise, append it to the result record
- if (array_key_exists('root', $modifying_record)) {
- $root_modifier = $modifying_record['root'];
- if($root_modifier[0] == '/') {
- $result['root'] = $root_modifier;
- }
- else {
- $result['root'] = $result['root'] . '/' . $root_modifier;
- }
- }
- // Poor man's realpath: take out the /../ with preg_replace.
- // (realpath fails if the files in the path do not exist)
- while(strpos($result['root'], '/../') !== FALSE) {
- $result['root'] = preg_replace('/\w+\/\.\.\//', '', $result['root']);
- }
-
- // TODO: Should we allow the uri to be transformed?
- // I think that if the uri does not match, then you should
- // always build the list by hand, and not rely on '_drush_sitealias_derive_record'.
-
- return $result;
-}
-
-/**
- * Convert from an alias record to a site specification
- *
- * @param alias_record
- * The full alias record to convert
- *
- * @param with_db
- * True if the site specification should include a ?db-url term
- *
- * @return string
- * The site specification
- */
-function drush_sitealias_alias_record_to_spec($alias_record, $with_db = false) {
- $result = '';
-
- // TODO: we should handle 'site-list' records too.
- if (array_key_exists('site-list', $alias_record)) {
- // TODO: we should actually expand the site list and recompose it
- $result = implode(',', $alias_record['site-list']);
- }
- else {
- // There should always be a uri
- if (array_key_exists('uri', $alias_record)) {
- $result = '#' . drush_sitealias_uri_to_site_dir($alias_record['uri'], drush_sitealias_get_root($alias_record));
- }
- // There should always be a root
- if (array_key_exists('root', $alias_record)) {
- $result = $alias_record['root'] . $result;
- }
- if (array_key_exists('remote-host', $alias_record)) {
- $result = drush_remote_host($alias_record) . $result;
- }
-
- // Add the database info to the specification if desired
- if ($with_db) {
- // If db-url is not supplied, look it up from the remote
- // or local site and add it to the site alias
- if (!isset($alias_record['db-url'])) {
- drush_sitealias_add_db_url($alias_record);
- }
- $result = $result . '?db-url=' . urlencode(is_array($alias_record['db-url']) ? $alias_record['db-url']['default'] : $alias_record['db-url']);
- }
- }
-
- return $result;
-}
-
-/**
- * Search for drupal installations in the search path.
- *
- * @param search_path
- * An array of drupal root folders
- *
- * @return
- * An array of site specifications (/path/to/root#sitename.com)
- */
-function _drush_sitealias_find_local_sites($search_path) {
- $result = array();
- foreach ($search_path as $a_drupal_root) {
- $result = array_merge($result, _drush_find_local_sites_at_root($a_drupal_root));
- }
- return $result;
-}
-
-/**
- * Return a list of all of the local sites at the specified drupal root.
- */
-function _drush_find_local_sites_at_root($a_drupal_root = '', $search_depth = 1) {
- $site_list = array();
- $base_path = (empty($a_drupal_root) ? drush_get_context('DRUSH_DRUPAL_ROOT') : $a_drupal_root );
- if (!empty($base_path)) {
- if (drush_valid_root($base_path)) {
- // If $a_drupal_root is in fact a valid drupal root, then return
- // all of the sites found inside the 'sites' folder of this drupal instance.
- $site_list = _drush_find_local_sites_in_sites_folder($base_path);
- }
- else {
- $bootstrap_files = drush_scan_directory($base_path, '/' . basename(DRUSH_DRUPAL_SIGNATURE) . '/' , array('.', '..', 'CVS', 'examples'), 0, drush_get_option('search-depth', $search_depth) + 1, 'filename', 1);
- foreach ($bootstrap_files as $one_bootstrap => $info) {
- $includes_dir = dirname($one_bootstrap);
- if (basename($includes_dir) == basename(dirname(DRUSH_DRUPAL_SIGNATURE))) {
- $drupal_root = dirname($includes_dir);
- $site_list = array_merge(_drush_find_local_sites_in_sites_folder($drupal_root), $site_list);
- }
- }
- }
- }
- return $site_list;
-}
-
-/**
- * Return a list of all of the local sites at the specified 'sites' folder.
- */
-function _drush_find_local_sites_in_sites_folder($a_drupal_root) {
- $site_list = array();
-
- // If anyone searches for sites at a given root, then
- // make sure that alias files stored at this root
- // directory are included in the alias search path
- drush_sitealias_add_to_alias_path($a_drupal_root);
-
- $base_path = $a_drupal_root . '/sites';
-
- // TODO: build a cache keyed off of $base_path (realpath($base_path)?),
- // so that it is guarenteed that the lists returned will definitely be
- // exactly the same should this routine be called twice with the same path.
-
- $files = drush_scan_directory($base_path, '/settings\.php/', array('.', '..', 'CVS', 'all'), 0, 1, 'filename', 1);
- foreach ($files as $filename => $info) {
- if ($info->basename == 'settings.php') {
- // First we'll resolve the realpath of the settings.php file,
- // so that we get the correct drupal root when symlinks are in use.
- $real_sitedir = dirname(realpath($filename));
- $real_root = drush_locate_root($filename);
- if ($real_root !== FALSE) {
- $a_drupal_site = $real_root . '#' . basename($real_sitedir);
- }
- // If the symlink points to some folder outside of any drupal
- // root, then we'll use the
- else {
- $uri = drush_sitealias_site_dir_from_filename($filename);
- $a_drupal_site = $a_drupal_root . '#' . $uri;
- }
- // Add the site if it isn't already in the array
- if (!in_array($a_drupal_site, $site_list)) {
- $site_list[] = $a_drupal_site;
- }
- }
- }
- return $site_list;
-}
-
-function drush_sitealias_create_sites_alias($a_drupal_root = '') {
- $sites_list = _drush_find_local_sites_at_root($a_drupal_root);
- _drush_sitealias_cache_alias('@sites', array('site-list' => $sites_list));
-}
-
-/**
- * Add "transient" default values to the given alias record. The
- * difference between a static default and a transient default is
- * that static defaults -always- exist in the alias record,
- * whereas transient defaults are only added if the given drush
- * command explicitly calls this function. The other advantage
- * of transient defaults is that it is possible to differentiate
- * between a default value and an unspecified value, since the
- * transient defaults are not added until requested.
- *
- * Since transient defaults are not cached, you should avoid doing
- * expensive operations here. To be safe, drush commands should
- * avoid calling this function more than once.
- *
- * @param alias_record
- * An alias record with most values already filled in
- */
-function _drush_sitealias_add_transient_defaults(&$alias_record) {
- if (isset($alias_record['path-aliases'])) {
- // Add the path to the drush folder to the path aliases as !drush
- if (!array_key_exists('%drush', $alias_record['path-aliases'])) {
- if (array_key_exists('%drush-script', $alias_record['path-aliases'])) {
- $alias_record['path-aliases']['%drush'] = dirname($alias_record['path-aliases']['%drush-script']);
- }
- else {
- $alias_record['path-aliases']['%drush'] = dirname(drush_find_drush());
- }
- }
- // Add the path to the site folder to the path aliases as !site
- if (!array_key_exists('%site', $alias_record['path-aliases']) && array_key_exists('uri', $alias_record)) {
- $alias_record['path-aliases']['%site'] = 'sites/' . drush_sitealias_uri_to_site_dir($alias_record['uri'], drush_sitealias_get_root($alias_record)) . '/';
- }
- }
-}
-
-/**
- * Find the name of a local alias record that has the specified
- * root and uri.
- */
-function _drush_sitealias_find_local_alias_name($root, $uri) {
- $result = '';
- $all_site_aliases =& drush_get_context('site-aliases');
-
- foreach ($all_site_aliases as $alias_name => $alias_values) {
- if (!array_key_exists('remote-host', $alias_values) && array_key_exists('root', $alias_values) && array_key_exists('uri', $alias_values) && ($alias_name != '@self')) {
- if (($root == $alias_values['root']) && ($uri == $alias_values['uri'])) {
- $result = $alias_name;
- }
- }
- }
-
- return $result;
-}
-
-/**
- * If '$alias' is the name of a folder in the sites folder of the given drupal
- * root, then build an alias record for it
- *
- * @param alias
- * The name of the site in the 'sites' folder to convert
- * @return array
- * An alias record, or empty if none found.
- */
-function _drush_sitealias_find_record_for_local_site($alias, $drupal_root = NULL) {
- $alias_record = array();
-
- // Clip off the leading '#' if it is there
- if (substr($alias,0,1) == '#') {
- $alias = substr($alias,1);
- }
-
- if (!isset($drupal_root)) {
- $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
- }
-
- if (!empty($drupal_root)) {
- $alias_dir = drush_sitealias_uri_to_site_dir($alias, $drupal_root);
- $site_settings_file = $drupal_root . '/sites/' . $alias_dir . '/settings.php';
- $alias_record = drush_sitealias_build_record_from_settings_file($site_settings_file, $alias, $drupal_root);
- }
-
- return $alias_record;
-}
-
-function drush_sitealias_build_record_from_settings_file($site_settings_file, $alias = null, $drupal_root = null) {
- $alias_record = array();
-
- if (file_exists($site_settings_file)) {
- if (!isset($drupal_root)) {
- $drupal_root = drush_locate_root($site_settings_file);
- }
-
- $alias_record['root'] = $drupal_root;
- if (isset($alias)) {
- $alias_record['uri'] = $alias;
- }
- else {
- $alias_record['uri'] = _drush_sitealias_site_dir_to_uri(drush_sitealias_site_dir_from_filename($site_settings_file));
- }
- }
-
- return $alias_record;
-}
-
-/**
- * Pull the site directory from the path to settings.php
- *
- * @param site_settings_file
- * path to settings.php
- *
- * @return string
- * the site directory component of the path to settings.php
- */
-function drush_sitealias_site_dir_from_filename($site_settings_file) {
- return basename(dirname($site_settings_file));
-}
-
-/**
- * Convert from a URI to a site directory.
- *
- * @param uri
- * A uri, such as http://domain.com:8080/drupal
- * @return string
- * A directory, such as domain.com.8080.drupal
- */
-function drush_sitealias_uri_to_site_dir($uri, $site_root = NULL) {
- $uri = str_replace('http://', '', $uri);
- $uri = str_replace('https://', '', $uri);
- if (drush_is_windows()) {
- // Handle absolute paths on windows
- $uri = str_replace(array(':/', ':\\'), array('.', '.'), $uri);
- }
-
- $hostname = str_replace(array('/', ':', '\\'), array('.', '.', '.'), $uri);
-
- // Check sites.php mappings
- $site_dir = drush_site_dir_lookup_from_hostname($hostname, $site_root);
-
- return $site_dir ? $site_dir : $hostname;
-}
-
-/**
- * Convert from an old-style database URL to an array of database settings.
- *
- * @param db_url
- * A Drupal 6 db url string to convert, or an array with a 'default' element.
- * @return array
- * An array of database values containing only the 'default' element of
- * the db url. If the parse fails the array is empty.
- */
-function drush_convert_db_from_db_url($db_url) {
- $db_spec = array();
-
- if (is_array($db_url)) {
- $db_url_default = $db_url['default'];
- }
- else {
- $db_url_default = $db_url;
- }
-
- // If it's a sqlite database, pick the database path and we're done.
- if (strpos($db_url_default, 'sqlite://') === 0) {
- $db_spec = array(
- 'driver' => 'sqlite',
- 'database' => substr($db_url_default, strlen('sqlite://')),
- );
- }
- else {
- $url = parse_url($db_url_default);
- if ($url) {
- // Fill in defaults to prevent notices.
- $url += array(
- 'scheme' => NULL,
- 'user' => NULL,
- 'pass' => NULL,
- 'host' => NULL,
- 'port' => NULL,
- 'path' => NULL,
- );
- $url = (object)array_map('urldecode', $url);
- $db_spec = array(
- 'driver' => $url->scheme == 'mysqli' ? 'mysql' : $url->scheme,
- 'username' => $url->user,
- 'password' => $url->pass,
- 'host' => $url->host,
- 'port' => $url->port,
- 'database' => ltrim($url->path, '/'),
- );
- }
- }
-
- return $db_spec;
-}
-
-/**
- * Convert from an old-style database URL to an array of database settings
- *
- * @param db_url
- * A Drupal 6 db-url string to convert, or an array with multiple db-urls.
- * @return array
- * An array of database values.
- */
-function drush_sitealias_convert_db_from_db_url($db_url) {
- $result = array();
-
- if (!is_array($db_url)) {
- $result = array('default' => array('default' => drush_convert_db_from_db_url($db_url)));
- }
- else {
- foreach ($db_url as $one_name => $one_db_url) {
- $result[$one_name] = array('default' => drush_convert_db_from_db_url($one_db_url));
- }
- }
-
- return $result;
-}
-
-/**
- * Utility function used by drush_get_alias; keys that start with
- * '%' or '!' are path aliases, the rest are entries in the alias record.
- */
-function _drush_sitealias_set_record_element(&$alias_record, $key, $value) {
- if ((substr($key,0,1) == '%') || (substr($key,0,1) == '!')) {
- $alias_record['path-aliases'][$key] = $value;
- }
- elseif (!empty($key)) {
- $alias_record[$key] = $value;
- }
-}
-
-/**
- * Looks up the specified alias record and calls through to
- * drush_sitealias_set_alias_context, below.
- *
- * @param alias
- * The name of the alias record
- * @param prefix
- * The prefix value to afix to the beginning of every
- * key set.
- * @return boolean
- * TRUE is an alias was found and processed.
- */
-function _drush_sitealias_set_context_by_name($alias, $prefix = '') {
- if ($alias) {
- $site_alias_settings = drush_sitealias_get_record($alias);
- if (!empty($site_alias_settings)) {
- drush_sitealias_set_alias_context($site_alias_settings, $prefix);
- drush_sitealias_cache_alias_by_path($site_alias_settings);
- if (empty($prefix)) {
-
- // Create an alias '@self'
- // Allow 'uri' from the commandline to override
- $drush_uri = drush_get_option(array('uri', 'l'), FALSE);
- if ($drush_uri) {
- $site_alias_settings['uri'] = $drush_uri;
- }
-
- _drush_sitealias_cache_alias('@self', $site_alias_settings);
- // Change the selected site to match the new --root and --uri, if any were set.
- _drush_preflight_root_uri();
- }
- return $site_alias_settings;
- }
- }
- return array();
-}
-
-/**
- * Given an alias record, overwrite its values with options
- * from the command line and other drush contexts as specified
- * by the provided prefix. For example, if the prefix is 'source-',
- * then any option 'source-foo' will set the value 'foo' in the
- * alias record.
- */
-function drush_sitealias_overlay_options($site_alias_record, $prefix) {
- return array_merge($site_alias_record, drush_get_merged_prefixed_options($prefix));
-}
-
-/**
- * First return an option set via drush_sitealias_overlay_options, if
- * any, then fall back on "%" . $option from the path aliases.
- */
-function drush_sitealias_get_path_option($site_alias_record, $option, $default = NULL) {
- if (isset($site_alias_record) && array_key_exists($option, $site_alias_record)) {
- return $site_alias_record[$option];
- }
- if (isset($site_alias_record) && array_key_exists('path-aliases', $site_alias_record) && array_key_exists("%$option", $site_alias_record['path-aliases'])) {
- return $site_alias_record['path-aliases']["%$option"];
- }
- else {
- return drush_get_option($option, $default);
- }
-}
-
-/**
- * Given a site alias record, copy selected fields from it
- * into the drush 'alias' context. The 'alias' context has
- * lower precedence than the 'cli' context, so values
- * set by an alias record can be overridden by command-line
- * parameters.
- *
- * @param site_alias_settings
- * An alias record
- * @param prefix
- * The prefix value to affix to the beginning of every
- * key set. For example, if this function is called once with
- * 'source-' and again with 'destination-' prefixes, then the
- * source database records will be stored in 'source-databases',
- * and the destination database records will be in
- * 'destination-databases'.
- */
-function drush_sitealias_set_alias_context($site_alias_settings, $prefix = '') {
- $options = drush_get_context('alias');
-
- // There are some items that we should just skip
- $skip_list = drush_get_special_keys();
- // If 'php-options' are set in the alias, then we will force drush
- // to redispatch via the remote dispatch mechanism even if the target is localhost.
- if ((array_key_exists('php-options', $site_alias_settings) || array_key_exists('php', $site_alias_settings)) && !drush_get_context('DRUSH_BACKEND', FALSE)) {
- if (!array_key_exists('remote-host', $site_alias_settings)) {
- $site_alias_settings['remote-host'] = 'localhost';
- }
- }
- // If 'php-options' are not set in the alias, then skip 'remote-host'
- // and 'remote-user' if 'remote-host' is actually the local machine.
- // This prevents drush from using the remote dispatch mechanism (the command
- // is just run directly on the local machine, bootstrapping to the specified alias)
- elseif (array_key_exists('remote-host', $site_alias_settings) && drush_is_local_host($site_alias_settings['remote-host'])) {
- $skip_list[] = 'remote-host';
- $skip_list[] = 'remote-user';
- }
- // If prefix is set, then copy from the 'prefix-' version
- // of the drush special keys ('command-specific', 'path-aliases')
- // into the ordinary version. This will allow us to set
- // 'source-command-specific' options that will only apply when
- // the alias is used as the source option for rsync or sql-sync.
- if (!empty($prefix)) {
- $special_contexts = drush_get_special_keys();
- foreach ($special_contexts as $option_name) {
- if (array_key_exists($prefix . $option_name, $site_alias_settings)) {
- $site_alias_settings[$option_name] = array_key_exists($option_name, $site_alias_settings) ? array_merge($site_alias_settings[$option_name], $site_alias_settings[$prefix . $option_name]) : $site_alias_settings[$prefix . $option_name];
- }
- }
- }
- // Transfer all options from the site alias to the drush options
- // in the 'alias' context.
- foreach ($site_alias_settings as $key => $value) {
- // Special handling for path aliases:
- if ($key == "path-aliases") {
- $path_aliases = $value;
- foreach (array('%drush-script', '%dump', '%dump-dir', '%include') as $path_key) {
- if (array_key_exists($path_key, $path_aliases)) {
- // Evaluate the path value, and substitute any path references found.
- // ex: '%dump-dir' => '%root/dumps' will store sql-dumps in the folder
- // 'dumps' in the Drupal root folder for the site.
- $evaluated_path = str_replace(array_keys($path_aliases), array_values($path_aliases), $path_aliases[$path_key]);
- $options[$prefix . substr($path_key, 1)] = $evaluated_path;
- }
- }
- }
- // Special handling for command-specific
- elseif ($key == "command-specific") {
- $options[$key] = $value;
- }
- elseif (!in_array($key, $skip_list)) {
- $options[$prefix . $key] = $value;
- }
- }
- drush_set_config_options('alias', $options);
-}
-
-/**
- * Call prior to drush_sitealias_evaluate_path to insure
- * that any site-specific aliases associated with any
- * local site in $path are defined.
- */
-function _drush_sitealias_preflight_path($path) {
- $alias = NULL;
- // Parse site aliases if there is a colon in the path
- // We allow:
- // @alias:/path
- // machine.domain.com:/path
- // machine:/path
- // Note that paths in the form "c:/path" are converted to
- // "/cygdrive/c/path" later; we do not want them to confuse
- // us here, so we skip paths that start with a single character
- // before the colon if we are running on Windows. Single-character
- // machine names are allowed in Linux only.
- $colon_pos = strpos($path, ':');
- if ($colon_pos > (drush_is_windows("LOCAL") ? 1 : 0)) {
- $alias = substr($path, 0, $colon_pos);
- $path = substr($path, $colon_pos + 1);
- $site_alias_settings = drush_sitealias_get_record($alias);
- if (empty($site_alias_settings) && (substr($path,0,1) == '@')) {
- return NULL;
- }
- $machine = $alias;
- }
- else {
- $machine = '';
- // if the path is a site alias or a local site...
- $site_alias_settings = drush_sitealias_get_record($path);
- if (empty($site_alias_settings) && (substr($path,0,1) == '@')) {
- return NULL;
- }
- if (!empty($site_alias_settings) || drush_is_local_host($path)) {
- $alias = $path;
- $path = '';
- }
- }
- return array('alias' => $alias, 'path' => $path, 'machine' => $machine);
-}
-
-/**
- * Given a properly-escaped options string, replace any occurance of
- * %files and so on embedded inside it with its corresponding path.
- */
-function drush_sitealias_evaluate_paths_in_options($option_string) {
- $path_aliases = _core_path_aliases();
- return str_replace(array_keys($path_aliases), array_values($path_aliases), $option_string);
-}
-
-/**
- * Evaluate a path from its shorthand form to a literal path
- * usable by rsync.
- *
- * A path is "machine:/path" or "machine:path" or "/path" or "path".
- * 'machine' might instead be an alias record, or the name
- * of a site in the 'sites' folder. 'path' might be (or contain)
- * '%root' or some other path alias. This function will examine
- * all components of the path and evaluate them as necessary to
- * come to the final path.
- *
- * @param path
- * The path to evaluate
- * @param additional_options
- * An array of options that overrides whatever was passed in on
- * the command line (like the 'process' context, but only for
- * the scope of this one call).
- * @param local_only
- * If TRUE, force an error if the provided path points to a remote
- * machine.
- * @param os
- * This should be the local system os, unless evaluate path is
- * being called for rsync, in which case it should be "CWRSYNC"
- * if cwrsync is being used, or "rsync" to automatically select
- * between "LOCAL" and "CWRSYNC" based on the platform.
- * @return
- * The site record for the machine specified in the path, if any,
- * with the path to pass to rsync (including the machine specifier)
- * in the 'evaluated-path' item.
- */
-function drush_sitealias_evaluate_path($path, &$additional_options, $local_only = FALSE, $os = NULL, $command_specific_prefix = '') {
- $site_alias_settings = array();
- $path_aliases = array();
- $remote_user = '';
-
- $preflight = _drush_sitealias_preflight_path($path);
- if (!isset($preflight)) {
- return NULL;
- }
-
- $alias = $preflight['alias'];
- $path = $preflight['path'];
- $machine = $preflight['machine'];
-
- if (isset($alias)) {
- // Note that the alias settings may have an 'os' component, but we do
- // not want to use it here. The paths passed to rsync should always be
- // escaped per the LOCAL rules, without regard to the remote platform type.
- $site_alias_settings = drush_sitealias_get_record($alias);
- if (!empty($command_specific_prefix)) {
- drush_command_set_command_specific_options($command_specific_prefix);
- drush_sitealias_command_default_options($site_alias_settings, $command_specific_prefix);
- }
- }
-
- if (!empty($site_alias_settings)) {
- if ($local_only && array_key_exists('remote-host', $site_alias_settings)) {
- return drush_set_error('DRUSH_REMOTE_SITE_IN_LOCAL_CONTEXT', dt("A remote site alias was used in a context where only a local alias is appropriate."));
- }
-
- // Apply any options from this alias that might affect our rsync
- drush_sitealias_set_alias_context($site_alias_settings);
-
- // Use 'remote-host' from settings if available; otherwise site is local
- if (drush_sitealias_is_remote_site($site_alias_settings)) {
- $machine = drush_remote_host($site_alias_settings);
- }
- else {
- $machine = '';
- }
- }
- else {
- // Strip the machine portion of the path if the
- // alias points to the local machine.
- if (drush_is_local_host($machine)) {
- $machine = '';
- }
- else {
- $machine = "$remote_user$machine";
- }
- }
-
- // TOD: The code below is a little rube-goldberg-ish, and needs to be
- // reworked. core-rsync will call this function twice: once to
- // evaluate the destination, and then again to evaluate the source. Things
- // get odd with --exclude-paths, especially in conjunction with command-specific
- // and the --exclude-files option. @see testCommandSpecific()
-
- // If the --exclude-other-sites option is specified, then
- // convert that into --include-paths='%site' and --exclude-sites.
- if (drush_get_option_override($additional_options, 'exclude-other-sites', FALSE) && !drush_get_context('exclude-other-sites-processed', FALSE)) {
- $include_path_option = drush_get_option_override($additional_options, 'include-paths', '');
- $additional_options['include-paths'] = '%site';
- if (!empty($include_path_option)) {
- // We use PATH_SEPARATOR here because we are later going to explicitly explode() this variable using PATH_SEPARATOR.
- $additional_options['include-paths'] .= PATH_SEPARATOR . $include_path_option;
- }
- $additional_options['exclude-sites'] = TRUE;
- drush_set_context('exclude-other-sites-processed', TRUE);
- }
- else {
- unset($additional_options['include-paths']);
- }
- // If the --exclude-files option is specified, then
- // convert that into --exclude-paths='%files'.
- if (drush_get_option_override($additional_options, 'exclude-files', FALSE) && !drush_get_option_override($additional_options, 'exclude-files-processed', FALSE, 'process')) {
- $exclude_path_option = drush_get_option_override($additional_options, 'exclude-paths', '');
- $additional_options['exclude-paths'] = '%files';
- if (!empty($exclude_path_option)) {
- // We use PATH_SEPARATOR here because we are later going to explicitly explode() this variable using PATH_SEPARATOR.
- $additional_options['exclude-paths'] .= PATH_SEPARATOR . $exclude_path_option;
- }
- $additional_options['exclude-files-processed'] = TRUE;
- }
- else {
- unset($additional_options['exclude-paths']);
- }
-
- // If there was no site specification given, and the
- // machine is local, then try to look
- // up an alias record for the default drush site.
- if (empty($site_alias_settings) && empty($machine)) {
- $drush_uri = drush_get_context('DRUSH_SELECTED_URI', 'default');
- $site_alias_settings = drush_sitealias_get_record($drush_uri);
- }
-
- // Always add transient defaults
- _drush_sitealias_add_transient_defaults($site_alias_settings);
-
- // The $resolve_path variable is used by drush_sitealias_resolve_path_references
- // to test to see if there are any path references such as %site or %files
- // in it, so that resolution is only done if the path alias is referenced.
- // Therefore, we can concatenate without worrying too much about the structure of
- // this variable's contents.
- $include_path = drush_get_option_override($additional_options, 'include-paths', '');
- $exclude_path = drush_get_option_override($additional_options, 'exclude-paths', '');
- if (is_array($include_path)) {
- $include_path = implode('/', $include_path);
- }
- if (is_array($exclude_path)) {
- $include_path = implode('/', $exclude_path);
- }
- $resolve_path = "$path/$include_path/$exclude_path";
- // Resolve path aliases such as %files, if any exist in the path
- if (!empty($resolve_path)) {
- drush_sitealias_resolve_path_references($site_alias_settings, $resolve_path);
- }
-
- if (array_key_exists('path-aliases', $site_alias_settings)) {
- $path_aliases = $site_alias_settings['path-aliases'];
- }
-
- // Get the 'root' setting from the alias; if it does not
- // exist, then get the root from the bootstrapped site.
- if (array_key_exists('root', $site_alias_settings)) {
- $drupal_root = $site_alias_settings['root'];
- }
- elseif (!drush_sitealias_is_remote_site($site_alias_settings)) {
- drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
- $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
- }
- if (empty($drupal_root)) {
- $drupal_root = '';
- }
- else {
- // Add a slash to the end of the drupal root, as below.
- $drupal_root = drush_trim_path($drupal_root) . "/";
- }
- $full_path_aliases = $path_aliases;
- foreach ($full_path_aliases as $key => $value) {
- // Expand all relative path aliases to be based off of the Drupal root
- if (!drush_is_absolute_path($value, "LOCAL") && ($key != '%root')) {
- $full_path_aliases[$key] = $drupal_root . $value;
- }
- // We do not want slashes on the end of our path aliases.
- $full_path_aliases[$key] = drush_trim_path($full_path_aliases[$key]);
- }
-
- // Fill in path aliases in the path, the include path and the exclude path.
- $path = str_replace(array_keys($full_path_aliases), array_values($full_path_aliases), $path);
- if (!empty($include_path)) {
- drush_set_option('include-paths', str_replace(array_keys($path_aliases), array_values($path_aliases), $include_path));
- }
- if (!empty($exclude_path)) {
- drush_set_option('exclude-paths', str_replace(array_keys($path_aliases), array_values($path_aliases), $exclude_path));
- }
- // Next make the rsync path, which includes the machine
- // and path components together.
- // First make empty paths or relative paths start from the drupal root.
- if (empty($path) || (!drush_is_absolute_path($path, "LOCAL"))) {
- $path = $drupal_root . $path;
- }
- // When calculating a path for use with rsync, we must correct
- // absolute paths in the form c:\path when cwrsync is in use.
- $path = drush_correct_absolute_path_for_exec($path, $os);
-
- // If there is a $machine component, to the path, then
- // add it to the beginning
- $evaluated_path = drush_escapeshellarg($path, $os);
- if (!empty($machine)) {
- $evaluated_path = $machine . ':' . $evaluated_path;
- }
-
- //
- // Add our result paths:
- //
- // evaluated-path: machine:/path
- // server-component: machine
- // path-component: :/path
- // path: /path
- // user-path: path (as specified in input parameter)
- //
- $site_alias_settings['evaluated-path'] = $evaluated_path;
- if (!empty($machine)) {
- $site_alias_settings['server-component'] = $machine;
- }
- $site_alias_settings['path-component'] = (!empty($path) ? ':' . $path : '');
- $site_alias_settings['path'] = $path;
- $site_alias_settings['user-path'] = $preflight['path'];
-
- return $site_alias_settings;
-}
-
-/**
- * Option keys used for site selection.
- */
-function drush_sitealias_site_selection_keys() {
- return array('remote-host', 'remote-user', 'ssh-options', '#name', 'os');
-}
-
-
-function sitealias_find_local_drupal_root($site_list) {
- $drupal_root = NULL;
-
- foreach ($site_list as $site) {
- if (($drupal_root == NULL) && (array_key_exists('root', $site) && !array_key_exists('remote-host', $site))) {
- $drupal_root = $site['root'];
- }
- }
-
- return $drupal_root;
-}
-
-
-/**
- * Helper function to obtain the keys' names that need special handling in certain
- * cases.
- * @return
- * A non-associative array containing the needed keys' names.
- */
-function drush_get_special_keys() {
- $special_keys = array(
- 'command-specific',
- 'site-aliases',
- );
- return $special_keys;
-}
-
-/**
- * Read the tmp file where the persistent site setting is stored.
- *
- * @return string
- * A valid site specification.
- */
-function drush_sitealias_site_get() {
- if (($filename = drush_sitealias_get_envar_filename()) && file_exists($filename)) {
- $site = file_get_contents($filename);
- return $site;
- }
- else {
- return FALSE;
- }
-}
-
-/**
- * Un-set the currently use'd site alias.
- */
-function drush_sitealias_site_clear() {
- if ($filename = drush_sitealias_get_envar_filename()) {
- return drush_delete_dir($filename);
- }
- return FALSE;
-}
-
-/**
- * Returns the filename for the file that stores the DRUPAL_SITE variable.
- *
- * @param string $filename_prefix
- * An arbitrary string to prefix the filename with.
- *
- * @return string|false
- * Returns the full path to temp file if possible, or FALSE if not.
- */
-function drush_sitealias_get_envar_filename($filename_prefix = 'drush-drupal-site-') {
- $shell_pid = getenv('DRUSH_SHELL_PID');
- if (!$shell_pid && function_exists('posix_getppid')) {
- $shell_pid = posix_getppid();
- }
- if (!$shell_pid) {
- return FALSE;
- }
-
- $tmp = getenv('TMPDIR') ? getenv('TMPDIR') : '/tmp';
- $username = drush_get_username();
-
- return "{$tmp}/drush-env-{$username}/{$filename_prefix}" . $shell_pid;
-}
-
-/**
- * Cache the specified alias in the alias path cache. The
- * alias path cache creates a lookup from the site folder
- * (/path/to/drupal/sites/default) to the provided alias record.
- *
- * Only the name of the alias and the path to the file it
- * is stored in is cached; when it is retrieved, it is
- * loaded directly from the correct file.
- */
-function drush_sitealias_cache_alias_by_path($alias_record) {
- if (!isset($alias_record['remote-host']) && isset($alias_record['root']) && isset($alias_record['uri']) && isset($alias_record['#name']) && isset($alias_record['#file'])) {
- $path = drush_sitealias_local_site_path($alias_record);
- if ($path) {
- $cid = drush_get_cid('alias-path-', array(), array($path));
- $alias_path_data = array(
- '#name' => $alias_record['#name'],
- '#file' => $alias_record['#file'],
- );
- drush_cache_set($cid, $alias_path_data);
- }
- }
-}
-
-/**
- * Look for a defined alias that points to the specified
- * site directory. The cache is tested first; if nothing
- * is cached, then an exhaustive search is done for the
- * specified site. If the exhaustive search returns a
- * match, then it is cached.
- *
- * @param $path
- * /path/to/drupal/sites/default
- * @return
- * An alias record for the provided path
- */
-function drush_sitealias_lookup_alias_by_path($path, $allow_best_match=FALSE) {
- $result = drush_sitealias_quick_lookup_cached_alias_by_path($path);
- $fallback = array();
- if (empty($result)) {
- $aliases = _drush_sitealias_find_and_load_all_aliases();
- foreach ($aliases as $name => $alias_record) {
- if (!isset($alias_record['remote-host']) && isset($alias_record['root']) && isset($alias_record['uri']) && isset($alias_record['#name']) && isset($alias_record['#file'])) {
- if ($path == drush_sitealias_local_site_path($alias_record)) {
- $result = $alias_record;
- break;
- }
- if (substr($path, 0, strlen($alias_record['root'])) == $alias_record['root']) {
- $fallback = $alias_record;
- }
- }
- }
- }
- if (empty($result) && $allow_best_match) {
- $result = $fallback;
- }
- if (!empty($result)) {
- _drush_sitealias_add_inherited_values_to_record($result);
- drush_sitealias_cache_alias_by_path($result);
- }
- return $result;
-}
-
-/**
- * Look for a cached alias that points to the specified
- * site directory. Nothing is returned if there is no
- * matching cached alias.
- *
- * @param $path
- * /path/to/drupal/sites/default
- * @return
- * An alias record for the provided path
- */
-function drush_sitealias_quick_lookup_cached_alias_by_path($path) {
- $alias_record = array();
- $cid = drush_get_cid('alias-path-', array(), array($path));
- $alias_path_cache = drush_cache_get($cid);
- if (isset($alias_path_cache->data)) {
- $alias_name = $alias_path_cache->data['#name'];
- $alias_file = $alias_path_cache->data['#file'];
-
- $alias_record = _drush_sitealias_find_and_load_alias_from_file($alias_name, array($alias_file));
- _drush_sitealias_add_inherited_values_to_record($alias_record);
- $alias_record['#name'] = $alias_name;
- }
- return $alias_record;
-}
-
-/**
- * Return the site root, if there is one in the record.
- */
-function drush_sitealias_get_root($alias_record) {
- return array_key_exists('root', $alias_record) ? $alias_record['root'] : NULL;
-}
-
-/**
- * Decide on which side to run a core-rsync.
- *
- * @param $source
- * @param $destination
- * @param $runner Where to run the rsync operation: 'destination', 'source',
- * 'auto' ('destination' if both are remote, otherwise '@self') or FALSE (@self)
- * @return mixed
- */
-function drush_get_runner($source, $destination, $runner = FALSE) {
- if (is_string($source)) {
- $source = drush_sitealias_get_record($site);
- }
- if (is_string($destination)) {
- $destination = drush_sitealias_get_record($destination);
- }
-
- // If both sites are remote, and --runner=auto, then we'll use the destination site.
- if (drush_sitealias_is_remote_site($source) && drush_sitealias_is_remote_site($destination)) {
- if ($runner == 'auto') {
- $runner = 'destination';
- }
- }
-
- // If the user explicitly requests a remote site, then return the selected one.
- if ($runner == 'destination') {
- return "@" . $destination['#name'];
- }
- if ($runner == 'source') {
- return "@" . $source['#name'];
- }
-
- // Default to running rsync locally. When in doubt, local is best, because
- // we can always resolve aliases here.
- return '@self';
-}