MDL-69746 tool_replace: additional skip tables
[moodle.git] / admin / tool / replace / cli / replace.php
CommitLineData
ee4b11e0
JF
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Search and replace strings throughout all texts in the whole database.
19 *
20 * @package tool_replace
21 * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25define('CLI_SCRIPT', true);
26
27require(__DIR__.'/../../../../config.php');
28require_once($CFG->libdir.'/clilib.php');
29require_once($CFG->libdir.'/adminlib.php');
30
31$help =
32 "Search and replace text throughout the whole database.
33
34Options:
35--search=STRING String to search for.
36--replace=STRING String to replace with.
6ced7617 37--skiptables=STRING Skip these tables (comma separated list of tables).
ee4b11e0
JF
38--shorten Shorten result if necessary.
39--non-interactive Perform the replacement without confirming.
40-h, --help Print out this help.
41
42Example:
43\$ sudo -u www-data /usr/bin/php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost
44";
45
46list($options, $unrecognized) = cli_get_params(
47 array(
48 'search' => null,
49 'replace' => null,
6ced7617 50 'skiptables' => '',
ee4b11e0
JF
51 'shorten' => false,
52 'non-interactive' => false,
53 'help' => false,
54 ),
55 array(
56 'h' => 'help',
57 )
58);
59
60if ($options['help'] || $options['search'] === null || $options['replace'] === null) {
61 echo $help;
62 exit(0);
63}
64
65if (!$DB->replace_all_text_supported()) {
66 cli_error(get_string('notimplemented', 'tool_replace'));
67}
68
69if (empty($options['shorten']) && core_text::strlen($options['search']) < core_text::strlen($options['replace'])) {
70 cli_error(get_string('cannotfit', 'tool_replace'));
71}
72
73try {
74 $search = validate_param($options['search'], PARAM_RAW);
75 $replace = validate_param($options['replace'], PARAM_RAW);
6ced7617 76 $skiptables = validate_param($options['skiptables'], PARAM_RAW);
ee4b11e0 77} catch (invalid_parameter_exception $e) {
06bb6478 78 cli_error(get_string('invalidcharacter', 'tool_replace'));
ee4b11e0
JF
79}
80
81if (!$options['non-interactive']) {
82 echo get_string('excludedtables', 'tool_replace') . "\n\n";
06bb6478 83 echo get_string('notsupported', 'tool_replace') . "\n\n";
ee4b11e0
JF
84 $prompt = get_string('cliyesnoprompt', 'admin');
85 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
86 if ($input == get_string('clianswerno', 'admin')) {
87 exit(1);
88 }
89}
90
6ced7617 91if (!db_replace($search, $replace, $skiptables)) {
06bb6478
DM
92 cli_heading(get_string('error'));
93 exit(1);
94}
ee4b11e0
JF
95
96cli_heading(get_string('success'));
97exit(0);