Skip to content

Commit

Permalink
MDL-60436 blocks: Improve performance of block loading
Browse files Browse the repository at this point in the history
  • Loading branch information
NeillM committed Nov 13, 2017
1 parent c0523b9 commit f01bcdc
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/blocklib.php
Expand Up @@ -689,7 +689,7 @@ public function load_blocks($includeinvisible = null) {
if ($includeinvisible) {
$visiblecheck = '';
} else {
$visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL)';
$visiblecheck = 'AND (bp.visible = 1 OR bp.visible IS NULL) AND (bs.visible = 1 OR bs.visible IS NULL)';
}

$context = $this->page->context;
Expand All @@ -710,24 +710,26 @@ public function load_blocks($includeinvisible = null) {
$ccjoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = bi.id AND ctx.contextlevel = :contextlevel)";

$systemcontext = context_system::instance();
list($bpcontext, $bpcontextidparams) = $DB->get_in_or_equal(array($context->id, $systemcontext->id),
SQL_PARAMS_NAMED, 'bpcontextid');
$params = array(
'contextlevel' => CONTEXT_BLOCK,
'subpage1' => $this->page->subpage,
'subpage2' => $this->page->subpage,
'subpage3' => $this->page->subpage,
'contextid1' => $context->id,
'contextid2' => $context->id,
'contextid3' => $systemcontext->id,
'contextid4' => $systemcontext->id,
'pagetype' => $this->page->pagetype,
'pagetype2' => $this->page->pagetype,
);
if ($this->page->subpage === '') {
$params['subpage1'] = '';
$params['subpage2'] = '';
$params['subpage3'] = '';
}
$sql = "SELECT
bi.id,
bp.id AS blockpositionid,
COALESCE(bp.id, bs.id) AS blockpositionid,
bi.blockname,
bi.parentcontextid,
bi.showinsubcontexts,
Expand All @@ -736,18 +738,22 @@ public function load_blocks($includeinvisible = null) {
bi.subpagepattern,
bi.defaultregion,
bi.defaultweight,
COALESCE(bp.visible, 1) AS visible,
COALESCE(bp.region, bi.defaultregion) AS region,
COALESCE(bp.weight, bi.defaultweight) AS weight,
COALESCE(bp.visible, bs.visible, 1) AS visible,
COALESCE(bp.region, bs.region, bi.defaultregion) AS region,
COALESCE(bp.weight, bs.weight, bi.defaultweight) AS weight,
bi.configdata
$ccselect
FROM {block_instances} bi
JOIN {block} b ON bi.blockname = b.name
LEFT JOIN {block_positions} bp ON bp.blockinstanceid = bi.id
AND bp.contextid $bpcontext
AND bp.contextid = :contextid1
AND bp.pagetype = :pagetype
AND bp.subpage = :subpage1
LEFT JOIN {block_positions} bs ON bs.blockinstanceid = bi.id
AND bs.contextid = :contextid4
AND bs.pagetype = :pagetype2
AND bs.subpage = :subpage3
$ccjoin
WHERE
Expand All @@ -759,12 +765,11 @@ public function load_blocks($includeinvisible = null) {
$requiredbythemecheck
ORDER BY
COALESCE(bp.region, bi.defaultregion),
COALESCE(bp.weight, bi.defaultweight),
COALESCE(bp.region, bs.region, bi.defaultregion),
COALESCE(bp.weight, bs.weight, bi.defaultweight),
bi.id";

$allparams = $params + $parentcontextparams + $pagetypepatternparams + $requiredbythemeparams;
$allparams = $allparams + $requiredbythemenotparams + $bpcontextidparams;
$allparams = $params + $parentcontextparams + $pagetypepatternparams + $requiredbythemeparams + $requiredbythemenotparams;
$blockinstances = $DB->get_recordset_sql($sql, $allparams);

$this->birecordsbyregion = $this->prepare_per_region_arrays();
Expand Down

0 comments on commit f01bcdc

Please sign in to comment.