Skip to content

Commit

Permalink
Split output
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Sep 19, 2024
1 parent f61587c commit 74417f7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 60 deletions.
46 changes: 28 additions & 18 deletions classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,35 @@ private static function regular_expression_search(string $search, string $table,
* Perform a search on a table and column.
*
* @param string $search The text to search for.
* @param string $table The table to search.
* @param array $columns The columns to search.
* @param bool $regex Whether to use regular expression search.
* @param string $tables A comma separated list of tables and columns to search.
* @param int $limit The maximum number of results to return.
* @return array
* @throws moodle_exception
*/
public static function search(string $search, bool $regex = false, string $tables = '', int $limit = 0): array {
public static function search(string $search, string $table, array $columns, bool $regex = false, int $limit = 0): array {
// Perform the search for each table and column.
$results = [];
foreach ($columns as $column) {
// Perform the search on this column.
if ($regex) {
$results = array_merge($results, self::regular_expression_search($search, $table, $column, $limit));
} else {
$results = array_merge($results, self::plain_text_search($search, $table, $column, $limit));
}
}

return $results;
}

/**
* Build searching list
*
* @param string $tables A comma separated list of tables and columns to search.
* @return array
*/
public static function build_searching_list(string $tables = ''): array {
global $DB;

// Build a list of tables and columns to search.
Expand All @@ -231,7 +254,7 @@ public static function search(string $search, bool $regex = false, string $table
}

// Skip if the column already exists in the list for that table.
if (!in_array($columnname, $searchlist[$tablename])) {
if (in_array($columnname, $searchlist[$tablename])) {
continue;
}
}
Expand All @@ -254,20 +277,7 @@ public static function search(string $search, bool $regex = false, string $table
$searchlist[$table] = [self::ALL_COLUMNS];
}
}

// Perform the search for each table and column.
$results = [];
foreach ($searchlist as $table => $columns) {
foreach ($columns as $column) {
// Perform the search on this column.
if ($regex) {
$results = array_merge($results, self::regular_expression_search($search, $table, $column, $limit));
} else {
$results = array_merge($results, self::plain_text_search($search, $table, $column, $limit));
}
}
}

return $results;
return $searchlist;
}

}
97 changes: 55 additions & 42 deletions cli/find.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
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.
--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 All @@ -56,6 +57,7 @@
[
'search' => null,
'regex-match' => null,
'output' => null,
'tables' => '',
'summary' => false,
'help' => false,
Expand Down Expand Up @@ -92,17 +94,12 @@
cli_error(get_string('invalidcharacter', 'tool_advancedreplace'));
}

// Perform the search.
$result = helper::search($search, !empty($options['regex-match']), $tables, $options['summary'] ? 1 : 0);

// Notifying the user if no results were found.
if (empty($result)) {
echo "No results found.\n";
exit(0);
}

// Start output.
$fp = fopen('php://stdout', 'w');
if (!empty($options['output'])) {
$fp = fopen($options['output'], 'w');
} else {
$fp = fopen('php://stdout', 'w');
}

// Show header.
if (!$options['summary']) {
Expand All @@ -111,40 +108,56 @@
fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber']);
}

// Output the result.
foreach ($result as $table => $columns) {
foreach ($columns as $column => $rows) {
if ($options['summary']) {
$courseid = reset($rows)->courseid ?? '';
$courseidnumber = reset($rows)->courseidnumber ?? '';
fputcsv($fp, [$table, $column, $courseid, $courseidnumber]);
} else {
foreach ($rows as $row) {
// Fields to show.
$courseid = $row->courseid ?? '';
$courseidnumber = $row->courseidnumber ?? '';
$fields = [$table, $column, $courseid, $courseidnumber, $row->id];
// Matched data.
$data = $row->$column;

if (!empty($options['regex-match'])) {
// If the search string is a regular expression, show each matching instance.

// Replace "/" with "\/", as it is used as delimiters.
$search = str_replace('/', '\\/', $options['regex-match']);

// Perform the regular expression search.
preg_match_all( "/" . $search . "/", $data, $matches);

if (!empty($matches[0])) {
// Show the result foreach match.
foreach ($matches[0] as $match) {
fputcsv($fp, array_merge($fields, [$match]));
// Perform the search.
$searchlist = helper::build_searching_list($tables);

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

// Show progress.
echo "Searching in table $table: \n";
$result = helper::search($search, $table, $columns, !empty($options['regex-match']), $options['summary'] ? 1 : 0);

// Notifying the user if no results were found.
if (empty($result)) {
echo "No results found.\n";
}

// Output the result.
foreach ($result as $table => $columns) {
foreach ($columns as $column => $rows) {
if ($options['summary']) {
$courseid = reset($rows)->courseid ?? '';
$courseidnumber = reset($rows)->courseidnumber ?? '';
fputcsv($fp, [$table, $column, $courseid, $courseidnumber]);
} else {
foreach ($rows as $row) {
// Fields to show.
$courseid = $row->courseid ?? '';
$courseidnumber = $row->courseidnumber ?? '';
$fields = [$table, $column, $courseid, $courseidnumber, $row->id];
// Matched data.
$data = $row->$column;

if (!empty($options['regex-match'])) {
// If the search string is a regular expression, show each matching instance.

// Replace "/" with "\/", as it is used as delimiters.
$search = str_replace('/', '\\/', $options['regex-match']);

// Perform the regular expression search.
preg_match_all( "/" . $search . "/", $data, $matches);

if (!empty($matches[0])) {
// Show the result foreach match.
foreach ($matches[0] as $match) {
fputcsv($fp, array_merge($fields, [$match]));
}
}
} else {
// Show the result for simple plain text search.
fputcsv($fp, array_merge($fields, [$data]));
}
} else {
// Show the result for simple plain text search.
fputcsv($fp, array_merge($fields, [$data]));
}
}
}
Expand Down

0 comments on commit 74417f7

Please sign in to comment.