<commit_before><?php

require_once(AK_LIB_DIR.DS.'AkLogger.php');

class FrameworkSetup extends AkObject
{
    var $available_databases = array(
    'mysql' => 'MySQL',
    'pgsql' => 'PostgreSQL',
    'sqlite' => 'SQLite'
    );
    var $available_locales = array('en', 'es');
    var $locales = array('en');

    var $stylesheets = array('scaffold','forms');

    function __construct()
    {
        if(file_exists(AK_CONFIG_DIR.DS.'config.php')){
            echo Ak::t('The framework_setup.php found that you already have a configuration file at config/config.php. You need to remove that file first in order to run the setup.', array(), 'framework_setup');
            die();
        }
    }



    /**
     * Will try to guess the best database on current server.
     * Per example, if current server has MySQL and PostgreSQL it will pick up
     * PostgreSQL if MySQL doens't support InnoDB tables.
     * 
     * If none of the above is
     *
     */
    function suggestDatabaseType()
    {
        /**
         * @todo add database check for postgre
         */
        return $this->_suggestMysql() ? 'mysql' : (function_exists('pg_connect') ? 'pgsql' : (AK_PHP5 ? 'sqlite' : 'mysql'));
    }

    function _suggestMysql()
    {
        if(function_exists('mysql_connect')){
            if($db = @mysql_connect(   $this->getDatabaseHost(),
            $this->getDatabaseUser(),
            $this->getDatabasePassword())){
                return true;
                /**
                 * @todo Checking if innodb is available break on some systems
                 * we should investigate a better way to do this
                 */
                /*
                $result = mysql_query($db, "SHOW VARIABLES LIKE 'have_innodb';");
                return strstr(
                strtolower(
                @array_pop(
                @mysql_fetch_row(
                @mysql_query("SHOW VARIABLES LIKE 'have_innodb';", $db)
                )
                )
                ),
                'yes');
                */
            }
        }
        return false;
    }

    function createDatabase($mode)
    {
        $success = true;

        $db = $this->databaseConnection('admin');

        if($db){
            if($this->getDatabaseType($mode) != 'sqlite'){
                $DataDict = NewDataDictionary($db);
                if($this->getDatabaseType($mode) == 'mysql'){
                    $success = $this->_createMysqlDatabase($db, $mode) ? $success : false;
                }
            }
            return $success;
        }
        return false;
    }

    function _createMysqlDatabase(&$db, $mode)
    {
        $success = true;
        $success = $db->Execute('CREATE DATABASE '.$this->getDatabaseName($mode)) ? $success : false;
        $success = $db->Execute("GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON ".
        $this->getDatabaseName($mode).".* TO '".$this->getDatabaseUser($mode)."'@'".
        $this->getDatabaseHost($mode)."' IDENTIFIED BY '".$this->getDatabasePassword($mode)."'") ? $success : false;
        return $success;
    }

    function getDatabaseHost($mode = '')
    {
        return !isset($this->{$mode.'_database_host'}) ? $this->suggestDatabaseHost() : $this->{$mode.'_database_host'};
    }

    function getDatabaseUser($mode = '')
    {
        return !isset($this->{$mode.'_database_user'}) ? $this->suggestUserName() : $this->{$mode.'_database_user'};
    }

    function getDatabasePassword($mode = '')
    {
        return !isset($this->{$mode.'_database_password'}) ? '' : $this->{$mode.'_database_password'};
    }

    function getDatabaseType()
    {
        return !isset($this->database_type) ? $this->suggestDatabaseType() : $this->database_type;
    }

    function setDatabaseType($database_type)
    {
        $database_type = strtolower($database_type);
        if(!in_array($database_type, array_keys($this->available_databases))){
            trigger_error(Ak::t('Selected database is not supported yet by the Akelos Framework',array(),'framework_setup'));
        }elseif(!$this->isDatabaseDriverAvalible($database_type)){
            trigger_error(Ak::t('Could not set %database_type as database type. Your current PHP settings do not support %database_type databases', array('%database_type '=>$database_type), 'framework_setup'));
        }else{
            $this->database_type = $database_type;
            return $this->database_type;
        }
        return false;
    }

    function getDatabaseName($mode)
    {
        return !isset($this->{$mode.'_database_name'}) ?
        $this->guessApplicationName().($mode=='development'?'_dev':($mode=='testing'?'_tests':'')) :
        $this->{$mode.'_database_name'};
    }

