From 9f9193f623a34f3cb0da89e07259be9129d76cc7 Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Tue, 13 Apr 2010 20:14:31 +0000 Subject: [PATCH] MDL-22061 new string_exists() method - this can be used in some corner cases --- lib/moodlelib.php | 55 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 105f1a65363..91a47087853 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -5701,6 +5701,20 @@ interface string_manager { */ public function get_string($identifier, $component = '', $a = 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 list of country names in the current language * @return array two-letter country code => translated name. @@ -5803,7 +5817,6 @@ class core_string_manager implements string_manager { $string = array(); // first load english pack if (!file_exists("$CFG->dirroot/lang/en/$file.php")) { - debugging("Invalid component parameter in get_string() call: $component", DEBUG_DEVELOPER); return array(); } include("$CFG->dirroot/lang/en/$file.php"); @@ -5828,7 +5841,6 @@ class core_string_manager implements string_manager { } else { if (!$location = get_plugin_directory($plugintype, $pluginname) or !is_dir($location)) { - debugging("Invalid component parameter in get_string() call, plugin files are missing: $component", DEBUG_DEVELOPER); return array(); } if ($plugintype === 'mod') { @@ -5840,7 +5852,7 @@ class core_string_manager implements string_manager { $string = array(); // first load english pack if (!file_exists("$location/lang/en/$file.php")) { - //english pack does not exist, so do not try to laod anything else + //english pack does not exist, so do not try to load anything else return array(); } include("$location/lang/en/$file.php"); @@ -5873,6 +5885,24 @@ class core_string_manager implements string_manager { 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. + * + * 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 * @@ -6054,9 +6084,28 @@ class install_string_manager implements string_manager { * @return array of all string for given component and lang */ public function load_component_strings($component, $lang) { + // 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 *