<commit_before><?php /// $Id$
      /// Search and replace strings throughout all texts in the whole database

require_once('../config.php');

$search  = optional_param('search', '', PARAM_RAW);
$replace = optional_param('replace', '', PARAM_RAW);

require_login();

require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID));

###################################################################
print_header('Search and replace throughout the whole database', 'Replace text within the whole database');


if (!$search or !$replace or !confirm_sesskey()) {   /// Print a form

    print_simple_box_start('center');
    echo '<div align="center">';
    echo '<form action="replace.php">';
    echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'">';
    echo 'Search whole database for: <input type="text" name="search"><br />';
    echo 'Replace with this string: <input type="text" name="replace"><br /></br />';
    echo '<input type="submit" value="Yes, do it now"><br />';
    echo '</form>';
    echo '</div>';
    print_simple_box_end();
    die;
}


if (!$tables = $db->Metatables() ) {    // No tables yet at all.
    error("no tables");
}

print_simple_box_start('center');
foreach ($tables as $table) {
    if (in_array($table, array($CFG->prefix.'config'))) {      // Don't process these
        continue;
    }
    if ($columns = $db->MetaColumns($table, false)) {
        foreach ($columns as $column => $data) {
            if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) {  // Text stuff only
                $db->debug = true;
                execute_sql("UPDATE $table SET $column = REPLACE($column, '$search', '$replace');");
                $db->debug = false;
            }
        }
    }
}
print_simple_box_end();

print_continue('index.php');

?><commit_msg>If the search and replace affected course->modinfo then courses may not work, so clear the cache<commit_after><?php /// $Id$
      /// Search and replace strings throughout all texts in the whole database

require_once('../config.php');

$search  = optional_param('search', '', PARAM_RAW);
$replace = optional_param('replace', '', PARAM_RAW);

require_login();

require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID));

###################################################################
print_header('Search and replace throughout the whole database', 'Replace text within the whole database');


if (!$search or !$replace or !confirm_sesskey()) {   /// Print a form

    print_simple_box_start('center');
    echo '<div align="center">';
    echo '<form action="replace.php">';
    echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey.'">';
    echo 'Search whole database for: <input type="text" name="search"><br />';
    echo 'Replace with this string: <input type="text" name="replace"><br /></br />';
    echo '<input type="submit" value="Yes, do it now"><br />';
    echo '</form>';
    echo '</div>';
    print_simple_box_end();
    die;
}


if (!$tables = $db->Metatables() ) {    // No tables yet at all.
    error("no tables");
}

print_simple_box_start('center');
foreach ($tables as $table) {
    if (in_array($table, array($CFG->prefix.'config'))) {      // Don't process these
        continue;
    }
    if ($columns = $db->MetaColumns($table, false)) {
        foreach ($columns as $column => $data) {
            if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) {  // Text stuff only
                $db->debug = true;
                execute_sql("UPDATE $table SET $column = REPLACE($column, '$search', '$replace');");
                $db->debug = false;
            }
        }
    }
}

/// Clear course cache which might be corrupted now
execute_sql("UPDATE ".$CFG->prefix."course SET modinfo = '' WHERE id <> ".SITEID);

print_simple_box_end();

print_continue('index.php');

?>
{'fraction_non_alphanumeric': 0.2069317023445464, 'fraction_numerical': 0.0, 'mean_word_length': 3.601406799531067, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1600583', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1139, 'n_words': 306}