Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Refactor getFilteredXMLRegistry() function
Browse files Browse the repository at this point in the history
Refactor clearAllCaches() function
  • Loading branch information
cdujeu committed Jul 21, 2015
1 parent 2175034 commit b82edb5
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 103 deletions.
84 changes: 3 additions & 81 deletions core/src/core/classes/class.AJXP_Controller.php
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion core/src/core/classes/class.AJXP_PluginsService.php
Expand Up @@ -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");
}

/**
Expand Down
107 changes: 107 additions & 0 deletions core/src/core/classes/class.ConfService.php
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions core/src/plugins/action.updater/class.AjaXplorerUpgrader.php
Expand Up @@ -385,8 +385,7 @@ public function updateVersion()

public function clearCache()
{
AJXP_PluginsService::clearPluginsCache();
ConfService::clearMessagesCache();
ConfService::clearAllCaches();
return "Ok";
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/boot.conf/class.BootConfLoader.php
Expand Up @@ -307,7 +307,7 @@ public function applyInstallerForm($action, $httpVars, $fileVars)
}


AJXP_PluginsService::clearPluginsCache();
ConfService::clearAllCaches();
AJXP_Utils::setApplicationFirstRunPassed();

if(isSet($htAccessToUpdate)){
Expand Down
8 changes: 2 additions & 6 deletions core/src/plugins/core.conf/class.AbstractConfDriver.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions core/src/plugins/gui.ajax/class.AJXP_ClientDriver.php
Expand Up @@ -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) {
Expand Down

0 comments on commit b82edb5

Please sign in to comment.