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 ascending Description
_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.
_drush_sitealias_site_list Return a list of all of the local sites at the current drupal root.
_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_print_record
_drush_sitealias_prepare_record Given a site alias name, print out a php-syntax representation of it.
_drush_sitealias_all_list Return the list of all site aliases and all local sites.
_drush_sitealias_alias_list Return a list of all site aliases known to drush.
sitealias_site_set_complete Command argument complete callback.
sitealias_sitealias_print_complete Command argument complete callback.
sitealias_drush_help
sitealias_drush_command
drush_sitealias_site_set_validate Validation callback for drush site-set.
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_reset Deletes the file that stores the DRUPAL_SITE variable for persistent site switching.
drush_sitealias_print Print out the specified site aliases (or else all) using the format specified.

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 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 display sites that are available on the local system (remote-site not set, and Drupal root exists).',
  28. ),
  29. 'outputformat' => array(
  30. 'default' => 'config',
  31. 'pipe-format' => 'var_export',
  32. 'variable-name' => 'aliases',
  33. 'hide-empty-fields' => TRUE,
  34. 'private-fields' => 'password',
  35. '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'),
  36. 'fields-default' => array('#name', 'root', 'uri', 'remote-host', 'remote-user'),
  37. 'field-mappings' => array('name' => '#name'),
  38. 'output-data-type' => 'format-table',
  39. ),
  40. 'aliases' => array('sa'),
  41. 'examples' => array(
  42. 'drush site-alias' => 'List all alias records known to drush.',
  43. 'drush site-alias @dev' => 'Print an alias record for the alias \'dev\'.',
  44. ),
  45. 'topics' => array('docs-aliases'),
  46. );
  47. $items['site-set'] = array(
  48. 'description' => 'Set a site alias to work on that will persist for the current session.',
  49. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  50. 'handle-remote-commands' => TRUE,
  51. 'arguments' => array(
  52. 'site' => 'Site specification to use, or "-" for previous site. Omit this argument to "unset"',
  53. ),
  54. 'aliases' => array('use'),
  55. 'examples' => array(
  56. 'drush site-set @dev' => 'Set the current session to use the @dev alias.',
  57. 'drush site-set user@server/path/to/drupal#sitename' => 'Set the current session to use a site specification.',
  58. 'drush site-set -' => 'Go back to the previously-set site (like `cd -`).',
  59. 'drush site-set' => 'Without an argument, any existing site becomes unset.',
  60. ),
  61. );
  62. // @deprecated and hidden as of 6.0.1. Use site-set without an argument.
  63. $items['site-reset'] = array(
  64. 'description' => 'Reset a persistently set site.',
  65. 'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
  66. 'handle-remote-commands' => TRUE,
  67. 'aliases' => array('unuse'),
  68. 'hidden' => TRUE,
  69. );
  70. return $items;
  71. }
  72. /**
  73. * Command argument complete callback.
  74. *
  75. * @return
  76. * Array of available site aliases.
  77. */
  78. function sitealias_sitealias_print_complete() {
  79. return array('values' => array_keys(_drush_sitealias_all_list()));
  80. }
  81. /**
  82. * Command argument complete callback.
  83. *
  84. * @return
  85. * Array of available site aliases.
  86. */
  87. function sitealias_site_set_complete() {
  88. return array('values' => array_keys(_drush_sitealias_all_list()));
  89. }
  90. /**
  91. * Return a list of all site aliases known to drush.
  92. *
  93. * The array key is the site alias name, and the array value
  94. * is the site specification for the given alias.
  95. */
  96. function _drush_sitealias_alias_list() {
  97. return drush_get_context('site-aliases');
  98. }
  99. /**
  100. * Return a list of all of the local sites at the current drupal root.
  101. *
  102. * The array key is the site folder name, and the array value
  103. * is the site specification for that site.
  104. */
  105. function _drush_sitealias_site_list() {
  106. $site_list = array();
  107. $base_path = drush_get_context('DRUSH_DRUPAL_ROOT');
  108. if ($base_path) {
  109. $base_path .= '/sites';
  110. $files = drush_scan_directory($base_path, '/settings\.php/', array('.', '..', 'CVS', 'all'), 0, 1);
  111. foreach ($files as $filename => $info) {
  112. if ($info->basename == 'settings.php') {
  113. $alias_record = drush_sitealias_build_record_from_settings_file($filename);
  114. if (!empty($alias_record)) {
  115. $site_list[drush_sitealias_uri_to_site_dir($alias_record['uri'])] = $alias_record;
  116. }
  117. }
  118. }
  119. }
  120. return $site_list;
  121. }
  122. /**
  123. * Return the list of all site aliases and all local sites.
  124. */
  125. function _drush_sitealias_all_list() {
  126. drush_sitealias_load_all();
  127. return array_merge(_drush_sitealias_alias_list(), _drush_sitealias_site_list());
  128. }
  129. /**
  130. * Return the list of site aliases (remote or local) that the
  131. * user specified on the command line. If none were specified,
  132. * then all are returned.
  133. */
  134. function _drush_sitealias_user_specified_list() {
  135. $command = drush_get_command();
  136. $specifications = $command['arguments'];
  137. $site_list = array();
  138. // Iterate over the arguments and convert them to alias records
  139. if (!empty($specifications)) {
  140. list($site_list, $not_found) = drush_sitealias_resolve_sitespecs($specifications);
  141. if (!empty($not_found)) {
  142. return drush_set_error('DRUSH_ALIAS_NOT_FOUND', dt("Not found: @list", array("@list" => implode(', ', $not_found))));
  143. }
  144. }
  145. // If the user provided no args, then we will return everything.
  146. else {
  147. drush_set_default_outputformat('list');
  148. $site_list = _drush_sitealias_all_list();
  149. // Filter out the hidden items
  150. foreach ($site_list as $site_name => $one_site) {
  151. if (array_key_exists('#hidden', $one_site)) {
  152. unset($site_list[$site_name]);
  153. }
  154. }
  155. }
  156. // Filter for only local sites if specified.
  157. if (drush_get_option('local', FALSE)) {
  158. foreach ($site_list as $site_name => $one_site) {
  159. if ( (array_key_exists('remote-site', $one_site)) ||
  160. (!array_key_exists('root', $one_site)) ||
  161. (!is_dir($one_site['root']))
  162. ) {
  163. unset($site_list[$site_name]);
  164. }
  165. }
  166. }
  167. return $site_list;
  168. }
  169. /**
  170. * Print out the specified site aliases (or else all) using the format
  171. * specified.
  172. */
  173. function drush_sitealias_print() {
  174. $site_list = _drush_sitealias_user_specified_list();
  175. if ($site_list === FALSE) {
  176. return FALSE;
  177. }
  178. ksort($site_list);
  179. $with_db = (drush_get_option('with-db') != NULL) || (drush_get_option('with-db-url') != NULL);
  180. $site_specs = array();
  181. foreach ($site_list as $site => $alias_record) {
  182. list($alias_name, $result_record) = _drush_sitealias_prepare_record($alias_record, $site);
  183. $site_specs[$alias_name] = $result_record;
  184. }
  185. ksort($site_specs);
  186. return $site_specs;
  187. }
  188. /**
  189. * Given a site alias name, print out a php-syntax
  190. * representation of it.
  191. *
  192. * @param alias_record
  193. * The name of the site alias to print
  194. */
  195. function _drush_sitealias_prepare_record($alias_record, $site_alias = '') {
  196. $output_db = drush_get_option('with-db');
  197. $output_db_url = drush_get_option('with-db-url');
  198. $output_optional_items = drush_get_option('with-optional');
  199. // Make sure that the default items have been added for all aliases
  200. _drush_sitealias_add_static_defaults($alias_record);
  201. // Include the optional items, if requested
  202. if ($output_optional_items) {
  203. _drush_sitealias_add_transient_defaults($alias_record);
  204. }
  205. drush_sitealias_resolve_path_references($alias_record);
  206. if (isset($output_db_url)) {
  207. drush_sitealias_add_db_url($alias_record);
  208. }
  209. if (isset($output_db_url) || isset($output_db)) {
  210. drush_sitealias_add_db_settings($alias_record);
  211. }
  212. // If the user specified --with-db-url, then leave the
  213. // 'db-url' entry in the alias record (unless it is not
  214. // set, in which case we will leave the 'databases' record instead).
  215. if (isset($output_db_url)) {
  216. if (isset($alias_record['db-url'])) {
  217. unset($alias_record['databases']);
  218. }
  219. }
  220. // If the user specified --with-db, then leave the
  221. // 'databases' entry in the alias record.
  222. else if (isset($output_db)) {
  223. unset($alias_record['db-url']);
  224. }
  225. // If neither --with-db nor --with-db-url were specified,
  226. // then remove both the 'db-url' and the 'databases' entries.
  227. else {
  228. unset($alias_record['db-url']);
  229. unset($alias_record['databases']);
  230. }
  231. // The alias name will be the same as the site alias name,
  232. // unless the user specified some other name on the command line.
  233. $alias_name = drush_get_option('alias-name');
  234. if (!isset($alias_name)) {
  235. $alias_name = $site_alias;
  236. if (empty($alias_name) || is_numeric($alias_name)) {
  237. $alias_name = drush_sitealias_uri_to_site_dir($alias_record['uri']);
  238. }
  239. }
  240. // We don't want certain fields to go into the output
  241. unset($alias_record['#group']);
  242. unset($alias_record['#hidden']);
  243. // We only want to output the 'root' item; don't output the '%root' path alias
  244. if (array_key_exists('path-aliases', $alias_record) && array_key_exists('%root', $alias_record['path-aliases'])) {
  245. unset($alias_record['path-aliases']['%root']);
  246. // If there is nothing left in path-aliases, then clear it out
  247. if (count($alias_record['path-aliases']) == 0) {
  248. unset($alias_record['path-aliases']);
  249. }
  250. }
  251. // Alias names contain an '@' when referenced, but do
  252. // not contain an '@' when defined.
  253. if (substr($alias_name,0,1) == '@') {
  254. $alias_name = substr($alias_name,1);
  255. }
  256. return array($alias_name, $alias_record);
  257. }
  258. function _drush_sitealias_print_record($alias_record, $site_alias = '') {
  259. list($alias_name, $result_record) = _drush_sitealias_prepare_record($alias_record, $site_alias);
  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 (_drush_sitealias_set_context_by_name($site)) {
  328. if (file_exists($filename)) {
  329. @unlink($last_site_filename);
  330. @rename($filename, $last_site_filename);
  331. }
  332. $success_message = dt("Site set to !site", array('!site' => $site));
  333. if ($site == '@none') {
  334. if (drush_delete_dir($filename)) {
  335. drush_print($success_message);
  336. }
  337. }
  338. elseif (drush_mkdir(dirname($filename), TRUE)) {
  339. if (file_put_contents($filename, $site)) {
  340. drush_print($success_message);
  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. }
  349. /**
  350. * Deletes the file that stores the DRUPAL_SITE variable for persistent
  351. * site switching.
  352. */
  353. function drush_sitealias_site_reset() {
  354. if (($filename = drush_sitealias_get_envar_filename()) && file_exists($filename)) {
  355. drush_delete_dir($filename);
  356. }
  357. }