weekly release 4.0dev
[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.
37--shorten Shorten result if necessary.
38--non-interactive Perform the replacement without confirming.
39-h, --help Print out this help.
40
41Example:
42\$ sudo -u www-data /usr/bin/php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost
43";
44
45list($options, $unrecognized) = cli_get_params(
46 array(
47 'search' => null,
48 'replace' => null,
49 'shorten' => false,
50 'non-interactive' => false,
51 'help' => false,
52 ),
53 array(
54 'h' => 'help',
55 )
56);
57
58if ($options['help'] || $options['search'] === null || $options['replace'] === null) {
59 echo $help;
60 exit(0);
61}
62
63if (!$DB->replace_all_text_supported()) {
64 cli_error(get_string('notimplemented', 'tool_replace'));
65}
66
67if (empty($options['shorten']) && core_text::strlen($options['search']) < core_text::strlen($options['replace'])) {
68 cli_error(get_string('cannotfit', 'tool_replace'));
69}
70
71try {
72 $search = validate_param($options['search'], PARAM_RAW);
73 $replace = validate_param($options['replace'], PARAM_RAW);
74} catch (invalid_parameter_exception $e) {
06bb6478 75 cli_error(get_string('invalidcharacter', 'tool_replace'));
ee4b11e0
JF
76}
77
78if (!$options['non-interactive']) {
79 echo get_string('excludedtables', 'tool_replace') . "\n\n";
06bb6478 80 echo get_string('notsupported', 'tool_replace') . "\n\n";
ee4b11e0
JF
81 $prompt = get_string('cliyesnoprompt', 'admin');
82 $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
83 if ($input == get_string('clianswerno', 'admin')) {
84 exit(1);
85 }
86}
87
06bb6478
DM
88if (!db_replace($search, $replace)) {
89 cli_heading(get_string('error'));
90 exit(1);
91}
ee4b11e0
JF
92
93cli_heading(get_string('success'));
94exit(0);