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