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