MDL-67515 tool_customlang: Ignore invalid component strings

After uninstalling a plugin, the translated strings remain in
tool_customlang table, throwing an exception when trying to localise
any strings. Currently there is no mechanism to
clean up customlang tables and files during the uninstall process,
so with this patch the invalid components will be ignored.
This commit is contained in:
Víctor Déniz 2021-02-10 13:55:16 +00:00
parent 4e398ff3f7
commit bb4da690cf

View File

@ -39,7 +39,7 @@ class tool_customlang_utils {
const ROUGH_NUMBER_OF_STRINGS = 16500; const ROUGH_NUMBER_OF_STRINGS = 16500;
/** @var array cache of {@link self::list_components()} results */ /** @var array cache of {@link self::list_components()} results */
protected static $components = null; private static $components = null;
/** /**
* This class can not be instantiated * This class can not be instantiated
@ -54,28 +54,30 @@ class tool_customlang_utils {
*/ */
public static function list_components() { public static function list_components() {
$list['moodle'] = 'core'; if (self::$components === null) {
$list['moodle'] = 'core';
$coresubsystems = core_component::get_core_subsystems(); $coresubsystems = core_component::get_core_subsystems();
ksort($coresubsystems); // should be but just in case ksort($coresubsystems); // Should be but just in case.
foreach ($coresubsystems as $name => $location) { foreach ($coresubsystems as $name => $location) {
$list[$name] = 'core_'.$name; $list[$name] = 'core_' . $name;
} }
$plugintypes = core_component::get_plugin_types(); $plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $location) { foreach ($plugintypes as $type => $location) {
$pluginlist = core_component::get_plugin_list($type); $pluginlist = core_component::get_plugin_list($type);
foreach ($pluginlist as $name => $ununsed) { foreach ($pluginlist as $name => $ununsed) {
if ($type == 'mod') { if ($type == 'mod') {
// Plugin names are now automatically validated. // Plugin names are now automatically validated.
$list[$name] = $type.'_'.$name; $list[$name] = $type . '_' . $name;
} else { } else {
$list[$type.'_'.$name] = $type.'_'.$name; $list[$type . '_' . $name] = $type . '_' . $name;
}
} }
} }
self::$components = $list;
} }
return self::$components;
return $list;
} }
/** /**
@ -211,14 +213,18 @@ class tool_customlang_utils {
return false; return false;
} }
// get all customized strings from updated components list($insql, $inparams) = $DB->get_in_or_equal(self::list_components());
// Get all customized strings from updated valid components.
$sql = "SELECT s.*, c.name AS component $sql = "SELECT s.*, c.name AS component
FROM {tool_customlang} s FROM {tool_customlang} s
JOIN {tool_customlang_components} c ON s.componentid = c.id JOIN {tool_customlang_components} c ON s.componentid = c.id
WHERE s.lang = ? WHERE s.lang = ?
AND (s.local IS NOT NULL OR s.modified = 1) AND (s.local IS NOT NULL OR s.modified = 1)
AND c.name $insql
ORDER BY componentid, stringid"; ORDER BY componentid, stringid";
$strings = $DB->get_records_sql($sql, array($lang)); array_unshift($inparams, $lang);
$strings = $DB->get_records_sql($sql, $inparams);
$files = array(); $files = array();
foreach ($strings as $string) { foreach ($strings as $string) {
@ -336,11 +342,9 @@ EOF
* @return string|boolean filename eg 'moodle.php' or 'workshop.php', false if not found * @return string|boolean filename eg 'moodle.php' or 'workshop.php', false if not found
*/ */
protected static function get_component_filename($component) { protected static function get_component_filename($component) {
if (is_null(self::$components)) {
self::$components = self::list_components();
}
$return = false; $return = false;
foreach (self::$components as $legacy => $normalized) { foreach (self::list_components() as $legacy => $normalized) {
if ($component === $normalized) { if ($component === $normalized) {
$return = $legacy.'.php'; $return = $legacy.'.php';
break; break;