From ee4b11e0a6e5db8781605158b4563dab9d2dc30f Mon Sep 17 00:00:00 2001 From: Jonathon Fowler Date: Thu, 9 Oct 2014 08:54:38 +1000 Subject: [PATCH 1/2] MDL-25763 tool_replace: cli version --- admin/tool/replace/cli/replace.php | 91 +++++++++++++++++++++ admin/tool/replace/lang/en/tool_replace.php | 3 + 2 files changed, 94 insertions(+) create mode 100644 admin/tool/replace/cli/replace.php diff --git a/admin/tool/replace/cli/replace.php b/admin/tool/replace/cli/replace.php new file mode 100644 index 00000000000..3d6d991f032 --- /dev/null +++ b/admin/tool/replace/cli/replace.php @@ -0,0 +1,91 @@ +. + +/** + * Search and replace strings throughout all texts in the whole database. + * + * @package tool_replace + * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) + * @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 and replace text throughout the whole database. + +Options: +--search=STRING String to search for. +--replace=STRING String to replace with. +--shorten Shorten result if necessary. +--non-interactive Perform the replacement without confirming. +-h, --help Print out this help. + +Example: +\$ sudo -u www-data /usr/bin/php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost +"; + +list($options, $unrecognized) = cli_get_params( + array( + 'search' => null, + 'replace' => null, + 'shorten' => false, + 'non-interactive' => false, + 'help' => false, + ), + array( + 'h' => 'help', + ) +); + +if ($options['help'] || $options['search'] === null || $options['replace'] === null) { + echo $help; + exit(0); +} + +if (!$DB->replace_all_text_supported()) { + cli_error(get_string('notimplemented', 'tool_replace')); +} + +if (empty($options['shorten']) && core_text::strlen($options['search']) < core_text::strlen($options['replace'])) { + cli_error(get_string('cannotfit', 'tool_replace')); +} + +try { + $search = validate_param($options['search'], PARAM_RAW); + $replace = validate_param($options['replace'], PARAM_RAW); +} catch (invalid_parameter_exception $e) { + cli_error(get_string('clibadparametertext', 'tool_replace')); +} + +if (!$options['non-interactive']) { + echo get_string('excludedtables', 'tool_replace') . "\n\n"; + echo get_string('cliconfirmation', 'tool_replace') . "\n\n"; + $prompt = get_string('cliyesnoprompt', 'admin'); + $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin'))); + if ($input == get_string('clianswerno', 'admin')) { + exit(1); + } +} + +db_replace($search, $replace); + +cli_heading(get_string('success')); +exit(0); diff --git a/admin/tool/replace/lang/en/tool_replace.php b/admin/tool/replace/lang/en/tool_replace.php index 919ed67ae0f..6300943d573 100644 --- a/admin/tool/replace/lang/en/tool_replace.php +++ b/admin/tool/replace/lang/en/tool_replace.php @@ -28,6 +28,9 @@ $string['disclaimer'] = 'I understand the risks of this operation'; $string['doit'] = 'Yes, do it!'; $string['excludedtables'] = 'Several tables are not updated as part of the text replacement. This include configuration, log, events, and session tables.'; $string['pageheader'] = 'Search and replace text throughout the whole database'; +$string['clibadparametertext'] = 'Invalid characters were found in the search or replacement text. Aborting.'; +$string['cliconfirmation'] = 'This script is not supported and the operation cannot be reverted, so always make a complete backup before proceeding. +Are you sure you want to begin?'; $string['notifyfinished'] = '...finished'; $string['notifyrebuilding'] = 'Rebuilding course cache...'; $string['notimplemented'] = 'Sorry, this feature is not implemented in your database driver.'; From 06bb6478fd138d747a077fe92a8c69284d4380f8 Mon Sep 17 00:00:00 2001 From: David Monllao Date: Fri, 21 Nov 2014 10:40:06 +0800 Subject: [PATCH 2/2] MDL-25763 tool_replace: Returning correct error code --- admin/tool/replace/cli/replace.php | 9 ++++++--- admin/tool/replace/lang/en/tool_replace.php | 8 +++----- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/admin/tool/replace/cli/replace.php b/admin/tool/replace/cli/replace.php index 3d6d991f032..1f9e74bce76 100644 --- a/admin/tool/replace/cli/replace.php +++ b/admin/tool/replace/cli/replace.php @@ -72,12 +72,12 @@ try { $search = validate_param($options['search'], PARAM_RAW); $replace = validate_param($options['replace'], PARAM_RAW); } catch (invalid_parameter_exception $e) { - cli_error(get_string('clibadparametertext', 'tool_replace')); + cli_error(get_string('invalidcharacter', 'tool_replace')); } if (!$options['non-interactive']) { echo get_string('excludedtables', 'tool_replace') . "\n\n"; - echo get_string('cliconfirmation', 'tool_replace') . "\n\n"; + echo get_string('notsupported', 'tool_replace') . "\n\n"; $prompt = get_string('cliyesnoprompt', 'admin'); $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin'))); if ($input == get_string('clianswerno', 'admin')) { @@ -85,7 +85,10 @@ if (!$options['non-interactive']) { } } -db_replace($search, $replace); +if (!db_replace($search, $replace)) { + cli_heading(get_string('error')); + exit(1); +} cli_heading(get_string('success')); exit(0); diff --git a/admin/tool/replace/lang/en/tool_replace.php b/admin/tool/replace/lang/en/tool_replace.php index 6300943d573..481fd0681cd 100644 --- a/admin/tool/replace/lang/en/tool_replace.php +++ b/admin/tool/replace/lang/en/tool_replace.php @@ -27,14 +27,12 @@ $string['cannotfit'] = 'The replacement is longer than original and shortening i $string['disclaimer'] = 'I understand the risks of this operation'; $string['doit'] = 'Yes, do it!'; $string['excludedtables'] = 'Several tables are not updated as part of the text replacement. This include configuration, log, events, and session tables.'; -$string['pageheader'] = 'Search and replace text throughout the whole database'; -$string['clibadparametertext'] = 'Invalid characters were found in the search or replacement text. Aborting.'; -$string['cliconfirmation'] = 'This script is not supported and the operation cannot be reverted, so always make a complete backup before proceeding. -Are you sure you want to begin?'; +$string['invalidcharacter'] = 'Invalid characters were found in the search or replacement text.'; $string['notifyfinished'] = '...finished'; $string['notifyrebuilding'] = 'Rebuilding course cache...'; $string['notimplemented'] = 'Sorry, this feature is not implemented in your database driver.'; -$string['notsupported'] ='This script is not supported, always make complete backup before proceeding!
This operation can not be reverted!'; +$string['notsupported'] = 'This script should be considered experimental and the changes it makes can not be reverted. Please make a complete backup for running this script!'; +$string['pageheader'] = 'Search and replace text throughout the whole database'; $string['pluginname'] = 'DB search and replace'; $string['replacewith'] = 'Replace with this string'; $string['replacewithhelp'] = 'usually new server URL';