Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Config.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace Friendica\Core\Console;
  3. use Asika\SimpleConsole\CommandArgsException;
  4. use Friendica\App;
  5. use Friendica\Core;
  6. use RuntimeException;
  7. /**
  8. * @brief tool to access the system config from the CLI
  9. *
  10. * With this script you can access the system configuration of your node from
  11. * the CLI. You can do both, reading current values stored in the database and
  12. * set new values to config variables.
  13. *
  14. * Usage:
  15. * If you specify no parameters at the CLI, the script will list all config
  16. * variables defined.
  17. *
  18. * If you specify one parameter, the script will list all config variables
  19. * defined in this section of the configuration (e.g. "system").
  20. *
  21. * If you specify two parameters, the script will show you the current value
  22. * of the named configuration setting. (e.g. "system loglevel")
  23. *
  24. * If you specify three parameters, the named configuration setting will be
  25. * set to the value of the last parameter. (e.g. "system loglevel 0" will
  26. * disable logging)
  27. *
  28. * @author Tobias Diekershoff <tobias.diekershoff@gmx.net>
  29. * @author Hypolite Petovan <hypolite@mrpetovan.com>
  30. */
  31. class Config extends \Asika\SimpleConsole\Console
  32. {
  33. protected $helpOptions = ['h', 'help', '?'];
  34. protected function getHelp()
  35. {
  36. $help = <<<HELP
  37. console config - Manage site configuration
  38. Synopsis
  39. bin/console config [-h|--help|-?] [-v]
  40. bin/console config <category> [-h|--help|-?] [-v]
  41. bin/console config <category> <key> [-h|--help|-?] [-v]
  42. bin/console config <category> <key> <value> [-h|--help|-?] [-v]
  43. Description
  44. bin/console config
  45. Lists all config values
  46. bin/console config <category>
  47. Lists all config values in the provided category
  48. bin/console config <category> <key>
  49. Shows the value of the provided key in the category
  50. bin/console config <category> <key> <value>
  51. Sets the value of the provided key in the category
  52. Notes:
  53. Setting config entries which are manually set in config/local.config.php may result in
  54. conflict between database settings and the manual startup settings.
  55. Options
  56. -h|--help|-? Show help information
  57. -v Show more debug information.
  58. HELP;
  59. return $help;
  60. }
  61. protected function doExecute()
  62. {
  63. $a = \Friendica\BaseObject::getApp();
  64. if ($this->getOption('v')) {
  65. $this->out('Executable: ' . $this->executable);
  66. $this->out('Class: ' . __CLASS__);
  67. $this->out('Arguments: ' . var_export($this->args, true));
  68. $this->out('Options: ' . var_export($this->options, true));
  69. }
  70. if (count($this->args) > 3) {
  71. throw new CommandArgsException('Too many arguments');
  72. }
  73. if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  74. $this->out('Database isn\'t ready or populated yet, showing file config only');
  75. }
  76. if (count($this->args) == 3) {
  77. $cat = $this->getArgument(0);
  78. $key = $this->getArgument(1);
  79. $value = $this->getArgument(2);
  80. if (is_array(Core\Config::get($cat, $key))) {
  81. throw new RuntimeException("$cat.$key is an array and can't be set using this command.");
  82. }
  83. $result = Core\Config::set($cat, $key, $value);
  84. if ($result) {
  85. $this->out("{$cat}.{$key} <= " .
  86. Core\Config::get($cat, $key));
  87. } else {
  88. $this->out("Unable to set {$cat}.{$key}");
  89. }
  90. }
  91. if (count($this->args) == 2) {
  92. $cat = $this->getArgument(0);
  93. $key = $this->getArgument(1);
  94. $value = Core\Config::get($this->getArgument(0), $this->getArgument(1));
  95. if (is_array($value)) {
  96. foreach ($value as $k => $v) {
  97. $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
  98. }
  99. } else {
  100. $this->out("{$cat}.{$key} => " . $value);
  101. }
  102. }
  103. if (count($this->args) == 1) {
  104. $cat = $this->getArgument(0);
  105. Core\Config::load($cat);
  106. if ($a->getConfigCache()->get($cat) !== null) {
  107. $this->out("[{$cat}]");
  108. $catVal = $a->getConfigCache()->get($cat);
  109. foreach ($catVal as $key => $value) {
  110. if (is_array($value)) {
  111. foreach ($value as $k => $v) {
  112. $this->out("{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
  113. }
  114. } else {
  115. $this->out("{$key} => " . $value);
  116. }
  117. }
  118. } else {
  119. $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
  120. }
  121. }
  122. if (count($this->args) == 0) {
  123. Core\Config::load();
  124. if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  125. $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
  126. }
  127. $config = $a->getConfigCache()->getAll();
  128. foreach ($config as $cat => $section) {
  129. if (is_array($section)) {
  130. foreach ($section as $key => $value) {
  131. if (is_array($value)) {
  132. foreach ($value as $k => $v) {
  133. $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
  134. }
  135. } else {
  136. $this->out("{$cat}.{$key} => " . $value);
  137. }
  138. }
  139. } else {
  140. $this->out("config.{$cat} => " . $section);
  141. }
  142. }
  143. }
  144. return 0;
  145. }
  146. }