private function install_db()
{
$db_host = $this->handler_vars['db_host'];
$db_type = $this->handler_vars['db_type'];
$db_schema = $this->handler_vars['db_schema'];
$db_user = $this->handler_vars['db_user'];
$db_pass = $this->handler_vars['db_pass'];
switch ($db_type) {
case 'mysql':
case 'pgsql':
// MySQL & PostgreSQL requires specific connection information
if (empty($db_user)) {
$this->theme->assign('form_errors', array("{$db_type}_db_user" => _t('User is required.')));
return false;
}
if (empty($db_schema)) {
$this->theme->assign('form_errors', array("{$db_type}_db_schema" => _t('Name for database is required.')));
return false;
}
if (empty($db_host)) {
$this->theme->assign('form_errors', array("{$db_type}_db_host" => _t('Host is required.')));
return false;
}
break;
case 'sqlite':
// If this is a SQLite database, let's check that the file
// exists and that we can access it.
if (!$this->check_sqlite()) {
return false;
}
break;
}
if (isset($this->handler_vars['table_prefix'])) {
// store prefix in the Config singleton so DatabaseConnection can access it
Config::set('db_connection', array('prefix' => $this->handler_vars['table_prefix']));
}
if (!$this->connect_to_existing_db()) {
$this->theme->assign('form_errors', array("{$db_type}_db_user" => _t('Problem connecting to supplied database credentials')));
return false;
}
DB::begin_transaction();
/* Let's install the DB tables now. */
$create_table_queries = $this->get_create_table_queries($this->handler_vars['db_type'], $this->handler_vars['table_prefix'], $this->handler_vars['db_schema']);
DB::clear_errors();
DB::dbdelta($create_table_queries, true, true, true);
if (DB::has_errors()) {
$error = DB::get_last_error();
$this->theme->assign('form_errors', array('db_host' => _t('Could not create schema tables… %s', array($error['message']))));
DB::rollback();
return false;
}
// Cool. DB installed. Create the default options
// but check first, to make sure
if (!Options::get('installed')) {
if (!$this->create_default_options()) {
$this->theme->assign('form_errors', array('options' => _t('Problem creating default options')));
DB::rollback();
return false;
}
}
// Create the Tags vocabulary
if (!$this->create_tags_vocabulary()) {
$this->theme->assign('form_errors', array('options' => _t('Problem creating tags vocabulary')));
DB::rollback();
return false;
}
// Create the standard post types and statuses
if (!$this->create_base_post_types()) {
$this->theme->assign('form_errors', array('options' => _t('Problem creating base post types')));
DB::rollback();
return false;
}
if (!$this->create_base_comment_types()) {
$this->theme->assign('form_errors', array('options' => _t('Problem creating base comment types and statuses')));
DB::rollback();
return false;
}
// Let's setup the admin user and group now.
// But first, let's make sure that no users exist
$all_users = Users::get_all();
if (count($all_users) < 1) {
$user = $this->create_admin_user();
if (!$user) {
$this->theme->assign('form_errors', array('admin_user' => _t('Problem creating admin user.')));
DB::rollback();
return false;
}
$admin_group = $this->create_admin_group($user);
if (!$admin_group) {
$this->theme->assign('form_errors', array('admin_user' => _t('Problem creating admin group.')));
DB::rollback();
return false;
}
// create default tokens
ACL::rebuild_permissions($user);
}
// create a first post, if none exists
if (!Posts::get(array('count' => 1))) {
if (!$this->create_first_post()) {
$this->theme->assign('form_errors', array('post' => _t('Problem creating first post.')));
DB::rollback();
return false;
}
}
/* Post::save_tags() closes transaction, until we fix that, check and reconnect if needed */
if (!DB::in_transaction()) {
DB::begin_transaction();
}
/* Store current DB version so we don't immediately run dbdelta. */
Version::save_dbversion();
/* Ready to roll. */
DB::commit();
return true;
}