MDL-67428 navigation: Apply navigation text filters at system context

On some larger sites, processing the navigation with format_string will
habitually load all the contexts for navigation which can take 400+ DB
queries. Explicitly tying all those format_string calls to the system
context reduces this overhead to a single DB query that probably has
already been run on the page previously.

Co-authored-by: Peter Burnett <[email protected]>
This commit is contained in:
Peter Spicer
2022-03-04 14:36:25 +10:00
committed by Peter Burnett
co-authored by Peter Burnett
parent 1d99ba19a2
commit edde68e078
5 changed files with 64 additions and 13 deletions
+21 -12
View File
@@ -679,10 +679,13 @@ class navigation_node implements renderable {
* @return string
*/
public function get_content($shorttext=false) {
$navcontext = \context_helper::get_navigation_filter_context(null);
$options = !empty($navcontext) ? ['context' => $navcontext] : null;
if ($shorttext && $this->shorttext!==null) {
return format_string($this->shorttext);
return format_string($this->shorttext, null, $options);
} else {
return format_string($this->text);
return format_string($this->text, null, $options);
}
}
@@ -2097,13 +2100,14 @@ class global_navigation extends navigation_node {
* @return void.
*/
protected function add_category(stdClass $category, navigation_node $parent, $nodetype = self::TYPE_CATEGORY) {
global $CFG;
if (array_key_exists($category->id, $this->addedcategories)) {
return;
}
$canview = core_course_category::can_view_category($category);
$url = $canview ? new moodle_url('/course/index.php', array('categoryid' => $category->id)) : null;
$context = context_coursecat::instance($category->id);
$categoryname = $canview ? format_string($category->name, true, array('context' => $context)) :
$context = \context_helper::get_navigation_filter_context(context_coursecat::instance($category->id));
$categoryname = $canview ? format_string($category->name, true, ['context' => $context]) :
get_string('categoryhidden');
$categorynode = $parent->add($categoryname, $url, $nodetype, $categoryname, $category->id);
if (!$canview) {
@@ -2314,7 +2318,8 @@ class global_navigation extends navigation_node {
}
// Prepare the default name and url for the node
$activityname = format_string($activity->name, true, array('context' => context_module::instance($activity->id)));
$displaycontext = \context_helper::get_navigation_filter_context(context_module::instance($activity->id));
$activityname = format_string($activity->name, true, ['context' => $displaycontext]);
$action = new moodle_url($activity->url);
// Check if the onclick property is set (puke!)
@@ -2766,8 +2771,9 @@ class global_navigation extends navigation_node {
}
$issite = ($course->id == $SITE->id);
$shortname = format_string($course->shortname, true, array('context' => $coursecontext));
$fullname = format_string($course->fullname, true, array('context' => $coursecontext));
$displaycontext = \context_helper::get_navigation_filter_context($coursecontext);
$shortname = format_string($course->shortname, true, ['context' => $displaycontext]);
$fullname = format_string($course->fullname, true, ['context' => $displaycontext]);
// This is the name that will be shown for the course.
$coursename = empty($CFG->navshowfullcoursenames) ? $shortname : $fullname;
@@ -2822,7 +2828,7 @@ class global_navigation extends navigation_node {
$coursenode->showinflatnavigation = $coursetype == self::COURSE_MY;
$coursenode->hidden = (!$course->visible);
$coursenode->title(format_string($course->fullname, true, array('context' => $coursecontext, 'escape' => false)));
$coursenode->title(format_string($course->fullname, true, ['context' => $displaycontext, 'escape' => false]));
if ($canexpandcourse) {
// This course can be expanded by the user, make it a branch to make the system aware that its expandable by ajax.
$coursenode->nodetype = self::NODETYPE_BRANCH;
@@ -3784,8 +3790,10 @@ class navbar extends navigation_node {
if (!core_course_category::can_view_category($category)) {
continue;
}
$url = new moodle_url('/course/index.php', array('categoryid' => $category->id));
$name = format_string($category->name, true, array('context' => $context));
$displaycontext = \context_helper::get_navigation_filter_context($context);
$url = new moodle_url('/course/index.php', ['categoryid' => $category->id]);
$name = format_string($category->name, true, ['context' => $displaycontext]);
$categorynode = breadcrumb_navigation_node::create($name, $url, self::TYPE_CATEGORY, null, $category->id);
if (!$category->visible) {
$categorynode->hidden = true;
@@ -4138,10 +4146,11 @@ class flat_navigation extends navigation_node_collection {
$url = new moodle_url('/course/view.php', array('id' => $course->id));
$coursecontext = context_course::instance($course->id, MUST_EXIST);
$displaycontext = \context_helper::get_navigation_filter_context($coursecontext);
// This is the name that will be shown for the course.
$coursename = empty($CFG->navshowfullcoursenames) ?
format_string($course->shortname, true, array('context' => $coursecontext)) :
format_string($course->fullname, true, array('context' => $coursecontext));
format_string($course->shortname, true, ['context' => $displaycontext]) :
format_string($course->fullname, true, ['context' => $displaycontext]);
$flat = new flat_navigation_node(navigation_node::create($coursename, $url), 0);
$flat->set_collectionlabel($coursename);