From d8c2d3087aa228222562db4eacab0ef82df12b89 Mon Sep 17 00:00:00 2001 From: Nicola Galgano Date: Sun, 12 Apr 2020 13:18:39 +0200 Subject: [PATCH 01/32] CheckJoomlaUpdatesCommand --- .../src/Console/CheckJoomlaUpdatesCommand.php | 148 ++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 libraries/src/Console/CheckJoomlaUpdatesCommand.php diff --git a/libraries/src/Console/CheckJoomlaUpdatesCommand.php b/libraries/src/Console/CheckJoomlaUpdatesCommand.php new file mode 100644 index 0000000000000..1131d5d866ae6 --- /dev/null +++ b/libraries/src/Console/CheckJoomlaUpdatesCommand.php @@ -0,0 +1,148 @@ +%command.name% Checks for Joomla updates. + + php %command.full_name% +EOF; + $this->setDescription('Checks for Joomla updates'); + $this->setHelp($help); + } + + /** + * Retrieves Update Information + * + * @return mixed + * + * @since 4.0 + */ + private function getUpdateInformationFromModel() + { + $app = $this->getApplication(); + $updatemodel = $app->bootComponent('com_joomlaupdate')->getMVCFactory($app)->createModel('Update', 'Administrator'); + $updatemodel->purge(); + $updatemodel->refreshUpdates(true); + + return $updatemodel; + } + + /** + * Gets the Update Information + * + * @return mixed + * + * @since 4.0 + */ + public function getUpdateInfo() + { + if (!$this->updateInfo) + { + $this->setUpdateInfo(); + } + + return $this->updateInfo; + } + + /** + * Sets the Update Information + * + * @param null $info stores update Information + * + * @return void + * + * @since 4.0 + */ + public function setUpdateInfo($info = null) + { + if (!$info) + { + $this->updateInfo = $this->getUpdateInformationFromModel(); + } + else + { + $this->updateInfo = $info; + } + } + + /** + * Internal function to execute the command. + * + * @param InputInterface $input The input to inject into the command. + * @param OutputInterface $output The output to inject into the command. + * + * @return integer The command exit code + * + * @since __DEPLOY_VERSION__ + */ + protected function doExecute(InputInterface $input, OutputInterface $output): int + { + $symfonyStyle = new SymfonyStyle($input, $output); + + $model = $this->getUpdateInfo(); + $data = $model->getUpdateInformation(); + $symfonyStyle->title('Joomla! Updates'); + + if (!$data['hasUpdate']) + { + $symfonyStyle->success('You already have the latest Joomla version ' . $data['latest']); + + return 0; + } + + $symfonyStyle->note('New Joomla Version ' . $data['latest'] . ' is available.'); + + if (!isset($data['object']->downloadurl->_data)) + { + $symfonyStyle->warning('We cannot find an update URL'); + } + + return 0; + } +} From 91d47ef061c257a1a28fdf86d9e013c18df4cf77 Mon Sep 17 00:00:00 2001 From: Nicola Galgano Date: Sun, 12 Apr 2020 13:21:14 +0200 Subject: [PATCH 02/32] SetConfigurationCommand --- .../src/Console/SetConfigurationCommand.php | 503 ++++++++++++++++++ 1 file changed, 503 insertions(+) create mode 100644 libraries/src/Console/SetConfigurationCommand.php diff --git a/libraries/src/Console/SetConfigurationCommand.php b/libraries/src/Console/SetConfigurationCommand.php new file mode 100644 index 0000000000000..db0b7543d1aec --- /dev/null +++ b/libraries/src/Console/SetConfigurationCommand.php @@ -0,0 +1,503 @@ +load('', JPATH_INSTALLATION, null, false, false) || + $language->load('', JPATH_INSTALLATION, null, true); + $this->cliInput = $input; + $this->ioStyle = new SymfonyStyle($input, $output); + } + + + /** + * Collects options from user input + * + * @param array $options Options inputed by users + * + * @return array + * + * @since 4.0 + */ + public function retrieveOptionsFromInput($options) + { + $collected = []; + + foreach ($options as $option) + { + if (strpos($option, '=') === false) + { + $this->ioStyle + ->error('Options and values should be separated by "="'); + exit; + } + + list($option, $value) = explode('=', $option); + + $collected[$option] = $value; + } + + return $collected; + } + + + /** + * Validates the options provided + * + * @param array $options Options Array + * + * @return mixed + * + * @since 4.0 + */ + public function validateOptions($options) + { + $config = $this->getInitialConfigurationOptions(); + + $configs = $config->toArray(); + + $valid = true; + array_walk( + $options, function ($value, $key) use ($configs, &$valid) { + if (!array_key_exists($key, $configs)) + { + $this->getApplication() + ->enqueueMessage( + "Can't find option *$key* in configuration list", + 'error' + ); + + $valid = false; + } + } + ); + + return $valid; + } + + /** + * Sets the options array + * + * @param string $options Options string + * + * @since 4.0 + * + * @return void + */ + public function setOptions($options) + { + $this->options = explode(' ', $options); + } + + /** + * Collects the options array + * + * @return array|mixed + * + * @since 4.0 + */ + public function getOptions() + { + if ($this->options) + { + return $this->options; + } + + return $this->cliInput->getArgument('options'); + } + + /** + * Returns Default configuration Object + * + * @return Registry + * + * @since 4.0 + */ + public function getInitialConfigurationOptions(): Registry + { + return (new Registry(new \JConfig)); + } + + + /** + * Save the configuration file + * + * @param array $options Collected options + * + * @return boolean + * + * @since 4.0 + */ + public function saveConfiguration($options) + { + $config = $this->getInitialConfigurationOptions(); + + foreach ($options as $key => $value) + { + $value = $value === 'false' ? false : $value; + $value = $value === 'true' ? true : $value; + + $config->set($key, $value); + } + + $config->remove('cwd'); + $config->remove('execution'); + $buffer = $config->toString( + 'PHP', + array('class' => 'JConfig', 'closingtag' => false) + ); + + $path = JPATH_CONFIGURATION . '/configuration.php'; + + if ($this->writeFile($buffer, $path)) + { + return true; + } + + return false; + } + + /** + * Initialise the command. + * + * @return void + * + * @since 4.0 + */ + protected function configure(): void + { + $this->setDescription('Sets a value for a configuration option'); + + $this->addArgument( + 'options', + InputArgument::REQUIRED | InputArgument::IS_ARRAY, + 'All options you want to set' + ); + + $help = "The %command.name% + Sets a value for a configuration option + \nUsage: php %command.full_name%