update.php 9.9 KB

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