update.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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\ILogger;
  34. use Symfony\Component\EventDispatcher\GenericEvent;
  35. if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
  36. @set_time_limit(0);
  37. }
  38. require_once '../../lib/base.php';
  39. $l = \OC::$server->getL10N('core');
  40. $eventSource = \OC::$server->createEventSource();
  41. // need to send an initial message to force-init the event source,
  42. // which will then trigger its own CSRF check and produces its own CSRF error
  43. // message
  44. $eventSource->send('success', $l->t('Preparing update'));
  45. class FeedBackHandler {
  46. /** @var integer */
  47. private $progressStateMax = 100;
  48. /** @var integer */
  49. private $progressStateStep = 0;
  50. /** @var string */
  51. private $currentStep;
  52. /** @var \OCP\IEventSource */
  53. private $eventSource;
  54. /** @var \OCP\IL10N */
  55. private $l10n;
  56. public function __construct(\OCP\IEventSource $eventSource, \OCP\IL10N $l10n) {
  57. $this->eventSource = $eventSource;
  58. $this->l10n = $l10n;
  59. }
  60. public function handleRepairFeedback($event) {
  61. if (!$event instanceof GenericEvent) {
  62. return;
  63. }
  64. switch ($event->getSubject()) {
  65. case '\OC\Repair::startProgress':
  66. $this->progressStateMax = $event->getArgument(0);
  67. $this->progressStateStep = 0;
  68. $this->currentStep = $event->getArgument(1);
  69. break;
  70. case '\OC\Repair::advance':
  71. $this->progressStateStep += $event->getArgument(0);
  72. $desc = $event->getArgument(1);
  73. if (empty($desc)) {
  74. $desc = $this->currentStep;
  75. }
  76. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
  77. break;
  78. case '\OC\Repair::finishProgress':
  79. $this->progressStateMax = $this->progressStateStep;
  80. $this->eventSource->send('success', $this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
  81. break;
  82. case '\OC\Repair::step':
  83. $this->eventSource->send('success', $this->l10n->t('Repair step:') . ' ' . $event->getArgument(0));
  84. break;
  85. case '\OC\Repair::info':
  86. $this->eventSource->send('success', $this->l10n->t('Repair info:') . ' ' . $event->getArgument(0));
  87. break;
  88. case '\OC\Repair::warning':
  89. $this->eventSource->send('notice', $this->l10n->t('Repair warning:') . ' ' . $event->getArgument(0));
  90. break;
  91. case '\OC\Repair::error':
  92. $this->eventSource->send('notice', $this->l10n->t('Repair error:') . ' ' . $event->getArgument(0));
  93. break;
  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 automatic updating is disabled in the 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. $logger = \OC::$server->get(\Psr\Log\LoggerInterface::class);
  108. $config = \OC::$server->getConfig();
  109. $updater = new \OC\Updater(
  110. $config,
  111. \OC::$server->getIntegrityCodeChecker(),
  112. $logger,
  113. \OC::$server->query(\OC\Installer::class)
  114. );
  115. $incompatibleApps = [];
  116. $dispatcher = \OC::$server->getEventDispatcher();
  117. $dispatcher->addListener('\OC\DB\Migrator::executeSql', function ($event) use ($eventSource, $l) {
  118. if ($event instanceof GenericEvent) {
  119. $eventSource->send('success', $l->t('[%d / %d]: %s', [$event[0], $event[1], $event->getSubject()]));
  120. }
  121. });
  122. $dispatcher->addListener('\OC\DB\Migrator::checkTable', function ($event) use ($eventSource, $l) {
  123. if ($event instanceof GenericEvent) {
  124. $eventSource->send('success', $l->t('[%d / %d]: Checking table %s', [$event[0], $event[1], $event->getSubject()]));
  125. }
  126. });
  127. $feedBack = new FeedBackHandler($eventSource, $l);
  128. $dispatcher->addListener('\OC\Repair::startProgress', [$feedBack, 'handleRepairFeedback']);
  129. $dispatcher->addListener('\OC\Repair::advance', [$feedBack, 'handleRepairFeedback']);
  130. $dispatcher->addListener('\OC\Repair::finishProgress', [$feedBack, 'handleRepairFeedback']);
  131. $dispatcher->addListener('\OC\Repair::step', [$feedBack, 'handleRepairFeedback']);
  132. $dispatcher->addListener('\OC\Repair::info', [$feedBack, 'handleRepairFeedback']);
  133. $dispatcher->addListener('\OC\Repair::warning', [$feedBack, 'handleRepairFeedback']);
  134. $dispatcher->addListener('\OC\Repair::error', [$feedBack, 'handleRepairFeedback']);
  135. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
  136. $eventSource->send('success', $l->t('Turned on maintenance mode'));
  137. });
  138. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
  139. $eventSource->send('success', $l->t('Turned off maintenance mode'));
  140. });
  141. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
  142. $eventSource->send('success', $l->t('Maintenance mode is kept active'));
  143. });
  144. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource, $l) {
  145. $eventSource->send('success', $l->t('Updating database schema'));
  146. });
  147. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
  148. $eventSource->send('success', $l->t('Updated database'));
  149. });
  150. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource, $l) {
  151. $eventSource->send('success', $l->t('Update app "%s" from App Store', [$app]));
  152. });
  153. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
  154. $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]));
  155. });
  156. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
  157. $eventSource->send('success', $l->t('Updated "%1$s" to %2$s', [$app, $version]));
  158. });
  159. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps) {
  160. $incompatibleApps[] = $app;
  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. \OC::$server->getLogger()->logException($e, [
  183. 'level' => ILogger::ERROR,
  184. 'app' => 'update',
  185. ]);
  186. $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
  187. $eventSource->close();
  188. exit();
  189. }
  190. $disabledApps = [];
  191. foreach ($incompatibleApps as $app) {
  192. $disabledApps[$app] = $l->t('%s (incompatible)', [$app]);
  193. }
  194. if (!empty($disabledApps)) {
  195. $eventSource->send('notice', $l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
  196. }
  197. } else {
  198. $eventSource->send('notice', $l->t('Already up to date'));
  199. }
  200. $eventSource->send('done', '');
  201. $eventSource->close();