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