Skip to content

Commit

Permalink
MDL-69746 tool_replace: additional skip tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Oct 2, 2020
1 parent e049d30 commit 6ced761
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
7 changes: 7 additions & 0 deletions admin/tool/replace/classes/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ function definition() {
$mform->addElement('text', 'replace', get_string('replacewith', 'tool_replace'), 'size="50"', PARAM_RAW);
$mform->addElement('static', 'replacest', '', get_string('replacewithhelp', 'tool_replace'));
$mform->setType('replace', PARAM_RAW);

$mform->addElement('textarea', 'additionalskiptables', get_string("additionalskiptables", "tool_replace"),
array('rows' => 5, 'cols' => 50));
$mform->addElement('static', 'additionalskiptables_desc', '', get_string('additionalskiptables_desc', 'tool_replace'));
$mform->setType('additionalskiptables', PARAM_RAW);
$mform->setDefault('additionalskiptables', '');

$mform->addElement('checkbox', 'shorten', get_string('shortenoversized', 'tool_replace'));
$mform->addRule('replace', get_string('required'), 'required', null, 'client');

Expand Down
5 changes: 4 additions & 1 deletion admin/tool/replace/cli/replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
Options:
--search=STRING String to search for.
--replace=STRING String to replace with.
--skiptables=STRING Skip these tables (comma separated list of tables).
--shorten Shorten result if necessary.
--non-interactive Perform the replacement without confirming.
-h, --help Print out this help.
Expand All @@ -46,6 +47,7 @@
array(
'search' => null,
'replace' => null,
'skiptables' => '',
'shorten' => false,
'non-interactive' => false,
'help' => false,
Expand All @@ -71,6 +73,7 @@
try {
$search = validate_param($options['search'], PARAM_RAW);
$replace = validate_param($options['replace'], PARAM_RAW);
$skiptables = validate_param($options['skiptables'], PARAM_RAW);
} catch (invalid_parameter_exception $e) {
cli_error(get_string('invalidcharacter', 'tool_replace'));
}
Expand All @@ -85,7 +88,7 @@
}
}

if (!db_replace($search, $replace)) {
if (!db_replace($search, $replace, $skiptables)) {
cli_heading(get_string('error'));
exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/replace/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
$PAGE->requires->js_init_code("window.scrollTo(0, 5000000);");

echo $OUTPUT->box_start();
db_replace($data->search, $data->replace);
db_replace($data->search, $data->replace, $data->additionalskiptables);
echo $OUTPUT->box_end();

// Course caches are now rebuilt on the fly.
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/replace/lang/en/tool_replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['additionalskiptables'] = 'Additional skip tables';
$string['additionalskiptables_desc'] = 'Please specify the additional tables (comma separated list) you want to skip while running DB search and replace.';
$string['cannotfit'] = 'The replacement is longer than the original and shortening is not allowed; cannot continue.';
$string['disclaimer'] = 'I understand the risks of this operation';
$string['doit'] = 'Yes, do it!';
Expand Down
11 changes: 8 additions & 3 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -9069,12 +9069,17 @@ function any_new_admin_settings($node) {
* @param string $column name
* @return bool success or fail
*/
function db_should_replace($table, $column = ''): bool {
function db_should_replace($table, $column = '', $additionalskiptables = ''): bool {

// TODO: this is horrible hack, we should do whitelisting and each plugin should be responsible for proper replacing...
$skiptables = ['config', 'config_plugins', 'filter_config', 'sessions',
'events_queue', 'repository_instance_config', 'block_instances', 'files'];

// Additional skip tables.
if (!empty($additionalskiptables)) {
$skiptables = array_merge($skiptables, explode(',', str_replace(' ', '', $additionalskiptables)));
}

// Don't process these.
if (in_array($table, $skiptables)) {
return false;
Expand Down Expand Up @@ -9103,7 +9108,7 @@ function db_should_replace($table, $column = ''): bool {
* @param string $replace string to replace
* @return bool success or fail
*/
function db_replace($search, $replace) {
function db_replace($search, $replace, $additionalskiptables = '') {
global $DB, $CFG, $OUTPUT;

// Turn off time limits, sometimes upgrades can be slow.
Expand All @@ -9114,7 +9119,7 @@ function db_replace($search, $replace) {
}
foreach ($tables as $table) {

if (!db_should_replace($table)) {
if (!db_should_replace($table, '', $additionalskiptables)) {
continue;
}

Expand Down
45 changes: 44 additions & 1 deletion lib/tests/adminlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,48 @@ public function test_db_should_replace(string $table, string $column, bool $expe
$this->assertSame($actual, $expected);
}

}
/**
* Data provider for additional skip tables.
*
* @return array
*/
public function db_should_replace_additional_skip_tables_dataprovider() {
return [
// Skipped tables.
['block_instances', '', false],
['config', '', false],
['config_plugins', '', false],
['config_log', '', false],
['events_queue', '', false],
['filter_config', '', false],
['log', '', false],
['repository_instance_config', '', false],
['sessions', '', false],
['upgrade_log', '', false],

// Additional skipped tables.
['context', '', false],
['quiz_attempts', '', false],
['role_assignments', '', false],

// Normal tables.
['assign', '', true],
['book', '', true],
];
}

/**
* Test additional skip tables.
*
* @dataProvider db_should_replace_additional_skip_tables_dataprovider
* @param string $table name
* @param string $column name
* @param bool $expected whether it should be replaced
*/
public function test_db_should_replace_additional_skip_tables(string $table, string $column, bool $expected) {
$this->resetAfterTest();
$additionalskiptables = 'context, quiz_attempts, role_assignments ';
$actual = db_should_replace($table, $column, $additionalskiptables);
$this->assertSame($actual, $expected);
}
}

0 comments on commit 6ced761

Please sign in to comment.