diff --git a/admin/cli/install.php b/admin/cli/install.php index 3c52838b912..c4b8e5eb7e5 100644 --- a/admin/cli/install.php +++ b/admin/cli/install.php @@ -138,6 +138,7 @@ require_once($CFG->libdir.'/environmentlib.php'); require_once($CFG->libdir.'/xmlize.php'); require_once($CFG->libdir.'/componentlib.class.php'); require_once($CFG->libdir.'/upgradelib.php'); +require_once($CFG->libdir.'/environmentlib.php'); //Database types $databases = array('mysqli' => moodle_database::get_driver_instance('mysqli', 'native'), @@ -513,6 +514,19 @@ if ($DB->get_tables() ) { cli_error(get_string('clitablesexist', 'install')); } +// test environment first +if (!check_moodle_environment($version, $environment_results, false, ENV_SELECT_RELEASE)) { + $errors = environment_get_errors($environment_results); + cli_heading(get_string('environment', 'admin')); + foreach ($errors as $error) { + list($info, $report) = $error; + echo "!! $info !!\n$report\n\n"; + } + //remove config.php, we do not want half finished upgrades! + unlink($configfile); + exit(1); +} + if (!$DB->setup_is_unicodedb()) { if (!$DB->change_db_encoding()) { // If could not convert successfully, throw error, and prevent installation diff --git a/admin/cli/upgrade.php b/admin/cli/upgrade.php index eab613149f9..f28ca6914a4 100644 --- a/admin/cli/upgrade.php +++ b/admin/cli/upgrade.php @@ -36,9 +36,10 @@ if (isset($_SERVER['REMOTE_ADDR'])) { } require_once dirname(dirname(dirname(__FILE__))).'/config.php'; -require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions -require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions -require_once($CFG->libdir.'/clilib.php'); // cli only functions +require_once($CFG->libdir.'/adminlib.php'); // various admin-only functions +require_once($CFG->libdir.'/upgradelib.php'); // general upgrade/install related functions +require_once($CFG->libdir.'/clilib.php'); // cli only functions +require_once($CFG->libdir.'/environmentlib.php'); // now get cli options @@ -84,6 +85,17 @@ if ($version < $CFG->version) { $newversion = "$release ($version)"; +// test environment first +if (!check_moodle_environment($version, $environment_results, false, ENV_SELECT_RELEASE)) { + $errors = environment_get_errors($environment_results); + cli_heading(get_string('environment', 'admin')); + foreach ($errors as $error) { + list($info, $report) = $error; + echo "!! $info !!\n$report\n\n"; + } + exit(1); +} + if ($interactive) { echo html_to_text(get_string('upgradesure', 'admin', $newversion))."\n"; $prompt = get_string('cliyesnoprompt', 'admin'); diff --git a/lib/environmentlib.php b/lib/environmentlib.php index 187c02cc9ab..1d165d9568d 100644 --- a/lib/environmentlib.php +++ b/lib/environmentlib.php @@ -1,7 +1,7 @@ . @@ -70,11 +70,11 @@ /// Define algorithm used to select the xml file /** To select the newer file available to perform checks */ - define('ENV_SELECT_NEWER', 0); + define('ENV_SELECT_NEWER', 0); /** To enforce the use of the file under dataroot */ - define('ENV_SELECT_DATAROOT', 1); + define('ENV_SELECT_DATAROOT', 1); /** To enforce the use of the file under admin (release) */ - define('ENV_SELECT_RELEASE', 2); + define('ENV_SELECT_RELEASE', 2); /** * This function will perform the whole check, returning @@ -322,6 +322,101 @@ function print_moodle_environment($result, $environment_results) { } +/** + * Returns array of critical errors in plain text format + * @param array $environment_results array of results gathered + * @return array errors + */ +function environment_get_errors($environment_results) { + global $CFG; + $errors = array(); + + // Iterate over each environment_result + foreach ($environment_results as $environment_result) { + $type = $environment_result->getPart(); + $info = $environment_result->getInfo(); + $status = $environment_result->getStatus(); + $error_code = $environment_result->getErrorCode(); + + $a = new object(); + if ($error_code) { + $a->error_code = $error_code; + $errors[] = array($info, get_string('environmentxmlerror', 'admin', $a)); + return $errors; + } + + /// Calculate the status value + if ($environment_result->getBypassStr() != '') { + // not interesting + continue; + } else if ($environment_result->getRestrictStr() != '') { + // error + } else { + if ($status) { + // ok + continue; + } else { + if ($environment_result->getLevel() == 'optional') { + // just a warning + continue; + } else { + // error + } + } + } + + // We are comparing versions + if ($rec->needed = $environment_result->getNeededVersion()) { + $rec->current = $environment_result->getCurrentVersion(); + if ($environment_result->getLevel() == 'required') { + $stringtouse = 'environmentrequireversion'; + } else { + $stringtouse = 'environmentrecommendversion'; + } + // We are checking installed & enabled things + } else if ($environment_result->getPart() == 'custom_check') { + if ($environment_result->getLevel() == 'required') { + $stringtouse = 'environmentrequirecustomcheck'; + } else { + $stringtouse = 'environmentrecommendcustomcheck'; + } + } else if ($environment_result->getPart() == 'php_setting') { + if ($status) { + $stringtouse = 'environmentsettingok'; + } else if ($environment_result->getLevel() == 'required') { + $stringtouse = 'environmentmustfixsetting'; + } else { + $stringtouse = 'environmentshouldfixsetting'; + } + } else { + if ($environment_result->getLevel() == 'required') { + $stringtouse = 'environmentrequireinstall'; + } else { + $stringtouse = 'environmentrecommendinstall'; + } + } + $report = get_string($stringtouse, 'admin', $rec); + + // Here we'll store all the feedback found + $feedbacktext = ''; + // Append the feedback if there is some + $feedbacktext .= $environment_result->strToReport($environment_result->getFeedbackStr(), 'error'); + // Append the restrict if there is some + $feedbacktext .= $environment_result->strToReport($environment_result->getRestrictStr(), 'error'); + + $report .= html_to_text($feedbacktext); + + if ($environment_result->getPart() == 'custom_check'){ + $errors[] = array($info, $report); + } else { + $errors[] = array(($info !== '' ? "$type $info" : $type), $report); + } + } + + return $errors; +} + + /** * This function will normalize any version to just a serie of numbers * separated by dots. Everything else will be removed. @@ -330,7 +425,7 @@ function print_moodle_environment($result, $environment_results) { * @return string the normalized version */ function normalize_version($version) { - + /// 1.9 Beta 2 should be read 1.9 on enviromental checks, not 1.9.2 /// we can discard everything after the first space $version = trim($version); @@ -1386,7 +1481,7 @@ class environment_results { } /** - * @todo Document this function + * @todo Document this function * * @param mixed $string params for get_string, either a string to fetch from admin.php or an array of * params for get_string.