updater.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC;
  9. use OC\Hooks\BasicEmitter;
  10. /**
  11. * Class that handles autoupdating of ownCloud
  12. *
  13. * Hooks provided in scope \OC\Updater
  14. * - maintenanceStart()
  15. * - maintenanceEnd()
  16. * - dbUpgrade()
  17. * - filecacheStart()
  18. * - filecacheProgress(int $percentage)
  19. * - filecacheDone()
  20. * - failure(string $message)
  21. */
  22. class Updater extends BasicEmitter {
  23. /**
  24. * @var \OC\Log $log
  25. */
  26. private $log;
  27. /**
  28. * @param \OC\Log $log
  29. */
  30. public function __construct($log = null) {
  31. $this->log = $log;
  32. }
  33. /**
  34. * Check if a new version is available
  35. * @param string $updaterUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php'
  36. * @return array | bool
  37. */
  38. public function check($updaterUrl) {
  39. // Look up the cache - it is invalidated all 30 minutes
  40. if ((\OC_Appconfig::getValue('core', 'lastupdatedat') + 1800) > time()) {
  41. return json_decode(\OC_Appconfig::getValue('core', 'lastupdateResult'), true);
  42. }
  43. \OC_Appconfig::setValue('core', 'lastupdatedat', time());
  44. if (\OC_Appconfig::getValue('core', 'installedat', '') == '') {
  45. \OC_Appconfig::setValue('core', 'installedat', microtime(true));
  46. }
  47. $version = \OC_Util::getVersion();
  48. $version['installed'] = \OC_Appconfig::getValue('core', 'installedat');
  49. $version['updated'] = \OC_Appconfig::getValue('core', 'lastupdatedat');
  50. $version['updatechannel'] = \OC_Util::getChannel();
  51. $version['edition'] = \OC_Util::getEditionString();
  52. $version['build'] = \OC_Util::getBuild();
  53. $versionString = implode('x', $version);
  54. //fetch xml data from updater
  55. $url = $updaterUrl . '?version=' . $versionString;
  56. // set a sensible timeout of 10 sec to stay responsive even if the update server is down.
  57. $ctx = stream_context_create(
  58. array(
  59. 'http' => array(
  60. 'timeout' => 10
  61. )
  62. )
  63. );
  64. $xml = @file_get_contents($url, 0, $ctx);
  65. if ($xml == false) {
  66. return array();
  67. }
  68. $data = @simplexml_load_string($xml);
  69. $tmp = array();
  70. $tmp['version'] = $data->version;
  71. $tmp['versionstring'] = $data->versionstring;
  72. $tmp['url'] = $data->url;
  73. $tmp['web'] = $data->web;
  74. // Cache the result
  75. \OC_Appconfig::setValue('core', 'lastupdateResult', json_encode($data));
  76. return $tmp;
  77. }
  78. /**
  79. * runs the update actions in maintenance mode, does not upgrade the source files
  80. */
  81. public function upgrade() {
  82. \OC_DB::enableCaching(false);
  83. \OC_Config::setValue('maintenance', true);
  84. $installedVersion = \OC_Config::getValue('version', '0.0.0');
  85. $currentVersion = implode('.', \OC_Util::getVersion());
  86. if ($this->log) {
  87. $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
  88. }
  89. $this->emit('\OC\Updater', 'maintenanceStart');
  90. try {
  91. \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  92. $this->emit('\OC\Updater', 'dbUpgrade');
  93. // do a file cache upgrade for users with files
  94. // this can take loooooooooooooooooooooooong
  95. $this->upgradeFileCache();
  96. } catch (\Exception $exception) {
  97. $this->emit('\OC\Updater', 'failure', array($exception->getMessage()));
  98. }
  99. \OC_Config::setValue('version', implode('.', \OC_Util::getVersion()));
  100. \OC_App::checkAppsRequirements();
  101. // load all apps to also upgrade enabled apps
  102. \OC_App::loadApps();
  103. $repair = new Repair();
  104. $repair->run();
  105. \OC_Config::setValue('maintenance', false);
  106. $this->emit('\OC\Updater', 'maintenanceEnd');
  107. }
  108. private function upgradeFileCache() {
  109. try {
  110. $query = \OC_DB::prepare('
  111. SELECT DISTINCT `user`
  112. FROM `*PREFIX*fscache`
  113. ');
  114. $result = $query->execute();
  115. } catch (\Exception $e) {
  116. return;
  117. }
  118. $users = $result->fetchAll();
  119. if (count($users) == 0) {
  120. return;
  121. }
  122. $step = 100 / count($users);
  123. $percentCompleted = 0;
  124. $lastPercentCompletedOutput = 0;
  125. $startInfoShown = false;
  126. foreach ($users as $userRow) {
  127. $user = $userRow['user'];
  128. \OC\Files\Filesystem::initMountPoints($user);
  129. \OC\Files\Cache\Upgrade::doSilentUpgrade($user);
  130. if (!$startInfoShown) {
  131. //We show it only now, because otherwise Info about upgraded apps
  132. //will appear between this and progress info
  133. $this->emit('\OC\Updater', 'filecacheStart');
  134. $startInfoShown = true;
  135. }
  136. $percentCompleted += $step;
  137. $out = floor($percentCompleted);
  138. if ($out != $lastPercentCompletedOutput) {
  139. $this->emit('\OC\Updater', 'filecacheProgress', array($out));
  140. $lastPercentCompletedOutput = $out;
  141. }
  142. }
  143. $this->emit('\OC\Updater', 'filecacheDone');
  144. }
  145. }