Browse Source

Adding chamilo:status, chamilo:upgrade and chamilo:install command lines see BT#5572

Julio Montoya 12 years ago
parent
commit
93d934c320

+ 282 - 0
src/ChamiloLMS/Command/Database/InstallCommand.php

@@ -0,0 +1,282 @@
+<?php
+
+namespace ChamiloLMS\Command\Database;
+
+use Symfony\Component\Config\Definition\Exception\Exception;
+use Symfony\Component\Console\Input\InputArgument,
+    Symfony\Component\Console\Input\InputOption,
+    Symfony\Component\Console;
+use Symfony\Component\Console\Input\ArrayInput;
+use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand;
+use Doctrine\Common\Annotations\AnnotationReader;
+use Doctrine\Common\Annotations\AnnotationRegistry;
+use Symfony\Component\Yaml\Dumper;
+
+/**
+ * Class MigrationCommand
+ */
+class InstallCommand extends AbstractCommand
+{
+    protected function configure()
+    {
+        $this
+            ->setName('chamilo:install')
+            ->setDescription('Execute a Chamilo installation to a specified version')
+            ->addArgument('version', InputArgument::OPTIONAL, 'The version to migrate to.', null)
+            ->addOption('path', null, InputOption::VALUE_OPTIONAL, 'The path to the chamilo folder');
+    }
+
+    /**
+     * Gets the configuration folder
+     *
+     * @return string
+     */
+    public function getConfigurationFile()
+    {
+        return api_get_path(SYS_PATH).'main/inc/conf/';
+    }
+
+    /**
+    * Gets the installation version path
+    *
+    * @param string $version
+    *
+    * @return string
+    */
+    public function getInstallationPath($version)
+    {
+        return api_get_path(SYS_PATH).'main/install/'.$version.'/';
+    }
+
+
+    /**
+     * Executes a command via CLI
+     *
+     * @param Console\Input\InputInterface $input
+     * @param Console\Output\OutputInterface $output
+     *
+     * @return int|null|void
+     */
+    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
+    {
+        $configurationPath = $this->getConfigurationFile();
+
+        $dialog = $this->getHelperSet()->get('dialog');
+
+        $version = $input->getArgument('version');
+        $defaultVersion = $this->getLatestVersion();
+
+        if (empty($version)) {
+            $version = $defaultVersion;
+        }
+
+        $output->writeln("<comment>Welcome to the Chamilo installation process!</comment>");
+
+        if (!is_writable($configurationPath)) {
+            $output->writeln("<comment>Folder ".$configurationPath." must be writable</comment>");
+        }
+
+        $sqlFolder = $this->getInstallationPath($version);
+
+        if (!is_dir($sqlFolder)) {
+            $output->writeln("<comment>Sorry you can't install Chamilo :( Installation files for version $version does not exists: </comment><info>".$sqlFolder);
+
+            return false;
+        }
+
+        /*if (!$dialog->askConfirmation(
+            $output,
+            '<comment>You are about to install Chamilo </comment><info>$version</info> <comment>here:</comment>'.$configurationPath.'</info> <question>Are you sure?</question>(y/N)',
+            false
+        )
+        ) {
+            return;
+        }*/
+
+        /*
+        if (file_exists($configurationPath.'configuration.php') || file_exists($configurationPath.'configuration.yml')) {
+            if (!$dialog->askConfirmation(
+                $output,
+                '<question>There is a Chamilo installation located here:</question> '.$configurationPath.' <question>Are you sure you want to continue?</question>(y/N)',
+                false
+            )
+            ) {
+                return;
+            }
+        }*/
+
+        //Getting default configuration parameters
+        require_once api_get_path(SYS_PATH).'main/install/configuration.dist.yml.php';
+
+        $avoidVariables = array(
+            'db_glue',
+            'code_append',
+            'course_folder',
+            'db_admin_path',
+            'cdn_enable',
+            'verbose_backup',
+            'session_stored_in_db',
+            'session_lifetime',
+            'software_name',
+            'software_url',
+            'deny_delete_users',
+            'system_version',
+            'system_stable'
+        );
+
+        $newConfigurationArray = array();
+        foreach ($_configuration as $key => $value) {
+            if (in_array($key, $avoidVariables)) {
+                $newConfigurationArray[$key] = $value;
+                continue;
+            }
+            if (!is_array($value)) {
+                $data = $dialog->ask(
+                    $output,
+                    "Please enter the value of the $key ($value): ",
+                    $value
+                );
+                $newConfigurationArray[$key] = $data;
+            } else {
+                $newConfigurationArray[$key] = $value;
+            }
+        }
+
+        //Installing database
+        $result = $this->install($version, $newConfigurationArray, $output);
+
+        if ($result) {
+            $this->createAdminUser($newConfigurationArray, $output);
+            $this->writeConfiguration($newConfigurationArray, $version);
+            $output->writeln("<comment>Database installation finished!</comment>");
+        }
+    }
+
+    /**
+     *
+     * @param $newConfigurationArray
+     * @param $output
+     * @return bool
+     */
+    public function createAdminUser($newConfigurationArray, $output)
+    {
+        $dialog = $this->getHelperSet()->get('dialog');
+
+        //Creating admin user
+        $output->writeln("<comment>Chamilo was successfully installed visit: </comment> <info>".$newConfigurationArray['root_web']);
+
+        $adminUser = array(
+            'lastname' => 'Julio',
+            'firstname' => 'M',
+            'username' => 'admin',
+            'password' => 'admin',
+            'email' => 'admin@example.org'
+        );
+
+        $output->writeln("<comment>Creating an admin User</comment>");
+        $userInfo = array();
+        foreach ($adminUser as $key => $value) {
+            $data = $dialog->ask(
+                $output,
+                "Please enter the $key ($value): ",
+                $value
+            );
+            $userInfo[$key] = $data;
+        }
+        $userInfo = \UserManager::add($userInfo);
+        if ($userInfo && isset($userInfo['user_id'])) {
+            $userId = $userInfo['user_id'];
+            \UserManager::add_user_as_admin($userInfo['user_id']);
+            $output->writeln("<comment>User admin created with id: $userId</comment>");
+
+            return true;
+        }
+
+        return false;
+    }
+
+    /**
+     * @return string
+     */
+    public function getLatestVersion()
+    {
+        return '1.10';
+    }
+
+    /**
+     * Writes the configuration file a yml file
+     * @param $newConfigurationArray
+     * @param $version
+     */
+    public function writeConfiguration($newConfigurationArray, $version)
+    {
+        $configurationPath = $this->getConfigurationFile();
+
+        $newConfigurationArray['system_version'] = $version;
+        $dumper = new Dumper();
+        $yaml = $dumper->dump($newConfigurationArray, 2); //inline
+        $newConfigurationFile = $configurationPath.'configuration.yml';
+        file_put_contents($newConfigurationFile, $yaml);
+    }
+
+    /**
+     * Installs Chamilo
+     *
+     * @param string $version
+     * @param array $_configuration
+     * @param $output
+     * @return bool
+     */
+    public function install($version, $_configuration, $output)
+    {
+        $sqlFolder = $this->getInstallationPath($version);
+        $output->writeln("<comment>Creating database ... </comment>");
+
+        $result = $this->createDatabase($_configuration);
+
+        //Importing files
+        if ($result) {
+            $command = $this->getApplication()->find('dbal:import');
+
+            $arguments = array(
+                'command' => 'dbal:import',
+                'file' => array(
+                    $sqlFolder.'db_main.sql',
+                    $sqlFolder.'db_stats.sql',
+                    $sqlFolder.'db_user.sql',
+                    $sqlFolder.'db_course.sql',
+                )
+            );
+            $input = new ArrayInput($arguments);
+            $command->run($input, $output);
+
+            //Getting extra information about the installation
+            $result = \Database::query("SELECT selected_value FROM ".\Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT)." WHERE variable = 'chamilo_database_version'");
+            $result = \Database::fetch_array($result);
+
+            $output->writeln("<comment>Showing chamilo_database_version value:</comment> ".$result['selected_value']);
+
+            return true;
+        }
+    }
+
+    /**
+     * Creates a Database
+     * @todo use doctrine?
+     *
+     * @return resource
+     */
+    public function createDatabase($_configuration)
+    {
+        /*
+        $command = $this->getApplication()->find('orm:schema-tool:create');
+        $arguments = array(
+            'command' => 'orm:schema-tool:create',
+        );
+        $input     = new ArrayInput($arguments);
+        $command->run($input, $output);
+        exit;
+        */
+        return  \Database::query("CREATE DATABASE IF NOT EXISTS ".mysql_real_escape_string($_configuration['main_database'])." DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci");
+    }
+}

