From 02cca994d02eb53086bc35aacb11f6162c851adf Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Sun, 23 Jun 2013 23:40:26 +0100 Subject: [PATCH 1/1] MDL-40280: Improve performance of coursecat::make_categories_list --- lib/coursecatlib.php | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/coursecatlib.php b/lib/coursecatlib.php index ba549e1..6969ed3 100644 --- a/lib/coursecatlib.php +++ b/lib/coursecatlib.php @@ -1881,6 +1881,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { } else if ($baselist !== false) { $thislist = array_keys($baselist); } + $changedbase = false; if ($baselist === false) { // We don't have $baselist cached, retrieve it. Retrieve $thislist again in any case. @@ -1912,7 +1913,7 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { } } $rs->close(); - $coursecatcache->set($basecachekey, $baselist); + $changedbase = true; if (!empty($requiredcapability)) { $coursecatcache->set($thiscachekey, join(',', $thislist)); } @@ -1935,14 +1936,36 @@ class coursecat implements renderable, cacheable_object, IteratorAggregate { // Now build the array of strings to return, mind $separator and $excludeid. $names = array(); foreach ($thislist as $id) { - $path = preg_split('|/|', $baselist[$id]['path'], -1, PREG_SPLIT_NO_EMPTY); - if (!$excludeid || !in_array($excludeid, $path)) { + if (!isset($baselist[$id])) { + // This should not happen - find out why it does. + continue; + } + + if ($excludeid) { + if ($excludeid === $id) { + // This is an exact match. + continue; + } + if (strpos($baselist[$id]['path'], "/{$excludeid}/") !== false) { + // If an ID is surrounded by the separator, then it is a parent. + continue; + } + } + + if (!isset($baselist[$id]['namedpath'])) { + $path = preg_split('|/|', $baselist[$id]['path'], -1, PREG_SPLIT_NO_EMPTY); $namechunks = array(); foreach ($path as $parentid) { $namechunks[] = $baselist[$parentid]['name']; } - $names[$id] = join($separator, $namechunks); + $baselist[$id]['namedpath'] = join($separator, $namechunks); + $changedbase = true; } + $names[$id] = $baselist[$id]['namedpath']; + + } + if ($changedbase) { + $coursecatcache->set($basecachekey, $baselist); } return $names; } -- 1.8.0.1