MDL-64506 phpunit: Update unit tests to use classic
[moodle.git] / admin / cli / cfg.php
CommitLineData
dfefcef2
DM
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
29define('CLI_SCRIPT', true);
30
31require(__DIR__.'/../../config.php');
32require_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
36Usage:
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
43Options:
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
54The list of all variables of the given component can be printed as
55tab-separated list (default) or JSON object (--json). Particular values are
56printed as raw text values, optionally escaped so that they can be directly
57used as shell script arguments (--shell-arg). Single values are displayed with
58trailing new line by default, unless explicitly disabled (--no-eol).
59
60In the read mode, the script exits with success status 0 if the requested value
61is found. If the requested variable is not set, the script exits with status 3.
62When listing all variables of the component, the exit status is always 0 even
63if no variables for the given component are found. When setting/unsetting a
00eeb686
DM
64value, the exit status is 0. When attempting to set/unset a value that has
65already been hard-set in config.php, the script exits with error status 4. In
66case of unexpected error, the script exits with error status 1.
dfefcef2
DM
67
68Examples:
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
e00f1c66 86 # php cfg.php --name=theme --set=classic
dfefcef2
DM
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
93list($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
106if ($unrecognised) {
107 $unrecognised = implode(PHP_EOL.' ', $unrecognised);
108 cli_error(get_string('cliunknowoption', 'core_admin', $unrecognised));
109}
110
111if ($options['help']) {
112 cli_writeln($usage);
113 exit(2);
114}
115
116if ($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 }
00eeb686
DM
121
122 // Check that the variable is not hard-set in the main config.php already.
123 if (array_key_exists($options['name'], $CFG->config_php_settings)) {
124 cli_error('The configuration variable is hard-set in the config.php, unable to change.', 4);
125 }
126
dfefcef2
DM
127 set_config($options['name'], $options['set'], $options['component']);
128 exit(0);
129}
130
131if ($options['name'] === null) {
132 // List all variables provided by the component (defaults to core).
133 $got = get_config($options['component']);
134
135 if ($options['json']) {
136 cli_writeln(json_encode($got));
137
138 } else {
139 foreach ($got as $name => $value) {
140 if ($options['shell-arg']) {
141 $value = escapeshellarg($value);
142 }
143 cli_writeln($name."\t".$value);
144 }
145 }
146
147 exit(0);
148
149} else {
150 // Display the value of a single variable.
151
152 $got = get_config($options['component'], $options['name']);
153
154 if ($got === false) {
155 cli_error('No such configuration variable found.', 3);
156 }
157
158 if ($options['shell-arg']) {
159 $got = escapeshellarg($got);
160 }
161
162 if ($options['json']) {
163 $got = json_encode($got);
164 }
165
166 if ($options['no-eol']) {
167 cli_write($got);
168 } else {
169 cli_writeln($got);
170 }
171
172 exit(0);
173}