update.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. Server::get(\OCP\ServerVersion::class),
  84. $config,
  85. Server::get(IAppConfig::class),
  86. \OC::$server->getIntegrityCodeChecker(),
  87. Server::get(LoggerInterface::class),
  88. Server::get(\OC\Installer::class)
  89. );
  90. $incompatibleApps = [];
  91. $incompatibleOverwrites = $config->getSystemValue('app_install_overwrite', []);
  92. /** @var IEventDispatcher $dispatcher */
  93. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  94. $dispatcher->addListener(
  95. MigratorExecuteSqlEvent::class,
  96. function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void {
  97. $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()]));
  98. }
  99. );
  100. $feedBack = new FeedBackHandler($eventSource, $l);
  101. $dispatcher->addListener(RepairStartEvent::class, [$feedBack, 'handleRepairFeedback']);
  102. $dispatcher->addListener(RepairAdvanceEvent::class, [$feedBack, 'handleRepairFeedback']);
  103. $dispatcher->addListener(RepairFinishEvent::class, [$feedBack, 'handleRepairFeedback']);
  104. $dispatcher->addListener(RepairStepEvent::class, [$feedBack, 'handleRepairFeedback']);
  105. $dispatcher->addListener(RepairInfoEvent::class, [$feedBack, 'handleRepairFeedback']);
  106. $dispatcher->addListener(RepairWarningEvent::class, [$feedBack, 'handleRepairFeedback']);
  107. $dispatcher->addListener(RepairErrorEvent::class, [$feedBack, 'handleRepairFeedback']);
  108. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
  109. $eventSource->send('success', $l->t('Turned on maintenance mode'));
  110. });
  111. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
  112. $eventSource->send('success', $l->t('Turned off maintenance mode'));
  113. });
  114. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
  115. $eventSource->send('success', $l->t('Maintenance mode is kept active'));
  116. });
  117. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource, $l) {
  118. $eventSource->send('success', $l->t('Updating database schema'));
  119. });
  120. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
  121. $eventSource->send('success', $l->t('Updated database'));
  122. });
  123. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource, $l) {
  124. $eventSource->send('success', $l->t('Update app "%s" from App Store', [$app]));
  125. });
  126. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
  127. $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]));
  128. });
  129. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
  130. $eventSource->send('success', $l->t('Updated "%1$s" to %2$s', [$app, $version]));
  131. });
  132. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps, &$incompatibleOverwrites) {
  133. if (!in_array($app, $incompatibleOverwrites)) {
  134. $incompatibleApps[] = $app;
  135. }
  136. });
  137. $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource, $config) {
  138. $eventSource->send('failure', $message);
  139. $eventSource->close();
  140. $config->setSystemValue('maintenance', false);
  141. });
  142. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  143. $eventSource->send('success', $l->t('Set log level to debug'));
  144. });
  145. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  146. $eventSource->send('success', $l->t('Reset log level'));
  147. });
  148. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($eventSource, $l) {
  149. $eventSource->send('success', $l->t('Starting code integrity check'));
  150. });
  151. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($eventSource, $l) {
  152. $eventSource->send('success', $l->t('Finished code integrity check'));
  153. });
  154. try {
  155. $updater->upgrade();
  156. } catch (\Exception $e) {
  157. Server::get(LoggerInterface::class)->error(
  158. $e->getMessage(),
  159. [
  160. 'exception' => $e,
  161. 'app' => 'update',
  162. ]);
  163. $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
  164. $eventSource->close();
  165. exit();
  166. }
  167. $disabledApps = [];
  168. foreach ($incompatibleApps as $app) {
  169. $disabledApps[$app] = $l->t('%s (incompatible)', [$app]);
  170. }
  171. if (!empty($disabledApps)) {
  172. $eventSource->send('notice', $l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
  173. }
  174. } else {
  175. $eventSource->send('notice', $l->t('Already up to date'));
  176. }
  177. $eventSource->send('done', '');
  178. $eventSource->close();