Application.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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\App\IAppManager;
  37. use OCP\Console\ConsoleEvent;
  38. use OCP\IConfig;
  39. use OCP\IRequest;
  40. use Psr\Log\LoggerInterface;
  41. use Symfony\Component\Console\Application as SymfonyApplication;
  42. use Symfony\Component\Console\Input\InputInterface;
  43. use Symfony\Component\Console\Input\InputOption;
  44. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  45. use Symfony\Component\Console\Output\OutputInterface;
  46. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  47. class Application {
  48. /** @var IConfig */
  49. private $config;
  50. /** @var EventDispatcherInterface */
  51. private $dispatcher;
  52. /** @var IRequest */
  53. private $request;
  54. /** @var LoggerInterface */
  55. private $logger;
  56. /** @var MemoryInfo */
  57. private $memoryInfo;
  58. public function __construct(IConfig $config,
  59. EventDispatcherInterface $dispatcher,
  60. IRequest $request,
  61. LoggerInterface $logger,
  62. MemoryInfo $memoryInfo) {
  63. $defaults = \OC::$server->getThemingDefaults();
  64. $this->config = $config;
  65. $this->application = new SymfonyApplication($defaults->getName(), \OC_Util::getVersionString());
  66. $this->dispatcher = $dispatcher;
  67. $this->request = $request;
  68. $this->logger = $logger;
  69. $this->memoryInfo = $memoryInfo;
  70. }
  71. /**
  72. * @param InputInterface $input
  73. * @param ConsoleOutputInterface $output
  74. * @throws \Exception
  75. */
  76. public function loadCommands(
  77. InputInterface $input,
  78. ConsoleOutputInterface $output
  79. ) {
  80. // $application is required to be defined in the register_command scripts
  81. $application = $this->application;
  82. $inputDefinition = $application->getDefinition();
  83. $inputDefinition->addOption(
  84. new InputOption(
  85. 'no-warnings',
  86. null,
  87. InputOption::VALUE_NONE,
  88. 'Skip global warnings, show command output only',
  89. null
  90. )
  91. );
  92. try {
  93. $input->bind($inputDefinition);
  94. } catch (\RuntimeException $e) {
  95. //expected if there are extra options
  96. }
  97. if ($input->getOption('no-warnings')) {
  98. $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
  99. }
  100. if ($this->memoryInfo->isMemoryLimitSufficient() === false) {
  101. $output->getErrorOutput()->writeln(
  102. '<comment>The current PHP memory limit ' .
  103. 'is below the recommended value of 512MB.</comment>'
  104. );
  105. }
  106. try {
  107. require_once __DIR__ . '/../../../core/register_command.php';
  108. if ($this->config->getSystemValue('installed', false)) {
  109. if (\OCP\Util::needUpgrade()) {
  110. throw new NeedsUpdateException();
  111. } elseif ($this->config->getSystemValueBool('maintenance')) {
  112. $this->writeMaintenanceModeInfo($input, $output);
  113. } else {
  114. OC_App::loadApps();
  115. $appManager = \OCP\Server::get(IAppManager::class);
  116. foreach ($appManager->getInstalledApps() as $app) {
  117. $appPath = \OC_App::getAppPath($app);
  118. if ($appPath === false) {
  119. continue;
  120. }
  121. // load commands using info.xml
  122. $info = $appManager->getAppInfo($app);
  123. if (isset($info['commands'])) {
  124. $this->loadCommandsFromInfoXml($info['commands']);
  125. }
  126. // load from register_command.php
  127. \OC_App::registerAutoloading($app, $appPath);
  128. $file = $appPath . '/appinfo/register_command.php';
  129. if (file_exists($file)) {
  130. try {
  131. require $file;
  132. } catch (\Exception $e) {
  133. $this->logger->error($e->getMessage(), [
  134. 'exception' => $e,
  135. ]);
  136. }
  137. }
  138. }
  139. }
  140. } elseif ($input->getArgument('command') !== '_completion' && $input->getArgument('command') !== 'maintenance:install') {
  141. $errorOutput = $output->getErrorOutput();
  142. $errorOutput->writeln("Nextcloud is not installed - only a limited number of commands are available");
  143. }
  144. } catch (NeedsUpdateException $e) {
  145. if ($input->getArgument('command') !== '_completion') {
  146. $errorOutput = $output->getErrorOutput();
  147. $errorOutput->writeln("Nextcloud or one of the apps require upgrade - only a limited number of commands are available");
  148. $errorOutput->writeln("You may use your browser or the occ upgrade command to do the upgrade");
  149. }
  150. }
  151. if ($input->getFirstArgument() !== 'check') {
  152. $errors = \OC_Util::checkServer(\OC::$server->getSystemConfig());
  153. if (!empty($errors)) {
  154. foreach ($errors as $error) {
  155. $output->writeln((string)$error['error']);
  156. $output->writeln((string)$error['hint']);
  157. $output->writeln('');
  158. }
  159. throw new \Exception("Environment not properly prepared.");
  160. }
  161. }
  162. }
  163. /**
  164. * Write a maintenance mode info.
  165. * The commands "_completion" and "maintenance:mode" are excluded.
  166. *
  167. * @param InputInterface $input The input implementation for reading inputs.
  168. * @param ConsoleOutputInterface $output The output implementation
  169. * for writing outputs.
  170. * @return void
  171. */
  172. private function writeMaintenanceModeInfo(
  173. InputInterface $input, ConsoleOutputInterface $output
  174. ) {
  175. if ($input->getArgument('command') !== '_completion'
  176. && $input->getArgument('command') !== 'maintenance:mode') {
  177. $errOutput = $output->getErrorOutput();
  178. $errOutput->writeln(
  179. '<comment>Nextcloud is in maintenance mode, hence the database isn\'t accessible.' . PHP_EOL .
  180. 'Cannot perform any command except \'maintenance:mode --off\'</comment>' . PHP_EOL
  181. );
  182. }
  183. }
  184. /**
  185. * Sets whether to automatically exit after a command execution or not.
  186. *
  187. * @param bool $boolean Whether to automatically exit after a command execution or not
  188. */
  189. public function setAutoExit($boolean) {
  190. $this->application->setAutoExit($boolean);
  191. }
  192. /**
  193. * @param InputInterface $input
  194. * @param OutputInterface $output
  195. * @return int
  196. * @throws \Exception
  197. */
  198. public function run(InputInterface $input = null, OutputInterface $output = null) {
  199. $this->dispatcher->dispatch(ConsoleEvent::EVENT_RUN, new ConsoleEvent(
  200. ConsoleEvent::EVENT_RUN,
  201. $this->request->server['argv']
  202. ));
  203. return $this->application->run($input, $output);
  204. }
  205. private function loadCommandsFromInfoXml($commands) {
  206. foreach ($commands as $command) {
  207. try {
  208. $c = \OC::$server->query($command);
  209. } catch (QueryException $e) {
  210. if (class_exists($command)) {
  211. try {
  212. $c = new $command();
  213. } catch (\ArgumentCountError $e2) {
  214. throw new \Exception("Failed to construct console command '$command': " . $e->getMessage(), 0, $e);
  215. }
  216. } else {
  217. throw new \Exception("Console command '$command' is unknown and could not be loaded");
  218. }
  219. }
  220. $this->application->add($c);
  221. }
  222. }
  223. }