    function setDatabaseName($database_name, $mode)
    {
        $this->{$mode.'_database_name'} = $database_name;
    }

    function setDatabaseHost($host, $mode)
    {
        $this->{$mode.'_database_host'} = $host;
    }

    function setDatabaseUser($user, $mode)
    {
        $this->{$mode.'_database_user'} = $user;
    }

    function setDatabasePassword($password, $mode)
    {
        $this->{$mode.'_database_password'} = $password;
    }


    function getDatabaseAdminUser()
    {
        return !isset($this->admin_database_user) ? 'root' : $this->admin_database_user;
    }

    function getDatabaseAdminPassword()
    {
        return !isset($this->admin_database_password) ? '' : $this->admin_database_password;
    }


    function setDatabaseAdminUser($user)
    {
        $this->admin_database_user = $user;
    }

    function setDatabaseAdminPassword($password)
    {
        $this->admin_database_password = $password;
    }


    function databaseConnection($mode)
    {
        static $connections = array();
        require_once(AK_CONTRIB_DIR.DS.'adodb'.DS.'adodb.inc.php');

        $dsn = $this->_getDsn($mode);
        if(!isset($connections[$dsn])){
            if(!$connections[$dsn] = @NewADOConnection($dsn)){
                return false;
            }
        }
        return $connections[$dsn];
    }

    function _getDsn($mode)
    {
        if($mode == 'admin'){
            $db_type = $this->getDatabaseType('production');
            return $db_type.'://'.
            $this->getDatabaseAdminUser().':'.
            $this->getDatabaseAdminPassword().'@'.$this->getDatabaseHost('production').($db_type == 'mysql' ? '/mysql' : '');
        }else{
            return $this->getDatabaseType() == 'sqlite' ?
            "sqlite://".urlencode(AK_CONFIG_DIR.DS.$this->getDatabaseName($mode).'-'.$this->random.'.sqlite') :
            $this->getDatabaseType($mode)."://".$this->getDatabaseUser($mode).":".$this->getDatabasePassword($mode).
            "@".$this->getDatabaseHost($mode)."/".$this->getDatabaseName($mode);
        }
    }


    function getAvailableDatabases()
    {
        $databases = array();
        foreach ($this->available_databases as $type=>$description){
            if($this->isDatabaseDriverAvalible($type)){
                $databases[] = array('type' => $type, 'name' => $description);
            }

        }
        return $databases;
    }


    function isDatabaseDriverAvalible($database_type = null)
    {
        $database_type = empty($database_type) ? $this->getDatabaseType() : $database_type;
        if(strstr($database_type,'mysql')){
            $function = 'mysql_connect';
        }elseif (strstr($database_type,'pg') || strstr($database_type,'gre')){
            $function = 'pg_connect';
        }elseif (strstr($database_type,'lite')){
            $function = 'sqlite_open';
        }else{
            $function = $database_type.'_connect';
        }
        return function_exists($function);
    }

    function runFrameworkInstaller()
    {
        static $unique_dsn = array();
        require_once(AK_LIB_DIR.DS.'AkInstaller.php');
        require_once(AK_APP_DIR.DS.'installers'.DS.'framework_installer.php');

        foreach (array('production', 'development') as $mode){
            $dsn = $this->_getDsn($mode);
            if(!isset($unique_dsn[$dsn])){
                $DbInstance =& AkDbAdapter::getInstance(array(
                'type' => $this->getDatabaseType($mode),
                'file' => AK_CONFIG_DIR.DS.$this->getDatabaseName($mode).'-'.$this->random.'.sqlite',
                'user' => $this->getDatabaseUser($mode),
                'password' => $this->getDatabasePassword($mode),
                'host' => $this->getDatabaseHost($mode),
                'database_name' => $this->getDatabaseName($mode)
                ));

                $installer =& new FrameworkInstaller($DbInstance);
                $installer->install(null, array('mode' => $mode));
                $unique_dsn[$dsn] = true;
            }
        }

        return true;
    }




    function getUrlSuffix()
    {
        return empty($this->url_suffix) ? AK_SITE_URL_SUFFIX : $this->url_suffix;
    }<commit_msg>Changing the old 'file' index for sqlite BD selection to the new 'database_file' enable sqlite installation via the web installer.<commit_after><?php

require_once(AK_LIB_DIR.DS.'AkLogger.php');

class FrameworkSetup extends AkObject
{
    var $available_databases = array(
    'mysql' => 'MySQL',
    'pgsql' => 'PostgreSQL',
    'sqlite' => 'SQLite'
    );
    var $available_locales = array('en', 'es');
    var $locales = array('en');

