1
0

update.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Ko- <k.stoffelen@cs.ru.nl>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  16. * @author Vincent Petry <pvince81@owncloud.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', (string)$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', (string)$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', (string)$this->l10n->t('[%d / %d]: %s', [$this->progressStateStep, $this->progressStateMax, $this->currentStep]));
  81. break;
  82. case '\OC\Repair::step':
  83. $this->eventSource->send('success', (string)$this->l10n->t('Repair step:') . ' ' . $event->getArgument(0));
  84. break;
  85. case '\OC\Repair::info':
  86. $this->eventSource->send('success', (string)$this->l10n->t('Repair info:') . ' ' . $event->getArgument(0));
  87. break;
  88. case '\OC\Repair::warning':
  89. $this->eventSource->send('notice', (string)$this->l10n->t('Repair warning:') . ' ' . $event->getArgument(0));
  90. break;
  91. case '\OC\Repair::error':
  92. $this->eventSource->send('notice', (string)$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', (string)$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->getLogger();
  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', (string)$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', (string)$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', (string)$l->t('Turned on maintenance mode'));
  137. });
  138. $updater->listen('\OC\Updater', 'maintenanceDisabled', function () use ($eventSource, $l) {
  139. $eventSource->send('success', (string)$l->t('Turned off maintenance mode'));
  140. });
  141. $updater->listen('\OC\Updater', 'maintenanceActive', function () use ($eventSource, $l) {
  142. $eventSource->send('success', (string)$l->t('Maintenance mode is kept active'));
  143. });
  144. $updater->listen('\OC\Updater', 'dbUpgradeBefore', function () use ($eventSource, $l) {
  145. $eventSource->send('success', (string)$l->t('Updating database schema'));
  146. });
  147. $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource, $l) {
  148. $eventSource->send('success', (string)$l->t('Updated database'));
  149. });
  150. $updater->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use ($eventSource, $l) {
  151. $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)'));
  152. });
  153. $updater->listen('\OC\Updater', 'dbSimulateUpgrade', function () use ($eventSource, $l) {
  154. $eventSource->send('success', (string)$l->t('Checked database schema update'));
  155. });
  156. $updater->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($eventSource, $l) {
  157. $eventSource->send('success', (string)$l->t('Checking updates of apps'));
  158. });
  159. $updater->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use ($eventSource, $l) {
  160. $eventSource->send('success', (string)$l->t('Checking for update of app "%s" in appstore', [$app]));
  161. });
  162. $updater->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use ($eventSource, $l) {
  163. $eventSource->send('success', (string)$l->t('Update app "%s" from appstore', [$app]));
  164. });
  165. $updater->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use ($eventSource, $l) {
  166. $eventSource->send('success', (string)$l->t('Checked for update of app "%s" in appstore', [$app]));
  167. });
  168. $updater->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($eventSource, $l) {
  169. $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]));
  170. });
  171. $updater->listen('\OC\Updater', 'appUpgradeCheck', function () use ($eventSource, $l) {
  172. $eventSource->send('success', (string)$l->t('Checked database schema update for apps'));
  173. });
  174. $updater->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($eventSource, $l) {
  175. $eventSource->send('success', (string)$l->t('Updated "%1$s" to %2$s', [$app, $version]));
  176. });
  177. $updater->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use (&$incompatibleApps) {
  178. $incompatibleApps[]= $app;
  179. });
  180. $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource, $config) {
  181. $eventSource->send('failure', $message);
  182. $eventSource->close();
  183. $config->setSystemValue('maintenance', false);
  184. });
  185. $updater->listen('\OC\Updater', 'setDebugLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  186. $eventSource->send('success', (string)$l->t('Set log level to debug'));
  187. });
  188. $updater->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use ($eventSource, $l) {
  189. $eventSource->send('success', (string)$l->t('Reset log level'));
  190. });
  191. $updater->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use ($eventSource, $l) {
  192. $eventSource->send('success', (string)$l->t('Starting code integrity check'));
  193. });
  194. $updater->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use ($eventSource, $l) {
  195. $eventSource->send('success', (string)$l->t('Finished code integrity check'));
  196. });
  197. try {
  198. $updater->upgrade();
  199. } catch (\Exception $e) {
  200. \OC::$server->getLogger()->logException($e, [
  201. 'level' => ILogger::ERROR,
  202. 'app' => 'update',
  203. ]);
  204. $eventSource->send('failure', get_class($e) . ': ' . $e->getMessage());
  205. $eventSource->close();
  206. exit();
  207. }
  208. $disabledApps = [];
  209. foreach ($incompatibleApps as $app) {
  210. $disabledApps[$app] = (string) $l->t('%s (incompatible)', [$app]);
  211. }
  212. if (!empty($disabledApps)) {
  213. $eventSource->send('notice',
  214. (string)$l->t('The following apps have been disabled: %s', [implode(', ', $disabledApps)]));
  215. }
  216. } else {
  217. $eventSource->send('notice', (string)$l->t('Already up to date'));
  218. }
  219. $eventSource->send('done', '');
  220. $eventSource->close();