diff --git a/classes/helper.php b/classes/helper.php index 3758c5d..02d46a7 100644 --- a/classes/helper.php +++ b/classes/helper.php @@ -21,6 +21,7 @@ require_once($CFG->libdir . '/adminlib.php'); use core\exception\moodle_exception; +use core_text; use database_column_info; /** @@ -272,6 +273,7 @@ public static function plain_text_search(string $search, string $table, $record->courseshortname ?? '', $record->id, $record->$columnname, + '', ]); } } else { @@ -360,6 +362,7 @@ public static function regular_expression_search(string $search, string $table, $record->courseshortname ?? '', $record->id, $match, + '', ]); } } @@ -372,8 +375,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 @@ -397,7 +426,9 @@ 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."} @@ -405,7 +436,7 @@ public static function replace_all_text($table, database_column_info $column, $s 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)) { @@ -414,7 +445,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); } } } diff --git a/cli/find.php b/cli/find.php index 5c64df9..892e48b 100644 --- a/cli/find.php +++ b/cli/find.php @@ -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']); } diff --git a/cli/replace.php b/cli/replace.php index 81a52b9..0de4841 100644 --- a/cli/replace.php +++ b/cli/replace.php @@ -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 'replace' field is not empty, in case user miss to fill them. + // TOOO: There may be cases whe we want to replace with empty string, probably we should add a flag for that. + if (empty($replace)) { + debugging("Skipping record with empty replace value: $table, $columnname, $id, $match", DEBUG_DEVELOPER); + continue; + } + // Replace the string. + $column = helper::get_column_info($table, $columnname); helper::replace_all_text($table, $column, $match, $replace, $id); } diff --git a/lang/en/tool_advancedreplace.php b/lang/en/tool_advancedreplace.php index 0f37377..e4adf85 100644 --- a/lang/en/tool_advancedreplace.php +++ b/lang/en/tool_advancedreplace.php @@ -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.';