Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Nguyen committed Sep 16, 2024
1 parent 09e56f2 commit ac669bc
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
93 changes: 93 additions & 0 deletions admin/tool/replace/cli/find.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Search strings throughout all texts in the whole database.
*
* @package tool_replace
* @copyright 2024 Catalyst IT Australia Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

define('CLI_SCRIPT', true);

require(__DIR__.'/../../../../config.php');
require_once($CFG->libdir.'/clilib.php');
require_once($CFG->libdir.'/adminlib.php');

$help =
"Search text throughout the whole database.
Options:
--search=STRING String to search for.
--skiptables=STRING Skip these tables (comma separated list of tables).
The
--summary Summary mode, only shows column/table where the text is found.
If not specified, run in detail mode, which shows the full text where the search string is found.
-h, --help Print out this help.
Example:
\$ sudo -u www-data /usr/bin/php admin/tool/replace/cli/find.php --search=thelostsoul --summary
";

list($options, $unrecognized) = cli_get_params(
array(
'search' => null,
'skiptables' => '',
'summary' => false,
'help' => false,
),
array(
'h' => 'help',
)
);

if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}

// Ensure that we have required parameters.
if ($options['help'] || !is_string($options['search'])) {
echo $help;
exit(0);
}

try {
$search = validate_param($options['search'], PARAM_RAW);
$skiptables = validate_param($options['skiptables'], PARAM_RAW);
} catch (invalid_parameter_exception $e) {
cli_error(get_string('invalidcharacter', 'tool_replace'));
}

// Perform the search.
$result = db_search($search, $skiptables, $options['summary']);

// Output the result.
foreach ($result as $table => $columns) {
foreach ($columns as $column => $rows) {
if ($options['summary']) {
echo "$table, $column\n";
} else {
foreach ($rows as $row) {
$data = $row->$column;
echo "$table, $column, \"$data\"\n";
}
}
}
}

exit(0);
46 changes: 46 additions & 0 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -9334,6 +9334,52 @@ function db_should_replace($table, $column = '', $additionalskiptables = ''): bo
return true;
}


/**
* Search for a string in the database.
*
* @param string $search the string to search for
* @param string $additionalskiptables additional tables to skip
* @param bool $summary if true, shown only the table and column names
* @return array|bool false if no tables, or an array of results
*/
function db_search(string $search, string $additionalskiptables = '', bool $summary = false) {
global $DB;

if (!$tables = $DB->get_tables() ) { // No tables yet at all.
return false;
}

// If we are doing a summary, we only want to return the first match.
$limit = $summary ? 1 : 0;

$result = [];
foreach ($tables as $table) {
// TODO: db_should_replace can be refactored/reused here.
if (!db_should_replace($table, '', $additionalskiptables)) {
continue;
}

if ($columns = $DB->get_columns($table)) {
foreach ($columns as $column) {
if (!db_should_replace($table, $column->name)) {
continue;
}

if ($match = $DB->search_all_text($table, $column, $search, $limit)) {
if ($summary) {
$result[$table][$column->name] = [];
} else {
$result[$table][$column->name] = $match;
}
}
}
}
}

return $result;
}

/**
* Moved from admin/replace.php so that we can use this in cron
*
Expand Down
27 changes: 27 additions & 0 deletions lib/dml/moodle_database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2585,6 +2585,33 @@ public function sql_intersect($selects, $fields) {
return $rv;
}

/**
* SQL to get the current timestamp.
*
* @param string $table The table name.
* @param database_column_info $column The column info.
* @param string $search The search string.
* @param int $limit number of records to return.
* @return array|false
*/
public function search_all_text($table, database_column_info $column, $search, $limit = 0) {
// Enclose the column name by the proper quotes if it's a reserved word.
$columnname = $this->get_manager()->generator->getEncQuoted($column->name);

$searchsql = $this->sql_like($columnname, '?');
$searchparam = '%'.$this->sql_like_escape($search).'%';

$sql = "SELECT $columnname
FROM {".$table."}
WHERE $searchsql";

if ($column->meta_type === 'X' || $column->meta_type === 'C') {
return $this->get_records_sql($sql, array($searchparam), 0, $limit);
}

return false;
}

/**
* Does this driver support tool_replace?
*
Expand Down

0 comments on commit ac669bc

Please sign in to comment.