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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.ini.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}] => " . $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 (!is_null($a->config[$cat])) {
  107. $this->out("[{$cat}]");
  108. foreach ($a->config[$cat] as $key => $value) {
  109. if (is_array($value)) {
  110. foreach ($value as $k => $v) {
  111. $this->out("{$key}[{$k}] => " . $v);
  112. }
  113. } else {
  114. $this->out("{$key} => " . $value);
  115. }
  116. }
  117. } else {
  118. $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
  119. }
  120. }
  121. if (count($this->args) == 0) {
  122. Core\Config::load();
  123. if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
  124. $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
  125. }
  126. foreach ($a->config as $cat => $section) {
  127. if (is_array($section)) {
  128. foreach ($section as $key => $value) {
  129. if (is_array($value)) {
  130. foreach ($value as $k => $v) {
  131. $this->out("{$cat}.{$key}[{$k}] => " . $v);
  132. }
  133. } else {
  134. $this->out("{$cat}.{$key} => " . $value);
  135. }
  136. }
  137. } else {
  138. $this->out("config.{$cat} => " . $section);
  139. }
  140. }
  141. }
  142. return 0;
  143. }
  144. }