From b82edb56c8eceeea82dff0f6a02fd86aed2562c3 Mon Sep 17 00:00:00 2001 From: cdujeu Date: Tue, 21 Jul 2015 11:36:09 +0200 Subject: [PATCH] Refactor getFilteredXMLRegistry() function Refactor clearAllCaches() function --- .../core/classes/class.AJXP_Controller.php | 84 +------------- .../classes/class.AJXP_PluginsService.php | 3 +- core/src/core/classes/class.ConfService.php | 107 ++++++++++++++++++ .../class.ajxp_confAccessDriver.php | 10 +- .../class.AjaXplorerUpgrader.php | 3 +- .../boot.conf/class.BootConfLoader.php | 2 +- .../core.conf/class.AbstractConfDriver.php | 8 +- .../gui.ajax/class.AJXP_ClientDriver.php | 5 +- 8 files changed, 119 insertions(+), 103 deletions(-) diff --git a/core/src/core/classes/class.AJXP_Controller.php b/core/src/core/classes/class.AJXP_Controller.php index 06ce525be9..6f7d5bb77d 100644 --- a/core/src/core/classes/class.AJXP_Controller.php +++ b/core/src/core/classes/class.AJXP_Controller.php @@ -45,101 +45,23 @@ class AJXP_Controller /** * Initialize the queryable xPath object * @static + * @param bool $useCache Whether to cache the registry version in a memory cache. * @return DOMXPath */ - private static function initXPath($rest = false) + private static function initXPath($useCache = false) { if (!isSet(self::$xPath)) { - if($rest){ - $kvCache = ConfService::getInstance()->getKeyValueCache(); - $cacheKey = self::getRestRegistryCacheKey(); - $cachedXml = $kvCache->fetch($cacheKey); - if($cachedXml !== false){ - $registry = new DOMDocument("1.0", "utf-8"); - $registry->loadXML($cachedXml); - AJXP_PluginsService::updateXmlRegistry($registry); - self::$xPath = new DOMXPath($registry); - return self::$xPath; - } - } - $registry = AJXP_PluginsService::getXmlRegistry( false ); - $changes = self::filterRegistryFromRole($registry); - if($changes) AJXP_PluginsService::updateXmlRegistry($registry); + $registry = ConfService::getFilteredXMLRegistry(false, false, $useCache); self::$xPath = new DOMXPath($registry); - - if(isSet($kvCache) && isSet($cacheKey)){ - $kvCache->save($cacheKey, $registry->saveXML()); - } } return self::$xPath; } - private static function getRestRegistryCacheKey(){ - - $logged = AuthService::getLoggedUser(); - $u = $logged == null ? "shared" : $logged->getId(); - $r = ConfService::getRepository(); - $a = $r->getSlug(); - return "xml_registry:".$u.":".$a; - - } - public static function registryReset(){ self::$xPath = null; self::$hooksCache = array(); } - /** - * Check the current user "specificActionsRights" and filter the full registry actions with these. - * @static - * @param DOMDocument $registry - * @return bool - */ - public static function filterRegistryFromRole(&$registry) - { - if(!AuthService::usersEnabled()) return false ; - $loggedUser = AuthService::getLoggedUser(); - if($loggedUser == null) return false; - $crtRepo = ConfService::getRepository(); - $crtRepoId = AJXP_REPO_SCOPE_ALL; // "ajxp.all"; - if ($crtRepo != null && is_a($crtRepo, "Repository")) { - $crtRepoId = $crtRepo->getId(); - } - $actionRights = $loggedUser->mergedRole->listActionsStatesFor($crtRepo); - $changes = false; - $xPath = new DOMXPath($registry); - foreach ($actionRights as $pluginName => $actions) { - foreach ($actions as $actionName => $enabled) { - if($enabled !== false) continue; - $actions = $xPath->query("actions/action[@name='$actionName']"); - if (!$actions->length) { - continue; - } - $action = $actions->item(0); - $action->parentNode->removeChild($action); - $changes = true; - } - } - $parameters = $loggedUser->mergedRole->listParameters(); - foreach ($parameters as $scope => $paramsPlugs) { - if ($scope == AJXP_REPO_SCOPE_ALL || $scope == $crtRepoId || ($crtRepo!=null && $crtRepo->hasParent() && $scope == AJXP_REPO_SCOPE_SHARED)) { - foreach ($paramsPlugs as $plugId => $params) { - foreach ($params as $name => $value) { - // Search exposed plugin_configs, replace if necessary. - $searchparams = $xPath->query("plugins/*[@id='$plugId']/plugin_configs/property[@name='$name']"); - if(!$searchparams->length) continue; - $param = $searchparams->item(0); - $newCdata = $registry->createCDATASection(json_encode($value)); - $param->removeChild($param->firstChild); - $param->appendChild($newCdata); - } - } - } - } - return $changes; - } - - /** * @param $actionName * @param $path diff --git a/core/src/core/classes/class.AJXP_PluginsService.php b/core/src/core/classes/class.AJXP_PluginsService.php index 6b916ddb3e..d3121ee56c 100644 --- a/core/src/core/classes/class.AJXP_PluginsService.php +++ b/core/src/core/classes/class.AJXP_PluginsService.php @@ -637,10 +637,11 @@ public static function getXmlRegistry($extendedVersion = true) * @param $registry * @return void */ - public static function updateXmlRegistry($registry) + public static function updateXmlRegistry($registry, $extendedVersion = true) { $self = self::getInstance(); $self->xmlRegistry = $registry; + $self->registryVersion = ($extendedVersion? "extended" : "light"); } /** diff --git a/core/src/core/classes/class.ConfService.php b/core/src/core/classes/class.ConfService.php index 0b1c5f4449..abfb8c9885 100644 --- a/core/src/core/classes/class.ConfService.php +++ b/core/src/core/classes/class.ConfService.php @@ -167,6 +167,15 @@ public function getKeyValueCache(){ return $this->keyValueCache; } + public static function clearAllCaches(){ + AJXP_PluginsService::clearPluginsCache(); + self::clearMessagesCache(); + self::getInstance()->getKeyValueCache()->deleteAll(); + if(function_exists('opcache_reset')){ + opcache_reset(); + } + } + /** * @static * @param $globalsArray @@ -281,6 +290,104 @@ public static function getAuthDriverImpl() return AJXP_PluginsService::getInstance()->getPluginById("core.auth")->getAuthImpl(); } + public static function getFilteredXMLRegistry($extendedVersion = true, $clone = false, $useCache = false){ + + if($useCache){ + $kvCache = ConfService::getInstance()->getKeyValueCache(); + $cacheKey = self::getRegistryCacheKey($extendedVersion); + $cachedXml = $kvCache->fetch($cacheKey); + if($cachedXml !== false){ + $registry = new DOMDocument("1.0", "utf-8"); + $registry->loadXML($cachedXml); + AJXP_PluginsService::updateXmlRegistry($registry, $extendedVersion); + if($clone){ + return $registry->cloneNode(true); + }else{ + return $registry; + } + } + } + + $registry = AJXP_PluginsService::getXmlRegistry($extendedVersion); + $changes = self::filterRegistryFromRole($registry); + if($changes){ + AJXP_PluginsService::updateXmlRegistry($registry, $extendedVersion); + } + + if($useCache && isSet($kvCache) && isSet($cacheKey)){ + $kvCache->save($cacheKey, $registry->saveXML()); + } + + if($clone){ + $cloneDoc = $registry->cloneNode(true); + $registry = $cloneDoc; + } + return $registry; + + } + + private static function getRegistryCacheKey($extendedVersion = true){ + + $logged = AuthService::getLoggedUser(); + $u = $logged == null ? "shared" : $logged->getId(); + $r = ConfService::getRepository(); + $a = $r->getSlug(); + $v = $extendedVersion ? "extended":"light"; + return "xml_registry:".$v.":".$u.":".$a; + + } + + /** + * Check the current user "specificActionsRights" and filter the full registry actions with these. + * @static + * @param DOMDocument $registry + * @return bool + */ + public static function filterRegistryFromRole(&$registry) + { + if(!AuthService::usersEnabled()) return false ; + $loggedUser = AuthService::getLoggedUser(); + if($loggedUser == null) return false; + $crtRepo = ConfService::getRepository(); + $crtRepoId = AJXP_REPO_SCOPE_ALL; // "ajxp.all"; + if ($crtRepo != null && is_a($crtRepo, "Repository")) { + $crtRepoId = $crtRepo->getId(); + } + $actionRights = $loggedUser->mergedRole->listActionsStatesFor($crtRepo); + $changes = false; + $xPath = new DOMXPath($registry); + foreach ($actionRights as $pluginName => $actions) { + foreach ($actions as $actionName => $enabled) { + if($enabled !== false) continue; + $actions = $xPath->query("actions/action[@name='$actionName']"); + if (!$actions->length) { + continue; + } + $action = $actions->item(0); + $action->parentNode->removeChild($action); + $changes = true; + } + } + $parameters = $loggedUser->mergedRole->listParameters(); + foreach ($parameters as $scope => $paramsPlugs) { + if ($scope == AJXP_REPO_SCOPE_ALL || $scope == $crtRepoId || ($crtRepo!=null && $crtRepo->hasParent() && $scope == AJXP_REPO_SCOPE_SHARED)) { + foreach ($paramsPlugs as $plugId => $params) { + foreach ($params as $name => $value) { + // Search exposed plugin_configs, replace if necessary. + $searchparams = $xPath->query("plugins/*[@id='$plugId']/plugin_configs/property[@name='$name']"); + if(!$searchparams->length) continue; + $param = $searchparams->item(0); + $newCdata = $registry->createCDATASection(json_encode($value)); + $param->removeChild($param->firstChild); + $param->appendChild($newCdata); + } + } + } + } + return $changes; + } + + /** * @param AbstractAjxpUser $loggedUser * @param String|int $parameterId diff --git a/core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php b/core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php index 9af6a8cc13..690a82d274 100644 --- a/core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php +++ b/core/src/plugins/access.ajxp_conf/class.ajxp_confAccessDriver.php @@ -531,13 +531,7 @@ public function switchAction($action, $httpVars, $fileVars) case "clear_plugins_cache": AJXP_XMLWriter::header(); - // Clear plugins cache if they exist - AJXP_PluginsService::clearPluginsCache(); - ConfService::clearMessagesCache(); - ConfService::getInstance()->getKeyValueCache()->deleteAll(); - if(function_exists('opcache_reset')){ - opcache_reset(); - } + ConfService::clearAllCaches(); AJXP_XMLWriter::sendMessage($mess["ajxp_conf.".(AJXP_SKIP_CACHE?"132":"131")], null); AJXP_XMLWriter::reloadDataNode(); AJXP_XMLWriter::close(); @@ -1815,7 +1809,7 @@ public function switchAction($action, $httpVars, $fileVars) $existing = $confStorage->loadPluginConfig($pType, $pName); $this->mergeExistingParameters($options, $existing); $confStorage->savePluginConfig($httpVars["plugin_id"], $options); - AJXP_PluginsService::clearPluginsCache(); + ConfService::clearAllCaches(); AJXP_XMLWriter::header(); AJXP_XMLWriter::sendMessage($mess["ajxp_conf.97"], null); AJXP_XMLWriter::close(); diff --git a/core/src/plugins/action.updater/class.AjaXplorerUpgrader.php b/core/src/plugins/action.updater/class.AjaXplorerUpgrader.php index 57608f2889..eedff285f3 100644 --- a/core/src/plugins/action.updater/class.AjaXplorerUpgrader.php +++ b/core/src/plugins/action.updater/class.AjaXplorerUpgrader.php @@ -385,8 +385,7 @@ public function updateVersion() public function clearCache() { - AJXP_PluginsService::clearPluginsCache(); - ConfService::clearMessagesCache(); + ConfService::clearAllCaches(); return "Ok"; } diff --git a/core/src/plugins/boot.conf/class.BootConfLoader.php b/core/src/plugins/boot.conf/class.BootConfLoader.php index fd4fdbfdb6..78b91ce353 100644 --- a/core/src/plugins/boot.conf/class.BootConfLoader.php +++ b/core/src/plugins/boot.conf/class.BootConfLoader.php @@ -307,7 +307,7 @@ public function applyInstallerForm($action, $httpVars, $fileVars) } - AJXP_PluginsService::clearPluginsCache(); + ConfService::clearAllCaches(); AJXP_Utils::setApplicationFirstRunPassed(); if(isSet($htAccessToUpdate)){ diff --git a/core/src/plugins/core.conf/class.AbstractConfDriver.php b/core/src/plugins/core.conf/class.AbstractConfDriver.php index 0815378492..aeec18d8c2 100644 --- a/core/src/plugins/core.conf/class.AbstractConfDriver.php +++ b/core/src/plugins/core.conf/class.AbstractConfDriver.php @@ -555,7 +555,7 @@ public function getExposedPreferences($userObject) public function switchAction($action, $httpVars, $fileVars) { - if(!isSet($this->actions[$action])) return; + //if(!isSet($this->actions[$action])) return; $xmlBuffer = ""; foreach ($httpVars as $getName=>$getValue) { $$getName = AJXP_Utils::securePath($getValue); @@ -599,11 +599,7 @@ public function switchAction($action, $httpVars, $fileVars) case "get_xml_registry" : case "state" : - $regDoc = AJXP_PluginsService::getXmlRegistry(); - $changes = AJXP_Controller::filterRegistryFromRole($regDoc); - if($changes) AJXP_PluginsService::updateXmlRegistry($regDoc); - - $clone = $regDoc->cloneNode(true); + $clone = ConfService::getFilteredXMLRegistry(true, true); $clonePath = new DOMXPath($clone); $serverCallbacks = $clonePath->query("//serverCallback|hooks"); foreach ($serverCallbacks as $callback) { diff --git a/core/src/plugins/gui.ajax/class.AJXP_ClientDriver.php b/core/src/plugins/gui.ajax/class.AJXP_ClientDriver.php index bee5a6240d..93ee8f110a 100644 --- a/core/src/plugins/gui.ajax/class.AJXP_ClientDriver.php +++ b/core/src/plugins/gui.ajax/class.AJXP_ClientDriver.php @@ -193,10 +193,7 @@ public function switchAction($action, $httpVars, $fileVars) // PRECOMPUTE REGISTRY if (!isSet($START_PARAMETERS["FORCE_REGISTRY_RELOAD"])) { - $regDoc = AJXP_PluginsService::getXmlRegistry(); - $changes = AJXP_Controller::filterRegistryFromRole($regDoc); - if($changes) AJXP_PluginsService::updateXmlRegistry($regDoc); - $clone = $regDoc->cloneNode(true); + $clone = ConfService::getFilteredXMLRegistry(true, true); $clonePath = new DOMXPath($clone); $serverCallbacks = $clonePath->query("//serverCallback|hooks"); foreach ($serverCallbacks as $callback) {