From a9470181d5d5ca0a18f4b0cec486b58832bab252 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 6 Jul 2013 17:00:00 +0200 Subject: backport: split upgrade logic from ajax file Conflicts: core/ajax/update.php lib/updater.php --- core/ajax/update.php | 128 +++++++++++---------------------------------------- 1 file changed, 27 insertions(+), 101 deletions(-) (limited to 'core') diff --git a/core/ajax/update.php b/core/ajax/update.php index 2b9275dabd5..43ed75b07f1 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -4,108 +4,34 @@ $RUNTIME_NOAPPS = true; require_once '../../lib/base.php'; if (OC::checkUpgrade(false)) { - \OC_DB::enableCaching(false); - - // initialize the event source before we enter maintenance mode because CSRF protection can terminate the script - $updateEventSource = new OC_EventSource(); - OC_Config::setValue('maintenance', true); - $installedVersion = OC_Config::getValue('version', '0.0.0'); - $currentVersion = implode('.', OC_Util::getVersion()); - OC_Log::write('core', 'starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, OC_Log::WARN); - $watcher = new UpdateWatcher($updateEventSource); - OC_Hook::connect('update', 'success', $watcher, 'success'); - OC_Hook::connect('update', 'failure', $watcher, 'failure'); - $watcher->success('Turned on maintenance mode'); - try { - $result = OC_DB::updateDbFromStructure(OC::$SERVERROOT.'/db_structure.xml'); - $watcher->success('Updated database'); - - // do a file cache upgrade for users with files - // this can take loooooooooooooooooooooooong - __doFileCacheUpgrade($watcher); - } catch (Exception $exception) { - $watcher->failure($exception->getMessage()); - } - OC_Config::setValue('version', implode('.', OC_Util::getVersion())); - OC_App::checkAppsRequirements(); - // load all apps to also upgrade enabled apps - OC_App::loadApps(); - OC_Config::setValue('maintenance', false); - $watcher->success('Turned off maintenance mode'); - $watcher->done(); -} - -/** - * The FileCache Upgrade routine - * - * @param UpdateWatcher $watcher - */ -function __doFileCacheUpgrade($watcher) { - try { - $query = \OC_DB::prepare(' - SELECT DISTINCT `user` - FROM `*PREFIX*fscache` - '); - $result = $query->execute(); - } catch (\Exception $e) { - 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 - $watcher->success('Updating filecache, this may take really long...'); - $startInfoShown = true; - } - $percentCompleted += $step; - $out = floor($percentCompleted); - if($out != $lastPercentCompletedOutput) { - $watcher->success('... '. $out.'% done ...'); - $lastPercentCompletedOutput = $out; - } - } - $watcher->success('Updated filecache'); -} - -class UpdateWatcher { - /** - * @var \OC_EventSource $eventSource; - */ - private $eventSource; - - public function __construct($eventSource) { - $this->eventSource = $eventSource; - } - - public function success($message) { - OC_Util::obEnd(); - $this->eventSource->send('success', $message); - ob_start(); - } - - public function failure($message) { - OC_Util::obEnd(); - $this->eventSource->send('failure', $message); - $this->eventSource->close(); + $eventSource = new OC_EventSource(); + $updater = new \OC\Updater(\OC_Log::$object); + $updater->listen('\OC\Updater', 'maintenanceStart', function () use ($eventSource) { + $eventSource->send('success', 'Turned on maintenance mode'); + }); + $updater->listen('\OC\Updater', 'maintenanceEnd', function () use ($eventSource) { + $eventSource->send('success', 'Turned off maintenance mode'); + }); + $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource) { + $eventSource->send('success', 'Updated database'); + }); + $updater->listen('\OC\Updater', 'filecacheStart', function () use ($eventSource) { + $eventSource->send('success', 'Updating filecache, this may take really long...'); + }); + $updater->listen('\OC\Updater', 'filecacheDone', function () use ($eventSource) { + $eventSource->send('success', 'Updated filecache'); + }); + $updater->listen('\OC\Updater', 'filecacheProgress', function ($out) use ($eventSource) { + $eventSource->send('success', '... ' . $out . '% done ...'); + }); + $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource) { + $eventSource->send('failure', $message); + $eventSource->close(); OC_Config::setValue('maintenance', false); - die(); - } + }); - public function done() { - OC_Util::obEnd(); - $this->eventSource->send('done', ''); - $this->eventSource->close(); - } + $updater->upgrade(); + $eventSource->send('done', ''); + $eventSource->close(); } -- cgit v1.2.3 From 9728192dc8a42a3408bbd62c3928cee838544fc6 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Fri, 12 Jul 2013 21:42:46 +0200 Subject: Stable5 adjustements (incl copy of lib/hooks/ from master) to make backport of #3963 work --- core/ajax/update.php | 5 +-- lib/hooks/basicemitter.php | 89 +++++++++++++++++++++++++++++++++++++++++ lib/hooks/emitter.php | 32 +++++++++++++++ lib/hooks/forwardingemitter.php | 50 +++++++++++++++++++++++ lib/hooks/legacyemitter.php | 16 ++++++++ lib/hooks/publicemitter.php | 20 +++++++++ lib/updater.php | 18 +-------- 7 files changed, 211 insertions(+), 19 deletions(-) create mode 100644 lib/hooks/basicemitter.php create mode 100644 lib/hooks/emitter.php create mode 100644 lib/hooks/forwardingemitter.php create mode 100644 lib/hooks/legacyemitter.php create mode 100644 lib/hooks/publicemitter.php (limited to 'core') diff --git a/core/ajax/update.php b/core/ajax/update.php index 43ed75b07f1..309bad819c8 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -5,7 +5,7 @@ require_once '../../lib/base.php'; if (OC::checkUpgrade(false)) { $eventSource = new OC_EventSource(); - $updater = new \OC\Updater(\OC_Log::$object); + $updater = new \OC\Updater(); $updater->listen('\OC\Updater', 'maintenanceStart', function () use ($eventSource) { $eventSource->send('success', 'Turned on maintenance mode'); }); @@ -29,9 +29,8 @@ if (OC::checkUpgrade(false)) { $eventSource->close(); OC_Config::setValue('maintenance', false); }); - $updater->upgrade(); $eventSource->send('done', ''); $eventSource->close(); -} +} \ No newline at end of file diff --git a/lib/hooks/basicemitter.php b/lib/hooks/basicemitter.php new file mode 100644 index 00000000000..9ffe1af2314 --- /dev/null +++ b/lib/hooks/basicemitter.php @@ -0,0 +1,89 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Hooks; + +abstract class BasicEmitter implements Emitter { + + /** + * @var (callable[])[] $listeners + */ + protected $listeners = array(); + + /** + * @param string $scope + * @param string $method + * @param callable $callback + */ + public function listen($scope, $method, $callback) { + $eventName = $scope . '::' . $method; + if (!isset($this->listeners[$eventName])) { + $this->listeners[$eventName] = array(); + } + if (array_search($callback, $this->listeners[$eventName]) === false) { + $this->listeners[$eventName][] = $callback; + } + } + + /** + * @param string $scope optional + * @param string $method optional + * @param callable $callback optional + */ + public function removeListener($scope = null, $method = null, $callback = null) { + $names = array(); + $allNames = array_keys($this->listeners); + if ($scope and $method) { + $name = $scope . '::' . $method; + if (isset($this->listeners[$name])) { + $names[] = $name; + } + } elseif ($scope) { + foreach ($allNames as $name) { + $parts = explode('::', $name, 2); + if ($parts[0] == $scope) { + $names[] = $name; + } + } + } elseif ($method) { + foreach ($allNames as $name) { + $parts = explode('::', $name, 2); + if ($parts[1] == $method) { + $names[] = $name; + } + } + } else { + $names = $allNames; + } + + foreach ($names as $name) { + if ($callback) { + $index = array_search($callback, $this->listeners[$name]); + if ($index !== false) { + unset($this->listeners[$name][$index]); + } + } else { + $this->listeners[$name] = array(); + } + } + } + + /** + * @param string $scope + * @param string $method + * @param array $arguments optional + */ + protected function emit($scope, $method, $arguments = array()) { + $eventName = $scope . '::' . $method; + if (isset($this->listeners[$eventName])) { + foreach ($this->listeners[$eventName] as $callback) { + call_user_func_array($callback, $arguments); + } + } + } +} diff --git a/lib/hooks/emitter.php b/lib/hooks/emitter.php new file mode 100644 index 00000000000..8e9074bad67 --- /dev/null +++ b/lib/hooks/emitter.php @@ -0,0 +1,32 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Hooks; + +/** + * Class Emitter + * + * interface for all classes that are able to emit events + * + * @package OC\Hooks + */ +interface Emitter { + /** + * @param string $scope + * @param string $method + * @param callable $callback + */ + public function listen($scope, $method, $callback); + + /** + * @param string $scope optional + * @param string $method optional + * @param callable $callback optional + */ + public function removeListener($scope = null, $method = null, $callback = null); +} diff --git a/lib/hooks/forwardingemitter.php b/lib/hooks/forwardingemitter.php new file mode 100644 index 00000000000..1aacc4012e0 --- /dev/null +++ b/lib/hooks/forwardingemitter.php @@ -0,0 +1,50 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Hooks; + +/** + * Class ForwardingEmitter + * + * allows forwarding all listen calls to other emitters + * + * @package OC\Hooks + */ +abstract class ForwardingEmitter extends BasicEmitter { + /** + * @var \OC\Hooks\Emitter[] array + */ + private $forwardEmitters = array(); + + /** + * @param string $scope + * @param string $method + * @param callable $callback + */ + public function listen($scope, $method, $callback) { + parent::listen($scope, $method, $callback); + foreach ($this->forwardEmitters as $emitter) { + $emitter->listen($scope, $method, $callback); + } + } + + /** + * @param \OC\Hooks\Emitter $emitter + */ + protected function forward($emitter) { + $this->forwardEmitters[] = $emitter; + + //forward all previously connected hooks + foreach ($this->listeners as $key => $listeners) { + list($scope, $method) = explode('::', $key, 2); + foreach ($listeners as $listener) { + $emitter->listen($scope, $method, $listener); + } + } + } +} diff --git a/lib/hooks/legacyemitter.php b/lib/hooks/legacyemitter.php new file mode 100644 index 00000000000..a2d16ace9a7 --- /dev/null +++ b/lib/hooks/legacyemitter.php @@ -0,0 +1,16 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Hooks; + +abstract class LegacyEmitter extends BasicEmitter { + protected function emit($scope, $method, $arguments = array()) { + \OC_Hook::emit($scope, $method, $arguments); + parent::emit($scope, $method, $arguments); + } +} diff --git a/lib/hooks/publicemitter.php b/lib/hooks/publicemitter.php new file mode 100644 index 00000000000..e2371713ac3 --- /dev/null +++ b/lib/hooks/publicemitter.php @@ -0,0 +1,20 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Hooks; + +class PublicEmitter extends BasicEmitter { + /** + * @param string $scope + * @param string $method + * @param array $arguments optional + */ + public function emit($scope, $method, $arguments = array()) { + parent::emit($scope, $method, $arguments); + } +} diff --git a/lib/updater.php b/lib/updater.php index 2bbdea36b0b..68b64b59b2d 100644 --- a/lib/updater.php +++ b/lib/updater.php @@ -22,19 +22,6 @@ use OC\Hooks\BasicEmitter; * - failure(string $message) */ class Updater extends BasicEmitter { - - /** - * @var \OC\Log $log - */ - private $log; - - /** - * @param \OC\Log $log - */ - public function __construct($log = null) { - $this->log = $log; - } - /** * Check if a new version is available * @param string $updateUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php' @@ -44,6 +31,7 @@ class Updater extends BasicEmitter { OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true)); if ((\OC_Appconfig::getValue('core', 'lastupdatedat') + 1800) > time()) { return json_decode(\OC_Appconfig::getValue('core', 'lastupdateResult'), true); + } \OC_Appconfig::setValue('core', 'lastupdatedat', time()); if (\OC_Appconfig::getValue('core', 'installedat', '') == '') { \OC_Appconfig::setValue('core', 'installedat', microtime(true)); @@ -91,9 +79,7 @@ class Updater extends BasicEmitter { \OC_Config::setValue('maintenance', true); $installedVersion = \OC_Config::getValue('version', '0.0.0'); $currentVersion = implode('.', \OC_Util::getVersion()); - if ($this->log) { - $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core')); - } + \OC_Log::write('core', 'starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, \OC_Log::WARN); $this->emit('\OC\Updater', 'maintenanceStart'); try { \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml'); -- cgit v1.2.3 From 5b18f2fc0e76dedaec00ab020b57c1fe380ccd97 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 17 Jul 2013 17:19:59 +0200 Subject: Rerename \OC\Updater to OC_Updater, fixes broken admin page --- core/ajax/update.php | 16 ++++++++-------- lib/legacy/updater.php | 14 -------------- lib/updater.php | 39 ++++++++++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 33 deletions(-) delete mode 100644 lib/legacy/updater.php (limited to 'core') diff --git a/core/ajax/update.php b/core/ajax/update.php index 309bad819c8..d530a4c7b6d 100644 --- a/core/ajax/update.php +++ b/core/ajax/update.php @@ -5,26 +5,26 @@ require_once '../../lib/base.php'; if (OC::checkUpgrade(false)) { $eventSource = new OC_EventSource(); - $updater = new \OC\Updater(); - $updater->listen('\OC\Updater', 'maintenanceStart', function () use ($eventSource) { + $updater = new OC_Updater(); + $updater->listen('\OC_Updater', 'maintenanceStart', function () use ($eventSource) { $eventSource->send('success', 'Turned on maintenance mode'); }); - $updater->listen('\OC\Updater', 'maintenanceEnd', function () use ($eventSource) { + $updater->listen('\OC_Updater', 'maintenanceEnd', function () use ($eventSource) { $eventSource->send('success', 'Turned off maintenance mode'); }); - $updater->listen('\OC\Updater', 'dbUpgrade', function () use ($eventSource) { + $updater->listen('\OC_Updater', 'dbUpgrade', function () use ($eventSource) { $eventSource->send('success', 'Updated database'); }); - $updater->listen('\OC\Updater', 'filecacheStart', function () use ($eventSource) { + $updater->listen('\OC_Updater', 'filecacheStart', function () use ($eventSource) { $eventSource->send('success', 'Updating filecache, this may take really long...'); }); - $updater->listen('\OC\Updater', 'filecacheDone', function () use ($eventSource) { + $updater->listen('\OC_Updater', 'filecacheDone', function () use ($eventSource) { $eventSource->send('success', 'Updated filecache'); }); - $updater->listen('\OC\Updater', 'filecacheProgress', function ($out) use ($eventSource) { + $updater->listen('\OC_Updater', 'filecacheProgress', function ($out) use ($eventSource) { $eventSource->send('success', '... ' . $out . '% done ...'); }); - $updater->listen('\OC\Updater', 'failure', function ($message) use ($eventSource) { + $updater->listen('\OC_Updater', 'failure', function ($message) use ($eventSource) { $eventSource->send('failure', $message); $eventSource->close(); OC_Config::setValue('maintenance', false); diff --git a/lib/legacy/updater.php b/lib/legacy/updater.php deleted file mode 100644 index eea7bb129cf..00000000000 --- a/lib/legacy/updater.php +++ /dev/null @@ -1,14 +0,0 @@ - - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -class OC_Updater { - public static function check() { - $updater = new \OC\Updater(); - return $updater->check('http://apps.owncloud.com/updater.php'); - } -} diff --git a/lib/updater.php b/lib/updater.php index 68b64b59b2d..93a8c49128e 100644 --- a/lib/updater.php +++ b/lib/updater.php @@ -6,7 +6,6 @@ * See the COPYING-README file. */ -namespace OC; use OC\Hooks\BasicEmitter; /** @@ -21,13 +20,13 @@ use OC\Hooks\BasicEmitter; * - filecacheDone() * - failure(string $message) */ -class Updater extends BasicEmitter { +class OC_Updater extends BasicEmitter { /** * Check if a new version is available * @param string $updateUrl the url to check, i.e. 'http://apps.owncloud.com/updater.php' * @return array | bool */ - public function check($updaterUrl) { + static public function check($updaterUrl='http://apps.owncloud.com/updater.php') { OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true)); if ((\OC_Appconfig::getValue('core', 'lastupdatedat') + 1800) > time()) { return json_decode(\OC_Appconfig::getValue('core', 'lastupdateResult'), true); @@ -71,6 +70,24 @@ class Updater extends BasicEmitter { return $tmp; } + public static function ShowUpdatingHint() { + $l = OC_L10N::get('lib'); + + if(OC_Config::getValue('updatechecker', true)==true) { + $data=OC_Updater::check(); + if(isset($data['version']) and $data['version']<>'') { + $txt='' + .$l->t('%s is available. Get more information', + array($data['versionstring'], $data['web'])).''; + }else{ + $txt=$l->t('up to date'); + } + } else { + $txt=$l->t('updates check is disabled'); + } + return($txt); + } + /** * runs the update actions in maintenance mode, does not upgrade the source files */ @@ -80,23 +97,23 @@ class Updater extends BasicEmitter { $installedVersion = \OC_Config::getValue('version', '0.0.0'); $currentVersion = implode('.', \OC_Util::getVersion()); \OC_Log::write('core', 'starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, \OC_Log::WARN); - $this->emit('\OC\Updater', 'maintenanceStart'); + $this->emit('\OC_Updater', 'maintenanceStart'); try { \OC_DB::updateDbFromStructure(\OC::$SERVERROOT . '/db_structure.xml'); - $this->emit('\OC\Updater', 'dbUpgrade'); + $this->emit('\OC_Updater', 'dbUpgrade'); // do a file cache upgrade for users with files // this can take loooooooooooooooooooooooong $this->upgradeFileCache(); } catch (\Exception $exception) { - $this->emit('\OC\Updater', 'failure', array($exception->getMessage())); + $this->emit('\OC_Updater', 'failure', array($exception->getMessage())); } \OC_Config::setValue('version', implode('.', \OC_Util::getVersion())); \OC_App::checkAppsRequirements(); // load all apps to also upgrade enabled apps \OC_App::loadApps(); \OC_Config::setValue('maintenance', false); - $this->emit('\OC\Updater', 'maintenanceEnd'); + $this->emit('\OC_Updater', 'maintenanceEnd'); } private function upgradeFileCache() { @@ -124,16 +141,16 @@ class Updater extends BasicEmitter { if (!$startInfoShown) { //We show it only now, because otherwise Info about upgraded apps //will appear between this and progress info - $this->emit('\OC\Updater', 'filecacheStart'); + $this->emit('\OC_Updater', 'filecacheStart'); $startInfoShown = true; } $percentCompleted += $step; $out = floor($percentCompleted); if ($out != $lastPercentCompletedOutput) { - $this->emit('\OC\Updater', 'filecacheProgress', array($out)); + $this->emit('\OC_Updater', 'filecacheProgress', array($out)); $lastPercentCompletedOutput = $out; } } - $this->emit('\OC\Updater', 'filecacheDone'); + $this->emit('\OC_Updater', 'filecacheDone'); } -} +} \ No newline at end of file -- cgit v1.2.3