. namespace core; use core\exception\moodle_exception; /** * Core setup functionality for Moodle. * * @package core * @copyright Andrew Lyons * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class setup { /** * Check the validity of the wwwroot, throwing a Moodle exception if invalid. * * @throws moodle_exception * @return true */ public function validate_wwwroot(): bool { if ($this->does_wwwroot_end_in_slash()) { // The wwwroot should not end in a slash as this may suggest a misconfiguration. throw new moodle_exception('wwwrootslash', 'error'); } if ($this->does_wwwroot_end_in_public()) { // The wwwroot should not end in /public as this may suggest a misconfiguration. // There may be legitimate sites out there that currently do this but it is not recommended. throw new moodle_exception('wwwrootpublic', 'error'); } return true; } /** * Detect whether the wwwroot ends in /public. * * @return bool */ protected function does_wwwroot_end_in_public(): bool { global $CFG; return substr($CFG->wwwroot, -7) == '/public'; } /** * Detect whether the wwwroot ends in /. * * @return bool */ protected function does_wwwroot_end_in_slash(): bool { global $CFG; return str_ends_with($CFG->wwwroot, '/'); } /** * Check whether an upgrade is currently running. * * @return bool */ public static function is_upgrade_running(): bool { global $CFG; return !empty($CFG->upgraderunning); } /** * Ensure that an upgrade is not running, emitting an exception if it is. * * @throws moodle_exception * @return bool false if no upgrade is running */ public static function ensure_upgrade_is_not_running(): bool { if (self::is_upgrade_running()) { throw new moodle_exception('cannotexecduringupgrade'); } return false; } /** * Warn if an upgrade is currently running. * * @return bool true if an upgrade is running, false if no upgrade is running */ public static function warn_if_upgrade_is_running(): bool { if (self::is_upgrade_running()) { debugging(get_string('cannotexecduringupgrade', 'error'), DEBUG_DEVELOPER); return true; } return false; } }