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()

Perform setup tasks for installation.

File

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

Code

function drush_core_pre_site_install($profile = NULL) {
  $sql = drush_sql_get_class();
  if (!$db_spec = $sql->db_spec()) {
    drush_set_error(dt('Could not determine database connection parameters. Pass --db-url option.'));
    return;
  }

  // Make sure URI is set so we get back a proper $alias_record. Needed for quick-drupal.
  _drush_bootstrap_selected_uri();

  $alias_record = drush_sitealias_get_record('@self');
  $sites_subdir = drush_sitealias_local_site_path($alias_record);
  // Override with sites-subdir if specified.
  if ($dir = drush_get_option('sites-subdir')) {
    $sites_subdir = "sites/$dir";
  }
  $conf_path = $sites_subdir;
  // Handle the case where someuse uses --variables to set the file public path. Won't work on D8+.
  $files = !empty($GLOBALS['conf']['files_public_path']) ? $GLOBALS['conf']['files_public_path'] : "$conf_path/files";
  $settingsfile = "$conf_path/settings.php";
  $sitesfile = "sites/sites.php";
  $default = realpath($alias_record['root'] . '/sites/default');
  $sitesfile_write = drush_drupal_major_version() >= 8 && $conf_path != $default && !file_exists($sitesfile);

  if (!file_exists($settingsfile)) {
    $msg[] = dt('create a @settingsfile file', array('@settingsfile' => $settingsfile));
  }
  if ($sitesfile_write) {
    $msg[] = dt('create a @sitesfile file', array('@sitesfile' => $sitesfile));
  }
  if ($sql->db_exists()) {
    $msg[] = dt("DROP all tables in your '@db' database.", array('@db' => $db_spec['database']));
  }
  else {
    $msg[] = dt("CREATE the '@db' database.", array('@db' => $db_spec['database']));
  }

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

  // Can't install without sites subdirectory and settings.php.
  if (!file_exists($conf_path)) {
    if (!drush_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 (!drush_file_not_empty($settingsfile)) {
    if (!drush_op('copy', 'sites/default/default.settings.php', $settingsfile) && !drush_get_context('DRUSH_SIMULATE')) {
      return drush_set_error(dt('Failed to copy sites/default/default.settings.php to @settingsfile', array('@settingsfile' => $settingsfile)));
    }

    if (drush_drupal_major_version() == 6) {
      // On D6, we have to write $db_url ourselves. In D7+, the installer does it.
      file_put_contents($settingsfile, "\n" . '$db_url = \'' . drush_get_option('db-url') . "';\n", FILE_APPEND);
      // Instead of parsing and performing string replacement on the configuration file,
      // the options are appended and override the defaults.
      // Database table prefix
      if (!empty($db_spec['db_prefix'])) {
        if (is_array($db_spec['db_prefix'])) {
          // Write db_prefix configuration as an array
          $db_prefix_config = '$db_prefix = ' . var_export($db_spec['db_prefix'], TRUE) . ';';
        }
        else {
          // Write db_prefix configuration as a string
          $db_prefix_config = '$db_prefix = \'' . $db_spec['db_prefix'] . '\';';
        }
        file_put_contents($settingsfile, "\n" . $db_prefix_config . "\n", FILE_APPEND);
      }
    }
  }

  // Write an empty sites.php if we are on D8 and using multi-site.
  if ($sitesfile_write) {
    if (!drush_op('copy', 'sites/example.sites.php', $sitesfile) && !drush_get_context('DRUSH_SIMULATE')) {
      return drush_set_error(dt('Failed to copy sites/sites.php to @sitesfile', array('@sitesfile' => $sitesfile)));
    }
  }

  // We need to be at least at DRUSH_BOOTSTRAP_DRUPAL_SITE to select the site uri to install to
  define('MAINTENANCE_MODE', 'install');
  if (drush_drupal_major_version() == 6) {
    // The Drupal 6 installer needs to bootstrap up to the specified site.
    drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION);
  }
  else {
    drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_SITE);
  }

  $sql->drop_or_create($db_spec);

  if (drush_drupal_major_version() >= 8) {
    // Empty out any existing config directories.
    $directories = array();
    // Check for alternative format:
    // @code
    // $config_directories = array(
    //   'active' => 'config/active',
    //   'staging' => 'config/staging',
    // );
    // @endcode
    // @todo update this regex to omit path.
    // preg_match('/\n\$config_directories[^;]*\'path\' => \'([^\']*)\'.*\'path\' => \'([^\']*)\'/s', file_get_contents($settingsfile), $directories);

    if (empty($directories)) {
      // Check for canonical format:
      // @code
      // $config_directories['active'] = 'config/active';
      // $config_directories['staging'] = 'config/staging';
      // @endcode
      foreach (array('active', 'staging') as $type) {
        preg_match('/\n\$config_directories[^;]*\'' . $type . '\'\] = \'([^\']*)\'/', file_get_contents($settingsfile), $directory);
        if (isset($directory[1])) {
          $directories[$type] = $directory[1];
        }
      }
    }
    else {
      $directories['active'] = $files . '/' . $directories[1];
      $directories['staging'] = $files . '/' . $directories[2];
    }

    if (drush_get_option('keep-config', FALSE) == FALSE) {
      foreach ($directories as $directory) {
        if (file_exists($directory)) {
          drush_delete_dir_contents($directory, TRUE);
        }
      }
    }

    if (drush_get_option('writable')) {
      if (is_writable($settingsfile)) {
        $chmod = <<<'EOD'
// Appended by Drush site-install.
$settings['file_chmod_directory'] = 0777;
$settings['file_chmod_file'] = 0777;
EOD;
        drush_file_append_data($settingsfile, "\n$chmod\n");
      }
      else {
        drush_log(dt('Not appending file_chmod_* to @settingsfile as it is not writable.', array('@settingsfile' => $settingsfile)), LogLevel::NOTICE);
      }
    }


    // Remove files/php if needed.
    drush_delete_dir("$files/php", TRUE);
  }

  return TRUE;
}