updater.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 $updateUrl 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'] = 'stable';
  51. $version['edition'] = \OC_Util::getEditionString();
  52. $versionString = implode('x', $version);
  53. //fetch xml data from updater
  54. $url = $updaterUrl . '?version=' . $versionString;
  55. // set a sensible timeout of 10 sec to stay responsive even if the update server is down.
  56. $ctx = stream_context_create(
  57. array(
  58. 'http' => array(
  59. 'timeout' => 10
  60. )
  61. )
  62. );
  63. $xml = @file_get_contents($url, 0, $ctx);
  64. if ($xml == false) {
  65. return array();
  66. }
  67. $data = @simplexml_load_string($xml);
  68. $tmp = array();
  69. $tmp['version'] = $data->version;
  70. $tmp['versionstring'] = $data->versionstring;
  71. $tmp['url'] = $data->url;
  72. $tmp['web'] = $data->web;
  73. // Cache the result
  74. \OC_Appconfig::setValue('core', 'lastupdateResult', json_encode($data));
  75. return $tmp;
  76. }
  77. /**
  78. * runs the update actions in maintenance mode, does not upgrade the source files
  79. */
  80. public function upgrade() {
  81. \OC_DB::enableCaching(false);
  82. \OC_Config::setValue('maintenance', true);
  83. $installedVersion = \OC_Config::getValue('version', '0.0.0');
  84. $currentVersion = implode('.', \OC_Util::getVersion());
  85. if ($this->log) {
  86. $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
  87. }
  88. $this->emit('\OC\Updater', 'maintenanceStart');
  89. try {
  90. \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
  91. $this->emit('\OC\Updater', 'dbUpgrade');
  92. // do a file cache upgrade for users with files
  93. // this can take loooooooooooooooooooooooong
  94. $this->upgradeFileCache();
  95. } catch (\Exception $exception) {
  96. $this->emit('\OC\Updater', 'failure', array($exception->getMessage()));
  97. }
  98. \OC_Config::setValue('version', implode('.', \OC_Util::getVersion()));
  99. \OC_App::checkAppsRequirements();
  100. // load all apps to also upgrade enabled apps
  101. \OC_App::loadApps();
  102. \OC_Config::setValue('maintenance', false);
  103. $this->emit('\OC\Updater', 'maintenanceEnd');
  104. }
  105. private function upgradeFileCache() {
  106. try {
  107. $query = \OC_DB::prepare('
  108. SELECT DISTINCT `user`
  109. FROM `*PREFIX*fscache`
  110. ');
  111. $result = $query->execute();
  112. } catch (\Exception $e) {
  113. return;
  114. }
  115. $users = $result->fetchAll();
  116. if (count($users) == 0) {
  117. return;
  118. }
  119. $step = 100 / count($users);
  120. $percentCompleted = 0;
  121. $lastPercentCompletedOutput = 0;
  122. $startInfoShown = false;
  123. foreach ($users as $userRow) {
  124. $user = $userRow['user'];
  125. \OC\Files\Filesystem::initMountPoints($user);
  126. \OC\Files\Cache\Upgrade::doSilentUpgrade($user);
  127. if (!$startInfoShown) {
  128. //We show it only now, because otherwise Info about upgraded apps
  129. //will appear between this and progress info
  130. $this->emit('\OC\Updater', 'filecacheStart');
  131. $startInfoShown = true;
  132. }
  133. $percentCompleted += $step;
  134. $out = floor($percentCompleted);
  135. if ($out != $lastPercentCompletedOutput) {
  136. $this->emit('\OC\Updater', 'filecacheProgress', array($out));
  137. $lastPercentCompletedOutput = $out;
  138. }
  139. }
  140. $this->emit('\OC\Updater', 'filecacheDone');
  141. }
  142. }