Skip to content

Commit 74417f7

Browse files
author
Nathan Nguyen
committed
Split output
1 parent f61587c commit 74417f7

File tree

2 files changed

+83
-60
lines changed

2 files changed

+83
-60
lines changed

classes/helper.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,35 @@ private static function regular_expression_search(string $search, string $table,
207207
* Perform a search on a table and column.
208208
*
209209
* @param string $search The text to search for.
210+
* @param string $table The table to search.
211+
* @param array $columns The columns to search.
210212
* @param bool $regex Whether to use regular expression search.
211-
* @param string $tables A comma separated list of tables and columns to search.
212213
* @param int $limit The maximum number of results to return.
213214
* @return array
215+
* @throws moodle_exception
214216
*/
215-
public static function search(string $search, bool $regex = false, string $tables = '', int $limit = 0): array {
217+
public static function search(string $search, string $table, array $columns, bool $regex = false, int $limit = 0): array {
218+
// Perform the search for each table and column.
219+
$results = [];
220+
foreach ($columns as $column) {
221+
// Perform the search on this column.
222+
if ($regex) {
223+
$results = array_merge($results, self::regular_expression_search($search, $table, $column, $limit));
224+
} else {
225+
$results = array_merge($results, self::plain_text_search($search, $table, $column, $limit));
226+
}
227+
}
228+
229+
return $results;
230+
}
231+
232+
/**
233+
* Build searching list
234+
*
235+
* @param string $tables A comma separated list of tables and columns to search.
236+
* @return array
237+
*/
238+
public static function build_searching_list(string $tables = ''): array {
216239
global $DB;
217240

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

233256
// Skip if the column already exists in the list for that table.
234-
if (!in_array($columnname, $searchlist[$tablename])) {
257+
if (in_array($columnname, $searchlist[$tablename])) {
235258
continue;
236259
}
237260
}
@@ -254,20 +277,7 @@ public static function search(string $search, bool $regex = false, string $table
254277
$searchlist[$table] = [self::ALL_COLUMNS];
255278
}
256279
}
257-
258-
// Perform the search for each table and column.
259-
$results = [];
260-
foreach ($searchlist as $table => $columns) {
261-
foreach ($columns as $column) {
262-
// Perform the search on this column.
263-
if ($regex) {
264-
$results = array_merge($results, self::regular_expression_search($search, $table, $column, $limit));
265-
} else {
266-
$results = array_merge($results, self::plain_text_search($search, $table, $column, $limit));
267-
}
268-
}
269-
}
270-
271-
return $results;
280+
return $searchlist;
272281
}
282+
273283
}

cli/find.php

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
Options:
3737
--search=STRING String to search for.
3838
--regex-match=STRING Use regular expression to match the search string.
39+
--output=FILE Output file. If not specified, output to stdout.
3940
--tables=tablename:columnname Tables and columns to search. Separate multiple tables/columns with a comma.
4041
If not specified, search all tables and columns.
4142
If specify table only, search all columns in the table.
@@ -56,6 +57,7 @@
5657
[
5758
'search' => null,
5859
'regex-match' => null,
60+
'output' => null,
5961
'tables' => '',
6062
'summary' => false,
6163
'help' => false,
@@ -92,17 +94,12 @@
9294
cli_error(get_string('invalidcharacter', 'tool_advancedreplace'));
9395
}
9496

95-
// Perform the search.
96-
$result = helper::search($search, !empty($options['regex-match']), $tables, $options['summary'] ? 1 : 0);
97-
98-
// Notifying the user if no results were found.
99-
if (empty($result)) {
100-
echo "No results found.\n";
101-
exit(0);
102-
}
103-
10497
// Start output.
105-
$fp = fopen('php://stdout', 'w');
98+
if (!empty($options['output'])) {
99+
$fp = fopen($options['output'], 'w');
100+
} else {
101+
$fp = fopen('php://stdout', 'w');
102+
}
106103

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

114-
// Output the result.
115-
foreach ($result as $table => $columns) {
116-
foreach ($columns as $column => $rows) {
117-
if ($options['summary']) {
118-
$courseid = reset($rows)->courseid ?? '';
119-
$courseidnumber = reset($rows)->courseidnumber ?? '';
120-
fputcsv($fp, [$table, $column, $courseid, $courseidnumber]);
121-
} else {
122-
foreach ($rows as $row) {
123-
// Fields to show.
124-
$courseid = $row->courseid ?? '';
125-
$courseidnumber = $row->courseidnumber ?? '';
126-
$fields = [$table, $column, $courseid, $courseidnumber, $row->id];
127-
// Matched data.
128-
$data = $row->$column;
129-
130-
if (!empty($options['regex-match'])) {
131-
// If the search string is a regular expression, show each matching instance.
132-
133-
// Replace "/" with "\/", as it is used as delimiters.
134-
$search = str_replace('/', '\\/', $options['regex-match']);
135-
136-
// Perform the regular expression search.
137-
preg_match_all( "/" . $search . "/", $data, $matches);
138-
139-
if (!empty($matches[0])) {
140-
// Show the result foreach match.
141-
foreach ($matches[0] as $match) {
142-
fputcsv($fp, array_merge($fields, [$match]));
111+
// Perform the search.
112+
$searchlist = helper::build_searching_list($tables);
113+
114+
// Output the result for each table.
115+
foreach ($searchlist as $table => $columns) {
116+
117+
// Show progress.
118+
echo "Searching in table $table: \n";
119+
$result = helper::search($search, $table, $columns, !empty($options['regex-match']), $options['summary'] ? 1 : 0);
120+
121+
// Notifying the user if no results were found.
122+
if (empty($result)) {
123+
echo "No results found.\n";
124+
}
125+
126+
// Output the result.
127+
foreach ($result as $table => $columns) {
128+
foreach ($columns as $column => $rows) {
129+
if ($options['summary']) {
130+
$courseid = reset($rows)->courseid ?? '';
131+
$courseidnumber = reset($rows)->courseidnumber ?? '';
132+
fputcsv($fp, [$table, $column, $courseid, $courseidnumber]);
133+
} else {
134+
foreach ($rows as $row) {
135+
// Fields to show.
136+
$courseid = $row->courseid ?? '';
137+
$courseidnumber = $row->courseidnumber ?? '';
138+
$fields = [$table, $column, $courseid, $courseidnumber, $row->id];
139+
// Matched data.
140+
$data = $row->$column;
141+
142+
if (!empty($options['regex-match'])) {
143+
// If the search string is a regular expression, show each matching instance.
144+
145+
// Replace "/" with "\/", as it is used as delimiters.
146+
$search = str_replace('/', '\\/', $options['regex-match']);
147+
148+
// Perform the regular expression search.
149+
preg_match_all( "/" . $search . "/", $data, $matches);
150+
151+
if (!empty($matches[0])) {
152+
// Show the result foreach match.
153+
foreach ($matches[0] as $match) {
154+
fputcsv($fp, array_merge($fields, [$match]));
155+
}
143156
}
157+
} else {
158+
// Show the result for simple plain text search.
159+
fputcsv($fp, array_merge($fields, [$data]));
144160
}
145-
} else {
146-
// Show the result for simple plain text search.
147-
fputcsv($fp, array_merge($fields, [$data]));
148161
}
149162
}
150163
}

0 commit comments

Comments
 (0)