Index: lib/moodlelib.php =================================================================== RCS file: /cvsroot/moodle/moodle/lib/moodlelib.php,v retrieving revision 1.1177 diff -u -r1.1177 moodlelib.php --- lib/moodlelib.php 19 Mar 2009 10:35:08 -0000 1.1177 +++ lib/moodlelib.php 25 Mar 2009 06:27:01 -0000 @@ -5206,64 +5206,241 @@ * within translation strings */ function print_string($identifier, $module='', $a=NULL) { - echo get_string($identifier, $module, $a); + echo string_manager::get_string($identifier, $module, $a); } -/** - * fix up the optional data in get_string()/print_string() etc - * ensure possible sprintf() format characters are escaped correctly - * needs to handle arbitrary strings and objects - * @param mixed $a An object, string or number that can be used - * @return mixed the supplied parameter 'cleaned' - */ -function clean_getstring_data( $a ) { - if (is_string($a)) { - return str_replace( '%','%%',$a ); - } - elseif (is_object($a)) { - $a_vars = get_object_vars( $a ); - $new_a_vars = array(); - foreach ($a_vars as $fname => $a_var) { - $new_a_vars[$fname] = clean_getstring_data( $a_var ); +class string_manager { + private static $parentlangs = array('en_utf8' => NULL); + private static $strings = array(); + private static $nonpluginfiles = array('moodle' => 1, 'langconfig' => 1); + private static $langconfigstrs = array('alphabet' => 1, 'backupnameformat' => 1, 'decsep' => 1, + 'firstdayofweek' => 1, 'listsep' => 1, 'locale' => 1, 'localewin' => 1, + 'localewincharset' => 1, 'oldcharset' => 1, 'parentlanguage' => 1, + 'strftimedate' => 1, 'strftimedateshort' => 1, 'strftimedatefullshort' => 1, + 'strftimedatetime' => 1, 'strftimedaydate' => 1, 'strftimedaydatetime' => 1, + 'strftimedayshort' => 1, 'strftimedaytime' => 1, 'strftimemonthyear' => 1, + 'strftimerecent' => 1, 'strftimerecentfull' => 1, 'strftimetime' => 1, + 'thischarset' => 1, 'thisdirection' => 1, 'thislanguage' => 1, + 'strftimedatetimeshort' => 1, 'thousandssep' => 1); + private static $initialised = false; + private static $searchplacesbyplugintype = NULL; + private static $corelocations = NULL; + private static $installstrings = NULL; + private static $parentlangfile = 'langconfig.php'; + + // Some of our arrays need $CFG. + private static function initialise() { + global $CFG; + self::$corelocations = array( + $CFG->dataroot . '/lang/', + $CFG->dirroot . '/lang/', + $CFG->dirroot . '/local/lang/', // TODO remove. + ); + self::$searchplacesbyplugintype = array( + 'assignment_' => array('mod/assignment/type'), + 'auth_' => array('auth'), + 'block_' => array('blocks'), + 'datafield_' => array('mod/data/field'), + 'datapreset_' => array('mod/data/preset'), + 'enrol_' => array('enrol'), + 'filter_' => array('filter'), + 'format_' => array('course/format'), + 'quiz_' => array('mod/quiz/report'), + 'qtype_' => array('question/type'), + 'qformat_' => array('question/format'), + 'report_' => array($CFG->admin.'/report', 'course/report'), + 'repository_'=>array('repository'), + 'resource_' => array('mod/resource/type'), + 'gradereport_' => array('grade/report'), + 'gradeimport_' => array('grade/import'), + 'gradeexport_' => array('grade/export'), + 'profilefield_' => array('user/profile/field'), + 'portfolio_' => array('portfolio/type'), + '' => array('mod') + ); + if (!empty($CFG->running_installer)) { + $stringnames = file($CFG->dirroot.'/install/stringnames.txt'); + self::$installstrings = array_map('trim', $stringnames); + self::$parentlangfile = 'installer.php'; } - return (object)$new_a_vars; + $initialised = true; } - else { - return $a; + + /** + * fix up the optional data in get_string()/print_string() etc + * ensure possible sprintf() format characters are escaped correctly + * needs to handle arbitrary strings and objects + * @param mixed $a An object, string or number that can be used + * @return mixed the supplied parameter 'cleaned' + */ + private static function clean_getstring_data( $a ) { + if (is_string($a)) { + return str_replace('%', '%%', $a); + } else if (is_object($a)) { + $avars = get_object_vars($a); + $newa = new stdClass; + foreach ($avars as $fname => $avar) { + $newa->$fname = self::clean_getstring_data($avar); + } + return $newa; + } else { + return $a; + } } -} -/** - * @return array places to look for lang strings based on the prefix to the - * module name. For example qtype_ in question/type. Used by get_string and - * help.php. - */ -function places_to_search_for_lang_strings() { - global $CFG; + private static function fix_deprecated_module_name($module) { + debugging('The module name you passed to get_string is the deprecated format ' . + 'like mod/mymod or block/myblock. The correct form looks like mymod, or block_myblock.' , DEBUG_DEVELOPER); + $modulepath = split('/', $module); + + switch ($modulepath[0]) { + case 'mod': + return $modulepath[1]; + case 'blocks': + case 'block': + return 'block_'.$modulepath[1]; + case 'enrol': + return 'enrol_'.$modulepath[1]; + case 'format': + return 'format_'.$modulepath[1]; + case 'grade': + return 'grade'.$modulepath[1].'_'.$modulepath[2]; + default: + return $module; + } + } - return array( - '__exceptions' => array('moodle', 'langconfig'), - 'assignment_' => array('mod/assignment/type'), - 'auth_' => array('auth'), - 'block_' => array('blocks'), - 'datafield_' => array('mod/data/field'), - 'datapreset_' => array('mod/data/preset'), - 'enrol_' => array('enrol'), - 'filter_' => array('filter'), - 'format_' => array('course/format'), - 'quiz_' => array('mod/quiz/report'), - 'qtype_' => array('question/type'), - 'qformat_' => array('question/format'), - 'report_' => array($CFG->admin.'/report', 'course/report'), - 'repository_'=>array('repository'), - 'resource_' => array('mod/resource/type'), - 'gradereport_' => array('grade/report'), - 'gradeimport_' => array('grade/import'), - 'gradeexport_' => array('grade/export'), - 'profilefield_' => array('user/profile/field'), - 'portfolio_' => array('portfolio/type'), - '' => array('mod') - ); + private static function parse_module_name($module) { + if (strpos($module, '/') !== false) { + $module = self::fix_deprecated_module_name($module); + } + + $dividerpos = strpos($module, '_'); + if ($dividerpos === false) { + $type = ''; + $plugin = $module; + } else { + $type = substr($module, 0, $dividerpos + 1); + $plugin = substr($module, $dividerpos + 1); + } + return array($type, $plugin); + } + + private static function add_extra_locations($locations, $extralocations) { + // This is an old, deprecated mechanism that predates the + // places_to_search_for_lang_strings mechanism that comes later in + // this function. So tell people who use it to change. + debugging('The fourth, $extralocations parameter to get_string is deprecated. ' . + 'See http://docs.moodle.org/en/Development:Places_to_search_for_lang_strings ' . + 'for a better way to package language strings with your plugin.', DEBUG_DEVELOPER); + if (is_array($extralocations)) { + $locations = array_merge($locations, $extralocations); + } else if (is_string($extralocations)) { + $locations[] = $extralocations; + } else { + debugging('Bad lang path provided'); + } + return $locations; + } + + private static function get_parent_language($lang) { + if (array_key_exists($lang, self::$parentlangs)) { + return self::$parentlangs[$lang]; + } + $parent = 'en_utf8'; // Will be used if nothing is specified explicitly. + foreach (self::$corelocations as $location) { + foreach (array('_local', '') as $suffix) { + $file = $location . $lang . $suffix . '/' . self::$parentlangfile; + if ($result = self::get_string_from_file('parentlang', $file, NULL)) { + $parent = $result; + break 2; + } + } + + } + self::$parentlangs[$lang] = $parent; + return $parent; + } + + private static function load_lang_file($langfile) { + if (isset(self::$strings[$langfile])) { + return self::$strings[$langfile]; + } + $string = array(); + if (file_exists($langfile)) { + include($langfile); + } + self::$strings[$langfile] = $string; + return $string; + } + + private static function get_string_from_file($identifier, $langfile, $a) { + $string = &self::load_lang_file($langfile); + if (!isset($string[$identifier])) { + return false; + } + $result = ''; + $code = '$result = sprintf("'. $string[$identifier] .'");'; + if (eval($code) === FALSE) { // Means parse error. + debugging('Parse error while trying to load string "'.$identifier.'" from file "' . $langfile . '".', DEBUG_DEVELOPER); + } + return $result; + } + + public function get_string($identifier, $module='', $a=NULL, $extralocations=NULL) { + global $CFG; + if (!self::$initialised) { + self::initialise(); + } + + /// Preprocess the arguments. + if ($module == '') { + $module = 'moodle'; + } + + if ($module == 'moodle' && array_key_exists($identifier, self::$langconfigstrs)) { + $module = 'langconfig'; + } + + if ($a) { + $a = self::clean_getstring_data($a); + } + + /// Build up a list of paths that need to be searched. + $locations = self::$corelocations; + + if (!empty($extralocations)) { + $locations = self::add_extra_locations($locations, $extralocations); + } + + if (!empty($CFG->running_installer) && in_array($identifier, self::$installstrings)) { + $module = 'installer'; + array_unshift($locations, $CFG->dirroot . '/install/lang/'); + } + + if (!array_key_exists($module, self::$nonpluginfiles)) { + list($type, $plugin) = self::parse_module_name($module); + if (isset(self::$searchplacesbyplugintype[$type])) { + foreach (self::$searchplacesbyplugintype[$type] as $location) { + $locations[] = $CFG->dirroot . "/$location/$plugin/lang/"; + } + } + } + + /// Now do the search. + for ($lang = current_language(); $lang; $lang = self::get_parent_language($lang)) { + foreach ($locations as $location) { + foreach (array('_local', '') as $suffix) { + $file = $location . $lang . $suffix . '/' . $module . '.php'; + if ($result = self::get_string_from_file($identifier, $file, $a)) { + return $result; + } + } + } + } + + return '[['.$identifier.']]'; // Last resort + } } /** @@ -5327,264 +5504,7 @@ * @return string The localized string. */ function get_string($identifier, $module='', $a=NULL, $extralocations=NULL) { - global $CFG; - -/// originally these special strings were stored in moodle.php now we are only in langconfig.php - $langconfigstrs = array('alphabet', 'backupnameformat', 'decsep', 'firstdayofweek', 'listsep', 'locale', - 'localewin', 'localewincharset', 'oldcharset', 'parentlanguage', - 'strftimedate', 'strftimedateshort', 'strftimedatefullshort', 'strftimedatetime', - 'strftimedaydate', 'strftimedaydatetime', 'strftimedayshort', 'strftimedaytime', - 'strftimemonthyear', 'strftimerecent', 'strftimerecentfull', 'strftimetime', - 'thischarset', 'thisdirection', 'thislanguage', 'strftimedatetimeshort', 'thousandssep'); - - $filetocheck = 'langconfig.php'; - $defaultlang = 'en_utf8'; - if (in_array($identifier, $langconfigstrs)) { - $module = 'langconfig'; //This strings are under langconfig.php for 1.6 lang packs - } - - $lang = current_language(); - - if ($module == '') { - $module = 'moodle'; - } - -/// If the "module" is actually a pathname, then automatically derive the proper module name - if (strpos($module, '/') !== false) { - $modulepath = split('/', $module); - - switch ($modulepath[0]) { - - case 'mod': - $module = $modulepath[1]; - break; - - case 'blocks': - case 'block': - $module = 'block_'.$modulepath[1]; - break; - - case 'enrol': - $module = 'enrol_'.$modulepath[1]; - break; - - case 'format': - $module = 'format_'.$modulepath[1]; - break; - - case 'grade': - $module = 'grade'.$modulepath[1].'_'.$modulepath[2]; - break; - } - } - -/// if $a happens to have % in it, double it so sprintf() doesn't break - if ($a) { - $a = clean_getstring_data( $a ); - } - -/// Define the two or three major locations of language strings for this module - $locations = array(); - - if (!empty($extralocations)) { - // This is an old, deprecated mechanism that predates the - // places_to_search_for_lang_strings mechanism that comes later in - // this function. So tell people who use it to change. - debugging('The fourth, $extralocations parameter to get_string is deprecated. ' . - 'See http://docs.moodle.org/en/Development:Places_to_search_for_lang_strings ' . - 'for a better way to package language strings with your plugin.', DEBUG_DEVELOPER); - if (is_array($extralocations)) { - $locations += $extralocations; - } else if (is_string($extralocations)) { - $locations[] = $extralocations; - } else { - debugging('Bad lang path provided'); - } - } - - if (!empty($CFG->running_installer) and $lang !== 'en_utf8') { - static $stringnames = null; - if (!$stringnames) { - $stringnames = file($CFG->dirroot.'/install/stringnames.txt'); - $stringnames = array_map('trim', $stringnames); - } - if (array_search($identifier, $stringnames) !== false) { - $module = 'installer'; - $filetocheck = 'installer.php'; - $defaultlang = 'en_utf8'; - $locations[] = $CFG->dirroot.'/install/lang/'; - } - } - - $locations[] = $CFG->dataroot.'/lang/'; - $locations[] = $CFG->dirroot.'/lang/'; - $locations[] = $CFG->dirroot.'/local/lang/'; - -/// Add extra places to look for strings for particular plugin types. - $rules = places_to_search_for_lang_strings(); - $exceptions = $rules['__exceptions']; - unset($rules['__exceptions']); - - if (!in_array($module, $exceptions)) { - $dividerpos = strpos($module, '_'); - if ($dividerpos === false) { - $type = ''; - $plugin = $module; - } else { - $type = substr($module, 0, $dividerpos + 1); - $plugin = substr($module, $dividerpos + 1); - } - if (!empty($rules[$type])) { - foreach ($rules[$type] as $location) { - $locations[] = $CFG->dirroot . "/$location/$plugin/lang/"; - } - } - } - -/// First check all the normal locations for the string in the current language - $resultstring = ''; - foreach ($locations as $location) { - $locallangfile = $location.$lang.'_local'.'/'.$module.'.php'; //first, see if there's a local file - if (file_exists($locallangfile)) { - if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) { - if (eval($result) === FALSE) { - trigger_error('Lang error: '.$identifier.':'.$locallangfile, E_USER_NOTICE); - } - return $resultstring; - } - } - //if local directory not found, or particular string does not exist in local direcotry - $langfile = $location.$lang.'/'.$module.'.php'; - if (file_exists($langfile)) { - if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { - if (eval($result) === FALSE) { - trigger_error('Lang error: '.$identifier.':'.$langfile, E_USER_NOTICE); - } - return $resultstring; - } - } - } - -/// If the preferred language was English (utf8) we can abort now -/// saving some checks beacuse it's the only "root" lang - if ($lang == 'en_utf8') { - return '[['. $identifier .']]'; - } - -/// Is a parent language defined? If so, try to find this string in a parent language file - - foreach ($locations as $location) { - $langfile = $location.$lang.'/'.$filetocheck; - if (file_exists($langfile)) { - if ($result = get_string_from_file('parentlanguage', $langfile, "\$parentlang")) { - if (eval($result) === FALSE) { - trigger_error('Lang error: '.$identifier.':'.$langfile, E_USER_NOTICE); - } - if (!empty($parentlang) and strpos($parentlang, '<') === false) { // found it! - - //first, see if there's a local file for parent - $locallangfile = $location.$parentlang.'_local'.'/'.$module.'.php'; - if (file_exists($locallangfile)) { - if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) { - if (eval($result) === FALSE) { - trigger_error('Lang error: '.$identifier.':'.$locallangfile, E_USER_NOTICE); - } - return $resultstring; - } - } - - //if local directory not found, or particular string does not exist in local direcotry - $langfile = $location.$parentlang.'/'.$module.'.php'; - if (file_exists($langfile)) { - if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { - eval($result); - return $resultstring; - } - } - } - } - } - } - -/// Our only remaining option is to try English - - foreach ($locations as $location) { - $locallangfile = $location.$defaultlang.'_local/'.$module.'.php'; //first, see if there's a local file - if (file_exists($locallangfile)) { - if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) { - eval($result); - return $resultstring; - } - } - - //if local_en not found, or string not found in local_en - $langfile = $location.$defaultlang.'/'.$module.'.php'; - - if (file_exists($langfile)) { - if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { - eval($result); - return $resultstring; - } - } - } - -/// And, because under 1.6 en is defined as en_utf8 child, me must try -/// if it hasn't been queried before. - if ($defaultlang == 'en') { - $defaultlang = 'en_utf8'; - foreach ($locations as $location) { - $locallangfile = $location.$defaultlang.'_local/'.$module.'.php'; //first, see if there's a local file - if (file_exists($locallangfile)) { - if ($result = get_string_from_file($identifier, $locallangfile, "\$resultstring")) { - eval($result); - return $resultstring; - } - } - - //if local_en not found, or string not found in local_en - $langfile = $location.$defaultlang.'/'.$module.'.php'; - - if (file_exists($langfile)) { - if ($result = get_string_from_file($identifier, $langfile, "\$resultstring")) { - eval($result); - return $resultstring; - } - } - } - } - - return '[['.$identifier.']]'; // Last resort -} - -/** - * This function is only used from {@link get_string()}. - * - * @internal Only used from get_string, not meant to be public API - * @param string $identifier ? - * @param string $langfile ? - * @param string $destination ? - * @return string|false ? - * @staticvar array $strings Localized strings - * @access private - * @todo Finish documenting this function. - */ -function get_string_from_file($identifier, $langfile, $destination) { - - static $strings; // Keep the strings cached in memory. - - if (empty($strings[$langfile])) { - $string = array(); - include ($langfile); - $strings[$langfile] = $string; - } else { - $string = &$strings[$langfile]; - } - - if (!isset ($string[$identifier])) { - return false; - } - - return $destination .'= sprintf("'. $string[$identifier] .'");'; + return string_manager::get_string($identifier, $module, $a, $extralocations); } /** @@ -5592,13 +5512,12 @@ * * @param array $array An array of strings * @param string $module The language module that these strings can be found in. - * @return string + * @return array and array of translated strings. */ function get_strings($array, $module='') { - - $string = NULL; + $string = new stdClass; foreach ($array as $item) { - $string->$item = get_string($item, $module); + $string->$item = string_manager::get_string($item, $module); } return $string; } Index: lib/simpletest/getstringperformancetester.php =================================================================== RCS file: lib/simpletest/getstringperformancetester.php diff -N lib/simpletest/getstringperformancetester.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/simpletest/getstringperformancetester.php 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,138 @@ +libdir . '/moodlelib.php'); + +define('NUM_CALLS', 20000); +define('NUM_REPITITIONS', 3); +$TEST_LANGUAGES = array('en_utf8', 'fr_utf8', 'fr_ca_utf8', 'nonexistant'); +$SUMMARYRESULTS = array(); + +require_login(); +require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); + +$title = 'get_string performance test'; +print_header($title, $title, build_navigation($title)); + +time_for_loop(); +time_function_call(); + +unset($COURSE->lang); +if (isset($SESSION->lang)) { + $originalsessionlang = $SESSION->lang; +} else { + $originalsessionlang = null; +} +try { + + foreach ($TEST_LANGUAGES as $lang) { + print_heading("Language '$lang'"); + $SESSION->lang = $lang; + test_one_case('info', '', null); + test_one_case('attemptquiznow', 'quiz', null); + $a = new stdClass; + $a->firstname = 'Martin'; + $a->lastname = 'Dougiamas'; + test_one_case('fullnamedisplay', '', $a); + test_one_case('stringthatdoesnotexistinanyfile', 'qtype_shortanswer', null); + } + + print_heading('Results summary'); + echo '
';
+    foreach ($SUMMARYRESULTS as $row) {
+        echo "'", implode("','", $row) , "'\n";
+    }
+    echo '
'; + +} catch(Exception $e) { // Did they really leave finally out of PHP? + if (is_null($originalsessionlang)) { + unset($SESSION->lang); + } else { + $SESSION->lang = $originalsessionlang; + } +} + +print_footer(); + +function test_one_case($string, $module, $a) { + global $SESSION,$SUMMARYRESULTS; + $row = array($SESSION->lang, $string); + print_heading("get_string('$string', '$module', " . print_r($a, true) . ")", '', 3); + echo '

Resulting string: ' . get_string($string, $module, $a) . "

\n"; + for ($i = 0; $i < NUM_REPITITIONS; ++$i) { + set_time_limit(60); + $startime = microtime(true); + for ($j = 0; $j < NUM_CALLS; $j++) { + get_string($string, $module, $a); + } + $duration = microtime(true) - $startime; + print_result_line($duration, 'calls to get_string'); + $row[] = $duration; + $row[] = NUM_CALLS / $duration; + } + $SUMMARYRESULTS[] = $row; +} + +function time_for_loop() { + print_heading('Timing an empty for loop'); + $startime = microtime(true); + for ($i = 0; $i < NUM_CALLS; $i++) { + } + $duration = microtime(true) - $startime; + print_result_line($duration, 'trips through an empty for loop', 'iterations per second'); +} + +function dummy_function($string, $module, $a) { +} +function time_function_call() { + print_heading('Timing calling an empty function with three arguments'); + $string = 'string'; + $module = 'module'; + $a = new stdClass; + $a->field = 'value'; + $startime = microtime(true); + for ($i = 0; $i < NUM_CALLS; $i++) { + dummy_function($string, $module, $a); + } + $duration = microtime(true) - $startime; + print_result_line($duration, 'calls to dummy_function'); +} + +function print_result_line($duration, $action1, $action2 = 'calls per second') { + echo '

Time for ' . format_float(NUM_CALLS, 0) . ' ' . $action1 . ': ' . + format_float($duration, 3) . 's which is ' . + format_float((NUM_CALLS / $duration), 0) . ' ' . $action2 . ".

\n"; + flush(); +} + +?>