Blame view

sources/lib/private/updater.php 4.58 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
  <?php
  /**
   * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
31b7f2792   Kload   Upgrade to ownclo...
8
  namespace OC;
03e52840d   Kload   Init
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  use OC\Hooks\BasicEmitter;
  
  /**
   * Class that handles autoupdating of ownCloud
   *
   * Hooks provided in scope \OC\Updater
   *  - maintenanceStart()
   *  - maintenanceEnd()
   *  - dbUpgrade()
   *  - filecacheStart()
   *  - filecacheProgress(int $percentage)
   *  - filecacheDone()
   *  - failure(string $message)
   */
31b7f2792   Kload   Upgrade to ownclo...
23
24
25
26
27
28
29
30
31
32
33
34
35
  class Updater extends BasicEmitter {
  
  	/**
  	 * @var \OC\Log $log
  	 */
  	private $log;
  
  	/**
  	 * @param \OC\Log $log
  	 */
  	public function __construct($log = null) {
  		$this->log = $log;
  	}
03e52840d   Kload   Init
36
37
  	/**
  	 * Check if a new version is available
31b7f2792   Kload   Upgrade to ownclo...
38
  	 * @param string $updaterUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php'
03e52840d   Kload   Init
39
40
  	 * @return array | bool
  	 */
31b7f2792   Kload   Upgrade to ownclo...
41
42
43
  	public function check($updaterUrl) {
  
  		// Look up the cache - it is invalidated all 30 minutes
03e52840d   Kload   Init
44
45
46
  		if ((\OC_Appconfig::getValue('core', 'lastupdatedat') + 1800) > time()) {
  			return json_decode(\OC_Appconfig::getValue('core', 'lastupdateResult'), true);
  		}
31b7f2792   Kload   Upgrade to ownclo...
47

03e52840d   Kload   Init
48
  		\OC_Appconfig::setValue('core', 'lastupdatedat', time());
31b7f2792   Kload   Upgrade to ownclo...
49

03e52840d   Kload   Init
50
51
52
53
54
55
56
  		if (\OC_Appconfig::getValue('core', 'installedat', '') == '') {
  			\OC_Appconfig::setValue('core', 'installedat', microtime(true));
  		}
  
  		$version = \OC_Util::getVersion();
  		$version['installed'] = \OC_Appconfig::getValue('core', 'installedat');
  		$version['updated'] = \OC_Appconfig::getValue('core', 'lastupdatedat');
31b7f2792   Kload   Upgrade to ownclo...
57
  		$version['updatechannel'] = \OC_Util::getChannel(); 
03e52840d   Kload   Init
58
  		$version['edition'] = \OC_Util::getEditionString();
31b7f2792   Kload   Upgrade to ownclo...
59
  		$version['build'] = \OC_Util::getBuild();
03e52840d   Kload   Init
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  		$versionString = implode('x', $version);
  
  		//fetch xml data from updater
  		$url = $updaterUrl . '?version=' . $versionString;
  
  		// set a sensible timeout of 10 sec to stay responsive even if the update server is down.
  		$ctx = stream_context_create(
  			array(
  				'http' => array(
  					'timeout' => 10
  				)
  			)
  		);
  		$xml = @file_get_contents($url, 0, $ctx);
  		if ($xml == false) {
  			return array();
  		}
  		$data = @simplexml_load_string($xml);
  
  		$tmp = array();
  		$tmp['version'] = $data->version;
  		$tmp['versionstring'] = $data->versionstring;
  		$tmp['url'] = $data->url;
  		$tmp['web'] = $data->web;
31b7f2792   Kload   Upgrade to ownclo...
84
  		// Cache the result
03e52840d   Kload   Init
85
  		\OC_Appconfig::setValue('core', 'lastupdateResult', json_encode($data));
03e52840d   Kload   Init
86

31b7f2792   Kload   Upgrade to ownclo...
87
  		return $tmp;
03e52840d   Kload   Init
88
89
90
91
92
93
94
95
96
97
  	}
  
  	/**
  	 * runs the update actions in maintenance mode, does not upgrade the source files
  	 */
  	public function upgrade() {
  		\OC_DB::enableCaching(false);
  		\OC_Config::setValue('maintenance', true);
  		$installedVersion = \OC_Config::getValue('version', '0.0.0');
  		$currentVersion = implode('.', \OC_Util::getVersion());
31b7f2792   Kload   Upgrade to ownclo...
98
99
100
101
  		if ($this->log) {
  			$this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
  		}
  		$this->emit('\OC\Updater', 'maintenanceStart');
03e52840d   Kload   Init
102
103
  		try {
  			\OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml');
31b7f2792   Kload   Upgrade to ownclo...
104
  			$this->emit('\OC\Updater', 'dbUpgrade');
03e52840d   Kload   Init
105
106
107
108
109
  
  			// do a file cache upgrade for users with files
  			// this can take loooooooooooooooooooooooong
  			$this->upgradeFileCache();
  		} catch (\Exception $exception) {
31b7f2792   Kload   Upgrade to ownclo...
110
  			$this->emit('\OC\Updater', 'failure', array($exception->getMessage()));
03e52840d   Kload   Init
111
112
113
114
115
  		}
  		\OC_Config::setValue('version', implode('.', \OC_Util::getVersion()));
  		\OC_App::checkAppsRequirements();
  		// load all apps to also upgrade enabled apps
  		\OC_App::loadApps();
31b7f2792   Kload   Upgrade to ownclo...
116
117
118
  
  		$repair = new Repair();
  		$repair->run();
03e52840d   Kload   Init
119
  		\OC_Config::setValue('maintenance', false);
31b7f2792   Kload   Upgrade to ownclo...
120
  		$this->emit('\OC\Updater', 'maintenanceEnd');
03e52840d   Kload   Init
121
122
123
124
125
126
127
128
129
  	}
  
  	private function upgradeFileCache() {
  		try {
  			$query = \OC_DB::prepare('
  				SELECT DISTINCT `user`
  				FROM `*PREFIX*fscache`
  			');
  			$result = $query->execute();
03e52840d   Kload   Init
130
  		} catch (\Exception $e) {
03e52840d   Kload   Init
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  			return;
  		}
  		$users = $result->fetchAll();
  		if (count($users) == 0) {
  			return;
  		}
  		$step = 100 / count($users);
  		$percentCompleted = 0;
  		$lastPercentCompletedOutput = 0;
  		$startInfoShown = false;
  		foreach ($users as $userRow) {
  			$user = $userRow['user'];
  			\OC\Files\Filesystem::initMountPoints($user);
  			\OC\Files\Cache\Upgrade::doSilentUpgrade($user);
  			if (!$startInfoShown) {
  				//We show it only now, because otherwise Info about upgraded apps
  				//will appear between this and progress info
31b7f2792   Kload   Upgrade to ownclo...
148
  				$this->emit('\OC\Updater', 'filecacheStart');
03e52840d   Kload   Init
149
150
151
152
153
  				$startInfoShown = true;
  			}
  			$percentCompleted += $step;
  			$out = floor($percentCompleted);
  			if ($out != $lastPercentCompletedOutput) {
31b7f2792   Kload   Upgrade to ownclo...
154
  				$this->emit('\OC\Updater', 'filecacheProgress', array($out));
03e52840d   Kload   Init
155
156
157
  				$lastPercentCompletedOutput = $out;
  			}
  		}
31b7f2792   Kload   Upgrade to ownclo...
158
  		$this->emit('\OC\Updater', 'filecacheDone');
03e52840d   Kload   Init
159
  	}
31b7f2792   Kload   Upgrade to ownclo...
160
  }