MDL-78010 mod_label: improve upgrade performance

This commit is contained in:
Ferran Recio 2023-05-24 10:30:46 +02:00
parent 03b605f2eb
commit 5f30a47660

View File

@ -71,26 +71,43 @@ function xmldb_label_upgrade($oldversion) {
if ($oldversion < 2022112801) {
$prevlang = force_current_language($CFG->lang);
$labels = $DB->get_recordset('label');
foreach ($labels as $label) {
// Make sure that all labels have now the same name according to the new convention.
// Note this is the same (and duplicated) code as in get_label_name as we cannot call any API function
// during upgrade.
$name = html_to_text(format_string($label->intro, true));
$name = preg_replace('/@@PLUGINFILE@@\/[[:^space:]]+/i', '', $name);
// Remove double space and also nbsp; characters.
$name = preg_replace('/\s+/u', ' ', $name);
$name = trim($name);
if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) {
$name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH) . "...";
$select = $DB->sql_like('name', ':tofind');
$params = ['tofind' => '%@@PLUGINFILE@@%'];
$total = $DB->count_records_select('label', $select, $params);
if ($total > 0) {
$labels = $DB->get_recordset_select('label', $select, $params, 'id, name, intro');
// Show a progress bar.
$pbar = new progress_bar('upgrademodlabelpluginfile', 500, true);
$current = 0;
$defaultname = get_string('modulename', 'label');
foreach ($labels as $label) {
$originalname = $label->name;
// Make sure that all labels have now the same name according to the new convention.
// Note this is the same (and duplicated) code as in get_label_name as we cannot call any API function
// during upgrade.
$name = html_to_text(format_string($label->intro, true));
$name = preg_replace('/@@PLUGINFILE@@\/[[:^space:]]+/i', '', $name);
// Remove double space and also nbsp; characters.
$name = preg_replace('/\s+/u', ' ', $name);
$name = trim($name);
if (core_text::strlen($name) > LABEL_MAX_NAME_LENGTH) {
$name = core_text::substr($name, 0, LABEL_MAX_NAME_LENGTH) . "...";
}
if (empty($name)) {
$name = $defaultname;
}
$label->name = $name;
if ($originalname !== $name) {
$DB->update_record('label', $label);
}
$current++;
$pbar->update($current, $total, "Upgrading label activity names - $current/$total.");
}
if (empty($name)) {
$name = get_string('modulename', 'label');
}
$label->name = $name;
$DB->update_record('label', $label);
$labels->close();
}
$labels->close();
force_current_language($prevlang);
upgrade_mod_savepoint(true, 2022112801, 'label');
}