Permalink
Browse files

Merge branch 'w07_MDL-24125_20_admintreecache' of git://github.com/sk…

…odak/moodle
  • Loading branch information...
2 parents b180a11 + cde44ed commit aabd2f0064de48ff7602c465676afdab772f946a @stronk7 stronk7 committed Feb 15, 2011
Showing with 50 additions and 6 deletions.
  1. +50 −6 lib/adminlib.php
View
@@ -739,7 +739,7 @@ public function add($destinationname, $something);
*/
class admin_category implements parentable_part_of_admin_tree {
-/** @var mixed An array of part_of_admin_tree objects that are this object's children */
+ /** @var mixed An array of part_of_admin_tree objects that are this object's children */
public $children;
/** @var string An internal name for this category. Must be unique amongst ALL part_of_admin_tree objects */
public $name;
@@ -752,6 +752,9 @@ class admin_category implements parentable_part_of_admin_tree {
/** @var mixed Either a string or an array or strings */
public $visiblepath;
+ /** @var array fast lookup category cache, all categories of one tree point to one cache */
+ protected $category_cache;
+
/**
* Constructor for an empty admin category
*
@@ -775,6 +778,11 @@ public function __construct($name, $visiblename, $hidden=false) {
* defaults to false
*/
public function locate($name, $findpath=false) {
+ if (is_array($this->category_cache) and !isset($this->category_cache[$this->name])) {
+ // somebody much have purged the cache
+ $this->category_cache[$this->name] = $this;
+ }
+
if ($this->name == $name) {
if ($findpath) {
$this->visiblepath[] = $this->visiblename;
@@ -783,6 +791,11 @@ public function locate($name, $findpath=false) {
return $this;
}
+ // quick category lookup
+ if (!$findpath and is_array($this->category_cache) and isset($this->category_cache[$name])) {
+ return $this->category_cache[$name];
+ }
+
$return = NULL;
foreach($this->children as $childid=>$unused) {
if ($return = $this->children[$childid]->locate($name, $findpath)) {
@@ -831,11 +844,16 @@ public function prune($name) {
foreach($this->children as $precedence => $child) {
if ($child->name == $name) {
- // found it!
+ // clear cache and delete self
+ if (is_array($this->category_cache)) {
+ while($this->category_cache) {
+ // delete the cache, but keep the original array address
+ array_pop($this->category_cache);
+ }
+ }
unset($this->children[$precedence]);
return true;
- }
- if ($this->children[$precedence]->prune($name)) {
+ } else if ($this->children[$precedence]->prune($name)) {
return true;
}
}
@@ -862,6 +880,25 @@ public function add($parentname, $something) {
return false;
}
$parent->children[] = $something;
+ if (is_array($this->category_cache) and ($something instanceof admin_category)) {
+ if (isset($this->category_cache[$something->name])) {
+ debugging('Duplicate admin catefory name: '.$something->name);
+ } else {
+ $this->category_cache[$something->name] = $something;
+ $something->category_cache =& $this->category_cache;
+ foreach ($something->children as $child) {
+ // just in case somebody already added subcategories
+ if ($child instanceof admin_category) {
+ if (isset($this->category_cache[$child->name])) {
+ debugging('Duplicate admin catefory name: '.$child->name);
+ } else {
+ $this->category_cache[$child->name] = $child;
+ $child->category_cache =& $this->category_cache;
+ }
+ }
+ }
+ }
+ }
return true;
} else {
@@ -938,6 +975,8 @@ public function __construct($fulltree) {
$this->fulltree = $fulltree;
$this->loaded = false;
+ $this->category_cache = array();
+
// load custom defaults if found
$this->custom_defaults = null;
$defaultsfile = "$CFG->dirroot/local/defaults.php";
@@ -959,6 +998,11 @@ public function purge_children($requirefulltree) {
$this->children = array();
$this->fulltree = ($requirefulltree || $this->fulltree);
$this->loaded = false;
+ //break circular dependencies - this helps PHP 5.2
+ while($this->category_cache) {
+ array_pop($this->category_cache);
+ }
+ $this->category_cache = array();
}
}
@@ -5437,7 +5481,7 @@ public function search($query) {
* This function must be called on each admin page before other code.
*
* @global moodle_page $PAGE
- *
+ *
* @param string $section name of page
* @param string $extrabutton extra HTML that is added after the blocks editing on/off button.
* @param array $extraurlparams an array paramname => paramvalue, or parameters that need to be
@@ -5499,7 +5543,7 @@ function admin_externalpage_setup($section, $extrabutton = '', array $extraurlpa
$PAGE->set_cacheable(false);
return;
}
-
+
// Locate the current item on the navigation and make it active when found.
$path = $extpage->path;
$node = $PAGE->settingsnav;

0 comments on commit aabd2f0

Please sign in to comment.