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