From b4e5e47a0fd75bd5f70e16d40eb696453b962f73 Mon Sep 17 00:00:00 2001 From: Farhan Karmali Date: Wed, 21 Sep 2022 14:08:56 +0530 Subject: [PATCH] MDL-75667 admin: Avoid DB calls within loops on admin/blocks.php --- admin/blocks.php | 17 +++++++++++------ lib/db/install.xml | 3 ++- lib/db/upgrade.php | 13 +++++++++++++ version.php | 2 +- 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/admin/blocks.php b/admin/blocks.php index 19f5829a704..536a9e930f4 100644 --- a/admin/blocks.php +++ b/admin/blocks.php @@ -74,9 +74,15 @@ /// Get and sort the existing blocks - if (!$blocks = $DB->get_records('block', array(), 'name ASC')) { - throw new \moodle_exception('noblocks', 'error'); // Should never happen. - } +$sql = "SELECT b.* , COUNT(DISTINCT binst.id) as totalcount, COUNT(DISTINCT bcinst.id) as courseviewcount + FROM {block} b + LEFT JOIN {block_instances} binst ON binst.blockname = b.name + LEFT JOIN {block_instances} bcinst ON bcinst.blockname = b.name AND bcinst.pagetypepattern = 'course-view-*' + GROUP BY b.id, binst.blockname, bcinst.blockname + ORDER BY b.name ASC"; +if (!$blocks = $DB->get_records_sql($sql)) { + throw new \moodle_exception('noblocks', 'error'); // Should never happen. +} $incompatible = array(); @@ -154,9 +160,8 @@ // MDL-11167, blocks can be placed on mymoodle, or the blogs page // and it should not show up on course search page - $totalcount = $DB->count_records('block_instances', array('blockname'=>$blockname)); - $count = $DB->count_records('block_instances', array('blockname'=>$blockname, 'pagetypepattern'=>'course-view-*')); - + $totalcount = $blocks[$blockid]->totalcount; + $count = $blocks[$blockid]->courseviewcount; if ($count>0) { $blocklist = "wwwroot}/course/search.php?blocklist=$blockid&sesskey=".sesskey()."\" "; $blocklist .= "title=\"$strshowblockcourse\" >$totalcount"; diff --git a/lib/db/install.xml b/lib/db/install.xml index 518e510b245..baa18195b80 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -2747,6 +2747,7 @@ + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 8a6f5ac745a..7de36e44385 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -3051,5 +3051,18 @@ privatefiles,moodle|/user/files.php'; upgrade_main_savepoint(true, 2022120900.01); } + if ($oldversion < 2022121600.01) { + // Define index blocknameindex (not unique) to be added to block_instances. + $table = new xmldb_table('block_instances'); + $index = new xmldb_index('blocknameindex', XMLDB_INDEX_NOTUNIQUE, ['blockname']); + + // Conditionally launch add index blocknameindex. + if (!$dbman->index_exists($table, $index)) { + $dbman->add_index($table, $index); + } + // Main savepoint reached. + upgrade_main_savepoint(true, 2022121600.01); + } + return true; } diff --git a/version.php b/version.php index 377236398a3..cd7501ef38f 100644 --- a/version.php +++ b/version.php @@ -29,7 +29,7 @@ defined('MOODLE_INTERNAL') || die(); -$version = 2022121600.00; // YYYYMMDD = weekly release date of this DEV branch. +$version = 2022121600.01; // YYYYMMDD = weekly release date of this DEV branch. // RR = release increments - 00 in DEV branches. // .XX = incremental changes. $release = '4.2dev (Build: 20221216)'; // Human-friendly version name