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