3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * This library includes all the necessary stuff to execute some standard
20 * tests of required versions and libraries to run Moodle. It can be
21 * used from the admin interface, and both at install and upgrade.
23 * All the info is stored in the admin/environment.xml file,
24 * supporting to have an updated version in dataroot/environment
26 * @copyright (C) 2001-3001 Eloy Lafuente (stronk7) {@link http://contiento.com}
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32 defined('MOODLE_INTERNAL') ||
die();
34 /// Add required files
36 * Include the necessary
38 require_once($CFG->libdir
.'/xmlize.php');
40 /// Define a bunch of XML processing errors
41 /** XML Processing Error */
42 define('NO_ERROR', 0);
43 /** XML Processing Error */
44 define('NO_VERSION_DATA_FOUND', 1);
45 /** XML Processing Error */
46 define('NO_DATABASE_SECTION_FOUND', 2);
47 /** XML Processing Error */
48 define('NO_DATABASE_VENDORS_FOUND', 3);
49 /** XML Processing Error */
50 define('NO_DATABASE_VENDOR_MYSQL_FOUND', 4);
51 /** XML Processing Error */
52 define('NO_DATABASE_VENDOR_POSTGRES_FOUND', 5);
53 /** XML Processing Error */
54 define('NO_PHP_SECTION_FOUND', 6);
55 /** XML Processing Error */
56 define('NO_PHP_VERSION_FOUND', 7);
57 /** XML Processing Error */
58 define('NO_PHP_EXTENSIONS_SECTION_FOUND', 8);
59 /** XML Processing Error */
60 define('NO_PHP_EXTENSIONS_NAME_FOUND', 9);
61 /** XML Processing Error */
62 define('NO_DATABASE_VENDOR_VERSION_FOUND', 10);
63 /** XML Processing Error */
64 define('NO_UNICODE_SECTION_FOUND', 11);
65 /** XML Processing Error */
66 define('NO_CUSTOM_CHECK_FOUND', 12);
67 /** XML Processing Error */
68 define('CUSTOM_CHECK_FILE_MISSING', 13);
69 /** XML Processing Error */
70 define('CUSTOM_CHECK_FUNCTION_MISSING', 14);
71 /** XML Processing Error */
72 define('NO_PHP_SETTINGS_NAME_FOUND', 15);
74 /// Define algorithm used to select the xml file
75 /** To select the newer file available to perform checks */
76 define('ENV_SELECT_NEWER', 0);
77 /** To enforce the use of the file under dataroot */
78 define('ENV_SELECT_DATAROOT', 1);
79 /** To enforce the use of the file under admin (release) */
80 define('ENV_SELECT_RELEASE', 2);
83 * This function checks all the requirements defined in environment.xml.
85 * @staticvar bool $result
86 * @staticvar array $env_results
87 * @staticvar bool $cache_exists
89 * @param string $version version to check.
90 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC)
91 * @return array with two elements. The first element true/false, depending on
92 * on whether the check passed. The second element is an array of environment_results
93 * objects that has detailed information about the checks and which ones passed.
95 function check_moodle_environment($version, $env_select = ENV_SELECT_NEWER
) {
99 /// This are cached per request
100 static $result = true;
102 static $cache_exists = false;
104 /// if we have results cached, use them
106 $environment_results = $env_results;
107 /// No cache exists, calculate everything
109 /// Get the more recent version before the requested
110 if (!$version = get_latest_version_available($version, $env_select)) {
114 /// Perform all the checks
115 if (!($environment_results = environment_check($version, $env_select)) && $status) {
119 /// Iterate over all the results looking for some error in required items
120 /// or some error_code
122 foreach ($environment_results as $environment_result) {
123 if (!$environment_result->getStatus() && $environment_result->getLevel() == 'required'
124 && !$environment_result->getBypassStr()) {
125 $result = false; // required item that is not bypased
126 } else if ($environment_result->getStatus() && $environment_result->getLevel() == 'required'
127 && $environment_result->getRestrictStr()) {
128 $result = false; // required item that is restricted
129 } else if ($environment_result->getErrorCode()) {
134 /// Going to end, we store environment_results to cache
135 $env_results = $environment_results;
136 $cache_exists = true;
137 } ///End of cache block
139 return array($result && $status, $environment_results);
144 * Returns array of critical errors in plain text format
145 * @param array $environment_results array of results gathered
146 * @return array errors
148 function environment_get_errors($environment_results) {
152 // Iterate over each environment_result
153 foreach ($environment_results as $environment_result) {
154 $type = $environment_result->getPart();
155 $info = $environment_result->getInfo();
156 $status = $environment_result->getStatus();
157 $error_code = $environment_result->getErrorCode();
161 $a->error_code
= $error_code;
162 $errors[] = array($info, get_string('environmentxmlerror', 'admin', $a));
166 /// Calculate the status value
167 if ($environment_result->getBypassStr() != '') {
170 } else if ($environment_result->getRestrictStr() != '') {
177 if ($environment_result->getLevel() == 'optional') {
186 // We are comparing versions
187 $rec = new stdClass();
188 if ($rec->needed
= $environment_result->getNeededVersion()) {
189 $rec->current
= $environment_result->getCurrentVersion();
190 if ($environment_result->getLevel() == 'required') {
191 $stringtouse = 'environmentrequireversion';
193 $stringtouse = 'environmentrecommendversion';
195 // We are checking installed & enabled things
196 } else if ($environment_result->getPart() == 'custom_check') {
197 if ($environment_result->getLevel() == 'required') {
198 $stringtouse = 'environmentrequirecustomcheck';
200 $stringtouse = 'environmentrecommendcustomcheck';
202 } else if ($environment_result->getPart() == 'php_setting') {
204 $stringtouse = 'environmentsettingok';
205 } else if ($environment_result->getLevel() == 'required') {
206 $stringtouse = 'environmentmustfixsetting';
208 $stringtouse = 'environmentshouldfixsetting';
211 if ($environment_result->getLevel() == 'required') {
212 $stringtouse = 'environmentrequireinstall';
214 $stringtouse = 'environmentrecommendinstall';
217 $report = get_string($stringtouse, 'admin', $rec);
219 // Here we'll store all the feedback found
221 // Append the feedback if there is some
222 $feedbacktext .= $environment_result->strToReport($environment_result->getFeedbackStr(), 'error');
223 // Append the restrict if there is some
224 $feedbacktext .= $environment_result->strToReport($environment_result->getRestrictStr(), 'error');
226 $report .= html_to_text($feedbacktext);
228 if ($environment_result->getPart() == 'custom_check'){
229 $errors[] = array($info, $report);
231 $errors[] = array(($info !== '' ?
"$type $info" : $type), $report);
240 * This function will normalize any version to just a serie of numbers
241 * separated by dots. Everything else will be removed.
243 * @param string $version the original version
244 * @return string the normalized version
246 function normalize_version($version) {
248 /// 1.9 Beta 2 should be read 1.9 on enviromental checks, not 1.9.2
249 /// we can discard everything after the first space
250 $version = trim($version);
251 $versionarr = explode(" ",$version);
252 if (!empty($versionarr)) {
253 $version = $versionarr[0];
255 /// Replace everything but numbers and dots by dots
256 $version = preg_replace('/[^\.\d]/', '.', $version);
257 /// Combine multiple dots in one
258 $version = preg_replace('/(\.{2,})/', '.', $version);
259 /// Trim possible leading and trailing dots
260 $version = trim($version, '.');
267 * This function will load the environment.xml file and xmlize it
270 * @staticvar mixed $data
271 * @uses ENV_SELECT_NEWER
272 * @uses ENV_SELECT_DATAROOT
273 * @uses ENV_SELECT_RELEASE
274 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use. Default ENV_SELECT_NEWER (BC)
275 * @return mixed the xmlized structure or false on error
277 function load_environment_xml($env_select=ENV_SELECT_NEWER
) {
281 static $data; //Only load and xmlize once by request
287 /// First of all, take a look inside $CFG->dataroot/environment/environment.xml
288 $file = $CFG->dataroot
.'/environment/environment.xml';
289 $internalfile = $CFG->dirroot
.'/'.$CFG->admin
.'/environment.xml';
290 switch ($env_select) {
291 case ENV_SELECT_NEWER
:
292 if (!is_file($file) ||
!is_readable($file) ||
filemtime($file) < filemtime($internalfile) ||
293 !$contents = file_get_contents($file)) {
294 /// Fallback to fixed $CFG->admin/environment.xml
295 if (!is_file($internalfile) ||
!is_readable($internalfile) ||
!$contents = file_get_contents($internalfile)) {
300 case ENV_SELECT_DATAROOT
:
301 if (!is_file($file) ||
!is_readable($file) ||
!$contents = file_get_contents($file)) {
305 case ENV_SELECT_RELEASE
:
306 if (!is_file($internalfile) ||
!is_readable($internalfile) ||
!$contents = file_get_contents($internalfile)) {
311 /// XML the whole file
312 $data = xmlize($contents);
319 * This function will return the list of Moodle versions available
321 * @staticvar array $versions
322 * @return mixed array of versions. False on error.
324 function get_list_of_environment_versions ($contents) {
326 static $versions = array();
328 if (!empty($versions)) {
332 if (isset($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'])) {
333 foreach ($contents['COMPATIBILITY_MATRIX']['#']['MOODLE'] as $version) {
334 $versions[] = $version['@']['version'];
343 * This function will return the most recent version in the environment.xml
344 * file previous or equal to the version requested
346 * @param string $version top version from which we start to look backwards
347 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
348 * @return string|bool string more recent version or false if not found
350 function get_latest_version_available ($version, $env_select) {
352 /// Normalize the version requested
353 $version = normalize_version($version);
356 if (!$contents = load_environment_xml($env_select)) {
360 /// Detect available versions
361 if (!$versions = get_list_of_environment_versions($contents)) {
364 /// First we look for exact version
365 if (in_array($version, $versions)) {
368 $found_version = false;
369 /// Not exact match, so we are going to iterate over the list searching
370 /// for the latest version before the requested one
371 foreach ($versions as $arrversion) {
372 if (version_compare($arrversion, $version, '<')) {
373 $found_version = $arrversion;
378 return $found_version;
383 * This function will return the xmlized data belonging to one Moodle version
385 * @param string $version top version from which we start to look backwards
386 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
387 * @return mixed the xmlized structure or false on error
389 function get_environment_for_version($version, $env_select) {
391 /// Normalize the version requested
392 $version = normalize_version($version);
395 if (!$contents = load_environment_xml($env_select)) {
399 /// Detect available versions
400 if (!$versions = get_list_of_environment_versions($contents)) {
404 /// If the version requested is available
405 if (!in_array($version, $versions)) {
409 /// We now we have it. Extract from full contents.
410 $fl_arr = array_flip($versions);
412 return $contents['COMPATIBILITY_MATRIX']['#']['MOODLE'][$fl_arr[$version]];
417 * This function will check for everything (DB, PHP and PHP extensions for now)
418 * returning an array of environment_result objects.
421 * @param string $version xml version we are going to use to test this server
422 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
423 * @return array array of results encapsulated in one environment_result object
425 function environment_check($version, $env_select) {
428 /// Normalize the version requested
429 $version = normalize_version($version);
431 $results = array(); //To store all the results
433 /// Only run the moodle versions checker on upgrade, not on install
434 if (!empty($CFG->version
)) {
435 $results[] = environment_check_moodle($version, $env_select);
437 $results[] = environment_check_unicode($version, $env_select);
438 $results[] = environment_check_database($version, $env_select);
439 $results[] = environment_check_php($version, $env_select);
441 $phpext_results = environment_check_php_extensions($version, $env_select);
442 $results = array_merge($results, $phpext_results);
444 $phpsetting_results = environment_check_php_settings($version, $env_select);
445 $results = array_merge($results, $phpsetting_results);
447 $custom_results = environment_custom_checks($version, $env_select);
448 $results = array_merge($results, $custom_results);
455 * This function will check if php extensions requirements are satisfied
457 * @uses NO_VERSION_DATA_FOUND
458 * @uses NO_PHP_EXTENSIONS_SECTION_FOUND
459 * @uses NO_PHP_EXTENSIONS_NAME_FOUND
460 * @param string $version xml version we are going to use to test this server
461 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
462 * @return array array of results encapsulated in one environment_result object
464 function environment_check_php_extensions($version, $env_select) {
468 /// Get the enviroment version we need
469 if (!$data = get_environment_for_version($version, $env_select)) {
470 /// Error. No version data found
471 $result = new environment_results('php_extension');
472 $result->setStatus(false);
473 $result->setErrorCode(NO_VERSION_DATA_FOUND
);
474 return array($result);
477 /// Extract the php_extension part
478 if (!isset($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'])) {
479 /// Error. No PHP section found
480 $result = new environment_results('php_extension');
481 $result->setStatus(false);
482 $result->setErrorCode(NO_PHP_EXTENSIONS_SECTION_FOUND
);
483 return array($result);
485 /// Iterate over extensions checking them and creating the needed environment_results
486 foreach($data['#']['PHP_EXTENSIONS']['0']['#']['PHP_EXTENSION'] as $extension) {
487 $result = new environment_results('php_extension');
489 $level = get_level($extension);
490 /// Check for extension name
491 if (!isset($extension['@']['name'])) {
492 $result->setStatus(false);
493 $result->setErrorCode(NO_PHP_EXTENSIONS_NAME_FOUND
);
495 $extension_name = $extension['@']['name'];
496 /// The name exists. Just check if it's an installed extension
497 if (!extension_loaded($extension_name)) {
498 $result->setStatus(false);
500 $result->setStatus(true);
502 $result->setLevel($level);
503 $result->setInfo($extension_name);
506 /// Do any actions defined in the XML file.
507 process_environment_result($extension, $result);
509 /// Add the result to the array of results
510 $results[] = $result;
518 * This function will check if php extensions requirements are satisfied
520 * @uses NO_VERSION_DATA_FOUND
521 * @uses NO_PHP_SETTINGS_NAME_FOUND
522 * @param string $version xml version we are going to use to test this server
523 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
524 * @return array array of results encapsulated in one environment_result object
526 function environment_check_php_settings($version, $env_select) {
530 /// Get the enviroment version we need
531 if (!$data = get_environment_for_version($version, $env_select)) {
532 /// Error. No version data found
533 $result = new environment_results('php_setting');
534 $result->setStatus(false);
535 $result->setErrorCode(NO_VERSION_DATA_FOUND
);
536 $results[] = $result;
540 /// Extract the php_setting part
541 if (!isset($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'])) {
542 /// No PHP section found - ignore
545 /// Iterate over settings checking them and creating the needed environment_results
546 foreach($data['#']['PHP_SETTINGS']['0']['#']['PHP_SETTING'] as $setting) {
547 $result = new environment_results('php_setting');
549 $level = get_level($setting);
550 $result->setLevel($level);
551 /// Check for extension name
552 if (!isset($setting['@']['name'])) {
553 $result->setStatus(false);
554 $result->setErrorCode(NO_PHP_SETTINGS_NAME_FOUND
);
556 $setting_name = $setting['@']['name'];
557 $setting_value = $setting['@']['value'];
558 $result->setInfo($setting_name);
560 if ($setting_name == 'memory_limit') {
561 $current = ini_get('memory_limit');
562 if ($current == -1) {
563 $result->setStatus(true);
565 $current = get_real_size($current);
566 $minlimit = get_real_size($setting_value);
567 if ($current < $minlimit) {
568 @ini_set
('memory_limit', $setting_value);
569 $current = ini_get('memory_limit');
570 $current = get_real_size($current);
572 $result->setStatus($current >= $minlimit);
576 $current = ini_get_bool($setting_name);
577 /// The name exists. Just check if it's an installed extension
578 if ($current == $setting_value) {
579 $result->setStatus(true);
581 $result->setStatus(false);
586 /// Do any actions defined in the XML file.
587 process_environment_result($setting, $result);
589 /// Add the result to the array of results
590 $results[] = $result;
598 * This function will do the custom checks.
601 * @uses CUSTOM_CHECK_FUNCTION_MISSING
602 * @uses CUSTOM_CHECK_FILE_MISSING
603 * @uses NO_CUSTOM_CHECK_FOUND
604 * @param string $version xml version we are going to use to test this server.
605 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
606 * @return array array of results encapsulated in environment_result objects.
608 function environment_custom_checks($version, $env_select) {
613 /// Get current Moodle version (release) for later compare
614 $release = isset($CFG->release
) ?
$CFG->release
: $version; /// In case $CFG fails (at install) use $version
615 $current_version = normalize_version($release);
617 /// Get the enviroment version we need
618 if (!$data = get_environment_for_version($version, $env_select)) {
619 /// Error. No version data found - but this will already have been reported.
623 /// Extract the CUSTOM_CHECKS part
624 if (!isset($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'])) {
625 /// No custom checks found - not a problem
629 /// Iterate over extensions checking them and creating the needed environment_results
630 foreach($data['#']['CUSTOM_CHECKS']['0']['#']['CUSTOM_CHECK'] as $check) {
631 $result = new environment_results('custom_check');
634 $level = get_level($check);
636 /// Check for extension name
637 if (isset($check['@']['file']) && isset($check['@']['function'])) {
638 $file = $CFG->dirroot
. '/' . $check['@']['file'];
639 $function = $check['@']['function'];
640 if (is_readable($file)) {
642 if (function_exists($function)) {
643 $result->setLevel($level);
644 $result->setInfo($function);
645 $result = $function($result);
647 /// Only show error for current version (where function MUST exist)
648 /// else, we are performing custom checks against future versiosn
649 /// and function MAY not exist, so it doesn't cause error, just skip
650 /// custom check by returning null. MDL-15939
651 if (version_compare($current_version, $version, '>=')) {
652 $result->setStatus(false);
653 $result->setInfo($function);
654 $result->setErrorCode(CUSTOM_CHECK_FUNCTION_MISSING
);
660 /// Only show error for current version (where function MUST exist)
661 /// else, we are performing custom checks against future versiosn
662 /// and function MAY not exist, so it doesn't cause error, just skip
663 /// custom check by returning null. MDL-15939
664 if (version_compare($current_version, $version, '>=')) {
665 $result->setStatus(false);
666 $result->setInfo($function);
667 $result->setErrorCode(CUSTOM_CHECK_FILE_MISSING
);
673 $result->setStatus(false);
674 $result->setErrorCode(NO_CUSTOM_CHECK_FOUND
);
677 if (!is_null($result)) {
678 /// Do any actions defined in the XML file.
679 process_environment_result($check, $result);
681 /// Add the result to the array of results
682 $results[] = $result;
690 * This function will check if Moodle requirements are satisfied
692 * @uses NO_VERSION_DATA_FOUND
693 * @param string $version xml version we are going to use to test this server
694 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
695 * @return object results encapsulated in one environment_result object
697 function environment_check_moodle($version, $env_select) {
699 $result = new environment_results('moodle');
701 /// Get the enviroment version we need
702 if (!$data = get_environment_for_version($version, $env_select)) {
703 /// Error. No version data found
704 $result->setStatus(false);
705 $result->setErrorCode(NO_VERSION_DATA_FOUND
);
709 /// Extract the moodle part
710 if (!isset($data['@']['requires'])) {
711 $needed_version = '1.0'; /// Default to 1.0 if no moodle requires is found
713 /// Extract required moodle version
714 $needed_version = $data['@']['requires'];
717 /// Now search the version we are using
718 $current_version = normalize_version(get_config('', 'release'));
720 /// And finally compare them, saving results
721 if (version_compare($current_version, $needed_version, '>=')) {
722 $result->setStatus(true);
724 $result->setStatus(false);
726 $result->setLevel('required');
727 $result->setCurrentVersion($current_version);
728 $result->setNeededVersion($needed_version);
734 * This function will check if php requirements are satisfied
736 * @uses NO_VERSION_DATA_FOUND
737 * @uses NO_PHP_SECTION_FOUND
738 * @uses NO_PHP_VERSION_FOUND
739 * @param string $version xml version we are going to use to test this server
740 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
741 * @return object results encapsulated in one environment_result object
743 function environment_check_php($version, $env_select) {
745 $result = new environment_results('php');
747 /// Get the enviroment version we need
748 if (!$data = get_environment_for_version($version, $env_select)) {
749 /// Error. No version data found
750 $result->setStatus(false);
751 $result->setErrorCode(NO_VERSION_DATA_FOUND
);
755 /// Extract the php part
756 if (!isset($data['#']['PHP'])) {
757 /// Error. No PHP section found
758 $result->setStatus(false);
759 $result->setErrorCode(NO_PHP_SECTION_FOUND
);
762 /// Extract level and version
763 $level = get_level($data['#']['PHP']['0']);
764 if (!isset($data['#']['PHP']['0']['@']['version'])) {
765 $result->setStatus(false);
766 $result->setErrorCode(NO_PHP_VERSION_FOUND
);
769 $needed_version = $data['#']['PHP']['0']['@']['version'];
773 /// Now search the version we are using
774 $current_version = normalize_version(phpversion());
776 /// And finally compare them, saving results
777 if (version_compare($current_version, $needed_version, '>=')) {
778 $result->setStatus(true);
780 $result->setStatus(false);
782 $result->setLevel($level);
783 $result->setCurrentVersion($current_version);
784 $result->setNeededVersion($needed_version);
786 /// Do any actions defined in the XML file.
787 process_environment_result($data['#']['PHP'][0], $result);
794 * This function will check if unicode database requirements are satisfied
797 * @uses NO_VERSION_DATA_FOUND
798 * @uses NO_UNICODE_SECTION_FOUND
799 * @param string $version xml version we are going to use to test this server
800 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
801 * @return object results encapsulated in one environment_result object
803 function environment_check_unicode($version, $env_select) {
806 $result = new environment_results('unicode');
808 /// Get the enviroment version we need
809 if (!$data = get_environment_for_version($version, $env_select)) {
810 /// Error. No version data found
811 $result->setStatus(false);
812 $result->setErrorCode(NO_VERSION_DATA_FOUND
);
816 /// Extract the unicode part
818 if (!isset($data['#']['UNICODE'])) {
819 /// Error. No UNICODE section found
820 $result->setStatus(false);
821 $result->setErrorCode(NO_UNICODE_SECTION_FOUND
);
825 $level = get_level($data['#']['UNICODE']['0']);
828 if (!$unicodedb = $DB->setup_is_unicodedb()) {
829 $result->setStatus(false);
831 $result->setStatus(true);
834 $result->setLevel($level);
836 /// Do any actions defined in the XML file.
837 process_environment_result($data['#']['UNICODE'][0], $result);
843 * This function will check if database requirements are satisfied
846 * @uses NO_VERSION_DATA_FOUND
847 * @uses NO_DATABASE_SECTION_FOUND
848 * @uses NO_DATABASE_VENDORS_FOUND
849 * @uses NO_DATABASE_VENDOR_MYSQL_FOUND
850 * @uses NO_DATABASE_VENDOR_POSTGRES_FOUND
851 * @uses NO_DATABASE_VENDOR_VERSION_FOUND
852 * @param string $version xml version we are going to use to test this server
853 * @param int $env_select one of ENV_SELECT_NEWER | ENV_SELECT_DATAROOT | ENV_SELECT_RELEASE decide xml to use.
854 * @return object results encapsulated in one environment_result object
856 function environment_check_database($version, $env_select) {
860 $result = new environment_results('database');
862 $vendors = array(); //Array of vendors in version
864 /// Get the enviroment version we need
865 if (!$data = get_environment_for_version($version, $env_select)) {
866 /// Error. No version data found
867 $result->setStatus(false);
868 $result->setErrorCode(NO_VERSION_DATA_FOUND
);
872 /// Extract the database part
873 if (!isset($data['#']['DATABASE'])) {
874 /// Error. No DATABASE section found
875 $result->setStatus(false);
876 $result->setErrorCode(NO_DATABASE_SECTION_FOUND
);
880 $level = get_level($data['#']['DATABASE']['0']);
883 /// Extract DB vendors. At least 2 are mandatory (mysql & postgres)
884 if (!isset($data['#']['DATABASE']['0']['#']['VENDOR'])) {
885 /// Error. No VENDORS found
886 $result->setStatus(false);
887 $result->setErrorCode(NO_DATABASE_VENDORS_FOUND
);
891 foreach ($data['#']['DATABASE']['0']['#']['VENDOR'] as $vendor) {
892 if (isset($vendor['@']['name']) && isset($vendor['@']['version'])) {
893 $vendors[$vendor['@']['name']] = $vendor['@']['version'];
894 $vendorsxml[$vendor['@']['name']] = $vendor;
898 /// Check we have the mysql vendor version
899 if (empty($vendors['mysql'])) {
900 $result->setStatus(false);
901 $result->setErrorCode(NO_DATABASE_VENDOR_MYSQL_FOUND
);
904 /// Check we have the postgres vendor version
905 if (empty($vendors['postgres'])) {
906 $result->setStatus(false);
907 $result->setErrorCode(NO_DATABASE_VENDOR_POSTGRES_FOUND
);
911 /// Now search the version we are using (depending of vendor)
912 $current_vendor = $DB->get_dbfamily();
914 $dbinfo = $DB->get_server_info();
915 $current_version = normalize_version($dbinfo['version']);
916 $needed_version = $vendors[$current_vendor];
918 /// Check we have a needed version
919 if (!$needed_version) {
920 $result->setStatus(false);
921 $result->setErrorCode(NO_DATABASE_VENDOR_VERSION_FOUND
);
925 /// And finally compare them, saving results
926 if (version_compare($current_version, $needed_version, '>=')) {
927 $result->setStatus(true);
929 $result->setStatus(false);
931 $result->setLevel($level);
932 $result->setCurrentVersion($current_version);
933 $result->setNeededVersion($needed_version);
934 $result->setInfo($current_vendor);
936 /// Do any actions defined in the XML file.
937 process_environment_result($vendorsxml[$current_vendor], $result);
944 * This function will post-process the result record by executing the specified
945 * function, modifying it as necessary, also a custom message will be added
946 * to the result object to be printed by the display layer.
947 * Every bypass function must be defined in this file and it'll return
948 * true/false to decide if the original test is bypassed or no. Also
949 * such bypass functions are able to directly handling the result object
950 * although it should be only under exceptional conditions.
952 * @param string xmldata containing the bypass data
953 * @param object result object to be updated
956 function process_environment_bypass($xml, &$result) {
958 /// Only try to bypass if we were in error and it was required
959 if ($result->getStatus() ||
$result->getLevel() == 'optional') {
963 /// It there is bypass info (function and message)
964 if (is_array($xml['#']) && isset($xml['#']['BYPASS'][0]['@']['function']) && isset($xml['#']['BYPASS'][0]['@']['message'])) {
965 $function = $xml['#']['BYPASS'][0]['@']['function'];
966 $message = $xml['#']['BYPASS'][0]['@']['message'];
967 /// Look for the function
968 if (function_exists($function)) {
969 /// Call it, and if bypass = true is returned, apply meesage
970 if ($function($result)) {
971 /// We only set the bypass message if the function itself hasn't defined it before
972 if (empty($result->getBypassStr
)) {
973 $result->setBypassStr($message);
981 * This function will post-process the result record by executing the specified
982 * function, modifying it as necessary, also a custom message will be added
983 * to the result object to be printed by the display layer.
984 * Every restrict function must be defined in this file and it'll return
985 * true/false to decide if the original test is restricted or no. Also
986 * such restrict functions are able to directly handling the result object
987 * although it should be only under exceptional conditions.
989 * @param string xmldata containing the restrict data
990 * @param object result object to be updated
993 function process_environment_restrict($xml, &$result) {
995 /// Only try to restrict if we were not in error and it was required
996 if (!$result->getStatus() ||
$result->getLevel() == 'optional') {
999 /// It there is restrict info (function and message)
1000 if (is_array($xml['#']) && isset($xml['#']['RESTRICT'][0]['@']['function']) && isset($xml['#']['RESTRICT'][0]['@']['message'])) {
1001 $function = $xml['#']['RESTRICT'][0]['@']['function'];
1002 $message = $xml['#']['RESTRICT'][0]['@']['message'];
1003 /// Look for the function
1004 if (function_exists($function)) {
1005 /// Call it, and if restrict = true is returned, apply meesage
1006 if ($function($result)) {
1007 /// We only set the restrict message if the function itself hasn't defined it before
1008 if (empty($result->getRestrictStr
)) {
1009 $result->setRestrictStr($message);
1017 * This function will detect if there is some message available to be added to the
1018 * result in order to clarify enviromental details.
1020 * @param string xmldata containing the feedback data
1021 * @param object reult object to be updated
1023 function process_environment_messages($xml, &$result) {
1025 /// If there is feedback info
1026 if (is_array($xml['#']) && isset($xml['#']['FEEDBACK'][0]['#'])) {
1027 $feedbackxml = $xml['#']['FEEDBACK'][0]['#'];
1029 if (!$result->status
and $result->getLevel() == 'required') {
1030 if (isset($feedbackxml['ON_ERROR'][0]['@']['message'])) {
1031 $result->setFeedbackStr($feedbackxml['ON_ERROR'][0]['@']['message']);
1033 } else if (!$result->status
and $result->getLevel() == 'optional') {
1034 if (isset($feedbackxml['ON_CHECK'][0]['@']['message'])) {
1035 $result->setFeedbackStr($feedbackxml['ON_CHECK'][0]['@']['message']);
1038 if (isset($feedbackxml['ON_OK'][0]['@']['message'])) {
1039 $result->setFeedbackStr($feedbackxml['ON_OK'][0]['@']['message']);
1046 //--- Helper Class to return results to caller ---//
1050 * Helper Class to return results to caller
1052 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
1053 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1054 * @package moodlecore
1056 class environment_results
{
1058 * @var string Which are we checking (database, php, php_extension, php_extension)
1066 * @var integer See constants at the beginning of the file
1070 * @var string required/optional
1074 * @var string current version detected
1076 var $current_version;
1078 * @var string version needed
1080 var $needed_version;
1082 * @var string Aux. info (DB vendor, library...)
1086 * @var string String to show on error|on check|on ok
1090 * @var string String to show if some bypass has happened
1094 * @var string String to show if some restrict has happened
1099 * Constructor of the environment_result class. Just set default values
1101 * @param string $part
1103 function environment_results($part) {
1105 $this->status
=false;
1106 $this->error_code
=NO_ERROR
;
1107 $this->level
='required';
1108 $this->current_version
='';
1109 $this->needed_version
='';
1111 $this->feedback_str
='';
1112 $this->bypass_str
='';
1113 $this->restrict_str
='';
1119 * @param boolean $status the status (true/false)
1121 function setStatus($status) {
1122 $this->status
=$status;
1124 $this->setErrorCode(NO_ERROR
);
1129 * Set the error_code
1131 * @param integer $error_code the error code (see constants above)
1133 function setErrorCode($error_code) {
1134 $this->error_code
=$error_code;
1140 * @param string $level the level (required, optional)
1142 function setLevel($level) {
1143 $this->level
=$level;
1147 * Set the current version
1149 * @param string $current_version the current version
1151 function setCurrentVersion($current_version) {
1152 $this->current_version
=$current_version;
1156 * Set the needed version
1158 * @param string $needed_version the needed version
1160 function setNeededVersion($needed_version) {
1161 $this->needed_version
=$needed_version;
1165 * Set the auxiliary info
1167 * @param string $info the auxiliary info
1169 function setInfo($info) {
1174 * Set the feedback string
1176 * @param mixed $str the feedback string that will be fetched from the admin lang file.
1177 * pass just the string or pass an array of params for get_string
1178 * You always should put your string in admin.php but a third param is useful
1179 * to pass an $a object / string to get_string
1181 function setFeedbackStr($str) {
1182 $this->feedback_str
=$str;
1187 * Set the bypass string
1189 * @param string $str the bypass string that will be fetched from the admin lang file.
1190 * pass just the string or pass an array of params for get_string
1191 * You always should put your string in admin.php but a third param is useful
1192 * to pass an $a object / string to get_string
1194 function setBypassStr($str) {
1195 $this->bypass_str
=$str;
1199 * Set the restrict string
1201 * @param string $str the restrict string that will be fetched from the admin lang file.
1202 * pass just the string or pass an array of params for get_string
1203 * You always should put your string in admin.php but a third param is useful
1204 * to pass an $a object / string to get_string
1206 function setRestrictStr($str) {
1207 $this->restrict_str
=$str;
1213 * @return boolean result
1215 function getStatus() {
1216 return $this->status
;
1220 * Get the error code
1222 * @return integer error code
1224 function getErrorCode() {
1225 return $this->error_code
;
1231 * @return string level
1233 function getLevel() {
1234 return $this->level
;
1238 * Get the current version
1240 * @return string current version
1242 function getCurrentVersion() {
1243 return $this->current_version
;
1247 * Get the needed version
1249 * @return string needed version
1251 function getNeededVersion() {
1252 return $this->needed_version
;
1258 * @return string info
1260 function getInfo() {
1265 * Get the part this result belongs to
1267 * @return string part
1269 function getPart() {
1274 * Get the feedback string
1276 * @return mixed feedback string (can be an array of params for get_string or a single string to fetch from
1277 * admin.php lang file).
1279 function getFeedbackStr() {
1280 return $this->feedback_str
;
1284 * Get the bypass string
1286 * @return mixed bypass string (can be an array of params for get_string or a single string to fetch from
1287 * admin.php lang file).
1289 function getBypassStr() {
1290 return $this->bypass_str
;
1294 * Get the restrict string
1296 * @return mixed restrict string (can be an array of params for get_string or a single string to fetch from
1297 * admin.php lang file).
1299 function getRestrictStr() {
1300 return $this->restrict_str
;
1304 * @todo Document this function
1306 * @param mixed $string params for get_string, either a string to fetch from admin.php or an array of
1307 * params for get_string.
1308 * @param string $class css class(es) for message.
1309 * @return string feedback string fetched from lang file wrapped in p tag with class $class or returns
1310 * empty string if $string is empty.
1312 function strToReport($string, $class){
1313 if (!empty($string)){
1314 if (is_array($string)){
1315 $str = call_user_func_array('get_string', $string);
1317 $str = get_string($string, 'admin');
1319 return '<p class="'.$class.'">'.$str.'</p>';
1326 /// Here all the bypass functions are coded to be used by the environment
1327 /// checker. All those functions will receive the result object and will
1328 /// return it modified as needed (status and bypass string)
1331 * This function will bypass MySQL 4.1.16 reqs if:
1332 * - We are using MySQL > 4.1.12, informing about problems with non latin chars in the future
1334 * @param object result object to handle
1335 * @return boolean true/false to determinate if the bypass has to be performed (true) or no (false)
1337 function bypass_mysql416_reqs ($result) {
1338 /// See if we are running MySQL >= 4.1.12
1339 if (version_compare($result->getCurrentVersion(), '4.1.12', '>=')) {
1346 /// Here all the restrict functions are coded to be used by the environment
1347 /// checker. All those functions will receive the result object and will
1348 /// return it modified as needed (status and bypass string)
1351 * This function will restrict PHP reqs if:
1352 * - We are using PHP 5.0.x, informing about the buggy version
1354 * @param object $result object to handle
1355 * @return boolean true/false to determinate if the restrict has to be performed (true) or no (false)
1357 function restrict_php50_version($result) {
1358 if (version_compare($result->getCurrentVersion(), '5.0.0', '>=')
1359 and version_compare($result->getCurrentVersion(), '5.0.99', '<')) {
1366 * @param array $element the element from the environment.xml file that should have
1367 * either a level="required" or level="optional" attribute.
1368 * @return string "required" or "optional".
1370 function get_level($element) {
1371 $level = 'required';
1372 if (isset($element['@']['level'])) {
1373 $level = $element['@']['level'];
1374 if (!in_array($level, array('required', 'optional'))) {
1375 debugging('The level of a check in the environment.xml file must be "required" or "optional".', DEBUG_DEVELOPER
);
1376 $level = 'required';
1379 debugging('Checks in the environment.xml file must have a level="required" or level="optional" attribute.', DEBUG_DEVELOPER
);
1385 * Once the result has been determined, look in the XML for any
1386 * messages, or other things that should be done depending on the outcome.
1388 * @param array $element the element from the environment.xml file which
1389 * may have children defining what should be done with the outcome.
1390 * @param object $result the result of the test, which may be modified by
1391 * this function as specified in the XML.
1393 function process_environment_result($element, &$result) {
1394 /// Process messages, modifying the $result if needed.
1395 process_environment_messages($element, $result);
1396 /// Process bypass, modifying $result if needed.
1397 process_environment_bypass($element, $result);
1398 /// Process restrict, modifying $result if needed.
1399 process_environment_restrict($element, $result);