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.

update.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use OC\DB\MigratorExecuteSqlEvent;
  8. use OC\Repair\Events\RepairAdvanceEvent;
  9. use OC\Repair\Events\RepairErrorEvent;
  10. use OC\Repair\Events\RepairFinishEvent;
  11. use OC\Repair\Events\RepairInfoEvent;
  12. use OC\Repair\Events\RepairStartEvent;
  13. use OC\Repair\Events\RepairStepEvent;
  14. use OC\Repair\Events\RepairWarningEvent;
  15. use OCP\EventDispatcher\Event;
  16. use OCP\EventDispatcher\IEventDispatcher;
  17. use OCP\IAppConfig;
  18. use OCP\IConfig;
  19. use OCP\IEventSource;
  20. use OCP\IEventSourceFactory;
  21. use OCP\IL10N;
  22. use OCP\L10N\IFactory;
  23. use OCP\Server;
  24. use Psr\Log\LoggerInterface;
  25. if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
  26. @set_time_limit(0);
  27. }
  28. require_once '../../lib/base.php';
  29. /** @var \OCP\IL10N $l */
  30. $l = \OC::$server->get(IFactory::class)->get('core');
  31. $eventSource = \OC::$server->get(IEventSourceFactory::class)->create();
  32. // need to send an initial message to force-init the event source,
  33. // which will then trigger its own CSRF check and produces its own CSRF error
  34. // message
  35. $eventSource->send('success', $l->t('Preparing update'));
  36. class FeedBackHandler {
  37. private int $progressStateMax = 100;
  38. private int $progressStateStep = 0;
  39. private string $currentStep = '';
  40. public function __construct(
  41. private IEventSource $eventSource,
  42. private IL10N $l10n,
  43. ) {
  44. }
  45. public function handleRepairFeedback(Event $event): void {
  46. if ($event instanceof RepairStartEvent) {
  47. $this->progressStateMax = $event->getMaxStep();
  48. $this->progressStateStep = 0;
  49. $this->currentStep = $event->getCurrentStepName();
  50. } elseif ($event instanceof RepairAdvanceEvent) {
  51. $this->progressStateStep += $event->getIncrement();
  52. $desc = $event->getDescription();
  53. if (empty($desc)) {
  54. $desc = $this->currentStep;
  55. }
  56. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
  57. } elseif ($event instanceof RepairFinishEvent) {
  58. $this->progressStateMax = $this->progressStateStep;
  59. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
  60. } elseif ($event instanceof RepairStepEvent) {
  61. $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getStepName());
  62. } elseif ($event instanceof RepairInfoEvent) {
  63. $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage());
  64. } elseif ($event instanceof RepairWarningEvent) {
  65. $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage());
  66. } elseif ($event instanceof RepairErrorEvent) {
  67. $this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage());
  68. }
  69. }
  70. }
  71. if (\OCP\Util::needUpgrade()) {
  72. $config = \OC::$server->getSystemConfig();
  73. if ($config->getValue('upgrade.disable-web', false)) {
  74. $eventSource->send('failure', $l->t('Please use the command line updater because updating via browser is disabled in your config.php.'));
  75. $eventSource->close();
  76. exit();
  77. }
  78. // if a user is currently logged in, their session must be ignored to
  79. // avoid side effects
  80. \OC_User::setIncognitoMode(true);
  81. $config = Server::get(IConfig::class);
  82. $updater = new \OC\Updater(
  83. $config,
  84. Server::get(IAppConfig::class),
  85. \OC::$server->getIntegrityCodeChecker(),
  86. Server::get(LoggerInterface::class),
  87. Server::get(\OC\Installer::class)
  88. );
  89. $incompatibleApps = [];
  90. $incompatibleOverwrites = $config->getSystemValue('app_install_overwrite', []);
  91. /** @var IEventDispatcher $dispatcher */
  92. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  93. $dispatcher->addListener(
  94. MigratorExecuteSqlEvent::class,
  95. function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void {
  96. $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()]));
  97. }
  98. );
  99. $feedBack = new FeedBackHandler($eventSource, $l);
  100. $dispatcher->addListener(RepairStartEvent::class, [$feedBack, 'handleRepairFeedback']);
  101. $dispatcher->addListener(RepairAdvanceEvent::class, [$feedBack, 'handleRepairFeedback']);
  102. $dispatcher->addListener(RepairFinishEvent::class, [$feedBack, 'handleRepairFeedback']);
  103. $dispatcher->addListener(RepairStepEvent::class, [$feedBack, 'handleRepairFeedback']);
  104. $dispatcher->addListener(RepairInfoEvent::class, [$feedBack, 'handleRepairFeedback']);
  105. $dispatcher->addListener(RepairWarningEvent::class, [$feedBack, 'handleRepairFeedback']);
  106. $dispatcher->addListener(RepairErrorEvent::class, [$feedBack, 'handleRepairFeedback']);
  107. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
  108. $eventSource->send('success', $l->t('Turned on maintenance mode'));
  109. });
  110. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
  111. $eventSource->send('success', $l->t('Turned off maintenance mode'));
  112. });
  113. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
  114. $eventSource->send('success', $l->t('Maintenance mode is kept active'));
  115. });
  116. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource, $l) {
  117. $eventSource->send('success', $l->t('Updating database schema'));
  118. });
  119. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
  120. $eventSource->send('success', $l->t('Updated database'));
  121. });
  122. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource, $l) {
  123. $eventSource->send('success', $l->t('Update app "%s" from App Store', [$app]));
  124. });
  125. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
  126. $eventSource->send('success', $l->t('Checking whether the database schema for %s can be updated (this can take a long time depending on the database size)', [$app]));
  127. });
  128. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
  129. $eventSource->send('success', $l->t('Updated "%1$s" to %2$s', [$app, $version]));
  130. });
  131. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps, &$incompatibleOverwrites) {
  132. if (!in_array($app, $incompatibleOverwrites)) {
  133. $incompatibleApps[] = $app;
  134. }
  135. });
  136. $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource, $config) {
  137. $eventSource->send('failure', $message);
  138. $eventSource->close();
  139. $config->setSystemValue('maintenance', false);
  140. });
  141. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  142. $eventSource->send('success', $l->t('Set log level to debug'));
  143. });
  144. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  145. $eventSource->send('success', $l->t('Reset log level'));
  146. });
  147. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($eventSource, $l) {
  148. $eventSource->send('success', $l->t('Starting code integrity check'));
  149. });
  150. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($eventSource, $l) {
  151. $eventSource->send('success', $l->t('Finished code integrity check'));
  152. });
  153. try {
  154. $updater->upgrade();
  155. } catch (\Exception $e) {
  156. Server::get(LoggerInterface::class)->error(
  157. $e->getMessage(),
  158. [
  159. 'exception' => $e,
  160. 'app' => 'update',
  161. ]);
  162. $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
  163. $eventSource->close();
  164. exit();
  165. }
  166. $disabledApps = [];
  167. foreach ($incompatibleApps as $app) {
  168. $disabledApps[$app] = $l->t('%s (incompatible)', [$app]);
  169. }
  170. if (!empty($disabledApps)) {
  171. $eventSource->send('notice', $l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
  172. }
  173. } else {
  174. $eventSource->send('notice', $l->t('Already up to date'));
  175. }
  176. $eventSource->send('done', '');
  177. $eventSource->close();