site_install.drush.inc

  1. 8.0.x commands/core/site_install.drush.inc
  2. 6.x commands/core/site_install.drush.inc
  3. 7.x commands/core/site_install.drush.inc
  4. 3.x commands/core/site_install.drush.inc
  5. 4.x commands/core/site_install.drush.inc
  6. 5.x commands/core/site_install.drush.inc
  7. master commands/core/site_install.drush.inc

Functions

File

commands/core/site_install.drush.inc
View source
  1. <?php
  2. // Perform setup tasks for installation.
  3. function drush_core_pre_site_install() {
  4. if (!$db_spec = drush_core_site_install_db_spec()) {
  5. drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
  6. return;
  7. }
  8. // TODO: not needed?
  9. $sites_subdir = drush_get_option('sites-subdir', 'default');
  10. $conf_path = "sites/$sites_subdir";
  11. $files = "$conf_path/files";
  12. $settingsfile = "$conf_path/settings.php";
  13. if (!file_exists($files)) {
  14. $msg[] = dt('create a @files directory', array('@files' => $files));
  15. }
  16. if (!file_exists($settingsfile)) {
  17. $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
  18. }
  19. $msg[] = dt("DROP your '@db' database and then CREATE a new one.", array('@db' => $db_spec['database']));
  20. if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
  21. return drush_set_error('CORE_SITE_INSTALL_ABORT', 'Aborting.');
  22. }
  23. // Can't install without sites directory and settings.php.
  24. if (!file_exists($conf_path)) {
  25. if (!drush_op('mkdir', $conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
  26. drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
  27. return;
  28. }
  29. }
  30. else {
  31. drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
  32. }
  33. if (!file_exists($settingsfile)) {
  34. if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
  35. drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
  36. return;
  37. }
  38. }
  39. // Add a files dir if needed
  40. if (!file_exists($files)) {
  41. if (!drush_op('mkdir', $files) && !drush_get_context('DRUSH_SIMULATE')) {
  42. drush_set_error(dt('Failed to create directory @name', array('@name' => $files)));
  43. return;
  44. }
  45. }
  46. // Now we can bootstrap up to the specified site if not already there.
  47. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  48. // Drop and create DB if needed.
  49. // TODO: support db-su like sql sync.
  50. // Can't use drush_sql_query() since might not have a DB.
  51. $exec = 'mysql ' . _drush_sql_get_credentials($db_spec);
  52. // Strip DB name from credentials. Soon it won't exist anymore. We do
  53. // need a DB name to connect to so use built-in mysql DB.
  54. $replacement_db = 'information_schema';
  55. // Make sure we are only replacing the database name,
  56. // and not a username or password that is the same as the database name.
  57. $exec = str_replace(" {$db_spec['database']}", " {$replacement_db}", $exec) . ' -e ';
  58. if (drush_op('system', $exec . ' "DROP DATABASE IF EXISTS ' . $db_spec['database'] . '"') && !drush_get_context('DRUSH_SIMULATE')) {
  59. drush_set_error(dt('Could not drop database: @name', array('@name' => $db_spec['database'])));
  60. return;
  61. }
  62. if (drush_op('system', $exec . '"CREATE DATABASE ' . $db_spec['database'] . '"') && !drush_get_context('DRUSH_SIMULATE')) {
  63. drush_set_error(dt('Could not create new database: @name', array('@name' => $db_spec['database'])));
  64. return;
  65. }
  66. }
  67. function drush_core_site_install($profile = 'standard') {
  68. define('MAINTENANCE_MODE', 'install');
  69. require_once DRUPAL_ROOT . '/includes/install.core.inc';
  70. $db_spec = drush_core_site_install_db_spec();
  71. $account_pass = drush_get_option('account-pass', 'admin');
  72. $settings = array(
  73. 'parameters' => array(
  74. 'profile' => $profile,
  75. 'locale' => drush_get_option('locale', 'en'),
  76. ),
  77. 'forms' => array(
  78. 'install_settings_form' => $db_spec,
  79. 'install_configure_form' => array(
  80. 'site_name' => drush_get_option('site-name', 'Site-Install'),
  81. 'site_mail' => drush_get_option('site-mail', 'admin@example.com'),
  82. 'account' => array(
  83. 'name' => drush_get_option('account-name', 'admin'),
  84. 'mail' => drush_get_option('account-mail', 'admin@example.com'),
  85. 'pass' => array(
  86. 'pass1' => $account_pass,
  87. 'pass2' => $account_pass,
  88. ),
  89. ),
  90. 'update_status_module' => array(
  91. 1 => TRUE,
  92. 2 => TRUE,
  93. ),
  94. 'clean_url' => drush_get_option('clean-url', TRUE),
  95. ),
  96. ),
  97. );
  98. drush_log(dt('Starting Drupal installation. This takes 30 seconds or so ...'), 'ok');
  99. install_drupal($settings);
  100. }
  101. // Return a db_spec based on supplied db_url/db_prefix options or
  102. // an existing settings.php.
  103. function drush_core_site_install_db_spec() {
  104. if ($db_url = drush_get_option('db-url')) {
  105. // We were passed a db_url. Usually a fresh site.
  106. $db_spec = drush_convert_db_from_db_url($db_url);
  107. $db_spec['db_prefix'] = drush_get_option('db-prefix');
  108. return $db_spec;
  109. }
  110. elseif (drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)) {
  111. // We have an existing settings.php.
  112. $db_spec = _drush_sql_get_db_spec();
  113. $db_spec['db_prefix'] = $GLOBALS['db_prefix'];
  114. return $db_spec;
  115. }
  116. else {
  117. return FALSE;
  118. }
  119. }