    var $stylesheets = array('scaffold','forms');

    function __construct()
    {
        if(file_exists(AK_CONFIG_DIR.DS.'config.php')){
            echo Ak::t('The framework_setup.php found that you already have a configuration file at config/config.php. You need to remove that file first in order to run the setup.', array(), 'framework_setup');
            die();
        }
    }



    /**
     * Will try to guess the best database on current server.
     * Per example, if current server has MySQL and PostgreSQL it will pick up
     * PostgreSQL if MySQL doens't support InnoDB tables.
     * 
     * If none of the above is
     *
     */
    function suggestDatabaseType()
    {
        /**
         * @todo add database check for postgre
         */
        return $this->_suggestMysql() ? 'mysql' : (function_exists('pg_connect') ? 'pgsql' : (AK_PHP5 ? 'sqlite' : 'mysql'));
    }

    function _suggestMysql()
    {
        if(function_exists('mysql_connect')){
            if($db = @mysql_connect(   $this->getDatabaseHost(),
            $this->getDatabaseUser(),
            $this->getDatabasePassword())){
                return true;
                /**
                 * @todo Checking if innodb is available break on some systems
                 * we should investigate a better way to do this
                 */
                /*
                $result = mysql_query($db, "SHOW VARIABLES LIKE 'have_innodb';");
                return strstr(
                strtolower(
                @array_pop(
                @mysql_fetch_row(
                @mysql_query("SHOW VARIABLES LIKE 'have_innodb';", $db)
                )
                )
                ),
                'yes');
                */
            }
        }
        return false;
    }

    function createDatabase($mode)
    {
        $success = true;

        $db = $this->databaseConnection('admin');

        if($db){
            if($this->getDatabaseType($mode) != 'sqlite'){
                $DataDict = NewDataDictionary($db);
                if($this->getDatabaseType($mode) == 'mysql'){
                    $success = $this->_createMysqlDatabase($db, $mode) ? $success : false;
                }
            }
            return $success;
        }
        return false;
    }

    function _createMysqlDatabase(&$db, $mode)
    {
        $success = true;
        $success = $db->Execute('CREATE DATABASE '.$this->getDatabaseName($mode)) ? $success : false;
        $success = $db->Execute("GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON ".
        $this->getDatabaseName($mode).".* TO '".$this->getDatabaseUser($mode)."'@'".
        $this->getDatabaseHost($mode)."' IDENTIFIED BY '".$this->getDatabasePassword($mode)."'") ? $success : false;
        return $success;
    }

    function getDatabaseHost($mode = '')
    {
        return !isset($this->{$mode.'_database_host'}) ? $this->suggestDatabaseHost() : $this->{$mode.'_database_host'};
    }

    function getDatabaseUser($mode = '')
    {
        return !isset($this->{$mode.'_database_user'}) ? $this->suggestUserName() : $this->{$mode.'_database_user'};
    }

    function getDatabasePassword($mode = '')
    {
        return !isset($this->{$mode.'_database_password'}) ? '' : $this->{$mode.'_database_password'};
    }

    function getDatabaseType()
    {
        return !isset($this->database_type) ? $this->suggestDatabaseType() : $this->database_type;
    }

    function setDatabaseType($database_type)
    {
        $database_type = strtolower($database_type);
        if(!in_array($database_type, array_keys($this->available_databases))){
            trigger_error(Ak::t('Selected database is not supported yet by the Akelos Framework',array(),'framework_setup'));
        }elseif(!$this->isDatabaseDriverAvalible($database_type)){
            trigger_error(Ak::t('Could not set %database_type as database type. Your current PHP settings do not support %database_type databases', array('%database_type '=>$database_type), 'framework_setup'));
        }else{
            $this->database_type = $database_type;
            return $this->database_type;
        }
        return false;
    }

    function getDatabaseName($mode)
    {
        return !isset($this->{$mode.'_database_name'}) ?
        $this->guessApplicationName().($mode=='development'?'_dev':($mode=='testing'?'_tests':'')) :
        $this->{$mode.'_database_name'};
    }

