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