|
36 | 36 | Options:
|
37 | 37 | --search=STRING String to search for.
|
38 | 38 | --regex-match=STRING Use regular expression to match the search string.
|
| 39 | +--output=FILE Output file. If not specified, output to stdout. |
39 | 40 | --tables=tablename:columnname Tables and columns to search. Separate multiple tables/columns with a comma.
|
40 | 41 | If not specified, search all tables and columns.
|
41 | 42 | If specify table only, search all columns in the table.
|
|
56 | 57 | [
|
57 | 58 | 'search' => null,
|
58 | 59 | 'regex-match' => null,
|
| 60 | + 'output' => null, |
59 | 61 | 'tables' => '',
|
60 | 62 | 'summary' => false,
|
61 | 63 | 'help' => false,
|
|
92 | 94 | cli_error(get_string('invalidcharacter', 'tool_advancedreplace'));
|
93 | 95 | }
|
94 | 96 |
|
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 |
| - |
104 | 97 | // 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 | +} |
106 | 103 |
|
107 | 104 | // Show header.
|
108 | 105 | if (!$options['summary']) {
|
|
111 | 108 | fputcsv($fp, ['Table', 'Column', 'courseid', 'idnumber']);
|
112 | 109 | }
|
113 | 110 |
|
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 | + } |
143 | 156 | }
|
| 157 | + } else { |
| 158 | + // Show the result for simple plain text search. |
| 159 | + fputcsv($fp, array_merge($fields, [$data])); |
144 | 160 | }
|
145 |
| - } else { |
146 |
| - // Show the result for simple plain text search. |
147 |
| - fputcsv($fp, array_merge($fields, [$data])); |
148 | 161 | }
|
149 | 162 | }
|
150 | 163 | }
|
|
0 commit comments