PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/drush/commands/core/sitealias.drush.inc

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