+ 58 - 0
src/ChamiloLMS/Command/Database/StatusCommand.php

@@ -0,0 +1,58 @@
+<?php
+
+namespace ChamiloLMS\Command\Database;
+
+use Symfony\Component\Config\Definition\Exception\Exception;
+use Symfony\Component\Console\Input\InputArgument,
+    Symfony\Component\Console\Input\InputOption,
+    Symfony\Component\Console;
+use Symfony\Component\Console\Input\ArrayInput;
+use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand;
+
+/**
+ * Class MigrationCommand
+ */
+class StatusCommand extends AbstractCommand
+{
+    protected function configure()
+    {
+        $this
+            ->setName('chamilo:status')
+            ->setDescription('Show the information of the current Chamilo installation')
+            ->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.');
+    }
+
+
+    /**
+     * Executes a command via CLI
+     *
+     * @param Console\Input\InputInterface $input
+     * @param Console\Output\OutputInterface $output
+     * @return int|null|void
+     */
+    protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
+    {
+        global $_configuration;
+
+        if (!isset($_configuration['root_sys'])) {
+            $output->writeln("<comment>Chamilo is not installed here!</comment>");
+            exit;
+        }
+
+        $configurationPath = api_get_path(SYS_PATH).'main/inc/conf/';
+
+        $query = "SELECT selected_value FROM settings_current WHERE variable = 'chamilo_database_version'";
+        $conn = $this->getHelper('main_database')->getConnection();
+        $data = $conn->executeQuery($query);
+        $data = $data->fetch();
+
+        $chamiloVersion = $data['selected_value'];
+        $output->writeln('<comment>Chamilo status</comment>');
+        $output->writeln("<comment>Chamilo configuration path:</comment> <info>".$configurationPath."</info>");
+
+        $output->writeln('<comment>Chamilo $_configuration[system_version]:</comment> <info>'.$_configuration['system_version'].'</info>');
+        $output->writeln("<comment>Chamilo setting: 'chamilo_database_version':</comment> <info>".$chamiloVersion."</info>");
+    }
+
+}
+

