Skip to content

Commit

Permalink
MDL_24125 admin tree fast category lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Feb 14, 2011
1 parent d911c72 commit cde44ed
Showing 1 changed file with 50 additions and 6 deletions.
56 changes: 50 additions & 6 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
*
Expand All @@ -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;
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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";
Expand All @@ -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();
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit cde44ed

Please sign in to comment.