2015-04-08 04:53:03 -04:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2015-06-25 05:43:55 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-06-25 05:43:55 -04:00
|
|
|
*/
|
2015-04-08 04:53:03 -04:00
|
|
|
namespace OC\Console;
|
|
|
|
|
|
2023-03-13 06:05:52 -04:00
|
|
|
use ArgumentCountError;
|
2018-08-04 15:53:50 -04:00
|
|
|
use OC\MemoryInfo;
|
2016-10-20 11:06:10 -04:00
|
|
|
use OC\NeedsUpdateException;
|
2023-03-13 06:05:52 -04:00
|
|
|
use OC\SystemConfig;
|
2024-03-06 05:54:58 -05:00
|
|
|
use OCP\App\AppPathNotFoundException;
|
2022-05-12 11:08:54 -04:00
|
|
|
use OCP\App\IAppManager;
|
2016-02-05 06:24:54 -05:00
|
|
|
use OCP\Console\ConsoleEvent;
|
2023-03-13 06:05:52 -04:00
|
|
|
use OCP\Defaults;
|
2023-06-30 06:50:28 -04:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2015-04-08 04:53:03 -04:00
|
|
|
use OCP\IConfig;
|
2016-02-05 06:24:54 -05:00
|
|
|
use OCP\IRequest;
|
2023-03-13 06:05:52 -04:00
|
|
|
use OCP\Server;
|
2024-09-17 16:38:44 -04:00
|
|
|
use OCP\ServerVersion;
|
2023-06-06 05:09:24 -04:00
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
2021-04-16 08:26:43 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-04-08 04:53:03 -04:00
|
|
|
use Symfony\Component\Console\Application as SymfonyApplication;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2016-02-17 18:42:00 -05:00
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
2018-07-01 14:54:19 -04:00
|
|
|
use Symfony\Component\Console\Output\ConsoleOutputInterface;
|
2015-04-08 04:53:03 -04:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
|
|
class Application {
|
2023-01-16 08:55:16 -05:00
|
|
|
private SymfonyApplication $application;
|
2015-04-08 04:53:03 -04:00
|
|
|
|
2024-03-06 05:54:58 -05:00
|
|
|
public function __construct(
|
2024-09-17 16:38:44 -04:00
|
|
|
ServerVersion $serverVersion,
|
2024-03-06 05:54:58 -05:00
|
|
|
private IConfig $config,
|
|
|
|
|
private IEventDispatcher $dispatcher,
|
|
|
|
|
private IRequest $request,
|
|
|
|
|
private LoggerInterface $logger,
|
|
|
|
|
private MemoryInfo $memoryInfo,
|
|
|
|
|
private IAppManager $appManager,
|
2023-03-13 06:05:52 -04:00
|
|
|
private Defaults $defaults,
|
2024-03-06 05:54:58 -05:00
|
|
|
) {
|
2024-09-17 16:38:44 -04:00
|
|
|
$this->application = new SymfonyApplication($defaults->getName(), $serverVersion->getVersionString());
|
2015-04-08 04:53:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-12-09 00:50:47 -05:00
|
|
|
* @throws \Exception
|
2015-04-08 04:53:03 -04:00
|
|
|
*/
|
2018-07-01 14:54:19 -04:00
|
|
|
public function loadCommands(
|
|
|
|
|
InputInterface $input,
|
2024-09-19 05:10:31 -04:00
|
|
|
ConsoleOutputInterface $output,
|
2023-03-13 06:05:52 -04:00
|
|
|
): void {
|
2015-04-08 04:53:03 -04:00
|
|
|
// $application is required to be defined in the register_command scripts
|
|
|
|
|
$application = $this->application;
|
2016-02-17 18:42:00 -05:00
|
|
|
$inputDefinition = $application->getDefinition();
|
|
|
|
|
$inputDefinition->addOption(
|
|
|
|
|
new InputOption(
|
2019-02-06 11:08:41 -05:00
|
|
|
'no-warnings',
|
|
|
|
|
null,
|
|
|
|
|
InputOption::VALUE_NONE,
|
|
|
|
|
'Skip global warnings, show command output only',
|
2016-02-17 18:42:00 -05:00
|
|
|
null
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
try {
|
|
|
|
|
$input->bind($inputDefinition);
|
|
|
|
|
} catch (\RuntimeException $e) {
|
|
|
|
|
//expected if there are extra options
|
|
|
|
|
}
|
|
|
|
|
if ($input->getOption('no-warnings')) {
|
|
|
|
|
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
|
|
|
|
|
}
|
2018-08-04 15:53:50 -04:00
|
|
|
|
|
|
|
|
if ($this->memoryInfo->isMemoryLimitSufficient() === false) {
|
2018-08-21 03:51:05 -04:00
|
|
|
$output->getErrorOutput()->writeln(
|
2025-06-30 09:04:05 -04:00
|
|
|
'<comment>The current PHP memory limit '
|
|
|
|
|
. 'is below the recommended value of 512MB.</comment>'
|
2018-08-21 03:51:05 -04:00
|
|
|
);
|
2018-08-04 15:53:50 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-20 11:06:10 -04:00
|
|
|
try {
|
|
|
|
|
require_once __DIR__ . '/../../../core/register_command.php';
|
2023-04-05 06:50:08 -04:00
|
|
|
if ($this->config->getSystemValueBool('installed', false)) {
|
2016-10-20 11:06:10 -04:00
|
|
|
if (\OCP\Util::needUpgrade()) {
|
|
|
|
|
throw new NeedsUpdateException();
|
2019-02-06 11:08:41 -05:00
|
|
|
} elseif ($this->config->getSystemValueBool('maintenance')) {
|
2018-07-01 14:54:19 -04:00
|
|
|
$this->writeMaintenanceModeInfo($input, $output);
|
2016-10-20 11:06:10 -04:00
|
|
|
} else {
|
2024-03-06 05:54:58 -05:00
|
|
|
$this->appManager->loadApps();
|
2025-02-10 05:35:38 -05:00
|
|
|
foreach ($this->appManager->getEnabledApps() as $app) {
|
2024-03-06 05:54:58 -05:00
|
|
|
try {
|
|
|
|
|
$appPath = $this->appManager->getAppPath($app);
|
|
|
|
|
} catch (AppPathNotFoundException) {
|
2016-10-20 11:06:10 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
// load commands using info.xml
|
2024-03-06 05:54:58 -05:00
|
|
|
$info = $this->appManager->getAppInfo($app);
|
2016-10-20 11:06:10 -04:00
|
|
|
if (isset($info['commands'])) {
|
2024-01-15 10:28:50 -05:00
|
|
|
try {
|
|
|
|
|
$this->loadCommandsFromInfoXml($info['commands']);
|
|
|
|
|
} catch (\Throwable $e) {
|
2024-08-23 09:10:27 -04:00
|
|
|
$output->writeln('<error>' . $e->getMessage() . '</error>');
|
2024-01-15 10:28:50 -05:00
|
|
|
$this->logger->error($e->getMessage(), [
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2016-10-20 11:06:10 -04:00
|
|
|
}
|
|
|
|
|
// load from register_command.php
|
|
|
|
|
\OC_App::registerAutoloading($app, $appPath);
|
|
|
|
|
$file = $appPath . '/appinfo/register_command.php';
|
|
|
|
|
if (file_exists($file)) {
|
2016-11-24 06:28:40 -05:00
|
|
|
try {
|
|
|
|
|
require $file;
|
|
|
|
|
} catch (\Exception $e) {
|
2021-04-16 08:26:43 -04:00
|
|
|
$this->logger->error($e->getMessage(), [
|
|
|
|
|
'exception' => $e,
|
|
|
|
|
]);
|
2016-11-24 06:28:40 -05:00
|
|
|
}
|
2016-10-20 11:06:10 -04:00
|
|
|
}
|
2015-04-08 04:53:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
2020-04-10 04:35:09 -04:00
|
|
|
} elseif ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'maintenance:install') {
|
2021-08-18 08:31:49 -04:00
|
|
|
$errorOutput = $output->getErrorOutput();
|
2024-08-23 09:10:27 -04:00
|
|
|
$errorOutput->writeln('Nextcloud is not installed - only a limited number of commands are available');
|
2016-10-20 11:06:10 -04:00
|
|
|
}
|
2023-03-13 06:05:52 -04:00
|
|
|
} catch (NeedsUpdateException) {
|
2024-12-21 11:14:20 -05:00
|
|
|
if ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'upgrade') {
|
2021-08-18 08:31:49 -04:00
|
|
|
$errorOutput = $output->getErrorOutput();
|
2024-08-23 09:10:27 -04:00
|
|
|
$errorOutput->writeln('Nextcloud or one of the apps require upgrade - only a limited number of commands are available');
|
|
|
|
|
$errorOutput->writeln('You may use your browser or the occ upgrade command to do the upgrade');
|
2015-04-08 04:53:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-29 09:35:36 -04:00
|
|
|
|
2015-04-08 04:53:03 -04:00
|
|
|
if ($input->getFirstArgument() !== 'check') {
|
2023-03-13 06:05:52 -04:00
|
|
|
$errors = \OC_Util::checkServer(Server::get(SystemConfig::class));
|
2015-04-08 04:53:03 -04:00
|
|
|
if (!empty($errors)) {
|
|
|
|
|
foreach ($errors as $error) {
|
2015-04-16 06:32:17 -04:00
|
|
|
$output->writeln((string)$error['error']);
|
|
|
|
|
$output->writeln((string)$error['hint']);
|
2015-04-08 04:53:03 -04:00
|
|
|
$output->writeln('');
|
|
|
|
|
}
|
2024-08-23 09:10:27 -04:00
|
|
|
throw new \Exception('Environment not properly prepared.');
|
2015-04-08 04:53:03 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-01 14:54:19 -04:00
|
|
|
/**
|
|
|
|
|
* Write a maintenance mode info.
|
|
|
|
|
* The commands "_completion" and "maintenance:mode" are excluded.
|
|
|
|
|
*
|
|
|
|
|
* @param InputInterface $input The input implementation for reading inputs.
|
|
|
|
|
* @param ConsoleOutputInterface $output The output implementation
|
2024-08-23 09:10:27 -04:00
|
|
|
* for writing outputs.
|
2018-07-01 14:54:19 -04:00
|
|
|
* @return void
|
|
|
|
|
*/
|
2023-04-25 12:43:55 -04:00
|
|
|
private function writeMaintenanceModeInfo(InputInterface $input, ConsoleOutputInterface $output): void {
|
2018-07-01 14:54:19 -04:00
|
|
|
if ($input->getArgument('command') !== '_completion'
|
2022-12-22 16:03:21 -05:00
|
|
|
&& $input->getArgument('command') !== 'maintenance:mode'
|
|
|
|
|
&& $input->getArgument('command') !== 'status') {
|
2018-07-01 14:54:19 -04:00
|
|
|
$errOutput = $output->getErrorOutput();
|
2023-04-25 12:43:55 -04:00
|
|
|
$errOutput->writeln('<comment>Nextcloud is in maintenance mode, no apps are loaded.</comment>');
|
|
|
|
|
$errOutput->writeln('<comment>Commands provided by apps are unavailable.</comment>');
|
2018-07-01 14:54:19 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-09 00:50:47 -05:00
|
|
|
/**
|
|
|
|
|
* Sets whether to automatically exit after a command execution or not.
|
|
|
|
|
*
|
|
|
|
|
* @param bool $boolean Whether to automatically exit after a command execution or not
|
|
|
|
|
*/
|
2023-03-13 06:05:52 -04:00
|
|
|
public function setAutoExit(bool $boolean): void {
|
2015-04-08 04:53:03 -04:00
|
|
|
$this->application->setAutoExit($boolean);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
2024-03-28 11:13:19 -04:00
|
|
|
public function run(?InputInterface $input = null, ?OutputInterface $output = null) {
|
2023-06-30 06:50:28 -04:00
|
|
|
$event = new ConsoleEvent(
|
2016-02-05 06:24:54 -05:00
|
|
|
ConsoleEvent::EVENT_RUN,
|
2016-07-07 06:14:45 -04:00
|
|
|
$this->request->server['argv']
|
2023-06-30 06:50:28 -04:00
|
|
|
);
|
|
|
|
|
$this->dispatcher->dispatchTyped($event);
|
|
|
|
|
$this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, $event);
|
2015-04-08 04:53:03 -04:00
|
|
|
return $this->application->run($input, $output);
|
|
|
|
|
}
|
2016-09-30 04:09:52 -04:00
|
|
|
|
2023-03-13 06:05:52 -04:00
|
|
|
/**
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
private function loadCommandsFromInfoXml(iterable $commands): void {
|
2016-09-30 04:09:52 -04:00
|
|
|
foreach ($commands as $command) {
|
|
|
|
|
try {
|
2023-03-13 06:05:52 -04:00
|
|
|
$c = Server::get($command);
|
2023-06-06 05:09:24 -04:00
|
|
|
} catch (ContainerExceptionInterface $e) {
|
2016-09-30 04:09:52 -04:00
|
|
|
if (class_exists($command)) {
|
2021-02-08 13:14:27 -05:00
|
|
|
try {
|
|
|
|
|
$c = new $command();
|
2023-03-13 06:05:52 -04:00
|
|
|
} catch (ArgumentCountError) {
|
2021-02-08 13:14:27 -05:00
|
|
|
throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e);
|
|
|
|
|
}
|
2016-09-30 04:09:52 -04:00
|
|
|
} else {
|
|
|
|
|
throw new \Exception("Console command '$command' is unknown and could not be loaded");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->application->add($c);
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-04-08 04:53:03 -04:00
|
|
|
}
|