]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-15249 support for environment test in cli scripts
authorskodak <skodak>
Wed, 3 Jun 2009 16:24:00 +0000 (16:24 +0000)
committerskodak <skodak>
Wed, 3 Jun 2009 16:24:00 +0000 (16:24 +0000)
admin/cli/install.php
admin/cli/upgrade.php
lib/environmentlib.php

index 3c52838b912188b95ec5894e7a6f19b43f0968be..c4b8e5eb7e5834ad190d2f722d92378d4293539e 100644 (file)
@@ -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
index eab613149f98597f9684952cfaa0000d96dbe771..f28ca6914a437cd00b27692591ef73ac08e65975 100644 (file)
@@ -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');
index 187c02cc9abc1db5e1a0b79676dc8b6d13a2825e..1d165d9568dc9ddffc8d95d9c3d45d97653e6c4e 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
-// This file is part of Moodle - http://moodle.org/ 
-// 
+// This file is part of Moodle - http://moodle.org/
+//
 // Moodle is free software: you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
 // the Free Software Foundation, either version 3 of the License, or
@@ -11,7 +11,7 @@
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.
-// 
+//
 // You should have received a copy of the GNU General Public License
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
 
 /// 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.