update.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <icewind@owncloud.com>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2016, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. use Symfony\Component\EventDispatcher\GenericEvent;
  30. set_time_limit(0);
  31. require_once '../../lib/base.php';
  32. $l = \OC::$server->getL10N('core');
  33. $eventSource = \OC::$server->createEventSource();
  34. // need to send an initial message to force-init the event source,
  35. // which will then trigger its own CSRF check and produces its own CSRF error
  36. // message
  37. $eventSource->send('success', (string)$l->t('Preparing update'));
  38. class FeedBackHandler {
  39. /** @var integer */
  40. private $progressStateMax = 100;
  41. /** @var integer */
  42. private $progressStateStep = 0;
  43. /** @var string */
  44. private $currentStep;
  45. public function __construct(\OCP\IEventSource $eventSource, \OCP\IL10N $l10n) {
  46. $this->eventSource = $eventSource;
  47. $this->l10n = $l10n;
  48. }
  49. public function handleRepairFeedback($event) {
  50. if (!$event instanceof GenericEvent) {
  51. return;
  52. }
  53. switch ($event->getSubject()) {
  54. case '\OC\Repair::startProgress':
  55. $this->progressStateMax = $event->getArgument(0);
  56. $this->progressStateStep = 0;
  57. $this->currentStep = $event->getArgument(1);
  58. break;
  59. case '\OC\Repair::advance':
  60. $this->progressStateStep += $event->getArgument(0);
  61. $desc = $event->getArgument(1);
  62. if (empty($desc)) {
  63. $desc = $this->currentStep;
  64. }
  65. $this->eventSource->send('success', (string)$this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $desc]));
  66. break;
  67. case '\OC\Repair::finishProgress':
  68. $this->progressStateMax = $this->progressStateStep;
  69. $this->eventSource->send('success', (string)$this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
  70. break;
  71. case '\OC\Repair::step':
  72. break;
  73. case '\OC\Repair::info':
  74. break;
  75. case '\OC\Repair::warning':
  76. $this->eventSource->send('notice', (string)$this->l10n->t('Repair warning: ') . $event->getArgument(0));
  77. break;
  78. case '\OC\Repair::error':
  79. $this->eventSource->send('notice', (string)$this->l10n->t('Repair error: ') . $event->getArgument(0));
  80. break;
  81. }
  82. }
  83. }
  84. if (OC::checkUpgrade(false)) {
  85. $config = \OC::$server->getSystemConfig();
  86. if ($config->getValue('upgrade.disable-web', false)) {
  87. $eventSource->send('failure', (string)$l->t('Please use the command line updater because automatic updating is disabled in the config.php.'));
  88. $eventSource->close();
  89. exit();
  90. }
  91. // if a user is currently logged in, their session must be ignored to
  92. // avoid side effects
  93. \OC_User::setIncognitoMode(true);
  94. $logger = \OC::$server->getLogger();
  95. $config = \OC::$server->getConfig();
  96. $updater = new \OC\Updater(
  97. $config,
  98. \OC::$server->getIntegrityCodeChecker(),
  99. $logger
  100. );
  101. $incompatibleApps = [];
  102. $disabledThirdPartyApps = [];
  103. $dispatcher = \OC::$server->getEventDispatcher();
  104. $dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($eventSource, $l) {
  105. if ($event instanceof GenericEvent) {
  106. $eventSource->send('success', (string)$l->t('[%d / %d]: %s', [$event[0], $event[1], $event->getSubject()]));
  107. }
  108. });
  109. $dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($eventSource, $l) {
  110. if ($event instanceof GenericEvent) {
  111. $eventSource->send('success', (string)$l->t('[%d / %d]: Checking table %s', [$event[0], $event[1], $event->getSubject()]));
  112. }
  113. });
  114. $feedBack = new FeedBackHandler($eventSource, $l);
  115. $dispatcher->addListener('\OC\Repair::startProgress', [$feedBack, 'handleRepairFeedback']);
  116. $dispatcher->addListener('\OC\Repair::advance', [$feedBack, 'handleRepairFeedback']);
  117. $dispatcher->addListener('\OC\Repair::finishProgress', [$feedBack, 'handleRepairFeedback']);
  118. $dispatcher->addListener('\OC\Repair::step', [$feedBack, 'handleRepairFeedback']);
  119. $dispatcher->addListener('\OC\Repair::info', [$feedBack, 'handleRepairFeedback']);
  120. $dispatcher->addListener('\OC\Repair::warning', [$feedBack, 'handleRepairFeedback']);
  121. $dispatcher->addListener('\OC\Repair::error', [$feedBack, 'handleRepairFeedback']);
  122. $updater->listen('\OC\Updater', 'maintenanceEnabled', function () use ($eventSource, $l) {
  123. $eventSource->send('success', (string)$l->t('Turned on maintenance mode'));
  124. });
  125. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
  126. $eventSource->send('success', (string)$l->t('Turned off maintenance mode'));
  127. });
  128. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
  129. $eventSource->send('success', (string)$l->t('Maintenance mode is kept active'));
  130. });
  131. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use($eventSource, $l) {
  132. $eventSource->send('success', (string)$l->t('Updating database schema'));
  133. });
  134. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
  135. $eventSource->send('success', (string)$l->t('Updated database'));
  136. });
  137. $updater->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($eventSource, $l) {
  138. $eventSource->send('success', (string)$l->t('Checking whether the database schema can be updated (this can take a long time depending on the database size)'));
  139. });
  140. $updater->listen('\OC\Updater', 'dbSimulateUpgrade', function () use ($eventSource, $l) {
  141. $eventSource->send('success', (string)$l->t('Checked database schema update'));
  142. });
  143. $updater->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($eventSource, $l) {
  144. $eventSource->send('success', (string)$l->t('Checking updates of apps'));
  145. });
  146. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
  147. $eventSource->send('success', (string)$l->t('Checking whether the database schema for %s can be updated (this can take a long time depending on the database size)', [$app]));
  148. });
  149. $updater->listen('\OC\Updater', 'appUpgradeCheck', function () use ($eventSource, $l) {
  150. $eventSource->send('success', (string)$l->t('Checked database schema update for apps'));
  151. });
  152. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
  153. $eventSource->send('success', (string)$l->t('Updated "%s" to %s', array($app, $version)));
  154. });
  155. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps) {
  156. $incompatibleApps[]= $app;
  157. });
  158. $updater->listen('\OC\Updater', 'thirdPartyAppDisabled', function ($app) use (&$disabledThirdPartyApps) {
  159. $disabledThirdPartyApps[]= $app;
  160. });
  161. $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource, $config) {
  162. $eventSource->send('failure', $message);
  163. $eventSource->close();
  164. $config->setSystemValue('maintenance', false);
  165. });
  166. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use($eventSource, $l) {
  167. $eventSource->send('success', (string)$l->t('Set log level to debug'));
  168. });
  169. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($eventSource, $l) {
  170. $eventSource->send('success', (string)$l->t('Reset log level'));
  171. });
  172. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($eventSource, $l) {
  173. $eventSource->send('success', (string)$l->t('Starting code integrity check'));
  174. });
  175. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($eventSource, $l) {
  176. $eventSource->send('success', (string)$l->t('Finished code integrity check'));
  177. });
  178. try {
  179. $updater->upgrade();
  180. } catch (\Exception $e) {
  181. $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
  182. $eventSource->close();
  183. exit();
  184. }
  185. $disabledApps = [];
  186. foreach ($disabledThirdPartyApps as $app) {
  187. $disabledApps[$app] = (string) $l->t('%s (3rdparty)', [$app]);
  188. }
  189. foreach ($incompatibleApps as $app) {
  190. $disabledApps[$app] = (string) $l->t('%s (incompatible)', [$app]);
  191. }
  192. if (!empty($disabledApps)) {
  193. $eventSource->send('notice',
  194. (string)$l->t('Following apps have been disabled: %s', implode(', ', $disabledApps)));
  195. }
  196. } else {
  197. $eventSource->send('notice', (string)$l->t('Already up to date'));
  198. }
  199. $eventSource->send('done', '');
  200. $eventSource->close();