update.php 9.0 KB

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