Skip to content

Commit

Permalink
Exclude tables, cols
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen authored and brendanheywood committed Sep 19, 2024
1 parent 91658d5 commit 921e120
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
39 changes: 34 additions & 5 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

namespace tool_advancedreplace;

use core\check\performance\debugging;
use core\exception\moodle_exception;
use database_column_info;

Expand All @@ -37,9 +36,10 @@ class helper {
*
* @param string $table The table to search.
* @param array $searchingcolumns The columns to search.
* @param string $searchstring The string to search for.
* @return array The columns to search.
*/
public static function get_columns(string $table, array $searchingcolumns = []): array {
public static function get_columns(string $table, array $searchingcolumns = [], string $searchstring = ''): array {
global $DB;
$columns = $DB->get_columns($table);

Expand All @@ -57,6 +57,14 @@ public static function get_columns(string $table, array $searchingcolumns = []):
return [];
}

// Knowns tables to skip.
$skiptables = ['grade_grades_history'];

// Skip tables that are in the skip list.
if (in_array($table, $skiptables)) {
return [];
}

// Only search the specified columns.
foreach ($searchingcolumns as $column) {
if ($column !== self::ALL_COLUMNS) {
Expand All @@ -71,6 +79,28 @@ public static function get_columns(string $table, array $searchingcolumns = []):
return db_should_replace($table, $col->name);
});

// Only search columns that are of type text or char.
$columns = array_filter($columns, function($col) {
return $col->meta_type === 'X' || $col->meta_type === 'C';
});

// Known columns to skip.
$skipcolumns = ['introformat'];

// Skip columns that are in the skip list.
$columns = array_filter($columns, function($col) use ($skipcolumns) {
return !in_array($col->name, $skipcolumns);
});

// Exclude columns that has max length less than the search string.
if (!empty($searchstring)) {
// Strip special characters from the search string.
$searchstring = preg_replace('/[^a-zA-Z0-9]/', '', $searchstring);
$columns = array_filter($columns, function($col) use ($searchstring) {
$col->max_length >= strlen($searchstring);
});
}

return $columns;
}

Expand All @@ -80,7 +110,7 @@ public static function get_columns(string $table, array $searchingcolumns = []):
* @param string $tables A comma separated list of tables and columns to search.
* @return array
*/
public static function build_searching_list(string $tables = ''): array {
public static function build_searching_list(string $tables = '', string $searchstring = ''): array {
global $DB;

// Build a list of tables and columns to search.
Expand Down Expand Up @@ -126,7 +156,7 @@ public static function build_searching_list(string $tables = ''): array {
// Return the list of tables and actual columns to search.
$actualsearchlist = [];
foreach ($searchlist as $table => $columns) {
$actualcolumns = self::get_columns($table, $columns);
$actualcolumns = self::get_columns($table, $columns, $searchstring);
if (!empty($actualcolumns)) {
$actualsearchlist[$table] = $actualcolumns;
}
Expand Down Expand Up @@ -321,5 +351,4 @@ public static function regular_expression_search(string $search, string $table,
}
return $results;
}

}
9 changes: 5 additions & 4 deletions cli/find.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
"Search text throughout the whole database.
Options:
--search=STRING String to search for.
--regex-match=STRING Use regular expression to match the search string.
--output=FILE Output file. If not specified, output to stdout.
--search=STRING Required if --regex-match is not specified. String to search for.
--regex-match=STRING Required if --search is not specified. Use regular expression to match the search string.
--output=FILE Required. .Output file. If not specified, output to stdout.
--tables=tablename:columnname Tables and columns to search. Separate multiple tables/columns with a comma.
If not specified, search all tables and columns.
If specify table only, search all columns in the table.
Expand Down Expand Up @@ -66,6 +66,7 @@
'h' => 'help',
]
);
core_php_time_limit::raise();

if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
Expand Down Expand Up @@ -109,7 +110,7 @@
}

// Perform the search.
$searchlist = helper::build_searching_list($tables);
$searchlist = helper::build_searching_list($tables, );

// Output the result for each table.
foreach ($searchlist as $table => $columns) {
Expand Down

0 comments on commit 921e120

Please sign in to comment.