You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
7.2 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Michael Weimann <mail@michael-weimann.eu>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Console;
  32. use OC\MemoryInfo;
  33. use OC\NeedsUpdateException;
  34. use OC_App;
  35. use OCP\AppFramework\QueryException;
  36. use OCP\Console\ConsoleEvent;
  37. use OCP\IConfig;
  38. use OCP\ILogger;
  39. use OCP\IRequest;
  40. use Symfony\Component\Console\Application as SymfonyApplication;
  41. use Symfony\Component\Console\Input\InputInterface;
  42. use Symfony\Component\Console\Input\InputOption;
  43. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  44. use Symfony\Component\Console\Output\OutputInterface;
  45. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  46. class Application {
  47. /** @var IConfig */
  48. private $config;
  49. /** @var EventDispatcherInterface */
  50. private $dispatcher;
  51. /** @var IRequest */
  52. private $request;
  53. /** @var ILogger */
  54. private $logger;
  55. /** @var MemoryInfo */
  56. private $memoryInfo;
  57. /**
  58. * @param IConfig $config
  59. * @param EventDispatcherInterface $dispatcher
  60. * @param IRequest $request
  61. * @param ILogger $logger
  62. * @param MemoryInfo $memoryInfo
  63. */
  64. public function __construct(IConfig $config,
  65. EventDispatcherInterface $dispatcher,
  66. IRequest $request,
  67. ILogger $logger,
  68. MemoryInfo $memoryInfo) {
  69. $defaults = \OC::$server->getThemingDefaults();
  70. $this->config = $config;
  71. $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString());
  72. $this->dispatcher = $dispatcher;
  73. $this->request = $request;
  74. $this->logger = $logger;
  75. $this->memoryInfo = $memoryInfo;
  76. }
  77. /**
  78. * @param InputInterface $input
  79. * @param ConsoleOutputInterface $output
  80. * @throws \Exception
  81. */
  82. public function loadCommands(
  83. InputInterface $input,
  84. ConsoleOutputInterface $output
  85. ) {
  86. // $application is required to be defined in the register_command scripts
  87. $application = $this->application;
  88. $inputDefinition = $application->getDefinition();
  89. $inputDefinition->addOption(
  90. new InputOption(
  91. 'no-warnings',
  92. null,
  93. InputOption::VALUE_NONE,
  94. 'Skip global warnings, show command output only',
  95. null
  96. )
  97. );
  98. try {
  99. $input->bind($inputDefinition);
  100. } catch (\RuntimeException $e) {
  101. //expected if there are extra options
  102. }
  103. if ($input->getOption('no-warnings')) {
  104. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  105. }
  106. if ($this->memoryInfo->isMemoryLimitSufficient() === false) {
  107. $output->getErrorOutput()->writeln(
  108. '<comment>The current PHP memory limit ' .
  109. 'is below the recommended value of 512MB.</comment>'
  110. );
  111. }
  112. try {
  113. require_once __DIR__ . '/../../../core/register_command.php';
  114. if ($this->config->getSystemValue('installed', false)) {
  115. if (\OCP\Util::needUpgrade()) {
  116. throw new NeedsUpdateException();
  117. } elseif ($this->config->getSystemValueBool('maintenance')) {
  118. $this->writeMaintenanceModeInfo($input, $output);
  119. } else {
  120. OC_App::loadApps();
  121. foreach (\OC::$server->getAppManager()->getInstalledApps() as $app) {
  122. $appPath = \OC_App::getAppPath($app);
  123. if ($appPath === false) {
  124. continue;
  125. }
  126. // load commands using info.xml
  127. $info = \OC_App::getAppInfo($app);
  128. if (isset($info['commands'])) {
  129. $this->loadCommandsFromInfoXml($info['commands']);
  130. }
  131. // load from register_command.php
  132. \OC_App::registerAutoloading($app, $appPath);
  133. $file = $appPath . '/appinfo/register_command.php';
  134. if (file_exists($file)) {
  135. try {
  136. require $file;
  137. } catch (\Exception $e) {
  138. $this->logger->logException($e);
  139. }
  140. }
  141. }
  142. }
  143. } elseif ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'maintenance:install') {
  144. $output->writeln("Nextcloud is not installed - only a limited number of commands are available");
  145. }
  146. } catch (NeedsUpdateException $e) {
  147. if ($input->getArgument('command') !== '_completion') {
  148. $output->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
  149. $output->writeln("You may use your browser or the occ upgrade command to do the upgrade");
  150. }
  151. }
  152. if ($input->getFirstArgument() !== 'check') {
  153. $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
  154. if (!empty($errors)) {
  155. foreach ($errors as $error) {
  156. $output->writeln((string)$error['error']);
  157. $output->writeln((string)$error['hint']);
  158. $output->writeln('');
  159. }
  160. throw new \Exception("Environment not properly prepared.");
  161. }
  162. }
  163. }
  164. /**
  165. * Write a maintenance mode info.
  166. * The commands "_completion" and "maintenance:mode" are excluded.
  167. *
  168. * @param InputInterface $input The input implementation for reading inputs.
  169. * @param ConsoleOutputInterface $output The output implementation
  170. * for writing outputs.
  171. * @return void
  172. */
  173. private function writeMaintenanceModeInfo(
  174. InputInterface $input, ConsoleOutputInterface $output
  175. ) {
  176. if ($input->getArgument('command') !== '_completion'
  177. && $input->getArgument('command') !== 'maintenance:mode') {
  178. $errOutput = $output->getErrorOutput();
  179. $errOutput->writeln(
  180. '<comment>Nextcloud is in maintenance mode - ' .
  181. 'no apps have been loaded</comment>' . PHP_EOL
  182. );
  183. }
  184. }
  185. /**
  186. * Sets whether to automatically exit after a command execution or not.
  187. *
  188. * @param bool $boolean Whether to automatically exit after a command execution or not
  189. */
  190. public function setAutoExit($boolean) {
  191. $this->application->setAutoExit($boolean);
  192. }
  193. /**
  194. * @param InputInterface $input
  195. * @param OutputInterface $output
  196. * @return int
  197. * @throws \Exception
  198. */
  199. public function run(InputInterface $input = null, OutputInterface $output = null) {
  200. $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
  201. ConsoleEvent::EVENT_RUN,
  202. $this->request->server['argv']
  203. ));
  204. return $this->application->run($input, $output);
  205. }
  206. private function loadCommandsFromInfoXml($commands) {
  207. foreach ($commands as $command) {
  208. try {
  209. $c = \OC::$server->query($command);
  210. } catch (QueryException $e) {
  211. if (class_exists($command)) {
  212. $c = new $command();
  213. } else {
  214. throw new \Exception("Console command '$command' is unknown and could not be loaded");
  215. }
  216. }
  217. $this->application->add($c);
  218. }
  219. }
  220. }