Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
MDL-25763 tool_replace: cli version
Browse files Browse the repository at this point in the history
  • Loading branch information
usqfowlerj authored and David Monllao committed Nov 21, 2014
1 parent c106341 commit ee4b11e
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
91 changes: 91 additions & 0 deletions admin/tool/replace/cli/replace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?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 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);
3 changes: 3 additions & 0 deletions admin/tool/replace/lang/en/tool_replace.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
$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.';
Expand Down

0 comments on commit ee4b11e

Please sign in to comment.