sitealias.inc

  1. 8.0.x includes/sitealias.inc
  2. 6.x includes/sitealias.inc
  3. 7.x includes/sitealias.inc
  4. 3.x includes/sitealias.inc
  5. 4.x includes/sitealias.inc
  6. 5.x includes/sitealias.inc
  7. master includes/sitealias.inc

The site alias API.

Run commands on remote server(s).

See also

example.aliases.drushrc.php

http://drupal.org/node/670460

Functions

Namesort descending Description
drush_convert_db_from_db_url Convert from an old-style database URL to an array of database settings.
drush_get_runner Decide on which side to run a core-rsync.
drush_get_special_keys Helper function to obtain the keys' names that need special handling in certain cases.
drush_sitealias_add_db_settings If the alias record does not contain a 'databases' or 'db-url' entry, then use backend invoke to look up the settings value from the remote or local site. The 'databases' form is preferred; 'db_url' will be…
drush_sitealias_add_db_url If the alias record does not contain a 'databases' or 'db-url' entry, then use backend invoke to look up the settings value from the remote or local site. The 'db_url' form is preferred; nothing is done if…
drush_sitealias_add_to_alias_path Add a path to the array of paths where alias files are searched for.
drush_sitealias_alias_base_directory If there is a directory 'site-aliases' in the specified search location, then search ONLY in that directory for aliases. Otherwise, search anywhere inside the specified directory for aliases.
drush_sitealias_alias_path Return the array of paths where alias files are searched for.
drush_sitealias_alias_record_to_spec Convert from an alias record to a site specification
drush_sitealias_bootstrapped_site_name Get the name of the current bootstrapped site
drush_sitealias_build_record_from_settings_file
drush_sitealias_cache_alias_by_path 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.
drush_sitealias_cache_db_settings
drush_sitealias_check_arg Check to see if the first command-line arg or the -l option is a site alias; if it is, copy its record values to the 'alias' context.
drush_sitealias_check_arg_and_site_set Check to see if the user specified an alias in an arguement, or via site-set. If so, return the name of the alias.
drush_sitealias_check_site_env
drush_sitealias_convert_databases_to_db_url Create a db-url from the databases record.
drush_sitealias_convert_db_from_db_url Convert from an old-style database URL to an array of database settings
drush_sitealias_convert_db_spec_to_db_url 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.
drush_sitealias_create_self_alias Check to see if a '@self' record was created during bootstrap. If not, make one now.
drush_sitealias_create_sites_alias
drush_sitealias_evaluate_path Evaluate a path from its shorthand form to a literal path usable by rsync.
drush_sitealias_evaluate_paths_in_options Given a properly-escaped options string, replace any occurance of %files and so on embedded inside it with its corresponding path.
drush_sitealias_get_db_spec Return the $db_spec record for the database associated with the provided alias record. which will be used to first add the database information to the alias records, invoking sql-conf to look them up if necessary.
drush_sitealias_get_envar_filename Returns the filename for the file that stores the DRUPAL_SITE variable.
drush_sitealias_get_path_option First return an option set via drush_sitealias_overlay_options, if any, then fall back on "%" . $option from the path aliases.
drush_sitealias_get_record Get a site alias record given an alias name or site specification.
drush_sitealias_get_root Return the site root, if there is one in the record.
drush_sitealias_is_bootstrapped_site Check to see if we have already bootstrapped to a site.
drush_sitealias_is_remote_site Determines whether a given site alias is for a remote site.
drush_sitealias_load_all Load every alias file that can be found anywhere in the alias search path.
drush_sitealias_local_site_path Return the full path to the site directory of the given alias record.
drush_sitealias_lookup_alias_by_path 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.
drush_sitealias_overlay_options 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…
drush_sitealias_quick_lookup_cached_alias_by_path Look for a cached alias that points to the specified site directory. Nothing is returned if there is no matching cached alias.
drush_sitealias_resolve_path_references 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.
drush_sitealias_resolve_sitelist 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.
drush_sitealias_resolve_sitespecs Given an array of site specifications, resolve each one in turn and return an array of alias records. If you only want a single record, it is preferable to simply call drush_sitealias_get_record directly.
drush_sitealias_set_alias_context 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…
drush_sitealias_simplify_names Given a list of alias records, shorten the name used if possible
drush_sitealias_site_dir_from_filename Pull the site directory from the path to settings.php
drush_sitealias_site_get Read the tmp file where the persistent site setting is stored.
drush_sitealias_site_selection_keys Option keys used for site selection.
drush_sitealias_uri_to_site_dir Convert from a URI to a site directory.
drush_sitealias_valid_alias_format Returns TRUE if $alias is a valid format for an alias name.
sitealias_find_local_drupal_root
sitealias_get_databases_from_record Return the databases record from the alias record
_drush_find_local_sites_at_root Return a list of all of the local sites at the specified drupal root.
_drush_find_local_sites_in_sites_folder Return a list of all of the local sites at the specified 'sites' folder.
_drush_sitealias_add_inherited_values Check to see if there is a 'parent' item in the alias; if there is, then load the parent alias record and overlay the entries in the current alias record on top of the items from the parent record.
_drush_sitealias_add_inherited_values_to_record
_drush_sitealias_add_static_defaults 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…
_drush_sitealias_add_transient_defaults 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_sitealias_cache_alias Add an empty record for the specified alias name
_drush_sitealias_derive_record
_drush_sitealias_find_alias_files Function to find all alias files that might contain aliases that match the requested alias name.
_drush_sitealias_find_and_load_alias Traverses the alias search path and finds the specified alias record.
_drush_sitealias_find_and_load_alias_from_file
_drush_sitealias_find_and_load_all_aliases Worker function called by _drush_sitealias_load_alias and drush_sitealias_load_all. Traverses the alias search path and finds the specified alias record.
_drush_sitealias_find_local_alias_name Find the name of a local alias record that has the specified root and uri.
_drush_sitealias_find_local_sites Search for drupal installations in the search path.
_drush_sitealias_find_record_for_local_site If '$alias' is the name of a folder in the sites folder of the given drupal root, then build an alias record for it
_drush_sitealias_get_record This is a continuation of drush_sitealias_get_record, above. It is not intended to be called directly.
_drush_sitealias_initialize_alias_record Initialize an alias record; called as soon as the alias record is loaded from its alias file, before it is stored in the cache.
_drush_sitealias_load_alias Check and see if an alias definition for $alias is available. If it is, load it into the list of aliases cached in the 'site-aliases' context.
_drush_sitealias_preflight_path Call prior to drush_sitealias_evaluate_path to insure that any site-specific aliases associated with any local site in $path are defined.
_drush_sitealias_set_context_by_name Looks up the specified alias record and calls through to drush_sitealias_set_alias_context, below.
_drush_sitealias_set_record_element Utility function used by drush_get_alias; keys that start with '%' or '!' are path aliases, the rest are entries in the alias record.
_drush_sitelist_check_site_records
_drush_sitelist_find_in_list
_sitealias_array_merge Merges two site aliases.

File

