Skip to content

Commit

Permalink
Update db replace to replace strings in PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
petersistrom committed Dec 10, 2024
1 parent 88f33f9 commit 3d7131c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 52 deletions.
68 changes: 17 additions & 51 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,61 +657,25 @@ public static function find_link_function($table, $column) {
* @param int $id The id of the record to restrict the search.
*/
public static function replace_text_in_a_record(string $table, string $columnname,
string $search, string $replace, int $id) {

$column = self::get_column_info($table, $columnname);
self::replace_all_text($table, $column, $search, $replace, ' AND id = ?', [$id]);
}

/**
* A clone of the core function replace_all_text.
* We have optional id parameter to restrict the search.
*
* @since Moodle 2.6.1
* @param string $table name of the table
* @param database_column_info $column
* @param string $search text to search for
* @param string $replace text to replace with
* @param string $wheresql additional where clause
* @param array $whereparams parameters for the where clause
*/
private static function replace_all_text($table, database_column_info $column, string $search, string $replace,
string $wheresql = '', array $whereparams = []) {
string $search, string $replace, int $id) : bool {
global $DB;

if (!$DB->replace_all_text_supported()) {
throw new moodle_exception(get_string('errorreplacetextnotsupported', 'tool_advancedreplace'));
}
$column = self::get_column_info($table, $columnname);

// Enclose the column name by the proper quotes if it's a reserved word.
$columnname = $DB->get_manager()->generator->getEncQuoted($column->name);

$searchsql = $DB->sql_like($columnname, '?');
$searchparam = '%'.$DB->sql_like_escape($search).'%';

// Additional where clause.
$searchsql .= $wheresql;
$params = array_merge([$search, $replace, $searchparam], $whereparams);

switch ($column->meta_type) {
case 'C':
if (core_text::strlen($search) < core_text::strlen($replace)) {
$colsize = $column->max_length;
$sql = "UPDATE {".$table."}
SET $columnname = " . $DB->sql_substr("REPLACE(" . $columnname . ", ?, ?)", 1, $colsize) . "
WHERE $searchsql";
break;
}
// Otherwise, do not break and use the same query as in the 'X' case.
case 'X':
$sql = "UPDATE {".$table."}
SET $columnname = REPLACE($columnname, ?, ?)
WHERE $searchsql";
break;
default:
throw new moodle_exception(get_string('errorcolumntypenotsupported', 'tool_advancedreplace'));
}
$DB->execute($sql, $params);
$record = $DB->get_record($table, array('id' => $id), $columnname);
$escapedrecord = str_replace(array("\r\n", "\n", "\r"), '', $record->$columnname);
$escapedsearchstring = str_replace(array("\r\n", "\n", "\r"), '', $search);

if ($escapedrecord == $escapedsearchstring) {
return $DB->set_field($table, $columnname, $replace, array('id' => $id));
} else {
mtrace(get_string('errorreplacingstring', 'tool_advancedreplace',
['id' => $id, 'table' => $table, 'column' => $columnname]));
return false;
}
}

/**
Expand Down Expand Up @@ -815,8 +779,10 @@ public static function handle_replace_csv(string $data, string $type = 'db') {
$rowskip++;
} else if ($type == 'db') {
// Replace the string.
self::replace_text_in_a_record($record[$tableindex], $record[$columnindex],
$record[$matchindex], $record[$replaceindex], $record[$idindex]);
if (!self::replace_text_in_a_record($record[$tableindex], $record[$columnindex],
$record[$matchindex], $record[$replaceindex], $record[$idindex])) {
$rowskip++;
}
} else if ($type == 'files') {
$filerecord = [
'contextid' => $record[$contextidindex],
Expand Down
2 changes: 1 addition & 1 deletion lang/en/tool_advancedreplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
$string['errorinvalidparam'] = 'Invalid parameter.';
$string['errormissingfields'] = 'The following fields are missing: {$a}';
$string['errorregexnotsupported'] = 'Regular expression searches are not supported by this database.';
$string['errorreplacetextnotsupported'] = 'Replace all text is not supported by this database.';
$string['errorreplacingstring'] = 'Skipped record -> Table: [{$a->table}] id: [{$a->id}] column: [{$a->column}] Search string does not match database entry.';
$string['errorsearchmethod'] = 'Please choose one of the search methods: plain text or regular expression.';
$string['errorreplacingfile'] = 'Error replacing string: [{$a->replace}] in file: [{$a->filename}]';
$string['errorreplacingfilenotfound'] = 'Error replacing string, file: [{$a->filename}] no found';
Expand Down

0 comments on commit 3d7131c

Please sign in to comment.