+ 92 - 20
src/ChamiloLMS/Command/Database/MigrationCommand.php → src/ChamiloLMS/Command/Database/UpgradeCommand.php

@@ -13,23 +13,31 @@ use Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand;
 /**
  * Class MigrationCommand
  */
-class MigrationCommand extends AbstractCommand
+class UpgradeCommand extends AbstractCommand
 {
     protected function configure()
     {
         $this
-            ->setName('migrations:migrate_chamilo')
+            ->setName('chamilo:upgrade')
             ->setDescription('Execute a chamilo migration to a specified version or the latest available version.')
             ->addArgument('version', InputArgument::REQUIRED, 'The version to migrate to.', null)
             ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the migration as a dry run.')
             ->addOption('configuration', null, InputOption::VALUE_OPTIONAL, 'The path to a migrations configuration file.');
     }
 
+    /**
+     * Gets the min version available to migrate
+     * @return mixed
+     */
     public function getMinVersionSupportedByInstall()
     {
         return key($this->availableVersions());
     }
 
+    /**
+     * Gets an array with the supported Chamilo versions to migrate
+     * @return array
+     */
     public function getVersionNumberList()
     {
         $versionList = $this->availableVersions();
@@ -37,9 +45,15 @@ class MigrationCommand extends AbstractCommand
         foreach ($versionList as $version => $info) {
             $versionNumberList[] = $version;
         }
+
         return $versionNumberList;
     }
 
+    /**
+     * Gets an array with the settings for every supported version
+     *
+     * @return array
+     */
     public function availableVersions()
     {
         $versionList = array(
@@ -72,6 +86,14 @@ class MigrationCommand extends AbstractCommand
         return $versionList;
     }
 
+    /**
+     *
+     * Gets the content of a version from the available versions
+     *
+     * @param $version
+     *
+     * @return bool
+     */
     public function getAvailableVersionInfo($version)
     {
         $versionList = $this->availableVersions();
@@ -83,8 +105,35 @@ class MigrationCommand extends AbstractCommand
         return false;
     }
 
+    /**
+     * Executes a command via CLI
+     *
+     * @param Console\Input\InputInterface $input
+     * @param Console\Output\OutputInterface $output
+     * @return int|null|void
+     */
     protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
     {
+        global $_configuration;
+
+        if (!isset($_configuration['root_sys'])) {
+            $output->writeln("<comment>Chamilo is not installed here!</comment>");
+            exit;
+        }
+
+        $configurationPath = api_get_path(SYS_PATH).'main/inc/conf/';
+
+        //Checking configuration file
+        if (!is_writable($configurationPath)) {
+            $output->writeln("<comment>Folder ".$configurationPath." must have writable permissions</comment>");
+        }
+
+        //Getting chamilo_database_version
+        /*$conn = $this->getHelper('main_database')->getConnection();
+        $data = $conn->executeQuery("SELECT selected_value FROM settings_current WHERE variable = 'chamilo_database_version'");
+        $data = $data->fetch();
+        $versionFromDB = $data['selected_value'];*/
+
         $version = $input->getArgument('version');
         $dryRun = $input->getOption('dry-run');
         $minVersion = $this->getMinVersionSupportedByInstall();
@@ -110,34 +159,43 @@ class MigrationCommand extends AbstractCommand
         global $_configuration;
 
         $currentVersion = null;
-        //Checking root_sys and correct Chamilo version to install
 
+        //Checking root_sys and correct Chamilo version to install
         if (!isset($_configuration['root_sys'])) {
             $output->writeln("<comment>Can't migrate Chamilo. This is not a Chamilo folder installation.</comment>");
         }
 
-        if (isset($_configuration['system_version']) &&
-            !empty($_configuration['system_version']) &&
-            $_configuration['system_version'] > $minVersion &&
-            $version > $_configuration['system_version']
-        ) {
+        //Checking system_version
+
+
+        if (!isset($_configuration['system_version']) || empty($_configuration['system_version'])) {
+            $output->writeln("<comment>You have something wrong in your Chamilo installation check it with chamilo:status.</comment>");
+            exit;
+        }
+
+        if (version_compare($_configuration['system_version'], $minVersion, '<')) {
+            $output->writeln("<comment>Your Chamilo version is not supported! The minimun version is: </comment> <info>$minVersion</info> <comment>You want to update from <info>".$_configuration['system_version']."</info> <comment>to</comment> <info>$minVersion</info>");
+            exit;
+        }
+
+        if (version_compare($version, $_configuration['system_version'], '>')) {
             $currentVersion = $_configuration['system_version'];
         } else {
-            $output->writeln("<comment>Please provide a version greater than your current installation > </comment><info>".$_configuration['system_version']."</info>");
+            $output->writeln("<comment>Please provide a version greater than </comment><info>".$_configuration['system_version']."</info> <comment>your selected version: </comment><info>$version</info>");
+            $output->writeln("<comment>You can also check your installation health's with </comment><info>chamilo:status");
             exit;
         }
 
         $versionInfo = $this->getAvailableVersionInfo($version);
 
-
         if (isset($versionInfo['hook_to_version']) && isset($doctrineVersion)) {
             if ($doctrineVersion == $versionInfo['hook_to_version']) {
-                $output->writeln("<comment>Nothing to update!</comment>");
+                $output->writeln("<comment>You already have the latest version. Nothing to update!</comment>");
                 exit;
             }
         }
 
-
+        $output->writeln("<comment>Welcome to the Chamilo upgrade CLI!</comment>");
 
         //Too much questions?
 
@@ -164,11 +222,11 @@ class MigrationCommand extends AbstractCommand
         $output->writeln('<comment>Migrating from Chamilo version: </comment><info>'.$_configuration['system_version'].'</info> <comment>to version <info>'.$version);
 
         //Starting
-        $output->writeln('<comment>Starting migration for Chamilo portal located here: </comment><info>'.$_configuration['root_sys'].'</info>');
+        $output->writeln('<comment>Starting upgrade for Chamilo with configuration file: </comment><info>'.$configurationPath.'configuration.php</info>');
 
         $oldVersion = $currentVersion;
         foreach ($versionList as $versionItem => $versionInfo) {
-            if ($versionItem > $currentVersion && $versionItem <= $version) {
+            if (version_compare($versionItem, $currentVersion, '>') && version_compare($versionItem, $version, '<=')) {
                 if (isset($versionInfo['require_update']) && $versionInfo['require_update'] == true) {
                     //Greater than my current version
                     $this->startMigration($oldVersion, $versionItem, $dryRun, $output);
@@ -178,10 +236,12 @@ class MigrationCommand extends AbstractCommand
                 }
             }
         }
+        $output->writeln("<comment>wow! You just finish to migrate. Too check the current status of your platform. Execute:</comment><info>chamilo:status</info>");
+
     }
 
     /**
-     *
+     * Gets the Doctrine configuration file path
      * @return string
      */
     public function getMigrationConfigurationFile()
@@ -190,8 +250,14 @@ class MigrationCommand extends AbstractCommand
     }
 
     /**
-     * @param $version
+     * Starts a migration
+     *
+     * @param $fromVersion
+     * @param $toVersion
+     * @param $dryRun
      * @param $output
+     *
+     * @return bool
      */
     public function startMigration($fromVersion, $toVersion, $dryRun, $output)
     {
@@ -205,7 +271,9 @@ class MigrationCommand extends AbstractCommand
             if (file_exists($sqlToInstall)) {
                 //$result = $this->processSQL($sqlToInstall, $dryRun, $output);
                 $result = true;
+                $output->writeln("");
                 $output->writeln("<comment>Executing file: <info>'$sqlToInstall'</info>");
+                $output->writeln("<comment>You have to select yes for the 'Chamilo Migrations'<comment>");
 
                 if ($result) {
                     $command = $this->getApplication()->find('migrations:migrate');
@@ -220,12 +288,15 @@ class MigrationCommand extends AbstractCommand
                 }
             }
         }
+        return false;
     }
 
     /**
      *
-     * @param $sqlFilePath
-     * @param $dryRun
+     * Converts a SQL file into a array of SQL queries in order to be executed by the Doctrine connection obj
+     *
+     * @param string $sqlFilePath
+     * @param bool $dryRun
      * @param $output
      *
      * @return bool
@@ -269,8 +340,9 @@ class MigrationCommand extends AbstractCommand
 
     /**
      * Function originally wrote in install.lib.php
-     * @param $file
-     * @param $section
+     *
+     * @param string $file
+     * @param string $section
      * @param bool $printErrors
      *
      * @return array|bool

+ 29 - 25
tests/doctrine_console/cli-config.php

@@ -8,6 +8,7 @@ $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache);
 
 use Doctrine\Common\Annotations\AnnotationReader;
 use Doctrine\Common\Annotations\AnnotationRegistry;
+use Symfony\Component\Yaml\Parser;
 
 AnnotationRegistry::registerFile(api_get_path(SYS_PATH)."vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php");
 $reader = new AnnotationReader();
@@ -18,7 +19,16 @@ $config->setMetadataDriverImpl($driverImpl);
 $config->setProxyDir(__DIR__ . '/Proxies');
 $config->setProxyNamespace('Proxies');
 
-$courseList = CourseManager::get_real_course_list();
+//$courseList = CourseManager::get_real_course_list();
+$courseList = array();
+
+$configurationPath = api_get_path(SYS_PATH).'main/inc/conf/';
+$newConfigurationFile = $configurationPath.'configuration.yml';
+
+if (is_file($newConfigurationFile) && file_exists($newConfigurationFile)) {
+    $yaml = new Parser();
+    $_configuration = $yaml->parse(file_get_contents($newConfigurationFile));
+}
 
 $connectionOptions = array();
 
@@ -42,31 +52,25 @@ $connectionOptions['main_database'] = array(
     'host'      => $_configuration['db_host'],
 );
 
-$connectionOptions['statistics_database'] = array(
-    'driver'    => 'pdo_mysql',
-    'dbname'    => $_configuration['statistics_database'],
-    'user'      => $_configuration['db_user'],
-    'password'  => $_configuration['db_password'],
-    'host'      => $_configuration['db_host'],
-);
-
-/*
-$connectionOptions['scorm_database'] = array(
-    'driver'    => 'pdo_mysql',
-    'dbname'    => $_configuration['scorm_database'],
-    'user'      => $_configuration['db_user'],
-    'password'  => $_configuration['db_password'],
-    'host'      => $_configuration['db_host'],
-);*/
-
-$connectionOptions['user_personal_database'] = array(
-    'driver'    => 'pdo_mysql',
-    'dbname'    => $_configuration['user_personal_database'],
-    'user'      => $_configuration['db_user'],
-    'password'  => $_configuration['db_password'],
-    'host'      => $_configuration['db_host'],
-);
+if (isset($_configuration['statistics_database'])) {
+    $connectionOptions['statistics_database'] = array(
+        'driver'    => 'pdo_mysql',
+        'dbname'    => $_configuration['statistics_database'],
+        'user'      => $_configuration['db_user'],
+        'password'  => $_configuration['db_password'],
+        'host'      => $_configuration['db_host'],
+    );
+}
 
+if (isset($_configuration['user_personal_database'])) {
+    $connectionOptions['user_personal_database'] = array(
+        'driver'    => 'pdo_mysql',
+        'dbname'    => $_configuration['user_personal_database'],
+        'user'      => $_configuration['db_user'],
+        'password'  => $_configuration['db_password'],
+        'host'      => $_configuration['db_host'],
+    );
+}
 
 $defaultConnection = array(
     'driver'    => 'pdo_mysql',

+ 4 - 3
tests/doctrine_console/doctrine.php

@@ -12,6 +12,7 @@ $helperSet = $cli->getHelperSet();
 foreach ($helpers as $name => $helper) {
     $helperSet->set($helper, $name);
 }
+
 $cli->addCommands(array(
     // DBAL Commands
     new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(),
@@ -40,8 +41,8 @@ $cli->addCommands(array(
     new \Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand(),
     new \Doctrine\DBAL\Migrations\Tools\Console\Command\StatusCommand(),
     new \Doctrine\DBAL\Migrations\Tools\Console\Command\VersionCommand(),
-    new ChamiloLMS\Command\Database\MigrationCommand()
-
-
+    new ChamiloLMS\Command\Database\UpgradeCommand(),
+    new ChamiloLMS\Command\Database\InstallCommand(),
+    new ChamiloLMS\Command\Database\StatusCommand(),
 ));
 $cli->run();