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_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_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_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_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_selection_keys Option keys used for site selection.
drush_sitealias_uri_to_site_dir Convert from a URI to a site directory.
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_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_and_load_alias 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

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