Skip to content

Commit

Permalink
Add replace field
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Sep 25, 2024
1 parent 5ca4705 commit 42d06ba
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
36 changes: 33 additions & 3 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public static function plain_text_search(string $search, string $table,
$record->courseshortname ?? '',
$record->id,
$record->$columnname,
'',
]);
}
} else {
Expand Down Expand Up @@ -360,6 +361,7 @@ public static function regular_expression_search(string $search, string $table,
$record->courseshortname ?? '',
$record->id,
$match,
'',
]);
}
}
Expand All @@ -372,8 +374,34 @@ public static function regular_expression_search(string $search, string $table,
return $results;
}

/**
* Get column info from column name.
*
* @param string $table The table name.
* @param string $columnname The column name.
*/
public static function get_column_info(string $table, string $columnname): database_column_info {
global $DB;

$columns = $DB->get_columns($table);
$column = null;
foreach ($columns as $col) {
if ($col->name == $columnname) {
$column = $col;
break;
}
}

if (is_null($column)) {
throw new moodle_exception(get_string('errorcolumnnotfound', 'tool_advancedreplace', $columnname));
}

return $column;
}

/**
* 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
Expand All @@ -397,15 +425,17 @@ public static function replace_all_text($table, database_column_info $column, $s
// Add the id to the search.
if (!is_null($id)) {
$searchsql .= ' AND id = ?';
$searchparam = [$searchparam, $id];
$params = [$search, $replace, $searchparam, $id];
} else {
$params = [$search, $replace, $searchparam];
}

$sql = "UPDATE {".$table."}
SET $columnname = REPLACE($columnname, ?, ?)
WHERE $searchsql";

if ($column->meta_type === 'X') {
$DB->execute($sql, [$search, $replace, $searchparam]);
$DB->execute($sql, $params);

} else if ($column->meta_type === 'C') {
if (core_text::strlen($search) < core_text::strlen($replace)) {
Expand All @@ -414,7 +444,7 @@ public static function replace_all_text($table, database_column_info $column, $s
SET $columnname = " . $DB->sql_substr("REPLACE(" . $columnname . ", ?, ?)", 1, $colsize) . "
WHERE $searchsql";
}
$DB->execute($sql, [$search, $replace, $searchparam]);
$DB->execute($sql, $params);
}
}
}
2 changes: 1 addition & 1 deletion cli/find.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

// Show header.
if (!$options['summary']) {
fputcsv($fp, ['table', 'column', 'courseid', 'shortname', 'id', 'match']);
fputcsv($fp, ['table', 'column', 'courseid', 'shortname', 'id', 'match', 'replace']);
} else {
fputcsv($fp, ['table', 'column']);
}
Expand Down
10 changes: 9 additions & 1 deletion cli/replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,20 @@
while ($record = $csvimport->next()) {
$dataset[] = $record;
$table = $record[$tableindex];
$column = $record[$columnindex];
$columnname = $record[$columnindex];
$id = $record[$idindex];
$match = $record[$matchindex];
$replace = $record[$replaceindex];

// Making sure match and replace not empty, in cases user missed to fill them.
// TDOO: There may be cases whe we want to replace with empty string, probably we should add a flag for that.
if (empty($match) || empty($replace)) {
debugging("Skipping record with empty match or replace value.", DEBUG_DEVELOPER);
continue;
}

// Replace the string.
$column = helper::get_column_info($table, $columnname);
helper::replace_all_text($table, $column, $match, $replace, $id);
}

Expand Down
1 change: 1 addition & 0 deletions lang/en/tool_advancedreplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['errorcolumnnotfound'] = 'Column not found.';
$string['errorinvalidparam'] = 'Invalid parameter.';
$string['errorregexnotsupported'] = 'Regular expression searches are not supported by this database.';
$string['errorsearchmethod'] = 'Please choose one of the search methods: plain text or regular expression.';
Expand Down

0 comments on commit 42d06ba

Please sign in to comment.