<commit_before><?php

namespace Pagekit\Installer;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Pagekit\Application as App;
use Pagekit\Application\Exception;
use Pagekit\Config\Config;

/**
 * @Route("/installer")
 */
class InstallerController
{
    /**
     * @var bool
     */
    protected $config;

    /**
     * @var string
     */
    protected $configFile = 'config.php';

    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->config = file_exists($this->configFile);
    }

    /**
     * @Response("app/installer/views/install.php")
     */
    public function indexAction()
    {
        return [
            '$meta' => ['title' => 'Pagekit Installer'],
            'redirect' => App::url('/admin')
        ];
    }

    /**
     * @Request({"config": "array"})
     * @Response("json")
     */
    public function checkAction($config = [])
    {
        $status  = 'no-connection';
        $message = '';

        try {

            try {

                if (!$this->config) {
                    foreach ($config as $name => $values) {
                        if ($module = App::module($name)) {
                            $module->config = array_replace_recursive($module->config, $values);
                        }
                    }
                }

                App::db()->connect();

                if (App::db()->getUtility()->tableExists('@system_option')) {
                    $status  = 'tables-exist';
                    $message = __('Existing Pagekit installation detected. Choose different table prefix?');
                } else {
                    $status = 'no-tables';
                }

            } catch (\PDOException $e) {

                if ($e->getCode() == 1049) {
                    $this->createDatabase();
                    $status = 'no-tables';
                } else {
                    throw $e;
                }
            }

        } catch (\Exception $e) {

            $message = __('Database connection failed!');

            if ($e->getCode() == 1045) {
                $message = __('Database access denied!');
            }
        }

        return ['status' => $status, 'message' => $message];
    }

    /**
     * @Request({"config": "array", "option": "array", "user": "array"})
     * @Response("json")
     */
    public function installAction($config = [], $option = [], $user = [])
    {
        $status  = $this->checkAction($config, false);
        $message = $status['message'];
        $status  = $status['status'];

        try {

            if ('no-connection' == $status) {
                throw new Exception(__('No database connection.'));
            }

            if ('tables-exist' == $status) {
                throw new Exception($message);
            }

            App::option()->set('system:version', App::migrator()->create('app/modules/system/migrations')->run());
            App::option()->set('system:config', array_merge_recursive(App::option('system:config', []), ['extensions' => ['blog', 'page']]), true);

            App::db()->insert('@system_user', [
                'name'       => $user['username'],
                'username'   => $user['username'],
                'password'   => App::get('auth.password')->hash($user['password']),
                'status'     => 1,
                'email'      => $user['email'],
                'registered' => new \DateTime
            ], ['string', 'string', 'string', 'string', 'string', 'datetime']);

            $id = App::db()->lastInsertId();<commit_msg>updated installer<commit_after><?php

namespace Pagekit\Installer;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Pagekit\Application as App;
use Pagekit\Application\Exception;
use Pagekit\Config\Config;

/**
 * @Route("/installer")
 */
class InstallerController
{
    /**
     * @var bool
     */
    protected $config;

    /**
     * @var string
     */
    protected $configFile = 'config.php';

    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->config = file_exists($this->configFile);
    }

    /**
     * @Response("app/installer/views/install.php")
     */
    public function indexAction()
    {
        return [
            '$meta' => ['title' => 'Pagekit Installer'],
            'redirect' => App::url('/admin')
        ];
    }

    /**
     * @Request({"config": "array"})
     * @Response("json")
     */
    public function checkAction($config = [])
    {
        $status  = 'no-connection';
        $message = '';

        try {

            try {

                if (!$this->config) {
                    foreach ($config as $name => $values) {
                        if ($module = App::module($name)) {
                            $module->config = array_replace_recursive($module->config, $values);
                        }
                    }
                }

                App::db()->connect();

                if (App::db()->getUtility()->tableExists('@system_option')) {
                    $status  = 'tables-exist';
                    $message = __('Existing Pagekit installation detected. Choose different table prefix?');
                } else {
                    $status = 'no-tables';
                }

            } catch (\PDOException $e) {

                if ($e->getCode() == 1049) {
                    $this->createDatabase();
                    $status = 'no-tables';
                } else {
                    throw $e;
                }
            }

        } catch (\Exception $e) {

            $message = __('Database connection failed!');

            if ($e->getCode() == 1045) {
                $message = __('Database access denied!');
            }
        }

        return ['status' => $status, 'message' => $message];
    }

    /**
     * @Request({"config": "array", "option": "array", "user": "array"})
     * @Response("json")
     */
    public function installAction($config = [], $option = [], $user = [])
    {
        $status  = $this->checkAction($config, false);
        $message = $status['message'];
        $status  = $status['status'];

        try {

            if ('no-connection' == $status) {
                throw new Exception(__('No database connection.'));
            }

            if ('tables-exist' == $status) {
                throw new Exception($message);
            }

            App::option()->set('system:version', App::migrator()->create('app/system/migrations')->run());

            App::db()->insert('@system_user', [
                'name'       => $user['username'],
                'username'   => $user['username'],
                'password'   => App::get('auth.password')->hash($user['password']),
                'status'     => 1,
                'email'      => $user['email'],
                'registered' => new \DateTime
            ], ['string', 'string', 'string', 'string', 'string', 'datetime']);

            $id = App::db()->lastInsertId();
{'fraction_non_alphanumeric': 0.172644463421577, 'fraction_numerical': 0.0025619128949615714, 'mean_word_length': 1.5645985401459854, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 65, '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': '7474553', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1841, 'n_words': 354}