includes/sitealias.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * The site alias API.
  5. *
  6. * Run commands on remote server(s).
  7. * @see example.aliases.drushrc.php
  8. * @see http://drupal.org/node/670460
  9. */
  10. use Drush\Log\LogLevel;
  11. /**
  12. * Check to see if the user specified an alias
  13. * in an arguement, or via site-set. If so, return
  14. * the name of the alias.
  15. *
  16. * If the alias came from args, then remove it
  17. * from args.
  18. */
  19. function drush_sitealias_check_arg_and_site_set() {
  20. $args = drush_get_arguments();
  21. // Test to see if the first arg is a valid alias identifier.
  22. // If the first arguement is a well-formed identifier, but we
  23. // cannot find a record for it, then we will fail with an error.
  24. if (!empty($args) && drush_sitealias_valid_alias_format($args[0])) {
  25. // Pop the alias off the arguments list first thing.
  26. $target_alias = array_shift($args);
  27. drush_set_arguments($args);
  28. // Record the user's desired target alias name
  29. drush_set_context('DRUSH_TARGET_SITE_ALIAS', $target_alias);
  30. return $target_alias;
  31. }
  32. // If the user did not specify an alias via an argument,
  33. // check to see if a site env was set. For the site env,
  34. // we insist on being able to find the alias record, or
  35. // we will silently ignore
  36. $site_env = drush_sitealias_site_get();
  37. if ($site_env) {
  38. $site_alias_settings = drush_sitealias_get_record($site_env);
  39. if (!empty($site_alias_settings)) {
  40. drush_set_context('DRUSH_TARGET_SITE_ALIAS', $site_env);
  41. return $site_env;
  42. }
  43. }
  44. // Return an empty array to indicate that no site alias was specified.
  45. return FALSE;
  46. }
  47. /**
  48. * Check to see if the first command-line arg or the
  49. * -l option is a site alias; if it is, copy its record
  50. * values to the 'alias' context.
  51. *
  52. * @return boolean
  53. * TRUE if a site alias was found and processed.
  54. */
  55. function drush_sitealias_check_arg() {
  56. $args = drush_get_arguments();
  57. // Test to see if the first arg is a site specification
  58. if (!empty($args) && _drush_sitealias_set_context_by_name($args[0])) {
  59. drush_set_context('DRUSH_TARGET_SITE_ALIAS', $args[0]);
  60. array_shift($args);
  61. // We only need to expand the site specification
  62. // once, then we are done.
  63. drush_set_arguments($args);
  64. return TRUE;
  65. }
  66. // Return false to indicate that no site alias was specified.
  67. return FALSE;
  68. }
  69. /*
  70. * Check to see if user has selected a site via site-set command.
  71. */
  72. function drush_sitealias_check_site_env() {
  73. $site = drush_get_context('DRUSH_TARGET_SITE_ALIAS');
  74. if (empty($site)) {
  75. $site_env = drush_sitealias_site_get();
  76. if (!empty($site_env) && (_drush_sitealias_set_context_by_name($site_env))) {
  77. drush_set_context('DRUSH_TARGET_SITE_ALIAS', $site_env);
  78. return TRUE;
  79. }
  80. }
  81. // Return false to indicate that no site alias was specified.
  82. return FALSE;
  83. }
  84. /**
  85. * Check to see if a '@self' record was created during bootstrap.
  86. * If not, make one now.
  87. */
  88. function drush_sitealias_create_self_alias() {
  89. $self_record = drush_sitealias_get_record('@self');
  90. if (!array_key_exists('root', $self_record) && !array_key_exists('remote-host', $self_record)) {
  91. $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
  92. $uri = drush_get_context('DRUSH_SELECTED_URI');
  93. if (!empty($drupal_root) && !empty($uri)) {
  94. // Create an alias '@self'
  95. _drush_sitealias_cache_alias('@self', array('root' => $drupal_root, 'uri' => $uri));
  96. }
  97. }
  98. }
  99. /**
  100. * Given a list of alias records, shorten the name used if possible
  101. */
  102. function drush_sitealias_simplify_names($site_list) {
  103. $result = array();
  104. foreach ($site_list as $original_name => $alias_record) {
  105. $adjusted_name = $alias_record['#name'];
  106. $hashpos = strpos($original_name, '#');
  107. if ($hashpos !== FALSE) {
  108. $adjusted_name = substr($original_name, $hashpos);
  109. if (array_key_exists('remote-host', $alias_record)) {
  110. $adjusted_name = $alias_record['remote-host'] . $adjusted_name;
  111. }
  112. }
  113. $result[$adjusted_name] = $alias_record;
  114. }
  115. return $result;
  116. }
  117. /**
  118. * Given an array of site specifications, resolve each one in turn and
  119. * return an array of alias records. If you only want a single record,
  120. * it is preferable to simply call drush_sitealias_get_record directly.
  121. *
  122. * @param $site_specifications
  123. * One of:
  124. * A comma-separated list of site specifications: '@site1,@site2'
  125. * An array of site specifications: array('@site1','@site2')
  126. * An array of alias records:
  127. * array(
  128. * 'site1' => array('root' => ...),
  129. * 'site2' => array('root' => ...)
  130. * )
  131. * An array of site specifications.
  132. * @see drush_sitealias_get_record() for the format of site specifications.
  133. * @return
  134. * An array of alias records
  135. */
  136. function drush_sitealias_resolve_sitespecs($site_specifications, $alias_path_context = NULL) {
  137. $result_list = array();
  138. $not_found = array();
  139. if (!is_array($site_specifications)) {
  140. $site_specifications = explode(',', $site_specifications);
  141. }
  142. if (!empty($site_specifications)) {
  143. foreach ($site_specifications as $site) {
  144. if (is_array($site)) {
  145. $result_list[] = $site;
  146. }
  147. else {
  148. $alias_record = drush_sitealias_get_record($site, $alias_path_context);
  149. if (!$alias_record) {
  150. $not_found[] = $site;
  151. }
  152. else {
  153. $result_list = array_merge($result_list, drush_sitealias_resolve_sitelist($alias_record));
  154. }
  155. }
  156. }
  157. }
  158. return array($result_list, $not_found);
  159. }
  160. /**
  161. * Returns TRUE if $alias is a valid format for an alias name.
  162. *
  163. * Mirrors the allowed formats shown below for drush_sitealias_get_record.
  164. */
  165. function drush_sitealias_valid_alias_format($alias) {
  166. return ( (strpos($alias, ',') !== false) ||
  167. ((strpos($alias, '@') === FALSE ? 0 : 1) + (strpos($alias, '/') === FALSE ? 0 : 1) + (strpos($alias, '#') === FALSE ? 0 : 1) >= 2) ||
  168. ($alias{0} == '#') ||
  169. ($alias{0} == '@')
  170. );
  171. }
  172. /**
  173. * Get a site alias record given an alias name or site specification.
  174. *
  175. * If it is the name of a site alias, return the alias record from
  176. * the site aliases array.
  177. *
  178. * If it is the name of a folder in the 'sites' folder, construct
  179. * an alias record from values stored in settings.php.
  180. *
  181. * If it is a site specification, construct an alias record from the
  182. * values in the specification.
  183. *
  184. * Site specifications come in several forms:
  185. * - /path/to/drupal#sitename
  186. * - user@server/path/to/drupal#sitename
  187. * - user@server/path/to/drupal (sitename == server)
  188. * - user@server#sitename (only if $option['r'] set in some drushrc file on server)
  189. * - #sitename (only if $option['r'] already set, and 'sitename' is a folder in $option['r']/sites)
  190. * - sitename (only if $option['r'] already set, and 'sitename' is a folder in $option['r']/sites)
  191. *
  192. * Note that in the case of the first four forms, it is also possible
  193. * to add additional site variable to the specification using uri query
  194. * syntax. For example:
  195. *
  196. * user@server/path/to/drupal?db-url=...#sitename
  197. *
  198. * @param alias
  199. * An alias name or site specification
  200. * @return array
  201. * An alias record, or empty if none found.
  202. */
  203. function drush_sitealias_get_record($alias, $alias_context = NULL) {
  204. // Check to see if the alias contains commas. If it does, then
  205. // we will go ahead and make a site list record
  206. $alias_record = array();
  207. if (strpos($alias, ',') !== false) {
  208. // TODO: If the site list contains any site lists, or site
  209. // search paths, then we should expand those and merge them
  210. // into this list longhand.
  211. $alias_record['site-list'] = explode(',', $alias);
  212. }
  213. else {
  214. $alias_record = _drush_sitealias_get_record($alias, $alias_context);
  215. }
  216. if (!empty($alias_record)) {
  217. if (array_key_exists('#name', $alias_record)) {
  218. if ($alias_record['#name'] == 'self') {
  219. $path = drush_sitealias_local_site_path($alias_record);
  220. if ($path) {
  221. $cached_alias_record = drush_sitealias_lookup_alias_by_path($path);
  222. $alias_record = array_merge($alias_record, $cached_alias_record);
  223. }
  224. }
  225. }
  226. else {
  227. $alias_record['#name'] = drush_sitealias_uri_to_site_dir($alias);
  228. }
  229. }
  230. return $alias_record;
  231. }
  232. /**
  233. * This is a continuation of drush_sitealias_get_record, above. It is
  234. * not intended to be called directly.
  235. */
  236. function _drush_sitealias_get_record($alias, $alias_context = NULL) {
  237. $alias_record = array();
  238. // Before we do anything else, load $alias if it needs to be loaded
  239. _drush_sitealias_load_alias($alias, $alias_context);
  240. // Check to see if the provided parameter is in fact a defined alias.
  241. $all_site_aliases =& drush_get_context('site-aliases');
  242. if (array_key_exists($alias, $all_site_aliases)) {
  243. $alias_record = $all_site_aliases[$alias];
  244. }
  245. // If the parameter is not an alias, then it is some form of
  246. // site specification (or it is nothing at all)
  247. else {
  248. if (isset($alias)) {
  249. // Cases 1.) - 4.):
  250. // We will check for a site specification if the alias has at least
  251. // two characters from the set '@', '/', '#'.
  252. if ((strpos($alias, '@') === FALSE ? 0 : 1) + ((strpos($alias, '/') === FALSE && strpos($alias, '\\') === FALSE) ? 0 : 1) + (strpos($alias, '#') === FALSE ? 0 : 1) >= 2) {
  253. if ((substr($alias,0,7) != 'http://') && !drush_is_absolute_path($alias)) {
  254. // Add on a scheme so that "user:pass@server" will always parse correctly
  255. $parsed = parse_url('http://' . $alias);
  256. }
  257. else if (drush_is_windows() && drush_is_absolute_path($alias)) {
  258. // On windows if alias begins with a filesystem path we must add file:// scheme to make it parse correcly
  259. $parsed = parse_url('file:///' . $alias);
  260. }
  261. else {
  262. $parsed = parse_url($alias);
  263. }
  264. // Copy various parts of the parsed URL into the appropriate records of the alias record
  265. foreach (array('user' => 'remote-user', 'pass' => 'remote-pass', 'host' => 'remote-host', 'fragment' => 'uri', 'path' => 'root') as $url_key => $option_key) {
  266. if (array_key_exists($url_key, $parsed)) {
  267. _drush_sitealias_set_record_element($alias_record, $option_key, $parsed[$url_key]);
  268. }
  269. }
  270. // If the site specification has a query, also set the query items
  271. // in the alias record. This allows passing db_url as part of the
  272. // site specification, for example.
  273. if (array_key_exists('query', $parsed)) {
  274. foreach (explode('&', $parsed['query']) as $query_arg) {
  275. $query_components = explode('=', $query_arg);
  276. _drush_sitealias_set_record_element($alias_record, urldecode($query_components[0]), urldecode($query_components[1]));
  277. }
  278. }
  279. // Case 3.): If the URL contains a 'host' portion but no fragment, then set the uri to the host
  280. // Note: We presume that 'server' is the best default for case 3; without this code, the default would
  281. // be whatever is set in $options['l'] on the target machine's drushrc.php settings file.
  282. if (array_key_exists('host', $parsed) && !array_key_exists('fragment', $parsed)) {
  283. $alias_record['uri'] = $parsed['host'];
  284. }
  285. // Special checking: relative aliases embedded in a path
  286. $relative_alias_pos = strpos($alias_record['root'], '/@');
  287. if ($relative_alias_pos !== FALSE) {
  288. // Special checking: /path/@sites
  289. $base = substr($alias_record['root'], 0, $relative_alias_pos);
  290. $relative_alias = substr($alias_record['root'], $relative_alias_pos + 1);
  291. if (drush_valid_root($base) || ($relative_alias == '@sites')) {
  292. drush_sitealias_create_sites_alias($base);
  293. $alias_record = drush_sitealias_get_record($relative_alias);
  294. }
  295. else {
  296. $alias_record = array();
  297. }
  298. }
  299. }
  300. else {
  301. // Case 5.) and 6.):
  302. // If the alias is the name of a folder in the 'sites' directory,
  303. // then use it as a local site specification.
  304. $alias_record = _drush_sitealias_find_record_for_local_site($alias);
  305. }
  306. }
  307. }
  308. if (!empty($alias_record)) {
  309. if (!isset($alias_record['remote']) && !isset($alias_record['#loaded-config'])) {
  310. if (array_key_exists('root', $alias_record)) {
  311. drush_sitealias_add_to_alias_path($alias_record['root'] . '/drush');
  312. drush_sitealias_add_to_alias_path($alias_record['root'] . '/sites/all/drush');
  313. }
  314. // TODO: We should probably remove this feature, and put it back
  315. // in, but in different places (e.g. site selection, sql-sync + rsync
  316. // parameters, etc.)
  317. $alias_site_dir = drush_sitealias_local_site_path($alias_record);
  318. if (isset($alias_site_dir)) {
  319. // Add the sites folder of this site to the alias search path list
  320. drush_sitealias_add_to_alias_path($alias_site_dir);
  321. }
  322. if (isset($alias_record['config']) && file_exists($alias_record['config'])) {
  323. drush_load_config_file('site', $alias_record['config']);
  324. $alias_record['#loaded-config'] = TRUE;
  325. }
  326. unset($alias_record['config']);
  327. }
  328. // Add the static defaults
  329. _drush_sitealias_add_static_defaults($alias_record);
  330. // Cache the result with all of its calculated values
  331. $all_site_aliases[$alias] = $alias_record;
  332. }
  333. return $alias_record;
  334. }
  335. /**
  336. * Add a path to the array of paths where alias files are searched for.
  337. *
  338. * @param $add_path
  339. * A path to add to the search path (or NULL to not add any).
  340. * Once added, the new path will remain available until drush
  341. * exits.
  342. * @return
  343. * An array of paths containing all values added so far
  344. */
  345. function drush_sitealias_add_to_alias_path($add_path) {
  346. static $site_paths = array();
  347. if ($add_path != NULL) {
  348. if (!is_array($add_path)) {
  349. $add_path = explode(PATH_SEPARATOR, $add_path);
  350. }
  351. // Normalize path to make sure we don't add the same path twice on
  352. // windows due to different spelling. e.g. c:\tmp and c:/tmp
  353. foreach($add_path as &$path) {
  354. $path = drush_normalize_path($path);
  355. }
  356. $site_paths = array_unique(array_merge($site_paths, $add_path));
  357. }
  358. return $site_paths;
  359. }
  360. /**
  361. * Return the array of paths where alias files are searched for.
  362. *
  363. * @param $alias_path_context
  364. * If the alias being looked up is part of a relative alias,
  365. * the alias path context specifies the context of the primary
  366. * alias the new alias is rooted from. Alias files stored in
  367. * the sites folder of this context, or inside the context itself
  368. * takes priority over any other search path that might define
  369. * a similarly-named alias. In this way, multiple sites can define
  370. * a '@peer' alias.
  371. * @return
  372. * An array of paths
  373. */
  374. function drush_sitealias_alias_path($alias_path_context = NULL) {
  375. $context_path = array();
  376. if (isset($alias_path_context)) {
  377. $context_path = array(drush_sitealias_local_site_path($alias_path_context));
  378. }
  379. // We get the current list of site paths by adding NULL
  380. // (nothing) to the path list, which is a no-op
  381. $site_paths = drush_sitealias_add_to_alias_path(NULL);
  382. // If the user defined the root of a drupal site, then also
  383. // look for alias files in /drush and /sites/all/drush.
  384. $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
  385. if (!empty($drupal_root)) {
  386. $site_paths[] = drush_sitealias_alias_base_directory($drupal_root . '/../drush');
  387. $site_paths[] = drush_sitealias_alias_base_directory($drupal_root . '/drush');
  388. $site_paths[] = drush_sitealias_alias_base_directory($drupal_root . '/sites/all/drush');
  389. $uri = drush_get_context('DRUSH_SELECTED_URI');
  390. if (empty($uri)) {
  391. $uri = 'default';
  392. }
  393. $site_dir = drush_sitealias_uri_to_site_dir($uri, $drupal_root);
  394. if ($site_dir) {
  395. $site_paths[] = drush_sitealias_alias_base_directory("$drupal_root/sites/$site_dir");
  396. }
  397. }
  398. $alias_path = (array) drush_get_context('ALIAS_PATH', array());
  399. return array_unique(array_merge($context_path, $alias_path, $site_paths));
  400. }
  401. /**
  402. * If there is a directory 'site-aliases' in the specified search location,
  403. * then search ONLY in that directory for aliases. Otherwise, search
  404. * anywhere inside the specified directory for aliases.
  405. */
  406. function drush_sitealias_alias_base_directory($dir) {
  407. $potential_location = $dir . '/site-aliases';
  408. if (is_dir($potential_location)) {
  409. return $potential_location;
  410. }
  411. return $dir;
  412. }
  413. /**
  414. * Return the full path to the site directory of the
  415. * given alias record.
  416. *
  417. * @param $alias_record
  418. * The alias record
  419. * @return
  420. * The path to the site directory of the associated
  421. * alias record, or NULL if the record is not a local site.
  422. */
  423. function drush_sitealias_local_site_path($alias_record) {
  424. $result = NULL;
  425. if (isset($alias_record['root']) && !isset($alias_record['remote-host'])) {
  426. if (isset($alias_record['uri'])) {
  427. $uri = $alias_record['uri'];
  428. $uri = preg_replace('#^[^:]*://#', '', $uri);
  429. while (!$result && !empty($uri)) {
  430. if (file_exists($alias_record['root'] . '/sites/sites.php')) {
  431. $sites = array();
  432. include($alias_record['root'] . '/sites/sites.php');
  433. if (array_key_exists($uri, $sites)) {
  434. $result = $alias_record['root'] . '/sites/' . $sites[$uri];
  435. }
  436. }
  437. if (!$result) {
  438. $result = ($alias_record['root'] . '/sites/' . drush_sitealias_uri_to_site_dir($uri, drush_sitealias_get_root($alias_record)));
  439. }
  440. $result = realpath($result);
  441. $uri = preg_replace('#^[^.]*\.*#', '', $uri);
  442. }
  443. }
  444. if (!$result) {
  445. $result = realpath($alias_record['root'] . '/sites/default');
  446. }
  447. }
  448. return $result;
  449. }
  450. /**
  451. * Check and see if an alias definition for $alias is available.
  452. * If it is, load it into the list of aliases cached in the
  453. * 'site-aliases' context.
  454. *
  455. * @param $alias
  456. * The name of the alias to load in ordinary form ('@name')
  457. * @param $alias_path_context
  458. * When looking up a relative alias, the alias path context is
  459. * the primary alias that we will start our search from.
  460. */
  461. function _drush_sitealias_load_alias($alias, $alias_path_context = NULL) {
  462. $all_site_aliases = drush_get_context('site-aliases');
  463. $result = array();
  464. // Only aliases--those named entities that begin with '@'--can be loaded this way.
  465. // We also skip any alias that has already been loaded.
  466. if ((substr($alias,0,1) == '@') && !array_key_exists($alias,$all_site_aliases)) {
  467. $aliasname = substr($alias,1);
  468. $result = _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context);
  469. if (!empty($result)) {
  470. $alias_options = array('site-aliases' => array($aliasname => $result));
  471. _drush_sitealias_add_inherited_values($alias_options['site-aliases']);
  472. drush_set_config_special_contexts($alias_options);
  473. if (array_key_exists('#file', $result)) {
  474. drush_log(dt('Loaded alias !alias from file !file', array('!alias' => $alias, '!file' => $result['#file'])));
  475. }
  476. }
  477. }
  478. return $result;
  479. }
  480. /**
  481. * Load every alias file that can be found anywhere in the
  482. * alias search path.
  483. */
  484. function drush_sitealias_load_all($resolve_parent = TRUE) {
  485. $result = _drush_sitealias_find_and_load_all_aliases();
  486. if (!empty($result) && ($resolve_parent == TRUE)) {
  487. // If any aliases were returned, then check for
  488. // inheritance and then store the aliases into the
  489. // alias cache
  490. _drush_sitealias_add_inherited_values($result);
  491. $alias_options = array('site-aliases' => $result);
  492. drush_set_config_special_contexts($alias_options);
  493. }
  494. }
  495. /**
  496. * Worker function called by _drush_sitealias_load_alias and
  497. * drush_sitealias_load_all. Traverses the alias search path
  498. * and finds the specified alias record.
  499. *
  500. * @return
  501. * An array of $kay => $value pair of alias names and alias records
  502. * loaded.
  503. */
  504. function _drush_sitealias_find_and_load_all_aliases() {
  505. $result = array();
  506. $drush_alias_files = _drush_sitealias_find_alias_files();
  507. drush_set_context('drush-alias-files', $drush_alias_files);
  508. // For every file that matches, check inside it for
  509. // an alias with a matching name.
  510. foreach ($drush_alias_files as $filename) {
  511. if (file_exists($filename)) {
  512. $aliases = $options = array();
  513. // silently ignore files we can't include
  514. if ((@include $filename) === FALSE) {
  515. drush_log(dt('Cannot open alias file "!alias", ignoring.', array('!alias' => realpath($filename))), LogLevel::BOOTSTRAP);
  516. continue;
  517. }
  518. unset($options['site-aliases']); // maybe unnecessary
  519. // If $aliases are not set, but $options are, then define one alias named
  520. // after the first word of the file, before '.alias.drushrc.php.
  521. if (empty($aliases) && !empty($options)) {
  522. $this_alias_name = substr(basename($filename),0,strpos(basename($filename),'.'));
  523. $aliases[$this_alias_name] = $options;
  524. $options = array();
  525. }
  526. // If this is a group alias file, then make an
  527. // implicit alias from the group name that contains
  528. // a site-list of all of the aliases in the file
  529. $group_name = '';
  530. if (substr($filename, -20) == ".aliases.drushrc.php") {
  531. $group_name = basename($filename,".aliases.drushrc.php");
  532. if (!empty($aliases) && !array_key_exists($group_name, $aliases)) {
  533. $alias_names = array();
  534. foreach (array_keys($aliases) as $one_alias) {
  535. $alias_names[] = "@$group_name.$one_alias";
  536. $aliases["$group_name.$one_alias"] = $aliases[$one_alias];
  537. unset($aliases[$one_alias]);
  538. }
  539. $aliases[$group_name] = array('site-list' => implode(',', $alias_names));
  540. }
  541. }
  542. if (!empty($aliases)) {
  543. if (!empty($options)) {
  544. foreach ($aliases as $name => $value) {
  545. $aliases[$name] = array_merge($options, $value);
  546. }
  547. $options = array();
  548. }
  549. foreach ($aliases as $name => $value) {
  550. _drush_sitealias_initialize_alias_record($aliases[$name]);
  551. $aliases[$name]['#name'] = $name;
  552. $aliases[$name]['#file'] = $filename;
  553. }
  554. $result = _sitealias_array_merge($result, $aliases);
  555. // If we found at least one alias from this file
  556. // then record it in the drush-alias-files context.
  557. $drush_alias_files = drush_get_context('drush-alias-files');
  558. if (!in_array($filename, $drush_alias_files)) {
  559. $drush_alias_files[] = $filename;
  560. }
  561. drush_set_context('drush-alias-files', $drush_alias_files);
  562. }
  563. }
  564. }
  565. return $result;
  566. }
  567. /**
  568. * Function to find all alias files that might contain aliases
  569. * that match the requested alias name.
  570. */
  571. function _drush_sitealias_find_alias_files($aliasname = NULL, $alias_path_context = NULL) {
  572. $alias_files_to_consider = array();
  573. // The alias path is a list of folders to search for alias settings files
  574. $alias_path = drush_sitealias_alias_path($alias_path_context);
  575. // $alias_files contains a list of filename patterns
  576. // to search for. We will find any matching file in
  577. // any folder in the alias path. The directory scan
  578. // is not deep, though; only files immediately in the
  579. // search path are considered.
  580. $alias_files = array('/.*aliases\.drush(' . DRUSH_MAJOR_VERSION . '|)rc\.php$/');
  581. if ($aliasname == NULL) {
  582. $alias_files[] = '/.*\.alias\.drush(' . DRUSH_MAJOR_VERSION . '|)rc\.php$/';
  583. }
  584. else {
  585. $alias_files[] = '/' . preg_quote($aliasname, '/') . '\.alias\.drush(' . DRUSH_MAJOR_VERSION . '|)rc\.php$/';
  586. }
  587. // Do not scan into the files directory.
  588. $blacklist = array_merge(array('files'), drush_filename_blacklist());
  589. // Search each path in turn.
  590. foreach ($alias_path as $path) {
  591. // Find all of the matching files in this location
  592. foreach ($alias_files as $file_pattern_to_search_for) {
  593. drush_log(dt('Scanning into @path for @pattern', array('@path' => $path, '@pattern' => $file_pattern_to_search_for)), LogLevel::DEBUG);
  594. $alias_files_to_consider = array_merge($alias_files_to_consider, array_keys(drush_scan_directory($path, $file_pattern_to_search_for, $blacklist, 0, TRUE)));
  595. }
  596. }
  597. return $alias_files_to_consider;
  598. }
  599. /**
  600. * Traverses the alias search path and finds the specified alias record.
  601. *
  602. * @param $aliasname
  603. * The name of the alias without the leading '@' (i.e. '#name')
  604. * or NULL to load every alias found in every alias file.
  605. * @param $alias_path_context
  606. * When looking up a relative alias, the alias path context is
  607. * the primary alias that we will start our search from.
  608. * @return
  609. * An empty array if nothing was loaded. If $aliasname is
  610. * not null, then the array returned is the alias record for
  611. * $aliasname. If $aliasname is NULL, then the array returned
  612. * is a $kay => $value pair of alias names and alias records
  613. * loaded.
  614. */
  615. function _drush_sitealias_find_and_load_alias($aliasname, $alias_path_context = NULL) {
  616. // Special checking for '@sites' alias
  617. if ($aliasname == 'sites') {
  618. $drupal_root = NULL;
  619. if ($alias_path_context != null) {
  620. if (array_key_exists('root', $alias_path_context) && !array_key_exists('remote-host', $alias_path_context)) {
  621. $drupal_root = $alias_path_context['root'];
  622. }
  623. }
  624. else {
  625. $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
  626. }
  627. if (isset($drupal_root) && !is_array($drupal_root)) {
  628. drush_sitealias_create_sites_alias($drupal_root);
  629. }
  630. }
  631. $alias_files_to_consider = _drush_sitealias_find_alias_files($aliasname, $alias_path_context);
  632. return _drush_sitealias_find_and_load_alias_from_file($aliasname, $alias_files_to_consider);
  633. }
  634. function _drush_sitealias_find_and_load_alias_from_file($aliasname, $alias_files_to_consider) {
  635. $result = array();
  636. $result_names = array();
  637. // For every file that matches, check inside it for
  638. // an alias with a matching name.
  639. $recorded_files = array();
  640. foreach ($alias_files_to_consider as $filename) {
  641. if (file_exists($filename)) {
  642. $aliases = $options = array();
  643. // silently ignore files we can't include
  644. if ((@include $filename) === FALSE) {
  645. drush_log(dt('Cannot open alias file "!alias", ignoring.', array('!alias' => realpath($filename))), LogLevel::BOOTSTRAP);
  646. continue;
  647. }
  648. unset($options['site-aliases']); // maybe unnecessary
  649. // If $aliases are not set, but $options are, then define one alias named
  650. // after the first word of the file, before '.alias.drushrc.php.
  651. if (empty($aliases) && !empty($options)) {
  652. $this_alias_name = substr(basename($filename),0,strpos(basename($filename),'.'));
  653. $aliases[$this_alias_name] = $options;
  654. $options = array();
  655. }
  656. // If this is a group alias file, then make an
  657. // implicit alias from the group name that contains
  658. // a site-list of all of the aliases in the file
  659. $group_prefix = '';
  660. if (substr($filename, -20) == ".aliases.drushrc.php") {
  661. $group_name = basename($filename,".aliases.drushrc.php");
  662. $group_prefix = $group_name . '.';
  663. if (!empty($aliases) && !array_key_exists($group_name, $aliases)) {
  664. $alias_names = array();
  665. foreach (array_keys($aliases) as $one_alias) {
  666. $alias_names[] = "@$group_name.$one_alias";
  667. $aliases[$one_alias]['#name'] = "$group_name.$one_alias";
  668. $aliases[$one_alias]['#group'] = $group_name;
  669. $aliases["$group_name.$one_alias"] = $aliases[$one_alias];
  670. $aliases[$one_alias]["#hidden"] = TRUE;
  671. }
  672. $aliases[$group_name] = array('site-list' => implode(',', $alias_names), '#group' => $group_name, '#name' => $group_name);
  673. }
  674. }
  675. // Store only the named alias into the alias cache
  676. if ((isset($aliases)) && !empty($aliasname) && array_key_exists($aliasname, $aliases)) {
  677. drush_set_config_special_contexts($options); // maybe unnecessary
  678. $one_result = array_merge($options, $aliases[$aliasname]);
  679. $one_result['#file'] = $filename;
  680. if (!array_key_exists('#name', $one_result)) {
  681. $one_result['#name'] = $aliasname;
  682. }
  683. _drush_sitealias_initialize_alias_record($one_result);
  684. // If the alias name is exactly the same as a previous match, then
  685. // merge the two records together
  686. if (!empty($result) && ($result['#name'] == $one_result['#name'])) {
  687. $result = _sitealias_array_merge($result, $one_result);
  688. }
  689. // Add the name of the found record to the list of results
  690. else {
  691. $result_names[] = "@" . $one_result['#name'];
  692. $result = $one_result;
  693. }
  694. }
  695. }
  696. }
  697. // If there are multiple matches, then return a list of results.
  698. if (count($result_names) > 1) {
  699. $result = array('site-list' => $result_names);
  700. }
  701. return $result;
  702. }
  703. /**
  704. * Merges two site aliases.
  705. *
  706. * array_merge_recursive is too much; we only want to run
  707. * array_merge on the common top-level keys of the array.
  708. *
  709. * @param array $site_alias_a
  710. * A site alias array.
  711. * @param array $site_alias_b
  712. * A site alias array.
  713. * @return
  714. * A site alias array where the keys from $site_alias_a are overwritten by the
  715. * keys from $site_alias_b.
  716. */
  717. function _sitealias_array_merge($site_alias_a, $site_alias_b) {
  718. $result = $site_alias_a;
  719. foreach($site_alias_b as $key => $value) {
  720. if (is_array($value) && array_key_exists($key, $result)) {
  721. $result[$key] = array_merge($result[$key], $value);
  722. }
  723. else {
  724. $result[$key] = $value;
  725. }
  726. }
  727. return $result;
  728. }
  729. /**
  730. * Check to see if there is a 'parent' item in the alias; if there is,
  731. * then load the parent alias record and overlay the entries in the
  732. * current alias record on top of the items from the parent record.
  733. *
  734. * @param $aliases
  735. * An array of alias records that are modified in-place.
  736. */
  737. function _drush_sitealias_add_inherited_values(&$aliases) {
  738. foreach ($aliases as $alias_name => $alias_value) {
  739. // Prevent circular references from causing an infinite loop
  740. _drush_sitealias_cache_alias("@$alias_name", array());
  741. _drush_sitealias_add_inherited_values_to_record($alias_value);
  742. $aliases[$alias_name] = $alias_value;
  743. }
  744. }
  745. function _drush_sitealias_add_inherited_values_to_record(&$alias_value) {
  746. drush_command_invoke_all_ref('drush_sitealias_alter', $alias_value);
  747. if (isset($alias_value['parent'])) {
  748. drush_log(dt("Using deprecated 'parent' element '!parent' in '!name'.", array('!parent' => $alias_value['parent'], '!name' => $alias_value['#name'])), LogLevel::DEBUG);
  749. // Fetch and merge in each parent
  750. foreach (explode(',', $alias_value['parent']) as $parent) {
  751. $parent_record = drush_sitealias_get_record($parent);
  752. unset($parent_record['#name']);
  753. unset($parent_record['#file']);
  754. unset($parent_record['#hidden']);
  755. $array_based_keys = array_merge(drush_get_special_keys(), array('path-aliases'));
  756. foreach ($array_based_keys as $array_based_key) {
  757. if (isset($alias_value[$array_based_key]) && isset($parent_record[$array_based_key])) {
  758. $alias_value[$array_based_key] = array_merge($parent_record[$array_based_key], $alias_value[$array_based_key]);
  759. }
  760. }
  761. $alias_value = array_merge($parent_record, $alias_value);
  762. }
  763. }
  764. unset($alias_value['parent']);
  765. }
  766. /**
  767. * Add an empty record for the specified alias name
  768. *
  769. * @param $alias_name
  770. * The name of the alias, including the leading "@"
  771. */
  772. function _drush_sitealias_cache_alias($alias_name, $alias_record) {
  773. $cache =& drush_get_context('site-aliases');
  774. // If the alias already exists in the cache, then merge
  775. // the new alias with the existing alias
  776. if (array_key_exists($alias_name, $cache)) {
  777. $alias_record = array_merge($cache[$alias_name], $alias_record);
  778. }
  779. if (!isset($alias_record['#name'])) {
  780. $alias_record['#name'] = trim($alias_name, '@');
  781. }
  782. $cache[$alias_name] = $alias_record;
  783. // If the alias record points at a local site, make sure
  784. // that /drush, /sites/all/drush and the site folder for that site
  785. // are added to the alias path, so that other alias files
  786. // stored in those locations become searchable.
  787. if (!array_key_exists('remote-host', $alias_record) && !empty($alias_record['root'])) {
  788. drush_sitealias_add_to_alias_path($alias_record['root'] . '/drush');
  789. drush_sitealias_add_to_alias_path($alias_record['root'] . '/sites/all/drush');
  790. $site_dir = drush_sitealias_local_site_path($alias_record);
  791. if (isset($site_dir)) {
  792. drush_sitealias_add_to_alias_path($site_dir);
  793. }
  794. }
  795. }
  796. /**
  797. * If the alias record does not contain a 'databases' or 'db-url'
  798. * entry, then use backend invoke to look up the settings value
  799. * from the remote or local site. The 'db_url' form is preferred;
  800. * nothing is done if 'db_url' is not available (e.g. on a D7 site)
  801. *
  802. * @param $alias_record
  803. * The full alias record to populate with database settings
  804. */
  805. function drush_sitealias_add_db_url(&$alias_record) {
  806. if (!isset($alias_record['db-url']) && !isset($alias_record['databases']) && !isset($alias_record['site-list'])) {
  807. drush_sitealias_add_db_settings($alias_record);
  808. }
  809. if (!isset($alias_record['db-url']) && isset($alias_record['databases'])) {
  810. $alias_record['db-url'] = drush_sitealias_convert_databases_to_db_url($alias_record['databases']);
  811. }
  812. }
  813. /**
  814. * Drush still accepts --db-url format database specifications as
  815. * cli parameters; it is therefore useful to be able to convert
  816. * from a database record back to a db-url sometimes.
  817. */
  818. function drush_sitealias_convert_db_spec_to_db_url($db_spec) {
  819. $result = urlencode($db_spec["driver"]) . "://";
  820. if (isset($db_spec["username"])) {
  821. $result .= urlencode($db_spec["username"]);
  822. if (isset($db_spec["password"])) {
  823. $result .= ":" . urlencode($db_spec["password"]);
  824. }
  825. $result .= "@";
  826. }
  827. // Host is required, unless this is an sqlite db.
  828. if (isset($db_spec["host"])) {
  829. $result .= urlencode($db_spec["host"]);
  830. if (isset($db_spec["port"])) {
  831. $result .= ":" . urlencode($db_spec["port"]);
  832. }
  833. $result .= '/' . urlencode($db_spec["database"]);
  834. }
  835. else {
  836. // URL-encode the database, but convert slashes
  837. // back to their original form for readability.
  838. // This portion is the "path" of the URL, so it may
  839. // contain slashes. This is important for sqlite.
  840. $result .= str_replace("%2F", "/", urlencode(ltrim($db_spec["database"], '/')));
  841. }
  842. return $result;
  843. }
  844. /**
  845. * Create a db-url from the databases record.
  846. */
  847. function drush_sitealias_convert_databases_to_db_url($databases) {
  848. if ((count($databases) == 1) && isset($databases['default'])) {
  849. $result = drush_sitealias_convert_db_spec_to_db_url($databases['default']['default']);
  850. }
  851. else {
  852. foreach ($databases as $key => $db_info) {
  853. $result[$key] = drush_sitealias_convert_db_spec_to_db_url($db_info['default']);
  854. }
  855. }
  856. return $result;
  857. }
  858. /**
  859. * Return the databases record from the alias record
  860. *
  861. * @param $alias_record
  862. * A record returned from drush_sitealias_get_record
  863. * @returns
  864. * A databases record (always in D7 format) or NULL
  865. * if the databases record could not be found.
  866. */
  867. function sitealias_get_databases_from_record(&$alias_record) {
  868. $altered_record = drush_sitealias_add_db_settings($alias_record);
  869. return array_key_exists('databases', $alias_record) ? $alias_record['databases'] : NULL;
  870. }
  871. /**
  872. * Return the $db_spec record for the database associated with
  873. * the provided alias record. @see drush_sitealias_add_db_settings(),
  874. * which will be used to first add the database information to the
  875. * alias records, invoking sql-conf to look them up if necessary.
  876. *
  877. * The options 'database' and 'target' are used to specify which
  878. * specific database should be fetched from the database record;
  879. * they may appear in the alias definition, or may be taken from the
  880. * command line options. The values 'default' and 'default' are
  881. * used if these options are not specified in either location.
  882. *
  883. * Note that in the context of sql-sync, the site alias record will
  884. * be taken from one of the source or target aliases
  885. * (e.g. `drush sql-sync @source @target`), which will be overlayed with
  886. * any options that begin with 'source-' or 'target-', respectively.
  887. * Therefore, the commandline options 'source-database' and 'source-target'
  888. * (or 'target-database' and 'source-target') may also affect the operation
  889. * of this function.
  890. */
  891. function drush_sitealias_get_db_spec(&$alias_record, $default_to_self = FALSE, $prefix = '') {
  892. $db_spec = NULL;
  893. $databases = sitealias_get_databases_from_record($alias_record);
  894. if (isset($databases) && !empty($databases)) {
  895. $database = drush_sitealias_get_option($alias_record, 'database', 'default', $prefix);
  896. $target = drush_sitealias_get_option($alias_record, 'target', 'default', $prefix);
  897. if (array_key_exists($database, $databases) && array_key_exists($target, $databases[$database])) {
  898. $db_spec = $databases[$database][$target];
  899. }
  900. }
  901. elseif ($default_to_self) {
  902. $db_spec = _drush_sql_get_db_spec();
  903. }
  904. if (isset($db_spec)) {
  905. $remote_host = drush_sitealias_get_option($alias_record, 'remote-host', NULL, $prefix);
  906. if (!drush_is_local_host($remote_host)) {
  907. $db_spec['remote-host'] = $remote_host;
  908. $db_spec['port'] = drush_sitealias_get_option($alias_record, 'remote-port', (isset($db_spec['port']) ? $db_spec['port'] : NULL), $prefix);
  909. }
  910. }
  911. return $db_spec;
  912. }
  913. /**
  914. * If the alias record does not contain a 'databases' or 'db-url'
  915. * entry, then use backend invoke to look up the settings value
  916. * from the remote or local site. The 'databases' form is
  917. * preferred; 'db_url' will be converted to 'databases' if necessary.
  918. *
  919. * @param $alias_record
  920. * The full alias record to populate with database settings
  921. */
  922. function drush_sitealias_add_db_settings(&$alias_record) {
  923. $altered_record = FALSE;
  924. if (isset($alias_record['root'])) {
  925. // If the alias record does not have a defined 'databases' entry,
  926. // then we'll need to look one up
  927. if (!isset($alias_record['db-url']) && !isset($alias_record['databases']) && !isset($alias_record['site-list'])) {
  928. $values = drush_invoke_process($alias_record, "sql-conf", array(), array('all' => TRUE), array('integrate' => FALSE, 'override-simulated' => TRUE));
  929. if (is_array($values) && ($values['error_status'] == 0)) {
  930. $altered_record = TRUE;
  931. // If there are any special settings in the '@self' record returned by drush_invoke_process,
  932. // then add those into our altered record as well
  933. if (array_key_exists('self', $values)) {
  934. $alias_record = array_merge($values['self'], $alias_record);
  935. }
  936. drush_sitealias_cache_db_settings($alias_record, $values['object']);
  937. }
  938. }
  939. }
  940. return $altered_record;
  941. }
  942. function drush_sitealias_cache_db_settings(&$alias_record, $databases) {
  943. if (!empty($databases)) {
  944. $alias_record['databases'] = $databases;
  945. }
  946. // If the name is set, then re-cache the record after we fetch the databases
  947. if (array_key_exists('#name', $alias_record)) {
  948. $all_site_aliases =& drush_get_context('site-aliases');
  949. $all_site_aliases['@' . $alias_record['#name']] = $alias_record;
  950. // Check and see if this record is a copy of 'self'
  951. 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'])) {
  952. $all_site_aliases['@self'] = $alias_record;
  953. }
  954. }
  955. }
  956. /**
  957. * Check to see if we have already bootstrapped to a site.
  958. */
  959. function drush_sitealias_is_bootstrapped_site($alias_record) {
  960. if (!isset($alias_record['remote-host']) && array_key_exists('root', $alias_record)) {
  961. $self_record = drush_sitealias_get_record("@self");
  962. if (empty($self_record) || !array_key_exists('root', $self_record)) {
  963. // TODO: If we have not bootstrapped to a site yet, we could
  964. // perhaps bootstrap to $alias_record here.
  965. return FALSE;
  966. }
  967. elseif(($alias_record['root'] == $self_record['root']) && ($alias_record['uri'] == $self_record['uri'])) {
  968. return TRUE;
  969. }
  970. }
  971. return FALSE;
  972. }
  973. /**
  974. * Determines whether a given site alias is for a remote site.
  975. *
  976. * @param string $alias
  977. * An alias name or site specification.
  978. *
  979. * @return bool
  980. * Returns TRUE if the alias refers to a remote site, FALSE if it does not, or NULL is unsure.
  981. */
  982. function drush_sitealias_is_remote_site($alias) {
  983. if (is_array($alias) && !empty($alias['remote-host'])) {
  984. return TRUE;
  985. }
  986. if (!is_string($alias) || !strlen($alias)) {
  987. return NULL;
  988. }
  989. $site_record = drush_sitealias_get_record($alias);
  990. if ($site_record) {
  991. if (!empty($site_record['remote-host'])) {
  992. return TRUE;
  993. }
  994. else {
  995. return FALSE;
  996. }
  997. }
  998. else {
  999. drush_set_error('Unrecognized site alias.');
  1000. }
  1001. }
  1002. /**
  1003. * Get the name of the current bootstrapped site
  1004. */
  1005. function drush_sitealias_bootstrapped_site_name() {
  1006. $site_name = NULL;
  1007. $self_record = drush_sitealias_get_record('@self');
  1008. if (array_key_exists('#name', $self_record)) {
  1009. $site_name = $self_record['#name'];
  1010. }
  1011. if (!isset($site_name) || ($site_name == '@self')) {
  1012. $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
  1013. if (isset($drupal_root)) {
  1014. $drupal_uri = drush_get_context('DRUSH_SELECTED_URI', 'default');
  1015. $drupal_uri = str_replace('http://', '', $drupal_uri);
  1016. // TODO: Maybe use _drush_sitealias_find_local_alias_name?
  1017. $site_name = $drupal_root . '#' . $drupal_uri;
  1018. }
  1019. }
  1020. return $site_name;
  1021. }
  1022. /**
  1023. * If there are any path aliases (items beginning with "%") in the test
  1024. * string, then resolve them as path aliases and add them to the provided
  1025. * alias record.
  1026. *
  1027. * @param $alias_record
  1028. * The full alias record to use in path alias expansion
  1029. * @param $test_string
  1030. * A slash-separated list of path aliases to resolve
  1031. * e.g. "%files/%special".
  1032. */
  1033. function drush_sitealias_resolve_path_references(&$alias_record, $test_string = '') {
  1034. $path_aliases = array_key_exists('path-aliases', $alias_record) ? $alias_record['path-aliases'] : array();
  1035. // Convert the test string into an array of items, and
  1036. // from this make a comma-separated list of projects
  1037. // that we can pass to 'drush status'.
  1038. $test_array = explode('/', $test_string);
  1039. $project_array = array();
  1040. foreach($test_array as $one_item) {
  1041. if (!empty($one_item) && ($one_item[0] == '%') && (!array_key_exists($one_item,$path_aliases))) {
  1042. $project_array[] = substr($one_item,1);
  1043. }
  1044. }
  1045. $project_list = implode(',', $project_array);
  1046. if (!empty($project_array)) {
  1047. // Optimization: if we're already bootstrapped to the
  1048. // site specified by $alias_record, then we can just
  1049. // call _core_site_status_table() rather than use backend invoke.
  1050. if (drush_sitealias_is_bootstrapped_site($alias_record) && drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  1051. $status_values = _core_site_status_table($project_list);
  1052. }
  1053. else {
  1054. $values = drush_invoke_process($alias_record, "core-status", array(), empty($project_list) ? array() : array('project' => $project_list), array('integrate' => FALSE, 'override-simulated' => TRUE));
  1055. $status_values = $values['object'];
  1056. }
  1057. if (isset($status_values['%paths'])) {
  1058. foreach ($status_values['%paths'] as $key => $path) {
  1059. $alias_record['path-aliases'][$key] = $path;
  1060. }
  1061. }
  1062. // If 'root' is not set in the alias, then fill it in from the status values.
  1063. if (!isset($alias_record['root']) && isset($status_values['root'])) {
  1064. $alias_record['root'] = $status_values['root'];
  1065. }
  1066. }
  1067. }
  1068. /**
  1069. * Given an alias record that is a site list (contains a 'site-list' entry),
  1070. * resolve all of the members of the site list and return them
  1071. * is an array of alias records.
  1072. *
  1073. * @param $alias_record
  1074. * The site list alias record array
  1075. * @return
  1076. * An array of individual site alias records
  1077. */
  1078. function drush_sitealias_resolve_sitelist($alias_record) {
  1079. $result_list = array();
  1080. if (isset($alias_record)) {
  1081. if (array_key_exists('site-list', $alias_record)) {
  1082. foreach ($alias_record['site-list'] as $sitespec) {
  1083. $one_result = drush_sitealias_get_record($sitespec);
  1084. $result_list = array_merge($result_list, drush_sitealias_resolve_sitelist($one_result));
  1085. }
  1086. }
  1087. elseif (array_key_exists('#name', $alias_record)) {
  1088. $result_list[$alias_record['#name']] = $alias_record;
  1089. }
  1090. }
  1091. return $result_list;
  1092. }
  1093. function _drush_sitelist_find_in_list($one_source, &$target) {
  1094. $result = FALSE;
  1095. foreach ($target as $key => $one_target) {
  1096. if(_drush_sitelist_check_site_records($one_source, $one_target)) {
  1097. $result = $one_target;
  1098. unset($target[$key]);
  1099. }
  1100. }
  1101. return $result;
  1102. }
  1103. function _drush_sitelist_check_site_records($source, $target) {
  1104. if ((array_key_exists('uri', $source)) && (array_key_exists('uri', $target)) && ($source['uri'] == $target['uri'])) {
  1105. return TRUE;
  1106. }
  1107. return FALSE;
  1108. }
  1109. /**
  1110. * Initialize an alias record; called as soon as the alias
  1111. * record is loaded from its alias file, before it is stored
  1112. * in the cache.
  1113. *
  1114. * @param alias_record
  1115. * The alias record to be initialized; parameter is modified in place.
  1116. */
  1117. function _drush_sitealias_initialize_alias_record(&$alias_record) {
  1118. // If there is a 'from-list' entry, then build a derived
  1119. // list based on the site list with the given name.
  1120. if (array_key_exists('from-list', $alias_record)) {
  1121. // danger of infinite loops... move to transient defaults?
  1122. $from_record = drush_sitealias_get_record($alias_record['from-list']);
  1123. $from_list = drush_sitealias_resolve_sitelist($from_record);
  1124. $derived_list = array();
  1125. foreach ($from_list as $one_record) {
  1126. $derived_record = _drush_sitealias_derive_record($one_record, $alias_record);
  1127. $derived_list[] = drush_sitealias_alias_record_to_spec($derived_record);
  1128. }
  1129. $alias_record = array();
  1130. if (!empty($derived_list)) {
  1131. $alias_record['site-list'] = $derived_list;
  1132. }
  1133. }
  1134. // If there is a 'site-search-path' entry, then build
  1135. // a 'site-list' entry from all of the sites that can be
  1136. // found in the search path.
  1137. if (array_key_exists('site-search-path', $alias_record)) {
  1138. // TODO: Is there any point in merging the sites from
  1139. // the search path with any sites already listed in the
  1140. // 'site-list' entry? For now we'll just overwrite.
  1141. $search_path = $alias_record['site-search-path'];
  1142. if (!is_array($search_path)) {
  1143. $search_path = explode(',', $search_path);
  1144. }
  1145. $found_sites = _drush_sitealias_find_local_sites($search_path);
  1146. $alias_record['site-list'] = $found_sites;
  1147. // The 'unordered-list' flag indicates that the order of the items in the site list is not stable.
  1148. $alias_record['unordered-list'] = '1';
  1149. // DEBUG: var_export($alias_record, FALSE);
  1150. }
  1151. if (array_key_exists('site-list', $alias_record)) {
  1152. if (!is_array($alias_record['site-list'])) {
  1153. $alias_record['site-list'] = explode(',', $alias_record['site-list']);
  1154. }
  1155. }
  1156. else {
  1157. if (isset($alias_record['root']) && !isset($alias_recort['uri'])) {
  1158. $alias_recort['uri'] = 'default';
  1159. }
  1160. }
  1161. }
  1162. /**
  1163. * Add "static" default values to the given alias record. The
  1164. * difference between a static default and a transient default is
  1165. * that static defaults -always- exist in the alias record, and
  1166. * they are cached, whereas transient defaults are only added
  1167. * if the given drush command explicitly adds them.
  1168. *
  1169. * @param alias_record
  1170. * An alias record with most values already filled in
  1171. */
  1172. function _drush_sitealias_add_static_defaults(&$alias_record) {
  1173. // If there is a 'db-url' entry but not 'databases' entry, then we will
  1174. // build 'databases' from 'db-url' so that drush commands that use aliases
  1175. // can always count on using a uniform 'databases' array.
  1176. if (isset($alias_record['db-url']) && !isset($alias_record['databases'])) {
  1177. $alias_record['databases'] = drush_sitealias_convert_db_from_db_url($alias_record['db-url']);
  1178. }
  1179. // Adjustments for aliases to drupal instances (as opposed to aliases that are site lists)
  1180. if (array_key_exists('uri', $alias_record)) {
  1181. // Make sure that there is always a 'path-aliases' array
  1182. if (!array_key_exists('path-aliases', $alias_record)) {
  1183. $alias_record['path-aliases'] = array();
  1184. }
  1185. // If there is a 'root' entry, then copy it to the '%root' path alias
  1186. if (isset($alias_record['root'])) {
  1187. $alias_record['path-aliases']['%root'] = $alias_record['root'];
  1188. }
  1189. }
  1190. }
  1191. function _drush_sitealias_derive_record($from_record, $modifying_record) {
  1192. $result = $from_record;
  1193. // If there is a 'remote-user' in the modifying record, copy it.
  1194. if (array_key_exists('remote-user', $modifying_record)) {
  1195. $result['remote-user'] = $from_record['remote_user'];
  1196. }
  1197. // If there is a 'remote-host', then:
  1198. // If it is empty, clear the remote host in the result record
  1199. // If it ends in '.', then prepend it to the remote host in the result record
  1200. // Otherwise, copy it to the result record
  1201. if (array_key_exists('remote-host', $modifying_record)) {
  1202. $remote_host_modifier = $modifying_record['remote-host'];
  1203. if(empty($remote_host_modifier)) {
  1204. unset($result['remote-host']);
  1205. unset($result['remote-user']);
  1206. }
  1207. elseif ($remote_host_modifier[strlen($remote_host_modifier)-1] == '.') {
  1208. $result['remote-host'] = $remote_host_modifier . $result['remote-host'];
  1209. }
  1210. else {
  1211. $result['remote-host'] = $remote_host_modifier;
  1212. }
  1213. }
  1214. // If there is a 'root', then:
  1215. // If it begins with '/', copy it to the result record
  1216. // Otherwise, append it to the result record
  1217. if (array_key_exists('root', $modifying_record)) {
  1218. $root_modifier = $modifying_record['root'];
  1219. if($root_modifier[0] == '/') {
  1220. $result['root'] = $root_modifier;
  1221. }
  1222. else {
  1223. $result['root'] = $result['root'] . '/' . $root_modifier;
  1224. }
  1225. }
  1226. // Poor man's realpath: take out the /../ with preg_replace.
  1227. // (realpath fails if the files in the path do not exist)
  1228. while(strpos($result['root'], '/../') !== FALSE) {
  1229. $result['root'] = preg_replace('/\w+\/\.\.\//', '', $result['root']);
  1230. }
  1231. // TODO: Should we allow the uri to be transformed?
  1232. // I think that if the uri does not match, then you should
  1233. // always build the list by hand, and not rely on '_drush_sitealias_derive_record'.
  1234. return $result;
  1235. }
  1236. /**
  1237. * Convert from an alias record to a site specification
  1238. *
  1239. * @param alias_record
  1240. * The full alias record to convert
  1241. *
  1242. * @param with_db
  1243. * True if the site specification should include a ?db-url term
  1244. *
  1245. * @return string
  1246. * The site specification
  1247. */
  1248. function drush_sitealias_alias_record_to_spec($alias_record, $with_db = false) {
  1249. $result = '';
  1250. // TODO: we should handle 'site-list' records too.
  1251. if (array_key_exists('site-list', $alias_record)) {
  1252. // TODO: we should actually expand the site list and recompose it
  1253. $result = implode(',', $alias_record['site-list']);
  1254. }
  1255. else {
  1256. // There should always be a uri
  1257. if (array_key_exists('uri', $alias_record)) {
  1258. $result = '#' . drush_sitealias_uri_to_site_dir($alias_record['uri'], drush_sitealias_get_root($alias_record));
  1259. }
  1260. // There should always be a root
  1261. if (array_key_exists('root', $alias_record)) {
  1262. $result = $alias_record['root'] . $result;
  1263. }
  1264. if (array_key_exists('remote-host', $alias_record)) {
  1265. $result = drush_remote_host($alias_record) . $result;
  1266. }
  1267. // Add the database info to the specification if desired
  1268. if ($with_db) {
  1269. // If db-url is not supplied, look it up from the remote
  1270. // or local site and add it to the site alias
  1271. if (!isset($alias_record['db-url'])) {
  1272. drush_sitealias_add_db_url($alias_record);
  1273. }
  1274. $result = $result . '?db-url=' . urlencode(is_array($alias_record['db-url']) ? $alias_record['db-url']['default'] : $alias_record['db-url']);
  1275. }
  1276. }
  1277. return $result;
  1278. }
  1279. /**
  1280. * Search for drupal installations in the search path.
  1281. *
  1282. * @param search_path
  1283. * An array of drupal root folders
  1284. *
  1285. * @return
  1286. * An array of site specifications (/path/to/root#sitename.com)
  1287. */
  1288. function _drush_sitealias_find_local_sites($search_path) {
  1289. $result = array();
  1290. foreach ($search_path as $a_drupal_root) {
  1291. $result = array_merge($result, _drush_find_local_sites_at_root($a_drupal_root));
  1292. }
  1293. return $result;
  1294. }
  1295. /**
  1296. * Return a list of all of the local sites at the specified drupal root.
  1297. */
  1298. function _drush_find_local_sites_at_root($a_drupal_root = '', $search_depth = 1) {
  1299. $site_list = array();
  1300. $base_path = (empty($a_drupal_root) ? drush_get_context('DRUSH_DRUPAL_ROOT') : $a_drupal_root );
  1301. if (!empty($base_path)) {
  1302. if (drush_valid_root($base_path)) {
  1303. // If $a_drupal_root is in fact a valid drupal root, then return
  1304. // all of the sites found inside the 'sites' folder of this drupal instance.
  1305. $site_list = _drush_find_local_sites_in_sites_folder($base_path);
  1306. }
  1307. else {
  1308. $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);
  1309. foreach ($bootstrap_files as $one_bootstrap => $info) {
  1310. $includes_dir = dirname($one_bootstrap);
  1311. if (basename($includes_dir) == basename(dirname(DRUSH_DRUPAL_SIGNATURE))) {
  1312. $drupal_root = dirname($includes_dir);
  1313. $site_list = array_merge(_drush_find_local_sites_in_sites_folder($drupal_root), $site_list);
  1314. }
  1315. }
  1316. }
  1317. }
  1318. return $site_list;
  1319. }
  1320. /**
  1321. * Return a list of all of the local sites at the specified 'sites' folder.
  1322. */
  1323. function _drush_find_local_sites_in_sites_folder($a_drupal_root) {
  1324. $site_list = array();
  1325. // If anyone searches for sites at a given root, then
  1326. // make sure that alias files stored at this root
  1327. // directory are included in the alias search path
  1328. drush_sitealias_add_to_alias_path($a_drupal_root);
  1329. $base_path = $a_drupal_root . '/sites';
  1330. // TODO: build a cache keyed off of $base_path (realpath($base_path)?),
  1331. // so that it is guarenteed that the lists returned will definitely be
  1332. // exactly the same should this routine be called twice with the same path.
  1333. $files = drush_scan_directory($base_path, '/settings\.php/', array('.', '..', 'CVS', 'all'), 0, 1, 'filename', 1);
  1334. foreach ($files as $filename => $info) {
  1335. if ($info->basename == 'settings.php') {
  1336. // First we'll resolve the realpath of the settings.php file,
  1337. // so that we get the correct drupal root when symlinks are in use.
  1338. $real_sitedir = dirname(realpath($filename));
  1339. $real_root = drush_locate_root($filename);
  1340. if ($real_root !== FALSE) {
  1341. $a_drupal_site = $real_root . '#' . basename($real_sitedir);
  1342. }
  1343. // If the symlink points to some folder outside of any drupal
  1344. // root, then we'll use the
  1345. else {
  1346. $uri = drush_sitealias_site_dir_from_filename($filename);
  1347. $a_drupal_site = $a_drupal_root . '#' . $uri;
  1348. }
  1349. // Add the site if it isn't already in the array
  1350. if (!in_array($a_drupal_site, $site_list)) {
  1351. $site_list[] = $a_drupal_site;
  1352. }
  1353. }
  1354. }
  1355. return $site_list;
  1356. }
  1357. function drush_sitealias_create_sites_alias($a_drupal_root = '') {
  1358. $sites_list = _drush_find_local_sites_at_root($a_drupal_root);
  1359. _drush_sitealias_cache_alias('@sites', array('site-list' => $sites_list));
  1360. }
  1361. /**
  1362. * Add "transient" default values to the given alias record. The
  1363. * difference between a static default and a transient default is
  1364. * that static defaults -always- exist in the alias record,
  1365. * whereas transient defaults are only added if the given drush
  1366. * command explicitly calls this function. The other advantage
  1367. * of transient defaults is that it is possible to differentiate
  1368. * between a default value and an unspecified value, since the
  1369. * transient defaults are not added until requested.
  1370. *
  1371. * Since transient defaults are not cached, you should avoid doing
  1372. * expensive operations here. To be safe, drush commands should
  1373. * avoid calling this function more than once.
  1374. *
  1375. * @param alias_record
  1376. * An alias record with most values already filled in
  1377. */
  1378. function _drush_sitealias_add_transient_defaults(&$alias_record) {
  1379. if (isset($alias_record['path-aliases'])) {
  1380. // Add the path to the drush folder to the path aliases as !drush
  1381. if (!array_key_exists('%drush', $alias_record['path-aliases'])) {
  1382. if (array_key_exists('%drush-script', $alias_record['path-aliases'])) {
  1383. $alias_record['path-aliases']['%drush'] = dirname($alias_record['path-aliases']['%drush-script']);
  1384. }
  1385. else {
  1386. $alias_record['path-aliases']['%drush'] = dirname(drush_find_drush());
  1387. }
  1388. }
  1389. // Add the path to the site folder to the path aliases as !site
  1390. if (!array_key_exists('%site', $alias_record['path-aliases']) && array_key_exists('uri', $alias_record)) {
  1391. $alias_record['path-aliases']['%site'] = 'sites/' . drush_sitealias_uri_to_site_dir($alias_record['uri'], drush_sitealias_get_root($alias_record)) . '/';
  1392. }
  1393. }
  1394. }
  1395. /**
  1396. * Find the name of a local alias record that has the specified
  1397. * root and uri.
  1398. */
  1399. function _drush_sitealias_find_local_alias_name($root, $uri) {
  1400. $result = '';
  1401. $all_site_aliases =& drush_get_context('site-aliases');
  1402. foreach ($all_site_aliases as $alias_name => $alias_values) {
  1403. if (!array_key_exists('remote-host', $alias_values) && array_key_exists('root', $alias_values) && array_key_exists('uri', $alias_values) && ($alias_name != '@self')) {
  1404. if (($root == $alias_values['root']) && ($uri == $alias_values['uri'])) {
  1405. $result = $alias_name;
  1406. }
  1407. }
  1408. }
  1409. return $result;
  1410. }
  1411. /**
  1412. * If '$alias' is the name of a folder in the sites folder of the given drupal
  1413. * root, then build an alias record for it
  1414. *
  1415. * @param alias
  1416. * The name of the site in the 'sites' folder to convert
  1417. * @return array
  1418. * An alias record, or empty if none found.
  1419. */
  1420. function _drush_sitealias_find_record_for_local_site($alias, $drupal_root = NULL) {
  1421. $alias_record = array();
  1422. // Clip off the leading '#' if it is there
  1423. if (substr($alias,0,1) == '#') {
  1424. $alias = substr($alias,1);
  1425. }
  1426. if (!isset($drupal_root)) {
  1427. $drupal_root = drush_get_context('DRUSH_SELECTED_DRUPAL_ROOT');
  1428. }
  1429. if (!empty($drupal_root)) {
  1430. $alias_dir = drush_sitealias_uri_to_site_dir($alias, $drupal_root);
  1431. $site_settings_file = $drupal_root . '/sites/' . $alias_dir . '/settings.php';
  1432. $alias_record = drush_sitealias_build_record_from_settings_file($site_settings_file, $alias, $drupal_root);
  1433. }
  1434. return $alias_record;
  1435. }
  1436. function drush_sitealias_build_record_from_settings_file($site_settings_file, $alias = null, $drupal_root = null) {
  1437. $alias_record = array();
  1438. if (file_exists($site_settings_file)) {
  1439. if (!isset($drupal_root)) {
  1440. $drupal_root = drush_locate_root($site_settings_file);
  1441. }
  1442. $alias_record['root'] = $drupal_root;
  1443. if (isset($alias)) {
  1444. $alias_record['uri'] = $alias;
  1445. }
  1446. else {
  1447. $alias_record['uri'] = _drush_sitealias_site_dir_to_uri(drush_sitealias_site_dir_from_filename($site_settings_file));
  1448. }
  1449. }
  1450. return $alias_record;
  1451. }
  1452. /**
  1453. * Pull the site directory from the path to settings.php
  1454. *
  1455. * @param site_settings_file
  1456. * path to settings.php
  1457. *
  1458. * @return string
  1459. * the site directory component of the path to settings.php
  1460. */
  1461. function drush_sitealias_site_dir_from_filename($site_settings_file) {
  1462. return basename(dirname($site_settings_file));
  1463. }
  1464. /**
  1465. * Convert from a URI to a site directory.
  1466. *
  1467. * @param uri
  1468. * A uri, such as http://domain.com:8080/drupal
  1469. * @return string
  1470. * A directory, such as domain.com.8080.drupal
  1471. */
  1472. function drush_sitealias_uri_to_site_dir($uri, $site_root = NULL) {
  1473. $uri = str_replace('http://', '', $uri);
  1474. if (drush_is_windows()) {
  1475. // Handle absolute paths on windows
  1476. $uri = str_replace(array(':/', ':\\'), array('.', '.'), $uri);
  1477. }
  1478. $hostname = str_replace(array('/', ':', '\\'), array('.', '.', '.'), $uri);
  1479. // Check sites.php mappings
  1480. $site_dir = drush_site_dir_lookup_from_hostname($hostname, $site_root);
  1481. return $site_dir ? $site_dir : $hostname;
  1482. }
  1483. /**
  1484. * Convert from an old-style database URL to an array of database settings.
  1485. *
  1486. * @param db_url
  1487. * A Drupal 6 db url string to convert, or an array with a 'default' element.
  1488. * @return array
  1489. * An array of database values containing only the 'default' element of
  1490. * the db url. If the parse fails the array is empty.
  1491. */
  1492. function drush_convert_db_from_db_url($db_url) {
  1493. $db_spec = array();
  1494. if (is_array($db_url)) {
  1495. $db_url_default = $db_url['default'];
  1496. }
  1497. else {
  1498. $db_url_default = $db_url;
  1499. }
  1500. // If it's a sqlite database, pick the database path and we're done.
  1501. if (strpos($db_url_default, 'sqlite://') === 0) {
  1502. $db_spec = array(
  1503. 'driver' => 'sqlite',
  1504. 'database' => substr($db_url_default, strlen('sqlite://')),
  1505. );
  1506. }
  1507. else {
  1508. $url = parse_url($db_url_default);
  1509. if ($url) {
  1510. // Fill in defaults to prevent notices.
  1511. $url += array(
  1512. 'scheme' => NULL,
  1513. 'user' => NULL,
  1514. 'pass' => NULL,
  1515. 'host' => NULL,
  1516. 'port' => NULL,
  1517. 'path' => NULL,
  1518. );
  1519. $url = (object)array_map('urldecode', $url);
  1520. $db_spec = array(
  1521. 'driver' => $url->scheme == 'mysqli' ? 'mysql' : $url->scheme,
  1522. 'username' => $url->user,
  1523. 'password' => $url->pass,
  1524. 'host' => $url->host,
  1525. 'port' => $url->port,
  1526. 'database' => ltrim($url->path, '/'),
  1527. );
  1528. }
  1529. }
  1530. return $db_spec;
  1531. }
  1532. /**
  1533. * Convert from an old-style database URL to an array of database settings
  1534. *
  1535. * @param db_url
  1536. * A Drupal 6 db-url string to convert, or an array with multiple db-urls.
  1537. * @return array
  1538. * An array of database values.
  1539. */
  1540. function drush_sitealias_convert_db_from_db_url($db_url) {
  1541. $result = array();
  1542. if (!is_array($db_url)) {
  1543. $result = array('default' => array('default' => drush_convert_db_from_db_url($db_url)));
  1544. }
  1545. else {
  1546. foreach ($db_url as $one_name => $one_db_url) {
  1547. $result[$one_name] = array('default' => drush_convert_db_from_db_url($one_db_url));
  1548. }
  1549. }
  1550. return $result;
  1551. }
  1552. /**
  1553. * Utility function used by drush_get_alias; keys that start with
  1554. * '%' or '!' are path aliases, the rest are entries in the alias record.
  1555. */
  1556. function _drush_sitealias_set_record_element(&$alias_record, $key, $value) {
  1557. if ((substr($key,0,1) == '%') || (substr($key,0,1) == '!')) {
  1558. $alias_record['path-aliases'][$key] = $value;
  1559. }
  1560. elseif (!empty($key)) {
  1561. $alias_record[$key] = $value;
  1562. }
  1563. }
  1564. /**
  1565. * Looks up the specified alias record and calls through to
  1566. * drush_sitealias_set_alias_context, below.
  1567. *
  1568. * @param alias
  1569. * The name of the alias record
  1570. * @param prefix
  1571. * The prefix value to afix to the beginning of every
  1572. * key set.
  1573. * @return boolean
  1574. * TRUE is an alias was found and processed.
  1575. */
  1576. function _drush_sitealias_set_context_by_name($alias, $prefix = '') {
  1577. if ($alias) {
  1578. $site_alias_settings = drush_sitealias_get_record($alias);
  1579. if (!empty($site_alias_settings)) {
  1580. drush_sitealias_set_alias_context($site_alias_settings, $prefix);
  1581. drush_sitealias_cache_alias_by_path($site_alias_settings);
  1582. if (empty($prefix)) {
  1583. // Create an alias '@self'
  1584. _drush_sitealias_cache_alias('@self', $site_alias_settings);
  1585. // change the selected site to match the new --root and --uri, if any were set
  1586. _drush_preflight_root_uri();
  1587. }
  1588. return $site_alias_settings;
  1589. }
  1590. }
  1591. return array();
  1592. }
  1593. /**
  1594. * Given an alias record, overwrite its values with options
  1595. * from the command line and other drush contexts as specified
  1596. * by the provided prefix. For example, if the prefix is 'source-',
  1597. * then any option 'source-foo' will set the value 'foo' in the
  1598. * alias record.
  1599. */
  1600. function drush_sitealias_overlay_options($site_alias_record, $prefix) {
  1601. return array_merge($site_alias_record, drush_get_merged_prefixed_options($prefix));
  1602. }
  1603. /**
  1604. * First return an option set via drush_sitealias_overlay_options, if
  1605. * any, then fall back on "%" . $option from the path aliases.
  1606. */
  1607. function drush_sitealias_get_path_option($site_alias_record, $option, $default = NULL) {
  1608. if (isset($site_alias_record) && array_key_exists($option, $site_alias_record)) {
  1609. return $site_alias_record[$option];
  1610. }
  1611. if (isset($site_alias_record) && array_key_exists('path-aliases', $site_alias_record) && array_key_exists("%$option", $site_alias_record['path-aliases'])) {
  1612. return $site_alias_record['path-aliases']["%$option"];
  1613. }
  1614. else {
  1615. return drush_get_option($option, $default);
  1616. }
  1617. }
  1618. /**
  1619. * Given a site alias record, copy selected fields from it
  1620. * into the drush 'alias' context. The 'alias' context has
  1621. * lower precedence than the 'cli' context, so values
  1622. * set by an alias record can be overridden by command-line
  1623. * parameters.
  1624. *
  1625. * @param site_alias_settings
  1626. * An alias record
  1627. * @param prefix
  1628. * The prefix value to affix to the beginning of every
  1629. * key set. For example, if this function is called once with
  1630. * 'source-' and again with 'destination-' prefixes, then the
  1631. * source database records will be stored in 'source-databases',
  1632. * and the destination database records will be in
  1633. * 'destination-databases'.
  1634. */
  1635. function drush_sitealias_set_alias_context($site_alias_settings, $prefix = '') {
  1636. $options = drush_get_context('alias');
  1637. // There are some items that we should just skip
  1638. $skip_list = drush_get_special_keys();
  1639. // If 'php-options' are set in the alias, then we will force drush
  1640. // to redispatch via the remote dispatch mechanism even if the target is localhost.
  1641. if ((array_key_exists('php-options', $site_alias_settings) || array_key_exists('php', $site_alias_settings)) && !drush_get_context('DRUSH_BACKEND', FALSE)) {
  1642. if (!array_key_exists('remote-host', $site_alias_settings)) {
  1643. $site_alias_settings['remote-host'] = 'localhost';
  1644. }
  1645. }
  1646. // If 'php-options' are not set in the alias, then skip 'remote-host'
  1647. // and 'remote-user' if 'remote-host' is actually the local machine.
  1648. // This prevents drush from using the remote dispatch mechanism (the command
  1649. // is just run directly on the local machine, bootstrapping to the specified alias)
  1650. elseif (array_key_exists('remote-host', $site_alias_settings) && drush_is_local_host($site_alias_settings['remote-host'])) {
  1651. $skip_list[] = 'remote-host';
  1652. $skip_list[] = 'remote-user';
  1653. }
  1654. // If prefix is set, then copy from the 'prefix-' version
  1655. // of the drush special keys ('command-specific', 'path-aliases')
  1656. // into the ordinary version. This will allow us to set
  1657. // 'source-command-specific' options that will only apply when
  1658. // the alias is used as the source option for rsync or sql-sync.
  1659. if (!empty($prefix)) {
  1660. $special_contexts = drush_get_special_keys();
  1661. foreach ($special_contexts as $option_name) {
  1662. if (array_key_exists($prefix . $option_name, $site_alias_settings)) {
  1663. $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];
  1664. }
  1665. }
  1666. }
  1667. // Transfer all options from the site alias to the drush options
  1668. // in the 'alias' context.
  1669. foreach ($site_alias_settings as $key => $value) {
  1670. // Special handling for path aliases:
  1671. if ($key == "path-aliases") {
  1672. $path_aliases = $value;
  1673. foreach (array('%drush-script', '%dump', '%dump-dir', '%include') as $path_key) {
  1674. if (array_key_exists($path_key, $path_aliases)) {
  1675. // Evaluate the path value, and substitute any path references found.
  1676. // ex: '%dump-dir' => '%root/dumps' will store sql-dumps in the folder
  1677. // 'dumps' in the Drupal root folder for the site.
  1678. $evaluated_path = str_replace(array_keys($path_aliases), array_values($path_aliases), $path_aliases[$path_key]);
  1679. $options[$prefix . substr($path_key, 1)] = $evaluated_path;
  1680. }
  1681. }
  1682. }
  1683. // Special handling for command-specific
  1684. elseif ($key == "command-specific") {
  1685. $options[$key] = $value;
  1686. }
  1687. elseif (!in_array($key, $skip_list)) {
  1688. $options[$prefix . $key] = $value;
  1689. }
  1690. }
  1691. drush_set_config_options('alias', $options);
  1692. }
  1693. /**
  1694. * Call prior to drush_sitealias_evaluate_path to insure
  1695. * that any site-specific aliases associated with any
  1696. * local site in $path are defined.
  1697. */
  1698. function _drush_sitealias_preflight_path($path) {
  1699. $alias = NULL;
  1700. // Parse site aliases if there is a colon in the path
  1701. // We allow:
  1702. // @alias:/path
  1703. // machine.domain.com:/path
  1704. // machine:/path
  1705. // Note that paths in the form "c:/path" are converted to
  1706. // "/cygdrive/c/path" later; we do not want them to confuse
  1707. // us here, so we skip paths that start with a single character
  1708. // before the colon if we are running on Windows. Single-character
  1709. // machine names are allowed in Linux only.
  1710. $colon_pos = strpos($path, ':');
  1711. if ($colon_pos > (drush_is_windows("LOCAL") ? 1 : 0)) {
  1712. $alias = substr($path, 0, $colon_pos);
  1713. $path = substr($path, $colon_pos + 1);
  1714. $site_alias_settings = drush_sitealias_get_record($alias);
  1715. if (empty($site_alias_settings) && (substr($path,0,1) == '@')) {
  1716. return NULL;
  1717. }
  1718. $machine = $alias;
  1719. }
  1720. else {
  1721. $machine = '';
  1722. // if the path is a site alias or a local site...
  1723. $site_alias_settings = drush_sitealias_get_record($path);
  1724. if (empty($site_alias_settings) && (substr($path,0,1) == '@')) {
  1725. return NULL;
  1726. }
  1727. if (!empty($site_alias_settings) || drush_is_local_host($path)) {
  1728. $alias = $path;
  1729. $path = '';
  1730. }
  1731. }
  1732. return array('alias' => $alias, 'path' => $path, 'machine' => $machine);
  1733. }
  1734. /**
  1735. * Given a properly-escaped options string, replace any occurance of
  1736. * %files and so on embedded inside it with its corresponding path.
  1737. */
  1738. function drush_sitealias_evaluate_paths_in_options($option_string) {
  1739. $path_aliases = _core_path_aliases();
  1740. return str_replace(array_keys($path_aliases), array_values($path_aliases), $option_string);
  1741. }
  1742. /**
  1743. * Evaluate a path from its shorthand form to a literal path
  1744. * usable by rsync.
  1745. *
  1746. * A path is "machine:/path" or "machine:path" or "/path" or "path".
  1747. * 'machine' might instead be an alias record, or the name
  1748. * of a site in the 'sites' folder. 'path' might be (or contain)
  1749. * '%root' or some other path alias. This function will examine
  1750. * all components of the path and evaluate them as necessary to
  1751. * come to the final path.
  1752. *
  1753. * @param path
  1754. * The path to evaluate
  1755. * @param additional_options
  1756. * An array of options that overrides whatever was passed in on
  1757. * the command line (like the 'process' context, but only for
  1758. * the scope of this one call).
  1759. * @param local_only
  1760. * If TRUE, force an error if the provided path points to a remote
  1761. * machine.
  1762. * @param os
  1763. * This should be the local system os, unless evaluate path is
  1764. * being called for rsync, in which case it should be "CWRSYNC"
  1765. * if cwrsync is being used, or "rsync" to automatically select
  1766. * between "LOCAL" and "CWRSYNC" based on the platform.
  1767. * @return
  1768. * The site record for the machine specified in the path, if any,
  1769. * with the path to pass to rsync (including the machine specifier)
  1770. * in the 'evaluated-path' item.
  1771. */
  1772. function drush_sitealias_evaluate_path($path, &$additional_options, $local_only = FALSE, $os = NULL, $command_specific_prefix = '') {
  1773. $site_alias_settings = array();
  1774. $path_aliases = array();
  1775. $remote_user = '';
  1776. $preflight = _drush_sitealias_preflight_path($path);
  1777. if (!isset($preflight)) {
  1778. return NULL;
  1779. }
  1780. $alias = $preflight['alias'];
  1781. $path = $preflight['path'];
  1782. $machine = $preflight['machine'];
  1783. if (isset($alias)) {
  1784. // Note that the alias settings may have an 'os' component, but we do
  1785. // not want to use it here. The paths passed to rsync should always be
  1786. // escaped per the LOCAL rules, without regard to the remote platform type.
  1787. $site_alias_settings = drush_sitealias_get_record($alias);
  1788. if (!empty($command_specific_prefix)) {
  1789. drush_command_set_command_specific_options($command_specific_prefix);
  1790. drush_sitealias_command_default_options($site_alias_settings, $command_specific_prefix);
  1791. }
  1792. }
  1793. if (!empty($site_alias_settings)) {
  1794. if ($local_only && array_key_exists('remote-host', $site_alias_settings)) {
  1795. 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."));
  1796. }
  1797. // Apply any options from this alias that might affect our rsync
  1798. drush_sitealias_set_alias_context($site_alias_settings);
  1799. // Use 'remote-host' from settings if available; otherwise site is local
  1800. if (array_key_exists('remote-host', $site_alias_settings) && !drush_is_local_host($site_alias_settings['remote-host'])) {
  1801. $machine = drush_remote_host($site_alias_settings);
  1802. }
  1803. else {
  1804. $machine = '';
  1805. }
  1806. }
  1807. else {
  1808. // Strip the machine portion of the path if the
  1809. // alias points to the local machine.
  1810. if (drush_is_local_host($machine)) {
  1811. $machine = '';
  1812. }
  1813. else {
  1814. $machine = "$remote_user$machine";
  1815. }
  1816. }
  1817. // TOD: The code below is a little rube-goldberg-ish, and needs to be
  1818. // reworked. core-rsync will call this function twice: once to
  1819. // evaluate the destination, and then again to evaluate the source. Things
  1820. // get odd with --exclude-paths, especially in conjunction with command-specific
  1821. // and the --exclude-files option. @see testCommandSpecific()
  1822. // If the --exclude-other-sites option is specified, then
  1823. // convert that into --include-paths='%site' and --exclude-sites.
  1824. if (drush_get_option_override($additional_options, 'exclude-other-sites', FALSE) && !drush_get_context('exclude-other-sites-processed', FALSE)) {
  1825. $include_path_option = drush_get_option_override($additional_options, 'include-paths', '');
  1826. $additional_options['include-paths'] = '%site';
  1827. if (!empty($include_path_option)) {
  1828. // We use PATH_SEPARATOR here because we are later going to explicitly explode() this variable using PATH_SEPARATOR.
  1829. $additional_options['include-paths'] .= PATH_SEPARATOR . $include_path_option;
  1830. }
  1831. $additional_options['exclude-sites'] = TRUE;
  1832. drush_set_context('exclude-other-sites-processed', TRUE);
  1833. }
  1834. else {
  1835. unset($additional_options['include-paths']);
  1836. }
  1837. // If the --exclude-files option is specified, then
  1838. // convert that into --exclude-paths='%files'.
  1839. if (drush_get_option_override($additional_options, 'exclude-files', FALSE) && !drush_get_option_override($additional_options, 'exclude-files-processed', FALSE, 'process')) {
  1840. $exclude_path_option = drush_get_option_override($additional_options, 'exclude-paths', '');
  1841. $additional_options['exclude-paths'] = '%files';
  1842. if (!empty($exclude_path_option)) {
  1843. // We use PATH_SEPARATOR here because we are later going to explicitly explode() this variable using PATH_SEPARATOR.
  1844. $additional_options['exclude-paths'] .= PATH_SEPARATOR . $exclude_path_option;
  1845. }
  1846. $additional_options['exclude-files-processed'] = TRUE;
  1847. }
  1848. else {
  1849. unset($additional_options['exclude-paths']);
  1850. }
  1851. // If there was no site specification given, and the
  1852. // machine is local, then try to look
  1853. // up an alias record for the default drush site.
  1854. if (empty($site_alias_settings) && empty($machine)) {
  1855. $drush_uri = drush_get_context('DRUSH_SELECTED_URI', 'default');
  1856. $site_alias_settings = drush_sitealias_get_record($drush_uri);
  1857. }
  1858. // Always add transient defaults
  1859. _drush_sitealias_add_transient_defaults($site_alias_settings);
  1860. // The $resolve_path variable is used by drush_sitealias_resolve_path_references
  1861. // to test to see if there are any path references such as %site or %files
  1862. // in it, so that resolution is only done if the path alias is referenced.
  1863. // Therefore, we can concatenate without worrying too much about the structure of
  1864. // this variable's contents.
  1865. $include_path = drush_get_option_override($additional_options, 'include-paths', '');
  1866. $exclude_path = drush_get_option_override($additional_options, 'exclude-paths', '');
  1867. if (is_array($include_path)) {
  1868. $include_path = implode('/', $include_path);
  1869. }
  1870. if (is_array($exclude_path)) {
  1871. $include_path = implode('/', $exclude_path);
  1872. }
  1873. $resolve_path = "$path/$include_path/$exclude_path";
  1874. // Resolve path aliases such as %files, if any exist in the path
  1875. if (!empty($resolve_path)) {
  1876. drush_sitealias_resolve_path_references($site_alias_settings, $resolve_path);
  1877. }
  1878. if (array_key_exists('path-aliases', $site_alias_settings)) {
  1879. $path_aliases = $site_alias_settings['path-aliases'];
  1880. }
  1881. // Get the 'root' setting from the alias; if it does not
  1882. // exist, then get the root from the bootstrapped site.
  1883. if (array_key_exists('root', $site_alias_settings)) {
  1884. $drupal_root = $site_alias_settings['root'];
  1885. }
  1886. else {
  1887. drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  1888. $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
  1889. }
  1890. if (empty($drupal_root)) {
  1891. $drupal_root = '';
  1892. }
  1893. else {
  1894. // Add a slash to the end of the drupal root, as below.
  1895. $drupal_root = drush_trim_path($drupal_root) . "/";
  1896. }
  1897. $full_path_aliases = $path_aliases;
  1898. foreach ($full_path_aliases as $key => $value) {
  1899. // Expand all relative path aliases to be based off of the Drupal root
  1900. if (!drush_is_absolute_path($value, "LOCAL") && ($key != '%root')) {
  1901. $full_path_aliases[$key] = $drupal_root . $value;
  1902. }
  1903. // We do not want slashes on the end of our path aliases.
  1904. $full_path_aliases[$key] = drush_trim_path($full_path_aliases[$key]);
  1905. }
  1906. // Fill in path aliases in the path, the include path and the exclude path.
  1907. $path = str_replace(array_keys($full_path_aliases), array_values($full_path_aliases), $path);
  1908. if (!empty($include_path)) {
  1909. drush_set_option('include-paths', str_replace(array_keys($path_aliases), array_values($path_aliases), $include_path));
  1910. }
  1911. if (!empty($exclude_path)) {
  1912. drush_set_option('exclude-paths', str_replace(array_keys($path_aliases), array_values($path_aliases), $exclude_path));
  1913. }
  1914. // Next make the rsync path, which includes the machine
  1915. // and path components together.
  1916. // First make empty paths or relative paths start from the drupal root.
  1917. if (empty($path) || (!drush_is_absolute_path($path, "LOCAL"))) {
  1918. $path = $drupal_root . $path;
  1919. }
  1920. // When calculating a path for use with rsync, we must correct
  1921. // absolute paths in the form c:\path when cwrsync is in use.
  1922. $path = drush_correct_absolute_path_for_exec($path, $os);
  1923. // If there is a $machine component, to the path, then
  1924. // add it to the beginning
  1925. $evaluated_path = drush_escapeshellarg($path, $os);
  1926. if (!empty($machine)) {
  1927. $evaluated_path = $machine . ':' . $evaluated_path;
  1928. }
  1929. //
  1930. // Add our result paths:
  1931. //
  1932. // evaluated-path: machine:/path
  1933. // server-component: machine
  1934. // path-component: :/path
  1935. // path: /path
  1936. // user-path: path (as specified in input parameter)
  1937. //
  1938. $site_alias_settings['evaluated-path'] = $evaluated_path;
  1939. if (!empty($machine)) {
  1940. $site_alias_settings['server-component'] = $machine;
  1941. }
  1942. $site_alias_settings['path-component'] = (!empty($path) ? ':' . $path : '');
  1943. $site_alias_settings['path'] = $path;
  1944. $site_alias_settings['user-path'] = $preflight['path'];
  1945. return $site_alias_settings;
  1946. }
  1947. /**
  1948. * Option keys used for site selection.
  1949. */
  1950. function drush_sitealias_site_selection_keys() {
  1951. return array('remote-host', 'remote-user', 'winrs-password', 'ssh-options', '#name', 'os');
  1952. }
  1953. function sitealias_find_local_drupal_root($site_list) {
  1954. $drupal_root = NULL;
  1955. foreach ($site_list as $site) {
  1956. if (($drupal_root == NULL) && (array_key_exists('root', $site) && !array_key_exists('remote-host', $site))) {
  1957. $drupal_root = $site['root'];
  1958. }
  1959. }
  1960. return $drupal_root;
  1961. }
  1962. /**
  1963. * Helper function to obtain the keys' names that need special handling in certain
  1964. * cases.
  1965. * @return
  1966. * A non-associative array containing the needed keys' names.
  1967. */
  1968. function drush_get_special_keys() {
  1969. $special_keys = array(
  1970. 'command-specific',
  1971. 'site-aliases',
  1972. );
  1973. return $special_keys;
  1974. }
  1975. /**
  1976. * Read the tmp file where the persistent site setting is stored.
  1977. *
  1978. * @return string
  1979. * A valid site specification.
  1980. */
  1981. function drush_sitealias_site_get() {
  1982. if (($filename = drush_sitealias_get_envar_filename()) && file_exists($filename)) {
  1983. $site = file_get_contents($filename);
  1984. return $site;
  1985. }
  1986. else {
  1987. return FALSE;
  1988. }
  1989. }
  1990. /**
  1991. * Returns the filename for the file that stores the DRUPAL_SITE variable.
  1992. *
  1993. * @param string $filename_prefix
  1994. * An arbitrary string to prefix the filename with.
  1995. *
  1996. * @return string|false
  1997. * Returns the full path to temp file if possible, or FALSE if not.
  1998. */
  1999. function drush_sitealias_get_envar_filename($filename_prefix = 'drush-drupal-site-') {
  2000. $shell_pid = getenv('DRUSH_SHELL_PID');
  2001. if (!$shell_pid && function_exists('posix_getppid')) {
  2002. $shell_pid = posix_getppid();
  2003. }
  2004. if (!$shell_pid) {
  2005. return FALSE;
  2006. }
  2007. $tmp = getenv('TMPDIR') ? getenv('TMPDIR') : '/tmp';
  2008. $username = drush_get_username();
  2009. return "{$tmp}/drush-env-{$username}/{$filename_prefix}" . $shell_pid;
  2010. }
  2011. /**
  2012. * Cache the specified alias in the alias path cache. The
  2013. * alias path cache creates a lookup from the site folder
  2014. * (/path/to/drupal/sites/default) to the provided alias record.
  2015. *
  2016. * Only the name of the alias and the path to the file it
  2017. * is stored in is cached; when it is retrieved, it is
  2018. * loaded directly from the correct file.
  2019. */
  2020. function drush_sitealias_cache_alias_by_path($alias_record) {
  2021. if (!isset($alias_record['remote-host']) && isset($alias_record['root']) && isset($alias_record['uri']) && isset($alias_record['#name']) && isset($alias_record['#file'])) {
  2022. $path = drush_sitealias_local_site_path($alias_record);
  2023. if ($path) {
  2024. $cid = drush_get_cid('alias-path-', array(), array($path));
  2025. $alias_path_data = array(
  2026. '#name' => $alias_record['#name'],
  2027. '#file' => $alias_record['#file'],
  2028. );
  2029. drush_cache_set($cid, $alias_path_data);
  2030. }
  2031. }
  2032. }
  2033. /**
  2034. * Look for a defined alias that points to the specified
  2035. * site directory. The cache is tested first; if nothing
  2036. * is cached, then an exhaustive search is done for the
  2037. * specified site. If the exhaustive search returns a
  2038. * match, then it is cached.
  2039. *
  2040. * @param $path
  2041. * /path/to/drupal/sites/default
  2042. * @return
  2043. * An alias record for the provided path
  2044. */
  2045. function drush_sitealias_lookup_alias_by_path($path, $allow_best_match=FALSE) {
  2046. $result = drush_sitealias_quick_lookup_cached_alias_by_path($path);
  2047. $fallback = array();
  2048. if (empty($result)) {
  2049. $aliases = _drush_sitealias_find_and_load_all_aliases();
  2050. foreach ($aliases as $name => $alias_record) {
  2051. if (!isset($alias_record['remote-host']) && isset($alias_record['root']) && isset($alias_record['uri']) && isset($alias_record['#name']) && isset($alias_record['#file'])) {
  2052. if ($path == drush_sitealias_local_site_path($alias_record)) {
  2053. $result = $alias_record;
  2054. break;
  2055. }
  2056. if (substr($path, 0, strlen($alias_record['root'])) == $alias_record['root']) {
  2057. $fallback = $alias_record;
  2058. }
  2059. }
  2060. }
  2061. }
  2062. if (empty($result) && $allow_best_match) {
  2063. $result = $fallback;
  2064. }
  2065. if (!empty($result)) {
  2066. _drush_sitealias_add_inherited_values_to_record($result);
  2067. drush_sitealias_cache_alias_by_path($result);
  2068. }
  2069. return $result;
  2070. }
  2071. /**
  2072. * Look for a cached alias that points to the specified
  2073. * site directory. Nothing is returned if there is no
  2074. * matching cached alias.
  2075. *
  2076. * @param $path
  2077. * /path/to/drupal/sites/default
  2078. * @return
  2079. * An alias record for the provided path
  2080. */
  2081. function drush_sitealias_quick_lookup_cached_alias_by_path($path) {
  2082. $alias_record = array();
  2083. $cid = drush_get_cid('alias-path-', array(), array($path));
  2084. $alias_path_cache = drush_cache_get($cid);
  2085. if (isset($alias_path_cache->data)) {
  2086. $alias_name = $alias_path_cache->data['#name'];
  2087. $alias_file = $alias_path_cache->data['#file'];
  2088. $alias_record = _drush_sitealias_find_and_load_alias_from_file($alias_name, array($alias_file));
  2089. _drush_sitealias_add_inherited_values_to_record($alias_record);
  2090. $alias_record['#name'] = $alias_name;
  2091. }
  2092. return $alias_record;
  2093. }
  2094. /**
  2095. * Return the site root, if there is one in the record.
  2096. */
  2097. function drush_sitealias_get_root($alias_record) {
  2098. return array_key_exists('root', $alias_record) ? $alias_record['root'] : NULL;
  2099. }
  2100. /**
  2101. * Decide on which side to run a core-rsync.
  2102. *
  2103. * @param $source
  2104. * @param $destination
  2105. * @return mixed
  2106. */
  2107. function drush_get_runner($source, $destination) {
  2108. // Try run to rsync locally so that aliases always resolve. https://github.com/drush-ops/drush/issues/668
  2109. if (drush_sitealias_is_remote_site($source) === FALSE) {
  2110. $runner = $source;
  2111. return $runner;
  2112. }
  2113. elseif (drush_sitealias_is_remote_site($destination) === FALSE) {
  2114. $runner = $destination;
  2115. return $runner;
  2116. }
  2117. else {
  2118. // Both are remote. Arbitrarily run rsync on destination. Aliases must be defined there (for now).
  2119. // @todo Add an option for choosing runner? Resolve aliases before rsync?
  2120. $runner = $destination;
  2121. return $runner;
  2122. }
  2123. }