update.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Ko- <k.stoffelen@cs.ru.nl>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Valdnet <47037905+Valdnet@users.noreply.github.com>
  15. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  16. * @author Vincent Petry <vincent@nextcloud.com>
  17. *
  18. * @license AGPL-3.0
  19. *
  20. * This code is free software: you can redistribute it and/or modify
  21. * it under the terms of the GNU Affero General Public License, version 3,
  22. * as published by the Free Software Foundation.
  23. *
  24. * This program is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. * GNU Affero General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Affero General Public License, version 3,
  30. * along with this program. If not, see <http://www.gnu.org/licenses/>
  31. *
  32. */
  33. use OC\DB\MigratorExecuteSqlEvent;
  34. use OC\Repair\Events\RepairAdvanceEvent;
  35. use OC\Repair\Events\RepairErrorEvent;
  36. use OC\Repair\Events\RepairFinishEvent;
  37. use OC\Repair\Events\RepairInfoEvent;
  38. use OC\Repair\Events\RepairStartEvent;
  39. use OC\Repair\Events\RepairStepEvent;
  40. use OC\Repair\Events\RepairWarningEvent;
  41. use OCP\EventDispatcher\Event;
  42. use OCP\EventDispatcher\IEventDispatcher;
  43. use OCP\IEventSource;
  44. use OCP\IEventSourceFactory;
  45. use OCP\IL10N;
  46. use OCP\ILogger;
  47. use OCP\L10N\IFactory;
  48. if (!str_contains(@ini_get('disable_functions'), 'set_time_limit')) {
  49. @set_time_limit(0);
  50. }
  51. require_once '../../lib/base.php';
  52. /** @var \OCP\IL10N $l */
  53. $l = \OC::$server->get(IFactory::class)->get('core');
  54. $eventSource = \OC::$server->get(IEventSourceFactory::class)->create();
  55. // need to send an initial message to force-init the event source,
  56. // which will then trigger its own CSRF check and produces its own CSRF error
  57. // message
  58. $eventSource->send('success', $l->t('Preparing update'));
  59. class FeedBackHandler {
  60. private int $progressStateMax = 100;
  61. private int $progressStateStep = 0;
  62. private string $currentStep = '';
  63. public function __construct(
  64. private IEventSource $eventSource,
  65. private IL10N $l10n,
  66. ) {
  67. }
  68. public function handleRepairFeedback(Event $event): void {
  69. if ($event instanceof RepairStartEvent) {
  70. $this->progressStateMax = $event->getMaxStep();
  71. $this->progressStateStep = 0;
  72. $this->currentStep = $event->getCurrentStepName();
  73. } elseif ($event instanceof RepairAdvanceEvent) {
  74. $this->progressStateStep += $event->getIncrement();
  75. $desc = $event->getDescription();
  76. if (empty($desc)) {
  77. $desc = $this->currentStep;
  78. }
  79. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
  80. } elseif ($event instanceof RepairFinishEvent) {
  81. $this->progressStateMax = $this->progressStateStep;
  82. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
  83. } elseif ($event instanceof RepairStepEvent) {
  84. $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getStepName());
  85. } elseif ($event instanceof RepairInfoEvent) {
  86. $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage());
  87. } elseif ($event instanceof RepairWarningEvent) {
  88. $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage());
  89. } elseif ($event instanceof RepairErrorEvent) {
  90. $this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage());
  91. }
  92. }
  93. }
  94. if (\OCP\Util::needUpgrade()) {
  95. $config = \OC::$server->getSystemConfig();
  96. if ($config->getValue('upgrade.disable-web', false)) {
  97. $eventSource->send('failure', $l->t('Please use the command line updater because updating via browser is disabled in your config.php.'));
  98. $eventSource->close();
  99. exit();
  100. }
  101. // if a user is currently logged in, their session must be ignored to
  102. // avoid side effects
  103. \OC_User::setIncognitoMode(true);
  104. $logger = \OC::$server->get(\Psr\Log\LoggerInterface::class);
  105. $config = \OC::$server->getConfig();
  106. $updater = new \OC\Updater(
  107. $config,
  108. \OC::$server->getIntegrityCodeChecker(),
  109. $logger,
  110. \OC::$server->query(\OC\Installer::class)
  111. );
  112. $incompatibleApps = [];
  113. /** @var IEventDispatcher $dispatcher */
  114. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  115. $dispatcher->addListener(
  116. MigratorExecuteSqlEvent::class,
  117. function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void {
  118. $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()]));
  119. }
  120. );
  121. $feedBack = new FeedBackHandler($eventSource, $l);
  122. $dispatcher->addListener(RepairStartEvent::class, [$feedBack, 'handleRepairFeedback']);
  123. $dispatcher->addListener(RepairAdvanceEvent::class, [$feedBack, 'handleRepairFeedback']);
  124. $dispatcher->addListener(RepairFinishEvent::class, [$feedBack, 'handleRepairFeedback']);
  125. $dispatcher->addListener(RepairStepEvent::class, [$feedBack, 'handleRepairFeedback']);
  126. $dispatcher->addListener(RepairInfoEvent::class, [$feedBack, 'handleRepairFeedback']);
  127. $dispatcher->addListener(RepairWarningEvent::class, [$feedBack, 'handleRepairFeedback']);
  128. $dispatcher->addListener(RepairErrorEvent::class, [$feedBack, 'handleRepairFeedback']);
  129. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
  130. $eventSource->send('success', $l->t('Turned on maintenance mode'));
  131. });
  132. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
  133. $eventSource->send('success', $l->t('Turned off maintenance mode'));
  134. });
  135. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
  136. $eventSource->send('success', $l->t('Maintenance mode is kept active'));
  137. });
  138. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource, $l) {
  139. $eventSource->send('success', $l->t('Updating database schema'));
  140. });
  141. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
  142. $eventSource->send('success', $l->t('Updated database'));
  143. });
  144. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource, $l) {
  145. $eventSource->send('success', $l->t('Update app "%s" from App Store', [$app]));
  146. });
  147. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
  148. $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]));
  149. });
  150. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
  151. $eventSource->send('success', $l->t('Updated "%1$s" to %2$s', [$app, $version]));
  152. });
  153. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps) {
  154. $incompatibleApps[] = $app;
  155. });
  156. $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource, $config) {
  157. $eventSource->send('failure', $message);
  158. $eventSource->close();
  159. $config->setSystemValue('maintenance', false);
  160. });
  161. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  162. $eventSource->send('success', $l->t('Set log level to debug'));
  163. });
  164. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  165. $eventSource->send('success', $l->t('Reset log level'));
  166. });
  167. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($eventSource, $l) {
  168. $eventSource->send('success', $l->t('Starting code integrity check'));
  169. });
  170. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($eventSource, $l) {
  171. $eventSource->send('success', $l->t('Finished code integrity check'));
  172. });
  173. try {
  174. $updater->upgrade();
  175. } catch (\Exception $e) {
  176. \OC::$server->getLogger()->logException($e, [
  177. 'level' => ILogger::ERROR,
  178. 'app' => 'update',
  179. ]);
  180. $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
  181. $eventSource->close();
  182. exit();
  183. }
  184. $disabledApps = [];
  185. foreach ($incompatibleApps as $app) {
  186. $disabledApps[$app] = $l->t('%s (incompatible)', [$app]);
  187. }
  188. if (!empty($disabledApps)) {
  189. $eventSource->send('notice', $l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
  190. }
  191. } else {
  192. $eventSource->send('notice', $l->t('Already up to date'));
  193. }
  194. $eventSource->send('done', '');
  195. $eventSource->close();