    function setDatabaseName($database_name, $mode)
    {
        $this->{$mode.'_database_name'} = $database_name;
    }

    function setDatabaseHost($host, $mode)
    {
        $this->{$mode.'_database_host'} = $host;
    }

    function setDatabaseUser($user, $mode)
    {
        $this->{$mode.'_database_user'} = $user;
    }

    function setDatabasePassword($password, $mode)
    {
        $this->{$mode.'_database_password'} = $password;
    }


    function getDatabaseAdminUser()
    {
        return !isset($this->admin_database_user) ? 'root' : $this->admin_database_user;
    }

    function getDatabaseAdminPassword()
    {
        return !isset($this->admin_database_password) ? '' : $this->admin_database_password;
    }


    function setDatabaseAdminUser($user)
    {
        $this->admin_database_user = $user;
    }

    function setDatabaseAdminPassword($password)
    {
        $this->admin_database_password = $password;
    }


    function databaseConnection($mode)
    {
        static $connections = array();
        require_once(AK_CONTRIB_DIR.DS.'adodb'.DS.'adodb.inc.php');

        $dsn = $this->_getDsn($mode);
        if(!isset($connections[$dsn])){
            if(!$connections[$dsn] = @NewADOConnection($dsn)){
                return false;
            }
        }
        return $connections[$dsn];
    }

    function _getDsn($mode)
    {
        if($mode == 'admin'){
            $db_type = $this->getDatabaseType('production');
            return $db_type.'://'.
            $this->getDatabaseAdminUser().':'.
            $this->getDatabaseAdminPassword().'@'.$this->getDatabaseHost('production').($db_type == 'mysql' ? '/mysql' : '');
        }else{
            return $this->getDatabaseType() == 'sqlite' ?
            "sqlite://".urlencode(AK_CONFIG_DIR.DS.$this->getDatabaseName($mode).'-'.$this->random.'.sqlite') :
            $this->getDatabaseType($mode)."://".$this->getDatabaseUser($mode).":".$this->getDatabasePassword($mode).
            "@".$this->getDatabaseHost($mode)."/".$this->getDatabaseName($mode);
        }
    }


    function getAvailableDatabases()
    {
        $databases = array();
        foreach ($this->available_databases as $type=>$description){
            if($this->isDatabaseDriverAvalible($type)){
                $databases[] = array('type' => $type, 'name' => $description);
            }

        }
        return $databases;
    }


    function isDatabaseDriverAvalible($database_type = null)
    {
        $database_type = empty($database_type) ? $this->getDatabaseType() : $database_type;
        if(strstr($database_type,'mysql')){
            $function = 'mysql_connect';
        }elseif (strstr($database_type,'pg') || strstr($database_type,'gre')){
            $function = 'pg_connect';
        }elseif (strstr($database_type,'lite')){
            $function = 'sqlite_open';
        }else{
            $function = $database_type.'_connect';
        }
        return function_exists($function);
    }

    function runFrameworkInstaller()
    {
        static $unique_dsn = array();
        require_once(AK_LIB_DIR.DS.'AkInstaller.php');
        require_once(AK_APP_DIR.DS.'installers'.DS.'framework_installer.php');

        foreach (array('production', 'development') as $mode){
            $dsn = $this->_getDsn($mode);
            if(!isset($unique_dsn[$dsn])){
                $DbInstance =& AkDbAdapter::getInstance(array(
                'type' => $this->getDatabaseType($mode),
                'database_file' => AK_CONFIG_DIR.DS.$this->getDatabaseName($mode).'-'.$this->random.'.sqlite',
                'user' => $this->getDatabaseUser($mode),
                'password' => $<PASSWORD>($<PASSWORD>),
                'host' => $this->getDatabaseHost($mode),
                'database_name' => $this->getDatabaseName($mode)
                ));

                $installer =& new FrameworkInstaller($DbInstance);
                $installer->install(null, array('mode' => $mode));
                $unique_dsn[$dsn] = true;
            }
        }

        return true;
    }




    function getUrlSuffix()
    {
        return empty($this->url_suffix) ? AK_SITE_URL_SUFFIX : $this->url_suffix;
    }
{'fraction_non_alphanumeric': 0.15496246872393662, 'fraction_numerical': 0.00011120378092855157, 'mean_word_length': 2.330740740740741, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 172, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7532465', 'n_tokens_mistral': 5395, 'n_tokens_neox': 5159, 'n_words': 889}