update.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 OCP\EventDispatcher\Event;
  34. use OCP\EventDispatcher\IEventDispatcher;
  35. use OCP\IEventSource;
  36. use OCP\IL10N;
  37. use OCP\ILogger;
  38. use OC\DB\MigratorExecuteSqlEvent;
  39. use OC\Repair\Events\RepairAdvanceEvent;
  40. use OC\Repair\Events\RepairErrorEvent;
  41. use OC\Repair\Events\RepairFinishEvent;
  42. use OC\Repair\Events\RepairInfoEvent;
  43. use OC\Repair\Events\RepairStartEvent;
  44. use OC\Repair\Events\RepairStepEvent;
  45. use OC\Repair\Events\RepairWarningEvent;
  46. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  47. @set_time_limit(0);
  48. }
  49. require_once '../../lib/base.php';
  50. $l = \OC::$server->getL10N('core');
  51. $eventSource = \OC::$server->createEventSource();
  52. // need to send an initial message to force-init the event source,
  53. // which will then trigger its own CSRF check and produces its own CSRF error
  54. // message
  55. $eventSource->send('success', $l->t('Preparing update'));
  56. class FeedBackHandler {
  57. private int $progressStateMax = 100;
  58. private int $progressStateStep = 0;
  59. private string $currentStep = '';
  60. private IEventSource $eventSource;
  61. private IL10N $l10n;
  62. public function __construct(IEventSource $eventSource, IL10N $l10n) {
  63. $this->eventSource = $eventSource;
  64. $this->l10n = $l10n;
  65. }
  66. public function handleRepairFeedback(Event $event): void {
  67. if ($event instanceof RepairStartEvent) {
  68. $this->progressStateMax = $event->getMaxStep();
  69. $this->progressStateStep = 0;
  70. $this->currentStep = $event->getCurrentStepName();
  71. } elseif ($event instanceof RepairAdvanceEvent) {
  72. $this->progressStateStep += $event->getIncrement();
  73. $desc = $event->getDescription();
  74. if (empty($desc)) {
  75. $desc = $this->currentStep;
  76. }
  77. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
  78. } elseif ($event instanceof RepairFinishEvent) {
  79. $this->progressStateMax = $this->progressStateStep;
  80. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
  81. } elseif ($event instanceof RepairStepEvent) {
  82. $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getStepName());
  83. } elseif ($event instanceof RepairInfoEvent) {
  84. $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getMessage());
  85. } elseif ($event instanceof RepairWarningEvent) {
  86. $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getMessage());
  87. } elseif ($event instanceof RepairErrorEvent) {
  88. $this->eventSource->send('error', $this->l10n->t('Repair error:') . ' ' . $event->getMessage());
  89. }
  90. }
  91. }
  92. if (\OCP\Util::needUpgrade()) {
  93. $config = \OC::$server->getSystemConfig();
  94. if ($config->getValue('upgrade.disable-web', false)) {
  95. $eventSource->send('failure', $l->t('Please use the command line updater because updating via browser is disabled in your config.php.'));
  96. $eventSource->close();
  97. exit();
  98. }
  99. // if a user is currently logged in, their session must be ignored to
  100. // avoid side effects
  101. \OC_User::setIncognitoMode(true);
  102. $logger = \OC::$server->get(\Psr\Log\LoggerInterface::class);
  103. $config = \OC::$server->getConfig();
  104. $updater = new \OC\Updater(
  105. $config,
  106. \OC::$server->getIntegrityCodeChecker(),
  107. $logger,
  108. \OC::$server->query(\OC\Installer::class)
  109. );
  110. $incompatibleApps = [];
  111. /** @var IEventDispatcher $dispatcher */
  112. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  113. $dispatcher->addListener(
  114. MigratorExecuteSqlEvent::class,
  115. function (MigratorExecuteSqlEvent $event) use ($eventSource, $l): void {
  116. $eventSource->send('success', $l->t('[%d / %d]: %s', [$event->getCurrentStep(), $event->getMaxStep(), $event->getSql()]));
  117. }
  118. );
  119. $feedBack = new FeedBackHandler($eventSource, $l);
  120. $dispatcher->addListener(RepairStartEvent::class, [$feedBack, 'handleRepairFeedback']);
  121. $dispatcher->addListener(RepairAdvanceEvent::class, [$feedBack, 'handleRepairFeedback']);
  122. $dispatcher->addListener(RepairFinishEvent::class, [$feedBack, 'handleRepairFeedback']);
  123. $dispatcher->addListener(RepairStepEvent::class, [$feedBack, 'handleRepairFeedback']);
  124. $dispatcher->addListener(RepairInfoEvent::class, [$feedBack, 'handleRepairFeedback']);
  125. $dispatcher->addListener(RepairWarningEvent::class, [$feedBack, 'handleRepairFeedback']);
  126. $dispatcher->addListener(RepairErrorEvent::class, [$feedBack, 'handleRepairFeedback']);
  127. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
  128. $eventSource->send('success', $l->t('Turned on maintenance mode'));
  129. });
  130. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
  131. $eventSource->send('success', $l->t('Turned off maintenance mode'));
  132. });
  133. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
  134. $eventSource->send('success', $l->t('Maintenance mode is kept active'));
  135. });
  136. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource, $l) {
  137. $eventSource->send('success', $l->t('Updating database schema'));
  138. });
  139. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
  140. $eventSource->send('success', $l->t('Updated database'));
  141. });
  142. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource, $l) {
  143. $eventSource->send('success', $l->t('Update app "%s" from App Store', [$app]));
  144. });
  145. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
  146. $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]));
  147. });
  148. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
  149. $eventSource->send('success', $l->t('Updated "%1$s" to %2$s', [$app, $version]));
  150. });
  151. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps) {
  152. $incompatibleApps[] = $app;
  153. });
  154. $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource, $config) {
  155. $eventSource->send('failure', $message);
  156. $eventSource->close();
  157. $config->setSystemValue('maintenance', false);
  158. });
  159. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  160. $eventSource->send('success', $l->t('Set log level to debug'));
  161. });
  162. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  163. $eventSource->send('success', $l->t('Reset log level'));
  164. });
  165. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($eventSource, $l) {
  166. $eventSource->send('success', $l->t('Starting code integrity check'));
  167. });
  168. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($eventSource, $l) {
  169. $eventSource->send('success', $l->t('Finished code integrity check'));
  170. });
  171. try {
  172. $updater->upgrade();
  173. } catch (\Exception $e) {
  174. \OC::$server->getLogger()->logException($e, [
  175. 'level' => ILogger::ERROR,
  176. 'app' => 'update',
  177. ]);
  178. $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
  179. $eventSource->close();
  180. exit();
  181. }
  182. $disabledApps = [];
  183. foreach ($incompatibleApps as $app) {
  184. $disabledApps[$app] = $l->t('%s (incompatible)', [$app]);
  185. }
  186. if (!empty($disabledApps)) {
  187. $eventSource->send('notice', $l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
  188. }
  189. } else {
  190. $eventSource->send('notice', $l->t('Already up to date'));
  191. }
  192. $eventSource->send('done', '');
  193. $eventSource->close();