update.php 9.0 KB

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