function drush_core_pre_site_install

8.0.x site_install.drush.inc drush_core_pre_site_install($profile = NULL)
6.x site_install.drush.inc drush_core_pre_site_install($profile = NULL)
7.x site_install.drush.inc drush_core_pre_site_install($profile = NULL)
3.x site_install.drush.inc drush_core_pre_site_install()
4.x site_install.drush.inc drush_core_pre_site_install()
5.x site_install.drush.inc drush_core_pre_site_install($profile = NULL)
master site_install.drush.inc drush_core_pre_site_install()

File

commands/core/site_install.drush.inc, line 4

Code

function drush_core_pre_site_install() {

  if (!$db_spec = drush_core_site_install_db_spec()) {
    drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
    return;
  }

  // TODO: not needed?
  $sites_subdir = drush_get_option('sites-subdir', 'default');
  $conf_path = "sites/$sites_subdir";
  $files = "$conf_path/files";
  $settingsfile = "$conf_path/settings.php";
  if (!file_exists($files)) {
    $msg[] = dt('create a @files directory', array('@files' => $files));
  }
  if (!file_exists($settingsfile)) {
    $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
  }
  $msg[] = dt("DROP your '@db' database and then CREATE a new one.", array('@db' => $db_spec['database']));

  if (!drush_confirm(dt('You are about to ') . implode(dt(' and '), $msg) . ' Do you want to continue?')) {
    return drush_set_error('CORE_SITE_INSTALL_ABORT', 'Aborting.');
  }

  // Can't install without sites directory and settings.php.
  if (!file_exists($conf_path)) {
    if (!drush_op('mkdir', $conf_path) && !drush_get_context('DRUSH_SIMULATE')) {
      drush_set_error(dt('Failed to create directory @conf_path', array('@conf_path' => $conf_path)));
      return;
    }
  }
  else {
    drush_log(dt('Sites directory @subdir already exists - proceeding.', array('@subdir' => $conf_path)));
  }
  if (!file_exists($settingsfile)) {
    if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
      drush_set_error(dt('Failed to copy sites/default/default.settings.php to  @settingsfile', array('@settingsfile' => $settingsfile)));
      return;
    }
  }


  // Add a files dir if needed
  if (!file_exists($files)) {
    if (!drush_op('mkdir', $files) && !drush_get_context('DRUSH_SIMULATE')) {
      drush_set_error(dt('Failed to create directory @name', array('@name' => $files)));
      return;
    }
  }

  // Now we can bootstrap up to the specified site if not already there.
  drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);

  // Drop and create DB if needed.
  // TODO: support db-su like sql sync.
  // Can't use drush_sql_query() since might not have a DB.
  $exec = 'mysql ' . _drush_sql_get_credentials($db_spec);
  // Strip DB name from credentials. Soon it won't exist anymore. We do
  // need a DB name to connect to so use built-in mysql DB.
  $replacement_db = 'information_schema';
  // Make sure we are only replacing the database name,
  // and not a username or password that is the same as the database name.
  $exec = str_replace(" {$db_spec['database']}", " {$replacement_db}", $exec) . ' -e ';
  if (drush_op('system', $exec . ' "DROP DATABASE IF EXISTS ' . $db_spec['database'] . '"') && !drush_get_context('DRUSH_SIMULATE')) {
    drush_set_error(dt('Could not drop database: @name', array('@name' => $db_spec['database'])));
    return;
  }

  if (drush_op('system', $exec . '"CREATE DATABASE ' . $db_spec['database'] . '"') && !drush_get_context('DRUSH_SIMULATE')) {
    drush_set_error(dt('Could not create new database: @name', array('@name' => $db_spec['database'])));
    return;
  }
}