From fbb36f89770ac0196efa646e55156deeabbaf584 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Wed, 2 Jun 2021 11:57:39 +1000 Subject: [PATCH] MDL-67822 report_performance: Add DB schema check --- lib/classes/check/manager.php | 1 + lib/classes/check/performance/dbschema.php | 85 +++++++++++++++++++ .../lang/en/report_performance.php | 3 + 3 files changed, 89 insertions(+) create mode 100644 lib/classes/check/performance/dbschema.php diff --git a/lib/classes/check/manager.php b/lib/classes/check/manager.php index 69ebcc65e9d..e4b35f88927 100644 --- a/lib/classes/check/manager.php +++ b/lib/classes/check/manager.php @@ -69,6 +69,7 @@ class manager { new performance\debugging(), new performance\backups(), new performance\stats(), + new performance\dbschema(), ]; // Any plugin can add status checks to this report by implementing a callback diff --git a/lib/classes/check/performance/dbschema.php b/lib/classes/check/performance/dbschema.php new file mode 100644 index 00000000000..cdbb8957938 --- /dev/null +++ b/lib/classes/check/performance/dbschema.php @@ -0,0 +1,85 @@ +. + +/** + * DB schema performance check + * + * @package core + * @category check + * @copyright 2021 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core\check\performance; + +defined('MOODLE_INTERNAL') || die(); + +use core\check\check; +use core\check\result; + +/** + * DB schema performance check + * + * @copyright 2021 Brendan Heywood + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class dbschema extends check { + + /** + * Get the short check name + * + * @return string + */ + public function get_name(): string { + return get_string('check_dbschema_name', 'report_performance'); + } + + /** + * A link to a place to action this + * + * @return action_link|null + */ + public function get_action_link(): ?\action_link { + return new \action_link( + new \moodle_url(\get_docs_url('Verify_Database_Schema')), + get_string('moodledocs')); + } + + /** + * Return result + * @return result + */ + public function get_result(): result { + global $DB; + + $dbmanager = $DB->get_manager(); + $schema = $dbmanager->get_install_xml_schema(); + + if (!$errors = $dbmanager->check_database_schema($schema)) { + return new result(result::OK, get_string('check_dbschema_ok', 'report_performance'), ''); + } + + $details = ''; + foreach ($errors as $tablename => $items) { + $details .= \html_writer::tag('h4', $tablename); + foreach ($items as $item) { + $details .= \html_writer::tag('pre', $item); + } + } + return new result(result::ERROR, get_string('check_dbschema_errors', 'report_performance'), $details); + } +} + diff --git a/report/performance/lang/en/report_performance.php b/report/performance/lang/en/report_performance.php index 57e08425943..f7172566f25 100644 --- a/report/performance/lang/en/report_performance.php +++ b/report/performance/lang/en/report_performance.php @@ -29,6 +29,9 @@ $string['check_backup_details'] = 'Enabling automated backup will automatically $string['check_cachejs_comment_disable'] = 'If enabled, page loading performance is improved.'; $string['check_cachejs_comment_enable'] = 'If disabled, page might load slow.'; $string['check_cachejs_details'] = 'Javascript caching and compression greatly improves page loading performance. It is strongly recommended for production sites.'; +$string['check_dbschema_name'] = 'Database schema check'; +$string['check_dbschema_ok'] = 'Database schema is correct.'; +$string['check_dbschema_errors'] = 'Database schema is not aligned.'; $string['check_debugmsg_comment_nodeveloper'] = 'If set to DEVELOPER, performance may be affected slightly.'; $string['check_debugmsg_comment_developer'] = 'If set to a value other than DEVELOPER, performance may be improved slightly.'; $string['check_debugmsg_details'] = 'There is rarely any advantage in going to Developer level, unless requested by a developer.

Once you have obtained the error message, and copied and pasted it somewhere, it is HIGHLY RECOMMENDED to turn Debug back to NONE. Debug messages can give clues to a hacker as to the setup of your site and may affect performance.

';