MDL-46585 lang: display debugging message when deprecated string is used

This commit is contained in:
Marina Glancy
2014-09-12 10:24:43 +08:00
parent c78cf06d9e
commit 4b867a063a
4 changed files with 136 additions and 1 deletions
+66 -1
View File
@@ -47,6 +47,8 @@ class core_string_manager_standard implements core_string_manager {
protected $translist;
/** @var cache stores list of available translations */
protected $menucache;
/** @var array list of cached deprecated strings */
protected $cacheddeprecated;
/**
* Create new instance of string manager
@@ -206,6 +208,57 @@ class core_string_manager_standard implements core_string_manager {
return $string;
}
/**
* Parses all deprecated.txt in all plugins lang locations and returns the list of deprecated strings.
*
* Static variable is used for caching, this function is only called in dev environment.
*
* @return array of deprecated strings in the same format they appear in deprecated.txt files: "identifier,component"
* where component is a normalised component (i.e. "core_moodle", "mod_assign", etc.)
*/
protected function load_deprecated_strings() {
global $CFG;
if ($this->cacheddeprecated !== null) {
return $this->cacheddeprecated;
}
$this->cacheddeprecated = array();
$content = '';
$filename = $CFG->dirroot . '/lang/en/deprecated.txt';
if (file_exists($filename)) {
$content .= file_get_contents($filename);
}
foreach (core_component::get_plugin_types() as $plugintype => $plugintypedir) {
foreach (core_component::get_plugin_list($plugintype) as $pluginname => $plugindir) {
$filename = $plugindir.'/lang/en/deprecated.txt';
if (file_exists($filename)) {
$content .= "\n". file_get_contents($filename);
}
}
}
$strings = preg_split('/\s*\n\s*/', $content, -1, PREG_SPLIT_NO_EMPTY);
$this->cacheddeprecated = array_flip($strings);
return $this->cacheddeprecated;
}
/**
* Has string been deprecated?
*
* Usually checked only inside get_string() to display debug warnings.
*
* @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 deprecated
*/
public function string_deprecated($identifier, $component) {
$deprecated = $this->load_deprecated_strings();
list($plugintype, $pluginname) = core_component::normalize_component($component);
return isset($deprecated[$identifier . ',' . $plugintype . '_' . $pluginname]);
}
/**
* Does the string actually exist?
*
@@ -234,6 +287,8 @@ class core_string_manager_standard implements core_string_manager {
* @return string The String !
*/
public function get_string($identifier, $component = '', $a = null, $lang = null) {
global $CFG;
$this->countgetstring++;
// 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.
@@ -279,7 +334,7 @@ class core_string_manager_standard implements core_string_manager {
// 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.
if (debugging('', DEBUG_DEVELOPER)) {
if ($CFG->debugdeveloper) {
list($plugintype, $pluginname) = core_component::normalize_component($component);
if ($plugintype === 'core') {
$file = "lang/en/{$component}.php";
@@ -324,6 +379,16 @@ class core_string_manager_standard implements core_string_manager {
}
}
if ($CFG->debugdeveloper) {
// Display a debugging message if sting exists but was deprecated.
if ($this->string_deprecated($identifier, $component)) {
list($plugintype, $pluginname) = core_component::normalize_component($component);
debugging("String [{$identifier},{$plugintype}_{$pluginname}] is deprecated. ".
'Either you should no longer be using that string, or the string has been incorrectly deprecated, in which case you should report this as a bug. '.
'Please refer to https://docs.moodle.org/dev/String_deprecation', DEBUG_DEVELOPER);
}
}
return $string;
}