Piwik\Plugins\Installation\FormDatabaseSetup::createDatabaseObject PHP Method

createDatabaseObject() public method

Creates database object based on form data.
public createDatabaseObject ( ) : array
return array The database connection info. Can be passed into Piwik::createDatabaseObject.
    public function createDatabaseObject()
    {
        $dbname = $this->getSubmitValue('dbname');
        if (empty($dbname)) {
            throw new Exception("No database name");
        }
        $adapter = $this->getSubmitValue('adapter');
        $port = Adapter::getDefaultPortForAdapter($adapter);
        $dbInfos = array('host' => $this->getSubmitValue('host'), 'username' => $this->getSubmitValue('username'), 'password' => $this->getSubmitValue('password'), 'dbname' => $dbname, 'tables_prefix' => $this->getSubmitValue('tables_prefix'), 'adapter' => $adapter, 'port' => $port, 'schema' => Config::getInstance()->database['schema'], 'type' => $this->getSubmitValue('type'));
        if (($portIndex = strpos($dbInfos['host'], '/')) !== false) {
            // unix_socket=/path/sock.n
            $dbInfos['port'] = substr($dbInfos['host'], $portIndex);
            $dbInfos['host'] = '';
        } else {
            if (($portIndex = strpos($dbInfos['host'], ':')) !== false) {
                // host:port
                $dbInfos['port'] = substr($dbInfos['host'], $portIndex + 1);
                $dbInfos['host'] = substr($dbInfos['host'], 0, $portIndex);
            }
        }
        try {
            @Db::createDatabaseObject($dbInfos);
        } catch (Zend_Db_Adapter_Exception $e) {
            $db = Adapter::factory($adapter, $dbInfos, $connect = false);
            // database not found, we try to create  it
            if ($db->isErrNo($e, '1049')) {
                $dbInfosConnectOnly = $dbInfos;
                $dbInfosConnectOnly['dbname'] = null;
                @Db::createDatabaseObject($dbInfosConnectOnly);
                @DbHelper::createDatabase($dbInfos['dbname']);
                // select the newly created database
                @Db::createDatabaseObject($dbInfos);
            } else {
                throw $e;
            }
        }
        return $dbInfos;
    }

Usage Example

Example #1
0
 /**
  * Installation Step 3: Database Set-up
  * @throws Exception|Zend_Db_Adapter_Exception
  */
 function databaseSetup()
 {
     $this->checkPiwikIsNotInstalled();
     $view = new View('@Installation/databaseSetup', $this->getInstallationSteps(), __FUNCTION__);
     $view->showNextStep = false;
     $form = new FormDatabaseSetup();
     if ($form->validate()) {
         try {
             $dbInfos = $form->createDatabaseObject();
             DbHelper::checkDatabaseVersion();
             Db::get()->checkClientVersion();
             $this->createConfigFile($dbInfos);
             $this->redirectToNextStep(__FUNCTION__);
         } catch (Exception $e) {
             $view->errorMessage = Common::sanitizeInputValue($e->getMessage());
         }
     }
     $view->addForm($form);
     return $view->render();
 }
All Usage Examples Of Piwik\Plugins\Installation\FormDatabaseSetup::createDatabaseObject