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

Namesort descending Description
drush_core_pre_site_install Perform setup tasks for installation.
drush_core_site_install Command callback.
drush_core_site_install_validate Command validate.

File

commands/core/site_install.drush.inc
View source
  1. <?php
  2. /**
  3. * Command validate.
  4. */
  5. function drush_core_site_install_validate() {
  6. if ($sites_subdir = drush_get_option('sites-subdir')) {
  7. $lower = strtolower($sites_subdir);
  8. if ($sites_subdir != $lower) {
  9. drush_log(dt('Only lowercase sites-subdir are valid. Switching to !lower.', array('!lower' => $lower)), 'warning');
  10. drush_set_option('sites-subdir', $lower);
  11. }
  12. // Make sure that we will bootstrap to the 'sites-subdir' site.
  13. drush_set_option('uri', 'http://' . $sites_subdir);
  14. }
  15. }
  16. /**
  17. * Perform setup tasks for installation.
  18. */
  19. function drush_core_pre_site_install($profile = NULL) {
  20. if (!$db_spec = _drush_sql_get_db_spec()) {
  21. drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
  22. return;
  23. }
  24. $default_sites_subdir = drush_get_context('DRUSH_DRUPAL_SITE', 'default');
  25. $sites_subdir = drush_get_option('sites-subdir', $default_sites_subdir);
  26. $conf_path = "sites/$sites_subdir";
  27. $files = "$conf_path/files";
  28. $settingsfile = "$conf_path/settings.php";
  29. if (!file_exists($files)) {
  30. $msg[] = dt('create a @files directory', array('@files' => $files));
  31. }
  32. if (!file_exists($settingsfile)) {
  33. $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
  34. }
  35. if (drush_sql_db_exists($db_spec)) {
  36. $msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
  37. }
  38. else {
  39. $msg[] = dt("CREATE the '@db' database.", array('@db' => $db_spec['database']));
  40. }
  41. if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
  42. return drush_user_abort();
  43. }
  44. // Can't install without sites directory and settings.php.
  45. if (!file_exists($conf_path)) {
  46. if (!drush_mkdir($conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
  47. drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
  48. return;
  49. }
  50. }
  51. else {
  52. drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
  53. }
  54. if (!drush_file_not_empty($settingsfile)) {
  55. if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
  56. drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
  57. return;
  58. }
  59. if (drush_drupal_major_version() == 6) {
  60. // On D6, we have to write $db_url ourselves. In D7+, the installer does it.
  61. file_put_contents($settingsfile, "\n" . '$db_url = \'' . drush_get_option('db-url') . "';\n", FILE_APPEND);
  62. // Instead of parsing and performing string replacement on the configuration file,
  63. // the options are appended and override the defaults.
  64. // Database table prefix
  65. if (!empty($db_spec['db_prefix'])) {
  66. if (is_array($db_spec['db_prefix'])) {
  67. // Write db_prefix configuration as an array
  68. $db_prefix_config = '$db_prefix = ' . var_export($db_spec['db_prefix'], TRUE) . ';';
  69. }
  70. else {
  71. // Write db_prefix configuration as a string
  72. $db_prefix_config = '$db_prefix = \'' . $db_spec['db_prefix'] . '\';';
  73. }
  74. file_put_contents($settingsfile, "\n" . $db_prefix_config . "\n", FILE_APPEND);
  75. }
  76. }
  77. }
  78. // If there is a profile, write it (as needed) into $conf['install_profile']
  79. // to help drush find commandfiles earlier in the drush bootstrap.
  80. if (!empty($profile)) {
  81. $contents = file_get_contents($settingsfile);
  82. if (strpos($contents, "\$conf['install_profile']") === FALSE && is_writable($settingsfile)) {
  83. $install_profile_config = "\$conf['install_profile'] = '$profile';";
  84. $appended = file_put_contents($settingsfile, "\n" . $install_profile_config . "\n", FILE_APPEND);
  85. }
  86. }
  87. // The Drupal 6 installer needs to bootstrap up to the specified site.
  88. // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
  89. define('MAINTENANCE_MODE', 'install');
  90. if (drush_drupal_major_version() == 6) {
  91. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  92. }
  93. elseif ($sites_subdir != 'default') {
  94. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  95. }
  96. // Add a files dir if needed
  97. if (!file_exists($files)) {
  98. if (!drush_mkdir($files) && !drush_get_context('DRUSH_SIMULATE')) {
  99. drush_set_error(dt('Failed to create directory @name', array('@name' => $files)));
  100. return;
  101. }
  102. }
  103. // Empty or create the DB as needed.
  104. $db_name = $db_spec['database'];
  105. $scheme = _drush_sql_get_scheme($db_spec);
  106. $simulate = drush_get_context('DRUSH_SIMULATE');
  107. if ($scheme === 'sqlite') {
  108. // With SQLite, we don't DROP DATABASEs. Each database is in a single file,
  109. // so we just remove the file. We also don't CREATE DATABASEs; it is created
  110. // when SQLite attempts to open a database file which doesn't exist.
  111. if (file_exists($db_spec['database']) && !$simulate) {
  112. if (!unlink($db_spec['database'])) {
  113. drush_set_error(dt('Could not drop database: @name', array('@name' => $db_name)));
  114. }
  115. }
  116. }
  117. else {
  118. drush_sql_empty_db($db_spec);
  119. }
  120. return TRUE;
  121. }
  122. /**
  123. * Command callback.
  124. */
  125. function drush_core_site_install($profile = NULL) {
  126. $args = func_get_args();
  127. $form_options = array();
  128. if ($args) {
  129. // The first argument is the profile.
  130. $profile = array_shift($args);
  131. // Subsequent arguments are additional form values.
  132. foreach ($args as $arg) {
  133. list($key, $value) = explode('=', $arg);
  134. $form_options[$key] = $value;
  135. }
  136. }
  137. drush_include_engine('drupal', 'site_install', drush_drupal_major_version());
  138. drush_core_site_install_version($profile, $form_options);
  139. }