From 8983b3ca142caef3f02867536090734345b1bee5 Mon Sep 17 00:00:00 2001 From: Julio Date: Mon, 16 Dec 2019 16:54:55 +0100 Subject: [PATCH] Minor - flint fixes --- chash.php | 8 +- createPhar.php | 2 +- src/Command/Chash/SelfUpdateCommand.php | 25 +- src/Command/Chash/SetupCommand.php | 36 +- src/Command/Common/ChamiloEmailCommand.php | 10 +- src/Command/Common/ChamiloUserCommand.php | 5 +- src/Command/Common/CommonCommand.php | 390 ++-- src/Command/Common/DatabaseCommand.php | 50 +- src/Command/Common/InfoCommand.php | 4 +- src/Command/Database/DropDatabaseCommand.php | 6 +- src/Command/Database/DumpCommand.php | 7 +- src/Command/Database/FullBackupCommand.php | 25 +- src/Command/Database/ImportCommand.php | 28 +- src/Command/Database/RestoreCommand.php | 7 +- src/Command/Database/RunSQLCommand.php | 16 +- src/Command/Database/SQLCountCommand.php | 8 +- src/Command/Database/ShowConnInfoCommand.php | 6 +- src/Command/Email/SendEmailCommand.php | 13 +- src/Command/Files/CleanConfigFilesCommand.php | 6 +- .../Files/CleanCoursesFilesCommand.php | 6 +- .../Files/CleanDeletedDocumentsCommand.php | 9 +- src/Command/Files/CleanTempFolderCommand.php | 10 +- src/Command/Files/ConvertVideosCommand.php | 35 +- src/Command/Files/DeleteCoursesCommand.php | 93 +- src/Command/Files/DeleteMultiUrlCommand.php | 107 +- .../GenerateTempFileStructureCommand.php | 55 +- src/Command/Files/MailConfCommand.php | 14 +- src/Command/Files/ReplaceURLCommand.php | 10 +- .../SetPermissionsAfterInstallCommand.php | 110 +- src/Command/Files/ShowDiskUsageCommand.php | 75 +- .../Files/UpdateDirectoryMaxSizeCommand.php | 7 +- src/Command/Info/GetInstancesCommand.php | 113 +- src/Command/Info/WhichCommand.php | 11 +- src/Command/Installation/InstallCommand.php | 347 ++-- src/Command/Installation/StatusCommand.php | 11 +- src/Command/Installation/UpgradeCommand.php | 1735 +++++++++-------- .../Installation/UpgradeDatabaseCommand.php | 587 +++--- src/Command/Installation/WipeCommand.php | 18 +- .../Translation/AddSubLanguageCommand.php | 14 +- .../Translation/DisableLanguageCommand.php | 11 +- .../Translation/EnableLanguageCommand.php | 9 +- .../Translation/ExportLanguageCommand.php | 13 +- .../Translation/ImportLanguageCommand.php | 9 +- .../Translation/ListLanguagesCommand.php | 9 +- .../Translation/PlatformLanguageCommand.php | 15 +- .../Translation/TermsPackageCommand.php | 20 +- src/Command/User/AddUserCommand.php | 21 +- src/Command/User/ChangePassCommand.php | 9 +- src/Command/User/DisableAdminsCommand.php | 10 +- src/Command/User/MakeAdminCommand.php | 4 +- src/Command/User/ResetLoginCommand.php | 8 +- src/Command/User/SetLanguageCommand.php | 6 +- src/Command/User/UsersPerUrlAccessCommand.php | 7 +- .../DBAL/Types/UTCDateTimeType.php | 2 +- src/Helpers/ConfigurationHelper.php | 121 +- src/Migrations/Version10.php | 23 +- src/Migrations/Version8.php | 11 +- src/Migrations/Version9.php | 11 +- src/Resources/Database/1.10.0/update.php | 18 +- src/Resources/Database/1.11.0/update.php | 9 +- .../1.8.8/update-db-1.8.7-1.8.8.inc.php | 1 - .../1.9.0/update-db-1.8.8-1.9.0.inc.php | 37 +- 62 files changed, 2180 insertions(+), 2223 deletions(-) diff --git a/chash.php b/chash.php index 82cdabd..ad00cb0 100755 --- a/chash.php +++ b/chash.php @@ -1,7 +1,7 @@ new Chash\Helpers\ConfigurationHelper() + 'configuration' => new Chash\Helpers\ConfigurationHelper(), ]; $application = new Application('Chamilo Command Line Interface', '1.0'); - +$helpers = [ + 'configuration' => new Chash\Helpers\ConfigurationHelper() +]; $helperSet = $application->getHelperSet(); foreach ($helpers as $name => $helper) { $helperSet->set($helper, $name); diff --git a/createPhar.php b/createPhar.php index dac549d..154c1e1 100644 --- a/createPhar.php +++ b/createPhar.php @@ -1,7 +1,7 @@ getOption('src-destination'); if (empty($destinationFolder)) { - $destinationFolder = realpath(__DIR__.'/../../../chash/'); + $destinationFolder = realpath(__DIR__.'/../../../chash/'); } if (!is_writable($destinationFolder)) { $output->writeln('Chash update failed: the "'.$destinationFolder.'" directory used to update the Chash file could not be written'); + return 0; } if (!is_writable($tempFolder)) { $output->writeln('Chash update failed: the "'.$tempFolder.'" directory used to download the temp file could not be written'); + return 0; } @@ -62,7 +61,8 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O $rfs->copy('github.com', 'https://github.com/chamilo/chash/archive/master.zip', $tempFile); if (!file_exists($tempFile)) { - $output->writeln('Chash update failed: the "'.$tempFile. '" file could not be written'); + $output->writeln('Chash update failed: the "'.$tempFile.'" file could not be written'); + return 0; } @@ -78,6 +78,7 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O } catch (\Alchemy\Zippy\Exception\RunTimeException $e) { $output->writeln("Chash update failed during unzip."); $output->writeln($e->getMessage()); + return 0; } $fs = new \Symfony\Component\Filesystem\Filesystem(); diff --git a/src/Command/Chash/SetupCommand.php b/src/Command/Chash/SetupCommand.php index b0c6ce0..1012cbd 100644 --- a/src/Command/Chash/SetupCommand.php +++ b/src/Command/Chash/SetupCommand.php @@ -3,19 +3,29 @@ namespace Chash\Command\Chash; use Doctrine\Migrations\Tools\Console\Command\AbstractCommand; +use Symfony\Component\Console; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console; -use Symfony\Component\Yaml\Dumper; use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Yaml\Dumper; /** - * Class SetupCommand + * Class SetupCommand. */ class SetupCommand extends AbstractCommand { public $migrationFile; + /** + * Gets the migration file path. + * + * @return string + */ + public function getMigrationFile() + { + return $this->migrationFile; + } + protected function configure(): void { $this @@ -27,12 +37,9 @@ protected function configure(): void } /** - * Executes a command via CLI - * - * @param Console\Input\InputInterface $input - * @param Console\Output\OutputInterface $output + * Executes a command via CLI. * - * @return int|null|void + * @return int|void|null */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { @@ -56,8 +63,6 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O if ($version == '110') { $file = $chamiloRoot.'app/config/migrations110.yml'; - - $this->migrationFile = $file; return 1; @@ -74,7 +79,7 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O 'name' => 'Chamilo Migrations', 'migrations_namespace' => 'Application\Migrations\Schema\V111', 'table_name' => 'version', - 'migrations_directory' => $migrationsFolder + 'migrations_directory' => $migrationsFolder, ]; $dumper = new Dumper(); @@ -90,13 +95,4 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O $output->writeln("Chash migrations.yml saved: $file"); $this->migrationFile = $file; } - - /** - * Gets the migration file path - * @return string - */ - public function getMigrationFile() - { - return $this->migrationFile; - } } diff --git a/src/Command/Common/ChamiloEmailCommand.php b/src/Command/Common/ChamiloEmailCommand.php index 5027086..97ff7e7 100644 --- a/src/Command/Common/ChamiloEmailCommand.php +++ b/src/Command/Common/ChamiloEmailCommand.php @@ -3,15 +3,13 @@ namespace Chash\Command\Common; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Formatter\OutputFormatterStyle; - /** - * Class CommonChamiloUserCommand + * Class CommonChamiloUserCommand. + * * @package Chash\Command\User */ class ChamiloEmailCommand extends Command @@ -28,9 +26,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Common/ChamiloUserCommand.php b/src/Command/Common/ChamiloUserCommand.php index 81294d5..ec0b3ef 100644 --- a/src/Command/Common/ChamiloUserCommand.php +++ b/src/Command/Common/ChamiloUserCommand.php @@ -2,7 +2,6 @@ namespace Chash\Command\Common; -use Chash\Command\Common\DatabaseCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -14,9 +13,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Common/CommonCommand.php b/src/Command/Common/CommonCommand.php index 3b124f0..f7bade9 100644 --- a/src/Command/Common/CommonCommand.php +++ b/src/Command/Common/CommonCommand.php @@ -3,21 +3,21 @@ namespace Chash\Command\Common; use Doctrine\Common\Annotations\AnnotationReader; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\ConnectionException; use Doctrine\ORM\EntityManager; -use Symfony\Component\Console\Helper\HelperSet; -use Symfony\Component\Yaml\Parser; use Symfony\Component\Console\Command\Command as AbstractCommand; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Filesystem\Exception\IOException; -use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Helper\HelperSet; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; -use Doctrine\DBAL\Connection; +use Symfony\Component\Yaml\Parser; use Symfony\Component\Yaml\Yaml; /** - * Class CommonCommand + * Class CommonCommand. */ class CommonCommand extends AbstractCommand { @@ -31,17 +31,14 @@ class CommonCommand extends AbstractCommand private $migrationConfigurationFile; private $manager; - /** - * @param array $configuration - */ public function setConfigurationArray(array $configuration) { $this->configuration = $configuration; } /** - * @return array - */ + * @return array + */ public function getConfigurationArray() { return $this->configuration; @@ -63,9 +60,6 @@ public function getConfigurationPath() return $this->configurationPath; } - /** - * @param array $portalSettings - */ public function setPortalSettings(array $portalSettings) { $this->portalSettings = $portalSettings; @@ -79,9 +73,6 @@ public function getPortalSettings() return $this->portalSettings; } - /** - * @param array $databaseSettings - */ public function setDatabaseSettings(array $databaseSettings) { $user = isset($databaseSettings['dbuser']) ? $databaseSettings['dbuser'] : $databaseSettings['user']; @@ -117,9 +108,6 @@ public function getDatabaseSettings() return $this->databaseSettings; } - /** - * @param array $databaseSettings - */ public function setExtraDatabaseSettings(array $databaseSettings) { $this->extraDatabaseSettings = $databaseSettings; @@ -133,9 +121,6 @@ public function getExtraDatabaseSettings() return $this->extraDatabaseSettings; } - /** - * @param array $adminSettings - */ public function setAdminSettings(array $adminSettings) { $this->adminSettings = $adminSettings; @@ -192,7 +177,7 @@ public function getInstallationFolder() } /** - * Gets the installation version path + * Gets the installation version path. * * @param string $version * @@ -203,11 +188,12 @@ public function getInstallationPath($version) if ($version === 'master') { $version = $this->getLatestVersion(); } + return $this->getInstallationFolder().$version.'/'; } /** - * Gets the version name folders located in main/install + * Gets the version name folders located in main/install. * * @return array */ @@ -235,49 +221,49 @@ public function getAdminSettingsParams() 'firstname' => [ 'attributes' => [ 'label' => 'Firstname', - 'data' => 'John', + 'data' => 'John', ], 'type' => 'text', ], - 'lastname' => [ + 'lastname' => [ 'attributes' => [ 'label' => 'Lastname', - 'data' => 'Doe', + 'data' => 'Doe', ], 'type' => 'text', ], 'username' => [ 'attributes' => [ 'label' => 'Username', - 'data' => 'admin', + 'data' => 'admin', ], 'type' => 'text', ], 'password' => [ 'attributes' => [ 'label' => 'Password', - 'data' => 'admin', + 'data' => 'admin', ], 'type' => 'password', ], 'email' => [ 'attributes' => [ 'label' => 'Email', - 'data' => 'admin@example.org', + 'data' => 'admin@example.org', ], 'type' => 'email', ], 'language' => [ 'attributes' => [ 'label' => 'Language', - 'data' => 'english', + 'data' => 'english', ], 'type' => 'text', ], 'phone' => [ 'attributes' => [ 'label' => 'Phone', - 'data' => '123456', + 'data' => '123456', ], 'type' => 'text', ], @@ -345,7 +331,8 @@ public function getPortalSettingsParams() } /** - * Database parameters that are going to be parsed during the console/browser installation + * Database parameters that are going to be parsed during the console/browser installation. + * * @return array */ public function getDatabaseSettingsParams() @@ -353,8 +340,7 @@ public function getDatabaseSettingsParams() return [ 'driver' => [ 'attributes' => [ - 'choices' => - [ + 'choices' => [ 'pdo_mysql' => 'pdo_mysql', 'pdo_sqlite' => 'pdo_sqlite', 'pdo_pgsql' => 'pdo_pgsql', @@ -414,7 +400,7 @@ public function getLatestVersion() } /** - * Gets the content of a version from the available versions + * Gets the content of a version from the available versions. * * @param string $version * @@ -433,7 +419,8 @@ public function getAvailableVersionInfo($version) } /** - * Gets the min version available to migrate with this command + * Gets the min version available to migrate with this command. + * * @return mixed */ public function getMinVersionSupportedByInstall() @@ -442,7 +429,8 @@ public function getMinVersionSupportedByInstall() } /** - * Gets an array with the supported versions to migrate + * Gets an array with the supported versions to migrate. + * * @return array */ public function getVersionNumberList() @@ -457,7 +445,7 @@ public function getVersionNumberList() } /** - * Gets an array with the settings for every supported version + * Gets an array with the settings for every supported version. * * @return array */ @@ -473,7 +461,7 @@ public function availableVersions() 'post' => null, 'update_db' => 'update-db-1.8.7-1.8.8.inc.php', //'update_files' => 'update-files-1.8.7-1.8.8.inc.php', - 'hook_to_doctrine_version' => '8' //see ChamiloLMS\Migrations\Version8.php file + 'hook_to_doctrine_version' => '8', //see ChamiloLMS\Migrations\Version8.php file ], '1.8.8.2' => [ 'require_update' => false, @@ -579,13 +567,13 @@ public function availableVersions() 'require_update' => false, 'parent' => '1.11.0', ], - '2.0' => [ + '2.0' => [ 'require_update' => true, 'update_files' => null, 'hook_to_doctrine_version' => '2', 'parent' => '2.0', ], - 'master' => [ + 'master' => [ 'require_update' => true, 'update_files' => null, 'hook_to_doctrine_version' => '2', @@ -597,7 +585,8 @@ public function availableVersions() } /** - * Gets the Doctrine configuration file path + * Gets the Doctrine configuration file path. + * * @return string */ public function getMigrationConfigurationFile() @@ -614,7 +603,6 @@ public function setMigrationConfigurationFile($file) } /** - * * @return \Chash\Helpers\ConfigurationHelper */ public function getConfigurationHelper() @@ -624,6 +612,7 @@ public function getConfigurationHelper() /** * @todo move to configurationhelper + * * @param string $path */ public function setRootSysDependingConfigurationPath($path) @@ -641,12 +630,13 @@ public function setRootSysDependingConfigurationPath($path) } /** - * Writes the configuration file for the first time (install command) + * Writes the configuration file for the first time (install command). + * * @param string $version * @param string $path - * @param object $output Output handler to print info messages - * @return bool + * @param object $output Output handler to print info messages * + * @return bool */ public function writeConfiguration($version, $path, $output) { @@ -754,11 +744,11 @@ public function writeConfiguration($version, $path, $output) } /** - * Updates the configuration.yml file + * Updates the configuration.yml file. * - * @param OutputInterface $output - * @param bool $dryRun + * @param bool $dryRun * @param array $newValues + * * @return bool */ public function updateConfiguration(OutputInterface $output, $dryRun, $newValues) @@ -817,7 +807,8 @@ public function updateConfiguration(OutputInterface $output, $dryRun, $newValues } /** - * Gets the SQL files relation with versions + * Gets the SQL files relation with versions. + * * @return array */ public function getDatabaseMap() @@ -906,135 +897,9 @@ public function getDatabaseMap() ]; } - /** - * Set Doctrine settings - * @param HelperSet $helperSet - * @return \Doctrine\ORM\EntityManager - */ - protected function setDoctrineSettings(HelperSet $helperSet) - { - $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); - $reader = new AnnotationReader(); - $driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver( - $reader, - [] - ); - $config->setMetadataDriverImpl($driverImpl); - $config->setProxyDir(__DIR__ . '/Proxies'); - $config->setProxyNamespace('Proxies'); - $settings = $this->getDatabaseSettings(); - $dbName = $settings['dbname']; - unset($settings['dbname']); - - $em = \Doctrine\ORM\EntityManager::create( - $settings, - $config - ); - - try { - $connection = $em->getConnection(); - $dbList = $connection->getSchemaManager()->listDatabases(); - // Check in db exists in list. - if (in_array($dbName, $dbList)) { - $settings['dbname'] = $dbName; - $em = \Doctrine\ORM\EntityManager::create( - $settings, - $config - ); - } - } catch (ConnectionException $e) { - echo $e->getMessage(); - } - - $platform = $em->getConnection()->getDatabasePlatform(); - $platform->registerDoctrineTypeMapping('enum', 'string'); - $platform->registerDoctrineTypeMapping('set', 'string'); - - $helpers = [ - 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper( - $em->getConnection() - ), - 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper( - $em - ) - //'configuration' => new \Chash\Helpers\ConfigurationHelper() - ]; - - foreach ($helpers as $name => $helper) { - $helperSet->set( - $helper, - $name - ); - } - - return $em; - } - - /** - * @param string $version - * @param string $path - * @param array $databaseList - */ - protected function setConnections($version, $path, $databaseList) - { - $_configuration = $this->getHelper('configuration')->getConfiguration($path); - - $config = new \Doctrine\ORM\Configuration(); - $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache); - $reader = new AnnotationReader(); - - $driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, []); - $config->setMetadataDriverImpl($driverImpl); - $config->setProxyDir(__DIR__ . '/Proxies'); - $config->setProxyNamespace('Proxies'); - - foreach ($databaseList as $section => &$dbList) { - foreach ($dbList as &$dbInfo) { - $params = $this->getDatabaseSettings(); - - if (isset($_configuration['single_database']) && $_configuration['single_database'] == true) { - $em = \Doctrine\ORM\EntityManager::create($params, $config); - } else { - if ($section == 'course') { - if (version_compare($version, '10', '<=')) { - if (strpos($dbInfo['database'], '_chamilo_course_') === false) { - //$params['dbname'] = $params['dbname']; - } else { - $params['dbname'] = str_replace('_chamilo_course_', '', $dbInfo['database']); - } - } - $em = \Doctrine\ORM\EntityManager::create($params, $config); - } else { - $databaseName = $params['dbname']; - switch ($dbInfo['database']) { - case 'statistics_database': - $databaseName = isset($_configuration['statistics_database']) ? $_configuration['statistics_database'] : $databaseName; - break; - case 'user_personal_database': - $databaseName = isset($_configuration['user_personal_database']) ? $_configuration['user_personal_database'] : $databaseName; - break; - } - $params['dbname'] = $databaseName; - $em = \Doctrine\ORM\EntityManager::create($params, $config); - } - } - - if (!empty($em)) { - $platform = $em->getConnection()->getDatabasePlatform(); - $platform->registerDoctrineTypeMapping('enum', 'string'); - $platform->registerDoctrineTypeMapping('set', 'string'); - } - - $helper = new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()); - $this->getApplication()->getHelperSet()->set($helper, $dbInfo['database']); - } - } - } - /** * @param Finder $files - * @param OutputInterface $output + * * @return int */ public function removeFiles($files, OutputInterface $output) @@ -1076,8 +941,6 @@ public function removeFiles($files, OutputInterface $output) } /** - * @param InputInterface $input - * @param array $params * @return array */ public function getParamsFromOptions(InputInterface $input, array $params) @@ -1093,11 +956,11 @@ public function getParamsFromOptions(InputInterface $input, array $params) } /** - * @param OutputInterface $output * @param string $version * @param string $updateInstallation * @param string $defaultTempFolder - * @return int|null|String + * + * @return int|string|null */ public function getPackage(OutputInterface $output, $version, $updateInstallation, $defaultTempFolder) { @@ -1127,7 +990,6 @@ public function getPackage(OutputInterface $output, $version, $updateInstallatio $updateInstallationOriginal = $updateInstallation; if (!empty($updateInstallation)) { - // Check temp folder if (!is_writable($defaultTempFolder)) { $output->writeln("We don't have permissions to write in the temp folder: $defaultTempFolder"); @@ -1196,7 +1058,7 @@ public function getPackage(OutputInterface $output, $version, $updateInstallatio $finder = new Finder(); $files = $finder->in($folderPath)->depth(0); - /** @var \SplFileInfo $file */ + /** @var \SplFileInfo $file */ foreach ($files as $file) { $chamiloLocationPath = $file->getRealPath(); break; @@ -1221,9 +1083,10 @@ public function getPackage(OutputInterface $output, $version, $updateInstallatio } /** - * @param array $_configuration + * @param array $_configuration * @param string $courseDatabase - * @return null|string + * + * @return string|null */ public function getTablePrefix($_configuration, $courseDatabase = null) { @@ -1241,9 +1104,9 @@ public function getTablePrefix($_configuration, $courseDatabase = null) } /** - * @param OutputInterface $output * @param string $chamiloLocationPath * @param string $destinationPath + * * @return int */ public function copyPackageIntoSystem( @@ -1259,6 +1122,7 @@ public function copyPackageIntoSystem( if (empty($chamiloLocationPath)) { $output->writeln("The chamiloLocationPath variable is empty"); + return 0; } @@ -1266,16 +1130,17 @@ public function copyPackageIntoSystem( if (empty($destinationPath)) { $output->writeln("The root path was not set."); + return 0; } else { $fileSystem->mirror($chamiloLocationPath, $destinationPath, null, ['override' => true]); $output->writeln("Copy finished."); + return 1; } } /** - * @param OutputInterface $output * @param string $title */ public function writeCommandHeader(OutputInterface $output, $title) @@ -1286,7 +1151,8 @@ public function writeCommandHeader(OutputInterface $output, $title) } /** - * Returns the config file list + * Returns the config file list. + * * @return array */ public function getConfigFiles() @@ -1302,9 +1168,6 @@ public function getConfigFiles() ]; } - /** - * @param \Symfony\Component\Console\Output\OutputInterface $output - */ public function generateConfFiles(OutputInterface $output) { $confDir = $this->getConfigurationPath(); @@ -1323,8 +1186,7 @@ public function generateConfFiles(OutputInterface $output) } /** - * Copy files from main/inc/conf to the new location config - * @param OutputInterface $output + * Copy files from main/inc/conf to the new location config. */ public function copyConfigFilesToNewLocation(OutputInterface $output) { @@ -1372,7 +1234,6 @@ public function copyConfigFilesToNewLocation(OutputInterface $output) } /** - * @param OutputInterface $output * @param $path */ public function removeUnUsedFiles(OutputInterface $output, $path) @@ -1394,10 +1255,6 @@ public function removeUnUsedFiles(OutputInterface $output, $path) } } - /** - * @param OutputInterface $output - * @param Connection $connection - */ public function setPortalSettingsInChamilo(OutputInterface $output, Connection $connection) { // Admin settings @@ -1470,10 +1327,6 @@ public function setPortalSettingsInChamilo(OutputInterface $output, Connection $ ); } - /** - * @param OutputInterface $output - * @param Connection $connection - */ public function setAdminSettingsInChamilo(OutputInterface $output, Connection $connection) { $settings = $this->getAdminSettings(); @@ -1498,6 +1351,7 @@ public function setAdminSettingsInChamilo(OutputInterface $output, Connection $c * * @param string $password * @param string $salt + * * @return string */ public function getEncryptedPassword($password, $salt = null) @@ -1531,4 +1385,130 @@ public function setManager($manager) { $this->manager = $manager; } + + /** + * Set Doctrine settings. + * + * @return \Doctrine\ORM\EntityManager + */ + protected function setDoctrineSettings(HelperSet $helperSet) + { + $config = new \Doctrine\ORM\Configuration(); + $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); + $reader = new AnnotationReader(); + $driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver( + $reader, + [] + ); + $config->setMetadataDriverImpl($driverImpl); + $config->setProxyDir(__DIR__.'/Proxies'); + $config->setProxyNamespace('Proxies'); + $settings = $this->getDatabaseSettings(); + $dbName = $settings['dbname']; + unset($settings['dbname']); + + $em = \Doctrine\ORM\EntityManager::create( + $settings, + $config + ); + + try { + $connection = $em->getConnection(); + $dbList = $connection->getSchemaManager()->listDatabases(); + // Check in db exists in list. + if (in_array($dbName, $dbList)) { + $settings['dbname'] = $dbName; + $em = \Doctrine\ORM\EntityManager::create( + $settings, + $config + ); + } + } catch (ConnectionException $e) { + echo $e->getMessage(); + } + + $platform = $em->getConnection()->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('enum', 'string'); + $platform->registerDoctrineTypeMapping('set', 'string'); + + $helpers = [ + 'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper( + $em->getConnection() + ), + 'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper( + $em + ), + //'configuration' => new \Chash\Helpers\ConfigurationHelper() + ]; + + foreach ($helpers as $name => $helper) { + $helperSet->set( + $helper, + $name + ); + } + + return $em; + } + + /** + * @param string $version + * @param string $path + * @param array $databaseList + */ + protected function setConnections($version, $path, $databaseList) + { + $_configuration = $this->getHelper('configuration')->getConfiguration($path); + + $config = new \Doctrine\ORM\Configuration(); + $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache()); + $reader = new AnnotationReader(); + + $driverImpl = new \Doctrine\ORM\Mapping\Driver\AnnotationDriver($reader, []); + $config->setMetadataDriverImpl($driverImpl); + $config->setProxyDir(__DIR__.'/Proxies'); + $config->setProxyNamespace('Proxies'); + + foreach ($databaseList as $section => &$dbList) { + foreach ($dbList as &$dbInfo) { + $params = $this->getDatabaseSettings(); + + if (isset($_configuration['single_database']) && $_configuration['single_database'] == true) { + $em = \Doctrine\ORM\EntityManager::create($params, $config); + } else { + if ($section == 'course') { + if (version_compare($version, '10', '<=')) { + if (strpos($dbInfo['database'], '_chamilo_course_') === false) { + //$params['dbname'] = $params['dbname']; + } else { + $params['dbname'] = str_replace('_chamilo_course_', '', $dbInfo['database']); + } + } + $em = \Doctrine\ORM\EntityManager::create($params, $config); + } else { + $databaseName = $params['dbname']; + switch ($dbInfo['database']) { + case 'statistics_database': + $databaseName = isset($_configuration['statistics_database']) ? $_configuration['statistics_database'] : $databaseName; + break; + case 'user_personal_database': + $databaseName = isset($_configuration['user_personal_database']) ? $_configuration['user_personal_database'] : $databaseName; + break; + } + $params['dbname'] = $databaseName; + $em = \Doctrine\ORM\EntityManager::create($params, $config); + } + } + + if (!empty($em)) { + $platform = $em->getConnection()->getDatabasePlatform(); + $platform->registerDoctrineTypeMapping('enum', 'string'); + $platform->registerDoctrineTypeMapping('set', 'string'); + } + + $helper = new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()); + $this->getApplication()->getHelperSet()->set($helper, $dbInfo['database']); + } + } + } } diff --git a/src/Command/Common/DatabaseCommand.php b/src/Command/Common/DatabaseCommand.php index 18f42f8..691fc0b 100644 --- a/src/Command/Common/DatabaseCommand.php +++ b/src/Command/Common/DatabaseCommand.php @@ -2,37 +2,20 @@ namespace Chash\Command\Common; -use Symfony\Component\Console\Style\SymfonyStyle; +use Exception; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Dotenv\Dotenv; -use Exception; /** - * Class CommonDatabaseCommand + * Class CommonDatabaseCommand. */ class DatabaseCommand extends CommonCommand { - protected function configure(): void - { - $this - ->addOption( - 'conf', - null, - InputOption::VALUE_OPTIONAL, - 'The configuration.php file path. Example /var/www/chamilo/config/configuration.php' - ) - ->addOption( - 'dry-run', - null, - InputOption::VALUE_NONE, - 'For tests' - ); - } - /** - * @inheritdoc + * {@inheritdoc} */ public function getConnection(InputInterface $input) { @@ -55,10 +38,25 @@ public function getEntityManager() } } + protected function configure(): void + { + $this + ->addOption( + 'conf', + null, + InputOption::VALUE_OPTIONAL, + 'The configuration.php file path. Example /var/www/chamilo/config/configuration.php' + ) + ->addOption( + 'dry-run', + null, + InputOption::VALUE_NONE, + 'For tests' + ); + } + /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -122,7 +120,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'host' => $configuration['db_host'], 'dbname' => $configuration['main_database'], 'user' => $configuration['db_user'], - 'password' => $configuration['db_password'] + 'password' => $configuration['db_password'], ]; } else { $databaseSettings = [ @@ -130,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'host' => $configuration['database_host'], 'dbname' => $configuration['database_name'], 'user' => $configuration['database_user'], - 'password' => $configuration['database_password'] + 'password' => $configuration['database_password'], ]; } diff --git a/src/Command/Common/InfoCommand.php b/src/Command/Common/InfoCommand.php index 2695842..93827d0 100644 --- a/src/Command/Common/InfoCommand.php +++ b/src/Command/Common/InfoCommand.php @@ -21,9 +21,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Database/DropDatabaseCommand.php b/src/Command/Database/DropDatabaseCommand.php index 0b71b8f..27e49a4 100644 --- a/src/Command/Database/DropDatabaseCommand.php +++ b/src/Command/Database/DropDatabaseCommand.php @@ -8,7 +8,7 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; /** - * Class DropDatabaseCommand + * Class DropDatabaseCommand. */ class DropDatabaseCommand extends DatabaseCommand { @@ -21,9 +21,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Database/DumpCommand.php b/src/Command/Database/DumpCommand.php index 7b6fa10..460b638 100644 --- a/src/Command/Database/DumpCommand.php +++ b/src/Command/Database/DumpCommand.php @@ -7,7 +7,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * Class DumpCommand + * Class DumpCommand. * * Returns a dump of the database (caller should use an output redirect of some * kind to store to a file. @@ -26,9 +26,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -36,6 +34,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $_configuration = $this->getConfigurationArray(); $dump = 'mysqldump -h '.$_configuration['db_host'].' -u '.$_configuration['db_user'].' -p'.$_configuration['db_password'].' '.$_configuration['main_database']; system($dump); + return null; } } diff --git a/src/Command/Database/FullBackupCommand.php b/src/Command/Database/FullBackupCommand.php index c0ad29d..b699efa 100644 --- a/src/Command/Database/FullBackupCommand.php +++ b/src/Command/Database/FullBackupCommand.php @@ -3,16 +3,17 @@ namespace Chash\Command\Database; use Chash\Command\Common\DatabaseCommand; +use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Input\ArrayInput; /** * Make a full backup of the given/current install and put the results * (files and db) into the given file. - * Store the temporary data into the /tmp/ directory + * Store the temporary data into the /tmp/ directory. + * * @param array $params The params received */ class FullBackupCommand extends DatabaseCommand @@ -48,17 +49,15 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); $_configuration = $this->getConfigurationArray(); - $resultPath = $input->getArgument('result'); - $tmpFolder = $input->getOption('tmp'); + $resultPath = $input->getArgument('result'); + $tmpFolder = $input->getOption('tmp'); if (empty($tmpFolder)) { $output->writeln( @@ -74,9 +73,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $command = $this->getApplication()->find('files:clean_temp_folder'); $arguments = [ - 'command' => 'files:clean_temp_folder' + 'command' => 'files:clean_temp_folder', ]; - $input = new ArrayInput($arguments); + $input = new ArrayInput($arguments); $command->run($input, $output); } else { $output->writeln('Temp archives are not removed'); @@ -89,10 +88,10 @@ protected function execute(InputInterface $input, OutputInterface $output) $f = $_configuration['db_user']; //backup the files (this requires root permissions) $bkp_dir = $tmpFolder.'/'.$f.'-'.date('Ymdhis'); - $err = @mkdir($bkp_dir); - $tgz = $bkp_dir.'/'.$f.'.tgz'; - $sql = $bkp_dir.'/'.$f.'-db.sql'; - $err = @system('tar zcf '.$tgz.' '.$cha_dir); + $err = @mkdir($bkp_dir); + $tgz = $bkp_dir.'/'.$f.'.tgz'; + $sql = $bkp_dir.'/'.$f.'-db.sql'; + $err = @system('tar zcf '.$tgz.' '.$cha_dir); $output->writeln('Generating mysqldump'); diff --git a/src/Command/Database/ImportCommand.php b/src/Command/Database/ImportCommand.php index 432f7c1..5d85d23 100644 --- a/src/Command/Database/ImportCommand.php +++ b/src/Command/Database/ImportCommand.php @@ -19,16 +19,16 @@ namespace Chash\Command\Database; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console; +use Symfony\Component\Console\Input\InputArgument; /** * Task for executing arbitrary SQL that can come from a file or directly from * the command line. * - * - * @link www.doctrine-project.org + * @see www.doctrine-project.org * @since 2.0 + * * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage @@ -49,7 +49,7 @@ protected function configure(): void 'file', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'File path(s) of SQL to be executed.' - ) + ), ]) ->setHelp( <<getArgument('file')) !== null) { foreach ((array) $fileNames as $fileName) { - if (! file_exists($fileName)) { - throw new \InvalidArgumentException( - sprintf("SQL file '%s' does not exist.", $fileName) - ); - } elseif (! is_readable($fileName)) { - throw new \InvalidArgumentException( - sprintf("SQL file '%s' does not have read permissions.", $fileName) - ); + if (!file_exists($fileName)) { + throw new \InvalidArgumentException(sprintf("SQL file '%s' does not exist.", $fileName)); + } elseif (!is_readable($fileName)) { + throw new \InvalidArgumentException(sprintf("SQL file '%s' does not have read permissions.", $fileName)); } $output->write(sprintf("Processing file '%s'... ", $fileName)); @@ -96,9 +92,9 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O $lines++; } while ($stmt->nextRowset()); - $output->write(sprintf('%d statements executed!', $lines) . PHP_EOL); + $output->write(sprintf('%d statements executed!', $lines).PHP_EOL); } catch (\PDOException $e) { - $output->write('error!' . PHP_EOL); + $output->write('error!'.PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } @@ -108,11 +104,11 @@ protected function execute(Console\Input\InputInterface $input, Console\Output\O $rs = $stmt->execute(); if ($rs) { - $output->writeln('OK!' . PHP_EOL); + $output->writeln('OK!'.PHP_EOL); } else { $error = $stmt->errorInfo(); - $output->write('error!' . PHP_EOL); + $output->write('error!'.PHP_EOL); throw new \RuntimeException($error[2], $error[0]); } diff --git a/src/Command/Database/RestoreCommand.php b/src/Command/Database/RestoreCommand.php index 243cc21..29d4b51 100644 --- a/src/Command/Database/RestoreCommand.php +++ b/src/Command/Database/RestoreCommand.php @@ -9,7 +9,8 @@ /** * Imports an SQL dump of the database (caller should use an output redirect of some kind - * to store to a file) + * to store to a file). + * * @param array $params params received */ class RestoreCommand extends DatabaseCommand @@ -31,9 +32,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Database/RunSQLCommand.php b/src/Command/Database/RunSQLCommand.php index 4618f39..6ea2a63 100644 --- a/src/Command/Database/RunSQLCommand.php +++ b/src/Command/Database/RunSQLCommand.php @@ -7,7 +7,8 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * Connects to the MySQL client without the need to introduce a password + * Connects to the MySQL client without the need to introduce a password. + * * @return int Exit code returned by mysql command */ class RunSQLCommand extends DatabaseCommand @@ -24,9 +25,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -35,11 +34,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $_configuration = $this->getConfigurationArray(); - $cmd = 'mysql -h '.$_configuration['db_host'].' -u '.$_configuration['db_user'].' -p'.$_configuration['db_password'].' '.$_configuration['main_database']; - $process = proc_open($cmd, [0 => STDIN, 1 => STDOUT, 2 => STDERR], $pipes); + $cmd = 'mysql -h '.$_configuration['db_host'].' -u '.$_configuration['db_user'].' -p'.$_configuration['db_password'].' '.$_configuration['main_database']; + $process = proc_open($cmd, [0 => STDIN, 1 => STDOUT, 2 => STDERR], $pipes); $proc_status = proc_get_status($process); - $exit_code = proc_close($process); - return ($proc_status["running"] ? $exit_code : $proc_status["exitcode"]); + $exit_code = proc_close($process); + + return $proc_status["running"] ? $exit_code : $proc_status["exitcode"]; /*$output->writeln('Starting Chamilo process'); $output->writeln('Chamilo process ended succesfully'); diff --git a/src/Command/Database/SQLCountCommand.php b/src/Command/Database/SQLCountCommand.php index e4887ff..b723679 100644 --- a/src/Command/Database/SQLCountCommand.php +++ b/src/Command/Database/SQLCountCommand.php @@ -8,7 +8,8 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * Count the number of rows in a specific table + * Count the number of rows in a specific table. + * * @return mixed Integer number of rows, or null on error */ class SQLCountCommand extends DatabaseCommand @@ -28,9 +29,8 @@ protected function configure(): void /** * @todo use doctrine - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Database/ShowConnInfoCommand.php b/src/Command/Database/ShowConnInfoCommand.php index f9d0552..68b3144 100644 --- a/src/Command/Database/ShowConnInfoCommand.php +++ b/src/Command/Database/ShowConnInfoCommand.php @@ -8,7 +8,7 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; /** - * Class ShowConnInfoCommand + * Class ShowConnInfoCommand. */ class ShowConnInfoCommand extends DatabaseCommand { @@ -22,9 +22,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Email/SendEmailCommand.php b/src/Command/Email/SendEmailCommand.php index b56c2c2..8cb33a0 100644 --- a/src/Command/Email/SendEmailCommand.php +++ b/src/Command/Email/SendEmailCommand.php @@ -3,10 +3,10 @@ namespace Chash\Command\Email; use Chash\Command\Common\ChamiloEmailCommand; +use Exception; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use Exception; /** * Class SendEmailCommand @@ -64,9 +64,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return null|void + * @return void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -100,13 +98,13 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($conn instanceof \Doctrine\DBAL\Connection) { $sql = "SELECT email, CONCAT(lastname, firstname) as name FROM $userTable u " - . "LEFT JOIN $adminTable a ON a.user_id = u.user_id " - . "ORDER BY u.user_id LIMIT 1"; + ."LEFT JOIN $adminTable a ON a.user_id = u.user_id " + ."ORDER BY u.user_id LIMIT 1"; try { $stmt = $conn->prepare($sql); $stmt->execute(); } catch (\PDOException $e) { - $output->write('SQL error!' . PHP_EOL); + $output->write('SQL error!'.PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } $row = $stmt->fetch(\PDO::FETCH_ASSOC); @@ -134,6 +132,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('The connection does not seem to be a valid PDO connection'); } } + return null; } } diff --git a/src/Command/Files/CleanConfigFilesCommand.php b/src/Command/Files/CleanConfigFilesCommand.php index a3d9f98..77adbe6 100644 --- a/src/Command/Files/CleanConfigFilesCommand.php +++ b/src/Command/Files/CleanConfigFilesCommand.php @@ -9,7 +9,7 @@ /** * Class CleanConfigFilesCommand - * Clean the archives directory, leaving only index.html, twig and Serializer + * Clean the archives directory, leaving only index.html, twig and Serializer. */ class CleanConfigFilesCommand extends DatabaseCommand { @@ -22,9 +22,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Files/CleanCoursesFilesCommand.php b/src/Command/Files/CleanCoursesFilesCommand.php index cf5d6fb..9f73c45 100644 --- a/src/Command/Files/CleanCoursesFilesCommand.php +++ b/src/Command/Files/CleanCoursesFilesCommand.php @@ -9,7 +9,7 @@ /** * Class CleanCoursesFilesCommand - * Clean the courses directory, leaving only index.html, twig and Serializer + * Clean the courses directory, leaving only index.html, twig and Serializer. */ class CleanCoursesFilesCommand extends DatabaseCommand { @@ -23,9 +23,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Files/CleanDeletedDocumentsCommand.php b/src/Command/Files/CleanDeletedDocumentsCommand.php index c7ea368..76fef92 100644 --- a/src/Command/Files/CleanDeletedDocumentsCommand.php +++ b/src/Command/Files/CleanDeletedDocumentsCommand.php @@ -11,7 +11,7 @@ /** * Class CleanDeletedDocumentsCommand * Clean the courses/[CODE]/documents/ directory, removing all documents - * and folders marked DELETED + * and folders marked DELETED. */ class CleanDeletedDocumentsCommand extends DatabaseCommand { @@ -49,9 +49,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -79,6 +77,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } } else { $output->writeln('No file to be deleted in courses/ directory'); + return; } } @@ -88,7 +87,7 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($files as $file) { $size += $file->getSize(); } - $output->writeln('Total size used by deleted documents: '.round(((float)$size/1024)/1024, 2).'MB'); + $output->writeln('Total size used by deleted documents: '.round(((float) $size / 1024) / 1024, 2).'MB'); } $helper = $this->getHelperSet()->get('question'); diff --git a/src/Command/Files/CleanTempFolderCommand.php b/src/Command/Files/CleanTempFolderCommand.php index 3619477..f27e754 100644 --- a/src/Command/Files/CleanTempFolderCommand.php +++ b/src/Command/Files/CleanTempFolderCommand.php @@ -3,15 +3,13 @@ namespace Chash\Command\Files; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; /** - * Class CleanTempFolderCommand + * Class CleanTempFolderCommand. + * * @package Chash\Command\Files */ class CleanTempFolderCommand extends DatabaseCommand @@ -26,9 +24,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Files/ConvertVideosCommand.php b/src/Command/Files/ConvertVideosCommand.php index ba16b9f..b604345 100644 --- a/src/Command/Files/ConvertVideosCommand.php +++ b/src/Command/Files/ConvertVideosCommand.php @@ -3,19 +3,19 @@ namespace Chash\Command\Files; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Finder\Finder; -use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Finder\Finder; /** * Class ConvertVideosCommand * Convert all videos found in the given directory (recursively) - * to the given format, using ffmpeg + * to the given format, using ffmpeg. + * * @package Chash\Command\Files */ class ConvertVideosCommand extends DatabaseCommand @@ -62,9 +62,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -77,10 +75,11 @@ protected function execute(InputInterface $input, OutputInterface $output) $dir = $input->getArgument('source'); //1 if the option was set if (substr($dir, 0, 1) != '/') { - $dir = $sysPath . $dir; + $dir = $sysPath.$dir; } if (!is_dir($dir)) { - $output->writeln($dir. ' was not confirmed as a directory (if not starting with /, it is considered as relative to Chamilo\'s root folder)'); + $output->writeln($dir.' was not confirmed as a directory (if not starting with /, it is considered as relative to Chamilo\'s root folder)'); + return; } $this->ext = $input->getOption('ext'); @@ -109,12 +108,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $filter = function (\SplFileInfo $file, $ext, $orig) { $combinedExt = '.'.$orig.'.'.$ext; $combinedExtLength = strlen($combinedExt); - $extLength = strlen('.' . $ext); + $extLength = strlen('.'.$ext); if (substr($file->getRealPath(), -$combinedExtLength) == $combinedExt) { return false; } - if (is_file(substr($file->getRealPath(), 0, -$extLength) . $combinedExt)) { + if (is_file(substr($file->getRealPath(), 0, -$extLength).$combinedExt)) { $this->excluded[] = $file; + return false; } }; @@ -137,6 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } } $output->writeln('No video left to convert'); + return; } @@ -165,17 +166,17 @@ protected function execute(InputInterface $input, OutputInterface $output) $origNameCommand = preg_replace('/\s/', '\ ', $origName); $origNameCommand = preg_replace('/\(/', '\(', $origNameCommand); $origNameCommand = preg_replace('/\)/', '\)', $origNameCommand); - $output->writeln('ffmpeg -i ' . $newNameCommand . ' -b ' . $bitRate . 'k -f ' . $this->ext . ' -vcodec ' . $vcodec . ' -acodec copy -r ' . $fps . ' ' . $origNameCommand); - $exec = @system('ffmpeg -i ' . $newNameCommand . ' -b ' . $bitRate . 'k -f ' . $this->ext . ' -vcodec ' . $vcodec . ' -acodec copy -r ' . $fps . ' ' . $origNameCommand, $out); + $output->writeln('ffmpeg -i '.$newNameCommand.' -b '.$bitRate.'k -f '.$this->ext.' -vcodec '.$vcodec.' -acodec copy -r '.$fps.' '.$origNameCommand); + $exec = @system('ffmpeg -i '.$newNameCommand.' -b '.$bitRate.'k -f '.$this->ext.' -vcodec '.$vcodec.' -acodec copy -r '.$fps.' '.$origNameCommand, $out); $sizeNew += filesize($origName); - $counter ++; + $counter++; } } $output->writeln(''); $output->writeln('Done converting all videos from '.$dir); - $output->writeln('Total videos converted: ' . $counter . ' videos in ' . (time() - $time) .' seconds'); - $output->writeln('Total size of old videos combined: ' . round($sizeOrig/(1024*1024)).'M'); - $output->writeln('Total size of all new videos combined: ' . round($sizeNew/(1024*1024)).'M'); + $output->writeln('Total videos converted: '.$counter.' videos in '.(time() - $time).' seconds'); + $output->writeln('Total size of old videos combined: '.round($sizeOrig / (1024 * 1024)).'M'); + $output->writeln('Total size of all new videos combined: '.round($sizeNew / (1024 * 1024)).'M'); //$this->removeFiles($files, $output); } } diff --git a/src/Command/Files/DeleteCoursesCommand.php b/src/Command/Files/DeleteCoursesCommand.php index 1b251bf..cf8692c 100644 --- a/src/Command/Files/DeleteCoursesCommand.php +++ b/src/Command/Files/DeleteCoursesCommand.php @@ -13,13 +13,14 @@ * Class DeleteMultiUrlCommand * Clean the files and database from one URL of the multi-URLs list, avoiding * all resources used by more than one URL, but trying to disassociate them - * progressively + * progressively. + * * @todo Add support for version 2.* */ class DeleteCoursesCommand extends DatabaseCommand { /** - * Define options for the command + * Define options for the command. */ protected function configure(): void { @@ -66,9 +67,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -87,6 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (empty($courseId) && empty($courseCode) && empty($courseCategory) && empty($beforeDate)) { $output->writeln('At least one search criteria (id, code, category or date) must be provided.'); $output->writeln('Use "--help" param for details.'); + return; } @@ -97,10 +97,10 @@ protected function execute(InputInterface $input, OutputInterface $output) FROM course WHERE id = $courseId "; if (!empty($beforeDate)) { - $output->writeln('ID-based course search: ' . $courseId . ' and date < ' . $beforeDate); + $output->writeln('ID-based course search: '.$courseId.' and date < '.$beforeDate); $sql .= " AND creation_date < '$beforeDate' "; } else { - $output->writeln('ID-based course search: ' . $courseId); + $output->writeln('ID-based course search: '.$courseId); } $sql .= " ORDER BY creation_date"; } elseif (!empty($courseCode)) { @@ -108,10 +108,10 @@ protected function execute(InputInterface $input, OutputInterface $output) FROM course WHERE code = '$courseCode' "; if (!empty($beforeDate)) { - $output->writeln('Code-based course search: ' . $courseCode . ' and date < ' . $beforeDate); + $output->writeln('Code-based course search: '.$courseCode.' and date < '.$beforeDate); $sql .= " AND creation_date < '$beforeDate' "; } else { - $output->writeln('Code-based course search: ' . $courseCode); + $output->writeln('Code-based course search: '.$courseCode); } $sql .= " ORDER BY creation_date"; } elseif (!empty($courseCategory)) { @@ -119,14 +119,14 @@ protected function execute(InputInterface $input, OutputInterface $output) FROM course WHERE category_code = '$courseCategory'"; if (!empty($beforeDate)) { - $output->writeln('Category-based course search: ' . $courseCategory . ' and date < ' . $beforeDate); + $output->writeln('Category-based course search: '.$courseCategory.' and date < '.$beforeDate); $sql .= " AND creation_date < '$beforeDate' "; } else { - $output->writeln('Category-based course search: ' . $courseCategory); + $output->writeln('Category-based course search: '.$courseCategory); } $sql .= " ORDER BY creation_date"; } elseif (!empty($beforeDate)) { - $output->writeln('Category-based course search: ' . $beforeDate); + $output->writeln('Category-based course search: '.$beforeDate); $sql = "SELECT id, code, category_code, creation_date FROM course WHERE creation_date < '$beforeDate' @@ -140,29 +140,28 @@ protected function execute(InputInterface $input, OutputInterface $output) $courses[$row['id']] = [ 'code' => $row['code'], 'category' => $row['category_code'], - 'date' => $row['creation_date'] + 'date' => $row['creation_date'], ]; $courseIdsString .= $row['id'].', '; } $courseIdsString = substr($courseIdsString, 0, -2); if (count($courses) >= 1) { - $output->writeln('ID' . "\t" . 'Code ' . "\t\t" . 'Category' . "\t" . 'Creation date'); + $output->writeln('ID'."\t".'Code '."\t\t".'Category'."\t".'Creation date'); foreach ($courses as $id => $course) { $output->writeln( - $id . "\t" . - $course['code'] . "\t\t" . - (empty($course['category'])?'--none--':$course['category']) . "\t" . + $id."\t". + $course['code']."\t\t". + (empty($course['category']) ? '--none--' : $course['category'])."\t". $course['date'] ); } } else { $output->writeln('No course found with that criteria. Bye bye.'); + return; } - - // Get courses vs URL match and measure disk usage $sql = "SELECT c.id, u.course_code, u.access_url_id, c.directory FROM access_url_rel_course u, course c @@ -200,10 +199,10 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!is_dir($courseDir)) { $size = 'N/A'; } else { - $res = @exec('du -s ' . $courseDir); + $res = @exec('du -s '.$courseDir); $res = preg_split('/\s/', $res); $size = $res[0]; - $output->writeln($id . ":\t" . $size); + $output->writeln($id.":\t".$size); if ($unique == 'yes') { $totalDiskUsage += $size; } @@ -240,34 +239,35 @@ protected function execute(InputInterface $input, OutputInterface $output) $cUrls = $coursesUrl[$id]; if (count($cUrls) > 1) { $output->writeln( - 'Course ' . $course['code'] . ' is used ' . - 'by more than one URL (' . implode(',', $coursesUrl[$id]) . ').' + 'Course '.$course['code'].' is used '. + 'by more than one URL ('.implode(',', $coursesUrl[$id]).').' ); } foreach ($cUrls as $urlId) { $output->writeln( - 'Deleting references to course ID ' . $id . ' in URL (url ' . $urlId . ')...' + 'Deleting references to course ID '.$id.' in URL (url '.$urlId.')...' ); $this->unlinkCourse($input, $output, $course['code'], $urlId); } // Removal of the course linking in all URLs is over. Delete the // course itself - $output->writeln('All references clear. Now deleting course ' . $id); + $output->writeln('All references clear. Now deleting course '.$id); $this->deleteCourse($input, $output, $course['code']); } } $output->writeln(''); - $output->writeln('All done. ' . $totalDiskUsage . 'KB have been freed. Bye bye.'); + $output->writeln('All done. '.$totalDiskUsage.'KB have been freed. Bye bye.'); $output->writeln(''); } /** * Delete all references to a course inside a given URL, but do not delete - * the course itself - * @param OutputInterface $output - * @param string $courseCode - * @param int $urlId - * @return bool + * the course itself. + * + * @param string $courseCode + * @param int $urlId + * + * @return bool */ private function unlinkCourse($input, OutputInterface $output, $courseCode, $urlId) { @@ -282,14 +282,14 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url FROM session_rel_course src JOIN access_url_rel_session aurs ON aurs.session_id = src.id_session - WHERE src.course_code = '" . $courseCode . "' + WHERE src.course_code = '".$courseCode."' AND aurs.access_url_id = $urlId"; $stmt = $connection->query($sql); $sessions = []; while ($row = $stmt->fetch()) { $sessions[] = $row['id_session']; } - $output->writeln('Sessions using course ' . $courseCode . ' in URL ' . $urlId . ': ' . implode( + $output->writeln('Sessions using course '.$courseCode.' in URL '.$urlId.': '.implode( ',', $sessions )); @@ -297,18 +297,18 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url // 2. Delete the session_rel_course and session_rel_course_rel_user foreach ($sessions as $sessionId) { $sql = "DELETE FROM session_rel_course_rel_user " - . "WHERE id_session = $sessionId " - . " AND course_code = '$courseCode' "; + ."WHERE id_session = $sessionId " + ." AND course_code = '$courseCode' "; $stmt = $connection->query($sql); $sql = "DELETE FROM session_rel_course " - . "WHERE id_session = $sessionId " - . " AND course_code = '$courseCode' "; + ."WHERE id_session = $sessionId " + ." AND course_code = '$courseCode' "; $stmt = $connection->query($sql); $sql = "SELECT count(*) as courseCount FROM session_rel_course WHERE id_session = $sessionId"; $stmt = $connection->query($sql); while ($row = $stmt->fetch()) { if ($row['courseCount'] === 0) { - $output->writeln('No course left in session ' . $sessionId . ' so deleting the session'); + $output->writeln('No course left in session '.$sessionId.' so deleting the session'); // No course within this session => delete the session // @todo: use sessionmanager::delete_session($sessionId) $sqlDelete = "DELETE FROM session WHERE id = $sessionId"; @@ -331,8 +331,8 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url // 3. Delete the access_url_rel_course reference $sql = "DELETE FROM access_url_rel_course " - . " WHERE access_url_id = $urlId " - . " AND course_code = '$courseCode'"; + ." WHERE access_url_id = $urlId " + ." AND course_code = '$courseCode'"; $connection->query($sql); } @@ -340,9 +340,11 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url * Delete a course completely * This operation follows the "unlink course" operation so that it just * completes it, but only in case the course is used only once. + * * @param object Output interface * @param string Course code - * @return bool + * + * @return bool */ private function deleteCourse($input, OutputInterface $output, $courseCode) { @@ -450,19 +452,20 @@ private function deleteCourse($input, OutputInterface $output, $courseCode) $connection->query($sql); } $output->writeln( - 'Deleted all references to course ' . $courseCode . ' in c_* tables.' + 'Deleted all references to course '.$courseCode.' in c_* tables.' ); $sysPath = $this->getConfigurationHelper()->getSysPath(); - $coursePath = $sysPath . 'courses/' . $courseDir; + $coursePath = $sysPath.'courses/'.$courseDir; $fs = new Filesystem(); $fs->remove($coursePath); - $output->writeln('Removed files from ' . $coursePath); + $output->writeln('Removed files from '.$coursePath); // Delete the course itself from the course table $sql = "DELETE FROM course WHERE id = $cid"; $connection->query($sql); $output->writeln( - 'Deleted course ' . $courseCode . ' reference in course table.' + 'Deleted course '.$courseCode.' reference in course table.' ); + return true; } } diff --git a/src/Command/Files/DeleteMultiUrlCommand.php b/src/Command/Files/DeleteMultiUrlCommand.php index 3280680..28cf7db 100644 --- a/src/Command/Files/DeleteMultiUrlCommand.php +++ b/src/Command/Files/DeleteMultiUrlCommand.php @@ -7,21 +7,23 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Filesystem\Filesystem; /** * Class DeleteMultiUrlCommand * Clean the files and database from one URL of the multi-URLs list, avoiding * all resources used by more than one URL, but trying to disassociate them - * progressively + * progressively. + * * @package Chash\Command\Files + * * @todo Add support for version 2.* */ class DeleteMultiUrlCommand extends DatabaseCommand { /** - * Define options for the command + * Define options for the command. */ protected function configure(): void { @@ -61,9 +63,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -84,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } if ($list) { if (count($urls) > 1) { - $output->writeln('ID' . "\t" . 'Active?' . "\t" . 'URL'); + $output->writeln('ID'."\t".'Active?'."\t".'URL'); $preventDelete = ''; foreach ($urls as $id => $url) { if ($id == 1) { @@ -93,14 +93,15 @@ protected function execute(InputInterface $input, OutputInterface $output) $preventDelete = ''; } $output->writeln( - $id . "\t" . - $url['active'] . "\t" . - $url['url'] . "\t" . + $id."\t". + $url['active']."\t". + $url['url']."\t". $preventDelete ); } } else { $output->writeln('Only one URL. Please use another command to delete'); + return; } } @@ -109,10 +110,10 @@ protected function execute(InputInterface $input, OutputInterface $output) // Get all courses by URL $sql = "SELECT u.course_code, u.access_url_id, c.directory " - . " FROM access_url_rel_course u, course c " - . " WHERE u.course_code = c.code " + ." FROM access_url_rel_course u, course c " + ." WHERE u.course_code = c.code " //." WHERE access_url_id != 1 " - . " ORDER BY access_url_id, course_code ASC"; + ." ORDER BY access_url_id, course_code ASC"; $stmt = $connection->query($sql); $urlCourses = []; $coursesUrl = []; @@ -134,7 +135,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $count = count($urlCourses); if ($count > 0) { $output->writeln('List of URLs vs courses'); - $output->writeln('URL ID' . "\t" . 'Only in this URL?' . "\t" . ($du ? 'Size (KB)' . "\t\t" : '') . 'Course code'); + $output->writeln('URL ID'."\t".'Only in this URL?'."\t".($du ? 'Size (KB)'."\t\t" : '').'Course code'); foreach ($urlCourses as $url => $courses) { if (!empty($urlId)) { // if a URL was defined, skip other URLs @@ -151,7 +152,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!is_dir($courseDir)) { $size = 'N/A'; } else { - $res = @exec('du -s ' . $courseDir); + $res = @exec('du -s '.$courseDir); $res = preg_split('/\s/', $res); $size = $res[0]; if ($unique == 'yes') { @@ -159,7 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } } } - $output->writeln($url . "\t" . $unique . "\t\t\t" . ($du ? $size . "\t\t" : '') . $code); + $output->writeln($url."\t".$unique."\t\t\t".($du ? $size."\t\t" : '').$code); } } } @@ -172,19 +173,21 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln(''); if ($urlId == 1) { $output->writeln('URL 1 cannot be deleted as it is the main URL'); + return; } - $output->writeln('Selected URL: ' . $urlId . ' (' . $urls[$urlId]['url'] . ')'); + $output->writeln('Selected URL: '.$urlId.' ('.$urls[$urlId]['url'].')'); if (!in_array($urlId, array_keys($urls))) { $output->writeln( - 'URL ' . $urlId . ' does not exist. ' . + 'URL '.$urlId.' does not exist. '. 'Please use the --list param to see the list of available URLs' ); + return; } $helper = $this->getHelperSet()->get('question'); $question = new ConfirmationQuestion( - 'Are you sure you want to delete URL ' . $urlId . ' and all the courses that are used only in this URL? (y/N)', + 'Are you sure you want to delete URL '.$urlId.' and all the courses that are used only in this URL? (y/N)', false ); if (!$helper->ask($input, $output, $question)) { @@ -200,18 +203,18 @@ protected function execute(InputInterface $input, OutputInterface $output) foreach ($urlCourses[$urlId] as $courseCode) { if (count($coursesUrl[$courseCode]) > 1) { $output->writeln( - 'Course ' . $courseCode . ' is used ' . - 'by more than one URL (' . - implode(',', $coursesUrl[$courseCode]) . + 'Course '.$courseCode.' is used '. + 'by more than one URL ('. + implode(',', $coursesUrl[$courseCode]). ').' ); $output->writeln( - 'Deleting only references to given URL (' . $urlId . ')...' + 'Deleting only references to given URL ('.$urlId.')...' ); $this->unlinkCourse($input, $output, $courseCode, $urlId); } else { // The course is only in the given URL $output->writeln( - 'Course ' . $courseCode . ' is only used ' . + 'Course '.$courseCode.' is only used '. 'on this portal. Proceeding with deletion...' ); $this->unlinkCourse($input, $output, $courseCode, $urlId); @@ -241,17 +244,17 @@ protected function execute(InputInterface $input, OutputInterface $output) $connection->query($sql); } else { if ($urls[0] == $urlId) { - $output->writeln('User ' . $user . ' is only in URL ' . $urlId . ', deleting...'); + $output->writeln('User '.$user.' is only in URL '.$urlId.', deleting...'); // DELETE user $this->deleteUser($input, $output, $user); } else { - $output->writeln('User ' . $user . 'is in only one URL, but not this one. Skipping.'); + $output->writeln('User '.$user.'is in only one URL, but not this one. Skipping.'); } } } } // Everything removed. Delete URL - $sql = 'DELETE FROM access_url WHERE id = ' . $urlId; + $sql = 'DELETE FROM access_url WHERE id = '.$urlId; $connection->query($sql); } } @@ -259,11 +262,12 @@ protected function execute(InputInterface $input, OutputInterface $output) /** * Delete all references to a course inside a given URL, but do not delete - * the course itself - * @param OutputInterface $output - * @param string $courseCode - * @param int $urlId - * @return bool + * the course itself. + * + * @param string $courseCode + * @param int $urlId + * + * @return bool */ private function unlinkCourse($input, OutputInterface $output, $courseCode, $urlId) { @@ -279,7 +283,7 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url JOIN access_url_rel_session aurs ON aurs.session_id = src.id_session WHERE - src.course_code = '" . $courseCode . "' AND + src.course_code = '".$courseCode."' AND aurs.access_url_id = $urlId"; $stmt = $connection->query($sql); $sessions = []; @@ -296,18 +300,18 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url // 2. Delete the session_rel_course and session_rel_course_rel_user foreach ($sessions as $sessionId) { $sql = "DELETE FROM session_rel_course_rel_user " - . "WHERE id_session = $sessionId " - . " AND course_code = '$courseCode' "; + ."WHERE id_session = $sessionId " + ." AND course_code = '$courseCode' "; $stmt = $connection->query($sql); $sql = "DELETE FROM session_rel_course " - . "WHERE id_session = $sessionId " - . " AND course_code = '$courseCode' "; + ."WHERE id_session = $sessionId " + ." AND course_code = '$courseCode' "; $stmt = $connection->query($sql); $sql = "SELECT count(*) as courseCount FROM session_rel_course WHERE id_session = $sessionId"; $stmt = $connection->query($sql); while ($row = $stmt->fetch()) { if ($row['courseCount'] === 0) { - $output->writeln('No course left in session ' . $sessionId . ' so deleting the session'); + $output->writeln('No course left in session '.$sessionId.' so deleting the session'); // No course within this session => delete the session // @todo: use sessionmanager::delete_session($sessionId) $sqlDelete = "DELETE FROM session WHERE id = $sessionId"; @@ -330,8 +334,8 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url // 3. Delete the access_url_rel_course reference $sql = "DELETE FROM access_url_rel_course " - . " WHERE access_url_id = $urlId " - . " AND course_code = '$courseCode'"; + ." WHERE access_url_id = $urlId " + ." AND course_code = '$courseCode'"; $stmt = $connection->query($sql); } @@ -339,9 +343,11 @@ private function unlinkCourse($input, OutputInterface $output, $courseCode, $url * Delete a course completely * This operation follows the "unlink course" operation so that it just * completes it, but only in case the course is used only once. + * * @param object Output interface * @param string Course code - * @return bool + * + * @return bool */ private function deleteCourse($input, OutputInterface $output, $courseCode) { @@ -448,13 +454,14 @@ private function deleteCourse($input, OutputInterface $output, $courseCode) $stmt = $connection->query($sql); } $output->writeln( - 'Deleted all references to course ' . $courseCode . ' in c_* tables.' + 'Deleted all references to course '.$courseCode.' in c_* tables.' ); $sysPath = $this->getConfigurationHelper()->getSysPath(); - $coursePath = $sysPath . 'courses/' . $courseDir; + $coursePath = $sysPath.'courses/'.$courseDir; $fs = new Filesystem(); $fs->remove($coursePath); - $output->writeln('Removed files from ' . $coursePath); + $output->writeln('Removed files from '.$coursePath); + return true; } @@ -463,9 +470,12 @@ private function deleteCourse($input, OutputInterface $output, $courseCode) * very dangerous function that should only be accessible by * super-admins. Other roles should only be able to disable a user, * which removes access to the platform but doesn't delete anything. + * * @param object Output interface * @param int User ID - * @return bool + * + * @return bool + * * @todo Use UserManager::delete_user() instead */ private function deleteUser($input, OutputInterface $output, $userId) @@ -516,9 +526,9 @@ private function deleteUser($input, OutputInterface $output, $userId) $stmt = $connection->query($sql); $row = $stmt->fetch(); if ($row['selected_value'] == 'true') { - $sub = substr($userId, 0, 1) . '/'; + $sub = substr($userId, 0, 1).'/'; } - $img_path = $sysPath . 'main/upload/users/'. $sub . $userId; + $img_path = $sysPath.'main/upload/users/'.$sub.$userId; if (file_exists($img_path)) { unlink($img_path); } @@ -562,7 +572,8 @@ private function deleteUser($input, OutputInterface $output, $userId) //$user_id_manager = api_get_user_id(); //event_system(LOG_USER_DELETE, LOG_USER_ID, $userId, api_get_utc_datetime(), $user_id_manager, null, $user_info); //event_system(LOG_USER_DELETE, LOG_USER_OBJECT, $user_info, api_get_utc_datetime(), $user_id_manager, null, $user_info); - $output->writeln('Removed user ' . $userId); + $output->writeln('Removed user '.$userId); + return true; } } diff --git a/src/Command/Files/GenerateTempFileStructureCommand.php b/src/Command/Files/GenerateTempFileStructureCommand.php index c36f4e8..fa07dad 100644 --- a/src/Command/Files/GenerateTempFileStructureCommand.php +++ b/src/Command/Files/GenerateTempFileStructureCommand.php @@ -3,47 +3,22 @@ namespace Chash\Command\Files; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Exception\IOException; +use Symfony\Component\Filesystem\Filesystem; /** - * Class GenerateTempFileStructureCommand + * Class GenerateTempFileStructureCommand. + * * @package Chash\Command\Files */ class GenerateTempFileStructureCommand extends DatabaseCommand { - protected function configure(): void - { - parent::configure(); - $this - ->setName('files:generate_temp_folders') - ->setDescription('Generate temp folder structure: twig'); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - parent::execute($input, $output); - $this->writeCommandHeader($output, 'Generating temp folders.'); - - // Data folders - $files = $this->getConfigurationHelper()->getTempFolderList(); - $this->createFolders($output, $files, 0777); - } - /** - * @param OutputInterface $output * @param array $files * @param $permission + * * @return int */ public function createFolders(OutputInterface $output, $files, $permission) @@ -52,6 +27,7 @@ public function createFolders(OutputInterface $output, $files, $permission) if (empty($files)) { $output->writeln('No files found.'); + return 0; } @@ -73,4 +49,25 @@ public function createFolders(OutputInterface $output, $files, $permission) echo "\n An error occurred while removing the directory: ".$e->getMessage()."\n "; } } + + protected function configure(): void + { + parent::configure(); + $this + ->setName('files:generate_temp_folders') + ->setDescription('Generate temp folder structure: twig'); + } + + /** + * @return int|void|null + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + $this->writeCommandHeader($output, 'Generating temp folders.'); + + // Data folders + $files = $this->getConfigurationHelper()->getTempFolderList(); + $this->createFolders($output, $files, 0777); + } } diff --git a/src/Command/Files/MailConfCommand.php b/src/Command/Files/MailConfCommand.php index e190986..6eea951 100644 --- a/src/Command/Files/MailConfCommand.php +++ b/src/Command/Files/MailConfCommand.php @@ -3,15 +3,13 @@ namespace Chash\Command\Files; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * Class MailConfCommand - * Returns the current mail configuration + * Returns the current mail configuration. + * * @package Chash\Command\Files */ class MailConfCommand extends DatabaseCommand @@ -25,9 +23,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -36,12 +32,12 @@ protected function execute(InputInterface $input, OutputInterface $output) $path = $this->getHelper('configuration')->getConfigurationPath(); $path .= 'mail.conf.php'; - define('IS_WINDOWS_OS', strtolower(substr(php_uname(), 0, 3)) == 'win'?true:false); + define('IS_WINDOWS_OS', strtolower(substr(php_uname(), 0, 3)) == 'win' ? true : false); $platform_email = []; if (isset($path) && is_file($path)) { $output->writeln('File: '.$path); $lines = file($path); - $list = ['SMTP_HOST','SMTP_PORT','SMTP_MAILER','SMTP_AUTH','SMTP_USER','SMTP_PASS']; + $list = ['SMTP_HOST', 'SMTP_PORT', 'SMTP_MAILER', 'SMTP_AUTH', 'SMTP_USER', 'SMTP_PASS']; foreach ($lines as $line) { $match = []; if (preg_match("/platform_email\['(.*)'\]/", $line, $match)) { diff --git a/src/Command/Files/ReplaceURLCommand.php b/src/Command/Files/ReplaceURLCommand.php index abf94ea..3e9cfe4 100644 --- a/src/Command/Files/ReplaceURLCommand.php +++ b/src/Command/Files/ReplaceURLCommand.php @@ -10,7 +10,7 @@ /** * Class ReplaceURLCommand - * Clean the archives directory, leaving only index.html, twig and Serializer + * Clean the archives directory, leaving only index.html, twig and Serializer. */ class ReplaceURLCommand extends DatabaseCommand { @@ -33,9 +33,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -119,7 +117,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!empty($results)) { foreach ($results as $row) { - $filePath = $coursePath . '/' . $row['directory'] . '/document' . $row['path']; + $filePath = $coursePath.'/'.$row['directory'].'/document'.$row['path']; $output->writeln($filePath); if (file_exists($filePath) && !empty($row['path'])) { if (!$dryRun) { @@ -162,7 +160,7 @@ private function getTables() 'c_tool_intro' => ['intro_text'], 'track_e_attempt' => ['answer'], 'c_link' => ['url'], - 'c_glossary' => ['description'] + 'c_glossary' => ['description'], ]; } } diff --git a/src/Command/Files/SetPermissionsAfterInstallCommand.php b/src/Command/Files/SetPermissionsAfterInstallCommand.php index a028da7..188690c 100644 --- a/src/Command/Files/SetPermissionsAfterInstallCommand.php +++ b/src/Command/Files/SetPermissionsAfterInstallCommand.php @@ -3,78 +3,25 @@ namespace Chash\Command\Files; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Filesystem\Filesystem; /** - * Class SetPermissionsAfterInstallCommand + * Class SetPermissionsAfterInstallCommand. + * * @package Chash\Command\Files */ class SetPermissionsAfterInstallCommand extends DatabaseCommand { - protected function configure(): void - { - parent::configure(); - $this - ->setName('files:set_permissions_after_install') - ->setDescription('Set permissions') - ->addOption('linux-user', null, InputOption::VALUE_OPTIONAL, 'user', 'www-data') - ->addOption('linux-group', null, InputOption::VALUE_OPTIONAL, 'group', 'www-data'); - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - parent::execute($input, $output); - $this->writeCommandHeader($output, 'Setting permissions ...'); - - $linuxUser = $input->getOption('linux-user'); - $linuxGroup = $input->getOption('linux-group'); - - // All files - $this->writeCommandHeader($output, 'System folders...'); - $files = $this->getConfigurationHelper()->getSysFolders(); - $this->setPermissions($output, $files, 0777, $linuxUser, $linuxGroup, false); - - $this->writeCommandHeader($output, 'System files ...'); - $files = $this->getConfigurationHelper()->getSysFiles(); - $this->setPermissions($output, $files, null, $linuxUser, $linuxGroup, false); - - // Data folders - $this->writeCommandHeader($output, 'Data folders ...'); - $files = $this->getConfigurationHelper()->getDataFolders(); - $this->setPermissions($output, $files, 0777, $linuxUser, $linuxGroup, false); - - // Config folders - $this->writeCommandHeader($output, 'Config folders ...'); - $files = $this->getConfigurationHelper()->getConfigFolders(); - $this->setPermissions($output, $files, 0555, $linuxUser, $linuxGroup, false); - - $this->writeCommandHeader($output, 'Config files...'); - $files = $this->getConfigurationHelper()->getConfigFiles(); - $this->setPermissions($output, $files, 0555, $linuxUser, $linuxGroup, false); - - // Temp folders - $this->writeCommandHeader($output, 'Temp files...'); - $files = $this->getConfigurationHelper()->getTempFolders(); - $this->setPermissions($output, $files, 0777, $linuxUser, $linuxGroup, false); - } - /** - * @param OutputInterface $output * @param array $files * @param $permission * @param $user * @param $group * @param bool $listFiles + * * @return int */ public function setPermissions( @@ -89,6 +36,7 @@ public function setPermissions( if (empty($files)) { $output->writeln('No files found.'); + return 0; } @@ -144,4 +92,54 @@ public function setPermissions( echo "\n An error occurred while removing the directory: ".$e->getMessage()."\n "; } } + + protected function configure(): void + { + parent::configure(); + $this + ->setName('files:set_permissions_after_install') + ->setDescription('Set permissions') + ->addOption('linux-user', null, InputOption::VALUE_OPTIONAL, 'user', 'www-data') + ->addOption('linux-group', null, InputOption::VALUE_OPTIONAL, 'group', 'www-data'); + } + + /** + * @return int|void|null + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + parent::execute($input, $output); + $this->writeCommandHeader($output, 'Setting permissions ...'); + + $linuxUser = $input->getOption('linux-user'); + $linuxGroup = $input->getOption('linux-group'); + + // All files + $this->writeCommandHeader($output, 'System folders...'); + $files = $this->getConfigurationHelper()->getSysFolders(); + $this->setPermissions($output, $files, 0777, $linuxUser, $linuxGroup, false); + + $this->writeCommandHeader($output, 'System files ...'); + $files = $this->getConfigurationHelper()->getSysFiles(); + $this->setPermissions($output, $files, null, $linuxUser, $linuxGroup, false); + + // Data folders + $this->writeCommandHeader($output, 'Data folders ...'); + $files = $this->getConfigurationHelper()->getDataFolders(); + $this->setPermissions($output, $files, 0777, $linuxUser, $linuxGroup, false); + + // Config folders + $this->writeCommandHeader($output, 'Config folders ...'); + $files = $this->getConfigurationHelper()->getConfigFolders(); + $this->setPermissions($output, $files, 0555, $linuxUser, $linuxGroup, false); + + $this->writeCommandHeader($output, 'Config files...'); + $files = $this->getConfigurationHelper()->getConfigFiles(); + $this->setPermissions($output, $files, 0555, $linuxUser, $linuxGroup, false); + + // Temp folders + $this->writeCommandHeader($output, 'Temp files...'); + $files = $this->getConfigurationHelper()->getTempFolders(); + $this->setPermissions($output, $files, 0777, $linuxUser, $linuxGroup, false); + } } diff --git a/src/Command/Files/ShowDiskUsageCommand.php b/src/Command/Files/ShowDiskUsageCommand.php index e221204..80d2c17 100644 --- a/src/Command/Files/ShowDiskUsageCommand.php +++ b/src/Command/Files/ShowDiskUsageCommand.php @@ -3,18 +3,17 @@ namespace Chash\Command\Files; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Helper\TableHelper; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Helper\TableHelper; +use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; /** * Class ShowDiskUsageCommand * Show the total disk usage per course compared to the maximum space allowed - * for the corresponding courses + * for the corresponding courses. + * * @package Chash\Command\Files */ class ShowDiskUsageCommand extends DatabaseCommand @@ -61,16 +60,14 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); $kb = $input->getOption('KB'); //1 if the option was set - $div = 1024*1024; + $div = 1024 * 1024; $div2 = 1024; $unit = 'MB'; @@ -119,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output) while ($row = $stmt->fetch()) { $globalCourses[$row['directory']] = [ 'code' => $row['code'], - 'quota' => $row['disk_quota'] + 'quota' => $row['disk_quota'], ]; } $globalCoursesSizeSum = []; @@ -166,9 +163,9 @@ protected function execute(InputInterface $input, OutputInterface $output) [ 'Portal', 'Code', - $docsOnly. '(' . $unit . ')', - 'DBDocs(' . $unit . ')', - 'DBQuota(' . $unit . ')', + $docsOnly.'('.$unit.')', + 'DBDocs('.$unit.')', + 'DBQuota('.$unit.')', 'UsedDiskVsDBQuota', ] ); @@ -211,36 +208,36 @@ protected function execute(InputInterface $input, OutputInterface $output) [ $portalName, $globalCourses[$file]['code'], - round($size/$div2, $precision), - round($dbSize/$div2, $precision), + round($size / $div2, $precision), + round($dbSize / $div2, $precision), $finalList[$globalCourses[$file]['code']]['quota'], - $finalList[$globalCourses[$file]['code']]['rate'] + $finalList[$globalCourses[$file]['code']]['rate'], ] ); $localSize += $size; $localDbSize += $dbSize; } else { - $res = exec('du -s ' . $dir->getRealPath() . $dirDoc); + $res = exec('du -s '.$dir->getRealPath().$dirDoc); $res = preg_split('/\s/', $res); $size = $res[0]; if (isset($localCourses[$file]['code'])) { $localSize += $size; //always add size to local portal (but only add to total size if new) $code = $localCourses[$file]['code']; - $dbSize = round($localCourses[$file]['dbSize']/$div2, $precision); + $dbSize = round($localCourses[$file]['dbSize'] / $div2, $precision); $localDbSize += $dbSize; - $quota = round($localCourses[$file]['quota']/$div, 0); + $quota = round($localCourses[$file]['quota'] / $div, 0); $rate = '-'; if ($quota > 0) { - $rate = round((round($size/$div2, 2)/$quota)*100, 0); + $rate = round((round($size / $div2, 2) / $quota) * 100, 0); } $finalList[$code] = [ - 'code' => $code, - 'dir' => $file, - 'size' => $size, - 'dbSize'=> $dbSize, + 'code' => $code, + 'dir' => $file, + 'size' => $size, + 'dbSize' => $dbSize, 'quota' => $quota, - 'rateVsDisk' => $rate, + 'rateVsDisk' => $rate, ]; //$finalListOrder[$code] = $size; $totalSize += $size; //only add to total if new course @@ -250,10 +247,10 @@ protected function execute(InputInterface $input, OutputInterface $output) [ $portalName, $code, - round($size/$div2, $precision), - round($dbSize/$div2, $precision), + round($size / $div2, $precision), + round($dbSize / $div2, $precision), $finalList[$code]['quota'], - $rate + $rate, ] ); } elseif (!isset($globalCourses[$file]['code']) && !isset($orphanList[$file])) { @@ -268,8 +265,8 @@ protected function execute(InputInterface $input, OutputInterface $output) [ $portalName, 'SubtotalWithoutOrphans', - round($localSize/$div2, $precision), - round($localDbSize/$div2, $precision), + round($localSize / $div2, $precision), + round($localDbSize / $div2, $precision), ] ); } @@ -280,21 +277,21 @@ protected function execute(InputInterface $input, OutputInterface $output) [ 'Portal', 'Code', - $docsOnly . '(' . $unit . ')', - 'DBDocs(' . $unit . ')', - 'Quota(' . $unit . ')', - 'UsedRatio' + $docsOnly.'('.$unit.')', + 'DBDocs('.$unit.')', + 'Quota('.$unit.')', + 'UsedRatio', ] ); //$output->writeln('CCC Code;Size' . $docsOnly . '(' . $unit . ');Quota(' . $unit . ');UsedRatio'); foreach ($orphanList as $key => $orphan) { $size = $orphan['size']; - $sizeToShow = !empty($orphan['size']) ? round($orphan['size']/$div2, $precision) : 0; + $sizeToShow = !empty($orphan['size']) ? round($orphan['size'] / $div2, $precision) : 0; //$output->writeln($portalName . ';ORPHAN-DIR: ' . $key . ';' . $sizeToShow . ';;;'); $table->addRow([ $portalName, - 'ORPHAN-DIR: ' . $key, - $sizeToShow + 'ORPHAN-DIR: '.$key, + $sizeToShow, ]); $totalSize += $size; } @@ -304,8 +301,8 @@ protected function execute(InputInterface $input, OutputInterface $output) [ $portalName, 'Total size', - round($totalSize/$div2, $precision), - round($totalDbSize/$div2, $precision) + round($totalSize / $div2, $precision), + round($totalDbSize / $div2, $precision), ] ); diff --git a/src/Command/Files/UpdateDirectoryMaxSizeCommand.php b/src/Command/Files/UpdateDirectoryMaxSizeCommand.php index 3cab366..8cb3697 100644 --- a/src/Command/Files/UpdateDirectoryMaxSizeCommand.php +++ b/src/Command/Files/UpdateDirectoryMaxSizeCommand.php @@ -37,9 +37,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return bool|int|null|void + * @return bool|int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -53,6 +51,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($add == 1) { $this->writeCommandHeader($output, 'Max space needs to be of at least 1MB for each course first'); + return; } @@ -85,7 +84,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $globalCourses[$row['cdir']] = [ 'id' => $row['cid'], 'code' => $row['ccode'], - 'quota' => $row['cquota'] + 'quota' => $row['cquota'], ]; } } diff --git a/src/Command/Info/GetInstancesCommand.php b/src/Command/Info/GetInstancesCommand.php index bbb62e8..a210da6 100644 --- a/src/Command/Info/GetInstancesCommand.php +++ b/src/Command/Info/GetInstancesCommand.php @@ -3,20 +3,74 @@ namespace Chash\Command\Info; use Chash\Command\Common\CommonCommand; +use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Finder\Finder; use Symfony\Component\Finder\SplFileInfo; -use Symfony\Component\Console\Helper\Table; /** - * Class GetInstancesCommand + * Class GetInstancesCommand. + * * @package Chash\Command\Translation */ class GetInstancesCommand extends CommonCommand { + /** + * @param string $configurationFile + * + * @return array + */ + public function getPortalInfoFromConfiguration($configurationFile) + { + $fs = new Filesystem(); + + if ($fs->exists($configurationFile)) { + $lines = file($configurationFile, FILE_IGNORE_NEW_LINES); + $version = ''; + $url = ''; + $packager = ''; + foreach ($lines as $line) { + if (strpos($line, 'system_version') !== false) { + $replace = [ + "\$_configuration['system_version']", + '=', + ';', + "'", + ]; + $version = str_replace($replace, '', $line); + } + if (strpos($line, 'root_web') !== false) { + $replace = [ + "\$_configuration['root_web']", + '=', + ';', + "'", + ]; + $url = str_replace($replace, '', $line); + } + if (strpos($line, 'packager') !== false) { + $replace = [ + "\$_configuration['packager']", + '=', + ';', + "'", + '//', + ]; + $packager = str_replace($replace, '', $line); + } + } + $portal = [$url, $version, $packager, $configurationFile]; + $portal = array_map('trim', $portal); + + return $portal; + } + + return []; + } + protected function configure(): void { $this @@ -36,9 +90,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -81,55 +133,4 @@ protected function execute(InputInterface $input, OutputInterface $output) return null; } - - /** - * @param string $configurationFile - * @return array - */ - public function getPortalInfoFromConfiguration($configurationFile) - { - $fs = new Filesystem(); - - if ($fs->exists($configurationFile)) { - $lines = file($configurationFile, FILE_IGNORE_NEW_LINES); - $version = ''; - $url = ''; - $packager = ''; - foreach ($lines as $line) { - if (strpos($line, 'system_version') !== false) { - $replace = [ - "\$_configuration['system_version']", - '=', - ';', - "'", - ]; - $version = str_replace($replace, '', $line); - } - if (strpos($line, 'root_web') !== false) { - $replace = [ - "\$_configuration['root_web']", - '=', - ';', - "'", - ]; - $url = str_replace($replace, '', $line); - } - if (strpos($line, 'packager') !== false) { - $replace = [ - "\$_configuration['packager']", - '=', - ';', - "'", - '//' - ]; - $packager = str_replace($replace, '', $line); - } - } - $portal = [$url, $version, $packager, $configurationFile]; - $portal = array_map('trim', $portal); - - return $portal; - } - return []; - } } diff --git a/src/Command/Info/WhichCommand.php b/src/Command/Info/WhichCommand.php index fa3c7d4..8132381 100644 --- a/src/Command/Info/WhichCommand.php +++ b/src/Command/Info/WhichCommand.php @@ -10,7 +10,7 @@ /** * Class WhichCommand * Command meant to deal with what the user of this script is calling it for. - * Gives info about where to find some important Chamilo code for specific tools + * Gives info about where to find some important Chamilo code for specific tools. */ class WhichCommand extends InfoCommand { @@ -79,14 +79,14 @@ class WhichCommand extends InfoCommand 'adminpath' => 'main/', ], 'aliases' => [ - 'admin' => ['administration', 'management'], + 'admin' => ['administration', 'management'], 'announcement' => ['announcements', 'news'], 'calendar' => ['agenda', 'schedule', 'events', 'event'], 'attendance' => ['attendances', 'attend', 'assistance'], 'auth' => ['openid', 'cas', 'sso', 'single-sign-on', 'shibboleth', 'drupal'], - ], ]; + protected function configure(): void { parent::configure(); @@ -102,9 +102,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return null|void + * @return void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -139,6 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if (!empty($this->tools[$tool]['libpath'])) { $output->writeln('* Tool\'s general library: '.$this->tools[$tool]['libpath']); } + return 0; } } diff --git a/src/Command/Installation/InstallCommand.php b/src/Command/Installation/InstallCommand.php index 742cea6..401cf04 100644 --- a/src/Command/Installation/InstallCommand.php +++ b/src/Command/Installation/InstallCommand.php @@ -5,16 +5,16 @@ use Chash\Command\Common\CommonCommand; use Chash\Helpers\ConfigurationHelper; use Doctrine\ORM\EntityManager; +use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; use Symfony\Component\Console\Question\Question; use Symfony\Component\Dotenv\Dotenv; -use Symfony\Bundle\FrameworkBundle\Console\Application; -use Symfony\Component\Console\Output\ConsoleOutput; /** * Class InstallCommand. @@ -32,44 +32,6 @@ class InstallCommand extends CommonCommand public $linuxGroup; /** - * Configure command. - */ - protected function configure(): void - { - $this - ->setName('chash:chamilo_install') - ->setDescription('Execute a Chamilo installation to a specified version.') - ->addArgument('version', InputArgument::REQUIRED, 'The version to migrate to.', null) - ->addArgument('path', InputArgument::OPTIONAL, 'The path to the chamilo folder') - ->addOption('download-package', null, InputOption::VALUE_NONE, 'Downloads the chamilo package') - ->addOption('only-download-package', null, InputOption::VALUE_NONE, 'Only downloads the package') - ->addOption('temp-folder', null, InputOption::VALUE_OPTIONAL, 'The temp folder.', '/tmp') - ->addOption('linux-user', null, InputOption::VALUE_OPTIONAL, 'user', 'www-data') - ->addOption('linux-group', null, InputOption::VALUE_OPTIONAL, 'group', 'www-data') - ->addOption('silent', null, InputOption::VALUE_NONE, 'Execute the migration with out asking questions.') - ; - - $params = $this->getPortalSettingsParams(); - - foreach ($params as $key => $value) { - $this->addOption($key, null, InputOption::VALUE_OPTIONAL); - } - - $params = $this->getAdminSettingsParams(); - foreach ($params as $key => $value) { - $this->addOption($key, null, InputOption::VALUE_OPTIONAL); - } - - $params = $this->getDatabaseSettingsParams(); - foreach ($params as $key => $value) { - $this->addOption($key, null, InputOption::VALUE_OPTIONAL); - } - } - - /** - * @param InputInterface $input - * @param OutputInterface $output - * * @return int */ public function installLegacy(InputInterface $input, OutputInterface $output) @@ -92,6 +54,7 @@ public function installLegacy(InputInterface $input, OutputInterface $output) if (!is_writable($configurationPath)) { $output->writeln("Folder ".$configurationPath." must be writable"); + return 0; } else { $output->writeln("Configuration file will be saved here: ".$configurationPath."configuration.php "); @@ -106,6 +69,7 @@ public function installLegacy(InputInterface $input, OutputInterface $output) if ($configurationDistExists == false) { $output->writeln("configuration.dist.php file nof found The file must exist in install/configuration.dist.php or app/config/parameter.yml"); + return 0; } @@ -117,6 +81,7 @@ public function installLegacy(InputInterface $input, OutputInterface $output) } else { $output->writeln("There's a Chamilo portal here: ".$configurationPath." "); } + return 0; } @@ -129,7 +94,7 @@ public function installLegacy(InputInterface $input, OutputInterface $output) $databaseSettings = $this->getDatabaseSettings(); $connectionToHost = true; - $connectionToHostConnect = true; + $connectionToHostConnect = true; if ($connectionToHostConnect) { if ($this->commandLine && false) { $eventManager = $connectionToHost->getSchemaManager(); @@ -171,6 +136,7 @@ public function installLegacy(InputInterface $input, OutputInterface $output) ); } else { $output->writeln("Configuration file was not saved"); + return 0; } @@ -239,7 +205,7 @@ public function installLegacy(InputInterface $input, OutputInterface $output) 'command' => 'files:set_permissions_after_install', '--conf' => $this->getConfigurationHelper()->getConfigurationFilePath($path), '--linux-user' => $linuxUser, - '--linux-group' => $linuxGroup + '--linux-group' => $linuxGroup, //'--dry-run' => $dryRun ]; @@ -250,13 +216,16 @@ public function installLegacy(InputInterface $input, OutputInterface $output) // Generating config files (auth, profile, etc) //$this->generateConfFiles($output); $output->writeln("Chamilo was successfully installed here: ".$this->getRootSys()." "); + return 1; } else { $output->writeln("Error during installation."); + return 0; } } else { $output->writeln("Can't create database '".$databaseSettings['dbname']."' "); + return 0; } } catch (\Exception $e) { @@ -270,6 +239,7 @@ public function installLegacy(InputInterface $input, OutputInterface $output) ) ); $output->writeln(sprintf('%s', $e->getMessage())); + return 0; } } else { @@ -279,14 +249,13 @@ public function installLegacy(InputInterface $input, OutputInterface $output) $databaseSettings['dbname'] ) ); + return 0; } } /** - * Install Chamilo - * @param InputInterface $input - * @param OutputInterface $output + * Install Chamilo. * * @return bool */ @@ -320,6 +289,7 @@ public function install(InputInterface $input, OutputInterface $output) if (empty($this->databaseSettings)) { $output->writeln("Cannot get database settings. "); + return false; } @@ -341,6 +311,7 @@ public function install(InputInterface $input, OutputInterface $output) $databaseSettings['dbname'] ) ); + return 0; } @@ -382,8 +353,6 @@ public function install(InputInterface $input, OutputInterface $output) /** * Ask for DB settings. - * @param InputInterface $input - * @param OutputInterface $output */ public function askDatabaseSettings(InputInterface $input, OutputInterface $output) { @@ -392,7 +361,7 @@ public function askDatabaseSettings(InputInterface $input, OutputInterface $outp $params = $this->getDatabaseSettingsParams(); $total = count($params); $output->writeln( - "Database settings: (" . $total . ")" + "Database settings: (".$total.")" ); $databaseSettings = []; $counter = 1; @@ -413,19 +382,19 @@ public function askDatabaseSettings(InputInterface $input, OutputInterface $outp case 'host': $databaseSettings[$key] = 'localhost'; $output->writeln( - "($counter/$total) Option: $key was not provided. Using default value " . $databaseSettings[$key] . "" + "($counter/$total) Option: $key was not provided. Using default value ".$databaseSettings[$key]."" ); break; case 'port': $databaseSettings[$key] = '3306'; $output->writeln( - "($counter/$total) Option: $key was not provided. Using default value " . $databaseSettings[$key] . "" + "($counter/$total) Option: $key was not provided. Using default value ".$databaseSettings[$key]."" ); break; case 'driver': $databaseSettings[$key] = 'pdo_mysql'; $output->writeln( - "($counter/$total) Option: $key was not provided. Using default value " . $databaseSettings[$key] . "" + "($counter/$total) Option: $key was not provided. Using default value ".$databaseSettings[$key]."" ); break; } @@ -441,7 +410,7 @@ public function askDatabaseSettings(InputInterface $input, OutputInterface $outp } } else { $output->writeln( - "($counter/$total) Option: $key = '" . $filledParams[$key] . "' was added as an option. " + "($counter/$total) Option: $key = '".$filledParams[$key]."' was added as an option. " ); $counter++; $databaseSettings[$key] = $filledParams[$key]; @@ -452,9 +421,6 @@ public function askDatabaseSettings(InputInterface $input, OutputInterface $outp /** * Asks for admin settings. - * - * @param InputInterface $input - * @param OutputInterface $output */ public function askAdminSettings(InputInterface $input, OutputInterface $output) { @@ -469,20 +435,20 @@ public function askAdminSettings(InputInterface $input, OutputInterface $output) $params = $this->getAdminSettingsParams(); $total = count($params); $output->writeln( - "Admin settings: (" . $total . ")" + "Admin settings: (".$total.")" ); $adminSettings = []; $counter = 1; foreach ($params as $key => $value) { if (!isset($filledParams[$key])) { - $question = new Question("($counter/$total) Please enter the value of the $key (" . $value['attributes']['data'] . "): "); + $question = new Question("($counter/$total) Please enter the value of the $key (".$value['attributes']['data']."): "); $data = $helper->ask($input, $output, $question); $counter++; $adminSettings[$key] = $data; } else { $output->writeln( - "($counter/$total) Option: $key = '" . $filledParams[$key] . "' was added as an option. " + "($counter/$total) Option: $key = '".$filledParams[$key]."' was added as an option. " ); $counter++; $adminSettings[$key] = $filledParams[$key]; @@ -494,9 +460,6 @@ public function askAdminSettings(InputInterface $input, OutputInterface $output) /** * Ask for portal settings. - * - * @param InputInterface $input - * @param OutputInterface $output */ public function askPortalSettings(InputInterface $input, OutputInterface $output) { @@ -515,7 +478,7 @@ public function askPortalSettings(InputInterface $input, OutputInterface $output foreach ($params as $key => $value) { // If not in array ASK! if (!isset($filledParams[$key])) { - $question = new Question("($counter/$total) Please enter the value of the $key (" . $value['attributes']['data'] . "): "); + $question = new Question("($counter/$total) Please enter the value of the $key (".$value['attributes']['data']."): "); $data = $helper->ask($input, $output, $question); $counter++; $portalSettings[$key] = $data; @@ -532,7 +495,6 @@ public function askPortalSettings(InputInterface $input, OutputInterface $output /** * Setting common parameters. - * @param InputInterface $input */ public function settingParameters(InputInterface $input) { @@ -582,85 +544,10 @@ public function settingParameters(InputInterface $input) } /** - * Executes a command via CLI - * - * @param InputInterface $input - * @param OutputInterface $output + * Get database version to install for a requested version. * - * @return int|null|void - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - // Setting configuration helper. - $this->getApplication()->getHelperSet()->set( - new ConfigurationHelper(), - 'configuration' - ); - - $this->settingParameters($input); - $output->writeln('Root sys value: '.$this->rootSys); - - $version = $this->version; - $download = $this->download; - $tempFolder = $this->tempFolder; - $path = $this->path; - - // @todo fix process in order to install minor versions: 1.9.6 - $versionList = $this->getVersionNumberList(); - - if (!in_array($version, $versionList)) { - $output->writeln("Sorry you can't install version: '$version' of Chamilo :("); - $output->writeln("Supported versions: ".implode(', ', $this->getVersionNumberList())); - return 0; - } - - if ($download) { - $chamiloLocationPath = $this->getPackage($output, $version, null, $tempFolder); - if (empty($chamiloLocationPath)) { - return 0; - } - - $result = $this->copyPackageIntoSystem($output, $chamiloLocationPath, $path); - if ($result == 0) { - return 0; - } - - $this->settingParameters($input); - if ($input->getOption('only-download-package')) { - return 0; - } - } - - $title = 'Chamilo installation process.'; - if ($this->commandLine) { - $title = 'Welcome to the Chamilo installation process.'; - } - - $this->writeCommandHeader($output, $title); - - $versionInfo = $this->availableVersions()[$version]; - if (isset($versionInfo['parent'])) { - $parent = $versionInfo['parent']; - if (in_array($parent, ['1.9.0', '1.10.0', '1.11.0'])) { - $isLegacy = true; - } else { - $isLegacy = false; - } - } else { - $output->writeln("Chamilo $version doesnt have a parent"); - return false; - } - - if ($isLegacy) { - $this->installLegacy($input, $output); - } else { - $this->install($input, $output); - } - } - - /** - * Get database version to install for a requested version * @param string $version + * * @return string */ public function getVersionToInstall($version) @@ -705,11 +592,12 @@ public function getVersionToInstall($version) } /** - * Installation command + * Installation command. * - * @param array $databaseSettings + * @param array $databaseSettings * @param string $version * @param $output + * * @return bool */ public function processInstallation($databaseSettings, $version, $output) @@ -753,7 +641,7 @@ public function processInstallation($databaseSettings, $version, $output) // Importing sql files. $arguments = [ 'command' => 'dbal:import', - 'file' => $dbList + 'file' => $dbList, ]; $input = new ArrayInput($arguments); $command->run($input, $output); @@ -776,7 +664,7 @@ public function processInstallation($databaseSettings, $version, $output) $databaseName = $courseInfo['name']; $output->writeln("Inserting course database in Chamilo: $databaseName"); $this->createCourse($this->getHelper('db')->getConnection(), $databaseName); - $sectionsCount ++; + $sectionsCount++; } } @@ -786,7 +674,7 @@ public function processInstallation($databaseSettings, $version, $output) $legacyFiles = [ '/vendor/autoload.php', '/public/main/install/install.lib.php', - '/public/legacy.php' + '/public/legacy.php', ]; foreach ($legacyFiles as $file) { @@ -946,36 +834,14 @@ public function processInstallation($databaseSettings, $version, $output) } /** - * @param $file - * @param $output - * @throws \Exception - */ - private function importSQLFile($file, $output) - { - $command = $this->getApplication()->find('dbal:import'); - - // Importing sql files. - $arguments = [ - 'command' => 'dbal:import', - 'file' => $file - ]; - $input = new ArrayInput($arguments); - $command->run($input, $output); - - // Getting extra information about the installation. - $output->writeln("File loaded $file"); - } - - /** - * * In step 3. Tests establishing connection to the database server. * If it's a single database environment the function checks if the database exist. * If the database doesn't exist we check the creation permissions. * - * @return int 1 when there is no problem; - * 0 when a new database is impossible to be created, - * then the single/multiple database configuration is impossible too - * -1 when there is no connection established. + * @return int 1 when there is no problem; + * 0 when a new database is impossible to be created, + * then the single/multiple database configuration is impossible too + * -1 when there is no connection established. */ public function testDatabaseConnection() { @@ -1018,7 +884,7 @@ public function getUserAccessConnectionToDatabase() } /** - * Creates a course (only an insert in the DB) + * Creates a course (only an insert in the DB). * * @param \Doctrine\DBAL\Connection * @param string $databaseName @@ -1030,8 +896,141 @@ public function createCourse($connection, $databaseName) 'db_name' => $databaseName, 'course_language' => 'english', 'title' => $databaseName, - 'visual_code' => $databaseName + 'visual_code' => $databaseName, ]; $connection->insert('course', $params); } + + /** + * Configure command. + */ + protected function configure(): void + { + $this + ->setName('chash:chamilo_install') + ->setDescription('Execute a Chamilo installation to a specified version.') + ->addArgument('version', InputArgument::REQUIRED, 'The version to migrate to.', null) + ->addArgument('path', InputArgument::OPTIONAL, 'The path to the chamilo folder') + ->addOption('download-package', null, InputOption::VALUE_NONE, 'Downloads the chamilo package') + ->addOption('only-download-package', null, InputOption::VALUE_NONE, 'Only downloads the package') + ->addOption('temp-folder', null, InputOption::VALUE_OPTIONAL, 'The temp folder.', '/tmp') + ->addOption('linux-user', null, InputOption::VALUE_OPTIONAL, 'user', 'www-data') + ->addOption('linux-group', null, InputOption::VALUE_OPTIONAL, 'group', 'www-data') + ->addOption('silent', null, InputOption::VALUE_NONE, 'Execute the migration with out asking questions.') + ; + + $params = $this->getPortalSettingsParams(); + + foreach ($params as $key => $value) { + $this->addOption($key, null, InputOption::VALUE_OPTIONAL); + } + + $params = $this->getAdminSettingsParams(); + foreach ($params as $key => $value) { + $this->addOption($key, null, InputOption::VALUE_OPTIONAL); + } + + $params = $this->getDatabaseSettingsParams(); + foreach ($params as $key => $value) { + $this->addOption($key, null, InputOption::VALUE_OPTIONAL); + } + } + + /** + * Executes a command via CLI. + * + * @return int|void|null + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + // Setting configuration helper. + $this->getApplication()->getHelperSet()->set( + new ConfigurationHelper(), + 'configuration' + ); + + $this->settingParameters($input); + $output->writeln('Root sys value: '.$this->rootSys); + + $version = $this->version; + $download = $this->download; + $tempFolder = $this->tempFolder; + $path = $this->path; + + // @todo fix process in order to install minor versions: 1.9.6 + $versionList = $this->getVersionNumberList(); + + if (!in_array($version, $versionList)) { + $output->writeln("Sorry you can't install version: '$version' of Chamilo :("); + $output->writeln("Supported versions: ".implode(', ', $this->getVersionNumberList())); + + return 0; + } + + if ($download) { + $chamiloLocationPath = $this->getPackage($output, $version, null, $tempFolder); + if (empty($chamiloLocationPath)) { + return 0; + } + + $result = $this->copyPackageIntoSystem($output, $chamiloLocationPath, $path); + if ($result == 0) { + return 0; + } + + $this->settingParameters($input); + if ($input->getOption('only-download-package')) { + return 0; + } + } + + $title = 'Chamilo installation process.'; + if ($this->commandLine) { + $title = 'Welcome to the Chamilo installation process.'; + } + + $this->writeCommandHeader($output, $title); + + $versionInfo = $this->availableVersions()[$version]; + if (isset($versionInfo['parent'])) { + $parent = $versionInfo['parent']; + if (in_array($parent, ['1.9.0', '1.10.0', '1.11.0'])) { + $isLegacy = true; + } else { + $isLegacy = false; + } + } else { + $output->writeln("Chamilo $version doesnt have a parent"); + + return false; + } + + if ($isLegacy) { + $this->installLegacy($input, $output); + } else { + $this->install($input, $output); + } + } + + /** + * @param $file + * @param $output + * + * @throws \Exception + */ + private function importSQLFile($file, $output) + { + $command = $this->getApplication()->find('dbal:import'); + + // Importing sql files. + $arguments = [ + 'command' => 'dbal:import', + 'file' => $file, + ]; + $input = new ArrayInput($arguments); + $command->run($input, $output); + + // Getting extra information about the installation. + $output->writeln("File loaded $file"); + } } diff --git a/src/Command/Installation/StatusCommand.php b/src/Command/Installation/StatusCommand.php index 5b1c7fb..1e20765 100644 --- a/src/Command/Installation/StatusCommand.php +++ b/src/Command/Installation/StatusCommand.php @@ -3,12 +3,12 @@ namespace Chash\Command\Installation; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** - * Class StatusCommand + * Class StatusCommand. */ class StatusCommand extends DatabaseCommand { @@ -28,12 +28,9 @@ protected function configure(): void } /** - * Executes a command via CLI - * - * @param InputInterface $input - * @param OutputInterface $output + * Executes a command via CLI. * - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Installation/UpgradeCommand.php b/src/Command/Installation/UpgradeCommand.php index 500739c..9f9a943 100644 --- a/src/Command/Installation/UpgradeCommand.php +++ b/src/Command/Installation/UpgradeCommand.php @@ -6,16 +6,16 @@ use Doctrine\Migrations\Tools\Console\Command\MigrateCommand; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Question\ConfirmationQuestion; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Yaml\Dumper; use Symfony\Component\Yaml\Parser; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Console\Question\ConfirmationQuestion; /** - * Class UpgradeCommand + * Class UpgradeCommand. */ class UpgradeCommand extends CommonCommand { @@ -24,1035 +24,1048 @@ class UpgradeCommand extends CommonCommand public $commandLine = true; /** - * Get connection - * @return \Doctrine\DBAL\Connection - */ - private function getConnection() - { - return $this->getHelper('db')->getConnection(); - } - - protected function configure(): void - { - $this - ->setName('chash: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('path', null, InputOption::VALUE_OPTIONAL, 'The path to the chamilo folder') - ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the migration as a dry run') - ->addOption('update-installation', null, InputOption::VALUE_OPTIONAL, 'Updates the portal with the current zip file. http:// or /var/www/file.zip') - ->addOption('temp-folder', null, InputOption::VALUE_OPTIONAL, 'The temp folder', '/tmp') - ->addOption('download-package', null, InputOption::VALUE_OPTIONAL, 'Download the chamilo package', 'true') - ->addOption('linux-user', null, InputOption::VALUE_OPTIONAL, 'user', 'www-data') - ->addOption('linux-group', null, InputOption::VALUE_OPTIONAL, 'group', 'www-data') - ->addOption('custom-package', null, InputOption::VALUE_OPTIONAL, 'Custom zip package location.', '') - ->addOption('remove-unused-table', null, InputOption::VALUE_NONE, 'Remove unused tables.') - ->addOption('only-update-db', null, InputOption::VALUE_NONE, 'Only updates the db.') - ; - //->addOption('force', null, InputOption::VALUE_NONE, 'Force the update. Only for tests'); - } - - /** - * Executes a command via CLI + * Starts a migration. + * + * @param array $courseList + * @param string $path + * @param string $toVersion + * @param bool $dryRun + * @param bool $removeUnusedTables + * @param bool $runFixIds + * @param bool $onlyUpdateDatabase * - * @param InputInterface $input - * @param OutputInterface $output + * @throws \Exception * - * @return int|null|void + * @return bool */ - protected function execute(InputInterface $input, OutputInterface $output) - { - // sudo php /var/www/html/chash/chash.php chash:chamilo_upgrade 1.11.x --linux-user=jmontoya --linux-group=jmontoya + public function startMigration( + $courseList, + $path, + $toVersion, + $dryRun, + OutputInterface $output, + $removeUnusedTables = false, + InputInterface $mainInput, + $runFixIds = true, + $onlyUpdateDatabase = false + ) { + // Cleaning query list. + $this->queryList = []; - $startTime = time(); + // Main DB connection. + $conn = $this->getConnection($mainInput); + $_configuration = $this->getHelper('configuration')->getConfiguration($path); + $versionInfo = $this->getAvailableVersionInfo($toVersion); + $installPath = $this->getInstallationFolder().$toVersion.'/'; - // Arguments and options - $version = $originalVersion = $input->getArgument('version'); - $path = $input->getOption('path'); - $dryRun = $input->getOption('dry-run'); - $silent = !$input->isInteractive(); - $tempFolder = $input->getOption('temp-folder'); - $downloadPackage = $input->getOption('download-package') == 'true' ? true : false; - $customPackage = $input->getOption('custom-package'); - $removeUnusedTables = $input->getOption('remove-unused-table'); - $linuxUser = $input->getOption('linux-user'); - $linuxGroup = $input->getOption('linux-group'); - $updateInstallation = $input->getOption('update-installation'); - $onlyUpdateDatabase = $input->getOption('only-update-db') == 'true' ? true : false; + // Filling sqlList array with "pre" db changes. + if (isset($versionInfo['pre']) && !empty($versionInfo['pre'])) { + $sqlToInstall = $installPath.$versionInfo['pre']; + $this->fillQueryList($sqlToInstall, $output, 'pre'); - if (!empty($customPackage)) { - $downloadPackage = false; + // Processing sql query list depending of the section (course, main, user). + $this->processQueryList( + $courseList, + $output, + $path, + $toVersion, + $dryRun, + 'pre' + ); } - if ($onlyUpdateDatabase) { - $downloadPackage = false; - } + try { + if (isset($versionInfo['hook_to_doctrine_version'])) { + // Doctrine migrations: + $em = $this->setDoctrineSettings($this->getHelperSet()); + $output->writeln(''); + $output->writeln( + "You have to select 'yes' for the 'Chamilo Migrations'" + ); - // Getting supported version number list - $versionNameList = $this->getVersionNumberList(); - $minVersion = $this->getMinVersionSupportedByInstall(); - $versionList = $this->availableVersions(); + // Setting migrations temporal ymls + $tempFolder = '/tmp'; + require_once $_configuration['root_sys'].'app/Migrations/AbstractMigrationChamilo.php'; + $migrationsFolder = $tempFolder.'/Migrations/'; - // Checking version. - if ($version != 'master') { - if (!in_array($version, $versionNameList)) { - $output->writeln("Version '$version' is not available."); - $output->writeln("Available versions: ".implode(', ', $versionNameList).""); - return 0; - } - } + $fs = new Filesystem(); + if (!$fs->exists($migrationsFolder)) { + $fs->mkdir($migrationsFolder); + } - // Setting the configuration path and configuration array - $_configuration = $this->getConfigurationHelper()->getConfiguration($path); + $migrations = [ + 'name' => 'Chamilo Migrations', + 'migrations_namespace' => $versionInfo['migrations_namespace'], + 'table_name' => 'version', + 'migrations_directory' => $versionInfo['migrations_directory'], + ]; - if (empty($_configuration)) { - $output->writeln("Chamilo is not installed here! You may add a path as an option:"); - $output->writeln("For example: chamilo:upgrade 1.11.x --path=/var/www/chamilo"); + $dumper = new Dumper(); + $yaml = $dumper->dump($migrations, 1); + $file = $migrationsFolder.$versionInfo['migrations_yml']; - return 0; - } + if (file_exists($file)) { + unlink($file); + } - $this->setConfigurationArray($_configuration); - $this->getConfigurationHelper()->setConfiguration($_configuration); - $this->setRootSysDependingConfigurationPath($path); + file_put_contents($file, $yaml); + $command = new MigrateCommand(); + // Creates the helper set + $helperSet = \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($em); + $helper = $this->getHelperSet()->get('question'); + $helperSet->set($helper, 'question'); + $command->setHelperSet($helperSet); - $configurationPath = $this->getHelper('configuration')->getConfigurationPath($path); + $arguments = [ + //'command' => 'migrations:migrate', + '--configuration' => $file, + '--dry-run' => $dryRun, + 'version' => $versionInfo['hook_to_doctrine_version'], + ]; - // Checking configuration file. - if (!is_writable($configurationPath)) { - $output->writeln("Folder ".$configurationPath." must have writeable permissions"); + $output->writeln( + "Executing migrations:migrate ".$versionInfo['hook_to_doctrine_version']." --configuration=".$file."" + ); + $input = new ArrayInput($arguments); + $command->run($input, $output); + $output->writeln( + "Migration ended successfully" + ); + } - return 0; - } + // Processing "db" changes. + if (isset($versionInfo['update_db']) && !empty($versionInfo['update_db'])) { + $sqlToInstall = $installPath.$versionInfo['update_db']; + if (is_file($sqlToInstall) && file_exists($sqlToInstall)) { + if ($dryRun) { + $output->writeln("File to be executed but not fired because of dry-run option: '$sqlToInstall'"); + } else { + $output->writeln("Executing update db: '$sqlToInstall'"); + } + require $sqlToInstall; - $this->setConfigurationPath($configurationPath); + if (!empty($update)) { + $update($_configuration, $conn, $courseList, $dryRun, $output, $this, $removeUnusedTables); + } + } else { + $output->writeln(sprintf("File doesn't exist: '%s'", $sqlToInstall)); + } + } - // $_configuration['password_encryption'] must exists - if (isset($_configuration['password_encryption'])) { - $output->writeln("\$_configuration[password_encryption] value found: ".$_configuration['password_encryption'].""); - } else { - $output->writeln("\$_configuration['password_encryption'] not found. The key 'password_encryption' or the variable '\$userPasswordCrypted' must exist in the configuration.php file "); - return 0; - } + // Processing "update file" changes. + if (isset($versionInfo['update_files']) && !empty($versionInfo['update_files']) && $onlyUpdateDatabase == false) { + $sqlToInstall = $installPath.$versionInfo['update_files']; + if (is_file($sqlToInstall) && file_exists($sqlToInstall)) { + if ($dryRun) { + $output->writeln("Files to be executed but dry-run is on: '$sqlToInstall'"); + } else { + $output->writeln("Executing update files: '$sqlToInstall'"); - if ($dryRun == false) { - if ($downloadPackage) { - $output->writeln("Downloading package ..."); - $chamiloLocationPath = $this->getPackage($output, $originalVersion, $updateInstallation, $tempFolder); - if (empty($chamiloLocationPath)) { - $output->writeln("Chash was not able to unzip the downloaded package for version: $originalVersion"); - return 0; - } + require $sqlToInstall; - $this->copyPackageIntoSystem($output, $chamiloLocationPath, null); - } else { - if (!empty($customPackage)) { - $chamiloLocationPath = $customPackage; + if (!empty($updateFiles)) { + $updateFiles($_configuration, $conn, $courseList, $dryRun, $output, $this); + } + } + } else { + $output->writeln(sprintf("File doesn't exist: '%s'", $sqlToInstall)); } } - } - // Checking Resources/Database dir. Getting the Resources/Database/1.8.7/db_main.sql. - $testFolder = $this->getInstallationFolder().'1.8.7/db_main.sql'; - $installationFolder = $this->getInstallationFolder(); - $_configuration = $this->getConfigurationHelper()->getConfiguration($path); + // Filling sqlList array with "post" db changes. + if (isset($versionInfo['post']) && !empty($versionInfo['post'])) { + $sqlToInstall = $installPath.$versionInfo['post']; + $this->fillQueryList($sqlToInstall, $output, 'post'); + // Processing sql query list depending of the section. + $this->processQueryList($courseList, $output, $path, $toVersion, $dryRun, 'post'); + } - if (PHP_SAPI != 'cli') { - $this->commandLine = false; - } + $filesToLoad = [ + $this->getRootSys().'/main/inc/lib/database.constants.inc.php', + $this->getRootSys().'/main/inc/lib/system/session.class.php', + $this->getRootSys().'/main/inc/lib/chamilo_session.class.php', + $this->getRootSys().'/main/inc/lib/api.lib.php', + $this->getRootSys().'/main/inc/lib/database.lib.php', + $this->getRootSys().'/main/inc/lib/custom_pages.class.php', + $this->getRootSys().'/main/install/install.lib.php', + $this->getRootSys().'/main/inc/lib/display.lib.php', + $this->getRootSys().'/main/inc/lib/group_portal_manager.lib.php', + $this->getRootSys().'/main/inc/lib/model.lib.php', + $this->getRootSys().'/main/inc/lib/events.lib.php', + $this->getRootSys().'/main/inc/lib/extra_field.lib.php', + $this->getRootSys().'/main/inc/lib/extra_field_value.lib.php', + $this->getRootSys().'/main/inc/lib/urlmanager.lib.php', + $this->getRootSys().'/main/inc/lib/usermanager.lib.php', + $this->getRootSys().'/src/Chamilo/CoreBundle/Entity/ExtraField.php', + $this->getRootSys().'/src/Chamilo/CoreBundle/Entity/ExtraFieldOptions.php', + ]; - $doctrineVersion = null; - $currentVersion = null; - if (!file_exists($testFolder)) { - $output->writeln("The migration directory was not detected: $installationFolder"); - return 0; - } else { - $output->writeln("Reading migrations from directory: $installationFolder"); - } - - // In order to use Doctrine migrations + if ($runFixIds) { + foreach ($filesToLoad as $file) { + require_once $file; + } - // Setting configuration variable in order to get the doctrine version: - //$input->setOption('configuration', $this->getMigrationConfigurationFile()); - //$configuration = $this->getMigrationConfiguration($input, $output); + $output->writeln("Run fixIds function "); + fixIds($em); + } - // Doctrine migrations version - //$doctrineVersion = $configuration->getCurrentVersion(); - // Moves files from main/inc/conf to config/ + if (method_exists('fixPostGroupIds') && + $versionInfo['migrations_yml'] == 'V111.yml' + ) { + foreach ($filesToLoad as $file) { + require_once $file; + } + $output->writeln("Run fixPostGroupIds function "); + fixPostGroupIds($conn); + } else { + $output->writeln("Not found function: fixPostGroupIds"); + } + } catch (\Exception $e) { + $output->write(sprintf('Migration failed. Error %s', $e->getMessage())); - // Checking system_version. - if (!isset($_configuration['system_version']) || - empty($_configuration['system_version']) - ) { - $output->writeln("There is something wrong in your Chamilo installation. Check it with the chash:chamilo_status command"); - return 0; + throw $e; } - if (version_compare($_configuration['system_version'], $minVersion, '<')) { - $output->writeln("Your Chamilo version is not supported! The minimun version is: $minVersion"); - $output->writeln("You want to upgrade from ".$_configuration['system_version']." to $minVersion"); - return 0; - } + return false; + } - if ($version != 'master') { - if (version_compare($version, $_configuration['system_version'], '>')) { - $currentVersion = $_configuration['system_version']; - } else { - $output->writeln("Please provide a version greater than ".$_configuration['system_version'].""); - $output->writeln("You selected version: $version"); - $output->writeln("You can also check your installation status with chash:chamilo_status"); - return 0; - } - } else { - $currentVersion = $_configuration['system_version']; - $version = $this->getLatestVersion(); - } + /** + * @return array + */ + public function getMigrationTypes() + { + return [ + 'pre', + 'post', + ]; + } - $versionInfo = $this->getAvailableVersionInfo($version); + /** + * Process the queryList array and executes queries to the correct section (main, user, course, etc). + * + * @param array $courseList + * @param OutputInterface $output + * @param $path + * @param $version + * @param $dryRun + * @param $type + * + * @throws \Exception + * + * @return bool + */ + public function processQueryList($courseList, $output, $path, $version, $dryRun, $type) + { + $databases = $this->getDatabaseList($output, $courseList, $path, $version, $type); + $this->setConnections($version, $path, $databases); - if (empty($versionInfo)) { - $output->writeln("The current version ($version) is not supported"); + foreach ($databases as $section => &$dbList) { + foreach ($dbList as &$dbInfo) { + $output->writeln(""); + $output->writeln("Loading section: $section using database key ".$dbInfo['database'].""); + $output->writeln("--------------------------"); - return 0; - } + if ($dbInfo['status'] == 'complete') { + $output->writeln("Database already updated."); + continue; + } - if (isset($versionInfo['hook_to_doctrine_version']) && - isset($doctrineVersion) - ) { - if ($doctrineVersion == $versionInfo['hook_to_doctrine_version']) { - $output->writeln("You already have the latest version. Nothing to update! Doctrine version $doctrineVersion"); - return 0; - } - } + if (isset($this->queryList[$type]) && + isset($this->queryList[$type][$section]) && + !empty($this->queryList[$type][$section]) + ) { + $queryList = $this->queryList[$type][$section]; + $output->writeln("Loading queries list: '$type' - '$section'"); - if (isset($versionInfo['parent']) && !empty($versionInfo['parent'])) { - $versionInfoParent = $this->getAvailableVersionInfo($versionInfo['parent']); - if ($doctrineVersion == $versionInfoParent['hook_to_doctrine_version']) { - $output->writeln("You already have the latest version. Nothing to update! Doctrine version $doctrineVersion"); - return 0; - } - } + if (!empty($queryList)) { + try { + $lines = 0; - $this->writeCommandHeader($output, 'Welcome to the Chamilo upgrade process!'); + /** @var \Doctrine\DBAL\Connection $conn */ + $conn = $this->getHelper($dbInfo['database'])->getConnection(); + $output->writeln("Executing queries in DB: ".$conn->getDatabase().""); - if ($dryRun == false) { - if ($downloadPackage) { - $output->writeln("When the installation process finishes, the files located here:"); - $output->writeln("$chamiloLocationPath"); - $output->writeln("will be copied to your portal here: ".$this->getRootSys().""); - } + $conn->beginTransaction(); - if ($removeUnusedTables) { - if (version_compare($currentVersion, '1.9.0', '<')) { - $onlyPrefix = $this->getTablePrefix($_configuration); - if (!empty($onlyPrefix)) { - $output->writeln("--remove-unused-table option is on. Unused tables will be removed."); - $tablesToDelete = "SHOW TABLES LIKE '".$onlyPrefix."%';"; - $output->writeln("All tables that match this query will be deleted: $tablesToDelete"); + foreach ($queryList as $query) { + // Add a prefix. + + if ($section == 'course') { + $query = str_replace('{prefix}', $dbInfo['prefix'], $query); + } + + if ($dryRun) { + $output->writeln($query); + } else { + $output->writeln(' -> '.$query); + $conn->executeQuery($query); + //$conn->exec($query); + } + $lines++; + } + + if (!$dryRun) { + if ($conn->isTransactionActive()) { + $conn->commit(); + } + $output->writeln(sprintf('%d statements executed!', $lines).PHP_EOL); + $dbInfo['status'] = 'complete'; + $this->saveDatabaseList($path, $databases, $version, $type); + } + } catch (\Exception $e) { + $conn->rollback(); + $output->write(sprintf('Migration failed. Error %s', $e->getMessage())); + throw $e; + } } else { - $output->writeln("--remove-unused-table option is on. But any table will be removed."); + $output->writeln(sprintf("queryList array is empty.")); } } else { - $output->writeln("--remove-unused-table option is on. This does not affect this $originalVersion version."); + $output->writeln(sprintf("Nothing to execute for section $section!")); + + return false; } } - } else { - $output->writeln("When the installation process finishes, PHP files are not going to be updated (--dry-run is on)."); } + $this->queryList = []; - $helper = $this->getHelperSet()->get('question'); - - + return true; + } - if ($silent == false) { - $question = new ConfirmationQuestion( - 'Are you sure you want to upgrade the Chamilo located here? '.$_configuration['root_sys'].' (y/N)', - false - ); - if (!$helper->ask($input, $output, $question)) { - return; - } + /** + * Reads a sql file and adds queries in the queryList array. + * + * @param string $sqlFilePath + * @param OutputInterface $output + * @param string type + */ + public function fillQueryList($sqlFilePath, $output, $type) + { + $output->writeln(sprintf("Processing file type: $type '%s'... ", $sqlFilePath)); + $sections = $this->getSections(); - $question = new ConfirmationQuestion( - 'Are you sure you want to upgrade from version '.$_configuration['system_version'].' to version '.$version.' (y/N)', - false - ); - if (!$helper->ask($input, $output, $question)) { - return; - } + foreach ($sections as $section) { + $sqlList = $this->getSQLContents($sqlFilePath, $section, $output); + $this->setQueryList($sqlList, $section, $type); } + } - $output->writeln('Migrating from Chamilo version: '.$_configuration['system_version'].' to version '.$version); - $output->writeln('Starting upgrade for Chamilo, reading configuration file located here: '.$configurationPath.'configuration.php'); - - // Getting configuration file. - $_configuration = $this->getHelper('configuration')->getConfiguration($path); + /** + * Setting the queryList array. + * + * @param array $queryList + * @param string $section + * @param string $type + */ + public function setQueryList($queryList, $section, $type) + { + if (!isset($this->queryList[$type][$section])) { + $this->queryList[$type][$section] = $queryList; + } else { + $this->queryList[$type][$section] = array_merge($this->queryList[$type][$section], $queryList); + } + } - // Upgrade always from a mysql driver - $databaseSettings = [ - 'driver' => 'pdo_mysql', - 'host' => $_configuration['db_host'], - 'dbname' => $_configuration['main_database'], - 'user' => $_configuration['db_user'], - 'password' => $_configuration['db_password'], + /** + * Returns sections. + * + * @return array + */ + public function getSections() + { + return [ + 'main', + 'user', + 'stats', + 'scorm', + 'course', ]; + } - // Setting DB access. - $this->setDatabaseSettings($databaseSettings); - - $extraDatabaseSettings = [ - 'single_database'=> isset($_configuration['single_database']) ? $_configuration['single_database'] : false, - 'table_prefix'=> isset($_configuration['table_prefix']) ? $_configuration['table_prefix'] : null, - 'db_glue' => isset($_configuration['db_glue']) ? $_configuration['db_glue'] : null, - 'db_prefix' => isset($_configuration['db_prefix']) ? $_configuration['db_prefix'] : null, + /** + * Generates database array info. + * + * @param array $courseList + * + * @return array + */ + public function generateDatabaseList($courseList) + { + $courseDbList = []; + $_configuration = $this->getConfigurationArray(); + if (!empty($courseList)) { + foreach ($courseList as $course) { + if (!empty($course['db_name'])) { + $courseDbList[] = [ + 'database' => '_chamilo_course_'.$course['db_name'], + 'prefix' => $this->getTablePrefix($_configuration, $course['db_name']), + 'status' => 'waiting', + ]; + } + } + } else { + $courseDbList = [ + [ + 'database' => 'main_database', + 'status' => 'waiting', + 'prefix' => null, + ], + ]; + } + + $databaseSection = [ + 'main' => [ + [ + 'database' => 'main_database', + 'status' => 'waiting', + ], + ], + 'user' => [ + [ + 'database' => 'user_personal_database', + 'status' => 'waiting', + ], + ], + 'stats' => [ + [ + 'database' => 'statistics_database', + 'status' => 'waiting', + ], + ], + 'course' => $courseDbList, ]; - $this->setExtraDatabaseSettings($extraDatabaseSettings); - $this->setDoctrineSettings($this->getHelperSet()); - $conn = $this->getConnection($input); - $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); + $this->setDatabaseList($databaseSection); - if ($conn) { - $output->writeln("Connection to the database established."); - } else { - $output->writeln("Can't connect to the DB with user:".$_configuration['db_user']).""; + return $this->databaseList; + } - return 0; - } + /** + * Sets the database list. + * + * @param array $list + */ + public function setDatabaseList($list) + { + $this->databaseList = $list; + } - // Get course list - $query = "SELECT * FROM course"; - $result = $conn->executeQuery($query); - $courseList = $result->fetchAll(); + /** + * @param OutputInterface $output + * @param array $courseList + * @param string $path + * @param string $version + * @param string $type + * + * @return mixed|void + */ + public function getDatabaseList($output, $courseList, $path, $version, $type) + { + return $this->generateDatabaseList($courseList); - $output->writeln("Current version: $currentVersion"); - $output->writeln("Latest version: $version"); - $oldVersion = $version; + $configurationPath = $this->getHelper('configuration')->getConfigurationPath($path); + $newConfigurationFile = $configurationPath.'db_migration_status_'.$version.'_'.$type.'.yml'; + if (file_exists($newConfigurationFile)) { + $yaml = new Parser(); + $output->writeln("Loading databases list status from file: $newConfigurationFile"); - // Handle 1.10.x as 1.10.1000 - if ($currentVersion == '1.10.x') { - $currentVersion = '1.10.1000'; + return $yaml->parse(file_get_contents($newConfigurationFile)); + } else { + return $this->generateDatabaseList($courseList); } + } - if ($currentVersion == '1.11.x') { - $currentVersion = '1.11.1000'; + /** + * @param string $path + * @param string $databaseSection + * @param string $version + * @param string $type + * + * @return bool + */ + public function saveDatabaseList($path, $databaseSection, $version, $type) + { + $configurationPath = $this->getHelper('configuration')->getConfigurationPath($path); + $dumper = new Dumper(); + $yaml = $dumper->dump($databaseSection, 2); //inline + $newConfigurationFile = $configurationPath.'db_migration_status_'.$version.'_'.$type.'.yml'; + file_put_contents($newConfigurationFile, $yaml); + + return file_exists($newConfigurationFile); + } + + /** + * @param OutputInterface $output + * @param array $courseList + * @param string $path + * @param string $section + * @param string $version + * @param string $type + * + * @return mixed + */ + public function getDatabasesPerSection($output, $courseList, $path, $section, $version, $type) + { + $databases = $this->getDatabaseList($output, $courseList, $path, $version, $type); + if (isset($databases[$section])) { + return $databases[$section]; } + } - if ($version == '1.10.x') { - $version = '1.10.1000'; + /** + * Function originally wrote in install.lib.php. + * + * @param string $file + * @param string $section + * @param OutputInterface $output + * + * @return array|bool + */ + public function getSQLContents($file, $section, $output) + { + if (empty($file) || file_exists($file) == false) { + $output->writeln(sprintf("File doesn't exist: '%s'... ", $file)); + + return false; } - if ($version == '1.11.x') { - $version = '1.11.1000'; + if (!in_array($section, ['main', 'user', 'stats', 'scorm', 'course'])) { + $output->writeln(sprintf("Section is %s not authorized in getSQLContents()", $section)); + + return false; } - if (version_compare($version, '1.10.0', '>=')) { - require_once $_configuration['root_sys'].'src/Chamilo/CoreBundle/Entity/SettingsCurrent.php'; - require_once $_configuration['root_sys'].'src/Chamilo/CoreBundle/Entity/SystemTemplate.php'; - require_once $_configuration['root_sys'].'src/Chamilo/CoreBundle/Entity/SettingsOptions.php'; - require_once $_configuration['root_sys'].'app/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php'; - require_once $_configuration['root_sys'].'main/inc/lib/api.lib.php'; - require_once $_configuration['root_sys'].'main/inc/lib/custom_pages.class.php'; - require_once $_configuration['root_sys'].'main/inc/lib/database.lib.php'; + // Empty lines should not be executed as SQL statements, because errors occur, see Task #2167. + $fileContents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + if (!is_array($fileContents) or count($fileContents) < 1) { + $output->writeln(sprintf("File '%s' looks empty in getSQLContents()", $file)); - if (!is_dir($_configuration['root_sys'].'vendor')) { - $output->writeln("Execute composer update in your chamilo instance first. Then continue with the upgrade"); + return false; + } - return 1; + // Prepare the resulting array + $sectionContents = []; + $record = false; + foreach ($fileContents as $line) { + if (substr($line, 0, 2) == '--') { + //This is a comment. Check if section name, otherwise ignore + $result = []; + if (preg_match('/^-- xx([A-Z]*)xx/', $line, $result)) { //we got a section name here + if ($result[1] == strtoupper($section)) { + //we have the section we are looking for, start recording + $record = true; + } else { + //we have another section's header. If we were recording, stop now and exit loop + if ($record) { + break; + } + $record = false; + } + } } else { - $question = new ConfirmationQuestion( - 'Are you sure you run composer before executing this upgrade?(y/N)', - false - ); - if (!$helper->ask($input, $output, $question)) { - return; + if ($record) { + if (!empty($line)) { + $sectionContents[] = $line; + } } } } - $versionsToRun = []; - foreach ($versionList as $versionItem => $versionInfo) { - if (version_compare($versionItem, $currentVersion, '>') && - version_compare($versionItem, $version, '<=') - ) { - $versionsToRun[$versionItem] = $versionInfo; - } - } - - $lastItem = count($versionsToRun); - $counter = 0; - $runFixIds = false; - foreach ($versionsToRun as $versionItem => $versionInfo) { - if ($lastItem == $counter) { - $runFixIds = true; - } - - if (isset($versionInfo['require_update']) && - $versionInfo['require_update'] == true - ) { - $output->writeln("----------------------------------------------------------------"); - $output->writeln("Starting migration from version: $currentVersion to version $versionItem "); - $output->writeln(""); + return $sectionContents; + } - // Greater than my current version. - $this->startMigration( - $courseList, - $path, - $versionItem, - $dryRun, - $output, - $removeUnusedTables, - $input, - $runFixIds, - $onlyUpdateDatabase - ); + /** + * Creates the course tables with the prefix c_. + * + * @param OutputInterface $output + * @param string $dryRun + * + * @return int + */ + public function createCourseTables($output, $dryRun) + { + if ($dryRun) { + $output->writeln("Creating c_* tables but dry-run is on. 0 table created."); - $currentVersion = $versionItem; - $output->writeln("End database migration"); - $output->writeln("----------------------------------------------------------------"); - } else { - $currentVersion = $versionItem; - $output->writeln("Skip migration from version: $currentVersion to version $versionItem "); - } - $counter++; + return 0; } - // Restore old version - $version = $oldVersion; + $output->writeln('Creating course tables (c_*)'); - // Update chamilo files. - if ($dryRun == false && $onlyUpdateDatabase == false) { - $output->writeln("Version: $version"); - if ( - $version === '10' || - $version === '1.10.x' || - $version === '11' || - $version === '1.11.x' - ) { - $this->removeUnUsedFiles($output, $path); - $this->copyConfigFilesToNewLocation($output); - } - } + $command = $this->getApplication()->find('dbal:import'); + $sqlFolder = $this->getInstallationPath('1.9.0'); - if ($onlyUpdateDatabase == false) { - $configurationPathFromHelper = $this->getConfigurationHelper()->getConfigurationFilePath($path); - $output->writeln("Reading path in : $path"); - $output->writeln("Configuration path in : $configurationPathFromHelper"); + // Importing sql files. + $arguments = [ + 'command' => 'dbal:import', + 'file' => $sqlFolder.'db_course.sql', + ]; + $input = new ArrayInput($arguments); + $command->run($input, $output); + } - if (empty($configurationPathFromHelper)) { - $output->writeln("Configuration path is not found. Check that the configuration.php exists here: $path."); - exit; - } + protected function configure(): void + { + $this + ->setName('chash: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('path', null, InputOption::VALUE_OPTIONAL, 'The path to the chamilo folder') + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Execute the migration as a dry run') + ->addOption('update-installation', null, InputOption::VALUE_OPTIONAL, 'Updates the portal with the current zip file. http:// or /var/www/file.zip') + ->addOption('temp-folder', null, InputOption::VALUE_OPTIONAL, 'The temp folder', '/tmp') + ->addOption('download-package', null, InputOption::VALUE_OPTIONAL, 'Download the chamilo package', 'true') + ->addOption('linux-user', null, InputOption::VALUE_OPTIONAL, 'user', 'www-data') + ->addOption('linux-group', null, InputOption::VALUE_OPTIONAL, 'group', 'www-data') + ->addOption('custom-package', null, InputOption::VALUE_OPTIONAL, 'Custom zip package location.', '') + ->addOption('remove-unused-table', null, InputOption::VALUE_NONE, 'Remove unused tables.') + ->addOption('only-update-db', null, InputOption::VALUE_NONE, 'Only updates the db.') + ; + //->addOption('force', null, InputOption::VALUE_NONE, 'Force the update. Only for tests'); + } - // Generating temp folders. - $command = $this->getApplication()->find( - 'files:generate_temp_folders' - ); - $arguments = [ - 'command' => 'files:generate_temp_folders', - '--conf' => $configurationPathFromHelper, - '--dry-run' => $dryRun - ]; + /** + * Executes a command via CLI. + * + * @return int|void|null + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + // sudo php /var/www/html/chash/chash.php chash:chamilo_upgrade 1.11.x --linux-user=jmontoya --linux-group=jmontoya - $input = new ArrayInput($arguments); - $command->run($input, $output); + $startTime = time(); - // Update configuration file new system_version - $newParams = ['system_version' => $version]; - $this->updateConfiguration($output, $dryRun, $newParams); + // Arguments and options + $version = $originalVersion = $input->getArgument('version'); + $path = $input->getOption('path'); + $dryRun = $input->getOption('dry-run'); + $silent = !$input->isInteractive(); + $tempFolder = $input->getOption('temp-folder'); + $downloadPackage = $input->getOption('download-package') == 'true' ? true : false; + $customPackage = $input->getOption('custom-package'); + $removeUnusedTables = $input->getOption('remove-unused-table'); + $linuxUser = $input->getOption('linux-user'); + $linuxGroup = $input->getOption('linux-group'); + $updateInstallation = $input->getOption('update-installation'); + $onlyUpdateDatabase = $input->getOption('only-update-db') == 'true' ? true : false; - // Fixing permissions. - $command = $this->getApplication()->find( - 'files:set_permissions_after_install' - ); - $arguments = [ - 'command' => 'files:set_permissions_after_install', - '--conf' => $configurationPathFromHelper, - '--linux-user' => $linuxUser, - '--linux-group' => $linuxGroup, - '--dry-run' => $dryRun - ]; + if (!empty($customPackage)) { + $downloadPackage = false; + } - $input = new ArrayInput($arguments); - $command->run($input, $output); + if ($onlyUpdateDatabase) { + $downloadPackage = false; } - $output->writeln("Hurray!!! You just finished this migration. To check the current status of your platform, run chamilo:status"); - $endTime = time(); - $totalTimeInMinutes = round(($endTime - $startTime)/60, 2); - $output->writeln("The script took $totalTimeInMinutes minutes to execute."); - } + // Getting supported version number list + $versionNameList = $this->getVersionNumberList(); + $minVersion = $this->getMinVersionSupportedByInstall(); + $versionList = $this->availableVersions(); - /** - * Starts a migration - * - * @param array $courseList - * @param string $path - * @param string $toVersion - * @param bool $dryRun - * @param OutputInterface $output - * @param bool $removeUnusedTables - * @param InputInterface $mainInput - * @param bool $runFixIds - * @param bool $onlyUpdateDatabase - * - * @return bool - * @throws \Exception - */ - public function startMigration( - $courseList, - $path, - $toVersion, - $dryRun, - OutputInterface $output, - $removeUnusedTables = false, - InputInterface $mainInput, - $runFixIds = true, - $onlyUpdateDatabase = false - ) { - // Cleaning query list. - $this->queryList = []; + // Checking version. + if ($version != 'master') { + if (!in_array($version, $versionNameList)) { + $output->writeln("Version '$version' is not available."); + $output->writeln("Available versions: ".implode(', ', $versionNameList).""); - // Main DB connection. - $conn = $this->getConnection($mainInput); - $_configuration = $this->getHelper('configuration')->getConfiguration($path); - $versionInfo = $this->getAvailableVersionInfo($toVersion); - $installPath = $this->getInstallationFolder().$toVersion.'/'; + return 0; + } + } - // Filling sqlList array with "pre" db changes. - if (isset($versionInfo['pre']) && !empty($versionInfo['pre'])) { - $sqlToInstall = $installPath.$versionInfo['pre']; - $this->fillQueryList($sqlToInstall, $output, 'pre'); + // Setting the configuration path and configuration array + $_configuration = $this->getConfigurationHelper()->getConfiguration($path); - // Processing sql query list depending of the section (course, main, user). - $this->processQueryList( - $courseList, - $output, - $path, - $toVersion, - $dryRun, - 'pre' - ); - } + if (empty($_configuration)) { + $output->writeln("Chamilo is not installed here! You may add a path as an option:"); + $output->writeln("For example: chamilo:upgrade 1.11.x --path=/var/www/chamilo"); - try { - if (isset($versionInfo['hook_to_doctrine_version'])) { - // Doctrine migrations: - $em = $this->setDoctrineSettings($this->getHelperSet()); - $output->writeln(''); - $output->writeln( - "You have to select 'yes' for the 'Chamilo Migrations'" - ); + return 0; + } - // Setting migrations temporal ymls - $tempFolder = '/tmp'; - require_once $_configuration['root_sys'].'app/Migrations/AbstractMigrationChamilo.php'; - $migrationsFolder = $tempFolder.'/Migrations/'; + $this->setConfigurationArray($_configuration); + $this->getConfigurationHelper()->setConfiguration($_configuration); + $this->setRootSysDependingConfigurationPath($path); - $fs = new Filesystem(); - if (!$fs->exists($migrationsFolder)) { - $fs->mkdir($migrationsFolder); - } + $configurationPath = $this->getHelper('configuration')->getConfigurationPath($path); - $migrations = [ - 'name' => 'Chamilo Migrations', - 'migrations_namespace' => $versionInfo['migrations_namespace'], - 'table_name' => 'version', - 'migrations_directory' => $versionInfo['migrations_directory'], - ]; + // Checking configuration file. + if (!is_writable($configurationPath)) { + $output->writeln("Folder ".$configurationPath." must have writeable permissions"); - $dumper = new Dumper(); - $yaml = $dumper->dump($migrations, 1); - $file = $migrationsFolder.$versionInfo['migrations_yml']; + return 0; + } - if (file_exists($file)) { - unlink($file); - } + $this->setConfigurationPath($configurationPath); - file_put_contents($file, $yaml); - $command = new MigrateCommand(); - // Creates the helper set - $helperSet = \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($em); - $helper = $this->getHelperSet()->get('question'); - $helperSet->set($helper, 'question'); - $command->setHelperSet($helperSet); + // $_configuration['password_encryption'] must exists + if (isset($_configuration['password_encryption'])) { + $output->writeln("\$_configuration[password_encryption] value found: ".$_configuration['password_encryption'].""); + } else { + $output->writeln("\$_configuration['password_encryption'] not found. The key 'password_encryption' or the variable '\$userPasswordCrypted' must exist in the configuration.php file "); - $arguments = [ - //'command' => 'migrations:migrate', - '--configuration' => $file, - '--dry-run' => $dryRun, - 'version' => $versionInfo['hook_to_doctrine_version'] - ]; + return 0; + } - $output->writeln( - "Executing migrations:migrate ".$versionInfo['hook_to_doctrine_version']." --configuration=".$file."" - ); - $input = new ArrayInput($arguments); - $command->run($input, $output); - $output->writeln( - "Migration ended successfully" - ); - } + if ($dryRun == false) { + if ($downloadPackage) { + $output->writeln("Downloading package ..."); + $chamiloLocationPath = $this->getPackage($output, $originalVersion, $updateInstallation, $tempFolder); + if (empty($chamiloLocationPath)) { + $output->writeln("Chash was not able to unzip the downloaded package for version: $originalVersion"); - // Processing "db" changes. - if (isset($versionInfo['update_db']) && !empty($versionInfo['update_db'])) { - $sqlToInstall = $installPath.$versionInfo['update_db']; - if (is_file($sqlToInstall) && file_exists($sqlToInstall)) { - if ($dryRun) { - $output->writeln("File to be executed but not fired because of dry-run option: '$sqlToInstall'"); - } else { - $output->writeln("Executing update db: '$sqlToInstall'"); - } - require $sqlToInstall; + return 0; + } - if (!empty($update)) { - $update($_configuration, $conn, $courseList, $dryRun, $output, $this, $removeUnusedTables); - } - } else { - $output->writeln(sprintf("File doesn't exist: '%s'", $sqlToInstall)); + $this->copyPackageIntoSystem($output, $chamiloLocationPath, null); + } else { + if (!empty($customPackage)) { + $chamiloLocationPath = $customPackage; } } + } - // Processing "update file" changes. - if (isset($versionInfo['update_files']) && !empty($versionInfo['update_files']) && $onlyUpdateDatabase == false) { - $sqlToInstall = $installPath.$versionInfo['update_files']; - if (is_file($sqlToInstall) && file_exists($sqlToInstall)) { - if ($dryRun) { - $output->writeln("Files to be executed but dry-run is on: '$sqlToInstall'"); - } else { - $output->writeln("Executing update files: '$sqlToInstall'"); + // Checking Resources/Database dir. Getting the Resources/Database/1.8.7/db_main.sql. + $testFolder = $this->getInstallationFolder().'1.8.7/db_main.sql'; + $installationFolder = $this->getInstallationFolder(); + $_configuration = $this->getConfigurationHelper()->getConfiguration($path); - require $sqlToInstall; + if (PHP_SAPI != 'cli') { + $this->commandLine = false; + } - if (!empty($updateFiles)) { - $updateFiles($_configuration, $conn, $courseList, $dryRun, $output, $this); - } - } - } else { - $output->writeln(sprintf("File doesn't exist: '%s'", $sqlToInstall)); - } - } + $doctrineVersion = null; + $currentVersion = null; + if (!file_exists($testFolder)) { + $output->writeln("The migration directory was not detected: $installationFolder"); - // Filling sqlList array with "post" db changes. - if (isset($versionInfo['post']) && !empty($versionInfo['post'])) { - $sqlToInstall = $installPath.$versionInfo['post']; - $this->fillQueryList($sqlToInstall, $output, 'post'); - // Processing sql query list depending of the section. - $this->processQueryList($courseList, $output, $path, $toVersion, $dryRun, 'post'); - } + return 0; + } else { + $output->writeln("Reading migrations from directory: $installationFolder"); + } - $filesToLoad = [ - $this->getRootSys().'/main/inc/lib/database.constants.inc.php', - $this->getRootSys().'/main/inc/lib/system/session.class.php', - $this->getRootSys().'/main/inc/lib/chamilo_session.class.php', - $this->getRootSys().'/main/inc/lib/api.lib.php', - $this->getRootSys().'/main/inc/lib/database.lib.php', - $this->getRootSys().'/main/inc/lib/custom_pages.class.php', - $this->getRootSys().'/main/install/install.lib.php', - $this->getRootSys().'/main/inc/lib/display.lib.php', - $this->getRootSys().'/main/inc/lib/group_portal_manager.lib.php', - $this->getRootSys().'/main/inc/lib/model.lib.php', - $this->getRootSys().'/main/inc/lib/events.lib.php', - $this->getRootSys().'/main/inc/lib/extra_field.lib.php', - $this->getRootSys().'/main/inc/lib/extra_field_value.lib.php', - $this->getRootSys().'/main/inc/lib/urlmanager.lib.php', - $this->getRootSys().'/main/inc/lib/usermanager.lib.php', - $this->getRootSys().'/src/Chamilo/CoreBundle/Entity/ExtraField.php', - $this->getRootSys().'/src/Chamilo/CoreBundle/Entity/ExtraFieldOptions.php' - ]; + // In order to use Doctrine migrations - if ($runFixIds) { - foreach ($filesToLoad as $file) { - require_once $file; - } + // Setting configuration variable in order to get the doctrine version: + //$input->setOption('configuration', $this->getMigrationConfigurationFile()); + //$configuration = $this->getMigrationConfiguration($input, $output); - $output->writeln("Run fixIds function "); - fixIds($em); - } + // Doctrine migrations version + //$doctrineVersion = $configuration->getCurrentVersion(); + // Moves files from main/inc/conf to config/ - if (method_exists('fixPostGroupIds') && - $versionInfo['migrations_yml'] == 'V111.yml' - ) { - foreach ($filesToLoad as $file) { - require_once $file; - } - $output->writeln("Run fixPostGroupIds function "); - fixPostGroupIds($conn); - } else { - $output->writeln("Not found function: fixPostGroupIds"); - } - } catch (\Exception $e) { - $output->write(sprintf('Migration failed. Error %s', $e->getMessage())); + // Checking system_version. + if (!isset($_configuration['system_version']) || + empty($_configuration['system_version']) + ) { + $output->writeln("There is something wrong in your Chamilo installation. Check it with the chash:chamilo_status command"); - throw $e; + return 0; } + if (version_compare($_configuration['system_version'], $minVersion, '<')) { + $output->writeln("Your Chamilo version is not supported! The minimun version is: $minVersion"); + $output->writeln("You want to upgrade from ".$_configuration['system_version']." to $minVersion"); - return false; - } + return 0; + } - /** - * @return array - */ - public function getMigrationTypes() - { - return [ - 'pre', - 'post' - ]; - } + if ($version != 'master') { + if (version_compare($version, $_configuration['system_version'], '>')) { + $currentVersion = $_configuration['system_version']; + } else { + $output->writeln("Please provide a version greater than ".$_configuration['system_version'].""); + $output->writeln("You selected version: $version"); + $output->writeln("You can also check your installation status with chash:chamilo_status"); - /** - * - * Process the queryList array and executes queries to the correct section (main, user, course, etc) - * - * @param array $courseList - * @param OutputInterface $output - * @param $path - * @param $version - * @param $dryRun - * @param $type - * @return bool - * @throws \Exception - */ - public function processQueryList($courseList, $output, $path, $version, $dryRun, $type) - { - $databases = $this->getDatabaseList($output, $courseList, $path, $version, $type); - $this->setConnections($version, $path, $databases); + return 0; + } + } else { + $currentVersion = $_configuration['system_version']; + $version = $this->getLatestVersion(); + } - foreach ($databases as $section => &$dbList) { - foreach ($dbList as &$dbInfo) { - $output->writeln(""); - $output->writeln("Loading section: $section using database key ".$dbInfo['database'].""); - $output->writeln("--------------------------"); + $versionInfo = $this->getAvailableVersionInfo($version); - if ($dbInfo['status'] == 'complete') { - $output->writeln("Database already updated."); - continue; - } + if (empty($versionInfo)) { + $output->writeln("The current version ($version) is not supported"); - if (isset($this->queryList[$type]) && - isset($this->queryList[$type][$section]) && - !empty($this->queryList[$type][$section]) - ) { - $queryList = $this->queryList[$type][$section]; - $output->writeln("Loading queries list: '$type' - '$section'"); + return 0; + } - if (!empty($queryList)) { - try { - $lines = 0; + if (isset($versionInfo['hook_to_doctrine_version']) && + isset($doctrineVersion) + ) { + if ($doctrineVersion == $versionInfo['hook_to_doctrine_version']) { + $output->writeln("You already have the latest version. Nothing to update! Doctrine version $doctrineVersion"); - /** @var \Doctrine\DBAL\Connection $conn */ - $conn = $this->getHelper($dbInfo['database'])->getConnection(); - $output->writeln("Executing queries in DB: ".$conn->getDatabase().""); + return 0; + } + } - $conn->beginTransaction(); + if (isset($versionInfo['parent']) && !empty($versionInfo['parent'])) { + $versionInfoParent = $this->getAvailableVersionInfo($versionInfo['parent']); + if ($doctrineVersion == $versionInfoParent['hook_to_doctrine_version']) { + $output->writeln("You already have the latest version. Nothing to update! Doctrine version $doctrineVersion"); - foreach ($queryList as $query) { - // Add a prefix. + return 0; + } + } - if ($section == 'course') { - $query = str_replace('{prefix}', $dbInfo['prefix'], $query); - } + $this->writeCommandHeader($output, 'Welcome to the Chamilo upgrade process!'); - if ($dryRun) { - $output->writeln($query); - } else { - $output->writeln(' -> ' . $query); - $conn->executeQuery($query); - //$conn->exec($query); - } - $lines++; - } + if ($dryRun == false) { + if ($downloadPackage) { + $output->writeln("When the installation process finishes, the files located here:"); + $output->writeln("$chamiloLocationPath"); + $output->writeln("will be copied to your portal here: ".$this->getRootSys().""); + } - if (!$dryRun) { - if ($conn->isTransactionActive()) { - $conn->commit(); - } - $output->writeln(sprintf('%d statements executed!', $lines) . PHP_EOL); - $dbInfo['status'] = 'complete'; - $this->saveDatabaseList($path, $databases, $version, $type); - } - } catch (\Exception $e) { - $conn->rollback(); - $output->write(sprintf('Migration failed. Error %s', $e->getMessage())); - throw $e; - } + if ($removeUnusedTables) { + if (version_compare($currentVersion, '1.9.0', '<')) { + $onlyPrefix = $this->getTablePrefix($_configuration); + if (!empty($onlyPrefix)) { + $output->writeln("--remove-unused-table option is on. Unused tables will be removed."); + $tablesToDelete = "SHOW TABLES LIKE '".$onlyPrefix."%';"; + $output->writeln("All tables that match this query will be deleted: $tablesToDelete"); } else { - $output->writeln(sprintf("queryList array is empty.")); + $output->writeln("--remove-unused-table option is on. But any table will be removed."); } } else { - $output->writeln(sprintf("Nothing to execute for section $section!")); - - return false; + $output->writeln("--remove-unused-table option is on. This does not affect this $originalVersion version."); } } + } else { + $output->writeln("When the installation process finishes, PHP files are not going to be updated (--dry-run is on)."); } - $this->queryList = []; - return true; - } + $helper = $this->getHelperSet()->get('question'); - /** - * - * Reads a sql file and adds queries in the queryList array. - * - * @param string $sqlFilePath - * @param OutputInterface $output - * @param string type - */ - public function fillQueryList($sqlFilePath, $output, $type) - { - $output->writeln(sprintf("Processing file type: $type '%s'... ", $sqlFilePath)); - $sections = $this->getSections(); + if ($silent == false) { + $question = new ConfirmationQuestion( + 'Are you sure you want to upgrade the Chamilo located here? '.$_configuration['root_sys'].' (y/N)', + false + ); + if (!$helper->ask($input, $output, $question)) { + return; + } - foreach ($sections as $section) { - $sqlList = $this->getSQLContents($sqlFilePath, $section, $output); - $this->setQueryList($sqlList, $section, $type); + $question = new ConfirmationQuestion( + 'Are you sure you want to upgrade from version '.$_configuration['system_version'].' to version '.$version.' (y/N)', + false + ); + if (!$helper->ask($input, $output, $question)) { + return; + } } - } - /** - * Setting the queryList array - * - * @param array $queryList - * @param string $section - * @param string $type - */ - public function setQueryList($queryList, $section, $type) - { - if (!isset($this->queryList[$type][$section])) { - $this->queryList[$type][$section] = $queryList; - } else { - $this->queryList[$type][$section] = array_merge($this->queryList[$type][$section], $queryList); - } - } + $output->writeln('Migrating from Chamilo version: '.$_configuration['system_version'].' to version '.$version); + $output->writeln('Starting upgrade for Chamilo, reading configuration file located here: '.$configurationPath.'configuration.php'); - /** - * Returns sections - * @return array - */ - public function getSections() - { - return [ - 'main', - 'user', - 'stats', - 'scorm', - 'course' + // Getting configuration file. + $_configuration = $this->getHelper('configuration')->getConfiguration($path); + + // Upgrade always from a mysql driver + $databaseSettings = [ + 'driver' => 'pdo_mysql', + 'host' => $_configuration['db_host'], + 'dbname' => $_configuration['main_database'], + 'user' => $_configuration['db_user'], + 'password' => $_configuration['db_password'], ]; - } - /** - * Generates database array info - * - * @param array $courseList - * @return array - */ - public function generateDatabaseList($courseList) - { - $courseDbList = []; - $_configuration = $this->getConfigurationArray(); - if (!empty($courseList)) { - foreach ($courseList as $course) { - if (!empty($course['db_name'])) { - $courseDbList[] = [ - 'database' => '_chamilo_course_'.$course['db_name'], - 'prefix' => $this->getTablePrefix($_configuration, $course['db_name']), - 'status' => 'waiting' - ]; - } - } + // Setting DB access. + $this->setDatabaseSettings($databaseSettings); + + $extraDatabaseSettings = [ + 'single_database' => isset($_configuration['single_database']) ? $_configuration['single_database'] : false, + 'table_prefix' => isset($_configuration['table_prefix']) ? $_configuration['table_prefix'] : null, + 'db_glue' => isset($_configuration['db_glue']) ? $_configuration['db_glue'] : null, + 'db_prefix' => isset($_configuration['db_prefix']) ? $_configuration['db_prefix'] : null, + ]; + + $this->setExtraDatabaseSettings($extraDatabaseSettings); + $this->setDoctrineSettings($this->getHelperSet()); + $conn = $this->getConnection($input); + $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); + + if ($conn) { + $output->writeln("Connection to the database established."); } else { - $courseDbList = [ - [ - 'database'=> 'main_database', - 'status' => 'waiting', - 'prefix' => null - ] - ]; + $output->writeln("Can't connect to the DB with user:".$_configuration['db_user']).""; + + return 0; } - $databaseSection = [ - 'main' => [ - [ - 'database' => 'main_database', - 'status' => 'waiting' - ] - ], - 'user' => [ - [ - 'database' => 'user_personal_database', - 'status' => 'waiting' - ] - ], - 'stats' => [ - [ - 'database' => 'statistics_database', - 'status' => 'waiting' - ] - ], - 'course'=> $courseDbList - ]; + // Get course list + $query = "SELECT * FROM course"; + $result = $conn->executeQuery($query); + $courseList = $result->fetchAll(); - $this->setDatabaseList($databaseSection); - return $this->databaseList; - } + $output->writeln("Current version: $currentVersion"); + $output->writeln("Latest version: $version"); + $oldVersion = $version; - /** - * Sets the database list - * @param array $list - */ - public function setDatabaseList($list) - { - $this->databaseList = $list; - } + // Handle 1.10.x as 1.10.1000 + if ($currentVersion == '1.10.x') { + $currentVersion = '1.10.1000'; + } - /** - * @param OutputInterface $output - * @param array $courseList - * @param string $path - * @param string $version - * @param string $type - * - * @return mixed|void - */ - public function getDatabaseList($output, $courseList, $path, $version, $type) - { - return $this->generateDatabaseList($courseList); + if ($currentVersion == '1.11.x') { + $currentVersion = '1.11.1000'; + } - $configurationPath = $this->getHelper('configuration')->getConfigurationPath($path); - $newConfigurationFile = $configurationPath.'db_migration_status_'.$version.'_'.$type.'.yml'; - if (file_exists($newConfigurationFile)) { - $yaml = new Parser(); - $output->writeln("Loading databases list status from file: $newConfigurationFile"); + if ($version == '1.10.x') { + $version = '1.10.1000'; + } - return $yaml->parse(file_get_contents($newConfigurationFile)); - } else { - return $this->generateDatabaseList($courseList); + if ($version == '1.11.x') { + $version = '1.11.1000'; } - } - /** - * @param string $path - * @param string $databaseSection - * @param string $version - * @param string $type - * - * @return bool - */ - public function saveDatabaseList($path, $databaseSection, $version, $type) - { - $configurationPath = $this->getHelper('configuration')->getConfigurationPath($path); - $dumper = new Dumper(); - $yaml = $dumper->dump($databaseSection, 2); //inline - $newConfigurationFile = $configurationPath.'db_migration_status_'.$version.'_'.$type.'.yml'; - file_put_contents($newConfigurationFile, $yaml); + if (version_compare($version, '1.10.0', '>=')) { + require_once $_configuration['root_sys'].'src/Chamilo/CoreBundle/Entity/SettingsCurrent.php'; + require_once $_configuration['root_sys'].'src/Chamilo/CoreBundle/Entity/SystemTemplate.php'; + require_once $_configuration['root_sys'].'src/Chamilo/CoreBundle/Entity/SettingsOptions.php'; + require_once $_configuration['root_sys'].'app/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php'; + require_once $_configuration['root_sys'].'main/inc/lib/api.lib.php'; + require_once $_configuration['root_sys'].'main/inc/lib/custom_pages.class.php'; + require_once $_configuration['root_sys'].'main/inc/lib/database.lib.php'; - return file_exists($newConfigurationFile); - } + if (!is_dir($_configuration['root_sys'].'vendor')) { + $output->writeln("Execute composer update in your chamilo instance first. Then continue with the upgrade"); - /** - * @param OutputInterface $output - * @param array $courseList - * @param string $path - * @param string $section - * @param string $version - * @param string $type - * - * @return mixed - */ - public function getDatabasesPerSection($output, $courseList, $path, $section, $version, $type) - { - $databases = $this->getDatabaseList($output, $courseList, $path, $version, $type); - if (isset($databases[$section])) { - return $databases[$section]; + return 1; + } else { + $question = new ConfirmationQuestion( + 'Are you sure you run composer before executing this upgrade?(y/N)', + false + ); + if (!$helper->ask($input, $output, $question)) { + return; + } + } } - } - /** - * Function originally wrote in install.lib.php - * - * @param string $file - * @param string $section - * @param OutputInterface $output - * - * @return array|bool - */ - public function getSQLContents($file, $section, $output) - { - if (empty($file) || file_exists($file) == false) { - $output->writeln(sprintf("File doesn't exist: '%s'... ", $file)); - return false; + $versionsToRun = []; + foreach ($versionList as $versionItem => $versionInfo) { + if (version_compare($versionItem, $currentVersion, '>') && + version_compare($versionItem, $version, '<=') + ) { + $versionsToRun[$versionItem] = $versionInfo; + } } - if (!in_array($section, ['main', 'user', 'stats', 'scorm', 'course'])) { - $output->writeln(sprintf("Section is %s not authorized in getSQLContents()", $section)); - return false; + $lastItem = count($versionsToRun); + $counter = 0; + $runFixIds = false; + foreach ($versionsToRun as $versionItem => $versionInfo) { + if ($lastItem == $counter) { + $runFixIds = true; + } + + if (isset($versionInfo['require_update']) && + $versionInfo['require_update'] == true + ) { + $output->writeln("----------------------------------------------------------------"); + $output->writeln("Starting migration from version: $currentVersion to version $versionItem "); + $output->writeln(""); + + // Greater than my current version. + $this->startMigration( + $courseList, + $path, + $versionItem, + $dryRun, + $output, + $removeUnusedTables, + $input, + $runFixIds, + $onlyUpdateDatabase + ); + + $currentVersion = $versionItem; + $output->writeln("End database migration"); + $output->writeln("----------------------------------------------------------------"); + } else { + $currentVersion = $versionItem; + $output->writeln("Skip migration from version: $currentVersion to version $versionItem "); + } + $counter++; } - // Empty lines should not be executed as SQL statements, because errors occur, see Task #2167. - $fileContents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); - if (!is_array($fileContents) or count($fileContents) < 1) { - $output->writeln(sprintf("File '%s' looks empty in getSQLContents()", $file)); - return false; + // Restore old version + $version = $oldVersion; + + // Update chamilo files. + if ($dryRun == false && $onlyUpdateDatabase == false) { + $output->writeln("Version: $version"); + if ( + $version === '10' || + $version === '1.10.x' || + $version === '11' || + $version === '1.11.x' + ) { + $this->removeUnUsedFiles($output, $path); + $this->copyConfigFilesToNewLocation($output); + } } - // Prepare the resulting array - $sectionContents = []; - $record = false; - foreach ($fileContents as $line) { - if (substr($line, 0, 2) == '--') { - //This is a comment. Check if section name, otherwise ignore - $result = []; - if (preg_match('/^-- xx([A-Z]*)xx/', $line, $result)) { //we got a section name here - if ($result[1] == strtoupper($section)) { - //we have the section we are looking for, start recording - $record = true; - } else { - //we have another section's header. If we were recording, stop now and exit loop - if ($record) { - break; - } - $record = false; - } - } - } else { - if ($record) { - if (!empty($line)) { - $sectionContents[] = $line; - } - } + if ($onlyUpdateDatabase == false) { + $configurationPathFromHelper = $this->getConfigurationHelper()->getConfigurationFilePath($path); + $output->writeln("Reading path in : $path"); + $output->writeln("Configuration path in : $configurationPathFromHelper"); + + if (empty($configurationPathFromHelper)) { + $output->writeln("Configuration path is not found. Check that the configuration.php exists here: $path."); + exit; } + + // Generating temp folders. + $command = $this->getApplication()->find( + 'files:generate_temp_folders' + ); + $arguments = [ + 'command' => 'files:generate_temp_folders', + '--conf' => $configurationPathFromHelper, + '--dry-run' => $dryRun, + ]; + + $input = new ArrayInput($arguments); + $command->run($input, $output); + + // Update configuration file new system_version + $newParams = ['system_version' => $version]; + $this->updateConfiguration($output, $dryRun, $newParams); + + // Fixing permissions. + $command = $this->getApplication()->find( + 'files:set_permissions_after_install' + ); + $arguments = [ + 'command' => 'files:set_permissions_after_install', + '--conf' => $configurationPathFromHelper, + '--linux-user' => $linuxUser, + '--linux-group' => $linuxGroup, + '--dry-run' => $dryRun, + ]; + + $input = new ArrayInput($arguments); + $command->run($input, $output); } - return $sectionContents; + $output->writeln("Hurray!!! You just finished this migration. To check the current status of your platform, run chamilo:status"); + $endTime = time(); + $totalTimeInMinutes = round(($endTime - $startTime) / 60, 2); + $output->writeln("The script took $totalTimeInMinutes minutes to execute."); } /** - * Creates the course tables with the prefix c_ - * @param OutputInterface $output - * @param string $dryRun - * @return int + * Get connection. + * + * @return \Doctrine\DBAL\Connection */ - public function createCourseTables($output, $dryRun) + private function getConnection() { - if ($dryRun) { - $output->writeln("Creating c_* tables but dry-run is on. 0 table created."); - return 0; - } - - $output->writeln('Creating course tables (c_*)'); - - $command = $this->getApplication()->find('dbal:import'); - $sqlFolder = $this->getInstallationPath('1.9.0'); - - // Importing sql files. - $arguments = [ - 'command' => 'dbal:import', - 'file' => $sqlFolder.'db_course.sql' - ]; - $input = new ArrayInput($arguments); - $command->run($input, $output); + return $this->getHelper('db')->getConnection(); } } diff --git a/src/Command/Installation/UpgradeDatabaseCommand.php b/src/Command/Installation/UpgradeDatabaseCommand.php index 11cefa6..d29bc9f 100644 --- a/src/Command/Installation/UpgradeDatabaseCommand.php +++ b/src/Command/Installation/UpgradeDatabaseCommand.php @@ -8,12 +8,12 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Yaml\Dumper; use Symfony\Component\Yaml\Parser; -use Symfony\Component\Filesystem\Filesystem; /** - * Class UpgradeDatabaseCommand + * Class UpgradeDatabaseCommand. */ class UpgradeDatabaseCommand extends CommonCommand { @@ -22,255 +22,17 @@ class UpgradeDatabaseCommand extends CommonCommand public $commandLine = true; /** - * Get connection - * @return \Doctrine\DBAL\Connection - */ - private function getConnection() - { - return $this->getHelper('db')->getConnection(); - } - - protected function configure(): void - { - $this - ->setName('chash:chamilo_upgrade_database') - ->setDescription('Execute a chamilo migration to a specified version or the latest available version') - ->addArgument('from-version', InputArgument::REQUIRED, 'The version to migrate to', null) - ->addArgument('to-version', InputArgument::REQUIRED, 'The version to migrate to', null) - ->addArgument('host', null, InputArgument::REQUIRED, 'host', 'localhost') - ->addArgument('username', null, InputArgument::REQUIRED, 'username', 'root') - ->addArgument('password', null, InputArgument::REQUIRED, 'password', 'root') - ->addArgument('db_name', null, InputArgument::REQUIRED, 'Database name', '') - ->addArgument('root_sys', null, InputArgument::REQUIRED, 'Chamilo root_sys', '') - ; - } - - /** - * Executes a command via CLI - * - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int|null|void - */ - protected function execute(InputInterface $input, OutputInterface $output) - { - // sudo php /var/www/html/chash/chash.php chash:chamilo_upgrade 1.11.x --linux-user=jmontoya --linux-group=jmontoya - - $startTime = time(); - - // Arguments and options - $currentVersion = $originalVersion = $input->getArgument('from-version'); - $version = $input->getArgument('to-version'); - $host = $input->getArgument('host'); - $username = $input->getArgument('username'); - $password = $input->getArgument('password'); - $database = $input->getArgument('db_name'); - $rootSys = $input->getArgument('root_sys'); - - // Getting supported version number list - $versionNameList = $this->getVersionNumberList(); - $minVersion = $this->getMinVersionSupportedByInstall(); - $versionList = $this->availableVersions(); - - // Checking version. - if ($version != 'master') { - if (!in_array($version, $versionNameList)) { - $output->writeln("Version '$version' is not available."); - $output->writeln("Available versions: ".implode(', ', $versionNameList).""); - return 0; - } - } - - if (version_compare($version, $minVersion, '<')) { - $output->writeln("Your Chamilo version is not supported! The minimun version is: $minVersion"); - $output->writeln("You want to upgrade from ".$version." to $minVersion"); - return 0; - } - - $versionInfo = $this->getAvailableVersionInfo($version); - - if (empty($versionInfo)) { - $output->writeln("The current version ($version) is not supported"); - - return 0; - } - - if (isset($versionInfo['hook_to_doctrine_version']) && - isset($doctrineVersion) - ) { - if ($doctrineVersion == $versionInfo['hook_to_doctrine_version']) { - $output->writeln("You already have the latest version. Nothing to update! Doctrine version $doctrineVersion"); - return 0; - } - } - - $this->writeCommandHeader($output, 'Welcome to the Chamilo upgrade process!'); - - $output->writeln('Migrating from Chamilo version: '.$currentVersion.' to version '.$version); - - // Upgrade always from a mysql driver - $databaseSettings = [ - 'driver' => 'pdo_mysql', - 'host' => $host, - 'dbname' => $database, - 'user' => $username, - 'password' => $password, - ]; - - // Setting DB access. - $this->setDatabaseSettings($databaseSettings); - - $extraDatabaseSettings = [ - 'single_database'=> isset($_configuration['single_database']) ? $_configuration['single_database'] : false, - 'table_prefix'=> isset($_configuration['table_prefix']) ? $_configuration['table_prefix'] : null, - 'db_glue' => isset($_configuration['db_glue']) ? $_configuration['db_glue'] : null, - 'db_prefix' => isset($_configuration['db_prefix']) ? $_configuration['db_prefix'] : null, - ]; - - $this->setExtraDatabaseSettings($extraDatabaseSettings); - $this->setDoctrineSettings($this->getHelperSet()); - $conn = $this->getConnection($input); - $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); - - if ($conn) { - $output->writeln("Connection to the database established."); - } else { - $output->writeln("Can't connect to the DB with user:".$_configuration['db_user']).""; - - return 0; - } - - // Get course list - $query = "SELECT * FROM course"; - $result = $conn->executeQuery($query); - $courseList = $result->fetchAll(); - - $output->writeln("Current version: $currentVersion"); - $output->writeln("Latest version: $version"); - $oldVersion = $version; - - // Handle 1.10.x as 1.10.1000 - if ($currentVersion == '1.9.x') { - $currentVersion = '1.9.1000'; - } - - if ($currentVersion == '1.10.x') { - $currentVersion = '1.10.1000'; - } - - if ($currentVersion == '1.11.x') { - $currentVersion = '1.11.1000'; - } - - if ($version == '1.9.x') { - $version = '1.9.1000'; - } - - if ($version == '1.10.x') { - $version = '1.10.1000'; - } - - if ($version == '1.11.x') { - $version = '1.11.1000'; - } - - require_once $rootSys.'src/Chamilo/CoreBundle/Entity/SettingsCurrent.php'; - require_once $rootSys.'src/Chamilo/CoreBundle/Entity/SystemTemplate.php'; - require_once $rootSys.'src/Chamilo/CoreBundle/Entity/SettingsOptions.php'; - require_once $rootSys.'app/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php'; - require_once $rootSys.'main/inc/lib/api.lib.php'; - require_once $rootSys.'main/inc/lib/custom_pages.class.php'; - require_once $rootSys.'main/inc/lib/database.lib.php'; - - if (!is_dir($rootSys.'vendor')) { - $output->writeln("Execute composer update in your chamilo instance first. Then continue with the upgrade"); - - return 1; - } - - $versionsToRun = []; - foreach ($versionList as $versionItem => $versionInfo) { - if (version_compare($versionItem, $currentVersion, '>') && - version_compare($versionItem, $version, '<=') - ) { - $versionsToRun[$versionItem] = $versionInfo; - } - } - - foreach ($versionsToRun as $versionItem => $versionInfo) { - if (isset($versionInfo['require_update']) && $versionInfo['require_update'] == true) { - $output->writeln("----------------------------------------------------------------"); - $output->writeln("Starting migration from version: $currentVersion to version $versionItem "); - $output->writeln(""); - - // Greater than my current version. - $this->startMigration( - $courseList, - null, - $versionItem, - $dryRun = false, - $output, - $removeUnusedTables = false, - $input, - false, - $onlyUpdateDatabase = true, - $rootSys - ); - $currentVersion = $versionItem; - $output->writeln("End database migration"); - $output->writeln("----------------------------------------------------------------"); - } else { - $currentVersion = $versionItem; - $output->writeln("Skip migration from version: $currentVersion to version $versionItem "); - } - - /* - * If the migration from 1.9.x to 1.10.x is finished then we fix the IDs and extra fields - */ - if ($originalVersion === '1.9.x' && $currentVersion === '1.10.x') { - require_once $rootSys.'main/inc/lib/database.constants.inc.php'; - require_once $rootSys.'main/inc/lib/system/session.class.php'; - require_once $rootSys.'main/inc/lib/chamilo_session.class.php'; - require_once $rootSys.'main/inc/lib/api.lib.php'; - require_once $rootSys.'main/inc/lib/database.lib.php'; - require_once $rootSys.'main/inc/lib/custom_pages.class.php'; - require_once $rootSys.'main/install/install.lib.php'; - require_once $rootSys.'main/inc/lib/display.lib.php'; - //require_once $rootSys.'main/inc/lib/group_portal_manager.lib.php'; - require_once $rootSys.'main/inc/lib/model.lib.php'; - require_once $rootSys.'main/inc/lib/events.lib.php'; - require_once $rootSys.'main/inc/lib/extra_field.lib.php'; - require_once $rootSys.'main/inc/lib/extra_field_value.lib.php'; - require_once $rootSys.'main/inc/lib/urlmanager.lib.php'; - require_once $rootSys.'main/inc/lib/usermanager.lib.php'; - require_once $rootSys.'src/Chamilo/CoreBundle/Entity/ExtraField.php'; - require_once $rootSys.'src/Chamilo/CoreBundle/Entity/ExtraFieldOptions.php'; - - $em = $this->setDoctrineSettings($this->getHelperSet()); - fixIds($em); - } - } - - $output->writeln("Hurray!!! You just finished this migration. To check the current status of your platform, run chamilo:status"); - $endTime = time(); - $totalTimeInMinutes = round(($endTime - $startTime)/60, 2); - $output->writeln("The script took $totalTimeInMinutes minutes to execute."); - } - - /** - * Starts a migration + * Starts a migration. * - * @param array $courseList + * @param array $courseList * @param string $path * @param string $toVersion - * @param bool $dryRun - * @param OutputInterface $output - * @param bool $removeUnusedTables - * @param InputInterface $mainInput + * @param bool $dryRun + * @param bool $removeUnusedTables * - * @return bool * @throws \Exception + * + * @return bool */ public function startMigration( $courseList, @@ -337,7 +99,7 @@ public function startMigration( //'command' => 'migrations:migrate', '--configuration' => $file, '--dry-run' => $dryRun, - 'version' => $versionInfo['hook_to_doctrine_version'] + 'version' => $versionInfo['hook_to_doctrine_version'], ]; $output->writeln( @@ -375,7 +137,6 @@ public function startMigration( throw $e; } - return false; } @@ -386,22 +147,23 @@ public function getMigrationTypes() { return [ 'pre', - 'post' + 'post', ]; } /** + * Process the queryList array and executes queries to the correct section (main, user, course, etc). * - * Process the queryList array and executes queries to the correct section (main, user, course, etc) - * - * @param array $courseList + * @param array $courseList * @param OutputInterface $output * @param $path * @param $version * @param $dryRun * @param $type - * @return bool + * * @throws \Exception + * + * @return bool */ public function processQueryList($courseList, $output, $path, $version, $dryRun, $type) { @@ -446,7 +208,7 @@ public function processQueryList($courseList, $output, $path, $version, $dryRun, if ($dryRun) { $output->writeln($query); } else { - $output->writeln(' -> ' . $query); + $output->writeln(' -> '.$query); $conn->executeQuery($query); //$conn->exec($query); } @@ -457,7 +219,7 @@ public function processQueryList($courseList, $output, $path, $version, $dryRun, if ($conn->isTransactionActive()) { $conn->commit(); } - $output->writeln(sprintf('%d statements executed!', $lines) . PHP_EOL); + $output->writeln(sprintf('%d statements executed!', $lines).PHP_EOL); $dbInfo['status'] = 'complete'; $this->saveDatabaseList($path, $databases, $version, $type); } @@ -482,10 +244,9 @@ public function processQueryList($courseList, $output, $path, $version, $dryRun, } /** - * * Reads a sql file and adds queries in the queryList array. * - * @param string $sqlFilePath + * @param string $sqlFilePath * @param OutputInterface $output * @param string type */ @@ -501,9 +262,9 @@ public function fillQueryList($sqlFilePath, $output, $type) } /** - * Setting the queryList array + * Setting the queryList array. * - * @param array $queryList + * @param array $queryList * @param string $section * @param string $type */ @@ -517,7 +278,8 @@ public function setQueryList($queryList, $section, $type) } /** - * Returns sections + * Returns sections. + * * @return array */ public function getSections() @@ -527,14 +289,15 @@ public function getSections() 'user', 'stats', 'scorm', - 'course' + 'course', ]; } /** - * Generates database array info + * Generates database array info. * * @param array $courseList + * * @return array */ public function generateDatabaseList($courseList) @@ -547,17 +310,17 @@ public function generateDatabaseList($courseList) $courseDbList[] = [ 'database' => '_chamilo_course_'.$course['db_name'], 'prefix' => $this->getTablePrefix($_configuration, $course['db_name']), - 'status' => 'waiting' + 'status' => 'waiting', ]; } } } else { $courseDbList = [ [ - 'database'=> 'main_database', + 'database' => 'main_database', 'status' => 'waiting', - 'prefix' => null - ] + 'prefix' => null, + ], ]; } @@ -565,30 +328,32 @@ public function generateDatabaseList($courseList) 'main' => [ [ 'database' => 'main_database', - 'status' => 'waiting' - ] + 'status' => 'waiting', + ], ], 'user' => [ [ 'database' => 'user_personal_database', - 'status' => 'waiting' - ] + 'status' => 'waiting', + ], ], 'stats' => [ [ 'database' => 'statistics_database', - 'status' => 'waiting' - ] + 'status' => 'waiting', + ], ], - 'course'=> $courseDbList + 'course' => $courseDbList, ]; $this->setDatabaseList($databaseSection); + return $this->databaseList; } /** - * Sets the database list + * Sets the database list. + * * @param array $list */ public function setDatabaseList($list) @@ -598,10 +363,10 @@ public function setDatabaseList($list) /** * @param OutputInterface $output - * @param array $courseList - * @param string $path - * @param string $version - * @param string $type + * @param array $courseList + * @param string $path + * @param string $version + * @param string $type * * @return mixed|void */ @@ -642,11 +407,11 @@ public function saveDatabaseList($path, $databaseSection, $version, $type) /** * @param OutputInterface $output - * @param array $courseList - * @param string $path - * @param string $section - * @param string $version - * @param string $type + * @param array $courseList + * @param string $path + * @param string $section + * @param string $version + * @param string $type * * @return mixed */ @@ -659,10 +424,10 @@ public function getDatabasesPerSection($output, $courseList, $path, $section, $v } /** - * Function originally wrote in install.lib.php + * Function originally wrote in install.lib.php. * - * @param string $file - * @param string $section + * @param string $file + * @param string $section * @param OutputInterface $output * * @return array|bool @@ -671,11 +436,13 @@ public function getSQLContents($file, $section, $output) { if (empty($file) || file_exists($file) == false) { $output->writeln(sprintf("File doesn't exist: '%s'... ", $file)); + return false; } if (!in_array($section, ['main', 'user', 'stats', 'scorm', 'course'])) { $output->writeln(sprintf("Section is %s not authorized in getSQLContents()", $section)); + return false; } @@ -683,6 +450,7 @@ public function getSQLContents($file, $section, $output) $fileContents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); if (!is_array($fileContents) or count($fileContents) < 1) { $output->writeln(sprintf("File '%s' looks empty in getSQLContents()", $file)); + return false; } @@ -718,15 +486,18 @@ public function getSQLContents($file, $section, $output) } /** - * Creates the course tables with the prefix c_ + * Creates the course tables with the prefix c_. + * * @param OutputInterface $output - * @param string $dryRun + * @param string $dryRun + * * @return int */ public function createCourseTables($output, $dryRun) { if ($dryRun) { $output->writeln("Creating c_* tables but dry-run is on. 0 table created."); + return 0; } @@ -738,9 +509,247 @@ public function createCourseTables($output, $dryRun) // Importing sql files. $arguments = [ 'command' => 'dbal:import', - 'file' => $sqlFolder.'db_course.sql' + 'file' => $sqlFolder.'db_course.sql', ]; $input = new ArrayInput($arguments); $command->run($input, $output); } + + protected function configure(): void + { + $this + ->setName('chash:chamilo_upgrade_database') + ->setDescription('Execute a chamilo migration to a specified version or the latest available version') + ->addArgument('from-version', InputArgument::REQUIRED, 'The version to migrate to', null) + ->addArgument('to-version', InputArgument::REQUIRED, 'The version to migrate to', null) + ->addArgument('host', null, InputArgument::REQUIRED, 'host', 'localhost') + ->addArgument('username', null, InputArgument::REQUIRED, 'username', 'root') + ->addArgument('password', null, InputArgument::REQUIRED, 'password', 'root') + ->addArgument('db_name', null, InputArgument::REQUIRED, 'Database name', '') + ->addArgument('root_sys', null, InputArgument::REQUIRED, 'Chamilo root_sys', '') + ; + } + + /** + * Executes a command via CLI. + * + * @return int|void|null + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + // sudo php /var/www/html/chash/chash.php chash:chamilo_upgrade 1.11.x --linux-user=jmontoya --linux-group=jmontoya + + $startTime = time(); + + // Arguments and options + $currentVersion = $originalVersion = $input->getArgument('from-version'); + $version = $input->getArgument('to-version'); + $host = $input->getArgument('host'); + $username = $input->getArgument('username'); + $password = $input->getArgument('password'); + $database = $input->getArgument('db_name'); + $rootSys = $input->getArgument('root_sys'); + + // Getting supported version number list + $versionNameList = $this->getVersionNumberList(); + $minVersion = $this->getMinVersionSupportedByInstall(); + $versionList = $this->availableVersions(); + + // Checking version. + if ($version != 'master') { + if (!in_array($version, $versionNameList)) { + $output->writeln("Version '$version' is not available."); + $output->writeln("Available versions: ".implode(', ', $versionNameList).""); + + return 0; + } + } + + if (version_compare($version, $minVersion, '<')) { + $output->writeln("Your Chamilo version is not supported! The minimun version is: $minVersion"); + $output->writeln("You want to upgrade from ".$version." to $minVersion"); + + return 0; + } + + $versionInfo = $this->getAvailableVersionInfo($version); + + if (empty($versionInfo)) { + $output->writeln("The current version ($version) is not supported"); + + return 0; + } + + if (isset($versionInfo['hook_to_doctrine_version']) && + isset($doctrineVersion) + ) { + if ($doctrineVersion == $versionInfo['hook_to_doctrine_version']) { + $output->writeln("You already have the latest version. Nothing to update! Doctrine version $doctrineVersion"); + + return 0; + } + } + + $this->writeCommandHeader($output, 'Welcome to the Chamilo upgrade process!'); + + $output->writeln('Migrating from Chamilo version: '.$currentVersion.' to version '.$version); + + // Upgrade always from a mysql driver + $databaseSettings = [ + 'driver' => 'pdo_mysql', + 'host' => $host, + 'dbname' => $database, + 'user' => $username, + 'password' => $password, + ]; + + // Setting DB access. + $this->setDatabaseSettings($databaseSettings); + + $extraDatabaseSettings = [ + 'single_database' => isset($_configuration['single_database']) ? $_configuration['single_database'] : false, + 'table_prefix' => isset($_configuration['table_prefix']) ? $_configuration['table_prefix'] : null, + 'db_glue' => isset($_configuration['db_glue']) ? $_configuration['db_glue'] : null, + 'db_prefix' => isset($_configuration['db_prefix']) ? $_configuration['db_prefix'] : null, + ]; + + $this->setExtraDatabaseSettings($extraDatabaseSettings); + $this->setDoctrineSettings($this->getHelperSet()); + $conn = $this->getConnection($input); + $conn->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); + + if ($conn) { + $output->writeln("Connection to the database established."); + } else { + $output->writeln("Can't connect to the DB with user:".$_configuration['db_user']).""; + + return 0; + } + + // Get course list + $query = "SELECT * FROM course"; + $result = $conn->executeQuery($query); + $courseList = $result->fetchAll(); + + $output->writeln("Current version: $currentVersion"); + $output->writeln("Latest version: $version"); + $oldVersion = $version; + + // Handle 1.10.x as 1.10.1000 + if ($currentVersion == '1.9.x') { + $currentVersion = '1.9.1000'; + } + + if ($currentVersion == '1.10.x') { + $currentVersion = '1.10.1000'; + } + + if ($currentVersion == '1.11.x') { + $currentVersion = '1.11.1000'; + } + + if ($version == '1.9.x') { + $version = '1.9.1000'; + } + + if ($version == '1.10.x') { + $version = '1.10.1000'; + } + + if ($version == '1.11.x') { + $version = '1.11.1000'; + } + + require_once $rootSys.'src/Chamilo/CoreBundle/Entity/SettingsCurrent.php'; + require_once $rootSys.'src/Chamilo/CoreBundle/Entity/SystemTemplate.php'; + require_once $rootSys.'src/Chamilo/CoreBundle/Entity/SettingsOptions.php'; + require_once $rootSys.'app/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php'; + require_once $rootSys.'main/inc/lib/api.lib.php'; + require_once $rootSys.'main/inc/lib/custom_pages.class.php'; + require_once $rootSys.'main/inc/lib/database.lib.php'; + + if (!is_dir($rootSys.'vendor')) { + $output->writeln("Execute composer update in your chamilo instance first. Then continue with the upgrade"); + + return 1; + } + + $versionsToRun = []; + foreach ($versionList as $versionItem => $versionInfo) { + if (version_compare($versionItem, $currentVersion, '>') && + version_compare($versionItem, $version, '<=') + ) { + $versionsToRun[$versionItem] = $versionInfo; + } + } + + foreach ($versionsToRun as $versionItem => $versionInfo) { + if (isset($versionInfo['require_update']) && $versionInfo['require_update'] == true) { + $output->writeln("----------------------------------------------------------------"); + $output->writeln("Starting migration from version: $currentVersion to version $versionItem "); + $output->writeln(""); + + // Greater than my current version. + $this->startMigration( + $courseList, + null, + $versionItem, + $dryRun = false, + $output, + $removeUnusedTables = false, + $input, + false, + $onlyUpdateDatabase = true, + $rootSys + ); + $currentVersion = $versionItem; + $output->writeln("End database migration"); + $output->writeln("----------------------------------------------------------------"); + } else { + $currentVersion = $versionItem; + $output->writeln("Skip migration from version: $currentVersion to version $versionItem "); + } + + /* + * If the migration from 1.9.x to 1.10.x is finished then we fix the IDs and extra fields + */ + if ($originalVersion === '1.9.x' && $currentVersion === '1.10.x') { + require_once $rootSys.'main/inc/lib/database.constants.inc.php'; + require_once $rootSys.'main/inc/lib/system/session.class.php'; + require_once $rootSys.'main/inc/lib/chamilo_session.class.php'; + require_once $rootSys.'main/inc/lib/api.lib.php'; + require_once $rootSys.'main/inc/lib/database.lib.php'; + require_once $rootSys.'main/inc/lib/custom_pages.class.php'; + require_once $rootSys.'main/install/install.lib.php'; + require_once $rootSys.'main/inc/lib/display.lib.php'; + //require_once $rootSys.'main/inc/lib/group_portal_manager.lib.php'; + require_once $rootSys.'main/inc/lib/model.lib.php'; + require_once $rootSys.'main/inc/lib/events.lib.php'; + require_once $rootSys.'main/inc/lib/extra_field.lib.php'; + require_once $rootSys.'main/inc/lib/extra_field_value.lib.php'; + require_once $rootSys.'main/inc/lib/urlmanager.lib.php'; + require_once $rootSys.'main/inc/lib/usermanager.lib.php'; + require_once $rootSys.'src/Chamilo/CoreBundle/Entity/ExtraField.php'; + require_once $rootSys.'src/Chamilo/CoreBundle/Entity/ExtraFieldOptions.php'; + + $em = $this->setDoctrineSettings($this->getHelperSet()); + fixIds($em); + } + } + + $output->writeln("Hurray!!! You just finished this migration. To check the current status of your platform, run chamilo:status"); + $endTime = time(); + $totalTimeInMinutes = round(($endTime - $startTime) / 60, 2); + $output->writeln("The script took $totalTimeInMinutes minutes to execute."); + } + + /** + * Get connection. + * + * @return \Doctrine\DBAL\Connection + */ + private function getConnection() + { + return $this->getHelper('db')->getConnection(); + } } diff --git a/src/Command/Installation/WipeCommand.php b/src/Command/Installation/WipeCommand.php index 1dc2d09..5672858 100644 --- a/src/Command/Installation/WipeCommand.php +++ b/src/Command/Installation/WipeCommand.php @@ -10,7 +10,7 @@ use Symfony\Component\Console\Question\ConfirmationQuestion; /** - * Class WipeCommand + * Class WipeCommand. */ class WipeCommand extends CommonCommand { @@ -23,12 +23,9 @@ protected function configure(): void } /** - * Executes a command via CLI + * Executes a command via CLI. * - * @param InputInterface $input - * @param OutputInterface $output - * - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -43,6 +40,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($configurationPath == false) { $output->writeln("A Chamilo installation was not detected. You can add a path: chamilo:wipe /var/www/chamilo "); + return 0; } else { $question = new ConfirmationQuestion( @@ -59,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $arguments = [ 'command' => 'files:drop_databases', - '--conf' => $configurationFilePath + '--conf' => $configurationFilePath, ]; $inputDrop = new ArrayInput($arguments); @@ -70,7 +68,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $arguments = [ 'command' => 'files:clean_temp_folder', - '--conf' => $configurationFilePath + '--conf' => $configurationFilePath, ]; $input = new ArrayInput($arguments); $command->run($input, $output); @@ -80,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $arguments = [ 'command' => 'files:clean_courses_files', - '--conf' => $configurationFilePath + '--conf' => $configurationFilePath, ]; $input = new ArrayInput($arguments); $command->run($input, $output); @@ -90,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $command = $this->getApplication()->find('files:clean_config_files'); $arguments = [ 'command' => 'files:clean_config_files', - '--conf' => $configurationFilePath + '--conf' => $configurationFilePath, ]; $input = new ArrayInput($arguments); $command->run($input, $output); diff --git a/src/Command/Translation/AddSubLanguageCommand.php b/src/Command/Translation/AddSubLanguageCommand.php index fcf9ebc..a4107f0 100644 --- a/src/Command/Translation/AddSubLanguageCommand.php +++ b/src/Command/Translation/AddSubLanguageCommand.php @@ -6,13 +6,13 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * Class AddSubLanguageCommand * Definition of the translation:add_sub_language command - * Does not support multi-url yet + * Does not support multi-url yet. + * * @package Chash\Command\Translation */ class AddSubLanguageCommand extends DatabaseCommand @@ -37,9 +37,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -63,6 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $count = $stmt->rowCount(); if ($count) { $output->writeln($lang.' already exists in the database. Pick another English name.'); + return null; } @@ -81,11 +80,13 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($count < 1) { $output->writeln("The parent language $parentQuoted does not exist. Please choose a valid parent."); + return null; } if (is_dir($_configuration['root_sys'].'main/lang/'.$lang)) { $output->writeln('The destination directory ('.$_configuration['root_sys'].'main/lang/'.$lang.') already exists. Please choose another sub-language name.'); + return null; } @@ -97,7 +98,7 @@ protected function execute(InputInterface $input, OutputInterface $output) 'isocode' => $parentData['isocode'], 'dokeos_folder' => $lang, 'available' => 0, - 'parent_id' => $parentData['id'] + 'parent_id' => $parentData['id'], ]); } catch (\PDOException $e) { $output->write('SQL error!'.PHP_EOL); @@ -112,6 +113,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln('The connection does not seem to be a valid PDO connection'); } + return null; } } diff --git a/src/Command/Translation/DisableLanguageCommand.php b/src/Command/Translation/DisableLanguageCommand.php index 5efb807..115ea43 100644 --- a/src/Command/Translation/DisableLanguageCommand.php +++ b/src/Command/Translation/DisableLanguageCommand.php @@ -6,13 +6,13 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * Class DisableLanguageCommand * Definition of the translation:disable command - * Disable a language. Does not support multi-url yet + * Disable a language. Does not support multi-url yet. + * * @package Chash\Command\Translation */ class DisableLanguageCommand extends DatabaseCommand @@ -32,9 +32,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -55,11 +53,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $num = $stmt->rowCount(); if ($num < 1) { $output->writeln($lang.' language not found in the database. Please make sure you use an existing language name.'); + return null; } $lr = $stmt->fetch(\PDO::FETCH_ASSOC); if ($lr['available'] == 0) { $output->writeln($lang.' language is already disabled. Nothing to do.'); + return null; } // Everything is OK so far, enable the language @@ -75,6 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln('The connection does not seem to be a valid PDO connection'); } + return null; } } diff --git a/src/Command/Translation/EnableLanguageCommand.php b/src/Command/Translation/EnableLanguageCommand.php index 364ee13..d13f7b9 100644 --- a/src/Command/Translation/EnableLanguageCommand.php +++ b/src/Command/Translation/EnableLanguageCommand.php @@ -6,7 +6,6 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -14,6 +13,7 @@ * Definition of the translation:enable command * Definition of command to enable a language. * Does not support multi-url yet. + * * @package Chash\Command\Translation */ class EnableLanguageCommand extends DatabaseCommand @@ -33,9 +33,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -56,11 +54,13 @@ protected function execute(InputInterface $input, OutputInterface $output) $num = $stmt->rowCount(); if ($num < 1) { $output->writeln($lang.' language not found in the database. Please make sure you use an existing language name.'); + return null; } $lr = $stmt->fetch(\PDO::FETCH_ASSOC); if ($lr['available'] == 1) { $output->writeln($lang.' language is already enabled. Nothing to do.'); + return null; } // Everything is OK so far, enable the language @@ -76,6 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln('The connection does not seem to be a valid PDO connection'); } + return null; } } diff --git a/src/Command/Translation/ExportLanguageCommand.php b/src/Command/Translation/ExportLanguageCommand.php index 9e27597..c6658f3 100644 --- a/src/Command/Translation/ExportLanguageCommand.php +++ b/src/Command/Translation/ExportLanguageCommand.php @@ -3,14 +3,13 @@ namespace Chash\Command\Translation; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** - * Class ExportLanguageCommand + * Class ExportLanguageCommand. + * * @package Chash\Command\Translation */ class ExportLanguageCommand extends DatabaseCommand @@ -35,15 +34,13 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); - $language = $input->getArgument('language'); + $language = $input->getArgument('language'); $tmpFolder = $input->getOption('tmp'); $_configuration = $this->getHelper('configuration')->getConfiguration(); @@ -127,7 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output) if ($langInfo) { $output->writeln("Creating translation package"); $fileName = $tmpFolder.$langInfo['english_name'].'.tar'; - $phar = new \PharData($fileName); + $phar = new \PharData($fileName); $phar->buildFromDirectory($langFolder); $phar->setMetadata($langInfo); diff --git a/src/Command/Translation/ImportLanguageCommand.php b/src/Command/Translation/ImportLanguageCommand.php index 4815b35..3bd78da 100644 --- a/src/Command/Translation/ImportLanguageCommand.php +++ b/src/Command/Translation/ImportLanguageCommand.php @@ -3,15 +3,14 @@ namespace Chash\Command\Translation; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; /** - * Class ImportLanguageCommand + * Class ImportLanguageCommand. + * * @package Chash\Command\Translation */ class ImportLanguageCommand extends DatabaseCommand @@ -30,9 +29,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { diff --git a/src/Command/Translation/ListLanguagesCommand.php b/src/Command/Translation/ListLanguagesCommand.php index ead41ae..8a3fc7c 100644 --- a/src/Command/Translation/ListLanguagesCommand.php +++ b/src/Command/Translation/ListLanguagesCommand.php @@ -6,13 +6,13 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * Class ListLanguagesCommand * Definition of the translation:list command - * Definition of command to list platform languages + * Definition of command to list platform languages. + * * @package Chash\Command\Translation */ class ListLanguagesCommand extends DatabaseCommand @@ -37,9 +37,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -106,6 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln($resultLine); } } + return null; } } diff --git a/src/Command/Translation/PlatformLanguageCommand.php b/src/Command/Translation/PlatformLanguageCommand.php index 145a600..bd2397f 100644 --- a/src/Command/Translation/PlatformLanguageCommand.php +++ b/src/Command/Translation/PlatformLanguageCommand.php @@ -2,23 +2,24 @@ /** * Definition of command to * change platform language - * Does not support multi-url yet + * Does not support multi-url yet. */ /** - * Necessary namespaces definitions and usage + * Necessary namespaces definitions and usage. */ + namespace Chash\Command\Translation; use Chash\Command\Common\DatabaseCommand; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** * Class PlatformLanguageCommand - * Definition of the translation:platform_language command + * Definition of the translation:platform_language command. + * * @package Chash\Command\Translation */ class PlatformLanguageCommand extends DatabaseCommand @@ -38,9 +39,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -75,6 +74,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } if (!in_array($lang, $languages)) { $output->writeln($lang.' must be available on your platform before you can set it as default'); + return null; } $lang = $conn->quote($lang); @@ -89,6 +89,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $output->writeln('Language set to '.$lang); } } + return null; } } diff --git a/src/Command/Translation/TermsPackageCommand.php b/src/Command/Translation/TermsPackageCommand.php index bbee93b..4771b52 100644 --- a/src/Command/Translation/TermsPackageCommand.php +++ b/src/Command/Translation/TermsPackageCommand.php @@ -10,7 +10,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * Class ExportLanguageCommand + * Class ExportLanguageCommand. * * TermsPackage command, made to simplify the live of * translators by providing them with a list of the 10,000 most used words in @@ -22,12 +22,13 @@ * in main/cron/lang/ * The present command serves only at the end of this process, to generate the * corresponding language packages in other languages than English + * * @package Chash\Command\Translation */ class TermsPackageCommand extends DatabaseCommand { /** - * Set the input variables and what will be shown in command helper + * Set the input variables and what will be shown in command helper. */ protected function configure(): void { @@ -66,16 +67,14 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { parent::execute($input, $output); - $source = $input->getArgument('source'); - $language = $input->getArgument('language'); + $source = $input->getArgument('source'); + $language = $input->getArgument('language'); $destination = $input->getArgument('dest'); $tgz = $input->getOption('tgz'); $allowNew = $input->getOption('new'); @@ -191,7 +190,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $w = file_put_contents($destination.$origLang.'/'.$file, $origFileLines); } $output->writeln('Written translation files for packaging in '.$destination.$language.'.'); - $output->writeln('Found '.$countVars.' variables, of which '.$countTranslatedVars.' were already translated (and '.($countVars-$countTranslatedVars).' are missing).'); + $output->writeln('Found '.$countVars.' variables, of which '.$countTranslatedVars.' were already translated (and '.($countVars - $countTranslatedVars).' are missing).'); $output->writeln('In words, there are '.$countWords.' words in total, of which only '.($countWords - $countTranslatedWords).' still need translating.'); if ($tgz) { $output->writeln('Compressing as .tar.gz...'); @@ -209,8 +208,10 @@ protected function execute(InputInterface $input, OutputInterface $output) /** * Gets all the variables in a language file as a hash - * This is a copy of the get_all_language_variable_in_file method in main/admin/sub_language.class.php + * This is a copy of the get_all_language_variable_in_file method in main/admin/sub_language.class.php. + * * @param string $file The asbolute path to the file from which to extract variables + * * @return array Named array of variable => translation */ private function _getLangVars($file) @@ -231,6 +232,7 @@ private function _getLangVars($file) $var = substr($var, 1); $res_list[$var] = $val; } + return $res_list; } } diff --git a/src/Command/User/AddUserCommand.php b/src/Command/User/AddUserCommand.php index 1e60bc3..774a15c 100644 --- a/src/Command/User/AddUserCommand.php +++ b/src/Command/User/AddUserCommand.php @@ -5,7 +5,6 @@ use Chash\Command\Common\DatabaseCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -14,7 +13,8 @@ */ /** * Class AddUserCommand - * Changes a user password to the one given + * Changes a user password to the one given. + * * @package Chash\Command\User */ class AddUserCommand extends DatabaseCommand @@ -64,9 +64,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return null|void + * @return void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -93,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $stmt->execute(); $un = $stmt->rowCount(); } catch (\PDOException $e) { - $output->write('SQL error!' . PHP_EOL); + $output->write('SQL error!'.PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } @@ -135,7 +133,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } // @TODO make UTC $time = date('Y-m-d h:i:s'); - $expiration = time()+(60*60*24*366*10); + $expiration = time() + (60 * 60 * 24 * 366 * 10); $timeExpiry = date('Y-m-d h:i:s', $expiration); $firstname = $conn->quote($firstname); $lastname = $conn->quote($lastname); @@ -184,7 +182,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $stmt->execute(); $newUserId = $conn->lastInsertId(); } catch (\PDOException $e) { - $output->write('SQL error!' . PHP_EOL); + $output->write('SQL error!'.PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } @@ -195,7 +193,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $stmt = $conn->prepare($uas); $stmt->execute(); } catch (\PDOException $e) { - $output->write('SQL error!' . PHP_EOL); + $output->write('SQL error!'.PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } } @@ -205,7 +203,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $stmt = $conn->prepare($uas); $stmt->execute(); } catch (\PDOException $e) { - $output->write('SQL error!' . PHP_EOL); + $output->write('SQL error!'.PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } $uas = "UPDATE user SET user_id = id WHERE id = $newUserId"; @@ -213,7 +211,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $stmt = $conn->prepare($uas); $stmt->execute(); } catch (\PDOException $e) { - $output->write('SQL error!' . PHP_EOL); + $output->write('SQL error!'.PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } } else { @@ -222,6 +220,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln('The connection does not seem to be a valid PDO connection'); } + return null; } } diff --git a/src/Command/User/ChangePassCommand.php b/src/Command/User/ChangePassCommand.php index ff94176..03ca9fd 100644 --- a/src/Command/User/ChangePassCommand.php +++ b/src/Command/User/ChangePassCommand.php @@ -5,7 +5,6 @@ use Chash\Command\Common\DatabaseCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -14,7 +13,8 @@ */ /** * Class ChangePassCommand - * Changes a user password to the one given + * Changes a user password to the one given. + * * @package Chash\Command\User */ class ChangePassCommand extends DatabaseCommand @@ -39,9 +39,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return null|void + * @return void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -96,6 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln('The connection does not seem to be a valid PDO connection'); } + return null; } } diff --git a/src/Command/User/DisableAdminsCommand.php b/src/Command/User/DisableAdminsCommand.php index 3ac8b34..f7b4c00 100644 --- a/src/Command/User/DisableAdminsCommand.php +++ b/src/Command/User/DisableAdminsCommand.php @@ -3,15 +3,14 @@ namespace Chash\Command\User; use Chash\Command\Common\DatabaseCommand; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Question\ConfirmationQuestion; /** * Class DisableAdminsCommand - * Remove the "admin" role from *ALL* users on all portals of this instance + * Remove the "admin" role from *ALL* users on all portals of this instance. + * * @package Chash\Command\User */ class DisableAdminsCommand extends DatabaseCommand @@ -26,9 +25,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -56,6 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } $output->writeln('All admins have been disabled. Use user:make-admin to add one back.'); } + return null; } } diff --git a/src/Command/User/MakeAdminCommand.php b/src/Command/User/MakeAdminCommand.php index 006c46c..b86e3c6 100644 --- a/src/Command/User/MakeAdminCommand.php +++ b/src/Command/User/MakeAdminCommand.php @@ -5,7 +5,6 @@ use Chash\Command\Common\DatabaseCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -13,7 +12,7 @@ * it for. */ /** - * Makes the given user an admin on the main portal + * Makes the given user an admin on the main portal. */ class MakeAdminCommand extends DatabaseCommand { @@ -76,6 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln('The connection does not seem to be a valid PDO connection'); } + return null; } } diff --git a/src/Command/User/ResetLoginCommand.php b/src/Command/User/ResetLoginCommand.php index 5c09df2..ae021f8 100644 --- a/src/Command/User/ResetLoginCommand.php +++ b/src/Command/User/ResetLoginCommand.php @@ -6,7 +6,6 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; /** @@ -16,7 +15,7 @@ /** * Class ResetLoginCommand * Returns a password reset link for the given username (user will receive - * an e-mail with new login + password) + * an e-mail with new login + password). */ class ResetLoginCommand extends ChamiloUserCommand { @@ -35,9 +34,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -67,6 +64,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } else { $output->writeln('The connection does not seem to be a valid PDO connection'); } + return null; } } diff --git a/src/Command/User/SetLanguageCommand.php b/src/Command/User/SetLanguageCommand.php index 2948454..8a942ea 100644 --- a/src/Command/User/SetLanguageCommand.php +++ b/src/Command/User/SetLanguageCommand.php @@ -36,9 +36,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -77,6 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } if (!in_array($lang, $languages)) { $output->writeln($lang.' must be available on your platform before you can use it'); + return null; } if (empty($username)) { @@ -105,6 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output) } } } + return null; } } diff --git a/src/Command/User/UsersPerUrlAccessCommand.php b/src/Command/User/UsersPerUrlAccessCommand.php index 7a229d7..c09aee9 100644 --- a/src/Command/User/UsersPerUrlAccessCommand.php +++ b/src/Command/User/UsersPerUrlAccessCommand.php @@ -8,7 +8,7 @@ /** * Class UsersPerUrlAccessCommand - * Changes the language for all platform users + * Changes the language for all platform users. * * Command functions meant to deal with what the user of this script is calling it for. */ @@ -25,9 +25,7 @@ protected function configure(): void } /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int|null|void + * @return int|void|null */ protected function execute(InputInterface $input, OutputInterface $output) { @@ -54,6 +52,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $table->setRows($usersPerUrl); $table->render($output); } + return null; } } diff --git a/src/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php b/src/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php index 58a3fca..5260ca4 100644 --- a/src/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php +++ b/src/DoctrineExtensions/DBAL/Types/UTCDateTimeType.php @@ -8,7 +8,7 @@ use Doctrine\DBAL\Types\DateTimeType; /** - * Save datetime values in UTC in the database + * Save datetime values in UTC in the database. * * @package Chamilo\CoreBundle\DoctrineExtensions\DBAL\Types */ diff --git a/src/Helpers/ConfigurationHelper.php b/src/Helpers/ConfigurationHelper.php index b11f84e..141f81a 100644 --- a/src/Helpers/ConfigurationHelper.php +++ b/src/Helpers/ConfigurationHelper.php @@ -2,13 +2,13 @@ namespace Chash\Helpers; -use Guzzle\Tests\Batch\ExceptionBufferingBatchTest; use Symfony\Component\Console\Helper\Helper; -use Symfony\Component\Yaml\Parser; use Symfony\Component\Finder\Finder; +use Symfony\Component\Yaml\Parser; /** - * Class ConfigurationHelper + * Class ConfigurationHelper. + * * @package Chash\Helpers */ class ConfigurationHelper extends Helper @@ -19,7 +19,7 @@ class ConfigurationHelper extends Helper protected $isLegacy = true; /** - * Constructor + * Constructor. */ public function __construct() { @@ -58,7 +58,8 @@ public function setDryRun($dryRun) } /** - * Get chamilo versions + * Get chamilo versions. + * * @return array */ public function chamiloVersions() @@ -77,7 +78,7 @@ public function chamiloVersions() '1.9.8', '1.10.0', '1.11.0', - '2.0.0' + '2.0.0', ]; return $versionList; @@ -89,8 +90,10 @@ public function chamiloVersions() * $configurationPath = $this->getConfigurationPath('/var/www/chamilo'); * // $configurationPath value is '/var/www/chamilo/app/config/'; or * // $configurationPath value is '/var/www/chamilo/inc/conf/'; or - * + * . + * * @param string $path the path of the Chamilo installation + * * @return bool|string @example /var/www/chamilo/app/config/configuration.php */ public function getConfigurationPath($path = null) @@ -106,6 +109,7 @@ public function getConfigurationPath($path = null) !is_file($chamiloPath.'/main/inc/local.inc.php') ) { $this->isLegacy = false; + return realpath($chamiloPath.'/config/').'/'; } @@ -129,9 +133,11 @@ public function getConfigurationPath($path = null) * * $newConfigurationPath = $this->getNewConfigurationPath('/var/www/chamilo'); * // $newConfigurationPath value is '/var/www/chamilo/app/config/configuration.php'; - * + * . + * * @param string $path the path of the Chamilo installation - * @return bool|string @example /var/www/chamilo/app/config/configuration.php + * + * @return bool|string @example /var/www/chamilo/app/config/configuration.php */ public function getNewConfigurationPath($path = null) { @@ -155,8 +161,10 @@ public function getNewConfigurationPath($path = null) } /** - * Converts /var/www/chamilo/main/inc/conf to /var/www/chamilo/app/config + * Converts /var/www/chamilo/main/inc/conf to /var/www/chamilo/app/config. + * * @param string $path + * * @return string new path */ public function convertOldConfigurationPathToNewPath($path) @@ -170,10 +178,11 @@ public function convertOldConfigurationPathToNewPath($path) * $configurationPath = $this->getConfigurationFilePath('/var/www/chamilo') * // $configurationPath value is '/var/www/chamilo/config/configuration.php'; or * // $configurationPath value is '/var/www/chamilo/main/inc/conf/configuration.php'; - * + * . + * * @param string $path the path of the Chamilo installation - * @return bool|string returns * + * @return bool|string returns */ public function getConfigurationFilePath($path = null) { @@ -191,13 +200,13 @@ public function getConfigurationFilePath($path = null) return false; } - /** * Returns the $configuration array * * $configuration = $this->getConfiguration('/var/www/chamilo'); * // $configuration contains the $_configuration array - * + * . + * * @param string $path * * @return array|bool|mixed @@ -215,12 +224,13 @@ public function getConfiguration($path = null) } /** - * * * $sysPath = $this->getSysPathFromConfigurationFile('/var/www/chamilo/app/config/configuration.php'); * // $sysPath is '/var/www/chamilo/' - * + * . + * * @param string $configurationFile + * * @return string */ public function getSysPathFromConfigurationFile($configurationFile) @@ -251,9 +261,10 @@ public function getSysPathFromConfigurationFile($configurationFile) /** * Reads the Chamilo configuration file and returns the $_configuration array - * Merges the configuration.php with the configuration.yml if it exists + * Merges the configuration.php with the configuration.yml if it exists. * * @param string $configurationFile + * * @return array|bool|mixed */ public function readConfigurationFile($configurationFile = null) @@ -275,6 +286,7 @@ public function readConfigurationFile($configurationFile = null) if (isset($userPasswordCrypted)) { $_configuration['password_encryption'] = $userPasswordCrypted; } + return $_configuration; } break; @@ -297,7 +309,8 @@ public function readConfigurationFile($configurationFile = null) } /** - * Sets the configuration variable + * Sets the configuration variable. + * * @param $configuration */ public function setConfiguration($configuration) @@ -306,7 +319,8 @@ public function setConfiguration($configuration) } /** - * Get chamilo config files + * Get chamilo config files. + * * @return Finder */ public function getConfigFiles() @@ -321,7 +335,7 @@ public function getConfigFiles() 'events.conf.php', 'mail.conf.php', 'portfolio.conf.php', - 'profile.conf.php' + 'profile.conf.php', ]; if (is_dir($sysPath.'main/inc/conf')) { @@ -382,8 +396,10 @@ public function getCoursesFiles() } /** - * Gets the documents and folders marked DELETED - * @param array $courseDirs If not null, restrict the search to only those directories + * Gets the documents and folders marked DELETED. + * + * @param array $courseDirs If not null, restrict the search to only those directories + * * @return Finder */ public function getDeletedDocuments($courseDirs = []) @@ -392,24 +408,24 @@ public function getDeletedDocuments($courseDirs = []) $sysPath = $this->getSysPath(); if (empty($courseDirs) || !is_array($courseDirs)) { - if (is_dir($sysPath . 'courses')) { - $finder->in($sysPath . 'courses/')->name('*DELETED*'); + if (is_dir($sysPath.'courses')) { + $finder->in($sysPath.'courses/')->name('*DELETED*'); } - if (is_dir($sysPath . 'app/courses')) { - $finder->in($sysPath . 'app/courses/')->name('*DELETED*'); + if (is_dir($sysPath.'app/courses')) { + $finder->in($sysPath.'app/courses/')->name('*DELETED*'); } } else { - $course = is_dir($sysPath . 'courses'); - $appCourse = is_dir($sysPath . 'app/courses'); + $course = is_dir($sysPath.'courses'); + $appCourse = is_dir($sysPath.'app/courses'); foreach ($courseDirs as $dir) { if ($course) { - $finder->in($sysPath . 'courses/' . $dir .'/')->name('*DELETED*'); + $finder->in($sysPath.'courses/'.$dir.'/')->name('*DELETED*'); } if ($appCourse) { - $finder->in($sysPath . 'app/courses/' . $dir . '/')->name('*DELETED*'); + $finder->in($sysPath.'app/courses/'.$dir.'/')->name('*DELETED*'); } } } @@ -493,7 +509,8 @@ public function getConfigFolders() } /** - * Lists the directories in the archive/ or app/cache directory (depends on Chamilo version) + * Lists the directories in the archive/ or app/cache directory (depends on Chamilo version). + * * @return Finder */ public function getTempFolders() @@ -517,7 +534,8 @@ public function getTempFolders() } /** - * Lists the files in the archive/ or app/cache directory (depends on Chamilo version) + * Lists the files in the archive/ or app/cache directory (depends on Chamilo version). + * * @return bool|Finder */ public function getTempFiles() @@ -543,7 +561,6 @@ public function getTempFiles() $filesAdded = true; } - if ($filesAdded) { return $finder; } @@ -552,29 +569,33 @@ public function getTempFiles() } /** - * Lists the lib folder + * Lists the lib folder. + * * @return Finder */ public function getLibFolder() { $sysPath = $this->getSysPath(); - if (is_dir($sysPath . 'main/inc/lib/')) { - return $sysPath . 'main/inc/lib/'; + if (is_dir($sysPath.'main/inc/lib/')) { + return $sysPath.'main/inc/lib/'; } return false; } + /** - * Lists the files in the main/inc/conf + * Lists the files in the main/inc/conf. + * * @param fileName + * * @return Finder */ public function getConfFile($fileName) { $finder = new Finder(); $sysPath = $this->getSysPath(); - if (is_dir($sysPath . 'main/inc/conf')) { - $finder->files()->name($fileName)->in($sysPath . 'main/inc/conf'); + if (is_dir($sysPath.'main/inc/conf')) { + $finder->files()->name($fileName)->in($sysPath.'main/inc/conf'); } foreach ($finder as $file) { @@ -583,17 +604,20 @@ public function getConfFile($fileName) return false; } + /** - * Lists the files in the main/inc/lib + * Lists the files in the main/inc/lib. + * * @param fileName + * * @return Finder */ public function getLibFile($fileName) { $finder = new Finder(); $sysPath = $this->getSysPath(); - if (is_dir($sysPath . 'main/inc/lib')) { - $finder->files()->name($fileName)->in($sysPath . 'main/inc/lib'); + if (is_dir($sysPath.'main/inc/lib')) { + $finder->files()->name($fileName)->in($sysPath.'main/inc/lib'); } foreach ($finder as $file) { @@ -604,7 +628,8 @@ public function getLibFile($fileName) } /** - * Sets the system's root path (e.g. /var/www/chamilo/) + * Sets the system's root path (e.g. /var/www/chamilo/). + * * @param $sysPath */ public function setSysPath($sysPath) @@ -684,8 +709,10 @@ public function getSysPath() } /** - * Gets an array with all the databases (particularly useful for Chamilo <1.9) + * Gets an array with all the databases (particularly useful for Chamilo <1.9). + * * @todo use connection instead of mysql_* + * * @return mixed Array of databases */ public function getAllDatabases() @@ -744,8 +771,9 @@ public function getName() } /** - * Gets the current install's major version. Requires getConfiguration() to be called first - * @return string The major version (two-parts version number, e.g. "1.9") + * Gets the current install's major version. Requires getConfiguration() to be called first. + * + * @return string The major version (two-parts version number, e.g. "1.9") */ public function getMajorVersion() { @@ -753,6 +781,7 @@ public function getMajorVersion() $this->getConfiguration(); } list($first, $second) = preg_split('/\./', $this->configuration['system_version']); + return $first.'.'.$second; } } diff --git a/src/Migrations/Version10.php b/src/Migrations/Version10.php index 993fc25..826421e 100644 --- a/src/Migrations/Version10.php +++ b/src/Migrations/Version10.php @@ -6,7 +6,8 @@ use Doctrine\Migrations\AbstractMigration; /** - * Manages the migration to Chamilo 10 + * Manages the migration to Chamilo 10. + * * @package ChamiloLMS\Controller\Migrations */ class Version10 extends AbstractMigration @@ -20,8 +21,7 @@ public function setContainer(ContainerInterface $container = null) }*/ /** - * Chamilo upgrade - * @param Schema $schema + * Chamilo upgrade. */ public function up(Schema $schema): void { @@ -60,7 +60,6 @@ public function up(Schema $schema): void $this->addSql("CREATE TABLE IF NOT EXISTS access_url_rel_usergroup (access_url_id int unsigned NOT NULL, usergroup_id int unsigned NOT NULL, PRIMARY KEY (access_url_id, usergroup_id))"); $this->addSql("CREATE TABLE IF NOT EXISTS access_url_rel_course_category (access_url_id int unsigned NOT NULL, course_category_id int unsigned NOT NULL, PRIMARY KEY (access_url_id, course_category_id))"); - $this->addSql("ALTER TABLE c_student_publication ADD COLUMN filename varchar(255) DEFAULT NULL"); $this->addSql("ALTER TABLE course ADD COLUMN add_teachers_to_sessions_courses tinyint NOT NULL default 0"); $this->addSql("ALTER TABLE c_quiz ADD COLUMN on_success_message longtext default ''"); @@ -383,8 +382,7 @@ public function up(Schema $schema): void } /** - * Chamilo downgrade - * @param Schema $schema + * Chamilo downgrade. */ public function down(Schema $schema): void { @@ -393,16 +391,10 @@ public function down(Schema $schema): void ); } - /** - * @param Schema $schema - */ public function preUp(Schema $schema): void { } - /** - * @param Schema $schema - */ public function postUp(Schema $schema): void { $this->addSql("DROP TABLE IF EXISTS track_c_referers"); @@ -433,7 +425,6 @@ public function postUp(Schema $schema): void -- ALTER TABLE session DROP COLUMN date_start; -- ALTER TABLE session DROP COLUMN date_end;*/ - $this->addSql("DELETE FROM settings_current WHERE variable = 'session_tutor_reports_visibility'"); $this->addSql("DELETE FROM settings_options WHERE variable = 'session_tutor_reports_visibility'"); $this->addSql("DELETE FROM settings_current WHERE variable = 'gradebook_show_percentage_in_reports'"); @@ -559,16 +550,10 @@ public function postUp(Schema $schema): void ); } - /** - * @param Schema $schema - */ public function preDown(Schema $schema): void { } - /** - * @param Schema $schema - */ public function postDown(Schema $schema): void { } diff --git a/src/Migrations/Version8.php b/src/Migrations/Version8.php index 2eed9e7..ae0a273 100644 --- a/src/Migrations/Version8.php +++ b/src/Migrations/Version8.php @@ -2,18 +2,16 @@ namespace Chash\Migrations; -use Doctrine\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; /** - * Manages the migration to version 1.8.0 + * Manages the migration to version 1.8.0. + * * @package ChamiloLMS\Controller\Migrations */ class Version8 extends AbstractMigration { - /** - * @param Schema $schema - */ public function up(Schema $schema): void { $sql = 'UPDATE settings_current SET selected_value = "1.8.8.14911" @@ -21,9 +19,6 @@ public function up(Schema $schema): void $this->addSql($sql); } - /** - * @param Schema $schema - */ public function down(Schema $schema): void { $sql = 'UPDATE settings_current SET selected_value = "1.8.7" diff --git a/src/Migrations/Version9.php b/src/Migrations/Version9.php index 448c42c..0360a7a 100644 --- a/src/Migrations/Version9.php +++ b/src/Migrations/Version9.php @@ -2,18 +2,16 @@ namespace Chash\Migrations; -use Doctrine\Migrations\AbstractMigration; use Doctrine\DBAL\Schema\Schema; +use Doctrine\Migrations\AbstractMigration; /** - * Manages the migration to version 1.9.0 + * Manages the migration to version 1.9.0. + * * @package ChamiloLMS\Controller\Migrations */ class Version9 extends AbstractMigration { - /** - * @param Schema $schema - */ public function up(Schema $schema): void { $sql = 'UPDATE settings_current SET selected_value = "1.9.0.18715" @@ -21,9 +19,6 @@ public function up(Schema $schema): void $this->addSql($sql); } - /** - * @param Schema $schema - */ public function down(Schema $schema): void { $sql = 'UPDATE settings_current SET selected_value = "1.8.8.14911" diff --git a/src/Resources/Database/1.10.0/update.php b/src/Resources/Database/1.10.0/update.php index 02c033e..e5db15c 100644 --- a/src/Resources/Database/1.10.0/update.php +++ b/src/Resources/Database/1.10.0/update.php @@ -138,12 +138,12 @@ $list = scandir($langPath); foreach ($list as $entry) { - if (is_dir($langPath . $entry) && + if (is_dir($langPath.$entry) && in_array($entry, $officialLanguages) ) { foreach ($filesToDelete as $file) { - if (is_file($langPath . $entry . '/' . $file . '.inc.php')) { - unlink($langPath . $entry . '/' . $file . '.inc.php'); + if (is_file($langPath.$entry.'/'.$file.'.inc.php')) { + unlink($langPath.$entry.'/'.$file.'.inc.php'); } } } @@ -171,12 +171,12 @@ // Move dirs into new structures. $movePathList = [ - $sysCodePath.'upload/users/groups' => $sysPath . 'app/upload/groups', - $sysCodePath.'upload/users' => $sysPath . 'app/upload/users', - $sysCodePath.'upload/badges' => $sysPath . 'app/upload/badges', - $sysPath.'courses' => $sysPath . 'app/courses', - $sysPath.'searchdb' => $sysPath . 'app/upload/plugins/xapian/', - $sysPath.'home' => $sysPath . 'app/home' + $sysCodePath.'upload/users/groups' => $sysPath.'app/upload/groups', + $sysCodePath.'upload/users' => $sysPath.'app/upload/users', + $sysCodePath.'upload/badges' => $sysPath.'app/upload/badges', + $sysPath.'courses' => $sysPath.'app/courses', + $sysPath.'searchdb' => $sysPath.'app/upload/plugins/xapian/', + $sysPath.'home' => $sysPath.'app/home', ]; $output->writeln('Moving folders'); diff --git a/src/Resources/Database/1.11.0/update.php b/src/Resources/Database/1.11.0/update.php index 11024cb..b050709 100644 --- a/src/Resources/Database/1.11.0/update.php +++ b/src/Resources/Database/1.11.0/update.php @@ -1,9 +1,8 @@ writeln("Remove $exercisePath"); $fs->remove($exercisePath); } // Same with main/newscorm, renamed main/lp - $lpPath = $sysCodePath . 'newscorm'; + $lpPath = $sysCodePath.'newscorm'; if (is_dir($lpPath)) { $output->writeln("Remove $lpPath"); $fs->remove($lpPath); } - $ticketPluginPath = $sysPath . 'plugin/ticket'; + $ticketPluginPath = $sysPath.'plugin/ticket'; if (is_dir($ticketPluginPath)) { $output->writeln("Remove $ticketPluginPath"); $fs->remove($ticketPluginPath); diff --git a/src/Resources/Database/1.8.8/update-db-1.8.7-1.8.8.inc.php b/src/Resources/Database/1.8.8/update-db-1.8.7-1.8.8.inc.php index 0749240..13c00e4 100644 --- a/src/Resources/Database/1.8.8/update-db-1.8.7-1.8.8.inc.php +++ b/src/Resources/Database/1.8.8/update-db-1.8.7-1.8.8.inc.php @@ -163,7 +163,6 @@ $result = $mainConnection->executeQuery($sql); $rows = $result->fetchAll(); foreach ($rows as $row) { - // Adding course to default URL just in case. // Check if already exists $sql = "SELECT course_code FROM access_url_rel_course diff --git a/src/Resources/Database/1.9.0/update-db-1.8.8-1.9.0.inc.php b/src/Resources/Database/1.9.0/update-db-1.8.8-1.9.0.inc.php index 5e93166..cc678f5 100644 --- a/src/Resources/Database/1.9.0/update-db-1.8.8-1.9.0.inc.php +++ b/src/Resources/Database/1.9.0/update-db-1.8.8-1.9.0.inc.php @@ -22,7 +22,6 @@ $mainConnection->beginTransaction(); try { - // Checking if option "students_download_folders" exists see BT#7678& $output->writeln("Checking option 'students_download_folders'"); $sql = "SELECT selected_value FROM settings_current @@ -42,7 +41,7 @@ 'comment' => 'AllowStudentsDownloadFoldersComment', 'scope' => '', 'subkeytext' => '', - 'access_url_changeable' => '0' + 'access_url_changeable' => '0', ]; $mainConnection->insert('settings_current', $params); @@ -50,14 +49,14 @@ $params = [ 'variable' => 'students_download_folders', 'value' => 'false', - 'display_text' => 'No' + 'display_text' => 'No', ]; $mainConnection->insert('settings_options', $params); $params = [ 'variable' => 'students_download_folders', 'value' => 'true', - 'display_text' => 'Yes' + 'display_text' => 'Yes', ]; $mainConnection->insert('settings_options', $params); @@ -72,7 +71,7 @@ $output->writeln($sql); $result = $result->fetch(); - $session_mode = $result['selected_value']; + $session_mode = $result['selected_value']; $output->writeln("Session mode: $session_mode"); if ($session_mode == 'true') { @@ -103,7 +102,7 @@ if (is_numeric($new_user_group_id)) { $mapping_classes[$old_id] = $new_user_group_id; - $classes_added ++; + $classes_added++; } } $output->writeln("Classes added: $classes_added"); @@ -124,7 +123,7 @@ } $values = [ 'usergroup_id' => $mapping_classes[$row['class_id']], - 'user_id' => $row['user_id'] + 'user_id' => $row['user_id'], ]; if ($dryRun) { @@ -150,7 +149,7 @@ $subResult = $mainConnection->executeQuery($sql_course); $courseInfo = $subResult->fetch(); - $course_id = $courseInfo['id']; + $course_id = $courseInfo['id']; if (empty($mapping_classes[$row['class_id']])) { // Cover a special case where data would not be // consistent - see BT#7254 @@ -159,7 +158,7 @@ } $values = [ 'usergroup_id' => $mapping_classes[$row['class_id']], - 'course_id' => $course_id + 'course_id' => $course_id, ]; if ($dryRun) { @@ -218,7 +217,7 @@ "personal_agenda", "personal_agenda_repeat", "personal_agenda_repeat_not", - "user_course_category" + "user_course_category", ]; $userSchemaManager = $userConnection->getSchemaManager(); @@ -351,7 +350,7 @@ 'wiki', 'wiki_conf', 'wiki_discuss', - 'wiki_mailcue' + 'wiki_mailcue', ]; $output->writeln(''); @@ -402,7 +401,6 @@ if ($dryRun) { $id = 1; } else { - // Fixing data switch ($table) { case 'forum_notification': @@ -516,8 +514,8 @@ foreach ($work_list as $work) { $session_id = intval($work['session_id']); - $group_id = intval($work['post_group_id']); - $work_key = $session_id.$group_id; + $group_id = intval($work['post_group_id']); + $work_key = $session_id.$group_id; $dir_name = "default_tasks_".$group_id."_".$session_id; @@ -539,7 +537,7 @@ qualificator_id = '', user_id = '".$user_id."'"; $mainConnection->executeQuery($sql); - $id = $mainConnection->lastInsertId(); + $id = $mainConnection->lastInsertId(); //2.2 Adding the folder in item property if ($id) { @@ -618,7 +616,7 @@ function check_work($mainConnection, $folder_id, $work_url, $work_table, $base_w { $uniq_id = uniqid(); // Looking for subfolders - $sql = "SELECT * FROM $work_table WHERE parent_id = $folder_id AND filetype ='folder' AND c_id = $courseId"; + $sql = "SELECT * FROM $work_table WHERE parent_id = $folder_id AND filetype ='folder' AND c_id = $courseId"; $result = $mainConnection->executeQuery($sql); $rows = $result->fetchAll(); @@ -638,7 +636,7 @@ function check_work($mainConnection, $folder_id, $work_url, $work_table, $base_w rename($base_work_dir.$work_url, $base_work_dir.$new_url); //Rename all files inside the folder - $sql = "SELECT * FROM $work_table WHERE parent_id = $folder_id AND filetype ='file' AND c_id = $courseId"; + $sql = "SELECT * FROM $work_table WHERE parent_id = $folder_id AND filetype ='file' AND c_id = $courseId"; $result = $mainConnection->executeQuery($sql); $rows = $result->fetchAll(); @@ -657,7 +655,8 @@ function check_work($mainConnection, $folder_id, $work_url, $work_table, $base_w /** * @param string $base_work_dir * @param string $desired_dir_name - * @param array $portalSettings + * @param array $portalSettings + * * @return bool|string */ function create_unexisting_work_directory($base_work_dir, $desired_dir_name, $portalSettings) @@ -665,7 +664,7 @@ function create_unexisting_work_directory($base_work_dir, $desired_dir_name, $po $nb = ''; $base_work_dir = (substr($base_work_dir, -1, 1) == '/' ? $base_work_dir : $base_work_dir.'/'); while (file_exists($base_work_dir.$desired_dir_name.$nb)) { - $nb += 1; + $nb++; } if (mkdir($base_work_dir.$desired_dir_name.$nb, $portalSettings['permissions_for_new_directories'])) { return $desired_dir_name.$nb;