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. $sitesfile = "sites/sites.php";
  30. $sitesfile_write = drush_drupal_major_version() >= 8 && !file_exists($sitesfile) && $sites_subdir != 'default';
  31. if (!file_exists($files)) {
  32. $msg[] = dt('create a @files directory', array('@files' => $files));
  33. }
  34. if (!file_exists($settingsfile)) {
  35. $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
  36. }
  37. if ($sitesfile_write) {
  38. $msg[] = dt('create a @sitesfile file', array('@sitesfile' => $sitesfile));
  39. }
  40. if (drush_sql_db_exists($db_spec)) {
  41. $msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
  42. }
  43. else {
  44. $msg[] = dt("CREATE the '@db' database.", array('@db' => $db_spec['database']));
  45. }
  46. if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
  47. return drush_user_abort();
  48. }
  49. // Can't install without sites subdirectory and settings.php.
  50. if (!file_exists($conf_path)) {
  51. if (!drush_mkdir($conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
  52. drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
  53. return;
  54. }
  55. }
  56. else {
  57. drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
  58. }
  59. if (!drush_file_not_empty($settingsfile)) {
  60. if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
  61. return drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
  62. }
  63. if (drush_drupal_major_version() == 6) {
  64. // On D6, we have to write $db_url ourselves. In D7+, the installer does it.
  65. file_put_contents($settingsfile, "\n" . '$db_url = \'' . drush_get_option('db-url') . "';\n", FILE_APPEND);
  66. // Instead of parsing and performing string replacement on the configuration file,
  67. // the options are appended and override the defaults.
  68. // Database table prefix
  69. if (!empty($db_spec['db_prefix'])) {
  70. if (is_array($db_spec['db_prefix'])) {
  71. // Write db_prefix configuration as an array
  72. $db_prefix_config = '$db_prefix = ' . var_export($db_spec['db_prefix'], TRUE) . ';';
  73. }
  74. else {
  75. // Write db_prefix configuration as a string
  76. $db_prefix_config = '$db_prefix = \'' . $db_spec['db_prefix'] . '\';';
  77. }
  78. file_put_contents($settingsfile, "\n" . $db_prefix_config . "\n", FILE_APPEND);
  79. }
  80. }
  81. }
  82. // Write an empty sites.php if we are on D8 and using multi-site.
  83. if ($sitesfile_write) {
  84. if (!drush_op('copy', 'sites/example.sites.php', $sitesfile) && !drush_get_context('DRUSH_SIMULATE')) {
  85. return drush_set_error(dt('Failed to copy sites/sites.php to @sitesfile', array('@sitesfile' => $sitesfile)));
  86. }
  87. }
  88. // The Drupal 6 installer needs to bootstrap up to the specified site.
  89. // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
  90. define('MAINTENANCE_MODE', 'install');
  91. if (drush_drupal_major_version() == 6) {
  92. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  93. }
  94. elseif ($sites_subdir != 'default') {
  95. drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  96. }
  97. // Add a files dir if needed
  98. if (!file_exists($files)) {
  99. if (!drush_mkdir($files) && !drush_get_context('DRUSH_SIMULATE')) {
  100. drush_set_error(dt('Failed to create directory @name', array('@name' => $files)));
  101. return;
  102. }
  103. }
  104. // Empty or create the DB as needed.
  105. drush_sql_empty_db($db_spec);
  106. if (drush_drupal_major_version() >= 8) {
  107. // Empty out any existing config directories.
  108. $directories = array();
  109. // Check for alternative format:
  110. // @code
  111. // $config_directories = array(
  112. // 'active' => array(
  113. // 'path' => 'config/active',
  114. // ),
  115. // 'staging' => array(
  116. // 'path' => 'config/staging',
  117. // ),
  118. // );
  119. // @endcode
  120. preg_match('/\n\$config_directories[^;]*\'path\' => \'([^\']*)\'.*\'path\' => \'([^\']*)\'/s', file_get_contents($settingsfile), $directories);
  121. if (empty($directories)) {
  122. // Check for alternative format:
  123. // @code
  124. // $config_directories['active']['path'] = 'config/active';
  125. // $config_directories['staging']['path'] = 'config/staging';
  126. // @endcode
  127. foreach (array('active', 'staging') as $type) {
  128. preg_match('/\n\$config_directories[^;]*\'' . $type . '\'\]\[\'path\'\] = \'([^\']*)\'/', file_get_contents($settingsfile), $directory);
  129. if (isset($directory[1])) {
  130. $directories[$type] = $files . '/' . $directory[1];
  131. }
  132. }
  133. }
  134. else {
  135. $directories['active'] = $files . '/' . $directories[1];
  136. $directories['staging'] = $files . '/' . $directories[2];
  137. }
  138. foreach ($directories as $directory) {
  139. if (file_exists($directory)) {
  140. drush_delete_dir_contents($directory, TRUE);
  141. }
  142. }
  143. // Remove files/php if needed.
  144. drush_delete_dir("$files/php", TRUE);
  145. }
  146. return TRUE;
  147. }
  148. /**
  149. * Command callback.
  150. */
  151. function drush_core_site_install($profile = NULL) {
  152. $args = func_get_args();
  153. $form_options = array();
  154. if ($args) {
  155. // The first argument is the profile.
  156. $profile = array_shift($args);
  157. // Subsequent arguments are additional form values.
  158. foreach ($args as $arg) {
  159. list($key, $value) = explode('=', $arg);
  160. // Allow for numeric and NULL values to be passed in.
  161. if (is_numeric($value)) {
  162. $value = intval($value);
  163. }
  164. elseif ($value == 'NULL') {
  165. $value = NULL;
  166. }
  167. $form_options[$key] = $value;
  168. }
  169. }
  170. drush_include_engine('drupal', 'site_install', drush_drupal_major_version());
  171. drush_core_site_install_version($profile, $form_options);
  172. }