sitealias.drush.inc

  1. 8.0.x commands/core/sitealias.drush.inc
  2. 6.x commands/core/sitealias.drush.inc
  3. 7.x commands/core/sitealias.drush.inc
  4. 3.x commands/core/sitealias.drush.inc
  5. 4.x commands/core/sitealias.drush.inc
  6. 5.x commands/core/sitealias.drush.inc
  7. master commands/core/sitealias.drush.inc

Site alias commands.

See also

example.drushrc.php for details.

Functions

Namesort descending Description
drush_sitealias_print Print out the specified site aliases using the format specified.
sitealias_drush_command
sitealias_drush_help
_drush_sitealias_alias_list Return a list of all site aliases known to drush.
_drush_sitealias_all_list Return the list of all site aliases and all local sites.
_drush_sitealias_print_record Given a site alias name, print out a php-syntax representation of it.
_drush_sitealias_site_dir_to_uri Use heuristics to attempt to convert from a site directory to a URI. This function should only be used when the URI really is unknown, as the mapping is not perfect.
_drush_sitealias_site_list Return a list of all of the local sites at the current drupal root.
_drush_sitealias_user_specified_list Return the list of sites (aliases or local) that the user specified on the command line. If none were specified, then all are returned.

File

commands/core/sitealias.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Site alias commands. @see example.drushrc.php for details.
  5. */
  6. function sitealias_drush_help($section) {
  7. switch ($section) {
  8. case 'drush:site-alias':
  9. return dt('Print an alias record.');
  10. }
  11. }
  12. function sitealias_drush_command() {
  13. $items = array();
  14. $items['site-alias'] = array(
  15. 'callback' => 'drush_sitealias_print',
  16. 'description' => 'Print site alias records for all known site aliases and local sites.',
  17. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  18. 'arguments' => array(
  19. 'site' => 'Site specification alias to print',
  20. ),
  21. 'options' => array(
  22. '--full' => 'Print the full alias record for each site. Default when aliases are specified on the command line.',
  23. '--short' => 'Print only the site alias name. Default when not command line arguments are specified.',
  24. '--pipe' => 'Print the long-form site specification for each site.',
  25. '--with-db' => 'Include the databases structure in the full alias record.',
  26. '--with-db-url' => 'Include the short-form db-url in the full alias record.',
  27. '--no-db' => 'Do not include the database record in the full alias record (default).',
  28. '--with-optional' => 'Include optional default items.',
  29. ),
  30. 'aliases' => array('sa'),
  31. 'examples' => array(
  32. 'drush site-alias' => 'List all alias records known to drush.',
  33. 'drush site-alias @dev' => 'Print an alias record for the alias \'dev\'.',
  34. ),
  35. );
  36. return $items;
  37. }
  38. /**
  39. * Return a list of all site aliases known to drush.
  40. *
  41. * The array key is the site alias name, and the array value
  42. * is the site specification for the given alias.
  43. */
  44. function _drush_sitealias_alias_list() {
  45. return drush_get_context('site-aliases');
  46. }
  47. /**
  48. * Return a list of all of the local sites at the current drupal root.
  49. *
  50. * The array key is the site folder name, and the array value
  51. * is the site specification for that site.
  52. */
  53. function _drush_sitealias_site_list() {
  54. $site_list = array();
  55. $base_path = drush_get_context('DRUSH_DRUPAL_ROOT') . '/sites';
  56. $files = drush_scan_directory($base_path, '/settings\.php/', array('.', '..', 'CVS', 'all'));
  57. foreach ($files as $filename => $info) {
  58. if ($info->basename == 'settings.php') {
  59. $alias_record = drush_sitealias_build_record_from_settings_file($filename);
  60. if (!empty($alias_record)) {
  61. $site_list[drush_sitealias_uri_to_site_dir($alias_record['uri'])] = $alias_record;
  62. }
  63. }
  64. }
  65. return $site_list;
  66. }
  67. /**
  68. * Return the list of all site aliases and all local sites.
  69. */
  70. function _drush_sitealias_all_list() {
  71. drush_sitealias_load_all();
  72. return array_merge(_drush_sitealias_alias_list(), _drush_sitealias_site_list());
  73. }
  74. /**
  75. * Return the list of sites (aliases or local) that the
  76. * user specified on the command line. If none were specified,
  77. * then all are returned.
  78. */
  79. function _drush_sitealias_user_specified_list() {
  80. $command = drush_get_command();
  81. $specifications = $command['arguments'];
  82. $site_list = array();
  83. // Did the user specify --short or --full output?
  84. $specified_output_style = drush_get_option(array('full', 'short'), FALSE);
  85. // Iterate over the arguments and convert them to alias records
  86. if (!empty($specifications)) {
  87. $site_list = drush_sitealias_resolve_sitespecs($specifications);
  88. if (!$specified_output_style) {
  89. drush_set_option('full', TRUE);
  90. }
  91. }
  92. // If the user provided no args, then we will return everything.
  93. else {
  94. $site_list = _drush_sitealias_all_list();
  95. }
  96. return $site_list;
  97. }
  98. /**
  99. * Print out the specified site aliases using the format
  100. * specified.
  101. */
  102. function drush_sitealias_print() {
  103. drush_bootstrap_max();
  104. $site_list = _drush_sitealias_user_specified_list();
  105. $full_output = drush_get_option('full');
  106. $long_output = drush_get_option('long');
  107. $with_db = (drush_get_option('with-db') != null) || (drush_get_option('with-db-url') != null);
  108. $site_specs = array();
  109. foreach ($site_list as $site => $alias_record) {
  110. if (!array_key_exists('site-list', $alias_record)) {
  111. $site_specs[] = drush_sitealias_alias_record_to_spec($alias_record, $with_db);
  112. }
  113. if (isset($full_output)) {
  114. _drush_sitealias_print_record($alias_record, $site);
  115. }
  116. else {
  117. drush_print($site);
  118. }
  119. }
  120. drush_print_pipe(array_unique($site_specs));
  121. }
  122. /**
  123. * Given a site alias name, print out a php-syntax
  124. * representation of it.
  125. *
  126. * @param alias_record
  127. * The name of the site alias to print
  128. */
  129. function _drush_sitealias_print_record($alias_record, $site_alias = '') {
  130. $output_db = drush_get_option('with-db');
  131. $output_db_url = drush_get_option('with-db-url');
  132. $output_optional_items = drush_get_option('with-optional');
  133. // Make sure that the default items have been added for all aliases
  134. _drush_sitealias_add_static_defaults($alias_record);
  135. // Include the optional items, if requested
  136. if ($output_optional_items) {
  137. _drush_sitealias_add_transient_defaults($alias_record);
  138. }
  139. drush_sitealias_resolve_path_references($alias_record);
  140. if (isset($output_db_url)) {
  141. drush_sitealias_add_db_url($alias_record);
  142. }
  143. if (isset($output_db_url) || isset($output_db)) {
  144. drush_sitealias_add_db_settings($alias_record);
  145. }
  146. // If the user specified --with-db-url, then leave the
  147. // 'db-url' entry in the alias record (unless it is not
  148. // set, in which case we will leave the 'databases' record instead).
  149. if (isset($output_db_url)) {
  150. if (isset($alias_record['db-url'])) {
  151. unset($alias_record['databases']);
  152. }
  153. }
  154. // If the user specified --with-db, then leave the
  155. // 'databases' entry in the alias record.
  156. else if (isset($output_db)) {
  157. unset($alias_record['db-url']);
  158. }
  159. // If neither --with-db nor --with-db-url were specified,
  160. // then remove both the 'db-url' and the 'databases' entries.
  161. else {
  162. unset($alias_record['db-url']);
  163. unset($alias_record['databases']);
  164. }
  165. // The alias name will be the same as the site alias name,
  166. // unless the user specified some other name on the command line.
  167. $alias_name = drush_get_option('alias-name');
  168. if (!isset($alias_name)) {
  169. $alias_name = $site_alias;
  170. if (empty($alias_name) || is_numeric($alias_name)) {
  171. $alias_name = drush_sitealias_uri_to_site_dir($alias_record['uri']);
  172. }
  173. }
  174. // We don't want the name to go into the output
  175. unset($alias_record['name']);
  176. // We only want to output the 'root' item; don't output the '%root' path alias
  177. if (array_key_exists('path-aliases', $alias_record) && array_key_exists('%root', $alias_record['path-aliases'])) {
  178. unset($alias_record['path-aliases']['%root']);
  179. // If there is nothing left in path-aliases, then clear it out
  180. if (count($alias_record['path-aliases']) == 0) {
  181. unset($alias_record['path-aliases']);
  182. }
  183. }
  184. // Alias names contain an '@' when referenced, but do
  185. // not contain an '@' when defined.
  186. if (substr($alias_name,0,1) == '@') {
  187. $alias_name = substr($alias_name,1);
  188. }
  189. if (!drush_get_option('show-passwords', FALSE)) {
  190. drush_unset_recursive($alias_record, 'password');
  191. }
  192. $exported_alias = var_export($alias_record, TRUE);
  193. drush_print('$aliases[\'' . $alias_name . '\'] = ' . $exported_alias . ';');
  194. }
  195. /**
  196. * Use heuristics to attempt to convert from a site directory to a URI.
  197. * This function should only be used when the URI really is unknown, as
  198. * the mapping is not perfect.
  199. *
  200. * @param site_dir
  201. * A directory, such as domain.com.8080.drupal
  202. *
  203. * @return string
  204. * A uri, such as http://domain.com:8080/drupal
  205. */
  206. function _drush_sitealias_site_dir_to_uri($site_dir) {
  207. // Protect IP addresses NN.NN.NN.NN by converting them
  208. // temporarily to NN_NN_NN_NN for now.
  209. $uri = preg_replace("/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/", "$1_$2_$3_$4", $site_dir);
  210. // Convert .[0-9]+. into :[0-9]+/
  211. $uri = preg_replace("/\.([0-9]+)\./", ":$1/", $uri);
  212. // Convert .[0-9]$ into :[0-9]
  213. $uri = preg_replace("/\.([0-9]+)$/", ":$1", $uri);
  214. // Convert .(com|net|org|info). into .(com|net|org|info)/
  215. $uri = str_replace(array('.com.', '.net.', '.org.', '.info.'), array('.com/', '.net/', '.org/', '.info/'), $uri);
  216. // If there is a / then convert every . after the / to /
  217. // Then again, if we did this we would break if the path contained a "."
  218. // I hope that the path would never contain a "."...
  219. $pos = strpos($uri, '/');
  220. if ($pos !== false) {
  221. $uri = substr($uri, 0, $pos + 1) . str_replace('.', '/', substr($uri, $pos + 1));
  222. }
  223. // n.b. this heuristic works all the time if there is a port,
  224. // it also works all the time if there is a port and no path,
  225. // but it does not work for domains such as .co.jp with no path,
  226. // and it can fail horribly if someone makes a domain like "info.org".
  227. // Still, I think this is the best we can do short of consulting DNS.
  228. // Convert from NN_NN_NN_NN back to NN.NN.NN.NN
  229. $uri = preg_replace("/([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)/", "$1.$2.$3.$4", $site_dir);
  230. return 'http://' . $uri;
  231. }