Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * CLI script allowing to get and set config values.
19
 *
20
 * This is technically just a thin wrapper for {@link get_config()} and
21
 * {@link set_config()} functions.
22
 *
23
 * @package     core
24
 * @subpackage  cli
25
 * @copyright   2017 David Mudrák <david@moodle.com>
26
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
 
29
define('CLI_SCRIPT', true);
30
 
31
require(__DIR__.'/../../config.php');
32
require_once($CFG->libdir.'/clilib.php');
33
 
34
$usage = "Displays the current value of the given site setting. Allows to set it to the given value, too.
35
 
36
Usage:
37
    # php cfg.php [--component=<componentname>] [--json] [--shell-arg]
38
    # php cfg.php --name=<configname> [--component=<componentname>] [--shell-arg] [--no-eol]
39
    # php cfg.php --name=<configname> [--component=<componentname>] --set=<value>
40
    # php cfg.php --name=<configname> [--component=<componentname>] --unset
41
    # php cfg.php [--help|-h]
42
 
43
Options:
44
    -h --help                   Print this help.
45
    --component=<frankenstyle>  Name of the component the variable is part of. Defaults to core.
46
    --name=<configname>         Name of the configuration variable to get/set. If missing, print all
47
                                configuration variables of the given component.
48
    --set=<value>               Set the given variable to this value.
49
    --unset                     Unset the given variable.
50
    --shell-arg                 Escape output values so that they can be directly used as shell script arguments.
51
    --json                      Encode output list of values using JSON notation.
52
    --no-eol                    Do not include the trailing new line character when printing the value.
53
 
54
The list of all variables of the given component can be printed as
55
tab-separated list (default) or JSON object (--json). Particular values are
56
printed as raw text values, optionally escaped so that they can be directly
57
used as shell script arguments (--shell-arg). Single values are displayed with
58
trailing new line by default, unless explicitly disabled (--no-eol).
59
 
60
In the read mode, the script exits with success status 0 if the requested value
61
is found. If the requested variable is not set, the script exits with status 3.
62
When listing all variables of the component, the exit status is always 0 even
63
if no variables for the given component are found. When setting/unsetting a
64
value, the exit status is 0. When attempting to set/unset a value that has
65
already been hard-set in config.php, the script exits with error status 4. In
66
case of unexpected error, the script exits with error status 1.
67
 
68
Examples:
69
 
70
    # php cfg.php
71
        Prints tab-separated list of all core configuration variables and their values.
72
 
73
    # php cfg.php --json
74
        Prints list of all core configuration variables and their values as a JSON object.
75
 
76
    # php cfg.php --name=release
77
        Prints the given configuration variable - e.g. \$CFG->release in this case.
78
 
79
    # php cfg.php --component=tool_recyclebin
80
    #   Prints tab-separated list of the plugin's configuration variables.
81
 
82
    # export DATAROOT=\$(php cfg.php --name=dataroot --shell-arg --no-eol)
83
        Stores the given configuration variable in the shell variable, escaped
84
        so that it can be safely used as a shell argument.
85
 
86
    # php cfg.php --name=theme --set=classic
87
        Sets the given configuration variable to the given value.
88
 
89
    # php cfg.php --name=noemailever --unset
90
        Unsets the previously configured variable.
91
";
92
 
93
list($options, $unrecognised) = cli_get_params([
94
    'help' => false,
95
    'component' => null,
96
    'name' => null,
97
    'set' => null,
98
    'unset' => false,
99
    'shell-arg' => false,
100
    'json' => false,
101
    'no-eol' => false,
102
], [
103
    'h' => 'help'
104
]);
105
 
106
if ($unrecognised) {
107
    $unrecognised = implode(PHP_EOL.'  ', $unrecognised);
108
    cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
109
}
110
 
111
if ($options['help']) {
112
    cli_writeln($usage);
113
    exit(2);
114
}
115
 
116
if ($options['unset'] || $options['set'] !== null) {
117
    // Unset the variable or set it to the given value.
118
    if (empty($options['name'])) {
119
        cli_error('Missing configuration variable name', 2);
120
    }
121
 
122
    // Check that the variable is not hard-set in the main config.php already.
123
    $component = $options['component'];
124
    if (!empty($component)) {
125
        $componentsettings = $CFG->forced_plugin_settings[$component] ?? [];
126
        $settinginconfig = array_key_exists($options['name'], $componentsettings);
127
    } else {
128
        $settinginconfig = array_key_exists($options['name'], $CFG->config_php_settings);
129
    }
130
    if ($settinginconfig) {
131
        cli_error('The configuration variable is hard-set in the config.php, unable to change.', 4);
132
    }
133
 
134
    $new = $options['set'];
135
    $old = get_config($options['component'], $options['name']);
136
    if ($new !== $old) {
137
        set_config($options['name'], $options['set'], $options['component']);
138
        add_to_config_log($options['name'], $old, $new, $options['component']);
139
    }
140
    exit(0);
141
}
142
 
143
if ($options['name'] === null) {
144
    // List all variables provided by the component (defaults to core).
145
    $got = get_config($options['component']);
146
 
147
    if ($options['json']) {
148
        cli_writeln(json_encode($got));
149
 
150
    } else {
151
        foreach ($got as $name => $value) {
152
            if ($options['shell-arg']) {
153
                $value = escapeshellarg($value);
154
            }
155
            cli_writeln($name."\t".$value);
156
        }
157
    }
158
 
159
    exit(0);
160
 
161
} else {
162
    // Display the value of a single variable.
163
 
164
    $got = get_config($options['component'], $options['name']);
165
 
166
    if ($got === false) {
167
        cli_error('No such configuration variable found.', 3);
168
    }
169
 
170
    if ($options['shell-arg']) {
171
        $got = escapeshellarg($got);
172
    }
173
 
174
    if ($options['json']) {
175
        $got = json_encode($got);
176
    }
177
 
178
    if ($options['no-eol']) {
179
        cli_write($got);
180
    } else {
181
        cli_writeln($got);
182
    }
183
 
184
    exit(0);
185
}