} else { $driverOptions = $configDatabase->database->params->driver_options->toArray(); } $params = array('dbname' => $configDatabase->database->params->dbname, 'username' => $configDatabase->database->params->username, 'password' => $configDatabase->database->params->password, 'driver_options' => $driverOptions); if (empty($configDatabase->database->params->unix_socket)) { $params['host'] = $configDatabase->database->params->host; $params['port'] = $configDatabase->database->params->port; } else { $params['unix_socket'] = $configDatabase->database->params->unix_socket; } $db = Zend_Db::factory($configDatabase->database->adapter, $params); Zend_Db_Table::setDefaultAdapter($db); Zend_Registry::set('dbAdapter', $db); Zend_Registry::set('configDatabase', $configDatabase); require_once BASE_PATH . '/core/controllers/components/UpgradeComponent.php'; $upgradeComponent = new UpgradeComponent(); $db = Zend_Registry::get('dbAdapter'); $dbtype = Zend_Registry::get('configDatabase')->database->adapter; $upgradeComponent->initUpgrade('core', $db, $dbtype); $version = Zend_Registry::get('configDatabase')->version; if (!isset($version)) { if (Zend_Registry::get('configDatabase')->database->adapter === 'PDO_MYSQL') { $type = 'mysql'; } elseif (Zend_Registry::get('configDatabase')->database->adapter === 'PDO_PGSQL') { $type = 'pgsql'; } elseif (Zend_Registry::get('configDatabase')->database->adapter === 'PDO_SQLITE') { $type = 'sqlite'; } else { echo 'Error'; exit; }
/** * install a module. * * @param string $moduleName * @throws Zend_Exception */ public function installModule($moduleName) { // TODO, The module installation process needs some improvement. $allModules = $this->getAllModules(); $version = $allModules[$moduleName]->version; $installScript = BASE_PATH . '/modules/' . $moduleName . '/database/InstallScript.php'; $installScriptExists = file_exists($installScript); if ($installScriptExists) { require_once BASE_PATH . '/core/models/MIDASModuleInstallScript.php'; require_once $installScript; $classname = ucfirst($moduleName) . '_InstallScript'; if (!class_exists($classname, false)) { throw new Zend_Exception('Could not find class "' . $classname . '" in file "' . $installScript . '"'); } $class = new $classname(); $class->preInstall(); } try { $configDatabase = Zend_Registry::get('configDatabase'); $db = Zend_Registry::get('dbAdapter'); switch ($configDatabase->database->adapter) { case 'PDO_MYSQL': if (file_exists(BASE_PATH . '/modules/' . $moduleName . '/database/mysql/' . $version . '.sql')) { $this->run_sql_from_file($db, BASE_PATH . '/modules/' . $moduleName . '/database/mysql/' . $version . '.sql'); } break; case 'PDO_PGSQL': if (file_exists(BASE_PATH . '/modules/' . $moduleName . '/database/pgsql/' . $version . '.sql')) { $this->run_sql_from_file($db, BASE_PATH . '/modules/' . $moduleName . '/database/pgsql/' . $version . '.sql'); } break; case 'PDO_SQLITE': if (file_exists(BASE_PATH . '/modules/' . $moduleName . '/database/sqlite/' . $version . '.sql')) { $this->run_sql_from_file($db, BASE_PATH . '/modules/' . $moduleName . '/database/sqlite/' . $version . '.sql'); } break; default: break; } } catch (Zend_Exception $exc) { $this->getLogger()->warn($exc->getMessage()); } if ($installScriptExists) { $class->postInstall(); } require_once dirname(__FILE__) . '/UpgradeComponent.php'; $upgrade = new UpgradeComponent(); $db = Zend_Registry::get('dbAdapter'); $dbtype = Zend_Registry::get('configDatabase')->database->adapter; $upgrade->initUpgrade($moduleName, $db, $dbtype); $upgrade->upgrade($version); }
/** */ public function step2Action() { if (file_exists(LOCAL_CONFIGS_PATH . '/database.local.ini')) { $this->redirect('/install/step3'); } $this->view->header = 'Step 2: Database Configuration'; $databases = array('mysql', 'pgsql', 'sqlite'); $this->view->databaseType = array(); foreach ($databases as $database) { if (!extension_loaded('pdo_' . $database) || !file_exists(BASE_PATH . '/core/database/' . $database)) { unset($database); } else { $form = $this->Form->Install->createDBForm(); $host = $form->getElement('host'); $port = $form->getElement('port'); $username = $form->getElement('username'); switch ($database) { case 'mysql': $port->setValue('3306'); $username->setValue('root'); break; case 'pgsql': $port->setValue('5432'); $username->setValue('postgres'); break; case 'sqlite': $host->setValue(''); $port->setValue(''); $username->setValue(''); break; default: break; } $this->view->databaseType[$database] = $this->getFormAsArray($form); } } $this->view->basePath = BASE_PATH; if ($this->_request->isPost()) { $type = $this->getParam('type'); $form = $this->Form->Install->createDBForm(); if ($form->isValid($this->getRequest()->getPost())) { require_once BASE_PATH . '/core/controllers/components/UpgradeComponent.php'; $upgradeComponent = new UpgradeComponent(); $upgradeComponent->dir = BASE_PATH . '/core/database/' . $type; $upgradeComponent->init = true; $sqlFile = $upgradeComponent->getNewestVersion(true); $sqlFile = BASE_PATH . '/core/database/' . $type . '/' . $sqlFile . '.sql'; if (!isset($sqlFile) || !file_exists($sqlFile)) { throw new Zend_Exception('Unable to find sql file'); } $dbtype = 'PDO_' . strtoupper($type); $version = str_replace('.sql', '', basename($sqlFile)); $options = array('allowModifications' => true); $databaseConfig = new Zend_Config_Ini(CORE_CONFIGS_PATH . '/database.ini', null, $options); $databaseConfig->production->database->adapter = $dbtype; $databaseConfig->production->database->params->host = $form->getValue('host'); $databaseConfig->production->database->params->port = $form->getValue('port'); $databaseConfig->production->database->params->unix_socket = $form->getValue('unix_socket'); $databaseConfig->production->database->params->dbname = $form->getValue('dbname'); $databaseConfig->production->database->params->username = $form->getValue('username'); $databaseConfig->production->database->params->password = $form->getValue('password'); $databaseConfig->production->version = $version; $databaseConfig->development->database->adapter = $dbtype; $databaseConfig->development->database->params->host = $form->getValue('host'); $databaseConfig->development->database->params->port = $form->getValue('port'); $databaseConfig->development->database->params->unix_socket = $form->getValue('unix_socket'); $databaseConfig->development->database->params->dbname = $form->getValue('dbname'); $databaseConfig->development->database->params->username = $form->getValue('username'); $databaseConfig->development->database->params->password = $form->getValue('password'); $databaseConfig->development->version = $version; $writer = new Zend_Config_Writer_Ini(); $writer->setConfig($databaseConfig); $writer->setFilename(LOCAL_CONFIGS_PATH . '/database.local.ini'); $writer->write(); $driverOptions = array(); $params = array('dbname' => $form->getValue('dbname'), 'driver_options' => $driverOptions); if ($dbtype != 'PDO_SQLITE') { $params['username'] = $form->getValue('username'); $params['password'] = $form->getValue('password'); $unixsocket = $form->getValue('unix_socket'); if ($unixsocket) { $params['unix_socket'] = $unixsocket; } else { $params['host'] = $form->getValue('host'); $params['port'] = $form->getValue('port'); } } $db = Zend_Db::factory($dbtype, $params); Zend_Db_Table::setDefaultAdapter($db); Zend_Registry::set('dbAdapter', $db); $this->Component->Utility->run_sql_from_file($db, $sqlFile); // Must generate and store our password salt before we create our first user $options = array('allowModifications' => true); $applicationConfig = new Zend_Config_Ini(CORE_CONFIGS_PATH . '/application.ini', null, $options); $prefix = $this->Component->Random->generateString(32); $applicationConfig->global->password->prefix = $prefix; $applicationConfig->global->gravatar = $form->getValue('gravatar'); $writer = new Zend_Config_Writer_Ini(); $writer->setConfig($applicationConfig); $writer->setFilename(LOCAL_CONFIGS_PATH . '/application.local.ini'); $writer->write(); $configGlobal = new Zend_Config_Ini(LOCAL_CONFIGS_PATH . '/application.local.ini', 'global'); Zend_Registry::set('configGlobal', $configGlobal); $configDatabase = new Zend_Config_Ini(LOCAL_CONFIGS_PATH . '/database.local.ini', 'production'); Zend_Registry::set('configDatabase', $configDatabase); require_once BASE_PATH . '/core/controllers/components/UpgradeComponent.php'; $upgradeComponent = new UpgradeComponent(); $upgradeComponent->initUpgrade('core', $db, $dbtype); $upgradeComponent->upgrade(str_replace('.sql', '', basename($sqlFile))); session_start(); require_once BASE_PATH . '/core/models/pdo/UserModel.php'; $userModel = new UserModel(); $this->userSession->Dao = $userModel->createUser($form->getValue('email'), $form->getValue('userpassword1'), $form->getValue('firstname'), $form->getValue('lastname'), 1); // create default assetstore $assetstoreDao = new AssetstoreDao(); $assetstoreDao->setName('Local'); $assetstoreDao->setPath($this->getDataDirectory('assetstore')); $assetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL); $this->Assetstore = new AssetstoreModel(); // reset Database adapter $this->Assetstore->save($assetstoreDao); $this->redirect('/install/step3'); } } }
/** * Install and upgrade core. * * @param Zend_Db_Adapter_Abstract $db * @param string $dbType * @param UtilityComponent $utilityComponent * @throws Zend_Exception */ function installCore($db, $dbType, $utilityComponent) { require_once BASE_PATH . '/core/controllers/components/UpgradeComponent.php'; $upgradeComponent = new UpgradeComponent(); $upgradeComponent->dir = BASE_PATH . '/core/database/' . $dbType; $upgradeComponent->init = true; $newestVersion = $upgradeComponent->getNewestVersion(true); $sqlFile = BASE_PATH . '/core/database/' . $dbType . '/' . $newestVersion . '.sql'; if (!isset($sqlFile) || !file_exists($sqlFile)) { throw new Zend_Exception('Unable to find SQL file: ' . $sqlFile); } switch ($dbType) { case 'mysql': $utilityComponent->run_sql_from_file($db, $sqlFile); $upgradeDbType = 'PDO_MYSQL'; break; case 'pgsql': $utilityComponent->run_sql_from_file($db, $sqlFile); $upgradeDbType = 'PDO_PGSQL'; break; case 'sqlite': $utilityComponent->run_sql_from_file($db, $sqlFile); $upgradeDbType = 'PDO_SQLITE'; break; default: throw new Zend_Exception('Unknown database type: ' . $dbType); break; } $options = array('allowModifications' => true); $databaseConfig = new Zend_Config_Ini(BASE_PATH . '/tests/configs/lock.' . $dbType . '.ini', null, $options); $databaseConfig->testing->version = $newestVersion; $writer = new Zend_Config_Writer_Ini(); $writer->setConfig($databaseConfig); $writer->setFilename(BASE_PATH . '/tests/configs/lock.' . $dbType . '.ini'); $writer->write(); $upgradeComponent->initUpgrade('core', $db, $upgradeDbType); $upgradeComponent->upgrade(str_replace('.sql', '', basename($sqlFile)), true); }