From 99a9d8d937f32c3c3493141f374ff701b737c408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sat, 3 Aug 2013 22:19:18 +0200 Subject: [PATCH 1/2] MDL-41019 refactor string managers to new core_ classes --- admin/cli/install.php | 3 + install.php | 3 + lib/classes/string_manager.php | 129 +++ lib/classes/string_manager_install.php | 249 +++++ lib/classes/string_manager_standard.php | 649 ++++++++++++ lib/moodlelib.php | 962 +----------------- ...t.php => string_manager_standard_test.php} | 6 +- 7 files changed, 1040 insertions(+), 961 deletions(-) create mode 100644 lib/classes/string_manager.php create mode 100644 lib/classes/string_manager_install.php create mode 100644 lib/classes/string_manager_standard.php rename lib/tests/{string_manager_test.php => string_manager_standard_test.php} (95%) diff --git a/admin/cli/install.php b/admin/cli/install.php index 2d992b5ae40..21617cf0860 100644 --- a/admin/cli/install.php +++ b/admin/cli/install.php @@ -172,6 +172,9 @@ ini_set('include_path', $CFG->libdir.'/pear' . PATH_SEPARATOR . ini_get('include require_once($CFG->libdir.'/classes/component.php'); require_once($CFG->libdir.'/classes/text.php'); +require_once($CFG->libdir.'/classes/string_manager.php'); +require_once($CFG->libdir.'/classes/string_manager_install.php'); +require_once($CFG->libdir.'/classes/string_manager_standard.php'); require_once($CFG->libdir.'/installlib.php'); require_once($CFG->libdir.'/clilib.php'); require_once($CFG->libdir.'/setuplib.php'); diff --git a/install.php b/install.php index 32f8f2d7be8..7b5fa239677 100644 --- a/install.php +++ b/install.php @@ -198,6 +198,9 @@ if (!empty($memlimit) and $memlimit != -1) { // Continue with lib loading require_once($CFG->libdir.'/classes/text.php'); +require_once($CFG->libdir.'/classes/string_manager.php'); +require_once($CFG->libdir.'/classes/string_manager_install.php'); +require_once($CFG->libdir.'/classes/string_manager_standard.php'); require_once($CFG->libdir.'/weblib.php'); require_once($CFG->libdir.'/outputlib.php'); require_once($CFG->libdir.'/dmllib.php'); diff --git a/lib/classes/string_manager.php b/lib/classes/string_manager.php new file mode 100644 index 00000000000..b0d617f146b --- /dev/null +++ b/lib/classes/string_manager.php @@ -0,0 +1,129 @@ +. + +/** + * String manager interface. + * + * @package core + * @copyright 2010 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Interface for string manager + * + * Interface describing class which is responsible for getting + * of localised strings from language packs. + * + * @package core + * @copyright 2010 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +interface core_string_manager { + /** + * Get String returns a requested string + * + * @param string $identifier The identifier of the string to search for + * @param string $component The module the string is associated with + * @param string|object|array $a An object, string or number that can be used + * within translation strings + * @param string $lang moodle translation language, null means use current + * @return string The String ! + */ + public function get_string($identifier, $component = '', $a = null, $lang = null); + + /** + * Does the string actually exist? + * + * get_string() is throwing debug warnings, sometimes we do not want them + * or we want to display better explanation of the problem. + * + * Use with care! + * + * @param string $identifier The identifier of the string to search for + * @param string $component The module the string is associated with + * @return bool true if exists + */ + public function string_exists($identifier, $component); + + /** + * Returns a localised list of all country names, sorted by country keys. + * @param bool $returnall return all or just enabled + * @param string $lang moodle translation language, null means use current + * @return array two-letter country code => translated name. + */ + public function get_list_of_countries($returnall = false, $lang = null); + + /** + * Returns a localised list of languages, sorted by code keys. + * + * @param string $lang moodle translation language, null means use current + * @param string $standard language list standard + * iso6392: three-letter language code (ISO 639-2/T) => translated name. + * @return array language code => translated name + */ + public function get_list_of_languages($lang = null, $standard = 'iso6392'); + + /** + * Checks if the translation exists for the language + * + * @param string $lang moodle translation language code + * @param bool $includeall include also disabled translations + * @return bool true if exists + */ + public function translation_exists($lang, $includeall = true); + + /** + * Returns localised list of installed translations + * @param bool $returnall return all or just enabled + * @return array moodle translation code => localised translation name + */ + public function get_list_of_translations($returnall = false); + + /** + * Returns localised list of currencies. + * + * @param string $lang moodle translation language, null means use current + * @return array currency code => localised currency name + */ + public function get_list_of_currencies($lang = null); + + /** + * Load all strings for one component + * @param string $component The module the string is associated with + * @param string $lang + * @param bool $disablecache Do not use caches, force fetching the strings from sources + * @param bool $disablelocal Do not use customized strings in xx_local language packs + * @return array of all string for given component and lang + */ + public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false); + + /** + * Invalidates all caches, should the implementation use any + * @param bool $phpunitreset true means called from our PHPUnit integration test reset + */ + public function reset_caches($phpunitreset = false); + + /** + * Returns string revision counter, this is incremented after any + * string cache reset. + * @return int lang string revision counter, -1 if unknown + */ + public function get_revision(); +} + diff --git a/lib/classes/string_manager_install.php b/lib/classes/string_manager_install.php new file mode 100644 index 00000000000..84d5971d263 --- /dev/null +++ b/lib/classes/string_manager_install.php @@ -0,0 +1,249 @@ +. + +/** + * Installation time string manager. + * + * @package core + * @copyright 2010 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Fetches minimum strings for installation + * + * Minimalistic string fetching implementation + * that is used in installer before we fetch the wanted + * language pack from moodle.org lang download site. + * + * @package core + * @copyright 2010 Petr Skoda (http://skodak.org) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_string_manager_install implements core_string_manager { + /** @var string location of pre-install packs for all langs */ + protected $installroot; + + /** + * Crate new instance of install string manager + */ + public function __construct() { + global $CFG; + $this->installroot = "$CFG->dirroot/install/lang"; + } + + /** + * Load all strings for one component + * @param string $component The module the string is associated with + * @param string $lang + * @param bool $disablecache Do not use caches, force fetching the strings from sources + * @param bool $disablelocal Do not use customized strings in xx_local language packs + * @return array of all string for given component and lang + */ + public function load_component_strings($component, $lang, $disablecache = false, $disablelocal = false) { + // Not needed in installer. + return array(); + } + + /** + * Does the string actually exist? + * + * get_string() is throwing debug warnings, sometimes we do not want them + * or we want to display better explanation of the problem. + * + * Use with care! + * + * @param string $identifier The identifier of the string to search for + * @param string $component The module the string is associated with + * @return boot true if exists + */ + public function string_exists($identifier, $component) { + // Simple old style hack ;). + $str = get_string($identifier, $component); + return (strpos($str, '[[') === false); + } + + /** + * Get String returns a requested string + * + * @param string $identifier The identifier of the string to search for + * @param string $component The module the string is associated with + * @param string|object|array $a An object, string or number that can be used + * within translation strings + * @param string $lang moodle translation language, null means use current + * @return string The String ! + */ + public function get_string($identifier, $component = '', $a = null, $lang = null) { + if (!$component) { + $component = 'moodle'; + } + + if ($lang === null) { + $lang = current_language(); + } + + // Get parent lang. + $parent = ''; + if ($lang !== 'en' and $identifier !== 'parentlanguage' and $component !== 'langconfig') { + if (file_exists("$this->installroot/$lang/langconfig.php")) { + $string = array(); + include("$this->installroot/$lang/langconfig.php"); + if (isset($string['parentlanguage'])) { + $parent = $string['parentlanguage']; + } + unset($string); + } + } + + // Include en string first. + if (!file_exists("$this->installroot/en/$component.php")) { + return "[[$identifier]]"; + } + $string = array(); + include("$this->installroot/en/$component.php"); + + // Now override en with parent if defined. + if ($parent and $parent !== 'en' and file_exists("$this->installroot/$parent/$component.php")) { + include("$this->installroot/$parent/$component.php"); + } + + // Finally override with requested language. + if ($lang !== 'en' and file_exists("$this->installroot/$lang/$component.php")) { + include("$this->installroot/$lang/$component.php"); + } + + if (!isset($string[$identifier])) { + return "[[$identifier]]"; + } + + $string = $string[$identifier]; + + if ($a !== null) { + if (is_object($a) or is_array($a)) { + $a = (array)$a; + $search = array(); + $replace = array(); + foreach ($a as $key => $value) { + if (is_int($key)) { + // We do not support numeric keys - sorry! + continue; + } + $search[] = '{$a->' . $key . '}'; + $replace[] = (string)$value; + } + if ($search) { + $string = str_replace($search, $replace, $string); + } + } else { + $string = str_replace('{$a}', (string)$a, $string); + } + } + + return $string; + } + + /** + * Returns a localised list of all country names, sorted by country keys. + * + * @param bool $returnall return all or just enabled + * @param string $lang moodle translation language, null means use current + * @return array two-letter country code => translated name. + */ + public function get_list_of_countries($returnall = false, $lang = null) { + // Not used in installer. + return array(); + } + + /** + * Returns a localised list of languages, sorted by code keys. + * + * @param string $lang moodle translation language, null means use current + * @param string $standard language list standard + * iso6392: three-letter language code (ISO 639-2/T) => translated name. + * @return array language code => translated name + */ + public function get_list_of_languages($lang = null, $standard = 'iso6392') { + // Not used in installer. + return array(); + } + + /** + * Checks if the translation exists for the language + * + * @param string $lang moodle translation language code + * @param bool $includeall include also disabled translations + * @return bool true if exists + */ + public function translation_exists($lang, $includeall = true) { + return file_exists($this->installroot . '/' . $lang . '/langconfig.php'); + } + + /** + * Returns localised list of installed translations + * @param bool $returnall return all or just enabled + * @return array moodle translation code => localised translation name + */ + public function get_list_of_translations($returnall = false) { + // Return all is ignored here - we need to know all langs in installer. + $languages = array(); + // Get raw list of lang directories. + $langdirs = get_list_of_plugins('install/lang'); + asort($langdirs); + // Get some info from each lang. + foreach ($langdirs as $lang) { + if (file_exists($this->installroot . '/' . $lang . '/langconfig.php')) { + $string = array(); + include($this->installroot . '/' . $lang . '/langconfig.php'); + if (!empty($string['thislanguage'])) { + $languages[$lang] = $string['thislanguage'] . ' (' . $lang . ')'; + } + } + } + // Return array. + return $languages; + } + + /** + * Returns localised list of currencies. + * + * @param string $lang moodle translation language, null means use current + * @return array currency code => localised currency name + */ + public function get_list_of_currencies($lang = null) { + // Not used in installer. + return array(); + } + + /** + * This implementation does not use any caches. + * + * @param bool $phpunitreset true means called from our PHPUnit integration test reset + */ + public function reset_caches($phpunitreset = false) { + // Nothing to do. + } + + /** + * Returns string revision counter, this is incremented after any string cache reset. + * @return int lang string revision counter, -1 if unknown + */ + public function get_revision() { + return -1; + } +} diff --git a/lib/classes/string_manager_standard.php b/lib/classes/string_manager_standard.php new file mode 100644 index 00000000000..1609d1b1284 --- /dev/null +++ b/lib/classes/string_manager_standard.php @@ -0,0 +1,649 @@ +. + +/** + * Standard string manager. + * + * @package core + * @copyright 2010 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + + +/** + * Standard string_manager implementation + * + * Implements string_manager with getting and printing localised strings + * + * @package core + * @copyright 2010 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_string_manager_standard implements core_string_manager { + /** @var string location of all packs except 'en' */ + protected $otherroot; + /** @var string location of all lang pack local modifications */ + protected $localroot; + /** @var cache lang string cache - it will be optimised more later */ + protected $cache; + /** @var int get_string() counter */ + protected $countgetstring = 0; + /** @var bool use disk cache */ + protected $usecache; + /** @var array limit list of translations */ + protected $translist; + /** @var string location of a file that caches the list of available translations */ + protected $menucache; + + /** + * Create new instance of string manager + * + * @param string $otherroot location of downlaoded lang packs - usually $CFG->dataroot/lang + * @param string $localroot usually the same as $otherroot + * @param bool $usecache use disk cache + * @param array $translist limit list of visible translations + * @param string $menucache the location of a file that caches the list of available translations + */ + public function __construct($otherroot, $localroot, $usecache, $translist, $menucache) { + $this->otherroot = $otherroot; + $this->localroot = $localroot; + $this->usecache = $usecache; + $this->translist = $translist; + $this->menucache = $menucache; + + if ($this->usecache) { + // We can use a proper cache, establish the cache using the 'String cache' definition. + $this->cache = cache::make('core', 'string'); + } else { + // We only want a cache for the length of the request, create a static cache. + $options = array( + 'simplekeys' => true, + 'simpledata' => true + ); + $this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string', array(), $options); + } + } + + /** + * Returns list of all explicit parent languages for the given language. + * + * English (en) is considered as the top implicit parent of all language packs + * and is not included in the returned list. The language itself is appended to the + * end of the list. The method is aware of circular dependency risk. + * + * @see self::populate_parent_languages() + * @param string $lang the code of the language + * @return array all explicit parent languages with the lang itself appended + */ + public function get_language_dependencies($lang) { + return $this->populate_parent_languages($lang); + } + + /** + * Load all strings for one component + * + * @param string $component The module the string is associated with + * @param string $lang + * @param bool $disablecache Do not use caches, force fetching the strings from sources + * @param bool $disablelocal Do not use customized strings in xx_local language packs + * @return array of all string for given component and lang + */ + public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false) { + global $CFG; + + list($plugintype, $pluginname) = core_component::normalize_component($component); + if ($plugintype == 'core' and is_null($pluginname)) { + $component = 'core'; + } else { + $component = $plugintype . '_' . $pluginname; + } + + $cachekey = $lang.'_'.$component; + + if (!$disablecache and !$disablelocal) { + $string = $this->cache->get($cachekey); + if ($string) { + return $string; + } + } + + // No cache found - let us merge all possible sources of the strings. + if ($plugintype === 'core') { + $file = $pluginname; + if ($file === null) { + $file = 'moodle'; + } + $string = array(); + // First load english pack. + if (!file_exists("$CFG->dirroot/lang/en/$file.php")) { + return array(); + } + include("$CFG->dirroot/lang/en/$file.php"); + $originalkeys = array_keys($string); + $originalkeys = array_flip($originalkeys); + + // And then corresponding local if present and allowed. + if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { + include("$this->localroot/en_local/$file.php"); + } + // Now loop through all langs in correct order. + $deps = $this->get_language_dependencies($lang); + foreach ($deps as $dep) { + // The main lang string location. + if (file_exists("$this->otherroot/$dep/$file.php")) { + include("$this->otherroot/$dep/$file.php"); + } + if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) { + include("$this->localroot/{$dep}_local/$file.php"); + } + } + + } else { + if (!$location = core_component::get_plugin_directory($plugintype, $pluginname) or !is_dir($location)) { + return array(); + } + if ($plugintype === 'mod') { + // Bloody mod hack. + $file = $pluginname; + } else { + $file = $plugintype . '_' . $pluginname; + } + $string = array(); + // First load English pack. + if (!file_exists("$location/lang/en/$file.php")) { + // English pack does not exist, so do not try to load anything else. + return array(); + } + include("$location/lang/en/$file.php"); + $originalkeys = array_keys($string); + $originalkeys = array_flip($originalkeys); + // And then corresponding local english if present. + if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { + include("$this->localroot/en_local/$file.php"); + } + + // Now loop through all langs in correct order. + $deps = $this->get_language_dependencies($lang); + foreach ($deps as $dep) { + // Legacy location - used by contrib only. + if (file_exists("$location/lang/$dep/$file.php")) { + include("$location/lang/$dep/$file.php"); + } + // The main lang string location. + if (file_exists("$this->otherroot/$dep/$file.php")) { + include("$this->otherroot/$dep/$file.php"); + } + // Local customisations. + if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) { + include("$this->localroot/{$dep}_local/$file.php"); + } + } + } + + // We do not want any extra strings from other languages - everything must be in en lang pack. + $string = array_intersect_key($string, $originalkeys); + + if (!$disablelocal) { + // Now we have a list of strings from all possible sources. put it into both in-memory and on-disk + // caches so we do not need to do all this merging and dependencies resolving again. + $this->cache->set($cachekey, $string); + } + return $string; + } + + /** + * Does the string actually exist? + * + * get_string() is throwing debug warnings, sometimes we do not want them + * or we want to display better explanation of the problem. + * Note: Use with care! + * + * @param string $identifier The identifier of the string to search for + * @param string $component The module the string is associated with + * @return boot true if exists + */ + public function string_exists($identifier, $component) { + $lang = current_language(); + $string = $this->load_component_strings($component, $lang); + return isset($string[$identifier]); + } + + /** + * Get String returns a requested string + * + * @param string $identifier The identifier of the string to search for + * @param string $component The module the string is associated with + * @param string|object|array $a An object, string or number that can be used + * within translation strings + * @param string $lang moodle translation language, null means use current + * @return string The String ! + */ + public function get_string($identifier, $component = '', $a = null, $lang = null) { + $this->countgetstring++; + // There are very many uses of these time formating strings without the 'langconfig' component, + // it would not be reasonable to expect that all of them would be converted during 2.0 migration. + static $langconfigstrs = array( + 'strftimedate' => 1, + 'strftimedatefullshort' => 1, + 'strftimedateshort' => 1, + 'strftimedatetime' => 1, + 'strftimedatetimeshort' => 1, + 'strftimedaydate' => 1, + 'strftimedaydatetime' => 1, + 'strftimedayshort' => 1, + 'strftimedaytime' => 1, + 'strftimemonthyear' => 1, + 'strftimerecent' => 1, + 'strftimerecentfull' => 1, + 'strftimetime' => 1); + + if (empty($component)) { + if (isset($langconfigstrs[$identifier])) { + $component = 'langconfig'; + } else { + $component = 'moodle'; + } + } + + if ($lang === null) { + $lang = current_language(); + } + + $string = $this->load_component_strings($component, $lang); + + if (!isset($string[$identifier])) { + if ($component === 'pix' or $component === 'core_pix') { + // This component contains only alt tags for emoticons, not all of them are supposed to be defined. + return ''; + } + if ($identifier === 'parentlanguage' and ($component === 'langconfig' or $component === 'core_langconfig')) { + // Identifier parentlanguage is a special string, undefined means use English if not defined. + return 'en'; + } + if ($this->usecache) { + // Maybe the on-disk cache is dirty - let the last attempt be to find the string in original sources, + // do NOT write the results to disk cache because it may end up in race conditions see MDL-31904. + $this->usecache = false; + $string = $this->load_component_strings($component, $lang, true); + $this->usecache = true; + } + if (!isset($string[$identifier])) { + // The string is still missing - should be fixed by developer. + list($plugintype, $pluginname) = core_component::normalize_component($component); + if ($plugintype == 'core') { + $file = "lang/en/{$component}.php"; + } else if ($plugintype == 'mod') { + $file = "mod/{$pluginname}/lang/en/{$pluginname}.php"; + } else { + $path = core_component::get_plugin_directory($plugintype, $pluginname); + $file = "{$path}/lang/en/{$plugintype}_{$pluginname}.php"; + } + debugging("Invalid get_string() identifier: '{$identifier}' or component '{$component}'. " . + "Perhaps you are missing \$string['{$identifier}'] = ''; in {$file}?", DEBUG_DEVELOPER); + return "[[$identifier]]"; + } + } + + $string = $string[$identifier]; + + if ($a !== null) { + // Process array's and objects (except lang_strings). + if (is_array($a) or (is_object($a) && !($a instanceof lang_string))) { + $a = (array)$a; + $search = array(); + $replace = array(); + foreach ($a as $key => $value) { + if (is_int($key)) { + // We do not support numeric keys - sorry! + continue; + } + if (is_array($value) or (is_object($value) && !($value instanceof lang_string))) { + // We support just string or lang_string as value. + continue; + } + $search[] = '{$a->'.$key.'}'; + $replace[] = (string)$value; + } + if ($search) { + $string = str_replace($search, $replace, $string); + } + } else { + $string = str_replace('{$a}', (string)$a, $string); + } + } + + return $string; + } + + /** + * Returns information about the string_manager performance. + * + * @return array + */ + public function get_performance_summary() { + return array(array( + 'langcountgetstring' => $this->countgetstring, + ), array( + 'langcountgetstring' => 'get_string calls', + )); + } + + /** + * Returns a localised list of all country names, sorted by localised name. + * + * @param bool $returnall return all or just enabled + * @param string $lang moodle translation language, null means use current + * @return array two-letter country code => translated name. + */ + public function get_list_of_countries($returnall = false, $lang = null) { + global $CFG; + + if ($lang === null) { + $lang = current_language(); + } + + $countries = $this->load_component_strings('core_countries', $lang); + core_collator::asort($countries); + if (!$returnall and !empty($CFG->allcountrycodes)) { + $enabled = explode(',', $CFG->allcountrycodes); + $return = array(); + foreach ($enabled as $c) { + if (isset($countries[$c])) { + $return[$c] = $countries[$c]; + } + } + return $return; + } + + return $countries; + } + + /** + * Returns a localised list of languages, sorted by code keys. + * + * @param string $lang moodle translation language, null means use current + * @param string $standard language list standard + * - iso6392: three-letter language code (ISO 639-2/T) => translated name + * - iso6391: two-letter langauge code (ISO 639-1) => translated name + * @return array language code => translated name + */ + public function get_list_of_languages($lang = null, $standard = 'iso6391') { + if ($lang === null) { + $lang = current_language(); + } + + if ($standard === 'iso6392') { + $langs = $this->load_component_strings('core_iso6392', $lang); + ksort($langs); + return $langs; + + } else if ($standard === 'iso6391') { + $langs2 = $this->load_component_strings('core_iso6392', $lang); + static $mapping = array('aar' => 'aa', 'abk' => 'ab', 'afr' => 'af', 'aka' => 'ak', 'sqi' => 'sq', 'amh' => 'am', 'ara' => 'ar', 'arg' => 'an', 'hye' => 'hy', + 'asm' => 'as', 'ava' => 'av', 'ave' => 'ae', 'aym' => 'ay', 'aze' => 'az', 'bak' => 'ba', 'bam' => 'bm', 'eus' => 'eu', 'bel' => 'be', 'ben' => 'bn', 'bih' => 'bh', + 'bis' => 'bi', 'bos' => 'bs', 'bre' => 'br', 'bul' => 'bg', 'mya' => 'my', 'cat' => 'ca', 'cha' => 'ch', 'che' => 'ce', 'zho' => 'zh', 'chu' => 'cu', 'chv' => 'cv', + 'cor' => 'kw', 'cos' => 'co', 'cre' => 'cr', 'ces' => 'cs', 'dan' => 'da', 'div' => 'dv', 'nld' => 'nl', 'dzo' => 'dz', 'eng' => 'en', 'epo' => 'eo', 'est' => 'et', + 'ewe' => 'ee', 'fao' => 'fo', 'fij' => 'fj', 'fin' => 'fi', 'fra' => 'fr', 'fry' => 'fy', 'ful' => 'ff', 'kat' => 'ka', 'deu' => 'de', 'gla' => 'gd', 'gle' => 'ga', + 'glg' => 'gl', 'glv' => 'gv', 'ell' => 'el', 'grn' => 'gn', 'guj' => 'gu', 'hat' => 'ht', 'hau' => 'ha', 'heb' => 'he', 'her' => 'hz', 'hin' => 'hi', 'hmo' => 'ho', + 'hrv' => 'hr', 'hun' => 'hu', 'ibo' => 'ig', 'isl' => 'is', 'ido' => 'io', 'iii' => 'ii', 'iku' => 'iu', 'ile' => 'ie', 'ina' => 'ia', 'ind' => 'id', 'ipk' => 'ik', + 'ita' => 'it', 'jav' => 'jv', 'jpn' => 'ja', 'kal' => 'kl', 'kan' => 'kn', 'kas' => 'ks', 'kau' => 'kr', 'kaz' => 'kk', 'khm' => 'km', 'kik' => 'ki', 'kin' => 'rw', + 'kir' => 'ky', 'kom' => 'kv', 'kon' => 'kg', 'kor' => 'ko', 'kua' => 'kj', 'kur' => 'ku', 'lao' => 'lo', 'lat' => 'la', 'lav' => 'lv', 'lim' => 'li', 'lin' => 'ln', + 'lit' => 'lt', 'ltz' => 'lb', 'lub' => 'lu', 'lug' => 'lg', 'mkd' => 'mk', 'mah' => 'mh', 'mal' => 'ml', 'mri' => 'mi', 'mar' => 'mr', 'msa' => 'ms', 'mlg' => 'mg', + 'mlt' => 'mt', 'mon' => 'mn', 'nau' => 'na', 'nav' => 'nv', 'nbl' => 'nr', 'nde' => 'nd', 'ndo' => 'ng', 'nep' => 'ne', 'nno' => 'nn', 'nob' => 'nb', 'nor' => 'no', + 'nya' => 'ny', 'oci' => 'oc', 'oji' => 'oj', 'ori' => 'or', 'orm' => 'om', 'oss' => 'os', 'pan' => 'pa', 'fas' => 'fa', 'pli' => 'pi', 'pol' => 'pl', 'por' => 'pt', + 'pus' => 'ps', 'que' => 'qu', 'roh' => 'rm', 'ron' => 'ro', 'run' => 'rn', 'rus' => 'ru', 'sag' => 'sg', 'san' => 'sa', 'sin' => 'si', 'slk' => 'sk', 'slv' => 'sl', + 'sme' => 'se', 'smo' => 'sm', 'sna' => 'sn', 'snd' => 'sd', 'som' => 'so', 'sot' => 'st', 'spa' => 'es', 'srd' => 'sc', 'srp' => 'sr', 'ssw' => 'ss', 'sun' => 'su', + 'swa' => 'sw', 'swe' => 'sv', 'tah' => 'ty', 'tam' => 'ta', 'tat' => 'tt', 'tel' => 'te', 'tgk' => 'tg', 'tgl' => 'tl', 'tha' => 'th', 'bod' => 'bo', 'tir' => 'ti', + 'ton' => 'to', 'tsn' => 'tn', 'tso' => 'ts', 'tuk' => 'tk', 'tur' => 'tr', 'twi' => 'tw', 'uig' => 'ug', 'ukr' => 'uk', 'urd' => 'ur', 'uzb' => 'uz', 'ven' => 've', + 'vie' => 'vi', 'vol' => 'vo', 'cym' => 'cy', 'wln' => 'wa', 'wol' => 'wo', 'xho' => 'xh', 'yid' => 'yi', 'yor' => 'yo', 'zha' => 'za', 'zul' => 'zu'); + $langs1 = array(); + foreach ($mapping as $c2 => $c1) { + $langs1[$c1] = $langs2[$c2]; + } + ksort($langs1); + return $langs1; + + } else { + debugging('Unsupported $standard parameter in get_list_of_languages() method: '.$standard); + } + + return array(); + } + + /** + * Checks if the translation exists for the language + * + * @param string $lang moodle translation language code + * @param bool $includeall include also disabled translations + * @return bool true if exists + */ + public function translation_exists($lang, $includeall = true) { + + if (strpos($lang, '_local') !== false) { + // Local packs are not real translations. + return false; + } + if (!$includeall and !empty($this->translist)) { + if (!in_array($lang, $this->translist)) { + return false; + } + } + if ($lang === 'en') { + // Part of distribution. + return true; + } + return file_exists("$this->otherroot/$lang/langconfig.php"); + } + + /** + * Returns localised list of installed translations + * + * @param bool $returnall return all or just enabled + * @return array moodle translation code => localised translation name + */ + public function get_list_of_translations($returnall = false) { + global $CFG; + + $languages = array(); + + if (!empty($CFG->langcache) and is_readable($this->menucache)) { + // Try to re-use the cached list of all available languages. + $cachedlist = json_decode(file_get_contents($this->menucache), true); + + if (is_array($cachedlist) and !empty($cachedlist)) { + // The cache file is restored correctly. + + if (!$returnall and !empty($this->translist)) { + // Return just enabled translations. + foreach ($cachedlist as $langcode => $langname) { + if (in_array($langcode, $this->translist)) { + $languages[$langcode] = $langname; + } + } + return $languages; + + } else { + // Return all translations. + return $cachedlist; + } + } + } + + // The cached list of languages is not available, let us populate the list. + if (!$returnall and !empty($this->translist)) { + // Return only some translations. + foreach ($this->translist as $lang) { + $lang = trim($lang); // Just trim spaces to be a bit more permissive. + if (strstr($lang, '_local') !== false) { + continue; + } + if (strstr($lang, '_utf8') !== false) { + continue; + } + if ($lang !== 'en' and !file_exists("$this->otherroot/$lang/langconfig.php")) { + // Some broken or missing lang - can not switch to it anyway. + continue; + } + $string = $this->load_component_strings('langconfig', $lang); + if (!empty($string['thislanguage'])) { + $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; + } + unset($string); + } + + } else { + // Return all languages available in system. + $langdirs = get_list_of_plugins('', '', $this->otherroot); + + $langdirs = array_merge($langdirs, array("$CFG->dirroot/lang/en" => 'en')); + // Sort all. + + // Loop through all langs and get info. + foreach ($langdirs as $lang) { + if (strstr($lang, '_local') !== false) { + continue; + } + if (strstr($lang, '_utf8') !== false) { + continue; + } + $string = $this->load_component_strings('langconfig', $lang); + if (!empty($string['thislanguage'])) { + $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; + } + unset($string); + } + + if (!empty($CFG->langcache) and !empty($this->menucache)) { + // Cache the list so that it can be used next time. + core_collator::asort($languages); + check_dir_exists(dirname($this->menucache), true, true); + file_put_contents($this->menucache, json_encode($languages)); + @chmod($this->menucache, $CFG->filepermissions); + } + } + + core_collator::asort($languages); + + return $languages; + } + + /** + * Returns localised list of currencies. + * + * @param string $lang moodle translation language, null means use current + * @return array currency code => localised currency name + */ + public function get_list_of_currencies($lang = null) { + if ($lang === null) { + $lang = current_language(); + } + + $currencies = $this->load_component_strings('core_currencies', $lang); + asort($currencies); + + return $currencies; + } + + /** + * Clears both in-memory and on-disk caches + * @param bool $phpunitreset true means called from our PHPUnit integration test reset + */ + public function reset_caches($phpunitreset = false) { + global $CFG; + require_once("$CFG->libdir/filelib.php"); + + // Clear the on-disk disk with aggregated string files. + $this->cache->purge(); + + if (!$phpunitreset) { + // Increment the revision counter. + $langrev = get_config('core', 'langrev'); + $next = time(); + if ($langrev !== false and $next <= $langrev and $langrev - $next < 60*60) { + // This resolves problems when reset is requested repeatedly within 1s, + // the < 1h condition prevents accidental switching to future dates + // because we might not recover from it. + $next = $langrev+1; + } + set_config('langrev', $next); + } + + // Clear the cache containing the list of available translations + // and re-populate it again. + fulldelete($this->menucache); + $this->get_list_of_translations(true); + + // Lang packs use PHP files in dataroot, it is better to invalidate opcode caches. + if (function_exists('opcache_reset')) { + opcache_reset(); + } + } + + /** + * Returns string revision counter, this is incremented after any string cache reset. + * @return int lang string revision counter, -1 if unknown + */ + public function get_revision() { + global $CFG; + if (isset($CFG->langrev)) { + return (int)$CFG->langrev; + } else { + return -1; + } + } + + // End of external API. + + /** + * Helper method that recursively loads all parents of the given language. + * + * @see self::get_language_dependencies() + * @param string $lang language code + * @param array $stack list of parent languages already populated in previous recursive calls + * @return array list of all parents of the given language with the $lang itself added as the last element + */ + protected function populate_parent_languages($lang, array $stack = array()) { + + // English does not have a parent language. + if ($lang === 'en') { + return $stack; + } + + // Prevent circular dependency (and thence the infinitive recursion loop). + if (in_array($lang, $stack)) { + return $stack; + } + + // Load language configuration and look for the explicit parent language. + if (!file_exists("$this->otherroot/$lang/langconfig.php")) { + return $stack; + } + $string = array(); + include("$this->otherroot/$lang/langconfig.php"); + + if (empty($string['parentlanguage']) or $string['parentlanguage'] === 'en') { + unset($string); + return array_merge(array($lang), $stack); + + } else { + $parentlang = $string['parentlanguage']; + unset($string); + return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack)); + } + } +} diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 9c1bae11665..dfe840c7078 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6616,7 +6616,7 @@ function get_parent_language($lang=null) { * * @category string * @param bool $forcereload shall the singleton be released and new instance created instead? - * @return string_manager + * @return core_string_manager */ function get_string_manager($forcereload=false) { global $CFG; @@ -6641,971 +6641,17 @@ function get_string_manager($forcereload=false) { $langmenucache = $CFG->langmenucachefile; } - $singleton = new core_string_manager($CFG->langotherroot, $CFG->langlocalroot, - !empty($CFG->langstringcache), $translist, $langmenucache); + $singleton = new core_string_manager_standard($CFG->langotherroot, $CFG->langlocalroot, + !empty($CFG->langstringcache), $translist, $langmenucache); } else { - $singleton = new install_string_manager(); + $singleton = new core_string_manager_install(); } } return $singleton; } - -/** - * Interface for string manager - * - * Interface describing class which is responsible for getting - * of localised strings from language packs. - * - * @package core - * @copyright 2010 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -interface string_manager { - /** - * Get String returns a requested string - * - * @param string $identifier The identifier of the string to search for - * @param string $component The module the string is associated with - * @param string|object|array $a An object, string or number that can be used - * within translation strings - * @param string $lang moodle translation language, null means use current - * @return string The String ! - */ - public function get_string($identifier, $component = '', $a = null, $lang = null); - - /** - * Does the string actually exist? - * - * get_string() is throwing debug warnings, sometimes we do not want them - * or we want to display better explanation of the problem. - * - * Use with care! - * - * @param string $identifier The identifier of the string to search for - * @param string $component The module the string is associated with - * @return boot true if exists - */ - public function string_exists($identifier, $component); - - /** - * Returns a localised list of all country names, sorted by country keys. - * @param bool $returnall return all or just enabled - * @param string $lang moodle translation language, null means use current - * @return array two-letter country code => translated name. - */ - public function get_list_of_countries($returnall = false, $lang = null); - - /** - * Returns a localised list of languages, sorted by code keys. - * - * @param string $lang moodle translation language, null means use current - * @param string $standard language list standard - * iso6392: three-letter language code (ISO 639-2/T) => translated name. - * @return array language code => translated name - */ - public function get_list_of_languages($lang = null, $standard = 'iso6392'); - - /** - * Checks if the translation exists for the language - * - * @param string $lang moodle translation language code - * @param bool $includeall include also disabled translations - * @return bool true if exists - */ - public function translation_exists($lang, $includeall = true); - - /** - * Returns localised list of installed translations - * @param bool $returnall return all or just enabled - * @return array moodle translation code => localised translation name - */ - public function get_list_of_translations($returnall = false); - - /** - * Returns localised list of currencies. - * - * @param string $lang moodle translation language, null means use current - * @return array currency code => localised currency name - */ - public function get_list_of_currencies($lang = null); - - /** - * Load all strings for one component - * @param string $component The module the string is associated with - * @param string $lang - * @param bool $disablecache Do not use caches, force fetching the strings from sources - * @param bool $disablelocal Do not use customized strings in xx_local language packs - * @return array of all string for given component and lang - */ - public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false); - - /** - * Invalidates all caches, should the implementation use any - * @param bool $phpunitreset true means called from our PHPUnit integration test reset - */ - public function reset_caches($phpunitreset = false); - - /** - * Returns string revision counter, this is incremented after any - * string cache reset. - * @return int lang string revision counter, -1 if unknown - */ - public function get_revision(); -} - - -/** - * Standard string_manager implementation - * - * Implements string_manager with getting and printing localised strings - * - * @package core - * @category string - * @copyright 2010 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class core_string_manager implements string_manager { - /** @var string location of all packs except 'en' */ - protected $otherroot; - /** @var string location of all lang pack local modifications */ - protected $localroot; - /** @var cache lang string cache - it will be optimised more later */ - protected $cache; - /** @var int get_string() counter */ - protected $countgetstring = 0; - /** @var bool use disk cache */ - protected $usecache; - /** @var array limit list of translations */ - protected $translist; - /** @var string location of a file that caches the list of available translations */ - protected $menucache; - - /** - * Create new instance of string manager - * - * @param string $otherroot location of downlaoded lang packs - usually $CFG->dataroot/lang - * @param string $localroot usually the same as $otherroot - * @param bool $usecache use disk cache - * @param array $translist limit list of visible translations - * @param string $menucache the location of a file that caches the list of available translations - */ - public function __construct($otherroot, $localroot, $usecache, $translist, $menucache) { - $this->otherroot = $otherroot; - $this->localroot = $localroot; - $this->usecache = $usecache; - $this->translist = $translist; - $this->menucache = $menucache; - - if ($this->usecache) { - // We can use a proper cache, establish the cache using the 'String cache' definition. - $this->cache = cache::make('core', 'string'); - } else { - // We only want a cache for the length of the request, create a static cache. - $options = array( - 'simplekeys' => true, - 'simpledata' => true - ); - $this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string', array(), $options); - } - } - - /** - * Returns list of all explicit parent languages for the given language. - * - * English (en) is considered as the top implicit parent of all language packs - * and is not included in the returned list. The language itself is appended to the - * end of the list. The method is aware of circular dependency risk. - * - * @see self::populate_parent_languages() - * @param string $lang the code of the language - * @return array all explicit parent languages with the lang itself appended - */ - public function get_language_dependencies($lang) { - return $this->populate_parent_languages($lang); - } - - /** - * Load all strings for one component - * - * @param string $component The module the string is associated with - * @param string $lang - * @param bool $disablecache Do not use caches, force fetching the strings from sources - * @param bool $disablelocal Do not use customized strings in xx_local language packs - * @return array of all string for given component and lang - */ - public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false) { - global $CFG; - - list($plugintype, $pluginname) = core_component::normalize_component($component); - if ($plugintype == 'core' and is_null($pluginname)) { - $component = 'core'; - } else { - $component = $plugintype . '_' . $pluginname; - } - - $cachekey = $lang.'_'.$component; - - if (!$disablecache and !$disablelocal) { - $string = $this->cache->get($cachekey); - if ($string) { - return $string; - } - } - - // No cache found - let us merge all possible sources of the strings. - if ($plugintype === 'core') { - $file = $pluginname; - if ($file === null) { - $file = 'moodle'; - } - $string = array(); - // First load english pack. - if (!file_exists("$CFG->dirroot/lang/en/$file.php")) { - return array(); - } - include("$CFG->dirroot/lang/en/$file.php"); - $originalkeys = array_keys($string); - $originalkeys = array_flip($originalkeys); - - // And then corresponding local if present and allowed. - if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { - include("$this->localroot/en_local/$file.php"); - } - // Now loop through all langs in correct order. - $deps = $this->get_language_dependencies($lang); - foreach ($deps as $dep) { - // The main lang string location. - if (file_exists("$this->otherroot/$dep/$file.php")) { - include("$this->otherroot/$dep/$file.php"); - } - if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) { - include("$this->localroot/{$dep}_local/$file.php"); - } - } - - } else { - if (!$location = core_component::get_plugin_directory($plugintype, $pluginname) or !is_dir($location)) { - return array(); - } - if ($plugintype === 'mod') { - // Bloody mod hack. - $file = $pluginname; - } else { - $file = $plugintype . '_' . $pluginname; - } - $string = array(); - // First load English pack. - if (!file_exists("$location/lang/en/$file.php")) { - // English pack does not exist, so do not try to load anything else. - return array(); - } - include("$location/lang/en/$file.php"); - $originalkeys = array_keys($string); - $originalkeys = array_flip($originalkeys); - // And then corresponding local english if present. - if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { - include("$this->localroot/en_local/$file.php"); - } - - // Now loop through all langs in correct order. - $deps = $this->get_language_dependencies($lang); - foreach ($deps as $dep) { - // Legacy location - used by contrib only. - if (file_exists("$location/lang/$dep/$file.php")) { - include("$location/lang/$dep/$file.php"); - } - // The main lang string location. - if (file_exists("$this->otherroot/$dep/$file.php")) { - include("$this->otherroot/$dep/$file.php"); - } - // Local customisations. - if (!$disablelocal and file_exists("$this->localroot/{$dep}_local/$file.php")) { - include("$this->localroot/{$dep}_local/$file.php"); - } - } - } - - // We do not want any extra strings from other languages - everything must be in en lang pack. - $string = array_intersect_key($string, $originalkeys); - - if (!$disablelocal) { - // Now we have a list of strings from all possible sources. put it into both in-memory and on-disk - // caches so we do not need to do all this merging and dependencies resolving again. - $this->cache->set($cachekey, $string); - } - return $string; - } - - /** - * Does the string actually exist? - * - * get_string() is throwing debug warnings, sometimes we do not want them - * or we want to display better explanation of the problem. - * Note: Use with care! - * - * @param string $identifier The identifier of the string to search for - * @param string $component The module the string is associated with - * @return boot true if exists - */ - public function string_exists($identifier, $component) { - $lang = current_language(); - $string = $this->load_component_strings($component, $lang); - return isset($string[$identifier]); - } - - /** - * Get String returns a requested string - * - * @param string $identifier The identifier of the string to search for - * @param string $component The module the string is associated with - * @param string|object|array $a An object, string or number that can be used - * within translation strings - * @param string $lang moodle translation language, null means use current - * @return string The String ! - */ - public function get_string($identifier, $component = '', $a = null, $lang = null) { - $this->countgetstring++; - // There are very many uses of these time formating strings without the 'langconfig' component, - // it would not be reasonable to expect that all of them would be converted during 2.0 migration. - static $langconfigstrs = array( - 'strftimedate' => 1, - 'strftimedatefullshort' => 1, - 'strftimedateshort' => 1, - 'strftimedatetime' => 1, - 'strftimedatetimeshort' => 1, - 'strftimedaydate' => 1, - 'strftimedaydatetime' => 1, - 'strftimedayshort' => 1, - 'strftimedaytime' => 1, - 'strftimemonthyear' => 1, - 'strftimerecent' => 1, - 'strftimerecentfull' => 1, - 'strftimetime' => 1); - - if (empty($component)) { - if (isset($langconfigstrs[$identifier])) { - $component = 'langconfig'; - } else { - $component = 'moodle'; - } - } - - if ($lang === null) { - $lang = current_language(); - } - - $string = $this->load_component_strings($component, $lang); - - if (!isset($string[$identifier])) { - if ($component === 'pix' or $component === 'core_pix') { - // This component contains only alt tags for emoticons, not all of them are supposed to be defined. - return ''; - } - if ($identifier === 'parentlanguage' and ($component === 'langconfig' or $component === 'core_langconfig')) { - // Identifier parentlanguage is a special string, undefined means use English if not defined. - return 'en'; - } - if ($this->usecache) { - // Maybe the on-disk cache is dirty - let the last attempt be to find the string in original sources, - // do NOT write the results to disk cache because it may end up in race conditions see MDL-31904. - $this->usecache = false; - $string = $this->load_component_strings($component, $lang, true); - $this->usecache = true; - } - if (!isset($string[$identifier])) { - // The string is still missing - should be fixed by developer. - list($plugintype, $pluginname) = core_component::normalize_component($component); - if ($plugintype == 'core') { - $file = "lang/en/{$component}.php"; - } else if ($plugintype == 'mod') { - $file = "mod/{$pluginname}/lang/en/{$pluginname}.php"; - } else { - $path = core_component::get_plugin_directory($plugintype, $pluginname); - $file = "{$path}/lang/en/{$plugintype}_{$pluginname}.php"; - } - debugging("Invalid get_string() identifier: '{$identifier}' or component '{$component}'. " . - "Perhaps you are missing \$string['{$identifier}'] = ''; in {$file}?", DEBUG_DEVELOPER); - return "[[$identifier]]"; - } - } - - $string = $string[$identifier]; - - if ($a !== null) { - // Process array's and objects (except lang_strings). - if (is_array($a) or (is_object($a) && !($a instanceof lang_string))) { - $a = (array)$a; - $search = array(); - $replace = array(); - foreach ($a as $key => $value) { - if (is_int($key)) { - // We do not support numeric keys - sorry! - continue; - } - if (is_array($value) or (is_object($value) && !($value instanceof lang_string))) { - // We support just string or lang_string as value. - continue; - } - $search[] = '{$a->'.$key.'}'; - $replace[] = (string)$value; - } - if ($search) { - $string = str_replace($search, $replace, $string); - } - } else { - $string = str_replace('{$a}', (string)$a, $string); - } - } - - return $string; - } - - /** - * Returns information about the string_manager performance. - * - * @return array - */ - public function get_performance_summary() { - return array(array( - 'langcountgetstring' => $this->countgetstring, - ), array( - 'langcountgetstring' => 'get_string calls', - )); - } - - /** - * Returns a localised list of all country names, sorted by localised name. - * - * @param bool $returnall return all or just enabled - * @param string $lang moodle translation language, null means use current - * @return array two-letter country code => translated name. - */ - public function get_list_of_countries($returnall = false, $lang = null) { - global $CFG; - - if ($lang === null) { - $lang = current_language(); - } - - $countries = $this->load_component_strings('core_countries', $lang); - core_collator::asort($countries); - if (!$returnall and !empty($CFG->allcountrycodes)) { - $enabled = explode(',', $CFG->allcountrycodes); - $return = array(); - foreach ($enabled as $c) { - if (isset($countries[$c])) { - $return[$c] = $countries[$c]; - } - } - return $return; - } - - return $countries; - } - - /** - * Returns a localised list of languages, sorted by code keys. - * - * @param string $lang moodle translation language, null means use current - * @param string $standard language list standard - * - iso6392: three-letter language code (ISO 639-2/T) => translated name - * - iso6391: two-letter langauge code (ISO 639-1) => translated name - * @return array language code => translated name - */ - public function get_list_of_languages($lang = null, $standard = 'iso6391') { - if ($lang === null) { - $lang = current_language(); - } - - if ($standard === 'iso6392') { - $langs = $this->load_component_strings('core_iso6392', $lang); - ksort($langs); - return $langs; - - } else if ($standard === 'iso6391') { - $langs2 = $this->load_component_strings('core_iso6392', $lang); - static $mapping = array('aar' => 'aa', 'abk' => 'ab', 'afr' => 'af', 'aka' => 'ak', 'sqi' => 'sq', 'amh' => 'am', 'ara' => 'ar', 'arg' => 'an', 'hye' => 'hy', - 'asm' => 'as', 'ava' => 'av', 'ave' => 'ae', 'aym' => 'ay', 'aze' => 'az', 'bak' => 'ba', 'bam' => 'bm', 'eus' => 'eu', 'bel' => 'be', 'ben' => 'bn', 'bih' => 'bh', - 'bis' => 'bi', 'bos' => 'bs', 'bre' => 'br', 'bul' => 'bg', 'mya' => 'my', 'cat' => 'ca', 'cha' => 'ch', 'che' => 'ce', 'zho' => 'zh', 'chu' => 'cu', 'chv' => 'cv', - 'cor' => 'kw', 'cos' => 'co', 'cre' => 'cr', 'ces' => 'cs', 'dan' => 'da', 'div' => 'dv', 'nld' => 'nl', 'dzo' => 'dz', 'eng' => 'en', 'epo' => 'eo', 'est' => 'et', - 'ewe' => 'ee', 'fao' => 'fo', 'fij' => 'fj', 'fin' => 'fi', 'fra' => 'fr', 'fry' => 'fy', 'ful' => 'ff', 'kat' => 'ka', 'deu' => 'de', 'gla' => 'gd', 'gle' => 'ga', - 'glg' => 'gl', 'glv' => 'gv', 'ell' => 'el', 'grn' => 'gn', 'guj' => 'gu', 'hat' => 'ht', 'hau' => 'ha', 'heb' => 'he', 'her' => 'hz', 'hin' => 'hi', 'hmo' => 'ho', - 'hrv' => 'hr', 'hun' => 'hu', 'ibo' => 'ig', 'isl' => 'is', 'ido' => 'io', 'iii' => 'ii', 'iku' => 'iu', 'ile' => 'ie', 'ina' => 'ia', 'ind' => 'id', 'ipk' => 'ik', - 'ita' => 'it', 'jav' => 'jv', 'jpn' => 'ja', 'kal' => 'kl', 'kan' => 'kn', 'kas' => 'ks', 'kau' => 'kr', 'kaz' => 'kk', 'khm' => 'km', 'kik' => 'ki', 'kin' => 'rw', - 'kir' => 'ky', 'kom' => 'kv', 'kon' => 'kg', 'kor' => 'ko', 'kua' => 'kj', 'kur' => 'ku', 'lao' => 'lo', 'lat' => 'la', 'lav' => 'lv', 'lim' => 'li', 'lin' => 'ln', - 'lit' => 'lt', 'ltz' => 'lb', 'lub' => 'lu', 'lug' => 'lg', 'mkd' => 'mk', 'mah' => 'mh', 'mal' => 'ml', 'mri' => 'mi', 'mar' => 'mr', 'msa' => 'ms', 'mlg' => 'mg', - 'mlt' => 'mt', 'mon' => 'mn', 'nau' => 'na', 'nav' => 'nv', 'nbl' => 'nr', 'nde' => 'nd', 'ndo' => 'ng', 'nep' => 'ne', 'nno' => 'nn', 'nob' => 'nb', 'nor' => 'no', - 'nya' => 'ny', 'oci' => 'oc', 'oji' => 'oj', 'ori' => 'or', 'orm' => 'om', 'oss' => 'os', 'pan' => 'pa', 'fas' => 'fa', 'pli' => 'pi', 'pol' => 'pl', 'por' => 'pt', - 'pus' => 'ps', 'que' => 'qu', 'roh' => 'rm', 'ron' => 'ro', 'run' => 'rn', 'rus' => 'ru', 'sag' => 'sg', 'san' => 'sa', 'sin' => 'si', 'slk' => 'sk', 'slv' => 'sl', - 'sme' => 'se', 'smo' => 'sm', 'sna' => 'sn', 'snd' => 'sd', 'som' => 'so', 'sot' => 'st', 'spa' => 'es', 'srd' => 'sc', 'srp' => 'sr', 'ssw' => 'ss', 'sun' => 'su', - 'swa' => 'sw', 'swe' => 'sv', 'tah' => 'ty', 'tam' => 'ta', 'tat' => 'tt', 'tel' => 'te', 'tgk' => 'tg', 'tgl' => 'tl', 'tha' => 'th', 'bod' => 'bo', 'tir' => 'ti', - 'ton' => 'to', 'tsn' => 'tn', 'tso' => 'ts', 'tuk' => 'tk', 'tur' => 'tr', 'twi' => 'tw', 'uig' => 'ug', 'ukr' => 'uk', 'urd' => 'ur', 'uzb' => 'uz', 'ven' => 've', - 'vie' => 'vi', 'vol' => 'vo', 'cym' => 'cy', 'wln' => 'wa', 'wol' => 'wo', 'xho' => 'xh', 'yid' => 'yi', 'yor' => 'yo', 'zha' => 'za', 'zul' => 'zu'); - $langs1 = array(); - foreach ($mapping as $c2 => $c1) { - $langs1[$c1] = $langs2[$c2]; - } - ksort($langs1); - return $langs1; - - } else { - debugging('Unsupported $standard parameter in get_list_of_languages() method: '.$standard); - } - - return array(); - } - - /** - * Checks if the translation exists for the language - * - * @param string $lang moodle translation language code - * @param bool $includeall include also disabled translations - * @return bool true if exists - */ - public function translation_exists($lang, $includeall = true) { - - if (strpos($lang, '_local') !== false) { - // Local packs are not real translations. - return false; - } - if (!$includeall and !empty($this->translist)) { - if (!in_array($lang, $this->translist)) { - return false; - } - } - if ($lang === 'en') { - // Part of distribution. - return true; - } - return file_exists("$this->otherroot/$lang/langconfig.php"); - } - - /** - * Returns localised list of installed translations - * - * @param bool $returnall return all or just enabled - * @return array moodle translation code => localised translation name - */ - public function get_list_of_translations($returnall = false) { - global $CFG; - - $languages = array(); - - if (!empty($CFG->langcache) and is_readable($this->menucache)) { - // Try to re-use the cached list of all available languages. - $cachedlist = json_decode(file_get_contents($this->menucache), true); - - if (is_array($cachedlist) and !empty($cachedlist)) { - // The cache file is restored correctly. - - if (!$returnall and !empty($this->translist)) { - // Return just enabled translations. - foreach ($cachedlist as $langcode => $langname) { - if (in_array($langcode, $this->translist)) { - $languages[$langcode] = $langname; - } - } - return $languages; - - } else { - // Return all translations. - return $cachedlist; - } - } - } - - // The cached list of languages is not available, let us populate the list. - if (!$returnall and !empty($this->translist)) { - // Return only some translations. - foreach ($this->translist as $lang) { - $lang = trim($lang); // Just trim spaces to be a bit more permissive. - if (strstr($lang, '_local') !== false) { - continue; - } - if (strstr($lang, '_utf8') !== false) { - continue; - } - if ($lang !== 'en' and !file_exists("$this->otherroot/$lang/langconfig.php")) { - // Some broken or missing lang - can not switch to it anyway. - continue; - } - $string = $this->load_component_strings('langconfig', $lang); - if (!empty($string['thislanguage'])) { - $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; - } - unset($string); - } - - } else { - // Return all languages available in system. - $langdirs = get_list_of_plugins('', '', $this->otherroot); - - $langdirs = array_merge($langdirs, array("$CFG->dirroot/lang/en" => 'en')); - // Sort all. - - // Loop through all langs and get info. - foreach ($langdirs as $lang) { - if (strstr($lang, '_local') !== false) { - continue; - } - if (strstr($lang, '_utf8') !== false) { - continue; - } - $string = $this->load_component_strings('langconfig', $lang); - if (!empty($string['thislanguage'])) { - $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; - } - unset($string); - } - - if (!empty($CFG->langcache) and !empty($this->menucache)) { - // Cache the list so that it can be used next time. - core_collator::asort($languages); - check_dir_exists(dirname($this->menucache), true, true); - file_put_contents($this->menucache, json_encode($languages)); - @chmod($this->menucache, $CFG->filepermissions); - } - } - - core_collator::asort($languages); - - return $languages; - } - - /** - * Returns localised list of currencies. - * - * @param string $lang moodle translation language, null means use current - * @return array currency code => localised currency name - */ - public function get_list_of_currencies($lang = null) { - if ($lang === null) { - $lang = current_language(); - } - - $currencies = $this->load_component_strings('core_currencies', $lang); - asort($currencies); - - return $currencies; - } - - /** - * Clears both in-memory and on-disk caches - * @param bool $phpunitreset true means called from our PHPUnit integration test reset - */ - public function reset_caches($phpunitreset = false) { - global $CFG; - require_once("$CFG->libdir/filelib.php"); - - // Clear the on-disk disk with aggregated string files. - $this->cache->purge(); - - if (!$phpunitreset) { - // Increment the revision counter. - $langrev = get_config('core', 'langrev'); - $next = time(); - if ($langrev !== false and $next <= $langrev and $langrev - $next < 60*60) { - // This resolves problems when reset is requested repeatedly within 1s, - // the < 1h condition prevents accidental switching to future dates - // because we might not recover from it. - $next = $langrev+1; - } - set_config('langrev', $next); - } - - // Clear the cache containing the list of available translations - // and re-populate it again. - fulldelete($this->menucache); - $this->get_list_of_translations(true); - - // Lang packs use PHP files in dataroot, it is better to invalidate opcode caches. - if (function_exists('opcache_reset')) { - opcache_reset(); - } - } - - /** - * Returns string revision counter, this is incremented after any string cache reset. - * @return int lang string revision counter, -1 if unknown - */ - public function get_revision() { - global $CFG; - if (isset($CFG->langrev)) { - return (int)$CFG->langrev; - } else { - return -1; - } - } - - // End of external API. - - /** - * Helper method that recursively loads all parents of the given language. - * - * @see self::get_language_dependencies() - * @param string $lang language code - * @param array $stack list of parent languages already populated in previous recursive calls - * @return array list of all parents of the given language with the $lang itself added as the last element - */ - protected function populate_parent_languages($lang, array $stack = array()) { - - // English does not have a parent language. - if ($lang === 'en') { - return $stack; - } - - // Prevent circular dependency (and thence the infinitive recursion loop). - if (in_array($lang, $stack)) { - return $stack; - } - - // Load language configuration and look for the explicit parent language. - if (!file_exists("$this->otherroot/$lang/langconfig.php")) { - return $stack; - } - $string = array(); - include("$this->otherroot/$lang/langconfig.php"); - - if (empty($string['parentlanguage']) or $string['parentlanguage'] === 'en') { - unset($string); - return array_merge(array($lang), $stack); - - } else { - $parentlang = $string['parentlanguage']; - unset($string); - return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack)); - } - } -} - - -/** - * Fetches minimum strings for installation - * - * Minimalistic string fetching implementation - * that is used in installer before we fetch the wanted - * language pack from moodle.org lang download site. - * - * @package core - * @copyright 2010 Petr Skoda (http://skodak.org) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ -class install_string_manager implements string_manager { - /** @var string location of pre-install packs for all langs */ - protected $installroot; - - /** - * Crate new instance of install string manager - */ - public function __construct() { - global $CFG; - $this->installroot = "$CFG->dirroot/install/lang"; - } - - /** - * Load all strings for one component - * @param string $component The module the string is associated with - * @param string $lang - * @param bool $disablecache Do not use caches, force fetching the strings from sources - * @param bool $disablelocal Do not use customized strings in xx_local language packs - * @return array of all string for given component and lang - */ - public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false) { - // Not needed in installer. - return array(); - } - - /** - * Does the string actually exist? - * - * get_string() is throwing debug warnings, sometimes we do not want them - * or we want to display better explanation of the problem. - * - * Use with care! - * - * @param string $identifier The identifier of the string to search for - * @param string $component The module the string is associated with - * @return boot true if exists - */ - public function string_exists($identifier, $component) { - // Simple old style hack ;). - $str = get_string($identifier, $component); - return (strpos($str, '[[') === false); - } - - /** - * Get String returns a requested string - * - * @param string $identifier The identifier of the string to search for - * @param string $component The module the string is associated with - * @param string|object|array $a An object, string or number that can be used - * within translation strings - * @param string $lang moodle translation language, null means use current - * @return string The String ! - */ - public function get_string($identifier, $component = '', $a = null, $lang = null) { - if (!$component) { - $component = 'moodle'; - } - - if ($lang === null) { - $lang = current_language(); - } - - // Get parent lang. - $parent = ''; - if ($lang !== 'en' and $identifier !== 'parentlanguage' and $component !== 'langconfig') { - if (file_exists("$this->installroot/$lang/langconfig.php")) { - $string = array(); - include("$this->installroot/$lang/langconfig.php"); - if (isset($string['parentlanguage'])) { - $parent = $string['parentlanguage']; - } - unset($string); - } - } - - // Include en string first. - if (!file_exists("$this->installroot/en/$component.php")) { - return "[[$identifier]]"; - } - $string = array(); - include("$this->installroot/en/$component.php"); - - // Now override en with parent if defined. - if ($parent and $parent !== 'en' and file_exists("$this->installroot/$parent/$component.php")) { - include("$this->installroot/$parent/$component.php"); - } - - // Finally override with requested language. - if ($lang !== 'en' and file_exists("$this->installroot/$lang/$component.php")) { - include("$this->installroot/$lang/$component.php"); - } - - if (!isset($string[$identifier])) { - return "[[$identifier]]"; - } - - $string = $string[$identifier]; - - if ($a !== null) { - if (is_object($a) or is_array($a)) { - $a = (array)$a; - $search = array(); - $replace = array(); - foreach ($a as $key => $value) { - if (is_int($key)) { - // We do not support numeric keys - sorry! - continue; - } - $search[] = '{$a->'.$key.'}'; - $replace[] = (string)$value; - } - if ($search) { - $string = str_replace($search, $replace, $string); - } - } else { - $string = str_replace('{$a}', (string)$a, $string); - } - } - - return $string; - } - - /** - * Returns a localised list of all country names, sorted by country keys. - * - * @param bool $returnall return all or just enabled - * @param string $lang moodle translation language, null means use current - * @return array two-letter country code => translated name. - */ - public function get_list_of_countries($returnall = false, $lang = null) { - // Not used in installer. - return array(); - } - - /** - * Returns a localised list of languages, sorted by code keys. - * - * @param string $lang moodle translation language, null means use current - * @param string $standard language list standard - * iso6392: three-letter language code (ISO 639-2/T) => translated name. - * @return array language code => translated name - */ - public function get_list_of_languages($lang = null, $standard = 'iso6392') { - // Not used in installer. - return array(); - } - - /** - * Checks if the translation exists for the language - * - * @param string $lang moodle translation language code - * @param bool $includeall include also disabled translations - * @return bool true if exists - */ - public function translation_exists($lang, $includeall = true) { - return file_exists($this->installroot.'/'.$lang.'/langconfig.php'); - } - - /** - * Returns localised list of installed translations - * @param bool $returnall return all or just enabled - * @return array moodle translation code => localised translation name - */ - public function get_list_of_translations($returnall = false) { - // Return all is ignored here - we need to know all langs in installer. - $languages = array(); - // Get raw list of lang directories. - $langdirs = get_list_of_plugins('install/lang'); - asort($langdirs); - // Get some info from each lang. - foreach ($langdirs as $lang) { - if (file_exists($this->installroot.'/'.$lang.'/langconfig.php')) { - $string = array(); - include($this->installroot.'/'.$lang.'/langconfig.php'); - if (!empty($string['thislanguage'])) { - $languages[$lang] = $string['thislanguage'].' ('.$lang.')'; - } - } - } - // Return array. - return $languages; - } - - /** - * Returns localised list of currencies. - * - * @param string $lang moodle translation language, null means use current - * @return array currency code => localised currency name - */ - public function get_list_of_currencies($lang = null) { - // Not used in installer. - return array(); - } - - /** - * This implementation does not use any caches. - * - * @param bool $phpunitreset true means called from our PHPUnit integration test reset - */ - public function reset_caches($phpunitreset = false) { - // Nothing to do. - } - - /** - * Returns string revision counter, this is incremented after any string cache reset. - * @return int lang string revision counter, -1 if unknown - */ - public function get_revision() { - return -1; - } -} - - /** * Returns a localized string. * diff --git a/lib/tests/string_manager_test.php b/lib/tests/string_manager_standard_test.php similarity index 95% rename from lib/tests/string_manager_test.php rename to lib/tests/string_manager_standard_test.php index b4ce9492b57..6ae3bbe9d37 100644 --- a/lib/tests/string_manager_test.php +++ b/lib/tests/string_manager_standard_test.php @@ -34,14 +34,14 @@ require_once($CFG->libdir.'/moodlelib.php'); * @copyright 2013 David Mudrak * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class core_string_manager_testcase extends advanced_testcase { +class core_string_manager_standard_testcase extends advanced_testcase { public function test_string_manager_instance() { $this->resetAfterTest(); $otherroot = dirname(__FILE__).'/fixtures/langtest'; $stringman = testable_core_string_manager::instance($otherroot); - $this->assertContains('string_manager', class_implements($stringman)); + $this->assertInstanceOf('core_string_manager', $stringman); } public function test_get_language_dependencies() { @@ -76,7 +76,7 @@ class core_string_manager_testcase extends advanced_testcase { * @copyright 2013 David Mudrak * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -class testable_core_string_manager extends core_string_manager { +class testable_core_string_manager extends core_string_manager_standard { /** * Factory method From 598a82c950c258a476b93952172c5e9d15333114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Sun, 4 Aug 2013 00:01:58 +0200 Subject: [PATCH 2/2] MDL-41019 improve language caching Includes: * no more hacky reloads, everything is written only once and kept until cache reset * lang menu list is now cached in MUC * both string and lang menu caches are compatible with local caches on cluster nodes * config-dist.php cleanup --- config-dist.php | 21 +- lang/en/cache.php | 1 + lib/classes/string_manager_install.php | 1 - lib/classes/string_manager_standard.php | 257 +++++++++++------------- lib/db/caches.php | 12 +- lib/moodlelib.php | 9 +- lib/setuplib.php | 3 +- lib/upgrade.txt | 2 + lib/weblib.php | 7 +- 9 files changed, 134 insertions(+), 179 deletions(-) diff --git a/config-dist.php b/config-dist.php index 2022649d79f..8c3b7339e76 100644 --- a/config-dist.php +++ b/config-dist.php @@ -365,25 +365,6 @@ $CFG->admin = 'admin'; // Locking resolves race conditions and is strongly recommended for production servers. // $CFG->preventfilelocking = false; // -// If $CFG->langstringcache is enabled (which should always be in production -// environment), Moodle keeps aggregated strings in its own internal format -// optimised for performance. By default, this on-disk cache is created in -// $CFG->cachedir/lang. In cluster environment, you may wish to specify -// an alternative location of this cache so that each web server in the cluster -// uses its own local cache and does not need to access the shared dataroot. -// Make sure that the web server process has write permission to this location -// and that it has permission to remove the folder, too (so that the cache can -// be pruned). -// -// $CFG->langcacheroot = '/var/www/moodle/htdocs/altcache/lang'; -// -// If $CFG->langcache is enabled (which should always be in production -// environment), Moodle stores the list of available languages in a cache file. -// By default, the file $CFG->dataroot/languages is used. You may wish to -// specify an alternative location of this cache file. -// -// $CFG->langmenucachefile = '/var/www/moodle/htdocs/altcache/languages'; -// // Site default language can be set via standard administration interface. If you // want to have initial error messages for eventual database connection problems // localized too, you have to set your language code here. @@ -488,7 +469,7 @@ $CFG->admin = 'admin'; // Prevent JS caching // $CFG->jsrev = -1; // NOT FOR PRODUCTION SERVERS! // -// Prevent core_string_manager on-disk cache +// Prevent core_string_manager application caching // $CFG->langstringcache = false; // NOT FOR PRODUCTION SERVERS! // // When working with production data on test servers, no emails or other messages diff --git a/lang/en/cache.php b/lang/en/cache.php index be7bb404c82..a60b503113b 100644 --- a/lang/en/cache.php +++ b/lang/en/cache.php @@ -46,6 +46,7 @@ $string['cachedef_databasemeta'] = 'Database meta information'; $string['cachedef_eventinvalidation'] = 'Event invalidation'; $string['cachedef_groupdata'] = 'Course group information'; $string['cachedef_htmlpurifier'] = 'HTML Purifier - cleaned content'; +$string['cachedef_langmenu'] = 'List of available languages'; $string['cachedef_locking'] = 'Locking'; $string['cachedef_observers'] = 'Event observers'; $string['cachedef_plugininfo_base'] = 'Plugin info - base'; diff --git a/lib/classes/string_manager_install.php b/lib/classes/string_manager_install.php index 84d5971d263..353e5072dae 100644 --- a/lib/classes/string_manager_install.php +++ b/lib/classes/string_manager_install.php @@ -107,7 +107,6 @@ class core_string_manager_install implements core_string_manager { if (isset($string['parentlanguage'])) { $parent = $string['parentlanguage']; } - unset($string); } } diff --git a/lib/classes/string_manager_standard.php b/lib/classes/string_manager_standard.php index 1609d1b1284..7ec3708f4a3 100644 --- a/lib/classes/string_manager_standard.php +++ b/lib/classes/string_manager_standard.php @@ -44,31 +44,30 @@ class core_string_manager_standard implements core_string_manager { /** @var int get_string() counter */ protected $countgetstring = 0; /** @var bool use disk cache */ - protected $usecache; - /** @var array limit list of translations */ protected $translist; - /** @var string location of a file that caches the list of available translations */ + /** @var cache stores list of available translations */ protected $menucache; /** * Create new instance of string manager * - * @param string $otherroot location of downlaoded lang packs - usually $CFG->dataroot/lang + * @param string $otherroot location of downloaded lang packs - usually $CFG->dataroot/lang * @param string $localroot usually the same as $otherroot - * @param bool $usecache use disk cache * @param array $translist limit list of visible translations - * @param string $menucache the location of a file that caches the list of available translations */ - public function __construct($otherroot, $localroot, $usecache, $translist, $menucache) { + public function __construct($otherroot, $localroot, $translist) { $this->otherroot = $otherroot; $this->localroot = $localroot; - $this->usecache = $usecache; - $this->translist = $translist; - $this->menucache = $menucache; + if ($translist) { + $this->translist = array_combine($translist, $translist); + } else { + $this->translist = array(); + } - if ($this->usecache) { + if ($this->get_revision() > 0) { // We can use a proper cache, establish the cache using the 'String cache' definition. $this->cache = cache::make('core', 'string'); + $this->menucache = cache::make('core', 'langmenu'); } else { // We only want a cache for the length of the request, create a static cache. $options = array( @@ -76,6 +75,7 @@ class core_string_manager_standard implements core_string_manager { 'simpledata' => true ); $this->cache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'string', array(), $options); + $this->menucache = cache::make_from_params(cache_store::MODE_REQUEST, 'core', 'langmenu', array(), $options); } } @@ -103,22 +103,22 @@ class core_string_manager_standard implements core_string_manager { * @param bool $disablelocal Do not use customized strings in xx_local language packs * @return array of all string for given component and lang */ - public function load_component_strings($component, $lang, $disablecache=false, $disablelocal=false) { + public function load_component_strings($component, $lang, $disablecache = false, $disablelocal = false) { global $CFG; list($plugintype, $pluginname) = core_component::normalize_component($component); - if ($plugintype == 'core' and is_null($pluginname)) { + if ($plugintype === 'core' and is_null($pluginname)) { $component = 'core'; } else { $component = $plugintype . '_' . $pluginname; } - $cachekey = $lang.'_'.$component; + $cachekey = $lang.'_'.$component.'_'.$this->get_key_suffix(); + $cachedstring = $this->cache->get($cachekey); if (!$disablecache and !$disablelocal) { - $string = $this->cache->get($cachekey); - if ($string) { - return $string; + if ($cachedstring !== false) { + return $cachedstring; } } @@ -134,8 +134,7 @@ class core_string_manager_standard implements core_string_manager { return array(); } include("$CFG->dirroot/lang/en/$file.php"); - $originalkeys = array_keys($string); - $originalkeys = array_flip($originalkeys); + $enstring = $string; // And then corresponding local if present and allowed. if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { @@ -170,8 +169,7 @@ class core_string_manager_standard implements core_string_manager { return array(); } include("$location/lang/en/$file.php"); - $originalkeys = array_keys($string); - $originalkeys = array_flip($originalkeys); + $enstring = $string; // And then corresponding local english if present. if (!$disablelocal and file_exists("$this->localroot/en_local/$file.php")) { include("$this->localroot/en_local/$file.php"); @@ -196,12 +194,14 @@ class core_string_manager_standard implements core_string_manager { } // We do not want any extra strings from other languages - everything must be in en lang pack. - $string = array_intersect_key($string, $originalkeys); + $string = array_intersect_key($string, $enstring); if (!$disablelocal) { - // Now we have a list of strings from all possible sources. put it into both in-memory and on-disk - // caches so we do not need to do all this merging and dependencies resolving again. - $this->cache->set($cachekey, $string); + // Now we have a list of strings from all possible sources, + // cache it in MUC cache if not already there. + if ($cachedstring === false) { + $this->cache->set($cachekey, $string); + } } return $string; } @@ -235,7 +235,7 @@ class core_string_manager_standard implements core_string_manager { */ public function get_string($identifier, $component = '', $a = null, $lang = null) { $this->countgetstring++; - // There are very many uses of these time formating strings without the 'langconfig' component, + // There are very many uses of these time formatting strings without the 'langconfig' component, // it would not be reasonable to expect that all of them would be converted during 2.0 migration. static $langconfigstrs = array( 'strftimedate' => 1, @@ -275,26 +275,23 @@ class core_string_manager_standard implements core_string_manager { // Identifier parentlanguage is a special string, undefined means use English if not defined. return 'en'; } - if ($this->usecache) { - // Maybe the on-disk cache is dirty - let the last attempt be to find the string in original sources, - // do NOT write the results to disk cache because it may end up in race conditions see MDL-31904. - $this->usecache = false; - $string = $this->load_component_strings($component, $lang, true); - $this->usecache = true; - } + // Do not rebuild caches here! + // Devs need to learn to purge all caches after any change or disable $CFG->langstringcache. if (!isset($string[$identifier])) { // The string is still missing - should be fixed by developer. - list($plugintype, $pluginname) = core_component::normalize_component($component); - if ($plugintype == 'core') { - $file = "lang/en/{$component}.php"; - } else if ($plugintype == 'mod') { - $file = "mod/{$pluginname}/lang/en/{$pluginname}.php"; - } else { - $path = core_component::get_plugin_directory($plugintype, $pluginname); - $file = "{$path}/lang/en/{$plugintype}_{$pluginname}.php"; + if (debugging('', DEBUG_DEVELOPER)) { + list($plugintype, $pluginname) = core_component::normalize_component($component); + if ($plugintype === 'core') { + $file = "lang/en/{$component}.php"; + } else if ($plugintype == 'mod') { + $file = "mod/{$pluginname}/lang/en/{$pluginname}.php"; + } else { + $path = core_component::get_plugin_directory($plugintype, $pluginname); + $file = "{$path}/lang/en/{$plugintype}_{$pluginname}.php"; + } + debugging("Invalid get_string() identifier: '{$identifier}' or component '{$component}'. " . + "Perhaps you are missing \$string['{$identifier}'] = ''; in {$file}?", DEBUG_DEVELOPER); } - debugging("Invalid get_string() identifier: '{$identifier}' or component '{$component}'. " . - "Perhaps you are missing \$string['{$identifier}'] = ''; in {$file}?", DEBUG_DEVELOPER); return "[[$identifier]]"; } } @@ -331,7 +328,7 @@ class core_string_manager_standard implements core_string_manager { } /** - * Returns information about the string_manager performance. + * Returns information about the core_string_manager performance. * * @return array */ @@ -379,7 +376,7 @@ class core_string_manager_standard implements core_string_manager { * @param string $lang moodle translation language, null means use current * @param string $standard language list standard * - iso6392: three-letter language code (ISO 639-2/T) => translated name - * - iso6391: two-letter langauge code (ISO 639-1) => translated name + * - iso6391: two-letter language code (ISO 639-1) => translated name * @return array language code => translated name */ public function get_list_of_languages($lang = null, $standard = 'iso6391') { @@ -433,21 +430,8 @@ class core_string_manager_standard implements core_string_manager { * @return bool true if exists */ public function translation_exists($lang, $includeall = true) { - - if (strpos($lang, '_local') !== false) { - // Local packs are not real translations. - return false; - } - if (!$includeall and !empty($this->translist)) { - if (!in_array($lang, $this->translist)) { - return false; - } - } - if ($lang === 'en') { - // Part of distribution. - return true; - } - return file_exists("$this->otherroot/$lang/langconfig.php"); + $translations = $this->get_list_of_translations($includeall); + return isset($translations[$lang]); } /** @@ -461,84 +445,65 @@ class core_string_manager_standard implements core_string_manager { $languages = array(); - if (!empty($CFG->langcache) and is_readable($this->menucache)) { - // Try to re-use the cached list of all available languages. - $cachedlist = json_decode(file_get_contents($this->menucache), true); - - if (is_array($cachedlist) and !empty($cachedlist)) { - // The cache file is restored correctly. - - if (!$returnall and !empty($this->translist)) { - // Return just enabled translations. - foreach ($cachedlist as $langcode => $langname) { - if (in_array($langcode, $this->translist)) { - $languages[$langcode] = $langname; - } - } - return $languages; - - } else { - // Return all translations. - return $cachedlist; + $cachekey = 'list_'.$this->get_key_suffix(); + $cachedlist = $this->menucache->get($cachekey); + if ($cachedlist !== false) { + // The cache content is invalid. + if ($returnall or empty($this->translist)) { + return $cachedlist; + } + // Return only enabled translations. + foreach ($cachedlist as $langcode => $langname) { + if (isset($this->translist[$langcode])) { + $languages[$langcode] = $langname; } } + return $languages; } - // The cached list of languages is not available, let us populate the list. - if (!$returnall and !empty($this->translist)) { - // Return only some translations. - foreach ($this->translist as $lang) { - $lang = trim($lang); // Just trim spaces to be a bit more permissive. - if (strstr($lang, '_local') !== false) { - continue; - } - if (strstr($lang, '_utf8') !== false) { - continue; - } - if ($lang !== 'en' and !file_exists("$this->otherroot/$lang/langconfig.php")) { - // Some broken or missing lang - can not switch to it anyway. - continue; - } - $string = $this->load_component_strings('langconfig', $lang); - if (!empty($string['thislanguage'])) { - $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; - } - unset($string); + // Get all languages available in system. + $langdirs = get_list_of_plugins('', 'en', $this->otherroot); + $langdirs["$CFG->dirroot/lang/en"] = 'en'; + + // Loop through all langs and get info. + foreach ($langdirs as $lang) { + if (strrpos($lang, '_local') !== false) { + // Just a local tweak of some other lang pack. + continue; } - - } else { - // Return all languages available in system. - $langdirs = get_list_of_plugins('', '', $this->otherroot); - - $langdirs = array_merge($langdirs, array("$CFG->dirroot/lang/en" => 'en')); - // Sort all. - - // Loop through all langs and get info. - foreach ($langdirs as $lang) { - if (strstr($lang, '_local') !== false) { - continue; - } - if (strstr($lang, '_utf8') !== false) { - continue; - } - $string = $this->load_component_strings('langconfig', $lang); - if (!empty($string['thislanguage'])) { - $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; - } - unset($string); + if (strrpos($lang, '_utf8') !== false) { + // Legacy 1.x lang pack. + continue; } - - if (!empty($CFG->langcache) and !empty($this->menucache)) { - // Cache the list so that it can be used next time. - core_collator::asort($languages); - check_dir_exists(dirname($this->menucache), true, true); - file_put_contents($this->menucache, json_encode($languages)); - @chmod($this->menucache, $CFG->filepermissions); + if ($lang !== clean_param($lang, PARAM_SAFEDIR)) { + // Invalid lang pack name! + continue; + } + $string = $this->load_component_strings('langconfig', $lang); + if (!empty($string['thislanguage'])) { + $languages[$lang] = $string['thislanguage'].' ('. $lang .')'; } } core_collator::asort($languages); + // Cache the list so that it can be used next time. + $this->menucache->set($cachekey, $languages); + + if ($returnall or empty($this->translist)) { + return $languages; + } + + $cachedlist = $languages; + + // Return only enabled translations. + $languages = array(); + foreach ($cachedlist as $langcode => $langname) { + if (isset($this->translist[$langcode])) { + $languages[$langcode] = $langname; + } + } + return $languages; } @@ -564,11 +529,9 @@ class core_string_manager_standard implements core_string_manager { * @param bool $phpunitreset true means called from our PHPUnit integration test reset */ public function reset_caches($phpunitreset = false) { - global $CFG; - require_once("$CFG->libdir/filelib.php"); - // Clear the on-disk disk with aggregated string files. $this->cache->purge(); + $this->menucache->purge(); if (!$phpunitreset) { // Increment the revision counter. @@ -583,23 +546,37 @@ class core_string_manager_standard implements core_string_manager { set_config('langrev', $next); } - // Clear the cache containing the list of available translations - // and re-populate it again. - fulldelete($this->menucache); - $this->get_list_of_translations(true); - // Lang packs use PHP files in dataroot, it is better to invalidate opcode caches. if (function_exists('opcache_reset')) { opcache_reset(); } } + /** + * Returns cache key suffix, this enables us to store string + lang menu + * caches in local caches on cluster nodes. We can not use prefix because + * it would cause problems when creating subdirs in cache file store. + * @return string + */ + protected function get_key_suffix() { + $rev = $this->get_revision(); + if ($rev < 0) { + // Simple keys do not like minus char. + $rev = 0; + } + + return $rev; + } + /** * Returns string revision counter, this is incremented after any string cache reset. * @return int lang string revision counter, -1 if unknown */ public function get_revision() { global $CFG; + if (empty($CFG->langstringcache)) { + return -1; + } if (isset($CFG->langrev)) { return (int)$CFG->langrev; } else { @@ -607,8 +584,6 @@ class core_string_manager_standard implements core_string_manager { } } - // End of external API. - /** * Helper method that recursively loads all parents of the given language. * @@ -637,13 +612,11 @@ class core_string_manager_standard implements core_string_manager { include("$this->otherroot/$lang/langconfig.php"); if (empty($string['parentlanguage']) or $string['parentlanguage'] === 'en') { - unset($string); return array_merge(array($lang), $stack); - } else { - $parentlang = $string['parentlanguage']; - unset($string); - return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack)); } + + $parentlang = $string['parentlanguage']; + return $this->populate_parent_languages($parentlang, array_merge(array($lang), $stack)); } } diff --git a/lib/db/caches.php b/lib/db/caches.php index 347c824c53b..9f36f54fc44 100644 --- a/lib/db/caches.php +++ b/lib/db/caches.php @@ -29,8 +29,9 @@ $definitions = array( // Used to store processed lang files. - // The keys used are the component of the string file. + // The keys used are the revision, lang and component of the string file. // The persistent max size has been based upon student access of the site. + // NOTE: this data may be safely stored in local caches on cluster nodes. 'string' => array( 'mode' => cache_store::MODE_APPLICATION, 'simplekeys' => true, @@ -39,6 +40,15 @@ $definitions = array( 'persistentmaxsize' => 30 ), + // Used to store cache of all available translations. + // NOTE: this data may be safely stored in local caches on cluster nodes. + 'langmenu' => array( + 'mode' => cache_store::MODE_APPLICATION, + 'simplekeys' => true, + 'simpledata' => true, + 'persistent' => true, + ), + // Used to store database meta information. // The database meta information includes information about tables and there columns. // Its keys are the table names. diff --git a/lib/moodlelib.php b/lib/moodlelib.php index dfe840c7078..2c406c935a9 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -6635,14 +6635,7 @@ function get_string_manager($forcereload=false) { $translist = explode(',', $CFG->langlist); } - if (empty($CFG->langmenucachefile)) { - $langmenucache = $CFG->cachedir . '/languages'; - } else { - $langmenucache = $CFG->langmenucachefile; - } - - $singleton = new core_string_manager_standard($CFG->langotherroot, $CFG->langlocalroot, - !empty($CFG->langstringcache), $translist, $langmenucache); + $singleton = new core_string_manager_standard($CFG->langotherroot, $CFG->langlocalroot, $translist); } else { $singleton = new core_string_manager_install(); diff --git a/lib/setuplib.php b/lib/setuplib.php index a74a1d34c45..13a26e521d4 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -529,8 +529,7 @@ function get_exception_info($ex) { // Remove some absolute paths from message and debugging info. $searches = array(); $replaces = array(); - $cfgnames = array('tempdir', 'cachedir', 'localcachedir', 'themedir', - 'langmenucachefile', 'langcacheroot', 'dataroot', 'dirroot'); + $cfgnames = array('tempdir', 'cachedir', 'localcachedir', 'themedir', 'dataroot', 'dirroot'); foreach ($cfgnames as $cfgname) { if (property_exists($CFG, $cfgname)) { $searches[] = $CFG->$cfgname; diff --git a/lib/upgrade.txt b/lib/upgrade.txt index c52f4ead290..f1e20c8a723 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -13,6 +13,8 @@ information provided here is intended especially for developers. * New $CFG->localcachedir setting useful for cluster nodes. Admins have to update X-Sendfile aliases if used. * MS SQL Server drivers are now using NVARCHAR(MAX) instead of NTEXT and VARBINARY(MAX) instead of IMAGE, this change should be fully transparent and it should help significantly with add-on compatibility. +* The string manager classes were renamed. Note that they should not be modified or used directly, + always use get_string_manager() to get instance of the string manager. DEPRECATIONS: Various previously deprecated functions have now been altered to throw DEBUG_DEVELOPER debugging notices diff --git a/lib/weblib.php b/lib/weblib.php index 319d83e49c6..853426c3614 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -3452,11 +3452,8 @@ function get_formatted_help_string($identifier, $component, $ajax = false) { global $CFG, $OUTPUT; $sm = get_string_manager(); - if (!$sm->string_exists($identifier, $component) || - !$sm->string_exists($identifier . '_help', $component)) { - // Strings in the on-disk cache may be dirty - try to rebuild it and check again. - $sm->load_component_strings($component, current_language(), true); - } + // Do not rebuild caches here! + // Devs need to learn to purge all caches after any change or disable $CFG->langstringcache. $data = new stdClass();