Skip to content

Commit

Permalink
MDL-67515 tool_customlang: Ignore invalid component strings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vmdef committed Mar 2, 2021
1 parent 4e398ff commit bb4da69
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions admin/tool/customlang/locallib.php
Expand Up @@ -39,7 +39,7 @@ class tool_customlang_utils {
const ROUGH_NUMBER_OF_STRINGS = 16500;

/** @var array cache of {@link self::list_components()} results */
protected static $components = null;
private static $components = null;

/**
* This class can not be instantiated
Expand All @@ -54,28 +54,30 @@ private function __construct() {
*/
public static function list_components() {

$list['moodle'] = 'core';
if (self::$components === null) {
$list['moodle'] = 'core';

$coresubsystems = core_component::get_core_subsystems();
ksort($coresubsystems); // should be but just in case
foreach ($coresubsystems as $name => $location) {
$list[$name] = 'core_'.$name;
}
$coresubsystems = core_component::get_core_subsystems();
ksort($coresubsystems); // Should be but just in case.
foreach ($coresubsystems as $name => $location) {
$list[$name] = 'core_' . $name;
}

$plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $location) {
$pluginlist = core_component::get_plugin_list($type);
foreach ($pluginlist as $name => $ununsed) {
if ($type == 'mod') {
// Plugin names are now automatically validated.
$list[$name] = $type.'_'.$name;
} else {
$list[$type.'_'.$name] = $type.'_'.$name;
$plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $location) {
$pluginlist = core_component::get_plugin_list($type);
foreach ($pluginlist as $name => $ununsed) {
if ($type == 'mod') {
// Plugin names are now automatically validated.
$list[$name] = $type . '_' . $name;
} else {
$list[$type . '_' . $name] = $type . '_' . $name;
}
}
}
self::$components = $list;
}

return $list;
return self::$components;
}

/**
Expand Down Expand Up @@ -211,14 +213,18 @@ public static function checkin($lang) {
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
FROM {tool_customlang} s
JOIN {tool_customlang_components} c ON s.componentid = c.id
WHERE s.lang = ?
AND (s.local IS NOT NULL OR s.modified = 1)
AND c.name $insql
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();
foreach ($strings as $string) {
Expand Down Expand Up @@ -336,11 +342,9 @@ protected static function dump_strings($lang, $component, $strings) {
* @return string|boolean filename eg 'moodle.php' or 'workshop.php', false if not found
*/
protected static function get_component_filename($component) {
if (is_null(self::$components)) {
self::$components = self::list_components();
}

$return = false;
foreach (self::$components as $legacy => $normalized) {
foreach (self::list_components() as $legacy => $normalized) {
if ($component === $normalized) {
$return = $legacy.'.php';
break;
Expand Down

0 comments on commit bb4da69

Please sign in to comment.