Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/admin/cli/ -> cfg.php (source)

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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      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  
 127      $new = $options['set'];
 128      $old = get_config($options['component'], $options['name']);
 129      if ($new !== $old) {
 130          set_config($options['name'], $options['set'], $options['component']);
 131          add_to_config_log($options['name'], $old, $new, $options['component']);
 132      }
 133      exit(0);
 134  }
 135  
 136  if ($options['name'] === null) {
 137      // List all variables provided by the component (defaults to core).
 138      $got = get_config($options['component']);
 139  
 140      if ($options['json']) {
 141          cli_writeln(json_encode($got));
 142  
 143      } else {
 144          foreach ($got as $name => $value) {
 145              if ($options['shell-arg']) {
 146                  $value = escapeshellarg($value);
 147              }
 148              cli_writeln($name."\t".$value);
 149          }
 150      }
 151  
 152      exit(0);
 153  
 154  } else {
 155      // Display the value of a single variable.
 156  
 157      $got = get_config($options['component'], $options['name']);
 158  
 159      if ($got === false) {
 160          cli_error('No such configuration variable found.', 3);
 161      }
 162  
 163      if ($options['shell-arg']) {
 164          $got = escapeshellarg($got);
 165      }
 166  
 167      if ($options['json']) {
 168          $got = json_encode($got);
 169      }
 170  
 171      if ($options['no-eol']) {
 172          cli_write($got);
 173      } else {
 174          cli_writeln($got);
 175      }
 176  
 177      exit(0);
 178  }