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 (or else all) using the format specified.
drush_sitealias_site_set Set the DRUPAL_SITE variable by writing it out to a temporary file that we then source for persistent site switching.
drush_sitealias_site_set_validate Validation callback for drush site-set.
sitealias_drush_command
sitealias_drush_help
sitealias_site_alias_complete Command argument complete callback.
sitealias_site_set_complete Command argument complete callback.
_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_prepare_record Given a site alias name, print out a php-syntax representation of it.
_drush_sitealias_print_record
_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 site aliases (remote 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_NONE,
  18. 'arguments' => array(
  19. 'site' => 'Site specification to print',
  20. ),
  21. 'options' => array(
  22. 'with-db' => 'Include the databases structure in the full alias record.',
  23. 'with-db-url' => 'Include the short-form db-url in the full alias record.',
  24. 'no-db' => 'Do not include the database record in the full alias record (default).',
  25. 'with-optional' => 'Include optional default items.',
  26. 'alias-name' => 'For a single alias, set the name to use in the output.',
  27. 'local-only' => 'Only display sites that are available on the local system (remote-site not set, and Drupal root exists).',
  28. 'show-hidden' => 'Include hidden internal elements in site alias output',
  29. ),
  30. 'outputformat' => array(
  31. 'default' => 'config',
  32. 'pipe-format' => 'var_export',
  33. 'variable-name' => 'aliases',
  34. 'hide-empty-fields' => TRUE,
  35. 'private-fields' => 'password',
  36. 'field-labels' => array('#name' => 'Name', 'root' => 'Root', 'uri' => 'URI', 'remote-host' => 'Host', 'remote-user' => 'User', 'remote-port' => 'Port', 'os' => 'OS', 'ssh-options' => 'SSH options', 'php' => 'PHP'),
  37. 'fields-default' => array('#name', 'root', 'uri', 'remote-host', 'remote-user'),
  38. 'field-mappings' => array('name' => '#name'),
  39. 'output-data-type' => 'format-table',
  40. ),
  41. 'aliases' => array('sa'),
  42. 'examples' => array(
  43. 'drush site-alias' => 'List all alias records known to drush.',
  44. 'drush site-alias @dev' => 'Print an alias record for the alias \'dev\'.',
  45. 'drush @none site-alias' => 'Print only actual aliases; omit multisites from the local Drupal installation.',
  46. ),
  47. 'topics' => array('docs-aliases'),
  48. );
  49. $items['site-set'] = array(
  50. 'description' => 'Set a site alias to work on that will persist for the current session.',
  51. 'bootstrap' => DRUSH_BOOTSTRAP_NONE,
  52. 'handle-remote-commands' => TRUE,
  53. 'arguments' => array(
  54. 'site' => 'Site specification to use, or "-" for previous site. Omit this argument to "unset"',
  55. ),
  56. 'aliases' => array('use'),
  57. 'examples' => array(
  58. 'drush site-set @dev' => 'Set the current session to use the @dev alias.',
  59. 'drush site-set user@server/path/to/drupal#sitename' => 'Set the current session to use a site specification.',
  60. 'drush site-set -' => 'Go back to the previously-set site (like `cd -`).',
  61. 'drush site-set' => 'Without an argument, any existing site becomes unset.',
  62. ),
  63. );
  64. return $items;
  65. }
  66. /**
  67. * Command argument complete callback.
  68. *
  69. * @return
  70. * Array of available site aliases.
  71. */
  72. function sitealias_site_alias_complete() {
  73. return array('values' => array_keys(_drush_sitealias_all_list()));
  74. }
  75. /**
  76. * Command argument complete callback.
  77. *
  78. * @return
  79. * Array of available site aliases.
  80. */
  81. function sitealias_site_set_complete() {
  82. return array('values' => array_keys(_drush_sitealias_all_list()));
  83. }
  84. /**
  85. * Return a list of all site aliases known to drush.
  86. *
  87. * The array key is the site alias name, and the array value
  88. * is the site specification for the given alias.
  89. */
  90. function _drush_sitealias_alias_list() {
  91. return drush_get_context('site-aliases');
  92. }
  93. /**
  94. * Return a list of all of the local sites at the current drupal root.
  95. *
  96. * The array key is the site folder name, and the array value
  97. * is the site specification for that site.
  98. */
  99. function _drush_sitealias_site_list() {
  100. $site_list = array();
  101. $base_path = drush_get_context('DRUSH_DRUPAL_ROOT');
  102. if ($base_path) {
  103. $base_path .= '/sites';
  104. $files = drush_scan_directory($base_path, '/settings\.php/', array('.', '..', 'CVS', 'all'), 0, 1);
  105. foreach ($files as $filename => $info) {
  106. if ($info->basename == 'settings.php') {
  107. $alias_record = drush_sitealias_build_record_from_settings_file($filename);
  108. if (!empty($alias_record)) {
  109. $site_list[drush_sitealias_uri_to_site_dir($alias_record['uri'])] = $alias_record;
  110. }
  111. }
  112. }
  113. }
  114. return $site_list;
  115. }
  116. /**
  117. * Return the list of all site aliases and all local sites.
  118. */
  119. function _drush_sitealias_all_list() {
  120. drush_sitealias_load_all();
  121. return array_merge(_drush_sitealias_alias_list(), _drush_sitealias_site_list());
  122. }
  123. /**
  124. * Return the list of site aliases (remote or local) that the
  125. * user specified on the command line. If none were specified,
  126. * then all are returned.
  127. */
  128. function _drush_sitealias_user_specified_list() {
  129. $command = drush_get_command();
  130. $specifications = $command['arguments'];
  131. $site_list = array();
  132. // Iterate over the arguments and convert them to alias records
  133. if (!empty($specifications)) {
  134. list($site_list, $not_found) = drush_sitealias_resolve_sitespecs($specifications);
  135. if (!empty($not_found)) {
  136. return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt("Not found: @list", array("@list" => implode(', ', $not_found))));
  137. }
  138. }
  139. // If the user provided no args, then we will return everything.
  140. else {
  141. drush_set_default_outputformat('list');
  142. $site_list = _drush_sitealias_all_list();
  143. // Filter out the hidden items
  144. foreach ($site_list as $site_name => $one_site) {
  145. if (array_key_exists('#hidden', $one_site)) {
  146. unset($site_list[$site_name]);
  147. }
  148. }
  149. }
  150. // Filter for only local sites if specified.
  151. if (drush_get_option('local-only', FALSE)) {
  152. foreach ($site_list as $site_name => $one_site) {
  153. if ( (array_key_exists('remote-site', $one_site)) ||
  154. (!array_key_exists('root', $one_site)) ||
  155. (!is_dir($one_site['root']))
  156. ) {
  157. unset($site_list[$site_name]);
  158. }
  159. }
  160. }
  161. return $site_list;
  162. }
  163. /**
  164. * Print out the specified site aliases (or else all) using the format
  165. * specified.
  166. */
  167. function drush_sitealias_print() {
  168. // Try to get the @self alias to be defined.
  169. $phase = drush_bootstrap_max(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  170. $site_list = _drush_sitealias_user_specified_list();
  171. if ($site_list === FALSE) {
  172. return FALSE;
  173. }
  174. ksort($site_list);
  175. $with_db = (drush_get_option('with-db') != NULL) || (drush_get_option('with-db-url') != NULL);
  176. $site_specs = array();
  177. foreach ($site_list as $site => $alias_record) {
  178. $result_record = _drush_sitealias_prepare_record($alias_record);
  179. $site_specs[$site] = $result_record;
  180. }
  181. ksort($site_specs);
  182. return $site_specs;
  183. }
  184. /**
  185. * Given a site alias name, print out a php-syntax
  186. * representation of it.
  187. *
  188. * @param alias_record
  189. * The name of the site alias to print
  190. */
  191. function _drush_sitealias_prepare_record($alias_record) {
  192. $output_db = drush_get_option('with-db');
  193. $output_db_url = drush_get_option('with-db-url');
  194. $output_optional_items = drush_get_option('with-optional');
  195. // Make sure that the default items have been added for all aliases
  196. _drush_sitealias_add_static_defaults($alias_record);
  197. // Include the optional items, if requested
  198. if ($output_optional_items) {
  199. _drush_sitealias_add_transient_defaults($alias_record);
  200. }
  201. drush_sitealias_resolve_path_references($alias_record);
  202. if (isset($output_db_url) || isset($output_db)) {
  203. drush_sitealias_add_db_settings($alias_record);
  204. }
  205. // If the user specified --with-db-url, then leave the
  206. // 'db-url' entry in the alias record (unless it is not
  207. // set, in which case we will leave the 'databases' record instead).
  208. if (isset($output_db_url)) {
  209. if (!isset($alias_record['db-url'])) {
  210. $alias_record['db-url'] = drush_sitealias_convert_databases_to_db_url($alias_record['databases']);
  211. }
  212. unset($alias_record['databases']);
  213. }
  214. // If the user specified --with-db, then leave the
  215. // 'databases' entry in the alias record.
  216. else if (isset($output_db)) {
  217. unset($alias_record['db-url']);
  218. }
  219. // If neither --with-db nor --with-db-url were specified,
  220. // then remove both the 'db-url' and the 'databases' entries.
  221. else {
  222. unset($alias_record['db-url']);
  223. unset($alias_record['databases']);
  224. }
  225. // We don't want certain fields to go into the output
  226. if (!drush_get_option('show-hidden')) {
  227. foreach ($alias_record as $key => $value) {
  228. if ($key[0] == '#') {
  229. unset($alias_record[$key]);
  230. }
  231. }
  232. }
  233. // We only want to output the 'root' item; don't output the '%root' path alias
  234. if (array_key_exists('path-aliases', $alias_record) && array_key_exists('%root', $alias_record['path-aliases'])) {
  235. unset($alias_record['path-aliases']['%root']);
  236. // If there is nothing left in path-aliases, then clear it out
  237. if (count($alias_record['path-aliases']) == 0) {
  238. unset($alias_record['path-aliases']);
  239. }
  240. }
  241. return $alias_record;
  242. }
  243. function _drush_sitealias_print_record($alias_record, $site_alias = '') {
  244. $result_record = _drush_sitealias_prepare_record($alias_record);
  245. // The alias name will be the same as the site alias name,
  246. // unless the user specified some other name on the command line.
  247. $alias_name = drush_get_option('alias-name');
  248. if (!isset($alias_name)) {
  249. $alias_name = $site_alias;
  250. if (empty($alias_name) || is_numeric($alias_name)) {
  251. $alias_name = drush_sitealias_uri_to_site_dir($result_record['uri']);
  252. }
  253. }
  254. // Alias names contain an '@' when referenced, but do
  255. // not contain an '@' when defined.
  256. if (substr($alias_name,0,1) == '@') {
  257. $alias_name = substr($alias_name,1);
  258. }
  259. $exported_alias = var_export($result_record, TRUE);
  260. drush_print('$aliases[\'' . $alias_name . '\'] = ' . $exported_alias . ';');
  261. }
  262. /**
  263. * Use heuristics to attempt to convert from a site directory to a URI.
  264. * This function should only be used when the URI really is unknown, as
  265. * the mapping is not perfect.
  266. *
  267. * @param site_dir
  268. * A directory, such as domain.com.8080.drupal
  269. *
  270. * @return string
  271. * A uri, such as http://domain.com:8080/drupal
  272. */
  273. function _drush_sitealias_site_dir_to_uri($site_dir) {
  274. // Protect IP addresses NN.NN.NN.NN by converting them
  275. // temporarily to NN_NN_NN_NN for now.
  276. $uri = preg_replace("/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/", "$1_$2_$3_$4", $site_dir);
  277. // Convert .[0-9]+. into :[0-9]+/
  278. $uri = preg_replace("/\.([0-9]+)\./", ":$1/", $uri);
  279. // Convert .[0-9]$ into :[0-9]
  280. $uri = preg_replace("/\.([0-9]+)$/", ":$1", $uri);
  281. // Convert .(com|net|org|info). into .(com|net|org|info)/
  282. $uri = str_replace(array('.com.', '.net.', '.org.', '.info.'), array('.com/', '.net/', '.org/', '.info/'), $uri);
  283. // If there is a / then convert every . after the / to /
  284. // Then again, if we did this we would break if the path contained a "."
  285. // I hope that the path would never contain a "."...
  286. $pos = strpos($uri, '/');
  287. if ($pos !== false) {
  288. $uri = substr($uri, 0, $pos + 1) . str_replace('.', '/', substr($uri, $pos + 1));
  289. }
  290. // n.b. this heuristic works all the time if there is a port,
  291. // it also works all the time if there is a port and no path,
  292. // but it does not work for domains such as .co.jp with no path,
  293. // and it can fail horribly if someone makes a domain like "info.org".
  294. // Still, I think this is the best we can do short of consulting DNS.
  295. // Convert from NN_NN_NN_NN back to NN.NN.NN.NN
  296. $uri = preg_replace("/([0-9]+)_([0-9]+)_([0-9]+)_([0-9]+)/", "$1.$2.$3.$4", $site_dir);
  297. return 'http://' . $uri;
  298. }
  299. /**
  300. * Validation callback for drush site-set.
  301. */
  302. function drush_sitealias_site_set_validate() {
  303. if (!function_exists('posix_getppid')) {
  304. $args = array('!command' => 'site-set', '!dependencies' => 'POSIX');
  305. return drush_set_error('DRUSH_COMMAND_PHP_DEPENDENCY_ERROR', dt('Command !command needs the following PHP extensions installed/enabled to run: !dependencies.', $args));
  306. }
  307. }
  308. /**
  309. * Set the DRUPAL_SITE variable by writing it out to a temporary file that we
  310. * then source for persistent site switching.
  311. *
  312. * @param site
  313. * A valid site specification.
  314. */
  315. function drush_sitealias_site_set($site = '@none') {
  316. if ($filename = drush_sitealias_get_envar_filename()) {
  317. $last_site_filename = drush_sitealias_get_envar_filename('drush-drupal-prev-site-');
  318. if ($site == '-') {
  319. if (file_exists($last_site_filename)) {
  320. $site = file_get_contents($last_site_filename);
  321. }
  322. else {
  323. $site = '@none';
  324. }
  325. }
  326. if (_drush_sitealias_set_context_by_name($site)) {
  327. if (file_exists($filename)) {
  328. @unlink($last_site_filename);
  329. @rename($filename, $last_site_filename);
  330. }
  331. $success_message = dt("Site set to !site", array('!site' => $site));
  332. if ($site == '@none') {
  333. if (drush_delete_dir($filename)) {
  334. drush_print($success_message);
  335. }
  336. }
  337. elseif (drush_mkdir(dirname($filename), TRUE)) {
  338. if (file_put_contents($filename, $site)) {
  339. drush_print($success_message);
  340. drush_log(dt("Site information stored in !file", array('!file' => $filename)));
  341. }
  342. }
  343. }
  344. else {
  345. drush_set_error('DRUPAL_SITE_NOT_FOUND', dt("Could not find a site definition for !site.", array('!site' => $site)));
  346. }
  347. }
  348. }